Unbreak buildworld.
[dragonfly.git] / usr.bin / sockstat / sockstat.c
bloba65a219817bff2bb5539539f8d8ca182bdc0575f
1 /*-
2 * Copyright (c) 2005 Joerg Sonnenberger <joerg@bec.de>. All rights reserved.
3 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/usr.bin/sockstat/sockstat.c,v 1.12 2004/12/06 09:28:05 ru Exp $
30 * $DragonFly: src/usr.bin/sockstat/sockstat.c,v 1.7 2007/02/01 10:33:26 corecode Exp $
33 #define _KERNEL_STRUCTURES
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sysctl.h>
38 #include <sys/file.h>
39 #include <sys/user.h>
41 #include <sys/un.h>
42 #include <sys/unpcb.h>
44 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/in_pcb.h>
48 #include <netinet/tcp.h>
49 #include <netinet/tcp_seq.h>
50 #include <netinet/tcp_var.h>
51 #include <arpa/inet.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <kinfo.h>
57 #include <netdb.h>
58 #include <pwd.h>
59 #include <stdarg.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
65 static int opt_4; /* Show IPv4 sockets */
66 static int opt_6; /* Show IPv6 sockets */
67 static int opt_c; /* Show connected sockets */
68 static int opt_l; /* Show listening sockets */
69 static int opt_u; /* Show Unix domain sockets */
70 static int opt_v; /* Verbose mode */
72 static int *ports;
74 #define INT_BIT (sizeof(int)*CHAR_BIT)
75 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
76 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
78 struct sock {
79 void *socket;
80 void *pcb;
81 int vflag;
82 int family;
83 int proto;
84 const char *protoname;
85 struct sockaddr_storage laddr;
86 struct sockaddr_storage faddr;
87 struct sock *next;
90 #define HASHSIZE 1009
91 static struct sock *sockhash[HASHSIZE];
93 static struct kinfo_file *xfiles;
94 static size_t nxfiles;
96 static int
97 xprintf(const char *fmt, ...)
99 va_list ap;
100 int len;
102 va_start(ap, fmt);
103 len = vprintf(fmt, ap);
104 va_end(ap);
105 if (len < 0)
106 err(1, "printf()");
107 return (len);
110 static void
111 parse_ports(const char *portspec)
113 const char *p, *q;
114 int port, end;
116 if (ports == NULL)
117 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
118 err(1, "calloc()");
119 p = portspec;
120 while (*p != '\0') {
121 if (!isdigit(*p))
122 errx(1, "syntax error in port range");
123 for (q = p; *q != '\0' && isdigit(*q); ++q)
124 /* nothing */ ;
125 for (port = 0; p < q; ++p)
126 port = port * 10 + (*p - '0');
127 if (port < 0 || port > 65535)
128 errx(1, "invalid port number");
129 SET_PORT(port);
130 switch (*p) {
131 case '-':
132 ++p;
133 break;
134 case ',':
135 ++p;
136 /* fall through */
137 case '\0':
138 default:
139 continue;
141 for (q = p; *q != '\0' && isdigit(*q); ++q)
142 /* nothing */ ;
143 for (end = 0; p < q; ++p)
144 end = end * 10 + (*p - '0');
145 if (end < port || end > 65535)
146 errx(1, "invalid port number");
147 while (port++ < end)
148 SET_PORT(port);
149 if (*p == ',')
150 ++p;
154 static void
155 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
157 struct sockaddr_in *sin4;
158 struct sockaddr_in6 *sin6;
160 bzero(sa, sizeof *sa);
161 switch (af) {
162 case AF_INET:
163 sin4 = (struct sockaddr_in *)sa;
164 sin4->sin_len = sizeof *sin4;
165 sin4->sin_family = af;
166 sin4->sin_port = port;
167 sin4->sin_addr = *(struct in_addr *)addr;
168 break;
169 case AF_INET6:
170 sin6 = (struct sockaddr_in6 *)sa;
171 sin6->sin6_len = sizeof *sin6;
172 sin6->sin6_family = af;
173 sin6->sin6_port = port;
174 sin6->sin6_addr = *(struct in6_addr *)addr;
175 break;
176 default:
177 abort();
181 static void
182 gather_inet(int proto)
184 void *so_begin, *so_end;
185 struct xinpcb *xip;
186 struct xtcpcb *xtp;
187 struct inpcb *inp;
188 struct xsocket *so;
189 struct sock *sock;
190 const char *varname, *protoname;
191 size_t len;
192 void *buf;
193 int hash, vflag;
195 vflag = 0;
196 if (opt_4)
197 vflag |= INP_IPV4;
198 if (opt_6)
199 vflag |= INP_IPV6;
201 switch (proto) {
202 case IPPROTO_TCP:
203 varname = "net.inet.tcp.pcblist";
204 protoname = "tcp";
205 break;
206 case IPPROTO_UDP:
207 varname = "net.inet.udp.pcblist";
208 protoname = "udp";
209 break;
210 case IPPROTO_DIVERT:
211 varname = "net.inet.divert.pcblist";
212 protoname = "div";
213 break;
214 default:
215 abort();
218 buf = NULL;
219 len = 0;
221 if (sysctlbyname(varname, NULL, &len, NULL, 0)) {
222 if (errno == ENOENT)
223 goto out;
224 err(1, "fetching %s", varname);
226 if ((buf = malloc(len)) == NULL)
227 err(1, "malloc()");
228 if (sysctlbyname(varname, buf, &len, NULL, 0)) {
229 if (errno == ENOENT)
230 goto out;
231 err(1, "fetching %s", varname);
234 so_begin = buf;
235 so_end = (uint8_t *)buf + len;
237 for (so_begin = buf, so_end = (uint8_t *)so_begin + len;
238 (uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end &&
239 (uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end;
240 so_begin = (uint8_t *)so_begin + *(size_t *)so_begin) {
241 switch (proto) {
242 case IPPROTO_TCP:
243 xtp = (struct xtcpcb *)so_begin;
244 if (xtp->xt_len != sizeof *xtp) {
245 warnx("struct xtcpcb size mismatch");
246 goto out;
248 inp = &xtp->xt_inp;
249 so = &xtp->xt_socket;
250 break;
251 case IPPROTO_UDP:
252 case IPPROTO_DIVERT:
253 xip = (struct xinpcb *)so_begin;
254 if (xip->xi_len != sizeof *xip) {
255 warnx("struct xinpcb size mismatch");
256 goto out;
258 inp = &xip->xi_inp;
259 so = &xip->xi_socket;
260 break;
261 default:
262 abort();
264 if ((inp->inp_vflag & vflag) == 0)
265 continue;
266 if (inp->inp_vflag & INP_IPV4) {
267 if ((inp->inp_fport == 0 && !opt_l) ||
268 (inp->inp_fport != 0 && !opt_c))
269 continue;
270 } else if (inp->inp_vflag & INP_IPV6) {
271 if ((inp->in6p_fport == 0 && !opt_l) ||
272 (inp->in6p_fport != 0 && !opt_c))
273 continue;
274 } else {
275 if (opt_v)
276 warnx("invalid vflag 0x%x", inp->inp_vflag);
277 continue;
279 if ((sock = calloc(1, sizeof *sock)) == NULL)
280 err(1, "malloc()");
281 sock->socket = so->xso_so;
282 sock->proto = proto;
283 if (inp->inp_vflag & INP_IPV4) {
284 sock->family = AF_INET;
285 sockaddr(&sock->laddr, sock->family,
286 &inp->inp_laddr, inp->inp_lport);
287 sockaddr(&sock->faddr, sock->family,
288 &inp->inp_faddr, inp->inp_fport);
289 } else if (inp->inp_vflag & INP_IPV6) {
290 sock->family = AF_INET6;
291 sockaddr(&sock->laddr, sock->family,
292 &inp->in6p_laddr, inp->in6p_lport);
293 sockaddr(&sock->faddr, sock->family,
294 &inp->in6p_faddr, inp->in6p_fport);
296 sock->vflag = inp->inp_vflag;
297 sock->protoname = protoname;
298 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
299 sock->next = sockhash[hash];
300 sockhash[hash] = sock;
302 out:
303 free(buf);
306 static void
307 gather_unix(int proto)
309 void *so_begin, *so_end;
310 struct xunpcb *xup;
311 struct sock *sock;
312 const char *varname, *protoname;
313 size_t len;
314 void *buf;
315 int hash;
317 switch (proto) {
318 case SOCK_STREAM:
319 varname = "net.local.stream.pcblist";
320 protoname = "stream";
321 break;
322 case SOCK_DGRAM:
323 varname = "net.local.dgram.pcblist";
324 protoname = "dgram";
325 break;
326 default:
327 abort();
330 buf = NULL;
331 len = 0;
333 if (sysctlbyname(varname, NULL, &len, NULL, 0))
334 err(1, "fetching %s", varname);
336 if ((buf = malloc(len)) == NULL)
337 err(1, "malloc()");
338 if (sysctlbyname(varname, buf, &len, NULL, 0))
339 err(1, "fetching %s", varname);
341 for (so_begin = buf, so_end = (uint8_t *)buf + len;
342 (uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end &&
343 (uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end;
344 so_begin = (uint8_t *)so_begin + *(size_t *)so_begin) {
345 xup = so_begin;
346 if (xup->xu_len != sizeof *xup) {
347 warnx("struct xunpcb size mismatch");
348 goto out;
350 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
351 (xup->xu_unp.unp_conn != NULL && !opt_c))
352 continue;
353 if ((sock = calloc(1, sizeof *sock)) == NULL)
354 err(1, "malloc()");
355 sock->socket = xup->xu_socket.xso_so;
356 sock->pcb = xup->xu_unpp;
357 sock->proto = proto;
358 sock->family = AF_UNIX;
359 sock->protoname = protoname;
360 if (xup->xu_unp.unp_addr != NULL)
361 sock->laddr =
362 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
363 else if (xup->xu_unp.unp_conn != NULL)
364 *(void **)&sock->faddr = xup->xu_unp.unp_conn;
365 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
366 sock->next = sockhash[hash];
367 sockhash[hash] = sock;
369 out:
370 free(buf);
373 static void
374 getfiles(void)
376 if (kinfo_get_files(&xfiles, &nxfiles))
377 err(1, "kinfo_get_files");
380 static int
381 printaddr(int af, struct sockaddr_storage *ss)
383 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
384 struct sockaddr_un *sun;
385 void *addr;
386 int off, port;
388 switch (af) {
389 case AF_INET:
390 addr = &((struct sockaddr_in *)ss)->sin_addr;
391 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
392 addrstr[0] = '*';
393 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
394 break;
395 case AF_INET6:
396 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
397 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
398 addrstr[0] = '*';
399 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
400 break;
401 case AF_UNIX:
402 sun = (struct sockaddr_un *)ss;
403 off = (int)((char *)&sun->sun_path - (char *)sun);
404 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
405 default:
406 abort();
408 if (addrstr[0] == '\0')
409 inet_ntop(af, addr, addrstr, sizeof addrstr);
410 if (port == 0)
411 return xprintf("%s:*", addrstr);
412 else
413 return xprintf("%s:%d", addrstr, port);
416 static const char *
417 getprocname(pid_t pid)
419 static struct kinfo_proc proc;
420 size_t len;
421 int mib[4];
423 mib[0] = CTL_KERN;
424 mib[1] = KERN_PROC;
425 mib[2] = KERN_PROC_PID;
426 mib[3] = (int)pid;
427 len = sizeof proc;
428 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
429 warn("sysctl()");
430 return ("??");
432 return (proc.kp_comm);
435 static int
436 check_ports(struct sock *s)
438 int port;
440 if (ports == NULL)
441 return (1);
442 if ((s->family != AF_INET) && (s->family != AF_INET6))
443 return (1);
444 if (s->family == AF_INET)
445 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port);
446 else
447 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port);
448 if (CHK_PORT(port))
449 return (1);
450 if (s->family == AF_INET)
451 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port);
452 else
453 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port);
454 if (CHK_PORT(port))
455 return (1);
456 return (0);
459 static void
460 display(void)
462 struct passwd *pwd;
463 struct kinfo_file *xf;
464 struct sock *s;
465 void *p;
466 int hash, n, pos;
468 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
469 "USER", "COMMAND", "PID", "FD", "PROTO",
470 "LOCAL ADDRESS", "FOREIGN ADDRESS");
471 setpassent(1);
472 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
473 if (xf->f_data == NULL)
474 continue;
475 hash = (int)((uintptr_t)xf->f_data % HASHSIZE);
476 for (s = sockhash[hash]; s != NULL; s = s->next)
477 if ((void *)s->socket == xf->f_data)
478 break;
479 if (s == NULL)
480 continue;
481 if (!check_ports(s))
482 continue;
483 pos = 0;
484 if ((pwd = getpwuid(xf->f_uid)) == NULL)
485 pos += xprintf("%lu", (u_long)xf->f_uid);
486 else
487 pos += xprintf("%s", pwd->pw_name);
488 while (pos < 9)
489 pos += xprintf(" ");
490 pos += xprintf("%.10s", getprocname(xf->f_pid));
491 while (pos < 20)
492 pos += xprintf(" ");
493 pos += xprintf("%lu", (u_long)xf->f_pid);
494 while (pos < 26)
495 pos += xprintf(" ");
496 pos += xprintf("%d", xf->f_fd);
497 while (pos < 29)
498 pos += xprintf(" ");
499 pos += xprintf("%s", s->protoname);
500 if (s->vflag & INP_IPV4)
501 pos += xprintf("4");
502 if (s->vflag & INP_IPV6)
503 pos += xprintf("6");
504 while (pos < 36)
505 pos += xprintf(" ");
506 switch (s->family) {
507 case AF_INET:
508 case AF_INET6:
509 pos += printaddr(s->family, &s->laddr);
510 while (pos < 57)
511 pos += xprintf(" ");
512 pos += xprintf(" ");
513 pos += printaddr(s->family, &s->faddr);
514 break;
515 case AF_UNIX:
516 /* server */
517 if (s->laddr.ss_len > 0) {
518 pos += printaddr(s->family, &s->laddr);
519 break;
521 /* client */
522 p = *(void **)&s->faddr;
523 if (p == NULL) {
524 pos += xprintf("(not connected)");
525 break;
527 pos += xprintf("-> ");
528 for (hash = 0; hash < HASHSIZE; ++hash) {
529 for (s = sockhash[hash]; s != NULL; s = s->next)
530 if (s->pcb == p)
531 break;
532 if (s != NULL)
533 break;
535 if (s == NULL || s->laddr.ss_len == 0)
536 pos += xprintf("??");
537 else
538 pos += printaddr(s->family, &s->laddr);
539 break;
540 default:
541 abort();
543 xprintf("\n");
547 static void
548 usage(void)
550 fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n");
551 exit(1);
555 main(int argc, char *argv[])
557 int o;
559 while ((o = getopt(argc, argv, "46clp:uv")) != -1)
560 switch (o) {
561 case '4':
562 opt_4 = 1;
563 break;
564 case '6':
565 opt_6 = 1;
566 break;
567 case 'c':
568 opt_c = 1;
569 break;
570 case 'l':
571 opt_l = 1;
572 break;
573 case 'p':
574 parse_ports(optarg);
575 break;
576 case 'u':
577 opt_u = 1;
578 break;
579 case 'v':
580 ++opt_v;
581 break;
582 default:
583 usage();
586 argc -= optind;
587 argv += optind;
589 if (argc > 0)
590 usage();
592 if (!opt_4 && !opt_6 && !opt_u)
593 opt_4 = opt_6 = opt_u = 1;
594 if (!opt_c && !opt_l)
595 opt_c = opt_l = 1;
597 if (opt_4 || opt_6) {
598 gather_inet(IPPROTO_TCP);
599 gather_inet(IPPROTO_UDP);
600 gather_inet(IPPROTO_DIVERT);
602 if (opt_u) {
603 gather_unix(SOCK_STREAM);
604 gather_unix(SOCK_DGRAM);
606 getfiles();
607 display();
609 exit(0);