Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 1 | #!/bin/sh |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 2 | # -*-sh-basic-offset: 4-*- |
| 3 | # Usage: udp_proxy_wrapper.sh [PROXY_PARAM...] -- [SERVER_PARAM...] |
Bence Szépkúti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 4 | # |
| 5 | # Copyright (C) 2017, Arm Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 6 | # 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úti | 51b41d5 | 2020-05-26 01:54:15 +0200 | [diff] [blame] | 13 | # |
| 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úti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 25 | # |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 26 | # ********** |
| 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úti | 468a76f | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 47 | # This file is part of Mbed TLS (https://tls.mbed.org) |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 48 | |
| 49 | set -u |
| 50 | |
Hanno Becker | 22829e9 | 2017-10-23 15:28:55 +0100 | [diff] [blame] | 51 | MBEDTLS_BASE="$(dirname -- "$0")/../.." |
| 52 | TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy" |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 53 | SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2" |
| 54 | |
| 55 | : ${VERBOSE:=0} |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 56 | |
| 57 | stop_proxy() { |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 58 | if [ -n "${tpxy_pid:-}" ]; then |
| 59 | echo |
| 60 | echo " * Killing proxy (pid $tpxy_pid) ..." |
| 61 | kill $tpxy_pid |
| 62 | fi |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | stop_server() { |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 66 | 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 Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | cleanup() { |
| 74 | stop_server |
| 75 | stop_proxy |
Gilles Peskine | 8149321 | 2017-10-24 12:22:40 +0200 | [diff] [blame] | 76 | exit 129 |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | trap cleanup INT TERM HUP |
| 80 | |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 81 | # Extract the proxy parameters |
| 82 | tpxy_cmd_snippet='"$TPXY_BIN"' |
| 83 | while [ $# -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 |
| 93 | done |
| 94 | unset tail quoted |
| 95 | if [ $# -eq 0 ]; then |
| 96 | echo " * No server arguments (must be preceded by \" -- \") - exit" |
| 97 | exit 3 |
| 98 | fi |
| 99 | shift |
Hanno Becker | a677cdd | 2017-10-23 15:29:31 +0100 | [diff] [blame] | 100 | |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 101 | dtls_enabled= |
| 102 | ipv6_in_use= |
| 103 | server_port_orig= |
| 104 | server_addr_orig= |
| 105 | for 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 |
| 112 | done |
| 113 | |
| 114 | if [ -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 Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 116 | if [ $VERBOSE -gt 0 ]; then |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 117 | echo "[ $SRV_BIN $* ]" |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 118 | fi |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 119 | exec "$SRV_BIN" "$@" |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 120 | fi |
| 121 | |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 122 | if [ -z "$server_port_orig" ]; then |
| 123 | server_port_orig=4433 |
| 124 | fi |
| 125 | echo " * Server port: $server_port_orig" |
| 126 | tpxy_cmd_snippet="$tpxy_cmd_snippet \"listen_port=\$server_port_orig\"" |
| 127 | tpxy_cmd_snippet="$tpxy_cmd_snippet \"server_port=\$server_port\"" |
| 128 | |
| 129 | if [ -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 Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 133 | fi |
| 134 | |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 135 | server_port=$(( server_port_orig + 1 )) |
| 136 | set -- "$@" "server_port=$server_port" |
| 137 | echo " * Intermediate port: $server_port" |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 138 | |
| 139 | echo " * Start proxy in background ..." |
| 140 | if [ $VERBOSE -gt 0 ]; then |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 141 | echo "[ $tpxy_cmd_snippet ]" |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 142 | fi |
Gilles Peskine | 8149321 | 2017-10-24 12:22:40 +0200 | [diff] [blame] | 143 | eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 & |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 144 | tpxy_pid=$! |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 145 | |
| 146 | if [ $VERBOSE -gt 0 ]; then |
| 147 | echo " * Proxy ID: $TPXY_PID" |
| 148 | fi |
| 149 | |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 150 | echo " * Starting server ..." |
| 151 | if [ $VERBOSE -gt 0 ]; then |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 152 | echo "[ $SRV_BIN $* ]" |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 153 | fi |
| 154 | |
Gilles Peskine | 8149321 | 2017-10-24 12:22:40 +0200 | [diff] [blame] | 155 | exec "$SRV_BIN" "$@" >&2 & |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 156 | srv_pid=$! |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 157 | |
Gilles Peskine | afc4f89 | 2017-10-24 10:00:17 +0200 | [diff] [blame] | 158 | wait $srv_pid |
Hanno Becker | f65ca32 | 2017-10-10 14:44:57 +0100 | [diff] [blame] | 159 | |
| 160 | stop_proxy |
| 161 | return 0 |