| 1 |
PORT=$(random_port) |
| 2 |
declare -a ARGS |
| 3 |
if [ "$NETCAT_UNIX" = "y" ]; then |
| 4 |
FAM="AF_UNIX" |
| 5 |
ARGS=( -${NETCAT_DGRAM:+u}U "$TEMPDIR/$PORT.sock" ) |
| 6 |
else |
| 7 |
FAM="AF_INET" |
| 8 |
ARGS=( ${NETCAT_DGRAM:+-u} "127.0.0.1" "$PORT" ) |
| 9 |
fi |
| 10 |
|
| 11 |
start_server() { |
| 12 |
#echo nc "$@" |
| 13 |
if [ -z "$NETCAT_DGRAM" ]; then |
| 14 |
STRACE_LINGER="n" PIPES="y" netcat_listen "$@" |
| 15 |
else |
| 16 |
STRACE_LINGER="y" PIPES="y" netcat_listen "$@" |
| 17 |
fi |
| 18 |
if [ "${BIND_ADDRESS#"{sa_family=$FAM"[,\}]}" = "$BIND_ADDRESS" ]; then # sanity check |
| 19 |
printf "ERROR at line %d: address \"%s\" isn't %s\\n" ${BASH_LINENO[0]} "$BIND_ADDRESS" "$FAM" >&2 |
| 20 |
exit 1 |
| 21 |
fi |
| 22 |
} |
| 23 |
start_client() { |
| 24 |
local in="$TEMPDIR/client.in" out="$TEMPDIR/client.out" |
| 25 |
mkfifo -- "$in" "$out" |
| 26 |
if [ -z "$NETCAT_DGRAM" ]; then |
| 27 |
$NC "$@" <"$in" >"$out" {LISTEN_IN}>&- {LISTEN_OUT}<&- & CLIENT_PID=$! |
| 28 |
else |
| 29 |
$NC "$@" <"$in" >"$out" {LISTEN_IN}>&- {LISTEN_OUT}<&- {STRACE_FD}<&- & CLIENT_PID=$! |
| 30 |
fi |
| 31 |
exec {CONNECT_IN}>"$in" {CONNECT_OUT}<"$out" |
| 32 |
rm -f -- "$in" "$out" |
| 33 |
} |
| 34 |
stop_client() { |
| 35 |
kill_and_wait $CLIENT_PID |
| 36 |
exec {CONNECT_IN}>&- {CONNECT_OUT}<&- |
| 37 |
} |
| 38 |
|
| 39 |
extract_source() { |
| 40 |
local strace_recvfrom peer="" IFS_old="$IFS" |
| 41 |
while IFS="" read -r strace_recvfrom; do |
| 42 |
# we don't want to use `grep -m1` here because its input is |
| 43 |
# buffered so it consumes multiple lines from the FIFO |
| 44 |
test "${strace_recvfrom#"recvfrom($BIND_FD,"}" = "$strace_recvfrom" || break |
| 45 |
done <&$STRACE_FD |
| 46 |
|
| 47 |
if [[ "$strace_recvfrom" =~ ^[A-Za-z0-9_]+\([0-9]+,[[:blank:]]*\"[^\"]*\",[[:blank:]]*[0-9]+,[[:blank:]]*[^,[:blank:]]+,[[:blank:]]*(\{sa_family=([^{},[:blank:]]+)(,[^{}]+)?\}), ]]; then |
| 48 |
peer="${BASH_REMATCH[1]}" |
| 49 |
fi |
| 50 |
|
| 51 |
if [ "$FAM" = "AF_INET" ] && [[ "$peer" =~ ^\{sa_family=$FAM,[[:blank:]]*sin_port=htons\(([0-9]+)\),[[:blank:]]*sin_addr=inet_addr\(\"([^\"]+)\"\)\}$ ]]; then |
| 52 |
SPORT=${BASH_REMATCH[1]} |
| 53 |
SADDR="${BASH_REMATCH[2]}" |
| 54 |
elif [ "$FAM" = "AF_UNIX" ] && [[ "$peer" =~ ^\{sa_family=$FAM,[[:blank:]]*sun_path=\"([^\"]+)\"\}$ ]]; then |
| 55 |
SPORT="" |
| 56 |
SADDR="${BASH_REMATCH[1]}" |
| 57 |
else |
| 58 |
printf "ERROR at line %d: Can't extract source port from \"%s\"\\n" ${BASH_LINENO[0]} "${peer:-"$strace_recvfrom"}" >&2 |
| 59 |
exit 1 |
| 60 |
fi |
| 61 |
IFS="$IFS_old" |
| 62 |
PEER_ADDR="$peer" |
| 63 |
} |
| 64 |
|
| 65 |
####################################################################### |
| 66 |
# without -k |
| 67 |
|
| 68 |
start_server "${ARGS[@]}" |
| 69 |
|
| 70 |
start_client "${ARGS[@]}" |
| 71 |
greet server |
| 72 |
greet client |
| 73 |
stop_client |
| 74 |
|
| 75 |
if [ -z "$NETCAT_DGRAM" ]; then |
| 76 |
# the server accepts a single connection and terminates afterwards |
| 77 |
wait $PID |
| 78 |
else |
| 79 |
# for UDP and not -k, the recvfrom() is followed with a connect() |
| 80 |
extract_source |
| 81 |
grep -m1 -E "^connect\\($BIND_FD,[[:blank:]]*\\{sa_family=$FAM," <&$STRACE_FD >"$TEMPDIR/conn" |
| 82 |
grep -Fq -e "$PEER_ADDR" <"$TEMPDIR/conn" |
| 83 |
|
| 84 |
if [ "$NETCAT_UNIX" = "y" ]; then |
| 85 |
# XXX for some reason reconnecting yields EPERM with UNIX domain datagram sockets |
| 86 |
! strace -o"$TEMPDIR/strace.log" -Z $NC -s "$SADDR" "${ARGS[@]}" 2>/dev/null |
| 87 |
grep -Eq '^connect\([0-9]+,\s*\{sa_family='"$FAM"',[^{}]+\},\s*[0-9]+\)\s*=\s*-1\s+EPERM\s' <"$TEMPDIR/strace.log" |
| 88 |
else |
| 89 |
# reconnecting from a different source is a no-op (we make sure later that "foo" |
| 90 |
# isn't read from the server) -- the test will fail if we reconnect from the |
| 91 |
# server address+port but that should be seldom enough |
| 92 |
$NC "${ARGS[@]}" <<<"foo" |
| 93 |
|
| 94 |
# make sure '-vz' reports it can't connect |
| 95 |
if $NC -vz "${ARGS[@]}" <<<"bar" 2>/dev/null; then |
| 96 |
printf "ERROR at line %d: Could reconnect!\\n" $LINENO >&2 |
| 97 |
exit 1 |
| 98 |
fi |
| 99 |
|
| 100 |
# can keep talking when reusing the same source though |
| 101 |
start_client -s "$SADDR" -p $SPORT "${ARGS[@]}" |
| 102 |
greet server |
| 103 |
greet client |
| 104 |
stop_client |
| 105 |
fi |
| 106 |
|
| 107 |
kill_and_wait "$PID" |
| 108 |
exec {STRACE_FD}<&- |
| 109 |
fi |
| 110 |
exec {LISTEN_IN}>&- {LISTEN_OUT}<&- |
| 111 |
|
| 112 |
|
| 113 |
####################################################################### |
| 114 |
# with -k |
| 115 |
|
| 116 |
start_server -k "${ARGS[@]}" |
| 117 |
|
| 118 |
if [ -z "$NETCAT_DGRAM" ]; then |
| 119 |
start_client "${ARGS[@]}" |
| 120 |
greet server |
| 121 |
greet client |
| 122 |
stop_client |
| 123 |
|
| 124 |
start_client "${ARGS[@]}" |
| 125 |
greet client |
| 126 |
greet server |
| 127 |
stop_client |
| 128 |
|
| 129 |
start_client "${ARGS[@]}" |
| 130 |
greet server |
| 131 |
stop_client |
| 132 |
|
| 133 |
start_client "${ARGS[@]}" |
| 134 |
greet client |
| 135 |
stop_client |
| 136 |
else |
| 137 |
# for UDP and not -k, the recvfrom() is not connected and while it can |
| 138 |
# receive UDP datagrams from multiple clients it cannot write to them |
| 139 |
for ((i=0; i<4; i++)); do |
| 140 |
start_client "${ARGS[@]}" |
| 141 |
greet server |
| 142 |
stop_client |
| 143 |
done |
| 144 |
fi |
| 145 |
|
| 146 |
kill_and_wait $PID |
| 147 |
|
| 148 |
# vim: set filetype=bash : |