1#!/bin/sh 2# -*-sh-basic-offset: 4-*- 3# Usage: udp_proxy_wrapper.sh [PROXY_PARAM...] -- [SERVER_PARAM...] 4# 5# Copyright The Mbed TLS Contributors 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: 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. 25# 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 47set -u 48 49MBEDTLS_BASE="$(dirname -- "$0")/../.." 50TPXY_BIN="$MBEDTLS_BASE/programs/test/udp_proxy" 51SRV_BIN="$MBEDTLS_BASE/programs/ssl/ssl_server2" 52 53: ${VERBOSE:=0} 54 55stop_proxy() { 56 if [ -n "${tpxy_pid:-}" ]; then 57 echo 58 echo " * Killing proxy (pid $tpxy_pid) ..." 59 kill $tpxy_pid 60 fi 61} 62 63stop_server() { 64 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 69} 70 71cleanup() { 72 stop_server 73 stop_proxy 74 exit 129 75} 76 77trap cleanup INT TERM HUP 78 79# 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 98 99dtls_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..." 114 if [ $VERBOSE -gt 0 ]; then 115 echo "[ $SRV_BIN $* ]" 116 fi 117 exec "$SRV_BIN" "$@" 118fi 119 120if [ -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\"" 131fi 132 133server_port=$(( server_port_orig + 1 )) 134set -- "$@" "server_port=$server_port" 135echo " * Intermediate port: $server_port" 136 137echo " * Start proxy in background ..." 138if [ $VERBOSE -gt 0 ]; then 139 echo "[ $tpxy_cmd_snippet ]" 140fi 141eval exec "$tpxy_cmd_snippet" >/dev/null 2>&1 & 142tpxy_pid=$! 143 144if [ $VERBOSE -gt 0 ]; then 145 echo " * Proxy ID: $TPXY_PID" 146fi 147 148echo " * Starting server ..." 149if [ $VERBOSE -gt 0 ]; then 150 echo "[ $SRV_BIN $* ]" 151fi 152 153exec "$SRV_BIN" "$@" >&2 & 154srv_pid=$! 155 156wait $srv_pid 157 158stop_proxy 159return 0 160