2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sysctl.h>
42 #include <sys/unpcb.h>
44 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/in_pcb.h>
48 #include <netinet/sctp.h>
49 #include <netinet/tcp.h>
50 #define TCPSTATES /* load state names */
51 #include <netinet/tcp_fsm.h>
52 #include <netinet/tcp_seq.h>
53 #include <netinet/tcp_var.h>
54 #include <arpa/inet.h>
68 #define sstosin(ss) ((struct sockaddr_in *)(ss))
69 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
70 #define sstosun(ss) ((struct sockaddr_un *)(ss))
71 #define sstosa(ss) ((struct sockaddr *)(ss))
73 static int opt_4; /* Show IPv4 sockets */
74 static int opt_6; /* Show IPv6 sockets */
75 static int opt_c; /* Show connected sockets */
76 static int opt_j; /* Show specified jail */
77 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
78 static int opt_l; /* Show listening sockets */
79 static int opt_s; /* Show protocol state if applicable */
80 static int opt_u; /* Show Unix domain sockets */
81 static int opt_v; /* Verbose mode */
84 * Default protocols to use if no -P was defined.
86 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
87 static size_t default_numprotos = nitems(default_protos);
89 static int *protos; /* protocols to use */
90 static size_t numprotos; /* allocated size of protos[] */
94 #define INT_BIT (sizeof(int)*CHAR_BIT)
95 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
96 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
99 struct sockaddr_storage address;
111 const char *protoname;
117 #define HASHSIZE 1009
118 static struct sock *sockhash[HASHSIZE];
120 static struct xfile *xfiles;
124 xprintf(const char *fmt, ...)
130 len = vprintf(fmt, ap);
138 get_proto_type(const char *proto)
140 struct protoent *pent;
142 if (strlen(proto) == 0)
144 pent = getprotobyname(proto);
146 warn("getprotobyname");
149 return (pent->p_proto);
160 /* Find the maximum number of possible protocols. */
161 while (getprotoent() != NULL)
166 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
168 numprotos = proto_count;
172 parse_protos(char *protospec)
175 int proto_type, proto_index;
177 if (protospec == NULL)
182 while ((prot = strsep(&protospec, ",")) != NULL) {
183 if (strlen(prot) == 0)
185 proto_type = get_proto_type(prot);
186 if (proto_type != -1)
187 protos[proto_index++] = proto_type;
189 numprotos = proto_index;
190 return (proto_index);
194 parse_ports(const char *portspec)
200 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
205 errx(1, "syntax error in port range");
206 for (q = p; *q != '\0' && isdigit(*q); ++q)
208 for (port = 0; p < q; ++p)
209 port = port * 10 + digittoint(*p);
210 if (port < 0 || port > 65535)
211 errx(1, "invalid port number");
224 for (q = p; *q != '\0' && isdigit(*q); ++q)
226 for (end = 0; p < q; ++p)
227 end = end * 10 + digittoint(*p);
228 if (end < port || end > 65535)
229 errx(1, "invalid port number");
238 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
240 struct sockaddr_in *sin4;
241 struct sockaddr_in6 *sin6;
243 bzero(ss, sizeof(*ss));
247 sin4->sin_len = sizeof(*sin4);
248 sin4->sin_family = af;
249 sin4->sin_port = port;
250 sin4->sin_addr = *(struct in_addr *)addr;
254 sin6->sin6_len = sizeof(*sin6);
255 sin6->sin6_family = af;
256 sin6->sin6_port = port;
257 sin6->sin6_addr = *(struct in6_addr *)addr;
258 #define s6_addr16 __u6_addr.__u6_addr16
259 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
260 sin6->sin6_scope_id =
261 ntohs(sin6->sin6_addr.s6_addr16[1]);
262 sin6->sin6_addr.s6_addr16[1] = 0;
271 free_socket(struct sock *sock)
273 struct addr *cur, *next;
276 while (cur != NULL) {
282 while (cur != NULL) {
294 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
295 struct xsctp_inpcb *xinpcb;
296 struct xsctp_tcb *xstcb;
297 struct xsctp_raddr *xraddr;
298 struct xsctp_laddr *xladdr;
303 int no_stcb, local_all_loopback, foreign_all_loopback;
311 varname = "net.inet.sctp.assoclist";
312 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
314 err(1, "sysctlbyname()");
317 if ((buf = (char *)malloc(len)) == NULL) {
321 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
322 err(1, "sysctlbyname()");
326 xinpcb = (struct xsctp_inpcb *)(void *)buf;
327 offset = sizeof(struct xsctp_inpcb);
328 while ((offset < len) && (xinpcb->last == 0)) {
329 if ((sock = calloc(1, sizeof *sock)) == NULL)
331 sock->socket = xinpcb->socket;
332 sock->proto = IPPROTO_SCTP;
333 sock->protoname = "sctp";
334 if (xinpcb->maxqlen == 0)
335 sock->state = SCTP_CLOSED;
337 sock->state = SCTP_LISTEN;
338 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
339 sock->family = AF_INET6;
341 * Currently there is no way to distinguish between
342 * IPv6 only sockets or dual family sockets.
343 * So mark it as dual socket.
345 sock->vflag = INP_IPV6 | INP_IPV4;
347 sock->family = AF_INET;
348 sock->vflag = INP_IPV4;
351 local_all_loopback = 1;
352 while (offset < len) {
353 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
354 offset += sizeof(struct xsctp_laddr);
355 if (xladdr->last == 1)
357 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
359 switch (xladdr->address.sa.sa_family) {
361 #define __IN_IS_ADDR_LOOPBACK(pina) \
362 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
363 if (!__IN_IS_ADDR_LOOPBACK(
364 &xladdr->address.sin.sin_addr))
365 local_all_loopback = 0;
366 #undef __IN_IS_ADDR_LOOPBACK
367 sockaddr(&laddr->address, AF_INET,
368 &xladdr->address.sin.sin_addr,
369 htons(xinpcb->local_port));
372 if (!IN6_IS_ADDR_LOOPBACK(
373 &xladdr->address.sin6.sin6_addr))
374 local_all_loopback = 0;
375 sockaddr(&laddr->address, AF_INET6,
376 &xladdr->address.sin6.sin6_addr,
377 htons(xinpcb->local_port));
380 errx(1, "address family %d not supported",
381 xladdr->address.sa.sa_family);
384 if (prev_laddr == NULL)
387 prev_laddr->next = laddr;
390 if (sock->laddr == NULL) {
392 calloc(1, sizeof(struct addr))) == NULL)
394 sock->laddr->address.ss_family = sock->family;
395 if (sock->family == AF_INET)
396 sock->laddr->address.ss_len =
397 sizeof(struct sockaddr_in);
399 sock->laddr->address.ss_len =
400 sizeof(struct sockaddr_in6);
401 local_all_loopback = 0;
403 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
405 sock->faddr->address.ss_family = sock->family;
406 if (sock->family == AF_INET)
407 sock->faddr->address.ss_len =
408 sizeof(struct sockaddr_in);
410 sock->faddr->address.ss_len =
411 sizeof(struct sockaddr_in6);
413 while (offset < len) {
414 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
415 offset += sizeof(struct xsctp_tcb);
417 if (opt_l && (sock->vflag & vflag) &&
418 (!opt_L || !local_all_loopback) &&
419 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
420 (xstcb->last == 1))) {
421 hash = (int)((uintptr_t)sock->socket %
423 sock->next = sockhash[hash];
424 sockhash[hash] = sock;
429 if (xstcb->last == 1)
433 if ((sock = calloc(1, sizeof *sock)) == NULL)
435 sock->socket = xinpcb->socket;
436 sock->proto = IPPROTO_SCTP;
437 sock->protoname = "sctp";
438 sock->state = (int)xstcb->state;
439 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
440 sock->family = AF_INET6;
442 * Currently there is no way to distinguish
443 * between IPv6 only sockets or dual family
444 * sockets. So mark it as dual socket.
446 sock->vflag = INP_IPV6 | INP_IPV4;
448 sock->family = AF_INET;
449 sock->vflag = INP_IPV4;
453 local_all_loopback = 1;
454 while (offset < len) {
455 xladdr = (struct xsctp_laddr *)(void *)(buf +
457 offset += sizeof(struct xsctp_laddr);
458 if (xladdr->last == 1)
462 laddr = calloc(1, sizeof(struct addr));
465 switch (xladdr->address.sa.sa_family) {
467 #define __IN_IS_ADDR_LOOPBACK(pina) \
468 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
469 if (!__IN_IS_ADDR_LOOPBACK(
470 &xladdr->address.sin.sin_addr))
471 local_all_loopback = 0;
472 #undef __IN_IS_ADDR_LOOPBACK
473 sockaddr(&laddr->address, AF_INET,
474 &xladdr->address.sin.sin_addr,
475 htons(xstcb->local_port));
478 if (!IN6_IS_ADDR_LOOPBACK(
479 &xladdr->address.sin6.sin6_addr))
480 local_all_loopback = 0;
481 sockaddr(&laddr->address, AF_INET6,
482 &xladdr->address.sin6.sin6_addr,
483 htons(xstcb->local_port));
487 "address family %d not supported",
488 xladdr->address.sa.sa_family);
491 if (prev_laddr == NULL)
494 prev_laddr->next = laddr;
498 foreign_all_loopback = 1;
499 while (offset < len) {
500 xraddr = (struct xsctp_raddr *)(void *)(buf +
502 offset += sizeof(struct xsctp_raddr);
503 if (xraddr->last == 1)
507 faddr = calloc(1, sizeof(struct addr));
510 switch (xraddr->address.sa.sa_family) {
512 #define __IN_IS_ADDR_LOOPBACK(pina) \
513 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
514 if (!__IN_IS_ADDR_LOOPBACK(
515 &xraddr->address.sin.sin_addr))
516 foreign_all_loopback = 0;
517 #undef __IN_IS_ADDR_LOOPBACK
518 sockaddr(&faddr->address, AF_INET,
519 &xraddr->address.sin.sin_addr,
520 htons(xstcb->remote_port));
523 if (!IN6_IS_ADDR_LOOPBACK(
524 &xraddr->address.sin6.sin6_addr))
525 foreign_all_loopback = 0;
526 sockaddr(&faddr->address, AF_INET6,
527 &xraddr->address.sin6.sin6_addr,
528 htons(xstcb->remote_port));
532 "address family %d not supported",
533 xraddr->address.sa.sa_family);
536 if (prev_faddr == NULL)
539 prev_faddr->next = faddr;
543 if ((sock->vflag & vflag) &&
545 !(local_all_loopback ||
546 foreign_all_loopback))) {
547 hash = (int)((uintptr_t)sock->socket %
549 sock->next = sockhash[hash];
550 sockhash[hash] = sock;
556 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
557 offset += sizeof(struct xsctp_inpcb);
563 gather_inet(int proto)
565 struct xinpgen *xig, *exig;
571 struct addr *laddr, *faddr;
572 const char *varname, *protoname;
575 int hash, retry, vflag;
585 varname = "net.inet.tcp.pcblist";
589 varname = "net.inet.udp.pcblist";
593 varname = "net.inet.divert.pcblist";
597 errx(1, "protocol %d not supported", proto);
605 if ((buf = realloc(buf, bufsize)) == NULL)
608 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
612 if (errno != ENOMEM || len != bufsize)
613 err(1, "sysctlbyname()");
616 xig = (struct xinpgen *)buf;
617 exig = (struct xinpgen *)(void *)
618 ((char *)buf + len - sizeof *exig);
619 if (xig->xig_len != sizeof *xig ||
620 exig->xig_len != sizeof *exig)
621 errx(1, "struct xinpgen size mismatch");
622 } while (xig->xig_gen != exig->xig_gen && retry--);
624 if (xig->xig_gen != exig->xig_gen && opt_v)
625 warnx("warning: data may be inconsistent");
628 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
631 xip = (struct xinpcb *)xig;
632 xtp = (struct xtcpcb *)xig;
635 if (xtp->xt_len != sizeof(*xtp)) {
636 warnx("struct xtcpcb size mismatch");
640 so = &xtp->xt_socket;
641 protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
645 if (xip->xi_len != sizeof(*xip)) {
646 warnx("struct xinpcb size mismatch");
650 so = &xip->xi_socket;
653 errx(1, "protocol %d not supported", proto);
655 if ((inp->inp_vflag & vflag) == 0)
657 if (inp->inp_vflag & INP_IPV4) {
658 if ((inp->inp_fport == 0 && !opt_l) ||
659 (inp->inp_fport != 0 && !opt_c))
661 #define __IN_IS_ADDR_LOOPBACK(pina) \
662 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
664 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
665 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
667 #undef __IN_IS_ADDR_LOOPBACK
668 } else if (inp->inp_vflag & INP_IPV6) {
669 if ((inp->inp_fport == 0 && !opt_l) ||
670 (inp->inp_fport != 0 && !opt_c))
673 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
674 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
678 warnx("invalid vflag 0x%x", inp->inp_vflag);
681 if ((sock = calloc(1, sizeof(*sock))) == NULL)
683 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
685 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
687 sock->socket = so->xso_so;
689 if (inp->inp_vflag & INP_IPV4) {
690 sock->family = AF_INET;
691 sockaddr(&laddr->address, sock->family,
692 &inp->inp_laddr, inp->inp_lport);
693 sockaddr(&faddr->address, sock->family,
694 &inp->inp_faddr, inp->inp_fport);
695 } else if (inp->inp_vflag & INP_IPV6) {
696 sock->family = AF_INET6;
697 sockaddr(&laddr->address, sock->family,
698 &inp->in6p_laddr, inp->inp_lport);
699 sockaddr(&faddr->address, sock->family,
700 &inp->in6p_faddr, inp->inp_fport);
706 sock->vflag = inp->inp_vflag;
707 if (proto == IPPROTO_TCP)
708 sock->state = xtp->xt_tp.t_state;
709 sock->protoname = protoname;
710 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
711 sock->next = sockhash[hash];
712 sockhash[hash] = sock;
719 gather_unix(int proto)
721 struct xunpgen *xug, *exug;
724 struct addr *laddr, *faddr;
725 const char *varname, *protoname;
732 varname = "net.local.stream.pcblist";
733 protoname = "stream";
736 varname = "net.local.dgram.pcblist";
740 varname = "net.local.seqpacket.pcblist";
741 protoname = "seqpac";
751 if ((buf = realloc(buf, bufsize)) == NULL)
754 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
756 if (errno != ENOMEM || len != bufsize)
757 err(1, "sysctlbyname()");
760 xug = (struct xunpgen *)buf;
761 exug = (struct xunpgen *)(void *)
762 ((char *)buf + len - sizeof(*exug));
763 if (xug->xug_len != sizeof(*xug) ||
764 exug->xug_len != sizeof(*exug)) {
765 warnx("struct xinpgen size mismatch");
768 } while (xug->xug_gen != exug->xug_gen && retry--);
770 if (xug->xug_gen != exug->xug_gen && opt_v)
771 warnx("warning: data may be inconsistent");
774 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
777 xup = (struct xunpcb *)xug;
778 if (xup->xu_len != sizeof(*xup)) {
779 warnx("struct xunpcb size mismatch");
782 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
783 (xup->xu_unp.unp_conn != NULL && !opt_c))
785 if ((sock = calloc(1, sizeof(*sock))) == NULL)
787 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
789 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
791 sock->socket = xup->xu_socket.xso_so;
792 sock->pcb = xup->xu_unpp;
794 sock->family = AF_UNIX;
795 sock->protoname = protoname;
796 if (xup->xu_unp.unp_addr != NULL)
798 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
799 else if (xup->xu_unp.unp_conn != NULL)
800 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
805 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
806 sock->next = sockhash[hash];
807 sockhash[hash] = sock;
818 olen = len = sizeof(*xfiles);
819 if ((xfiles = malloc(len)) == NULL)
821 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
822 if (errno != ENOMEM || len != olen)
823 err(1, "sysctlbyname()");
825 if ((xfiles = realloc(xfiles, len)) == NULL)
828 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
829 errx(1, "struct xfile size mismatch");
830 nxfiles = len / sizeof(*xfiles);
834 printaddr(struct sockaddr_storage *ss)
836 struct sockaddr_un *sun;
837 char addrstr[NI_MAXHOST] = { '\0', '\0' };
838 int error, off, port = 0;
840 switch (ss->ss_family) {
842 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
844 port = ntohs(sstosin(ss)->sin_port);
847 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
849 port = ntohs(sstosin6(ss)->sin6_port);
853 off = (int)((char *)&sun->sun_path - (char *)sun);
854 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
856 if (addrstr[0] == '\0') {
857 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
858 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
860 errx(1, "getnameinfo()");
863 return xprintf("%s:*", addrstr);
865 return xprintf("%s:%d", addrstr, port);
869 getprocname(pid_t pid)
871 static struct kinfo_proc proc;
877 mib[2] = KERN_PROC_PID;
880 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
881 /* Do not warn if the process exits before we get its name. */
886 return (proc.ki_comm);
890 getprocjid(pid_t pid)
892 static struct kinfo_proc proc;
898 mib[2] = KERN_PROC_PID;
901 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
902 /* Do not warn if the process exits before we get its jid. */
907 return (proc.ki_jid);
911 check_ports(struct sock *s)
918 if ((s->family != AF_INET) && (s->family != AF_INET6))
920 for (addr = s->laddr; addr != NULL; addr = addr->next) {
921 if (s->family == AF_INET)
922 port = ntohs(sstosin(&addr->address)->sin_port);
924 port = ntohs(sstosin6(&addr->address)->sin6_port);
928 for (addr = s->faddr; addr != NULL; addr = addr->next) {
929 if (s->family == AF_INET)
930 port = ntohs(sstosin(&addr->address)->sin_port);
932 port = ntohs(sstosin6(&addr->address)->sin6_port);
940 sctp_state(int state)
952 case SCTP_COOKIE_WAIT:
953 return "COOKIE_WAIT";
955 case SCTP_COOKIE_ECHOED:
956 return "COOKIE_ECHOED";
958 case SCTP_ESTABLISHED:
959 return "ESTABLISHED";
961 case SCTP_SHUTDOWN_SENT:
962 return "SHUTDOWN_SENT";
964 case SCTP_SHUTDOWN_RECEIVED:
965 return "SHUTDOWN_RECEIVED";
967 case SCTP_SHUTDOWN_ACK_SENT:
968 return "SHUTDOWN_ACK_SENT";
970 case SCTP_SHUTDOWN_PENDING:
971 return "SHUTDOWN_PENDING";
980 displaysock(struct sock *s, int pos)
984 struct addr *laddr, *faddr;
989 pos += xprintf("%s", s->protoname);
990 if (s->vflag & INP_IPV4)
992 if (s->vflag & INP_IPV6)
994 if (s->vflag & (INP_IPV4 | INP_IPV6))
999 while (laddr != NULL || faddr != NULL) {
1001 pos += xprintf(" ");
1002 switch (s->family) {
1005 if (laddr != NULL) {
1006 pos += printaddr(&laddr->address);
1007 if (s->family == AF_INET6 && pos >= 58)
1008 pos += xprintf(" ");
1011 pos += xprintf(" ");
1013 pos += printaddr(&faddr->address);
1016 if ((laddr == NULL) || (faddr == NULL))
1017 errx(1, "laddr = %p or faddr = %p is NULL",
1018 (void *)laddr, (void *)faddr);
1020 if (laddr->address.ss_len > 0) {
1021 pos += printaddr(&laddr->address);
1025 p = *(void **)&(faddr->address);
1027 pos += xprintf("(not connected)");
1030 pos += xprintf("-> ");
1031 for (hash = 0; hash < HASHSIZE; ++hash) {
1032 for (s_tmp = sockhash[hash];
1034 s_tmp = s_tmp->next)
1035 if (s_tmp->pcb == p)
1040 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1041 s_tmp->laddr->address.ss_len == 0)
1042 pos += xprintf("??");
1044 pos += printaddr(&s_tmp->laddr->address);
1049 if (first && opt_s &&
1050 (s->proto == IPPROTO_SCTP || s->proto == IPPROTO_TCP)) {
1052 pos += xprintf(" ");
1055 pos += xprintf("%s", sctp_state(s->state));
1058 if (s->state >= 0 && s->state < TCP_NSTATES)
1060 xprintf("%s", tcpstates[s->state]);
1062 pos += xprintf("?");
1067 laddr = laddr->next;
1069 faddr = faddr->next;
1070 if ((laddr != NULL) || (faddr != NULL)) {
1087 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s",
1088 "USER", "COMMAND", "PID", "FD", "PROTO",
1089 "LOCAL ADDRESS", "FOREIGN ADDRESS");
1091 printf(" %-12s", "STATE");
1094 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1095 if (xf->xf_data == NULL)
1097 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1099 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1100 for (s = sockhash[hash]; s != NULL; s = s->next) {
1101 if ((void *)s->socket != xf->xf_data)
1103 if (!check_ports(s))
1107 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1108 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1110 pos += xprintf("%s ", pwd->pw_name);
1112 pos += xprintf(" ");
1113 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1115 pos += xprintf(" ");
1116 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1118 pos += xprintf(" ");
1119 pos += xprintf("%d ", xf->xf_fd);
1120 displaysock(s, pos);
1125 for (hash = 0; hash < HASHSIZE; hash++) {
1126 for (s = sockhash[hash]; s != NULL; s = s->next) {
1129 if (!check_ports(s))
1132 pos += xprintf("%-8s %-10s %-5s %-2s ",
1133 "?", "?", "?", "?");
1134 displaysock(s, pos);
1139 static int set_default_protos(void)
1141 struct protoent *prot;
1145 init_protos(default_numprotos);
1147 for (pindex = 0; pindex < default_numprotos; pindex++) {
1148 pname = default_protos[pindex];
1149 prot = getprotobyname(pname);
1151 err(1, "getprotobyname: %s", pname);
1152 protos[pindex] = prot->p_proto;
1162 "usage: sockstat [-46cLlsu] [-j jid] [-p ports] [-P protocols]\n");
1167 main(int argc, char *argv[])
1169 int protos_defined = -1;
1173 while ((o = getopt(argc, argv, "46cj:Llp:P:suv")) != -1)
1185 opt_j = jail_getid(optarg);
1187 errx(1, "%s", jail_errmsg);
1196 parse_ports(optarg);
1199 protos_defined = parse_protos(optarg);
1220 if ((!opt_4 && !opt_6) && protos_defined != -1)
1222 if (!opt_4 && !opt_6 && !opt_u)
1223 opt_4 = opt_6 = opt_u = 1;
1224 if ((opt_4 || opt_6) && protos_defined == -1)
1225 protos_defined = set_default_protos();
1226 if (!opt_c && !opt_l)
1229 if (opt_4 || opt_6) {
1230 for (i = 0; i < protos_defined; i++)
1231 if (protos[i] == IPPROTO_SCTP)
1234 gather_inet(protos[i]);
1237 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1238 gather_unix(SOCK_STREAM);
1239 gather_unix(SOCK_DGRAM);
1240 gather_unix(SOCK_SEQPACKET);