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/sctp.h>
47 #include <netinet/tcp.h>
48 #include <netinet/tcp_seq.h>
49 #include <netinet/tcp_var.h>
50 #include <arpa/inet.h>
63 static int opt_4; /* Show IPv4 sockets */
64 static int opt_6; /* Show IPv6 sockets */
65 static int opt_c; /* Show connected sockets */
66 static int opt_j; /* Show specified jail */
67 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
68 static int opt_l; /* Show listening sockets */
69 static int opt_u; /* Show Unix domain sockets */
70 static int opt_v; /* Verbose mode */
73 * Default protocols to use if no -P was defined.
75 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
76 static size_t default_numprotos =
77 sizeof(default_protos) / sizeof(default_protos[0]);
79 static int *protos; /* protocols to use */
80 static size_t numprotos; /* allocated size of protos[] */
84 #define INT_BIT (sizeof(int)*CHAR_BIT)
85 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
86 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
89 struct sockaddr_storage address;
100 const char *protoname;
106 #define HASHSIZE 1009
107 static struct sock *sockhash[HASHSIZE];
109 static struct xfile *xfiles;
113 xprintf(const char *fmt, ...)
119 len = vprintf(fmt, ap);
128 get_proto_type(const char *proto)
130 struct protoent *pent;
132 if (strlen(proto) == 0)
134 pent = getprotobyname(proto);
136 warn("getprotobyname");
139 return (pent->p_proto);
143 static void init_protos(int num)
150 /* Find the maximum number of possible protocols. */
151 while (getprotoent() != NULL)
156 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
158 numprotos = proto_count;
163 parse_protos(char *protospec)
166 char *tmp = protospec;
167 int proto_type, proto_index;
169 if (protospec == NULL)
174 while ((prot = strsep(&tmp, ",")) != NULL) {
175 if (strlen(prot) == 0)
177 proto_type = get_proto_type(prot);
178 if (proto_type != -1)
179 protos[proto_index++] = proto_type;
181 numprotos = proto_index;
182 return (proto_index);
187 parse_ports(const char *portspec)
193 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
198 errx(1, "syntax error in port range");
199 for (q = p; *q != '\0' && isdigit(*q); ++q)
201 for (port = 0; p < q; ++p)
202 port = port * 10 + digittoint(*p);
203 if (port < 0 || port > 65535)
204 errx(1, "invalid port number");
217 for (q = p; *q != '\0' && isdigit(*q); ++q)
219 for (end = 0; p < q; ++p)
220 end = end * 10 + digittoint(*p);
221 if (end < port || end > 65535)
222 errx(1, "invalid port number");
231 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port)
233 struct sockaddr_in *sin4;
234 struct sockaddr_in6 *sin6;
236 bzero(sa, sizeof *sa);
239 sin4 = (struct sockaddr_in *)sa;
240 sin4->sin_len = sizeof *sin4;
241 sin4->sin_family = af;
242 sin4->sin_port = port;
243 sin4->sin_addr = *(struct in_addr *)addr;
246 sin6 = (struct sockaddr_in6 *)sa;
247 sin6->sin6_len = sizeof *sin6;
248 sin6->sin6_family = af;
249 sin6->sin6_port = port;
250 sin6->sin6_addr = *(struct in6_addr *)addr;
261 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
262 struct xsctp_inpcb *xinpcb;
263 struct xsctp_tcb *xstcb;
264 struct xsctp_raddr *xraddr;
265 struct xsctp_laddr *xladdr;
270 int no_stcb, local_all_loopback, foreign_all_loopback;
278 varname = "net.inet.sctp.assoclist";
279 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
281 err(1, "sysctlbyname()");
284 if ((buf = (char *)malloc(len)) == NULL) {
288 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
289 err(1, "sysctlbyname()");
293 xinpcb = (struct xsctp_inpcb *)(void *)buf;
294 offset = sizeof(struct xsctp_inpcb);
295 while ((offset < len) && (xinpcb->last == 0)) {
296 if ((sock = calloc(1, sizeof *sock)) == NULL)
298 sock->socket = xinpcb->socket;
299 sock->proto = IPPROTO_SCTP;
300 sock->protoname = "sctp";
301 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
302 sock->family = AF_INET6;
303 sock->vflag = INP_IPV6;
305 sock->family = AF_INET;
306 sock->vflag = INP_IPV4;
309 local_all_loopback = 1;
310 while (offset < len) {
311 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
312 offset += sizeof(struct xsctp_laddr);
313 if (xladdr->last == 1)
315 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
317 switch (xladdr->address.sa.sa_family) {
319 #define __IN_IS_ADDR_LOOPBACK(pina) \
320 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
321 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
322 local_all_loopback = 0;
323 #undef __IN_IS_ADDR_LOOPBACK
324 sockaddr(&laddr->address,
326 &xladdr->address.sin.sin_addr,
327 htons(xinpcb->local_port));
330 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
331 local_all_loopback = 0;
332 sockaddr(&laddr->address,
334 &xladdr->address.sin6.sin6_addr,
335 htons(xinpcb->local_port));
338 errx(1, "adress family %d not supported",
339 xladdr->address.sa.sa_family);
342 if (prev_laddr == NULL)
345 prev_laddr->next = laddr;
348 if (sock->laddr == NULL) {
349 if ((sock->laddr = calloc(1, sizeof(struct addr))) == NULL)
351 sock->laddr->address.ss_family = sock->family;
352 if (sock->family == AF_INET)
353 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
355 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
356 local_all_loopback = 0;
358 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
360 sock->faddr->address.ss_family = sock->family;
361 if (sock->family == AF_INET)
362 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
364 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
366 while (offset < len) {
367 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
368 offset += sizeof(struct xsctp_tcb);
371 (!opt_L || !local_all_loopback) &&
372 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
373 (xstcb->last == 1))) {
374 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
375 sock->next = sockhash[hash];
376 sockhash[hash] = sock;
378 if (xstcb->last == 1)
382 if ((sock = calloc(1, sizeof *sock)) == NULL)
384 sock->socket = xinpcb->socket;
385 sock->proto = IPPROTO_SCTP;
386 sock->protoname = "sctp";
387 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
388 sock->family = AF_INET6;
389 sock->vflag = INP_IPV6;
391 sock->family = AF_INET;
392 sock->vflag = INP_IPV4;
396 local_all_loopback = 1;
397 while (offset < len) {
398 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
399 offset += sizeof(struct xsctp_laddr);
400 if (xladdr->last == 1)
404 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
406 switch (xladdr->address.sa.sa_family) {
408 #define __IN_IS_ADDR_LOOPBACK(pina) \
409 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
410 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
411 local_all_loopback = 0;
412 #undef __IN_IS_ADDR_LOOPBACK
413 sockaddr(&laddr->address,
415 &xladdr->address.sin.sin_addr,
416 htons(xstcb->local_port));
419 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
420 local_all_loopback = 0;
421 sockaddr(&laddr->address,
423 &xladdr->address.sin6.sin6_addr,
424 htons(xstcb->local_port));
427 errx(1, "adress family %d not supported",
428 xladdr->address.sa.sa_family);
431 if (prev_laddr == NULL)
434 prev_laddr->next = laddr;
438 foreign_all_loopback = 1;
439 while (offset < len) {
440 xraddr = (struct xsctp_raddr *)(void *)(buf + offset);
441 offset += sizeof(struct xsctp_raddr);
442 if (xraddr->last == 1)
446 if ((faddr = calloc(1, sizeof(struct addr))) == NULL)
448 switch (xraddr->address.sa.sa_family) {
450 #define __IN_IS_ADDR_LOOPBACK(pina) \
451 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
452 if (!__IN_IS_ADDR_LOOPBACK(&xraddr->address.sin.sin_addr))
453 foreign_all_loopback = 0;
454 #undef __IN_IS_ADDR_LOOPBACK
455 sockaddr(&faddr->address,
457 &xraddr->address.sin.sin_addr,
458 htons(xstcb->remote_port));
461 if (!IN6_IS_ADDR_LOOPBACK(&xraddr->address.sin6.sin6_addr))
462 foreign_all_loopback = 0;
463 sockaddr(&faddr->address,
465 &xraddr->address.sin6.sin6_addr,
466 htons(xstcb->remote_port));
469 errx(1, "adress family %d not supported",
470 xraddr->address.sa.sa_family);
473 if (prev_faddr == NULL)
476 prev_faddr->next = faddr;
480 (!opt_L || !(local_all_loopback || foreign_all_loopback))) {
481 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
482 sock->next = sockhash[hash];
483 sockhash[hash] = sock;
486 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
487 offset += sizeof(struct xsctp_inpcb);
493 gather_inet(int proto)
495 struct xinpgen *xig, *exig;
501 struct addr *laddr, *faddr;
502 const char *varname, *protoname;
505 int hash, retry, vflag;
515 varname = "net.inet.tcp.pcblist";
519 varname = "net.inet.udp.pcblist";
523 varname = "net.inet.divert.pcblist";
527 errx(1, "protocol %d not supported", proto);
535 if ((buf = realloc(buf, bufsize)) == NULL)
538 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
542 if (errno != ENOMEM || len != bufsize)
543 err(1, "sysctlbyname()");
546 xig = (struct xinpgen *)buf;
547 exig = (struct xinpgen *)(void *)
548 ((char *)buf + len - sizeof *exig);
549 if (xig->xig_len != sizeof *xig ||
550 exig->xig_len != sizeof *exig)
551 errx(1, "struct xinpgen size mismatch");
552 } while (xig->xig_gen != exig->xig_gen && retry--);
554 if (xig->xig_gen != exig->xig_gen && opt_v)
555 warnx("warning: data may be inconsistent");
558 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
563 xtp = (struct xtcpcb *)xig;
564 if (xtp->xt_len != sizeof *xtp) {
565 warnx("struct xtcpcb size mismatch");
569 so = &xtp->xt_socket;
570 protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
574 xip = (struct xinpcb *)xig;
575 if (xip->xi_len != sizeof *xip) {
576 warnx("struct xinpcb size mismatch");
580 so = &xip->xi_socket;
583 errx(1, "protocol %d not supported", proto);
585 if ((inp->inp_vflag & vflag) == 0)
587 if (inp->inp_vflag & INP_IPV4) {
588 if ((inp->inp_fport == 0 && !opt_l) ||
589 (inp->inp_fport != 0 && !opt_c))
591 #define __IN_IS_ADDR_LOOPBACK(pina) \
592 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
594 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
595 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
597 #undef __IN_IS_ADDR_LOOPBACK
598 } else if (inp->inp_vflag & INP_IPV6) {
599 if ((inp->inp_fport == 0 && !opt_l) ||
600 (inp->inp_fport != 0 && !opt_c))
603 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
604 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
608 warnx("invalid vflag 0x%x", inp->inp_vflag);
611 if ((sock = calloc(1, sizeof *sock)) == NULL)
613 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
615 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
617 sock->socket = so->xso_so;
619 if (inp->inp_vflag & INP_IPV4) {
620 sock->family = AF_INET;
621 sockaddr(&laddr->address, sock->family,
622 &inp->inp_laddr, inp->inp_lport);
623 sockaddr(&faddr->address, sock->family,
624 &inp->inp_faddr, inp->inp_fport);
625 } else if (inp->inp_vflag & INP_IPV6) {
626 sock->family = AF_INET6;
627 sockaddr(&laddr->address, sock->family,
628 &inp->in6p_laddr, inp->inp_lport);
629 sockaddr(&faddr->address, sock->family,
630 &inp->in6p_faddr, inp->inp_fport);
636 sock->vflag = inp->inp_vflag;
637 sock->protoname = protoname;
638 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
639 sock->next = sockhash[hash];
640 sockhash[hash] = sock;
647 gather_unix(int proto)
649 struct xunpgen *xug, *exug;
652 struct addr *laddr, *faddr;
653 const char *varname, *protoname;
660 varname = "net.local.stream.pcblist";
661 protoname = "stream";
664 varname = "net.local.dgram.pcblist";
675 if ((buf = realloc(buf, bufsize)) == NULL)
678 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
680 if (errno != ENOMEM || len != bufsize)
681 err(1, "sysctlbyname()");
684 xug = (struct xunpgen *)buf;
685 exug = (struct xunpgen *)(void *)
686 ((char *)buf + len - sizeof *exug);
687 if (xug->xug_len != sizeof *xug ||
688 exug->xug_len != sizeof *exug) {
689 warnx("struct xinpgen size mismatch");
692 } while (xug->xug_gen != exug->xug_gen && retry--);
694 if (xug->xug_gen != exug->xug_gen && opt_v)
695 warnx("warning: data may be inconsistent");
698 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
701 xup = (struct xunpcb *)xug;
702 if (xup->xu_len != sizeof *xup) {
703 warnx("struct xunpcb size mismatch");
706 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
707 (xup->xu_unp.unp_conn != NULL && !opt_c))
709 if ((sock = calloc(1, sizeof *sock)) == NULL)
711 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
713 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
715 sock->socket = xup->xu_socket.xso_so;
716 sock->pcb = xup->xu_unpp;
718 sock->family = AF_UNIX;
719 sock->protoname = protoname;
720 if (xup->xu_unp.unp_addr != NULL)
722 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
723 else if (xup->xu_unp.unp_conn != NULL)
724 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
729 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
730 sock->next = sockhash[hash];
731 sockhash[hash] = sock;
742 olen = len = sizeof *xfiles;
743 if ((xfiles = malloc(len)) == NULL)
745 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
746 if (errno != ENOMEM || len != olen)
747 err(1, "sysctlbyname()");
749 if ((xfiles = realloc(xfiles, len)) == NULL)
752 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
753 errx(1, "struct xfile size mismatch");
754 nxfiles = len / sizeof *xfiles;
758 printaddr(struct sockaddr_storage *ss)
760 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
761 struct sockaddr_un *sun;
762 void *addr = NULL; /* Keep compiler happy. */
765 switch (ss->ss_family) {
767 addr = &((struct sockaddr_in *)ss)->sin_addr;
768 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
770 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
773 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
774 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
776 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
779 sun = (struct sockaddr_un *)ss;
780 off = (int)((char *)&sun->sun_path - (char *)sun);
781 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
783 if (addrstr[0] == '\0')
784 inet_ntop(ss->ss_family, addr, addrstr, sizeof addrstr);
786 return xprintf("%s:*", addrstr);
788 return xprintf("%s:%d", addrstr, port);
792 getprocname(pid_t pid)
794 static struct kinfo_proc proc;
800 mib[2] = KERN_PROC_PID;
803 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
804 /* Do not warn if the process exits before we get its name. */
809 return (proc.ki_comm);
813 getprocjid(pid_t pid)
815 static struct kinfo_proc proc;
821 mib[2] = KERN_PROC_PID;
824 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
825 /* Do not warn if the process exits before we get its jid. */
830 return (proc.ki_jid);
834 check_ports(struct sock *s)
841 if ((s->family != AF_INET) && (s->family != AF_INET6))
843 for (addr = s->laddr; addr != NULL; addr = addr->next) {
844 if (addr->address.ss_family == AF_INET)
845 port = ntohs(((struct sockaddr_in *)(&addr->address))->sin_port);
847 port = ntohs(((struct sockaddr_in6 *)(&addr->address))->sin6_port);
851 for (addr = s->faddr; addr != NULL; addr = addr->next) {
852 if (addr->address.ss_family == AF_INET)
853 port = ntohs(((struct sockaddr_in *)&(addr->address))->sin_port);
855 port = ntohs(((struct sockaddr_in6 *)&(addr->address))->sin6_port);
863 displaysock(struct sock *s, int pos)
867 struct addr *laddr, *faddr;
872 pos += xprintf("%s", s->protoname);
873 if (s->vflag & INP_IPV4)
874 pos += xprintf("4 ");
875 if (s->vflag & INP_IPV6)
876 pos += xprintf("6 ");
879 while (laddr != NULL || faddr != NULL) {
886 pos += printaddr(&laddr->address);
887 if (s->family == AF_INET6 && pos >= 58)
893 pos += printaddr(&faddr->address);
896 if ((laddr == NULL) || (faddr == NULL))
897 errx(1, "laddr = %p or faddr = %p is NULL",
898 (void *)laddr, (void *)faddr);
900 if (laddr->address.ss_len > 0) {
901 pos += printaddr(&laddr->address);
905 p = *(void **)&(faddr->address);
907 pos += xprintf("(not connected)");
910 pos += xprintf("-> ");
911 for (hash = 0; hash < HASHSIZE; ++hash) {
912 for (s_tmp = sockhash[hash];
921 s_tmp->laddr == NULL ||
922 s_tmp->laddr->address.ss_len == 0)
923 pos += xprintf("??");
925 pos += printaddr(&s_tmp->laddr->address);
934 if ((laddr != NULL) || (faddr != NULL)) {
950 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
951 "USER", "COMMAND", "PID", "FD", "PROTO",
952 "LOCAL ADDRESS", "FOREIGN ADDRESS");
954 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
955 if (xf->xf_data == NULL)
957 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
959 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
960 for (s = sockhash[hash]; s != NULL; s = s->next) {
961 if ((void *)s->socket != xf->xf_data)
967 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
968 pos += xprintf("%lu ", (u_long)xf->xf_uid);
970 pos += xprintf("%s ", pwd->pw_name);
973 pos += xprintf("%.10s", getprocname(xf->xf_pid));
976 pos += xprintf("%lu ", (u_long)xf->xf_pid);
979 pos += xprintf("%d ", xf->xf_fd);
985 for (hash = 0; hash < HASHSIZE; hash++) {
986 for (s = sockhash[hash]; s != NULL; s = s->next) {
992 pos += xprintf("%-8s %-10s %-5s %-2s ",
999 static int set_default_protos(void)
1001 struct protoent *prot;
1005 init_protos(default_numprotos);
1007 for (pindex = 0; pindex < default_numprotos; pindex++) {
1008 pname = default_protos[pindex];
1009 prot = getprotobyname(pname);
1011 err(1, "getprotobyname: %s", pname);
1012 protos[pindex] = prot->p_proto;
1023 "Usage: sockstat [-46cLlu] [-j jid] [-p ports] [-P protocols]\n");
1028 main(int argc, char *argv[])
1030 int protos_defined = -1;
1034 while ((o = getopt(argc, argv, "46cj:Llp:P:uv")) != -1)
1046 opt_j = atoi(optarg);
1055 parse_ports(optarg);
1058 protos_defined = parse_protos(optarg);
1076 if ((!opt_4 && !opt_6) && protos_defined != -1)
1078 if (!opt_4 && !opt_6 && !opt_u)
1079 opt_4 = opt_6 = opt_u = 1;
1080 if ((opt_4 || opt_6) && protos_defined == -1)
1081 protos_defined = set_default_protos();
1082 if (!opt_c && !opt_l)
1085 if (opt_4 || opt_6) {
1086 for (i = 0; i < protos_defined; i++)
1087 if (protos[i] == IPPROTO_SCTP)
1090 gather_inet(protos[i]);
1093 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1094 gather_unix(SOCK_STREAM);
1095 gather_unix(SOCK_DGRAM);