blob: 1d87408c3339eaf9521195fde82c13ba6396f7e9 [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#
5# Copyright (C) 2017, Arm Limited, All Rights Reserved
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# **********
46#
Bence Szépkúti468a76f2020-05-26 00:33:31 +020047# This file is part of Mbed TLS (https://tls.mbed.org)
Hanno Beckerf65ca322017-10-10 14:44:57 +010048
49set -u
50
Hanno Becker22829e92017-10-23 15:28:55 +010051MBEDTLS_BASE="$(dirname -- "$0")/../.."
52TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy"
Hanno Beckerf65ca322017-10-10 14:44:57 +010053SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2"
54
55: ${VERBOSE:=0}
Hanno Beckerf65ca322017-10-10 14:44:57 +010056
57stop_proxy() {
Gilles Peskineafc4f892017-10-24 10:00:17 +020058 if [ -n "${tpxy_pid:-}" ]; then
59 echo
60 echo " * Killing proxy (pid $tpxy_pid) ..."
61 kill $tpxy_pid
62 fi
Hanno Beckerf65ca322017-10-10 14:44:57 +010063}
64
65stop_server() {
Gilles Peskineafc4f892017-10-24 10:00:17 +020066 if [ -n "${srv_pid:-}" ]; then
67 echo
68 echo " * Killing server (pid $srv_pid) ..."
69 kill $srv_pid >/dev/null 2>/dev/null
70 fi
Hanno Beckerf65ca322017-10-10 14:44:57 +010071}
72
73cleanup() {
74 stop_server
75 stop_proxy
Gilles Peskine81493212017-10-24 12:22:40 +020076 exit 129
Hanno Beckerf65ca322017-10-10 14:44:57 +010077}
78
79trap cleanup INT TERM HUP
80
Gilles Peskineafc4f892017-10-24 10:00:17 +020081# Extract the proxy parameters
82tpxy_cmd_snippet='"$TPXY_BIN"'
83while [ $# -ne 0 ] && [ "$1" != "--" ]; do
84 tail="$1" quoted=""
85 while [ -n "$tail" ]; do
86 case "$tail" in
87 *\'*) quoted="${quoted}${tail%%\'*}'\\''" tail="${tail#*\'}";;
88 *) quoted="${quoted}${tail}"; tail=; false;;
89 esac
90 done
91 tpxy_cmd_snippet="$tpxy_cmd_snippet '$quoted'"
92 shift
93done
94unset tail quoted
95if [ $# -eq 0 ]; then
96 echo " * No server arguments (must be preceded by \" -- \") - exit"
97 exit 3
98fi
99shift
Hanno Beckera677cdd2017-10-23 15:29:31 +0100100
Gilles Peskineafc4f892017-10-24 10:00:17 +0200101dtls_enabled=
102ipv6_in_use=
103server_port_orig=
104server_addr_orig=
105for param; do
106 case "$param" in
107 server_port=*) server_port_orig="${param#*=}";;
108 server_addr=*:*) server_addr_orig="${param#*=}"; ipv6_in_use=1;;
109 server_addr=*) server_addr_orig="${param#*=}";;
110 dtls=[!0]*) dtls_enabled=1;;
111 esac
112done
113
114if [ -z "$dtls_enabled" ] || [ -n "$ipv6_in_use" ]; then
115 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 +0100116 if [ $VERBOSE -gt 0 ]; then
Gilles Peskineafc4f892017-10-24 10:00:17 +0200117 echo "[ $SRV_BIN $* ]"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100118 fi
Gilles Peskineafc4f892017-10-24 10:00:17 +0200119 exec "$SRV_BIN" "$@"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100120fi
121
Gilles Peskineafc4f892017-10-24 10:00:17 +0200122if [ -z "$server_port_orig" ]; then
123 server_port_orig=4433
124fi
125echo " * Server port: $server_port_orig"
126tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\""
127tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\""
128
129if [ -n "$server_addr_orig" ]; then
130 echo " * Server address: $server_addr_orig"
131 tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_addr=\$server_addr_orig\""
132 tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_addr=\$server_addr_orig\""
Hanno Beckerf65ca322017-10-10 14:44:57 +0100133fi
134
Gilles Peskineafc4f892017-10-24 10:00:17 +0200135server_port=$(( server_port_orig + 1 ))
136set -- "$@" "server_port=$server_port"
137echo " * Intermediate port: $server_port"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100138
139echo " * Start proxy in background ..."
140if [ $VERBOSE -gt 0 ]; then
Gilles Peskineafc4f892017-10-24 10:00:17 +0200141 echo "[ $tpxy_cmd_snippet ]"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100142fi
Gilles Peskine81493212017-10-24 12:22:40 +0200143eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 &
Gilles Peskineafc4f892017-10-24 10:00:17 +0200144tpxy_pid=$!
Hanno Beckerf65ca322017-10-10 14:44:57 +0100145
146if [ $VERBOSE -gt 0 ]; then
147 echo " * Proxy ID: $TPXY_PID"
148fi
149
Hanno Beckerf65ca322017-10-10 14:44:57 +0100150echo " * Starting server ..."
151if [ $VERBOSE -gt 0 ]; then
Gilles Peskineafc4f892017-10-24 10:00:17 +0200152 echo "[ $SRV_BIN $* ]"
Hanno Beckerf65ca322017-10-10 14:44:57 +0100153fi
154
Gilles Peskine81493212017-10-24 12:22:40 +0200155exec "$SRV_BIN" "$@" >&2 &
Gilles Peskineafc4f892017-10-24 10:00:17 +0200156srv_pid=$!
Hanno Beckerf65ca322017-10-10 14:44:57 +0100157
Gilles Peskineafc4f892017-10-24 10:00:17 +0200158wait $srv_pid
Hanno Beckerf65ca322017-10-10 14:44:57 +0100159
160stop_proxy
161return 0