blob: bb291b2b091e2ea6fb38601cccc423d1ee15592b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#!/bin/bash
prog="ss"
usage ()
{
cat >&2 <<EOF
Usage: $prog { -t|--tcp | -x|--unix } [options] [ FILTER ]
A fake ss stub that prints items depending on the variables
FAKE_NETSTAT_TCP_ESTABLISHED, FAKE_TCP_LISTEN,
FAKE_NETSTAT_UNIX_LISTEN, depending on command-line options.
Note that -n is ignored.
EOF
exit 1
}
not_supported ()
{
echo "Options not supported in stub: $*" >&2
usage
}
############################################################
#
parse_filter ()
{
# Very limited implementation:
# We only expect to find || inside parentheses
# We don't expect to see && - it is implied by juxtaposition
# Operator for port comparison is ignored and assumed to be ==
# Build lists of source ports and source IP addresses where
# each entry is surrounded by '|' characters. These lists can
# be easily "searched" using the POSIX prefix and suffix
# removal operators.
in_parens=false
sports="|"
srcs="|"
while [ -n "$1" ] ; do
case "$1" in
\()
in_parens=true
shift
;;
\))
in_parens=false
shift
;;
\|\|)
if ! $in_parens ; then
not_supported "|| in parentheses"
fi
shift
;;
sport)
p="${3#:}" ; sports="${sports}${p}|"
shift 3
;;
src)
ip="${2#\[}" ; ip="${ip%\]}" ; srcs="${srcs}${ip}|"
shift 2
;;
*)
usage
;;
esac
done
}
# Check if socket has matches in both ok_ips and ok_ports
filter_socket ()
{
ok_ips="$1"
ok_ports="$2"
socket="$3"
ip="${socket%:*}"
port="${socket##*:}"
if [ "$ok_ports" != "|" -a "${ok_ports#*|${port}|}" = "$ok_ports" ] ; then
return 1
fi
if [ "$ok_ips" != "|" -a "${ok_ips#*|${ip}|}" = "$ok_ips" ] ; then
return 1
fi
return 0
}
ss_tcp_established ()
{
if $header ; then
echo "Recv-Q Send-Q Local Address:Port Peer Address:Port"
fi
parse_filter $*
for i in $FAKE_NETSTAT_TCP_ESTABLISHED ; do
src="${i%|*}"
dst="${i#*|}"
if filter_socket "$srcs" "$sports" "$src" ; then
echo 0 0 "$src" "$dst"
fi
done
if [ -z "$FAKE_NETSTAT_TCP_ESTABLISHED_FILE" ] ; then
return
fi
while read src dst ; do
if filter_socket "$srcs" "$sports" "$src" ; then
echo 0 0 "$src" "$dst"
fi
done <"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
}
############################################################
unix_listen ()
{
if $header ; then
cat <<EOF
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port"
EOF
fi
parse_filter $*
_n=12345
for _s in $FAKE_NETSTAT_UNIX_LISTEN ; do
# ss matches Unix domain sockets as either src or
# sport.
if filter_socket "$srcs" "$sports" "${_s}:" || \
filter_socket "$srcs" "$sports" ":${_s}" ; then
printf "u_str LISTEN 0 128 %s %d * 0\n" "$_s" "$_n"
_n=$((_n + 1))
fi
done
}
############################################################
# Defaults.
tcp=false
unix=false
all=false
listen=false
header=true
orig="$*"
temp=$(getopt -n "$prog" -o "txnalHh" -l tcp -l unix -l help -- "$@")
[ $? -eq 0 ] || usage
eval set -- "$temp"
while true ; do
case "$1" in
--tcp|-t) tcp=true ; shift ;;
--unix|-x) unix=true ; shift ;;
-l) listen=true ; shift ;;
-a) all=true ; shift ;;
-H) header=false ; shift ;;
-n) shift ;;
--) shift ; break ;;
-h|--help|*) usage ;;
esac
done
$tcp || $unix || not_supported "$*"
if $tcp ; then
if [ "$1" != "state" -o "$2" != "established" ] || $listen ; then
usage
fi
shift 2
# Yes, lose the quoting so we can do a hacky parsing job
ss_tcp_established $*
exit
fi
if $unix ; then
if ! $listen ; then
not_supported "$orig"
fi
# Yes, lose the quoting so we can do a hacky parsing job
unix_listen $*
exit
fi
|