Skip to content

Commit db8d96d

Browse files
committed
AF_ROUTE, AF_NDRV, AF_SYSTEM sockets added
1 parent 1f5ada5 commit db8d96d

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
* 2023.11.17 v0.4
1313
* Wildcat addresses in output
1414
* Some fixes and optimizations
15+
16+
* 2023.11.18 v0.5
17+
* `AF_ROUTE`, `AF_NDRV`, `AF_SYSTEM` sockets added

sockstat.c

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void usage(int ecode);
4444

4545
/* ------------------------------------------------------------------------------------------------------------------ */
4646
#define PROG_NAME "sockstat"
47-
#define PROG_VERSION "0.4"
47+
#define PROG_VERSION "0.5"
4848

4949
#define MAXPROC 16384;
5050
/* ------------------------------------------------------------------------------------------------------------------ */
@@ -90,8 +90,9 @@ int main(int argc, char* argv[]) {
9090
}
9191

9292
pids = (int *)malloc(sizeof(int) * mproc);
93-
npids = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(int) * mproc); /* PIDs */
94-
/* proc_listpids() returns bytes (!), not count of pids (!) */
93+
/* PIDs */
94+
/* NB! proc_listpids() returns bytes (!), not count of pids (!) */
95+
npids = proc_listpids(PROC_ALL_PIDS, 0, pids, sizeof(int) * mproc);
9596

9697
fds = (struct proc_fdinfo *)malloc(sizeof(struct proc_fdinfo) * OPEN_MAX);
9798

@@ -101,7 +102,8 @@ int main(int argc, char* argv[]) {
101102
for (int i = 0; i < npids/sizeof(int); i++) {
102103
/* PID => FDs */
103104
if ((mfds = proc_pidinfo(pids[i], PROC_PIDLISTFDS, 0, fds, sizeof(struct proc_fdinfo) * OPEN_MAX))) {
104-
proc_pidinfo(pids[i], PROC_PIDTBSDINFO, 0, &pinfo, sizeof(pinfo)); /* a PIDs => PID */
105+
/* a PIDs => PID */
106+
proc_pidinfo(pids[i], PROC_PIDTBSDINFO, 0, &pinfo, sizeof(pinfo));
105107

106108
nfds = (int)(mfds / sizeof(struct proc_fdinfo));
107109
for (int k = 0; k < nfds; k++) {
@@ -115,7 +117,7 @@ int main(int argc, char* argv[]) {
115117
switch (si.psi.soi_family) {
116118
case AF_INET:
117119
if (flg_i4 || flg_a) {
118-
if (si.psi.soi_kind == SOCKINFO_TCP) { /* IPv4 TCP */
120+
if (si.psi.soi_kind == SOCKINFO_TCP) { /* IPv4 TCP */
119121
/* Local address and port */
120122
if (si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport)
121123
sprintf(outstr + strlen(outstr), "\ttcp4\t%s:%d",
@@ -144,7 +146,7 @@ int main(int argc, char* argv[]) {
144146
INADDR_ANY ? "*" : inet_ntop(AF_INET,
145147
&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_46.i46a_addr4,
146148
buf, INET_ADDRSTRLEN));
147-
} else { /* IPv4 UDP */
149+
} else { /* IPv4 UDP */
148150
/* Local address and port */
149151
if (si.psi.soi_proto.pri_in.insi_lport)
150152
sprintf(outstr + strlen(outstr), "\tudp4\t%s:%d",
@@ -180,7 +182,7 @@ int main(int argc, char* argv[]) {
180182

181183
case AF_INET6:
182184
if (flg_i6 || flg_a) {
183-
if (si.psi.soi_kind == SOCKINFO_TCP) { /* IPv6 TCP */
185+
if (si.psi.soi_kind == SOCKINFO_TCP) { /* IPv6 TCP */
184186
/* Local address and port */
185187
if (si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport)
186188
sprintf(outstr + strlen(outstr), "\ttcp6\t%s:%d",
@@ -209,7 +211,7 @@ int main(int argc, char* argv[]) {
209211
"*" : inet_ntop(AF_INET6,
210212
&si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_faddr.ina_6,
211213
buf, INET6_ADDRSTRLEN));
212-
} else { /* IPv6 UDP */
214+
} else { /* IPv6 UDP */
213215
/* Local address and port */
214216
if (si.psi.soi_proto.pri_in.insi_lport)
215217
sprintf(outstr + strlen(outstr), "\tudp6\t%s:%d",
@@ -243,7 +245,7 @@ int main(int argc, char* argv[]) {
243245
}
244246
break;
245247

246-
case AF_UNIX: /* UNIX socket */
248+
case AF_UNIX: /* aka LOCAL socket */
247249
if (flg_u || flg_a) {
248250
sprintf(outstr + strlen(outstr), "\tunix");
249251
if (si.psi.soi_proto.pri_un.unsi_addr.ua_sun.sun_family != AF_UNIX &&
@@ -263,9 +265,57 @@ int main(int argc, char* argv[]) {
263265
}
264266
break;
265267

268+
case AF_ROUTE: /* Not much info, do we need more? */
269+
if (flg_a) {
270+
sprintf(outstr + strlen(outstr), "\troute\t0x%llx", si.psi.soi_pcb);
271+
printf("%s\n", outstr);
272+
}
273+
break;
274+
275+
case AF_NDRV:
276+
if (flg_a) {
277+
if (si.psi.soi_kind == SOCKINFO_NDRV) { /* is this check useless? */
278+
sprintf(outstr + strlen(outstr), "\tndrv\tunit: %d name: %s",
279+
si.psi.soi_proto.pri_ndrv.ndrvsi_if_unit,
280+
si.psi.soi_proto.pri_ndrv.ndrvsi_if_name);
281+
printf("%s\n", outstr);
282+
}
283+
}
284+
break;
285+
286+
case AF_SYSTEM:
287+
if (flg_a) {
288+
switch (si.psi.soi_kind) {
289+
case SOCKINFO_KERN_EVENT:
290+
sprintf(outstr + strlen(outstr), "\tkevt\t0x%llx evt: 0x%x:0x%x:0x%x",
291+
si.psi.soi_pcb,
292+
si.psi.soi_proto.pri_kern_event.kesi_vendor_code_filter,
293+
si.psi.soi_proto.pri_kern_event.kesi_class_filter,
294+
si.psi.soi_proto.pri_kern_event.kesi_subclass_filter);
295+
printf("%s\n", outstr);
296+
break;
297+
298+
case SOCKINFO_KERN_CTL:
299+
sprintf(outstr + strlen(outstr), "\tkctl\t0x%llx ctl: %s id: %d unit: %d",
300+
si.psi.soi_pcb,
301+
si.psi.soi_proto.pri_kern_ctl.kcsi_name,
302+
si.psi.soi_proto.pri_kern_ctl.kcsi_id,
303+
si.psi.soi_proto.pri_kern_ctl.kcsi_unit);
304+
printf("%s\n", outstr);
305+
break;
306+
307+
default: /* May this happen? */
308+
sprintf(outstr + strlen(outstr), "\tsunkn");
309+
printf("%s\n", outstr);
310+
break;
311+
}
312+
}
313+
break;
314+
315+
266316
default:
267317
if (flg_a) {
268-
sprintf(outstr + strlen(outstr), "\tunkn");
318+
sprintf(outstr + strlen(outstr), "\tunk");
269319
printf("%s\n", outstr);
270320
}
271321
break;

0 commit comments

Comments
 (0)