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; /* Show listening sockets */
66 static int opt_u; /* Show Unix domain sockets */
67 static int opt_v; /* Verbose mode */
70 * Default protocols to use if no -P was defined.
72 static const char *default_protos[] = {"tcp", "udp", "divert" };
73 static size_t default_numprotos =
74 sizeof(default_protos) / sizeof(default_protos[0]);
76 static int *protos; /* protocols to use */
77 static size_t numprotos; /* allocated size of protos[] */
81 #define INT_BIT (sizeof(int)*CHAR_BIT)
82 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
83 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
91 const char *protoname;
92 struct sockaddr_storage laddr;
93 struct sockaddr_storage faddr;
98 static struct sock *sockhash[HASHSIZE];
100 static struct xfile *xfiles;
104 xprintf(const char *fmt, ...)
110 len = vprintf(fmt, ap);
119 get_proto_type(const char *proto)
121 struct protoent *pent;
123 if (strlen(proto) == 0)
125 pent = getprotobyname(proto);
127 warn("getprotobyname");
130 return (pent->p_proto);
134 static void init_protos(int num)
141 /* Find the maximum number of possible protocols. */
142 while (getprotoent() != NULL)
147 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
149 numprotos = proto_count;
154 parse_protos(char *protospec)
157 char *tmp = protospec;
158 int proto_type, proto_index;
160 if (protospec == NULL)
165 while ((prot = strsep(&tmp, ",")) != NULL) {
166 if (strlen(prot) == 0)
168 proto_type = get_proto_type(prot);
169 if (proto_type != -1)
170 protos[proto_index++] = proto_type;
172 numprotos = proto_index;
173 return (proto_index);
178 parse_ports(const char *portspec)
184 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
189 errx(1, "syntax error in port range");
190 for (q = p; *q != '\0' && isdigit(*q); ++q)
192 for (port = 0; p < q; ++p)
193 port = port * 10 + digittoint(*p);
194 if (port < 0 || port > 65535)
195 errx(1, "invalid port number");
208 for (q = p; *q != '\0' && isdigit(*q); ++q)
210 for (end = 0; p < q; ++p)
211 end = end * 10 + digittoint(*p);
212 if (end < port || end > 65535)
213 errx(1, "invalid port number");
222 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
224 struct sockaddr_in *sin4;
225 struct sockaddr_in6 *sin6;
227 bzero(sa, sizeof *sa);
230 sin4 = (struct sockaddr_in *)sa;
231 sin4->sin_len = sizeof *sin4;
232 sin4->sin_family = af;
233 sin4->sin_port = port;
234 sin4->sin_addr = *(struct in_addr *)addr;
237 sin6 = (struct sockaddr_in6 *)sa;
238 sin6->sin6_len = sizeof *sin6;
239 sin6->sin6_family = af;
240 sin6->sin6_port = port;
241 sin6->sin6_addr = *(struct in6_addr *)addr;
249 gather_inet(int proto)
251 struct xinpgen *xig, *exig;
257 const char *varname, *protoname;
260 int hash, retry, vflag;
270 varname = "net.inet.tcp.pcblist";
274 varname = "net.inet.udp.pcblist";
278 varname = "net.inet.divert.pcblist";
282 errx(1, "protocol %d not supported", proto);
290 if ((buf = realloc(buf, bufsize)) == NULL)
293 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
298 err(1, "sysctlbyname()");
301 xig = (struct xinpgen *)buf;
302 exig = (struct xinpgen *)(void *)
303 ((char *)buf + len - sizeof *exig);
304 if (xig->xig_len != sizeof *xig ||
305 exig->xig_len != sizeof *exig)
306 errx(1, "struct xinpgen size mismatch");
307 } while (xig->xig_gen != exig->xig_gen && retry--);
309 if (xig->xig_gen != exig->xig_gen && opt_v)
310 warnx("warning: data may be inconsistent");
313 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
318 xtp = (struct xtcpcb *)xig;
319 if (xtp->xt_len != sizeof *xtp) {
320 warnx("struct xtcpcb size mismatch");
324 so = &xtp->xt_socket;
328 xip = (struct xinpcb *)xig;
329 if (xip->xi_len != sizeof *xip) {
330 warnx("struct xinpcb size mismatch");
334 so = &xip->xi_socket;
337 errx(1, "protocol %d not supported", proto);
339 if ((inp->inp_vflag & vflag) == 0)
341 if (inp->inp_vflag & INP_IPV4) {
342 if ((inp->inp_fport == 0 && !opt_l) ||
343 (inp->inp_fport != 0 && !opt_c))
345 } else if (inp->inp_vflag & INP_IPV6) {
346 if ((inp->inp_fport == 0 && !opt_l) ||
347 (inp->inp_fport != 0 && !opt_c))
351 warnx("invalid vflag 0x%x", inp->inp_vflag);
354 if ((sock = calloc(1, sizeof *sock)) == NULL)
356 sock->socket = so->xso_so;
358 if (inp->inp_vflag & INP_IPV4) {
359 sock->family = AF_INET;
360 sockaddr(&sock->laddr, sock->family,
361 &inp->inp_laddr, inp->inp_lport);
362 sockaddr(&sock->faddr, sock->family,
363 &inp->inp_faddr, inp->inp_fport);
364 } else if (inp->inp_vflag & INP_IPV6) {
365 sock->family = AF_INET6;
366 sockaddr(&sock->laddr, sock->family,
367 &inp->in6p_laddr, inp->inp_lport);
368 sockaddr(&sock->faddr, sock->family,
369 &inp->in6p_faddr, inp->inp_fport);
371 sock->vflag = inp->inp_vflag;
372 sock->protoname = protoname;
373 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
374 sock->next = sockhash[hash];
375 sockhash[hash] = sock;
382 gather_unix(int proto)
384 struct xunpgen *xug, *exug;
387 const char *varname, *protoname;
394 varname = "net.local.stream.pcblist";
395 protoname = "stream";
398 varname = "net.local.dgram.pcblist";
409 if ((buf = realloc(buf, bufsize)) == NULL)
412 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
415 err(1, "sysctlbyname()");
418 xug = (struct xunpgen *)buf;
419 exug = (struct xunpgen *)(void *)
420 ((char *)buf + len - sizeof *exug);
421 if (xug->xug_len != sizeof *xug ||
422 exug->xug_len != sizeof *exug) {
423 warnx("struct xinpgen size mismatch");
426 } while (xug->xug_gen != exug->xug_gen && retry--);
428 if (xug->xug_gen != exug->xug_gen && opt_v)
429 warnx("warning: data may be inconsistent");
432 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
435 xup = (struct xunpcb *)xug;
436 if (xup->xu_len != sizeof *xup) {
437 warnx("struct xunpcb size mismatch");
440 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
441 (xup->xu_unp.unp_conn != NULL && !opt_c))
443 if ((sock = calloc(1, sizeof *sock)) == NULL)
445 sock->socket = xup->xu_socket.xso_so;
446 sock->pcb = xup->xu_unpp;
448 sock->family = AF_UNIX;
449 sock->protoname = protoname;
450 if (xup->xu_unp.unp_addr != NULL)
452 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
453 else if (xup->xu_unp.unp_conn != NULL)
454 *(void **)&sock->faddr = xup->xu_unp.unp_conn;
455 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
456 sock->next = sockhash[hash];
457 sockhash[hash] = sock;
468 if ((xfiles = malloc(len = sizeof *xfiles)) == NULL)
470 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
472 err(1, "sysctlbyname()");
474 if ((xfiles = realloc(xfiles, len)) == NULL)
477 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
478 errx(1, "struct xfile size mismatch");
479 nxfiles = len / sizeof *xfiles;
483 printaddr(int af, struct sockaddr_storage *ss)
485 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
486 struct sockaddr_un *sun;
492 addr = &((struct sockaddr_in *)ss)->sin_addr;
493 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
495 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
498 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
499 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
501 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
504 sun = (struct sockaddr_un *)ss;
505 off = (int)((char *)&sun->sun_path - (char *)sun);
506 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
508 if (addrstr[0] == '\0')
509 inet_ntop(af, addr, addrstr, sizeof addrstr);
511 return xprintf("%s:*", addrstr);
513 return xprintf("%s:%d", addrstr, port);
517 getprocname(pid_t pid)
519 static struct kinfo_proc proc;
525 mib[2] = KERN_PROC_PID;
528 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
529 /* Do not warn if the process exits before we get its name. */
534 return (proc.ki_comm);
538 check_ports(struct sock *s)
544 if ((s->family != AF_INET) && (s->family != AF_INET6))
546 if (s->family == AF_INET)
547 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port);
549 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port);
552 if (s->family == AF_INET)
553 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port);
555 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port);
570 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
571 "USER", "COMMAND", "PID", "FD", "PROTO",
572 "LOCAL ADDRESS", "FOREIGN ADDRESS");
574 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
575 if (xf->xf_data == NULL)
577 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
578 for (s = sockhash[hash]; s != NULL; s = s->next)
579 if ((void *)s->socket == xf->xf_data)
586 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
587 pos += xprintf("%lu", (u_long)xf->xf_uid);
589 pos += xprintf("%s", pwd->pw_name);
592 pos += xprintf("%.10s", getprocname(xf->xf_pid));
595 pos += xprintf("%lu", (u_long)xf->xf_pid);
598 pos += xprintf("%d", xf->xf_fd);
601 pos += xprintf("%s", s->protoname);
602 if (s->vflag & INP_IPV4)
604 if (s->vflag & INP_IPV6)
611 pos += printaddr(s->family, &s->laddr);
614 pos += printaddr(s->family, &s->faddr);
618 if (s->laddr.ss_len > 0) {
619 pos += printaddr(s->family, &s->laddr);
623 p = *(void **)&s->faddr;
625 pos += xprintf("(not connected)");
628 pos += xprintf("-> ");
629 for (hash = 0; hash < HASHSIZE; ++hash) {
630 for (s = sockhash[hash]; s != NULL; s = s->next)
636 if (s == NULL || s->laddr.ss_len == 0)
637 pos += xprintf("??");
639 pos += printaddr(s->family, &s->laddr);
648 static int set_default_protos(void)
650 struct protoent *prot;
654 init_protos(default_numprotos);
656 for (pindex = 0; pindex < default_numprotos; pindex++) {
657 pname = default_protos[pindex];
658 prot = getprotobyname(pname);
660 err(1, "getprotobyname: %s", pname);
661 protos[pindex] = prot->p_proto;
672 "Usage: sockstat [-46clu] [-p ports] [-P protocols]\n");
677 main(int argc, char *argv[])
679 int protos_defined = -1;
682 while ((o = getopt(argc, argv, "46clp:P:uv")) != -1)
700 protos_defined = parse_protos(optarg);
718 if ((!opt_4 && !opt_6) && protos_defined != -1)
720 if (!opt_4 && !opt_6 && !opt_u)
721 opt_4 = opt_6 = opt_u = 1;
722 if ((opt_4 || opt_6) && protos_defined == -1)
723 protos_defined = set_default_protos();
724 if (!opt_c && !opt_l)
727 if (opt_4 || opt_6) {
728 for (i = 0; i < protos_defined; i++)
729 gather_inet(protos[i]);
732 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
733 gather_unix(SOCK_STREAM);
734 gather_unix(SOCK_DGRAM);