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>
66 #define sstosin(ss) ((struct sockaddr_in *)(ss))
67 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
68 #define sstosun(ss) ((struct sockaddr_un *)(ss))
69 #define sstosa(ss) ((struct sockaddr *)(ss))
71 static int opt_4; /* Show IPv4 sockets */
72 static int opt_6; /* Show IPv6 sockets */
73 static int opt_c; /* Show connected sockets */
74 static int opt_j; /* Show specified jail */
75 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
76 static int opt_l; /* Show listening sockets */
77 static int opt_s; /* Show protocol state if applicable */
78 static int opt_u; /* Show Unix domain sockets */
79 static int opt_v; /* Verbose mode */
82 * Default protocols to use if no -P was defined.
84 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
85 static size_t default_numprotos = nitems(default_protos);
87 static int *protos; /* protocols to use */
88 static size_t numprotos; /* allocated size of protos[] */
92 #define INT_BIT (sizeof(int)*CHAR_BIT)
93 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
94 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
97 struct sockaddr_storage address;
109 const char *protoname;
115 #define HASHSIZE 1009
116 static struct sock *sockhash[HASHSIZE];
118 static struct xfile *xfiles;
122 xprintf(const char *fmt, ...)
128 len = vprintf(fmt, ap);
137 get_proto_type(const char *proto)
139 struct protoent *pent;
141 if (strlen(proto) == 0)
143 pent = getprotobyname(proto);
145 warn("getprotobyname");
148 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;
173 parse_protos(char *protospec)
176 int proto_type, proto_index;
178 if (protospec == NULL)
183 while ((prot = strsep(&protospec, ",")) != NULL) {
184 if (strlen(prot) == 0)
186 proto_type = get_proto_type(prot);
187 if (proto_type != -1)
188 protos[proto_index++] = proto_type;
190 numprotos = proto_index;
191 return (proto_index);
196 parse_ports(const char *portspec)
202 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
207 errx(1, "syntax error in port range");
208 for (q = p; *q != '\0' && isdigit(*q); ++q)
210 for (port = 0; p < q; ++p)
211 port = port * 10 + digittoint(*p);
212 if (port < 0 || port > 65535)
213 errx(1, "invalid port number");
226 for (q = p; *q != '\0' && isdigit(*q); ++q)
228 for (end = 0; p < q; ++p)
229 end = end * 10 + digittoint(*p);
230 if (end < port || end > 65535)
231 errx(1, "invalid port number");
240 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
242 struct sockaddr_in *sin4;
243 struct sockaddr_in6 *sin6;
245 bzero(ss, sizeof(*ss));
249 sin4->sin_len = sizeof(*sin4);
250 sin4->sin_family = af;
251 sin4->sin_port = port;
252 sin4->sin_addr = *(struct in_addr *)addr;
256 sin6->sin6_len = sizeof(*sin6);
257 sin6->sin6_family = af;
258 sin6->sin6_port = port;
259 sin6->sin6_addr = *(struct in6_addr *)addr;
260 #define s6_addr16 __u6_addr.__u6_addr16
261 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
262 sin6->sin6_scope_id =
263 ntohs(sin6->sin6_addr.s6_addr16[1]);
264 sin6->sin6_addr.s6_addr16[1] = 0;
273 free_socket(struct sock *sock)
275 struct addr *cur, *next;
278 while (cur != NULL) {
284 while (cur != NULL) {
296 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
297 struct xsctp_inpcb *xinpcb;
298 struct xsctp_tcb *xstcb;
299 struct xsctp_raddr *xraddr;
300 struct xsctp_laddr *xladdr;
305 int no_stcb, local_all_loopback, foreign_all_loopback;
313 varname = "net.inet.sctp.assoclist";
314 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
316 err(1, "sysctlbyname()");
319 if ((buf = (char *)malloc(len)) == NULL) {
323 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
324 err(1, "sysctlbyname()");
328 xinpcb = (struct xsctp_inpcb *)(void *)buf;
329 offset = sizeof(struct xsctp_inpcb);
330 while ((offset < len) && (xinpcb->last == 0)) {
331 if ((sock = calloc(1, sizeof *sock)) == NULL)
333 sock->socket = xinpcb->socket;
334 sock->proto = IPPROTO_SCTP;
335 sock->protoname = "sctp";
336 if (xinpcb->maxqlen == 0)
337 sock->state = SCTP_CLOSED;
339 sock->state = SCTP_LISTEN;
340 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
341 sock->family = AF_INET6;
342 sock->vflag = INP_IPV6;
344 sock->family = AF_INET;
345 sock->vflag = INP_IPV4;
348 local_all_loopback = 1;
349 while (offset < len) {
350 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
351 offset += sizeof(struct xsctp_laddr);
352 if (xladdr->last == 1)
354 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
356 switch (xladdr->address.sa.sa_family) {
358 #define __IN_IS_ADDR_LOOPBACK(pina) \
359 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
360 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
361 local_all_loopback = 0;
362 #undef __IN_IS_ADDR_LOOPBACK
363 sockaddr(&laddr->address,
365 &xladdr->address.sin.sin_addr,
366 htons(xinpcb->local_port));
369 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
370 local_all_loopback = 0;
371 sockaddr(&laddr->address,
373 &xladdr->address.sin6.sin6_addr,
374 htons(xinpcb->local_port));
377 errx(1, "address family %d not supported",
378 xladdr->address.sa.sa_family);
381 if (prev_laddr == NULL)
384 prev_laddr->next = laddr;
387 if (sock->laddr == NULL) {
388 if ((sock->laddr = calloc(1, sizeof(struct addr))) == NULL)
390 sock->laddr->address.ss_family = sock->family;
391 if (sock->family == AF_INET)
392 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
394 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
395 local_all_loopback = 0;
397 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
399 sock->faddr->address.ss_family = sock->family;
400 if (sock->family == AF_INET)
401 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
403 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
405 while (offset < len) {
406 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
407 offset += sizeof(struct xsctp_tcb);
410 (!opt_L || !local_all_loopback) &&
411 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
412 (xstcb->last == 1))) {
413 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
414 sock->next = sockhash[hash];
415 sockhash[hash] = sock;
420 if (xstcb->last == 1)
424 if ((sock = calloc(1, sizeof *sock)) == NULL)
426 sock->socket = xinpcb->socket;
427 sock->proto = IPPROTO_SCTP;
428 sock->protoname = "sctp";
429 sock->state = (int)xstcb->state;
430 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
431 sock->family = AF_INET6;
432 sock->vflag = INP_IPV6;
434 sock->family = AF_INET;
435 sock->vflag = INP_IPV4;
439 local_all_loopback = 1;
440 while (offset < len) {
441 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
442 offset += sizeof(struct xsctp_laddr);
443 if (xladdr->last == 1)
447 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
449 switch (xladdr->address.sa.sa_family) {
451 #define __IN_IS_ADDR_LOOPBACK(pina) \
452 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
453 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
454 local_all_loopback = 0;
455 #undef __IN_IS_ADDR_LOOPBACK
456 sockaddr(&laddr->address,
458 &xladdr->address.sin.sin_addr,
459 htons(xstcb->local_port));
462 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
463 local_all_loopback = 0;
464 sockaddr(&laddr->address,
466 &xladdr->address.sin6.sin6_addr,
467 htons(xstcb->local_port));
470 errx(1, "address family %d not supported",
471 xladdr->address.sa.sa_family);
474 if (prev_laddr == NULL)
477 prev_laddr->next = laddr;
481 foreign_all_loopback = 1;
482 while (offset < len) {
483 xraddr = (struct xsctp_raddr *)(void *)(buf + offset);
484 offset += sizeof(struct xsctp_raddr);
485 if (xraddr->last == 1)
489 if ((faddr = calloc(1, sizeof(struct addr))) == NULL)
491 switch (xraddr->address.sa.sa_family) {
493 #define __IN_IS_ADDR_LOOPBACK(pina) \
494 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
495 if (!__IN_IS_ADDR_LOOPBACK(&xraddr->address.sin.sin_addr))
496 foreign_all_loopback = 0;
497 #undef __IN_IS_ADDR_LOOPBACK
498 sockaddr(&faddr->address,
500 &xraddr->address.sin.sin_addr,
501 htons(xstcb->remote_port));
504 if (!IN6_IS_ADDR_LOOPBACK(&xraddr->address.sin6.sin6_addr))
505 foreign_all_loopback = 0;
506 sockaddr(&faddr->address,
508 &xraddr->address.sin6.sin6_addr,
509 htons(xstcb->remote_port));
512 errx(1, "address family %d not supported",
513 xraddr->address.sa.sa_family);
516 if (prev_faddr == NULL)
519 prev_faddr->next = faddr;
523 if (!opt_L || !(local_all_loopback || foreign_all_loopback)) {
524 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
525 sock->next = sockhash[hash];
526 sockhash[hash] = sock;
532 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
533 offset += sizeof(struct xsctp_inpcb);
539 gather_inet(int proto)
541 struct xinpgen *xig, *exig;
547 struct addr *laddr, *faddr;
548 const char *varname, *protoname;
551 int hash, retry, vflag;
561 varname = "net.inet.tcp.pcblist";
565 varname = "net.inet.udp.pcblist";
569 varname = "net.inet.divert.pcblist";
573 errx(1, "protocol %d not supported", proto);
581 if ((buf = realloc(buf, bufsize)) == NULL)
584 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
588 if (errno != ENOMEM || len != bufsize)
589 err(1, "sysctlbyname()");
592 xig = (struct xinpgen *)buf;
593 exig = (struct xinpgen *)(void *)
594 ((char *)buf + len - sizeof *exig);
595 if (xig->xig_len != sizeof *xig ||
596 exig->xig_len != sizeof *exig)
597 errx(1, "struct xinpgen size mismatch");
598 } while (xig->xig_gen != exig->xig_gen && retry--);
600 if (xig->xig_gen != exig->xig_gen && opt_v)
601 warnx("warning: data may be inconsistent");
604 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
607 xip = (struct xinpcb *)xig;
608 xtp = (struct xtcpcb *)xig;
611 if (xtp->xt_len != sizeof(*xtp)) {
612 warnx("struct xtcpcb size mismatch");
616 so = &xtp->xt_socket;
617 protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
621 if (xip->xi_len != sizeof(*xip)) {
622 warnx("struct xinpcb size mismatch");
626 so = &xip->xi_socket;
629 errx(1, "protocol %d not supported", proto);
631 if ((inp->inp_vflag & vflag) == 0)
633 if (inp->inp_vflag & INP_IPV4) {
634 if ((inp->inp_fport == 0 && !opt_l) ||
635 (inp->inp_fport != 0 && !opt_c))
637 #define __IN_IS_ADDR_LOOPBACK(pina) \
638 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
640 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
641 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
643 #undef __IN_IS_ADDR_LOOPBACK
644 } else if (inp->inp_vflag & INP_IPV6) {
645 if ((inp->inp_fport == 0 && !opt_l) ||
646 (inp->inp_fport != 0 && !opt_c))
649 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
650 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
654 warnx("invalid vflag 0x%x", inp->inp_vflag);
657 if ((sock = calloc(1, sizeof(*sock))) == NULL)
659 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
661 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
663 sock->socket = so->xso_so;
665 if (inp->inp_vflag & INP_IPV4) {
666 sock->family = AF_INET;
667 sockaddr(&laddr->address, sock->family,
668 &inp->inp_laddr, inp->inp_lport);
669 sockaddr(&faddr->address, sock->family,
670 &inp->inp_faddr, inp->inp_fport);
671 } else if (inp->inp_vflag & INP_IPV6) {
672 sock->family = AF_INET6;
673 sockaddr(&laddr->address, sock->family,
674 &inp->in6p_laddr, inp->inp_lport);
675 sockaddr(&faddr->address, sock->family,
676 &inp->in6p_faddr, inp->inp_fport);
682 sock->vflag = inp->inp_vflag;
683 if (proto == IPPROTO_TCP)
684 sock->state = xtp->xt_tp.t_state;
685 sock->protoname = protoname;
686 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
687 sock->next = sockhash[hash];
688 sockhash[hash] = sock;
695 gather_unix(int proto)
697 struct xunpgen *xug, *exug;
700 struct addr *laddr, *faddr;
701 const char *varname, *protoname;
708 varname = "net.local.stream.pcblist";
709 protoname = "stream";
712 varname = "net.local.dgram.pcblist";
716 varname = "net.local.seqpacket.pcblist";
717 protoname = "seqpac";
727 if ((buf = realloc(buf, bufsize)) == NULL)
730 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
732 if (errno != ENOMEM || len != bufsize)
733 err(1, "sysctlbyname()");
736 xug = (struct xunpgen *)buf;
737 exug = (struct xunpgen *)(void *)
738 ((char *)buf + len - sizeof(*exug));
739 if (xug->xug_len != sizeof(*xug) ||
740 exug->xug_len != sizeof(*exug)) {
741 warnx("struct xinpgen size mismatch");
744 } while (xug->xug_gen != exug->xug_gen && retry--);
746 if (xug->xug_gen != exug->xug_gen && opt_v)
747 warnx("warning: data may be inconsistent");
750 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
753 xup = (struct xunpcb *)xug;
754 if (xup->xu_len != sizeof(*xup)) {
755 warnx("struct xunpcb size mismatch");
758 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
759 (xup->xu_unp.unp_conn != NULL && !opt_c))
761 if ((sock = calloc(1, sizeof(*sock))) == NULL)
763 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
765 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
767 sock->socket = xup->xu_socket.xso_so;
768 sock->pcb = xup->xu_unpp;
770 sock->family = AF_UNIX;
771 sock->protoname = protoname;
772 if (xup->xu_unp.unp_addr != NULL)
774 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
775 else if (xup->xu_unp.unp_conn != NULL)
776 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
781 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
782 sock->next = sockhash[hash];
783 sockhash[hash] = sock;
794 olen = len = sizeof(*xfiles);
795 if ((xfiles = malloc(len)) == NULL)
797 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
798 if (errno != ENOMEM || len != olen)
799 err(1, "sysctlbyname()");
801 if ((xfiles = realloc(xfiles, len)) == NULL)
804 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
805 errx(1, "struct xfile size mismatch");
806 nxfiles = len / sizeof(*xfiles);
810 printaddr(struct sockaddr_storage *ss)
812 struct sockaddr_un *sun;
813 char addrstr[NI_MAXHOST] = { '\0', '\0' };
814 int error, off, port = 0;
816 switch (ss->ss_family) {
818 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
820 port = ntohs(sstosin(ss)->sin_port);
823 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
825 port = ntohs(sstosin6(ss)->sin6_port);
829 off = (int)((char *)&sun->sun_path - (char *)sun);
830 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
832 if (addrstr[0] == '\0') {
833 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
834 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
836 errx(1, "getnameinfo()");
839 return xprintf("%s:*", addrstr);
841 return xprintf("%s:%d", addrstr, port);
845 getprocname(pid_t pid)
847 static struct kinfo_proc proc;
853 mib[2] = KERN_PROC_PID;
856 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
857 /* Do not warn if the process exits before we get its name. */
862 return (proc.ki_comm);
866 getprocjid(pid_t pid)
868 static struct kinfo_proc proc;
874 mib[2] = KERN_PROC_PID;
877 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
878 /* Do not warn if the process exits before we get its jid. */
883 return (proc.ki_jid);
887 check_ports(struct sock *s)
894 if ((s->family != AF_INET) && (s->family != AF_INET6))
896 for (addr = s->laddr; addr != NULL; addr = addr->next) {
897 if (s->family == AF_INET)
898 port = ntohs(sstosin(&addr->address)->sin_port);
900 port = ntohs(sstosin6(&addr->address)->sin6_port);
904 for (addr = s->faddr; addr != NULL; addr = addr->next) {
905 if (s->family == AF_INET)
906 port = ntohs(sstosin(&addr->address)->sin_port);
908 port = ntohs(sstosin6(&addr->address)->sin6_port);
916 sctp_state(int state)
928 case SCTP_COOKIE_WAIT:
929 return "COOKIE_WAIT";
931 case SCTP_COOKIE_ECHOED:
932 return "COOKIE_ECHOED";
934 case SCTP_ESTABLISHED:
935 return "ESTABLISHED";
937 case SCTP_SHUTDOWN_SENT:
938 return "SHUTDOWN_SENT";
940 case SCTP_SHUTDOWN_RECEIVED:
941 return "SHUTDOWN_RECEIVED";
943 case SCTP_SHUTDOWN_ACK_SENT:
944 return "SHUTDOWN_ACK_SENT";
946 case SCTP_SHUTDOWN_PENDING:
947 return "SHUTDOWN_PENDING";
956 displaysock(struct sock *s, int pos)
960 struct addr *laddr, *faddr;
965 pos += xprintf("%s", s->protoname);
966 if (s->vflag & INP_IPV4)
967 pos += xprintf("4 ");
968 if (s->vflag & INP_IPV6)
969 pos += xprintf("6 ");
973 while (laddr != NULL || faddr != NULL) {
980 pos += printaddr(&laddr->address);
981 if (s->family == AF_INET6 && pos >= 58)
987 pos += printaddr(&faddr->address);
990 if ((laddr == NULL) || (faddr == NULL))
991 errx(1, "laddr = %p or faddr = %p is NULL",
992 (void *)laddr, (void *)faddr);
994 if (laddr->address.ss_len > 0) {
995 pos += printaddr(&laddr->address);
999 p = *(void **)&(faddr->address);
1001 pos += xprintf("(not connected)");
1004 pos += xprintf("-> ");
1005 for (hash = 0; hash < HASHSIZE; ++hash) {
1006 for (s_tmp = sockhash[hash];
1008 s_tmp = s_tmp->next)
1009 if (s_tmp->pcb == p)
1014 if (s_tmp == NULL ||
1015 s_tmp->laddr == NULL ||
1016 s_tmp->laddr->address.ss_len == 0)
1017 pos += xprintf("??");
1019 pos += printaddr(&s_tmp->laddr->address);
1024 if (first && opt_s &&
1025 (s->proto == IPPROTO_SCTP || s->proto == IPPROTO_TCP)) {
1027 pos += xprintf(" ");
1030 pos += xprintf("%s", sctp_state(s->state));
1033 if (s->state >= 0 && s->state < TCP_NSTATES)
1035 xprintf("%s", tcpstates[s->state]);
1037 pos += xprintf("?");
1042 laddr = laddr->next;
1044 faddr = faddr->next;
1045 if ((laddr != NULL) || (faddr != NULL)) {
1062 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s",
1063 "USER", "COMMAND", "PID", "FD", "PROTO",
1064 "LOCAL ADDRESS", "FOREIGN ADDRESS");
1066 printf(" %-12s", "STATE");
1069 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1070 if (xf->xf_data == NULL)
1072 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1074 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1075 for (s = sockhash[hash]; s != NULL; s = s->next) {
1076 if ((void *)s->socket != xf->xf_data)
1078 if (!check_ports(s))
1082 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1083 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1085 pos += xprintf("%s ", pwd->pw_name);
1087 pos += xprintf(" ");
1088 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1090 pos += xprintf(" ");
1091 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1093 pos += xprintf(" ");
1094 pos += xprintf("%d ", xf->xf_fd);
1095 displaysock(s, pos);
1100 for (hash = 0; hash < HASHSIZE; hash++) {
1101 for (s = sockhash[hash]; s != NULL; s = s->next) {
1104 if (!check_ports(s))
1107 pos += xprintf("%-8s %-10s %-5s %-2s ",
1108 "?", "?", "?", "?");
1109 displaysock(s, pos);
1114 static int set_default_protos(void)
1116 struct protoent *prot;
1120 init_protos(default_numprotos);
1122 for (pindex = 0; pindex < default_numprotos; pindex++) {
1123 pname = default_protos[pindex];
1124 prot = getprotobyname(pname);
1126 err(1, "getprotobyname: %s", pname);
1127 protos[pindex] = prot->p_proto;
1138 "usage: sockstat [-46cLlsu] [-j jid] [-p ports] [-P protocols]\n");
1143 main(int argc, char *argv[])
1145 int protos_defined = -1;
1149 while ((o = getopt(argc, argv, "46cj:Llp:P:suv")) != -1)
1161 opt_j = jail_getid(optarg);
1163 errx(1, "%s", jail_errmsg);
1172 parse_ports(optarg);
1175 protos_defined = parse_protos(optarg);
1196 if ((!opt_4 && !opt_6) && protos_defined != -1)
1198 if (!opt_4 && !opt_6 && !opt_u)
1199 opt_4 = opt_6 = opt_u = 1;
1200 if ((opt_4 || opt_6) && protos_defined == -1)
1201 protos_defined = set_default_protos();
1202 if (!opt_c && !opt_l)
1205 if (opt_4 || opt_6) {
1206 for (i = 0; i < protos_defined; i++)
1207 if (protos[i] == IPPROTO_SCTP)
1210 gather_inet(protos[i]);
1213 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1214 gather_unix(SOCK_STREAM);
1215 gather_unix(SOCK_DGRAM);
1216 gather_unix(SOCK_SEQPACKET);