2 # Copyright Gerhard Rieger and contributors (see file CHANGES)
3 # Published under the GNU General Public License V.2, see file COPYING
5 # Shell script to perform group communications, sometimes called brokering.
6 # It starts a Socat instance that forks a child process for each
7 # connected client; the clients communicate via IPv4 broadcast
11 # socat-broker.sh TCP-L:1234
12 # Now connect with an arbitrary number of clients like TCP:<server>:1234
14 # socat-broker.sh SSL-L:1234,cert=server.pem,cafile=clients.crt
15 # Now connect with an arbitrary number of clients like SSL:<server>:1234,cafile=server.cert=clients.pem
20 $ECHO "Usage: $0 <options> <listener>"
21 $ECHO " <listener> is a passive address like TCP4-L or SSL-L"
23 $ECHO " -d* -S -t <timeout> -T <timeout> are passed to socat"
24 $ECHO " -V prints the socat command before starting it"
28 $ECHO "Then connect with clients to port 1234"
29 $ECHO "Data sent by any client is forwarded to all other clients"
37 X-q
) QUIET
=1; OPTS
="-d0" ;;
38 X-d
*|X-l?
*) OPTS
="$OPTS $1" ;;
39 X-b|X-S|X-t|X-T|X-l
) OPT
=$1; shift; OPTS
="$OPTS $OPT $1" ;;
41 X-
*) echo "Unknown option \"$1\"" >&2
51 if [ -z "$LISTENER" ]; then
52 echo "$0: Missing parameter" >&2
58 if ! [[ "$LISTENER" =~ .
*,fork
]] ||
[[ "$LISTENER" =~ .
*,fork
, ]]; then
59 LISTENER
="$LISTENER,fork"
63 */*) SOCAT
=${0%/*}/socat
;;
67 PORT
=$
($SOCAT -d -d -T 0.000001 UDP4-RECV
:0 /dev
/null
2>&1 |
grep bound |
sed 's/.*:\([1-9][0-9]*\)$/\1/')
68 if [ -z "$PORT" ]; then
69 echo "$0: Failed to determine free UDP port" >&2
73 BCADDR
=127.255.255.255
75 if [ "$VERBOSE" ]; then
76 echo -e "$SOCAT -lp socat-broker $OPTS \\
78 UDP4-DATAGRAM:$BCADDR:$PORT,bind=:$PORT,so-broadcast,so-reuseaddr"
80 $SOCAT -lp socat-broker
$OPTS \
82 "UDP4-DATAGRAM:$BCADDR:$PORT,bind=:$PORT,so-broadcast,so-reuseaddr"