1
0
Fork 0
mirror of https://repo.or.cz/socat.git synced 2025-07-19 17:32:57 +00:00

socat-chain.sh, socat-mux.sh, and socat-broker.sh work with older Socat versions

This commit is contained in:
Gerhard Rieger 2024-02-23 10:50:55 +01:00
parent 9fe8206b52
commit 1241600b81
5 changed files with 55 additions and 19 deletions

View file

@ -28,7 +28,7 @@ usage () {
$ECHO "data provided by 10.2.3.4 is sent to ALL clients"
$ECHO " <options>:"
$ECHO "\t-h\tShow this help text and exit"
$ECHO "\t-V\tShow Socat commands"
$ECHO "\t-V\tShows executed Socat commands and some infos"
$ECHO "\t-q\tSuppress most messages"
$ECHO "\t-d*\tOptions beginning with -d are passed to Socat processes"
$ECHO "\t-l*\tOptions beginning with -l are passed to Socat processes"
@ -67,19 +67,31 @@ if ! [[ "$LISTENER" =~ .*,fork ]] || [[ "$LISTENER" =~ .*,fork, ]]; then
fi
case "$0" in
*/*) SOCAT=${0%/*}/socat ;;
*) SOCAT=socat ;;
*/*) if [ -x ${0%/*}/socat ]; then SOCAT=${0%/*}/socat; fi ;;
esac
if [ -z "$SOCAT" ]; then SOCAT=socat; fi
[ "$VERBOSE" ] && echo "# $0: Using executable $SOCAT" >&2
# We need two free UDP ports (on loopback)
PORT1=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/')
PORT2=$($SOCAT -d -d -T 0.000001 UDP4-RECV:0 /dev/null 2>&1 |grep bound |sed 's/.*:\([1-9][0-9]*\)$/\1/')
if [ -z "$PORT1" -o -z "$PORT2" ]; then
echo "$0: Failed to determine free UDP ports" >&2
exit 1
fi
if [ "$PORT1" = "$PORT2" ]; then # seen on etch
PORT2=$((PORT1+1))
# Probably old Socat version, use a different approach
if type ss >/dev/null 2>&1; then
:
elif type netstat >/dev/null 2>&1; then
alias ss=netstat
else
echo "$0: Failed to determine free UDP ports (old Socat version, no ss, no netstat?)" >&2
exit 1
fi
PORT1= PORT2=
while [ -z "$PORT1" -o -z "$PORT2" -o "$PORT1" = "$PORT2" ] || ss -aun |grep -e ":$PORT1\>" -e ":$PORT2\>" >/dev/null; do
PORT1=$((16384+RANDOM))
PORT2=$((16384+RANDOM))
done
fi
[ "$VERBOSE" ] && echo "# $0: Using UDP ports $PORT1, $PORT2" >&2
IFADDR=127.0.0.1
BCADDR=127.255.255.255