-
Notifications
You must be signed in to change notification settings - Fork 2
/
sockstat.c
349 lines (315 loc) · 19.6 KB
/
sockstat.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* ------------------------------------------------------------------------------------------------------------------ */
/* FreeBSD-like sockstat for macOS using libproc */
/* ------------------------------------------------------------------------------------------------------------------ */
/*
* Copyright (c) 2023-2024, Mikhail Zakharov <zmey20000@yahoo.com>
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* ------------------------------------------------------------------------------------------------------------------ */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <pwd.h>
#include <uuid/uuid.h>
#include <arpa/inet.h>
#include <libproc.h>
/* ------------------------------------------------------------------------------------------------------------------ */
void usage(int ecode);
/* ------------------------------------------------------------------------------------------------------------------ */
#define PROG_NAME "sockstat"
#define PROG_VERSION "1.1.0"
#define MAXPROC 16384;
/* ------------------------------------------------------------------------------------------------------------------ */
int main(int argc, char* argv[]) {
int mproc = MAXPROC; /* Max number of concurrent processes */
size_t mproc_len = sizeof(mproc); /* Set it enormously big to tune later*/
int *pids = NULL; /* PIDs array buffer */
int npids = 0; /* Number of PIDs */
static struct proc_fdinfo *fds = NULL; /* FDs array */
int mfds = 0, nfds = 0; /* Memory and number of FDS */
struct proc_bsdinfo pinfo;
struct socket_fdinfo si;
struct passwd *pwd;
char buf[INET6_ADDRSTRLEN] = {0}; /* Local/Remote IPv4/6 addresses bufs */
char outstr[160] = {0};
int ready = 0; /* Socket info collected - ready to print */
int listen = 1; /* Socket is listening for flg_l output */
int flg = 0; /* CLI flags, see below */
int flg_i4 = 0; /* IPv4 */
int flg_i6 = 0; /* IPv6 */
int flg_k = 0; /* Kern/system sockets */
int flg_l = 0; /* TCP LISTENing sockets */
int flg_n = 0; /* NDRV sockets */
int flg_q = 0; /* Quiet - hide header */
int flg_T = 0; /* Include TCP */
int flg_U = 0; /* Include UDP */
int flg_r = 0; /* ROUTE sockets */
int flg_u = 0; /* UNIX aka LOCAL sockets */
int flg_a = 0; /* pseudo-flag ALL socket flags are on */
while ((flg = getopt(argc, argv, "46TUklnrquhv")) != -1)
switch(flg) {
case '4': flg_i4 = 1; break;
case '6': flg_i6 = 1; break;
case 'T': flg_T = 1; break;
case 'U': flg_U = 1; break;
case 'k': flg_k = 1; break;
case 'l': flg_l = 1; break;
case 'n': flg_n = 1; break;
case 'q': flg_q = 1; break;
case 'r': flg_r = 1; break;
case 'u': flg_u = 1; break;
case 'h': (void)usage(0); break;
case 'v': printf("%s %s\n", PROG_NAME, PROG_VERSION); exit(0); break;
default: (void)usage(1);
}
if (!flg_i4 && !flg_i6 && !flg_T && !flg_U && !flg_k && !flg_n && !flg_r && !flg_u) flg_a = 1;
if (sysctlbyname("kern.maxproc", &mproc, &mproc_len, NULL, 0) == -1) {
perror("Unable to get the maximum allowed number of processes");
printf("Assuming it to be lower than: %d\n", mproc);
}
pids = (int *)malloc(sizeof(int) * mproc);
/* PIDs */
/* NB! proc_listpids() returns bytes (!), not count of pids (!) */
npids = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(int) * mproc);
fds = (struct proc_fdinfo *)malloc(sizeof(struct proc_fdinfo) * OPEN_MAX);
if (!flg_q)
printf("%-23s\t%-5s\t%-31s\t%-3s\t%-5s\t%-19s\t%s\n",
"USER", "PID", "COMMAND", "FD", "PROTO", "LOCAL ADDRESS", "REMOTE ADDRESS");
for (int i = 0; i < npids/sizeof(int); i++) {
/* PID => FDs */
if ((mfds = proc_pidinfo(pids[i], PROC_PIDLISTFDS, 0, fds, sizeof(struct proc_fdinfo) * OPEN_MAX))) {
/* a PIDs => PID */
proc_pidinfo(pids[i], PROC_PIDTBSDINFO, 0, &pinfo, sizeof(pinfo));
nfds = (int)(mfds / sizeof(struct proc_fdinfo));
for (int k = 0; k < nfds; k++) {
if (proc_pidfdinfo(pids[i], fds[k].proc_fd, PROC_PIDFDSOCKETINFO, &si, sizeof(si)) >= sizeof(si)) {
pwd = getpwuid(pinfo.pbi_uid);
sprintf(outstr, "%-23s\t%-5d\t%-31s\t%-3d",
pwd->pw_name, pinfo.pbi_pid, pinfo.pbi_name[0] ? pinfo.pbi_name : pinfo.pbi_comm,
fds[k].proc_fd);
ready = 0;
listen = 0;
switch (si.psi.soi_family) {
case AF_INET:
if (flg_i4 || flg_a || flg_T || flg_U) {
if (si.psi.soi_kind == SOCKINFO_TCP && (flg_a || flg_T)) { /* IPv4 TCP */
/* Local address and port */
if (si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport) {
sprintf(outstr + strlen(outstr), "\ttcp4\t%s:%d",
si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_46.i46a_addr4.s_addr ==
INADDR_ANY ? "*" : inet_ntop(AF_INET,
&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_46.i46a_addr4,
buf, INET_ADDRSTRLEN),
ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport));
if (si.psi.soi_proto.pri_tcp.tcpsi_state == TSI_S_LISTEN) listen = 1;
ready = 1;
}
/* Remote address and port */
if (si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport) {
sprintf(outstr + strlen(outstr), "\t%s:%d",
inet_ntop(AF_INET,
&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_46.i46a_addr4,
buf, INET_ADDRSTRLEN),
ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport));
ready = 1;
} else {
/* No port - no address */
sprintf(outstr + strlen(outstr), "\t*:*");
ready = 1;
}
} else if (flg_a || flg_U) { /* IPv4 UDP */
/* Local address and port */
if (si.psi.soi_proto.pri_in.insi_lport) {
sprintf(outstr + strlen(outstr), "\tudp4\t%s:%d",
si.psi.soi_proto.pri_in.insi_laddr.ina_46.i46a_addr4.s_addr ==
INADDR_ANY ? "*" : inet_ntop(AF_INET,
&si.psi.soi_proto.pri_in.insi_laddr.ina_46.i46a_addr4,
buf, INET_ADDRSTRLEN),
ntohs(si.psi.soi_proto.pri_in.insi_lport));
} else {
sprintf(outstr+strlen(outstr), "\tudp4\t%s:*",
si.psi.soi_proto.pri_in.insi_laddr.ina_46.i46a_addr4.s_addr ==
INADDR_ANY ? "*" : inet_ntop(AF_INET,
&si.psi.soi_proto.pri_in.insi_laddr.ina_46.i46a_addr4,
buf, INET_ADDRSTRLEN));
}
/* Remote address and port */
/* UDP is stateless protocol, marking it LISTEN */
listen = 1;
sprintf(outstr + strlen(outstr), "\t*:*");
ready = 1;
}
}
break;
case AF_INET6:
if (flg_i6 || flg_a || flg_T || flg_U) {
if (si.psi.soi_kind == SOCKINFO_TCP && (flg_a || flg_T)) { /* IPv6 TCP */
/* Local address and port */
if (si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport) {
sprintf(outstr + strlen(outstr), "\ttcp6\t%s:%d",
IN6_IS_ADDR_UNSPECIFIED(&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_6) ?
"*" : inet_ntop(AF_INET6,
&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_laddr.ina_6,
buf, INET6_ADDRSTRLEN),
ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport));
if (si.psi.soi_proto.pri_tcp.tcpsi_state == TSI_S_LISTEN) listen = 1;
ready = 1;
}
/* Remote address and port */
if (si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport) {
sprintf(outstr + strlen(outstr), "\t%s:%d",
inet_ntop(AF_INET6,
&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_6,
buf, INET6_ADDRSTRLEN),
ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport));
ready = 1;
} else {
/* No port - no address */
sprintf(outstr + strlen(outstr), "\t*:*");
ready = 1;
}
} else if (flg_a || flg_U) { /* IPv6 UDP */
listen = 1;
/* Local address and port */
if (si.psi.soi_proto.pri_in.insi_lport) {
sprintf(outstr + strlen(outstr), "\tudp6\t%s:%d",
IN6_IS_ADDR_UNSPECIFIED(&si.psi.soi_proto.pri_in.insi_laddr.ina_6) ?
"*" : inet_ntop(AF_INET6,
&si.psi.soi_proto.pri_in.insi_laddr.ina_6,
buf, INET6_ADDRSTRLEN),
ntohs(si.psi.soi_proto.pri_in.insi_lport));
ready = 1;
} else {
sprintf(outstr + strlen(outstr), "\tudp6\t%s:*",
IN6_IS_ADDR_UNSPECIFIED(&si.psi.soi_proto.pri_in.insi_laddr.ina_6) ?
"*" : inet_ntop(AF_INET6,
&si.psi.soi_proto.pri_in.insi_laddr.ina_6,
buf, INET6_ADDRSTRLEN));
ready = 1;
}
/* Remote address and port */
/* UDP is stateless protocol, marking it LISTEN */
listen = 1;
sprintf(outstr + strlen(outstr), "\t*:*");
ready = 1;
}
}
break;
case AF_UNIX: /* aka LOCAL socket */
if (flg_u || flg_a) {
sprintf(outstr + strlen(outstr), "\tunix");
/* Bound address */
if (si.psi.soi_proto.pri_un.unsi_addr.ua_sun.sun_path[0])
sprintf(outstr + strlen(outstr), "\t%s",
si.psi.soi_proto.pri_un.unsi_addr.ua_sun.sun_path);
else
sprintf(outstr + strlen(outstr), "\t0x%llx",
si.psi.soi_proto.pri_un.unsi_conn_pcb);
/* Address of socket connected to */
if (si.psi.soi_proto.pri_un.unsi_caddr.ua_sun.sun_path[0])
sprintf(outstr + strlen(outstr), "\t%s",
si.psi.soi_proto.pri_un.unsi_caddr.ua_sun.sun_path);
else {
sprintf(outstr + strlen(outstr), "\t->??");
listen = 1;
}
ready = 1;
}
break;
case AF_ROUTE: /* Not much info, do we need more? */
if (flg_r || flg_a) {
sprintf(outstr + strlen(outstr), "\troute\t0x%llx", si.psi.soi_pcb);
ready = 1;
}
break;
case AF_NDRV:
if (flg_n || flg_a) {
if (si.psi.soi_kind == SOCKINFO_NDRV) { /* is this check useless? */
sprintf(outstr + strlen(outstr), "\tndrv\tunit: %d name: %s",
si.psi.soi_proto.pri_ndrv.ndrvsi_if_unit,
si.psi.soi_proto.pri_ndrv.ndrvsi_if_name);
ready = 1;
}
}
break;
case AF_SYSTEM:
if (flg_k || flg_a) {
switch (si.psi.soi_kind) {
case SOCKINFO_KERN_EVENT:
sprintf(outstr + strlen(outstr), "\tkevt\t0x%llx evt: 0x%x:0x%x:0x%x",
si.psi.soi_pcb,
si.psi.soi_proto.pri_kern_event.kesi_vendor_code_filter,
si.psi.soi_proto.pri_kern_event.kesi_class_filter,
si.psi.soi_proto.pri_kern_event.kesi_subclass_filter);
ready = 1;
break;
case SOCKINFO_KERN_CTL:
sprintf(outstr + strlen(outstr), "\tkctl\t0x%llx ctl: %s id: %d unit: %d",
si.psi.soi_pcb,
si.psi.soi_proto.pri_kern_ctl.kcsi_name,
si.psi.soi_proto.pri_kern_ctl.kcsi_id,
si.psi.soi_proto.pri_kern_ctl.kcsi_unit);
ready = 1;
break;
default: /* May this ever happen? */
sprintf(outstr + strlen(outstr), "\tsunkn");
ready = 1;
break;
}
}
break;
default:
if (flg_a) {
sprintf(outstr + strlen(outstr), "\tunk");
ready = 1;
}
break;
}
if (ready && ((flg_l && listen) || !flg_l)) {
puts(outstr);
}
}
}
}
}
return 0;
}
/* ------------------------------------------------------------------------------------------------------------------ */
void usage(int ecode) {
printf("Usage: sockstat [-46TUklnrquhv]\n\n\
-4\tShow AF_INET (IPv4) sockets\n\
-6\tShow AF_INET6 (IPv6) sockets\n\
-T\tShow TCP protocol\n\
-U\tShow UDP protocol\n\
-k\tShow AF_SYSTEM (Kernel) sockets\n\
-n\tShow AF_NDRV sockets\n\
-r\tShow AF_ROUTE sockets\n\
-u\tShow AF_LOCAL (UNIX) sockets\n\
\n\
-l\tShow only LISTENing sockets\n\
-q\tQuiet mode - suppress header\n\
\n\
-h\tThis help message\n\
-v\tShow program version\n\n");
exit(ecode);
}