2 * Copyright (c) 2005 Joerg Sonnenberger <joerg@bec.de>. All rights reserved.
3 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 $
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/sysctl.h>
39 #include <sys/unpcb.h>
41 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <netinet/in_pcb.h>
45 #include <netinet/tcp.h>
46 #include <netinet/tcp_seq.h>
47 #include <netinet/tcp_var.h>
48 #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)))
80 const char *protoname;
81 struct sockaddr_storage laddr;
82 struct sockaddr_storage faddr;
86 static int xprintf(const char *, ...) __printflike(1, 2);
89 static struct sock *sockhash[HASHSIZE];
91 static struct kinfo_file *xfiles;
92 static size_t nxfiles;
95 xprintf(const char *fmt, ...)
101 len = vprintf(fmt, ap);
109 parse_ports(const char *portspec)
115 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
120 errx(1, "syntax error in port range");
121 for (q = p; *q != '\0' && isdigit(*q); ++q)
123 for (port = 0; p < q; ++p)
124 port = port * 10 + (*p - '0');
125 if (port < 0 || port > 65535)
126 errx(1, "invalid port number");
139 for (q = p; *q != '\0' && isdigit(*q); ++q)
141 for (end = 0; p < q; ++p)
142 end = end * 10 + (*p - '0');
143 if (end < port || end > 65535)
144 errx(1, "invalid port number");
153 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
155 struct sockaddr_in *sin4;
156 struct sockaddr_in6 *sin6;
158 bzero(sa, sizeof *sa);
161 sin4 = (struct sockaddr_in *)sa;
162 sin4->sin_len = sizeof *sin4;
163 sin4->sin_family = af;
164 sin4->sin_port = port;
165 sin4->sin_addr = *(struct in_addr *)addr;
168 sin6 = (struct sockaddr_in6 *)sa;
169 sin6->sin6_len = sizeof *sin6;
170 sin6->sin6_family = af;
171 sin6->sin6_port = port;
172 sin6->sin6_addr = *(struct in6_addr *)addr;
180 gather_inet(int proto)
182 void *so_begin, *so_end;
188 const char *varname, *protoname;
195 varname = "net.inet.tcp.pcblist";
199 varname = "net.inet.udp.pcblist";
203 varname = "net.inet.divert.pcblist";
213 if (sysctlbyname(varname, NULL, &len, NULL, 0)) {
216 err(1, "fetching %s", varname);
218 if ((buf = malloc(len)) == NULL)
220 if (sysctlbyname(varname, buf, &len, NULL, 0)) {
223 err(1, "fetching %s", varname);
227 so_end = (uint8_t *)buf + len;
229 for (so_begin = buf, so_end = (uint8_t *)so_begin + len;
230 (uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end &&
231 (uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end;
232 so_begin = (uint8_t *)so_begin + *(size_t *)so_begin) {
235 xtp = (struct xtcpcb *)so_begin;
236 if (xtp->xt_len != sizeof *xtp) {
237 warnx("struct xtcpcb size mismatch");
241 so = &xtp->xt_socket;
245 xip = (struct xinpcb *)so_begin;
246 if (xip->xi_len != sizeof *xip) {
247 warnx("struct xinpcb size mismatch");
251 so = &xip->xi_socket;
256 if ((INP_ISIPV4(inp) && !opt_4) || (INP_ISIPV6(inp) && !opt_6))
258 if (INP_ISIPV4(inp)) {
259 if ((inp->inp_fport == 0 && !opt_l) ||
260 (inp->inp_fport != 0 && !opt_c))
262 } else if (INP_ISIPV6(inp)) {
263 if ((inp->in6p_fport == 0 && !opt_l) ||
264 (inp->in6p_fport != 0 && !opt_c))
268 warnx("invalid af 0x%x", inp->inp_af);
271 if ((sock = calloc(1, sizeof *sock)) == NULL)
273 sock->socket = so->xso_so;
275 if (INP_ISIPV4(inp)) {
276 sock->family = AF_INET;
277 sockaddr(&sock->laddr, sock->family,
278 &inp->inp_laddr, inp->inp_lport);
279 sockaddr(&sock->faddr, sock->family,
280 &inp->inp_faddr, inp->inp_fport);
281 } else if (INP_ISIPV6(inp)) {
282 sock->family = AF_INET6;
283 sockaddr(&sock->laddr, sock->family,
284 &inp->in6p_laddr, inp->in6p_lport);
285 sockaddr(&sock->faddr, sock->family,
286 &inp->in6p_faddr, inp->in6p_fport);
288 sock->protoname = protoname;
289 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
290 sock->next = sockhash[hash];
291 sockhash[hash] = sock;
298 gather_unix(int proto)
300 void *so_begin, *so_end;
303 const char *varname, *protoname;
310 varname = "net.local.stream.pcblist";
311 protoname = "stream";
314 varname = "net.local.dgram.pcblist";
324 if (sysctlbyname(varname, NULL, &len, NULL, 0))
325 err(1, "fetching %s", varname);
327 if ((buf = malloc(len)) == NULL)
329 if (sysctlbyname(varname, buf, &len, NULL, 0))
330 err(1, "fetching %s", varname);
332 for (so_begin = buf, so_end = (uint8_t *)buf + len;
333 (uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end &&
334 (uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end;
335 so_begin = (uint8_t *)so_begin + *(size_t *)so_begin) {
337 if (xup->xu_len != sizeof *xup) {
338 warnx("struct xunpcb size mismatch");
341 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
342 (xup->xu_unp.unp_conn != NULL && !opt_c))
344 if ((sock = calloc(1, sizeof *sock)) == NULL)
346 sock->socket = xup->xu_socket.xso_so;
347 sock->pcb = xup->xu_unpp;
349 sock->family = AF_UNIX;
350 sock->protoname = protoname;
351 if (xup->xu_unp.unp_addr != NULL)
353 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
354 else if (xup->xu_unp.unp_conn != NULL)
355 *(void **)&sock->faddr = xup->xu_unp.unp_conn;
356 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
357 sock->next = sockhash[hash];
358 sockhash[hash] = sock;
367 if (kinfo_get_files(&xfiles, &nxfiles))
368 err(1, "kinfo_get_files");
372 printaddr(int af, struct sockaddr_storage *ss)
374 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
375 struct sockaddr_un *sun;
381 addr = &((struct sockaddr_in *)ss)->sin_addr;
382 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
384 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
387 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
388 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
390 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
393 sun = (struct sockaddr_un *)ss;
394 off = (int)((char *)&sun->sun_path - (char *)sun);
395 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
399 if (addrstr[0] == '\0')
400 inet_ntop(af, addr, addrstr, sizeof addrstr);
402 return xprintf("%s:*", addrstr);
404 return xprintf("%s:%d", addrstr, port);
408 getprocname(pid_t pid)
410 static struct kinfo_proc proc;
416 mib[2] = KERN_PROC_PID;
419 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
423 return (proc.kp_comm);
427 check_ports(struct sock *s)
433 if ((s->family != AF_INET) && (s->family != AF_INET6))
435 if (s->family == AF_INET)
436 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port);
438 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port);
441 if (s->family == AF_INET)
442 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port);
444 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port);
454 struct kinfo_file *xf;
459 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
460 "USER", "COMMAND", "PID", "FD", "PROTO",
461 "LOCAL ADDRESS", "FOREIGN ADDRESS");
463 for (xf = xfiles, n = 0; n < (int)nxfiles; ++n, ++xf) {
464 if (xf->f_data == NULL)
466 hash = (int)((uintptr_t)xf->f_data % HASHSIZE);
467 for (s = sockhash[hash]; s != NULL; s = s->next)
468 if ((void *)s->socket == xf->f_data)
475 if ((pwd = getpwuid(xf->f_uid)) == NULL)
476 pos += xprintf("%lu", (u_long)xf->f_uid);
478 pos += xprintf("%s", pwd->pw_name);
481 pos += xprintf("%.10s", getprocname(xf->f_pid));
484 pos += xprintf("%lu", (u_long)xf->f_pid);
487 pos += xprintf("%d", xf->f_fd);
490 pos += xprintf("%s", s->protoname);
491 if (s->family == AF_INET)
493 if (s->family == AF_INET6)
500 pos += printaddr(s->family, &s->laddr);
504 pos += printaddr(s->family, &s->faddr);
508 if (s->laddr.ss_len > 0) {
509 pos += printaddr(s->family, &s->laddr);
513 p = *(void **)&s->faddr;
515 pos += xprintf("(not connected)");
518 pos += xprintf("-> ");
519 for (hash = 0; hash < HASHSIZE; ++hash) {
520 for (s = sockhash[hash]; s != NULL; s = s->next)
526 if (s == NULL || s->laddr.ss_len == 0)
527 pos += xprintf("??");
529 pos += printaddr(s->family, &s->laddr);
541 fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n");
546 main(int argc, char *argv[])
550 while ((o = getopt(argc, argv, "46clp:uv")) != -1)
583 if (!opt_4 && !opt_6 && !opt_u)
584 opt_4 = opt_6 = opt_u = 1;
585 if (!opt_c && !opt_l)
588 if (opt_4 || opt_6) {
589 gather_inet(IPPROTO_TCP);
590 gather_inet(IPPROTO_UDP);
591 gather_inet(IPPROTO_DIVERT);
594 gather_unix(SOCK_STREAM);
595 gather_unix(SOCK_DGRAM);