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)))
92 const char *protoname;
93 struct sockaddr_storage laddr;
94 struct sockaddr_storage faddr;
99 static struct sock *sockhash[HASHSIZE];
101 static struct xfile *xfiles;
105 xprintf(const char *fmt, ...)
111 len = vprintf(fmt, ap);
120 get_proto_type(const char *proto)
122 struct protoent *pent;
124 if (strlen(proto) == 0)
126 pent = getprotobyname(proto);
128 warn("getprotobyname");
131 return (pent->p_proto);
135 static void init_protos(int num)
142 /* Find the maximum number of possible protocols. */
143 while (getprotoent() != NULL)
148 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
150 numprotos = proto_count;
155 parse_protos(char *protospec)
158 char *tmp = protospec;
159 int proto_type, proto_index;
161 if (protospec == NULL)
166 while ((prot = strsep(&tmp, ",")) != NULL) {
167 if (strlen(prot) == 0)
169 proto_type = get_proto_type(prot);
170 if (proto_type != -1)
171 protos[proto_index++] = proto_type;
173 numprotos = proto_index;
174 return (proto_index);
179 parse_ports(const char *portspec)
185 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
190 errx(1, "syntax error in port range");
191 for (q = p; *q != '\0' && isdigit(*q); ++q)
193 for (port = 0; p < q; ++p)
194 port = port * 10 + digittoint(*p);
195 if (port < 0 || port > 65535)
196 errx(1, "invalid port number");
209 for (q = p; *q != '\0' && isdigit(*q); ++q)
211 for (end = 0; p < q; ++p)
212 end = end * 10 + digittoint(*p);
213 if (end < port || end > 65535)
214 errx(1, "invalid port number");
223 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
225 struct sockaddr_in *sin4;
226 struct sockaddr_in6 *sin6;
228 bzero(sa, sizeof *sa);
231 sin4 = (struct sockaddr_in *)sa;
232 sin4->sin_len = sizeof *sin4;
233 sin4->sin_family = af;
234 sin4->sin_port = port;
235 sin4->sin_addr = *(struct in_addr *)addr;
238 sin6 = (struct sockaddr_in6 *)sa;
239 sin6->sin6_len = sizeof *sin6;
240 sin6->sin6_family = af;
241 sin6->sin6_port = port;
242 sin6->sin6_addr = *(struct in6_addr *)addr;
250 gather_inet(int proto)
252 struct xinpgen *xig, *exig;
258 const char *varname, *protoname;
261 int hash, retry, vflag;
271 varname = "net.inet.tcp.pcblist";
275 varname = "net.inet.udp.pcblist";
279 varname = "net.inet.divert.pcblist";
283 errx(1, "protocol %d not supported", proto);
291 if ((buf = realloc(buf, bufsize)) == NULL)
294 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
299 err(1, "sysctlbyname()");
302 xig = (struct xinpgen *)buf;
303 exig = (struct xinpgen *)(void *)
304 ((char *)buf + len - sizeof *exig);
305 if (xig->xig_len != sizeof *xig ||
306 exig->xig_len != sizeof *exig)
307 errx(1, "struct xinpgen size mismatch");
308 } while (xig->xig_gen != exig->xig_gen && retry--);
310 if (xig->xig_gen != exig->xig_gen && opt_v)
311 warnx("warning: data may be inconsistent");
314 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
319 xtp = (struct xtcpcb *)xig;
320 if (xtp->xt_len != sizeof *xtp) {
321 warnx("struct xtcpcb size mismatch");
325 so = &xtp->xt_socket;
329 xip = (struct xinpcb *)xig;
330 if (xip->xi_len != sizeof *xip) {
331 warnx("struct xinpcb size mismatch");
335 so = &xip->xi_socket;
338 errx(1, "protocol %d not supported", proto);
340 if ((inp->inp_vflag & vflag) == 0)
342 if (inp->inp_vflag & INP_IPV4) {
343 if ((inp->inp_fport == 0 && !opt_l) ||
344 (inp->inp_fport != 0 && !opt_c))
346 #define __IN_IS_ADDR_LOOPBACK(pina) \
347 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
349 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
350 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
352 #undef __IN_IS_ADDR_LOOPBACK
353 } else if (inp->inp_vflag & INP_IPV6) {
354 if ((inp->inp_fport == 0 && !opt_l) ||
355 (inp->inp_fport != 0 && !opt_c))
358 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
359 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
363 warnx("invalid vflag 0x%x", inp->inp_vflag);
366 if ((sock = calloc(1, sizeof *sock)) == NULL)
368 sock->socket = so->xso_so;
370 if (inp->inp_vflag & INP_IPV4) {
371 sock->family = AF_INET;
372 sockaddr(&sock->laddr, sock->family,
373 &inp->inp_laddr, inp->inp_lport);
374 sockaddr(&sock->faddr, sock->family,
375 &inp->inp_faddr, inp->inp_fport);
376 } else if (inp->inp_vflag & INP_IPV6) {
377 sock->family = AF_INET6;
378 sockaddr(&sock->laddr, sock->family,
379 &inp->in6p_laddr, inp->inp_lport);
380 sockaddr(&sock->faddr, sock->family,
381 &inp->in6p_faddr, inp->inp_fport);
383 sock->vflag = inp->inp_vflag;
384 sock->protoname = protoname;
385 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
386 sock->next = sockhash[hash];
387 sockhash[hash] = sock;
394 gather_unix(int proto)
396 struct xunpgen *xug, *exug;
399 const char *varname, *protoname;
406 varname = "net.local.stream.pcblist";
407 protoname = "stream";
410 varname = "net.local.dgram.pcblist";
421 if ((buf = realloc(buf, bufsize)) == NULL)
424 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
427 err(1, "sysctlbyname()");
430 xug = (struct xunpgen *)buf;
431 exug = (struct xunpgen *)(void *)
432 ((char *)buf + len - sizeof *exug);
433 if (xug->xug_len != sizeof *xug ||
434 exug->xug_len != sizeof *exug) {
435 warnx("struct xinpgen size mismatch");
438 } while (xug->xug_gen != exug->xug_gen && retry--);
440 if (xug->xug_gen != exug->xug_gen && opt_v)
441 warnx("warning: data may be inconsistent");
444 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
447 xup = (struct xunpcb *)xug;
448 if (xup->xu_len != sizeof *xup) {
449 warnx("struct xunpcb size mismatch");
452 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
453 (xup->xu_unp.unp_conn != NULL && !opt_c))
455 if ((sock = calloc(1, sizeof *sock)) == NULL)
457 sock->socket = xup->xu_socket.xso_so;
458 sock->pcb = xup->xu_unpp;
460 sock->family = AF_UNIX;
461 sock->protoname = protoname;
462 if (xup->xu_unp.unp_addr != NULL)
464 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
465 else if (xup->xu_unp.unp_conn != NULL)
466 *(void **)&sock->faddr = xup->xu_unp.unp_conn;
467 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
468 sock->next = sockhash[hash];
469 sockhash[hash] = sock;
480 if ((xfiles = malloc(len = sizeof *xfiles)) == NULL)
482 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
484 err(1, "sysctlbyname()");
486 if ((xfiles = realloc(xfiles, len)) == NULL)
489 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
490 errx(1, "struct xfile size mismatch");
491 nxfiles = len / sizeof *xfiles;
495 printaddr(int af, struct sockaddr_storage *ss)
497 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
498 struct sockaddr_un *sun;
504 addr = &((struct sockaddr_in *)ss)->sin_addr;
505 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
507 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
510 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
511 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
513 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
516 sun = (struct sockaddr_un *)ss;
517 off = (int)((char *)&sun->sun_path - (char *)sun);
518 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
520 if (addrstr[0] == '\0')
521 inet_ntop(af, addr, addrstr, sizeof addrstr);
523 return xprintf("%s:*", addrstr);
525 return xprintf("%s:%d", addrstr, port);
529 getprocname(pid_t pid)
531 static struct kinfo_proc proc;
537 mib[2] = KERN_PROC_PID;
540 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
541 /* Do not warn if the process exits before we get its name. */
546 return (proc.ki_comm);
550 check_ports(struct sock *s)
556 if ((s->family != AF_INET) && (s->family != AF_INET6))
558 if (s->family == AF_INET)
559 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port);
561 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port);
564 if (s->family == AF_INET)
565 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port);
567 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port);
582 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
583 "USER", "COMMAND", "PID", "FD", "PROTO",
584 "LOCAL ADDRESS", "FOREIGN ADDRESS");
586 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
587 if (xf->xf_data == NULL)
589 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
590 for (s = sockhash[hash]; s != NULL; s = s->next)
591 if ((void *)s->socket == xf->xf_data)
598 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
599 pos += xprintf("%lu ", (u_long)xf->xf_uid);
601 pos += xprintf("%s ", pwd->pw_name);
604 pos += xprintf("%.10s", getprocname(xf->xf_pid));
607 pos += xprintf("%lu ", (u_long)xf->xf_pid);
610 pos += xprintf("%d ", xf->xf_fd);
613 pos += xprintf("%s", s->protoname);
614 if (s->vflag & INP_IPV4)
615 pos += xprintf("4 ");
616 if (s->vflag & INP_IPV6)
617 pos += xprintf("6 ");
623 pos += printaddr(s->family, &s->laddr);
624 if (s->family == AF_INET6 && pos >= 58)
628 pos += printaddr(s->family, &s->faddr);
632 if (s->laddr.ss_len > 0) {
633 pos += printaddr(s->family, &s->laddr);
637 p = *(void **)&s->faddr;
639 pos += xprintf("(not connected)");
642 pos += xprintf("-> ");
643 for (hash = 0; hash < HASHSIZE; ++hash) {
644 for (s = sockhash[hash]; s != NULL; s = s->next)
650 if (s == NULL || s->laddr.ss_len == 0)
651 pos += xprintf("??");
653 pos += printaddr(s->family, &s->laddr);
662 static int set_default_protos(void)
664 struct protoent *prot;
668 init_protos(default_numprotos);
670 for (pindex = 0; pindex < default_numprotos; pindex++) {
671 pname = default_protos[pindex];
672 prot = getprotobyname(pname);
674 err(1, "getprotobyname: %s", pname);
675 protos[pindex] = prot->p_proto;
686 "Usage: sockstat [-46cLlu] [-p ports] [-P protocols]\n");
691 main(int argc, char *argv[])
693 int protos_defined = -1;
696 while ((o = getopt(argc, argv, "46cLlp:P:uv")) != -1)
717 protos_defined = parse_protos(optarg);
735 if ((!opt_4 && !opt_6) && protos_defined != -1)
737 if (!opt_4 && !opt_6 && !opt_u)
738 opt_4 = opt_6 = opt_u = 1;
739 if ((opt_4 || opt_6) && protos_defined == -1)
740 protos_defined = set_default_protos();
741 if (!opt_c && !opt_l)
744 if (opt_4 || opt_6) {
745 for (i = 0; i < protos_defined; i++)
746 gather_inet(protos[i]);
749 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
750 gather_unix(SOCK_STREAM);
751 gather_unix(SOCK_DGRAM);