blob: 6f68daf9070999e58c0763f8c3f346c6f1a19fc6 [file] [log] [blame]
Hanno Beckerf65ca322017-10-10 14:44:57 +01001#!/bin/sh
Gilles Peskineafc4f892017-10-24 10:00:17 +02002# -*-sh-basic-offset: 4-*-
3# Usage: udp_proxy_wrapper.sh [PROXY_PARAM...] -- [SERVER_PARAM...]
Bence Szépkúti468a76f2020-05-26 00:33:31 +02004#
Bence Szépkútia2947ac2020-08-19 16:37:36 +02005# Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02006# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7#
8# This file is provided under the Apache License 2.0, or the
9# GNU General Public License v2.0 or later.
10#
11# **********
12# Apache License 2.0:
Bence Szépkúti51b41d52020-05-26 01:54:15 +020013#
14# Licensed under the Apache License, Version 2.0 (the "License"); you may
15# not use this file except in compliance with the License.
16# You may obtain a copy of the License at
17#
18# http://www.apache.org/licenses/LICENSE-2.0
19#
20# Unless required by applicable law or agreed to in writing, software
21# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23# See the License for the specific language governing permissions and
24# limitations under the License.
Bence Szépkúti468a76f2020-05-26 00:33:31 +020025#
Bence Szépkútif744bd72020-06-05 13:02:18 +020026# **********
27#
28# **********
29# GNU General Public License v2.0 or later:
30#
31# This program is free software; you can redistribute it and/or modify
32# it under the terms of the GNU General Public License as published by
33# the Free Software Foundation; either version 2 of the License, or
34# (at your option) any later version.
35#
36# This program is distributed in the hope that it will be useful,
37# but WITHOUT ANY WARRANTY; without even the implied warranty of
38# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39# GNU General Public License for more details.
40#
41# You should have received a copy of the GNU General Public License along
42# with this program; if not, write to the Free Software Foundation, Inc.,
43# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
44#
45# **********
Hanno Beckerf65ca322017-10-10 14:44:57 +010046
47set -u
48
Hanno Becker22829e92017-10-23 15:28:55 +010049MBEDTLS_BASE="$(dirname -- "$0")/../.."
50TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy"
Hanno Beckerf65ca322017-10-10 14:44:57 +010051SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
52
53: ${VERBOSE:=0}
Hanno Beckerf65ca322017-10-10 14:44:57 +010054
55stop_proxy() {
Gilles Peskineafc4f892017-10-24 10:00:17 +020056 if [ -n "${tpxy_pid:-}" ]; then
57 echo
58 echo " * Killing proxy (pid $tpxy_pid) ..."
59 kill $tpxy_pid
60 fi
Hanno Beckerf65ca322017-10-10 14:44:57 +010061}
62
63stop_server() {
Gilles Peskineafc4f892017-10-24 10:00:17 +020064 if [ -n "${srv_pid:-}" ]; then
65 echo
66 echo " * Killing server (pid $srv_pid) ..."
67 kill $srv_pid >/dev/null 2>/dev/null
68 fi
Hanno Beckerf65ca322017-10-10 14:44:57 +010069}
70
71cleanup() {
72 stop_server
73 stop_proxy
Gilles Peskine81493212017-10-24 12:22:40 +020074 exit 129
Hanno Beckerf65ca322017-10-10 14:44:57 +010075}
76
77trap cleanup INT TERM HUP
78
Gilles Peskineafc4f892017-10-24 10:00:17 +020079# Extract the proxy parameters
80tpxy_cmd_snippet='"$TPXY_BIN"'
81while [ $# -ne 0 ] && [ "$1" != "--" ]; do
82 tail="$1" quoted=""
83 while [ -n "$tail" ]; do
84 case "$tail" in
85 *\'*) quoted="${quoted}${tail%%\'*}'\\''" tail="${tail#*\'}";;
86 *) quoted="${quoted}${tail}"; tail=; false;;
87 esac
88 done
89 tpxy_cmd_snippet="$tpxy_cmd_snippet '$quoted'"
90 shift
91done
92unset tail quoted
93if [ $# -eq 0 ]; then
94 echo " * No server arguments (must be preceded by \" -- \") - exit"
95 exit 3
96fi
97shift
Hanno Beckera677cdd2017-10-23 15:29:31 +010098
Gilles Peskineafc4f892017-10-24 10:00:17 +020099dtls_enabled=
100ipv6_in_use=
101server_port_orig=
102server_addr_orig=
103for param; do
104 case "$param" in
105 server_port=*) server_port_orig="${param#*=}";;
106 server_addr=*:*) server_addr_orig="${param#*=}"; ipv6_in_use=1;;
107 server_addr=*) server_addr_orig="${param#*=}";;
108 dtls=[!0]*) dtls_enabled=1;;
109 esac
110done
111
112if [ -z "$dtls_enabled" ] || [ -n "$ipv6_in_use" ]; then
113 echo >&2 "$0: Couldn't find DTLS enabling, or IPv6 is in use - immediate fallback to server application..."
Hanno Beckerf65ca322017-10-10 14:44:57 +0100114 if [ $VERBOSE -gt 0 ]; then
Gilles Peskineafc4f892017-10-24 10:00:17 +0200115 echo "[ $SRV_BIN $* ]"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100116 fi
Gilles Peskineafc4f892017-10-24 10:00:17 +0200117 exec "$SRV_BIN" "$@"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100118fi
119
Gilles Peskineafc4f892017-10-24 10:00:17 +0200120if [ -z "$server_port_orig" ]; then
121 server_port_orig=4433
122fi
123echo " * Server port: $server_port_orig"
124tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\""
125tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\""
126
127if [ -n "$server_addr_orig" ]; then
128 echo " * Server address: $server_addr_orig"
129 tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_addr=\$server_addr_orig\""
130 tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_addr=\$server_addr_orig\""
Hanno Beckerf65ca322017-10-10 14:44:57 +0100131fi
132
Gilles Peskineafc4f892017-10-24 10:00:17 +0200133server_port=$(( server_port_orig + 1 ))
134set -- "$@" "server_port=$server_port"
135echo " * Intermediate port: $server_port"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100136
137echo " * Start proxy in background ..."
138if [ $VERBOSE -gt 0 ]; then
Gilles Peskineafc4f892017-10-24 10:00:17 +0200139 echo "[ $tpxy_cmd_snippet ]"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100140fi
Gilles Peskine81493212017-10-24 12:22:40 +0200141eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 &
Gilles Peskineafc4f892017-10-24 10:00:17 +0200142tpxy_pid=$!
Hanno Beckerf65ca322017-10-10 14:44:57 +0100143
144if [ $VERBOSE -gt 0 ]; then
145 echo " * Proxy ID: $TPXY_PID"
146fi
147
Hanno Beckerf65ca322017-10-10 14:44:57 +0100148echo " * Starting server ..."
149if [ $VERBOSE -gt 0 ]; then
Gilles Peskineafc4f892017-10-24 10:00:17 +0200150 echo "[ $SRV_BIN $* ]"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100151fi
152
Gilles Peskine81493212017-10-24 12:22:40 +0200153exec "$SRV_BIN" "$@" >&2 &
Gilles Peskineafc4f892017-10-24 10:00:17 +0200154srv_pid=$!
Hanno Beckerf65ca322017-10-10 14:44:57 +0100155
Gilles Peskineafc4f892017-10-24 10:00:17 +0200156wait $srv_pid
Hanno Beckerf65ca322017-10-10 14:44:57 +0100157
158stop_proxy
159return 0