2 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/sysctl.h>
40 #include <sys/unpcb.h>
42 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcp_seq.h>
48 #include <netinet/tcp_var.h>
49 #include <arpa/inet.h>
62 static int opt_4; /* Show IPv4 sockets */
63 static int opt_6; /* Show IPv6 sockets */
64 static int opt_c; /* Show connected sockets */
65 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
66 static int opt_l; /* Show listening sockets */
67 static int opt_u; /* Show Unix domain sockets */
68 static int opt_v; /* Verbose mode */
71 * Default protocols to use if no -P was defined.
73 static const char *default_protos[] = {"tcp", "udp", "divert" };
74 static size_t default_numprotos =
75 sizeof(default_protos) / sizeof(default_protos[0]);
77 static int *protos; /* protocols to use */
78 static size_t numprotos; /* allocated size of protos[] */
82 #define INT_BIT (sizeof(int)*CHAR_BIT)
83 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
84 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
93 const char *protoname;
94 struct sockaddr_storage laddr;
95 struct sockaddr_storage faddr;
100 static struct sock *sockhash[HASHSIZE];
102 static struct xfile *xfiles;
106 xprintf(const char *fmt, ...)
112 len = vprintf(fmt, ap);
121 get_proto_type(const char *proto)
123 struct protoent *pent;
125 if (strlen(proto) == 0)
127 pent = getprotobyname(proto);
129 warn("getprotobyname");
132 return (pent->p_proto);
136 static void init_protos(int num)
143 /* Find the maximum number of possible protocols. */
144 while (getprotoent() != NULL)
149 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
151 numprotos = proto_count;
156 parse_protos(char *protospec)
159 char *tmp = protospec;
160 int proto_type, proto_index;
162 if (protospec == NULL)
167 while ((prot = strsep(&tmp, ",")) != NULL) {
168 if (strlen(prot) == 0)
170 proto_type = get_proto_type(prot);
171 if (proto_type != -1)
172 protos[proto_index++] = proto_type;
174 numprotos = proto_index;
175 return (proto_index);
180 parse_ports(const char *portspec)
186 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
191 errx(1, "syntax error in port range");
192 for (q = p; *q != '\0' && isdigit(*q); ++q)
194 for (port = 0; p < q; ++p)
195 port = port * 10 + digittoint(*p);
196 if (port < 0 || port > 65535)
197 errx(1, "invalid port number");
210 for (q = p; *q != '\0' && isdigit(*q); ++q)
212 for (end = 0; p < q; ++p)
213 end = end * 10 + digittoint(*p);
214 if (end < port || end > 65535)
215 errx(1, "invalid port number");
224 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
226 struct sockaddr_in *sin4;
227 struct sockaddr_in6 *sin6;
229 bzero(sa, sizeof *sa);
232 sin4 = (struct sockaddr_in *)sa;
233 sin4->sin_len = sizeof *sin4;
234 sin4->sin_family = af;
235 sin4->sin_port = port;
236 sin4->sin_addr = *(struct in_addr *)addr;
239 sin6 = (struct sockaddr_in6 *)sa;
240 sin6->sin6_len = sizeof *sin6;
241 sin6->sin6_family = af;
242 sin6->sin6_port = port;
243 sin6->sin6_addr = *(struct in6_addr *)addr;
251 gather_inet(int proto)
253 struct xinpgen *xig, *exig;
259 const char *varname, *protoname;
262 int hash, retry, vflag;
272 varname = "net.inet.tcp.pcblist";
276 varname = "net.inet.udp.pcblist";
280 varname = "net.inet.divert.pcblist";
284 errx(1, "protocol %d not supported", proto);
292 if ((buf = realloc(buf, bufsize)) == NULL)
295 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
299 if (errno != ENOMEM || len != bufsize)
300 err(1, "sysctlbyname()");
303 xig = (struct xinpgen *)buf;
304 exig = (struct xinpgen *)(void *)
305 ((char *)buf + len - sizeof *exig);
306 if (xig->xig_len != sizeof *xig ||
307 exig->xig_len != sizeof *exig)
308 errx(1, "struct xinpgen size mismatch");
309 } while (xig->xig_gen != exig->xig_gen && retry--);
311 if (xig->xig_gen != exig->xig_gen && opt_v)
312 warnx("warning: data may be inconsistent");
315 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
320 xtp = (struct xtcpcb *)xig;
321 if (xtp->xt_len != sizeof *xtp) {
322 warnx("struct xtcpcb size mismatch");
326 so = &xtp->xt_socket;
330 xip = (struct xinpcb *)xig;
331 if (xip->xi_len != sizeof *xip) {
332 warnx("struct xinpcb size mismatch");
336 so = &xip->xi_socket;
339 errx(1, "protocol %d not supported", proto);
341 if ((inp->inp_vflag & vflag) == 0)
343 if (inp->inp_vflag & INP_IPV4) {
344 if ((inp->inp_fport == 0 && !opt_l) ||
345 (inp->inp_fport != 0 && !opt_c))
347 #define __IN_IS_ADDR_LOOPBACK(pina) \
348 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
350 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
351 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
353 #undef __IN_IS_ADDR_LOOPBACK
354 } else if (inp->inp_vflag & INP_IPV6) {
355 if ((inp->inp_fport == 0 && !opt_l) ||
356 (inp->inp_fport != 0 && !opt_c))
359 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
360 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
364 warnx("invalid vflag 0x%x", inp->inp_vflag);
367 if ((sock = calloc(1, sizeof *sock)) == NULL)
369 sock->socket = so->xso_so;
371 if (inp->inp_vflag & INP_IPV4) {
372 sock->family = AF_INET;
373 sockaddr(&sock->laddr, sock->family,
374 &inp->inp_laddr, inp->inp_lport);
375 sockaddr(&sock->faddr, sock->family,
376 &inp->inp_faddr, inp->inp_fport);
377 } else if (inp->inp_vflag & INP_IPV6) {
378 sock->family = AF_INET6;
379 sockaddr(&sock->laddr, sock->family,
380 &inp->in6p_laddr, inp->inp_lport);
381 sockaddr(&sock->faddr, sock->family,
382 &inp->in6p_faddr, inp->inp_fport);
384 sock->vflag = inp->inp_vflag;
385 sock->protoname = protoname;
386 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
387 sock->next = sockhash[hash];
388 sockhash[hash] = sock;
395 gather_unix(int proto)
397 struct xunpgen *xug, *exug;
400 const char *varname, *protoname;
407 varname = "net.local.stream.pcblist";
408 protoname = "stream";
411 varname = "net.local.dgram.pcblist";
422 if ((buf = realloc(buf, bufsize)) == NULL)
425 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
427 if (errno != ENOMEM || len != bufsize)
428 err(1, "sysctlbyname()");
431 xug = (struct xunpgen *)buf;
432 exug = (struct xunpgen *)(void *)
433 ((char *)buf + len - sizeof *exug);
434 if (xug->xug_len != sizeof *xug ||
435 exug->xug_len != sizeof *exug) {
436 warnx("struct xinpgen size mismatch");
439 } while (xug->xug_gen != exug->xug_gen && retry--);
441 if (xug->xug_gen != exug->xug_gen && opt_v)
442 warnx("warning: data may be inconsistent");
445 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
448 xup = (struct xunpcb *)xug;
449 if (xup->xu_len != sizeof *xup) {
450 warnx("struct xunpcb size mismatch");
453 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
454 (xup->xu_unp.unp_conn != NULL && !opt_c))
456 if ((sock = calloc(1, sizeof *sock)) == NULL)
458 sock->socket = xup->xu_socket.xso_so;
459 sock->pcb = xup->xu_unpp;
461 sock->family = AF_UNIX;
462 sock->protoname = protoname;
463 if (xup->xu_unp.unp_addr != NULL)
465 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
466 else if (xup->xu_unp.unp_conn != NULL)
467 *(void **)&sock->faddr = xup->xu_unp.unp_conn;
468 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
469 sock->next = sockhash[hash];
470 sockhash[hash] = sock;
481 olen = len = sizeof *xfiles;
482 if ((xfiles = malloc(len)) == NULL)
484 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
485 if (errno != ENOMEM || len != olen)
486 err(1, "sysctlbyname()");
488 if ((xfiles = realloc(xfiles, len)) == NULL)
491 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
492 errx(1, "struct xfile size mismatch");
493 nxfiles = len / sizeof *xfiles;
497 printaddr(int af, struct sockaddr_storage *ss)
499 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
500 struct sockaddr_un *sun;
506 addr = &((struct sockaddr_in *)ss)->sin_addr;
507 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
509 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
512 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
513 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
515 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
518 sun = (struct sockaddr_un *)ss;
519 off = (int)((char *)&sun->sun_path - (char *)sun);
520 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
522 if (addrstr[0] == '\0')
523 inet_ntop(af, addr, addrstr, sizeof addrstr);
525 return xprintf("%s:*", addrstr);
527 return xprintf("%s:%d", addrstr, port);
531 getprocname(pid_t pid)
533 static struct kinfo_proc proc;
539 mib[2] = KERN_PROC_PID;
542 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
543 /* Do not warn if the process exits before we get its name. */
548 return (proc.ki_comm);
552 check_ports(struct sock *s)
558 if ((s->family != AF_INET) && (s->family != AF_INET6))
560 if (s->family == AF_INET)
561 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port);
563 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port);
566 if (s->family == AF_INET)
567 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port);
569 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port);
576 displaysock(struct sock *s, int pos)
583 pos += xprintf("%s", s->protoname);
584 if (s->vflag & INP_IPV4)
585 pos += xprintf("4 ");
586 if (s->vflag & INP_IPV6)
587 pos += xprintf("6 ");
593 pos += printaddr(s->family, &s->laddr);
594 if (s->family == AF_INET6 && pos >= 58)
598 pos += printaddr(s->family, &s->faddr);
602 if (s->laddr.ss_len > 0) {
603 pos += printaddr(s->family, &s->laddr);
607 p = *(void **)&s->faddr;
609 pos += xprintf("(not connected)");
612 pos += xprintf("-> ");
613 for (hash = 0; hash < HASHSIZE; ++hash) {
614 for (s = sockhash[hash]; s != NULL; s = s->next)
620 if (s == NULL || s->laddr.ss_len == 0)
621 pos += xprintf("??");
623 pos += printaddr(s->family, &s->laddr);
639 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
640 "USER", "COMMAND", "PID", "FD", "PROTO",
641 "LOCAL ADDRESS", "FOREIGN ADDRESS");
643 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
644 if (xf->xf_data == NULL)
646 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
647 for (s = sockhash[hash]; s != NULL; s = s->next)
648 if ((void *)s->socket == xf->xf_data)
656 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
657 pos += xprintf("%lu ", (u_long)xf->xf_uid);
659 pos += xprintf("%s ", pwd->pw_name);
662 pos += xprintf("%.10s", getprocname(xf->xf_pid));
665 pos += xprintf("%lu ", (u_long)xf->xf_pid);
668 pos += xprintf("%d ", xf->xf_fd);
671 for (hash = 0; hash < HASHSIZE; hash++) {
672 for (s = sockhash[hash]; s != NULL; s = s->next) {
678 pos += xprintf("%-8s %-10s %-5s %-2s ",
685 static int set_default_protos(void)
687 struct protoent *prot;
691 init_protos(default_numprotos);
693 for (pindex = 0; pindex < default_numprotos; pindex++) {
694 pname = default_protos[pindex];
695 prot = getprotobyname(pname);
697 err(1, "getprotobyname: %s", pname);
698 protos[pindex] = prot->p_proto;
709 "Usage: sockstat [-46cLlu] [-p ports] [-P protocols]\n");
714 main(int argc, char *argv[])
716 int protos_defined = -1;
719 while ((o = getopt(argc, argv, "46cLlp:P:uv")) != -1)
740 protos_defined = parse_protos(optarg);
758 if ((!opt_4 && !opt_6) && protos_defined != -1)
760 if (!opt_4 && !opt_6 && !opt_u)
761 opt_4 = opt_6 = opt_u = 1;
762 if ((opt_4 || opt_6) && protos_defined == -1)
763 protos_defined = set_default_protos();
764 if (!opt_c && !opt_l)
767 if (opt_4 || opt_6) {
768 for (i = 0; i < protos_defined; i++)
769 gather_inet(protos[i]);
772 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
773 gather_unix(SOCK_STREAM);
774 gather_unix(SOCK_DGRAM);