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 #define TCPSTATES /* load state names */
49 #include <netinet/tcp_fsm.h>
50 #include <netinet/tcp_seq.h>
51 #include <netinet/tcp_var.h>
52 #include <arpa/inet.h>
65 #define sstosin(ss) ((struct sockaddr_in *)(ss))
66 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
67 #define sstosun(ss) ((struct sockaddr_un *)(ss))
68 #define sstosa(ss) ((struct sockaddr *)(ss))
70 static int opt_4; /* Show IPv4 sockets */
71 static int opt_6; /* Show IPv6 sockets */
72 static int opt_c; /* Show connected sockets */
73 static int opt_j; /* Show specified jail */
74 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
75 static int opt_l; /* Show listening sockets */
76 static int opt_s; /* Show protocol state if applicable */
77 static int opt_u; /* Show Unix domain sockets */
78 static int opt_v; /* Verbose mode */
81 * Default protocols to use if no -P was defined.
83 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
84 static size_t default_numprotos = nitems(default_protos);
86 static int *protos; /* protocols to use */
87 static size_t numprotos; /* allocated size of protos[] */
91 #define INT_BIT (sizeof(int)*CHAR_BIT)
92 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
93 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
96 struct sockaddr_storage address;
108 const char *protoname;
114 #define HASHSIZE 1009
115 static struct sock *sockhash[HASHSIZE];
117 static struct xfile *xfiles;
121 xprintf(const char *fmt, ...)
127 len = vprintf(fmt, ap);
136 get_proto_type(const char *proto)
138 struct protoent *pent;
140 if (strlen(proto) == 0)
142 pent = getprotobyname(proto);
144 warn("getprotobyname");
147 return (pent->p_proto);
159 /* Find the maximum number of possible protocols. */
160 while (getprotoent() != NULL)
165 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
167 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);
195 parse_ports(const char *portspec)
201 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
206 errx(1, "syntax error in port range");
207 for (q = p; *q != '\0' && isdigit(*q); ++q)
209 for (port = 0; p < q; ++p)
210 port = port * 10 + digittoint(*p);
211 if (port < 0 || port > 65535)
212 errx(1, "invalid port number");
225 for (q = p; *q != '\0' && isdigit(*q); ++q)
227 for (end = 0; p < q; ++p)
228 end = end * 10 + digittoint(*p);
229 if (end < port || end > 65535)
230 errx(1, "invalid port number");
239 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
241 struct sockaddr_in *sin4;
242 struct sockaddr_in6 *sin6;
244 bzero(ss, sizeof(*ss));
248 sin4->sin_len = sizeof(*sin4);
249 sin4->sin_family = af;
250 sin4->sin_port = port;
251 sin4->sin_addr = *(struct in_addr *)addr;
255 sin6->sin6_len = sizeof(*sin6);
256 sin6->sin6_family = af;
257 sin6->sin6_port = port;
258 sin6->sin6_addr = *(struct in6_addr *)addr;
259 #define s6_addr16 __u6_addr.__u6_addr16
260 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
261 sin6->sin6_scope_id =
262 ntohs(sin6->sin6_addr.s6_addr16[1]);
263 sin6->sin6_addr.s6_addr16[1] = 0;
272 free_socket(struct sock *sock)
274 struct addr *cur, *next;
277 while (cur != NULL) {
283 while (cur != NULL) {
295 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
296 struct xsctp_inpcb *xinpcb;
297 struct xsctp_tcb *xstcb;
298 struct xsctp_raddr *xraddr;
299 struct xsctp_laddr *xladdr;
304 int no_stcb, local_all_loopback, foreign_all_loopback;
312 varname = "net.inet.sctp.assoclist";
313 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
315 err(1, "sysctlbyname()");
318 if ((buf = (char *)malloc(len)) == NULL) {
322 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
323 err(1, "sysctlbyname()");
327 xinpcb = (struct xsctp_inpcb *)(void *)buf;
328 offset = sizeof(struct xsctp_inpcb);
329 while ((offset < len) && (xinpcb->last == 0)) {
330 if ((sock = calloc(1, sizeof *sock)) == NULL)
332 sock->socket = xinpcb->socket;
333 sock->proto = IPPROTO_SCTP;
334 sock->protoname = "sctp";
335 if (xinpcb->maxqlen == 0)
336 sock->state = SCTP_CLOSED;
338 sock->state = SCTP_LISTEN;
339 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
340 sock->family = AF_INET6;
341 sock->vflag = INP_IPV6;
343 sock->family = AF_INET;
344 sock->vflag = INP_IPV4;
347 local_all_loopback = 1;
348 while (offset < len) {
349 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
350 offset += sizeof(struct xsctp_laddr);
351 if (xladdr->last == 1)
353 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
355 switch (xladdr->address.sa.sa_family) {
357 #define __IN_IS_ADDR_LOOPBACK(pina) \
358 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
359 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
360 local_all_loopback = 0;
361 #undef __IN_IS_ADDR_LOOPBACK
362 sockaddr(&laddr->address,
364 &xladdr->address.sin.sin_addr,
365 htons(xinpcb->local_port));
368 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
369 local_all_loopback = 0;
370 sockaddr(&laddr->address,
372 &xladdr->address.sin6.sin6_addr,
373 htons(xinpcb->local_port));
376 errx(1, "address family %d not supported",
377 xladdr->address.sa.sa_family);
380 if (prev_laddr == NULL)
383 prev_laddr->next = laddr;
386 if (sock->laddr == NULL) {
387 if ((sock->laddr = calloc(1, sizeof(struct addr))) == NULL)
389 sock->laddr->address.ss_family = sock->family;
390 if (sock->family == AF_INET)
391 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
393 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
394 local_all_loopback = 0;
396 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
398 sock->faddr->address.ss_family = sock->family;
399 if (sock->family == AF_INET)
400 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
402 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
404 while (offset < len) {
405 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
406 offset += sizeof(struct xsctp_tcb);
409 (!opt_L || !local_all_loopback) &&
410 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
411 (xstcb->last == 1))) {
412 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
413 sock->next = sockhash[hash];
414 sockhash[hash] = sock;
419 if (xstcb->last == 1)
423 if ((sock = calloc(1, sizeof *sock)) == NULL)
425 sock->socket = xinpcb->socket;
426 sock->proto = IPPROTO_SCTP;
427 sock->protoname = "sctp";
428 sock->state = (int)xstcb->state;
429 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
430 sock->family = AF_INET6;
431 sock->vflag = INP_IPV6;
433 sock->family = AF_INET;
434 sock->vflag = INP_IPV4;
438 local_all_loopback = 1;
439 while (offset < len) {
440 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
441 offset += sizeof(struct xsctp_laddr);
442 if (xladdr->last == 1)
446 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
448 switch (xladdr->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(&xladdr->address.sin.sin_addr))
453 local_all_loopback = 0;
454 #undef __IN_IS_ADDR_LOOPBACK
455 sockaddr(&laddr->address,
457 &xladdr->address.sin.sin_addr,
458 htons(xstcb->local_port));
461 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
462 local_all_loopback = 0;
463 sockaddr(&laddr->address,
465 &xladdr->address.sin6.sin6_addr,
466 htons(xstcb->local_port));
469 errx(1, "address family %d not supported",
470 xladdr->address.sa.sa_family);
473 if (prev_laddr == NULL)
476 prev_laddr->next = laddr;
480 foreign_all_loopback = 1;
481 while (offset < len) {
482 xraddr = (struct xsctp_raddr *)(void *)(buf + offset);
483 offset += sizeof(struct xsctp_raddr);
484 if (xraddr->last == 1)
488 if ((faddr = calloc(1, sizeof(struct addr))) == NULL)
490 switch (xraddr->address.sa.sa_family) {
492 #define __IN_IS_ADDR_LOOPBACK(pina) \
493 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
494 if (!__IN_IS_ADDR_LOOPBACK(&xraddr->address.sin.sin_addr))
495 foreign_all_loopback = 0;
496 #undef __IN_IS_ADDR_LOOPBACK
497 sockaddr(&faddr->address,
499 &xraddr->address.sin.sin_addr,
500 htons(xstcb->remote_port));
503 if (!IN6_IS_ADDR_LOOPBACK(&xraddr->address.sin6.sin6_addr))
504 foreign_all_loopback = 0;
505 sockaddr(&faddr->address,
507 &xraddr->address.sin6.sin6_addr,
508 htons(xstcb->remote_port));
511 errx(1, "address family %d not supported",
512 xraddr->address.sa.sa_family);
515 if (prev_faddr == NULL)
518 prev_faddr->next = faddr;
522 if (!opt_L || !(local_all_loopback || foreign_all_loopback)) {
523 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
524 sock->next = sockhash[hash];
525 sockhash[hash] = sock;
531 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
532 offset += sizeof(struct xsctp_inpcb);
538 gather_inet(int proto)
540 struct xinpgen *xig, *exig;
546 struct addr *laddr, *faddr;
547 const char *varname, *protoname;
550 int hash, retry, vflag;
560 varname = "net.inet.tcp.pcblist";
564 varname = "net.inet.udp.pcblist";
568 varname = "net.inet.divert.pcblist";
572 errx(1, "protocol %d not supported", proto);
580 if ((buf = realloc(buf, bufsize)) == NULL)
583 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
587 if (errno != ENOMEM || len != bufsize)
588 err(1, "sysctlbyname()");
591 xig = (struct xinpgen *)buf;
592 exig = (struct xinpgen *)(void *)
593 ((char *)buf + len - sizeof *exig);
594 if (xig->xig_len != sizeof *xig ||
595 exig->xig_len != sizeof *exig)
596 errx(1, "struct xinpgen size mismatch");
597 } while (xig->xig_gen != exig->xig_gen && retry--);
599 if (xig->xig_gen != exig->xig_gen && opt_v)
600 warnx("warning: data may be inconsistent");
603 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
606 xip = (struct xinpcb *)xig;
607 xtp = (struct xtcpcb *)xig;
610 if (xtp->xt_len != sizeof(*xtp)) {
611 warnx("struct xtcpcb size mismatch");
615 so = &xtp->xt_socket;
616 protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
620 if (xip->xi_len != sizeof(*xip)) {
621 warnx("struct xinpcb size mismatch");
625 so = &xip->xi_socket;
628 errx(1, "protocol %d not supported", proto);
630 if ((inp->inp_vflag & vflag) == 0)
632 if (inp->inp_vflag & INP_IPV4) {
633 if ((inp->inp_fport == 0 && !opt_l) ||
634 (inp->inp_fport != 0 && !opt_c))
636 #define __IN_IS_ADDR_LOOPBACK(pina) \
637 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
639 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
640 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
642 #undef __IN_IS_ADDR_LOOPBACK
643 } else if (inp->inp_vflag & INP_IPV6) {
644 if ((inp->inp_fport == 0 && !opt_l) ||
645 (inp->inp_fport != 0 && !opt_c))
648 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
649 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
653 warnx("invalid vflag 0x%x", inp->inp_vflag);
656 if ((sock = calloc(1, sizeof(*sock))) == NULL)
658 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
660 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
662 sock->socket = so->xso_so;
664 if (inp->inp_vflag & INP_IPV4) {
665 sock->family = AF_INET;
666 sockaddr(&laddr->address, sock->family,
667 &inp->inp_laddr, inp->inp_lport);
668 sockaddr(&faddr->address, sock->family,
669 &inp->inp_faddr, inp->inp_fport);
670 } else if (inp->inp_vflag & INP_IPV6) {
671 sock->family = AF_INET6;
672 sockaddr(&laddr->address, sock->family,
673 &inp->in6p_laddr, inp->inp_lport);
674 sockaddr(&faddr->address, sock->family,
675 &inp->in6p_faddr, inp->inp_fport);
681 sock->vflag = inp->inp_vflag;
682 if (proto == IPPROTO_TCP)
683 sock->state = xtp->xt_tp.t_state;
684 sock->protoname = protoname;
685 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
686 sock->next = sockhash[hash];
687 sockhash[hash] = sock;
694 gather_unix(int proto)
696 struct xunpgen *xug, *exug;
699 struct addr *laddr, *faddr;
700 const char *varname, *protoname;
707 varname = "net.local.stream.pcblist";
708 protoname = "stream";
711 varname = "net.local.dgram.pcblist";
715 varname = "net.local.seqpacket.pcblist";
716 protoname = "seqpac";
726 if ((buf = realloc(buf, bufsize)) == NULL)
729 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
731 if (errno != ENOMEM || len != bufsize)
732 err(1, "sysctlbyname()");
735 xug = (struct xunpgen *)buf;
736 exug = (struct xunpgen *)(void *)
737 ((char *)buf + len - sizeof(*exug));
738 if (xug->xug_len != sizeof(*xug) ||
739 exug->xug_len != sizeof(*exug)) {
740 warnx("struct xinpgen size mismatch");
743 } while (xug->xug_gen != exug->xug_gen && retry--);
745 if (xug->xug_gen != exug->xug_gen && opt_v)
746 warnx("warning: data may be inconsistent");
749 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
752 xup = (struct xunpcb *)xug;
753 if (xup->xu_len != sizeof(*xup)) {
754 warnx("struct xunpcb size mismatch");
757 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
758 (xup->xu_unp.unp_conn != NULL && !opt_c))
760 if ((sock = calloc(1, sizeof(*sock))) == NULL)
762 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
764 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
766 sock->socket = xup->xu_socket.xso_so;
767 sock->pcb = xup->xu_unpp;
769 sock->family = AF_UNIX;
770 sock->protoname = protoname;
771 if (xup->xu_unp.unp_addr != NULL)
773 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
774 else if (xup->xu_unp.unp_conn != NULL)
775 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
780 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
781 sock->next = sockhash[hash];
782 sockhash[hash] = sock;
793 olen = len = sizeof(*xfiles);
794 if ((xfiles = malloc(len)) == NULL)
796 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
797 if (errno != ENOMEM || len != olen)
798 err(1, "sysctlbyname()");
800 if ((xfiles = realloc(xfiles, len)) == NULL)
803 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
804 errx(1, "struct xfile size mismatch");
805 nxfiles = len / sizeof(*xfiles);
809 printaddr(struct sockaddr_storage *ss)
811 struct sockaddr_un *sun;
812 char addrstr[NI_MAXHOST] = { '\0', '\0' };
813 int error, off, port = 0;
815 switch (ss->ss_family) {
817 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
819 port = ntohs(sstosin(ss)->sin_port);
822 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
824 port = ntohs(sstosin6(ss)->sin6_port);
828 off = (int)((char *)&sun->sun_path - (char *)sun);
829 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
831 if (addrstr[0] == '\0') {
832 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
833 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
835 errx(1, "getnameinfo()");
838 return xprintf("%s:*", addrstr);
840 return xprintf("%s:%d", addrstr, port);
844 getprocname(pid_t pid)
846 static struct kinfo_proc proc;
852 mib[2] = KERN_PROC_PID;
855 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
856 /* Do not warn if the process exits before we get its name. */
861 return (proc.ki_comm);
865 getprocjid(pid_t pid)
867 static struct kinfo_proc proc;
873 mib[2] = KERN_PROC_PID;
876 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
877 /* Do not warn if the process exits before we get its jid. */
882 return (proc.ki_jid);
886 check_ports(struct sock *s)
893 if ((s->family != AF_INET) && (s->family != AF_INET6))
895 for (addr = s->laddr; addr != NULL; addr = addr->next) {
896 if (s->family == AF_INET)
897 port = ntohs(sstosin(&addr->address)->sin_port);
899 port = ntohs(sstosin6(&addr->address)->sin6_port);
903 for (addr = s->faddr; addr != NULL; addr = addr->next) {
904 if (s->family == AF_INET)
905 port = ntohs(sstosin(&addr->address)->sin_port);
907 port = ntohs(sstosin6(&addr->address)->sin6_port);
915 sctp_state(int state)
927 case SCTP_COOKIE_WAIT:
928 return "COOKIE_WAIT";
930 case SCTP_COOKIE_ECHOED:
931 return "COOKIE_ECHOED";
933 case SCTP_ESTABLISHED:
934 return "ESTABLISHED";
936 case SCTP_SHUTDOWN_SENT:
937 return "SHUTDOWN_SENT";
939 case SCTP_SHUTDOWN_RECEIVED:
940 return "SHUTDOWN_RECEIVED";
942 case SCTP_SHUTDOWN_ACK_SENT:
943 return "SHUTDOWN_ACK_SENT";
945 case SCTP_SHUTDOWN_PENDING:
946 return "SHUTDOWN_PENDING";
955 displaysock(struct sock *s, int pos)
959 struct addr *laddr, *faddr;
964 pos += xprintf("%s", s->protoname);
965 if (s->vflag & INP_IPV4)
966 pos += xprintf("4 ");
967 if (s->vflag & INP_IPV6)
968 pos += xprintf("6 ");
972 while (laddr != NULL || faddr != NULL) {
979 pos += printaddr(&laddr->address);
980 if (s->family == AF_INET6 && pos >= 58)
986 pos += printaddr(&faddr->address);
989 if ((laddr == NULL) || (faddr == NULL))
990 errx(1, "laddr = %p or faddr = %p is NULL",
991 (void *)laddr, (void *)faddr);
993 if (laddr->address.ss_len > 0) {
994 pos += printaddr(&laddr->address);
998 p = *(void **)&(faddr->address);
1000 pos += xprintf("(not connected)");
1003 pos += xprintf("-> ");
1004 for (hash = 0; hash < HASHSIZE; ++hash) {
1005 for (s_tmp = sockhash[hash];
1007 s_tmp = s_tmp->next)
1008 if (s_tmp->pcb == p)
1013 if (s_tmp == NULL ||
1014 s_tmp->laddr == NULL ||
1015 s_tmp->laddr->address.ss_len == 0)
1016 pos += xprintf("??");
1018 pos += printaddr(&s_tmp->laddr->address);
1023 if (first && opt_s &&
1024 (s->proto == IPPROTO_SCTP || s->proto == IPPROTO_TCP)) {
1026 pos += xprintf(" ");
1029 pos += xprintf("%s", sctp_state(s->state));
1032 if (s->state >= 0 && s->state < TCP_NSTATES)
1034 xprintf("%s", tcpstates[s->state]);
1036 pos += xprintf("?");
1041 laddr = laddr->next;
1043 faddr = faddr->next;
1044 if ((laddr != NULL) || (faddr != NULL)) {
1061 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s",
1062 "USER", "COMMAND", "PID", "FD", "PROTO",
1063 "LOCAL ADDRESS", "FOREIGN ADDRESS");
1065 printf(" %-12s", "STATE");
1068 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1069 if (xf->xf_data == NULL)
1071 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1073 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1074 for (s = sockhash[hash]; s != NULL; s = s->next) {
1075 if ((void *)s->socket != xf->xf_data)
1077 if (!check_ports(s))
1081 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1082 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1084 pos += xprintf("%s ", pwd->pw_name);
1086 pos += xprintf(" ");
1087 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1089 pos += xprintf(" ");
1090 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1092 pos += xprintf(" ");
1093 pos += xprintf("%d ", xf->xf_fd);
1094 displaysock(s, pos);
1099 for (hash = 0; hash < HASHSIZE; hash++) {
1100 for (s = sockhash[hash]; s != NULL; s = s->next) {
1103 if (!check_ports(s))
1106 pos += xprintf("%-8s %-10s %-5s %-2s ",
1107 "?", "?", "?", "?");
1108 displaysock(s, pos);
1113 static int set_default_protos(void)
1115 struct protoent *prot;
1119 init_protos(default_numprotos);
1121 for (pindex = 0; pindex < default_numprotos; pindex++) {
1122 pname = default_protos[pindex];
1123 prot = getprotobyname(pname);
1125 err(1, "getprotobyname: %s", pname);
1126 protos[pindex] = prot->p_proto;
1137 "usage: sockstat [-46cLlsu] [-j jid] [-p ports] [-P protocols]\n");
1142 main(int argc, char *argv[])
1144 int protos_defined = -1;
1148 while ((o = getopt(argc, argv, "46cj:Llp:P:suv")) != -1)
1160 opt_j = atoi(optarg);
1169 parse_ports(optarg);
1172 protos_defined = parse_protos(optarg);
1193 if ((!opt_4 && !opt_6) && protos_defined != -1)
1195 if (!opt_4 && !opt_6 && !opt_u)
1196 opt_4 = opt_6 = opt_u = 1;
1197 if ((opt_4 || opt_6) && protos_defined == -1)
1198 protos_defined = set_default_protos();
1199 if (!opt_c && !opt_l)
1202 if (opt_4 || opt_6) {
1203 for (i = 0; i < protos_defined; i++)
1204 if (protos[i] == IPPROTO_SCTP)
1207 gather_inet(protos[i]);
1210 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1211 gather_unix(SOCK_STREAM);
1212 gather_unix(SOCK_DGRAM);
1213 gather_unix(SOCK_SEQPACKET);