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 */
71 #define INT_BIT (sizeof(int)*CHAR_BIT)
72 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
73 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
81 const char *protoname;
82 struct sockaddr_storage laddr;
83 struct sockaddr_storage faddr;
88 static struct sock *sockhash[HASHSIZE];
90 static struct xfile *xfiles;
94 xprintf(const char *fmt, ...)
100 len = vprintf(fmt, ap);
108 parse_ports(const char *portspec)
114 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
119 errx(1, "syntax error in port range");
120 for (q = p; *q != '\0' && isdigit(*q); ++q)
122 for (port = 0; p < q; ++p)
123 port = port * 10 + digittoint(*p);
124 if (port < 0 || port > 65535)
125 errx(1, "invalid port number");
138 for (q = p; *q != '\0' && isdigit(*q); ++q)
140 for (end = 0; p < q; ++p)
141 end = end * 10 + digittoint(*p);
142 if (end < port || end > 65535)
143 errx(1, "invalid port number");
152 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
154 struct sockaddr_in *sin4;
155 struct sockaddr_in6 *sin6;
157 bzero(sa, sizeof *sa);
160 sin4 = (struct sockaddr_in *)sa;
161 sin4->sin_len = sizeof *sin4;
162 sin4->sin_family = af;
163 sin4->sin_port = port;
164 sin4->sin_addr = *(struct in_addr *)addr;
167 sin6 = (struct sockaddr_in6 *)sa;
168 sin6->sin6_len = sizeof *sin6;
169 sin6->sin6_family = af;
170 sin6->sin6_port = port;
171 sin6->sin6_addr = *(struct in6_addr *)addr;
179 gather_inet(int proto)
181 struct xinpgen *xig, *exig;
187 const char *varname, *protoname;
190 int hash, retry, vflag;
200 varname = "net.inet.tcp.pcblist";
204 varname = "net.inet.udp.pcblist";
208 varname = "net.inet.divert.pcblist";
220 if ((buf = realloc(buf, bufsize)) == NULL)
223 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
228 err(1, "sysctlbyname()");
231 xig = (struct xinpgen *)buf;
232 exig = (struct xinpgen *)(void *)
233 ((char *)buf + len - sizeof *exig);
234 if (xig->xig_len != sizeof *xig ||
235 exig->xig_len != sizeof *exig)
236 errx(1, "struct xinpgen size mismatch");
237 } while (xig->xig_gen != exig->xig_gen && retry--);
239 if (xig->xig_gen != exig->xig_gen && opt_v)
240 warnx("warning: data may be inconsistent");
243 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
248 xtp = (struct xtcpcb *)xig;
249 if (xtp->xt_len != sizeof *xtp) {
250 warnx("struct xtcpcb size mismatch");
254 so = &xtp->xt_socket;
258 xip = (struct xinpcb *)xig;
259 if (xip->xi_len != sizeof *xip) {
260 warnx("struct xinpcb size mismatch");
264 so = &xip->xi_socket;
269 if ((inp->inp_vflag & vflag) == 0)
271 if (inp->inp_vflag & INP_IPV4) {
272 if ((inp->inp_fport == 0 && !opt_l) ||
273 (inp->inp_fport != 0 && !opt_c))
275 } else if (inp->inp_vflag & INP_IPV6) {
276 if ((inp->in6p_fport == 0 && !opt_l) ||
277 (inp->in6p_fport != 0 && !opt_c))
281 warnx("invalid vflag 0x%x", inp->inp_vflag);
285 if ((sock = calloc(1, sizeof *sock)) == NULL)
287 sock->socket = so->xso_so;
289 if (inp->inp_vflag & INP_IPV4) {
290 sock->family = AF_INET;
291 sockaddr(&sock->laddr, sock->family,
292 &inp->inp_laddr, inp->inp_lport);
293 sockaddr(&sock->faddr, sock->family,
294 &inp->inp_faddr, inp->inp_fport);
295 } else if (inp->inp_vflag & INP_IPV6) {
296 sock->family = AF_INET6;
297 sockaddr(&sock->laddr, sock->family,
298 &inp->in6p_laddr, inp->in6p_lport);
299 sockaddr(&sock->faddr, sock->family,
300 &inp->in6p_faddr, inp->in6p_fport);
302 sock->vflag = inp->inp_vflag;
303 sock->protoname = protoname;
304 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
305 sock->next = sockhash[hash];
306 sockhash[hash] = sock;
313 gather_unix(int proto)
315 struct xunpgen *xug, *exug;
318 const char *varname, *protoname;
325 varname = "net.local.stream.pcblist";
326 protoname = "stream";
329 varname = "net.local.dgram.pcblist";
340 if ((buf = realloc(buf, bufsize)) == NULL)
343 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
346 err(1, "sysctlbyname()");
349 xug = (struct xunpgen *)buf;
350 exug = (struct xunpgen *)(void *)
351 ((char *)buf + len - sizeof *exug);
352 if (xug->xug_len != sizeof *xug ||
353 exug->xug_len != sizeof *exug) {
354 warnx("struct xinpgen size mismatch");
357 } while (xug->xug_gen != exug->xug_gen && retry--);
359 if (xug->xug_gen != exug->xug_gen && opt_v)
360 warnx("warning: data may be inconsistent");
363 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
366 xup = (struct xunpcb *)xug;
367 if (xup->xu_len != sizeof *xup) {
368 warnx("struct xunpcb size mismatch");
371 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
372 (xup->xu_unp.unp_conn != NULL && !opt_c))
374 if ((sock = calloc(1, sizeof *sock)) == NULL)
376 sock->socket = xup->xu_socket.xso_so;
377 sock->pcb = xup->xu_unpp;
379 sock->family = AF_UNIX;
380 sock->protoname = protoname;
381 if (xup->xu_unp.unp_addr != NULL)
383 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
384 else if (xup->xu_unp.unp_conn != NULL)
385 *(void **)&sock->faddr = xup->xu_unp.unp_conn;
386 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
387 sock->next = sockhash[hash];
388 sockhash[hash] = sock;
399 if ((xfiles = malloc(len = sizeof *xfiles)) == NULL)
401 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
403 err(1, "sysctlbyname()");
405 if ((xfiles = realloc(xfiles, len)) == NULL)
408 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
409 errx(1, "struct xfile size mismatch");
410 nxfiles = len / sizeof *xfiles;
414 printaddr(int af, struct sockaddr_storage *ss)
416 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
417 struct sockaddr_un *sun;
423 addr = &((struct sockaddr_in *)ss)->sin_addr;
424 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
426 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
429 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
430 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
432 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
435 sun = (struct sockaddr_un *)ss;
436 off = (int)((char *)&sun->sun_path - (char *)sun);
437 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
439 if (addrstr[0] == '\0')
440 inet_ntop(af, addr, addrstr, sizeof addrstr);
442 return xprintf("%s:*", addrstr);
444 return xprintf("%s:%d", addrstr, port);
448 getprocname(pid_t pid)
450 static struct kinfo_proc proc;
456 mib[2] = KERN_PROC_PID;
459 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
463 return (proc.ki_ocomm);
467 check_ports(struct sock *s)
473 if ((s->family != AF_INET) && (s->family != AF_INET6))
475 if (s->family == AF_INET)
476 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port);
478 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port);
481 if (s->family == AF_INET)
482 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port);
484 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port);
499 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
500 "USER", "COMMAND", "PID", "FD", "PROTO",
501 "LOCAL ADDRESS", "FOREIGN ADDRESS");
503 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
504 if (xf->xf_data == NULL)
506 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
507 for (s = sockhash[hash]; s != NULL; s = s->next)
508 if ((void *)s->socket == xf->xf_data)
515 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
516 pos += xprintf("%lu", (u_long)xf->xf_uid);
518 pos += xprintf("%s", pwd->pw_name);
521 pos += xprintf("%.10s", getprocname(xf->xf_pid));
524 pos += xprintf("%lu", (u_long)xf->xf_pid);
527 pos += xprintf("%d", xf->xf_fd);
530 pos += xprintf("%s", s->protoname);
531 if (s->vflag & INP_IPV4)
533 if (s->vflag & INP_IPV6)
540 pos += printaddr(s->family, &s->laddr);
543 pos += printaddr(s->family, &s->faddr);
547 if (s->laddr.ss_len > 0) {
548 pos += printaddr(s->family, &s->laddr);
552 p = *(void **)&s->faddr;
554 pos += xprintf("(not connected)");
557 pos += xprintf("-> ");
558 for (hash = 0; hash < HASHSIZE; ++hash) {
559 for (s = sockhash[hash]; s != NULL; s = s->next)
565 if (s == NULL || s->laddr.ss_len == 0)
566 pos += xprintf("??");
568 pos += printaddr(s->family, &s->laddr);
580 fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n");
585 main(int argc, char *argv[])
589 while ((o = getopt(argc, argv, "46clp:uv")) != -1)
622 if (!opt_4 && !opt_6 && !opt_u)
623 opt_4 = opt_6 = opt_u = 1;
624 if (!opt_c && !opt_l)
627 if (opt_4 || opt_6) {
628 gather_inet(IPPROTO_TCP);
629 gather_inet(IPPROTO_UDP);
630 gather_inet(IPPROTO_DIVERT);
633 gather_unix(SOCK_STREAM);
634 gather_unix(SOCK_DGRAM);