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;
258 free_socket(struct sock *sock)
260 struct addr *cur, *next;
263 while (cur != NULL) {
269 while (cur != NULL) {
281 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
282 struct xsctp_inpcb *xinpcb;
283 struct xsctp_tcb *xstcb;
284 struct xsctp_raddr *xraddr;
285 struct xsctp_laddr *xladdr;
290 int no_stcb, local_all_loopback, foreign_all_loopback;
298 varname = "net.inet.sctp.assoclist";
299 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
301 err(1, "sysctlbyname()");
304 if ((buf = (char *)malloc(len)) == NULL) {
308 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
309 err(1, "sysctlbyname()");
313 xinpcb = (struct xsctp_inpcb *)(void *)buf;
314 offset = sizeof(struct xsctp_inpcb);
315 while ((offset < len) && (xinpcb->last == 0)) {
316 if ((sock = calloc(1, sizeof *sock)) == NULL)
318 sock->socket = xinpcb->socket;
319 sock->proto = IPPROTO_SCTP;
320 sock->protoname = "sctp";
321 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
322 sock->family = AF_INET6;
323 sock->vflag = INP_IPV6;
325 sock->family = AF_INET;
326 sock->vflag = INP_IPV4;
329 local_all_loopback = 1;
330 while (offset < len) {
331 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
332 offset += sizeof(struct xsctp_laddr);
333 if (xladdr->last == 1)
335 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
337 switch (xladdr->address.sa.sa_family) {
339 #define __IN_IS_ADDR_LOOPBACK(pina) \
340 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
341 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
342 local_all_loopback = 0;
343 #undef __IN_IS_ADDR_LOOPBACK
344 sockaddr(&laddr->address,
346 &xladdr->address.sin.sin_addr,
347 htons(xinpcb->local_port));
350 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
351 local_all_loopback = 0;
352 sockaddr(&laddr->address,
354 &xladdr->address.sin6.sin6_addr,
355 htons(xinpcb->local_port));
358 errx(1, "adress family %d not supported",
359 xladdr->address.sa.sa_family);
362 if (prev_laddr == NULL)
365 prev_laddr->next = laddr;
368 if (sock->laddr == NULL) {
369 if ((sock->laddr = calloc(1, sizeof(struct addr))) == NULL)
371 sock->laddr->address.ss_family = sock->family;
372 if (sock->family == AF_INET)
373 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
375 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
376 local_all_loopback = 0;
378 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
380 sock->faddr->address.ss_family = sock->family;
381 if (sock->family == AF_INET)
382 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
384 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
386 while (offset < len) {
387 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
388 offset += sizeof(struct xsctp_tcb);
391 (!opt_L || !local_all_loopback) &&
392 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
393 (xstcb->last == 1))) {
394 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
395 sock->next = sockhash[hash];
396 sockhash[hash] = sock;
401 if (xstcb->last == 1)
405 if ((sock = calloc(1, sizeof *sock)) == NULL)
407 sock->socket = xinpcb->socket;
408 sock->proto = IPPROTO_SCTP;
409 sock->protoname = "sctp";
410 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
411 sock->family = AF_INET6;
412 sock->vflag = INP_IPV6;
414 sock->family = AF_INET;
415 sock->vflag = INP_IPV4;
419 local_all_loopback = 1;
420 while (offset < len) {
421 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
422 offset += sizeof(struct xsctp_laddr);
423 if (xladdr->last == 1)
427 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
429 switch (xladdr->address.sa.sa_family) {
431 #define __IN_IS_ADDR_LOOPBACK(pina) \
432 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
433 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
434 local_all_loopback = 0;
435 #undef __IN_IS_ADDR_LOOPBACK
436 sockaddr(&laddr->address,
438 &xladdr->address.sin.sin_addr,
439 htons(xstcb->local_port));
442 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
443 local_all_loopback = 0;
444 sockaddr(&laddr->address,
446 &xladdr->address.sin6.sin6_addr,
447 htons(xstcb->local_port));
450 errx(1, "adress family %d not supported",
451 xladdr->address.sa.sa_family);
454 if (prev_laddr == NULL)
457 prev_laddr->next = laddr;
461 foreign_all_loopback = 1;
462 while (offset < len) {
463 xraddr = (struct xsctp_raddr *)(void *)(buf + offset);
464 offset += sizeof(struct xsctp_raddr);
465 if (xraddr->last == 1)
469 if ((faddr = calloc(1, sizeof(struct addr))) == NULL)
471 switch (xraddr->address.sa.sa_family) {
473 #define __IN_IS_ADDR_LOOPBACK(pina) \
474 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
475 if (!__IN_IS_ADDR_LOOPBACK(&xraddr->address.sin.sin_addr))
476 foreign_all_loopback = 0;
477 #undef __IN_IS_ADDR_LOOPBACK
478 sockaddr(&faddr->address,
480 &xraddr->address.sin.sin_addr,
481 htons(xstcb->remote_port));
484 if (!IN6_IS_ADDR_LOOPBACK(&xraddr->address.sin6.sin6_addr))
485 foreign_all_loopback = 0;
486 sockaddr(&faddr->address,
488 &xraddr->address.sin6.sin6_addr,
489 htons(xstcb->remote_port));
492 errx(1, "adress family %d not supported",
493 xraddr->address.sa.sa_family);
496 if (prev_faddr == NULL)
499 prev_faddr->next = faddr;
503 if (!opt_L || !(local_all_loopback || foreign_all_loopback)) {
504 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
505 sock->next = sockhash[hash];
506 sockhash[hash] = sock;
512 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
513 offset += sizeof(struct xsctp_inpcb);
519 gather_inet(int proto)
521 struct xinpgen *xig, *exig;
527 struct addr *laddr, *faddr;
528 const char *varname, *protoname;
531 int hash, retry, vflag;
541 varname = "net.inet.tcp.pcblist";
545 varname = "net.inet.udp.pcblist";
549 varname = "net.inet.divert.pcblist";
553 errx(1, "protocol %d not supported", proto);
561 if ((buf = realloc(buf, bufsize)) == NULL)
564 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
568 if (errno != ENOMEM || len != bufsize)
569 err(1, "sysctlbyname()");
572 xig = (struct xinpgen *)buf;
573 exig = (struct xinpgen *)(void *)
574 ((char *)buf + len - sizeof *exig);
575 if (xig->xig_len != sizeof *xig ||
576 exig->xig_len != sizeof *exig)
577 errx(1, "struct xinpgen size mismatch");
578 } while (xig->xig_gen != exig->xig_gen && retry--);
580 if (xig->xig_gen != exig->xig_gen && opt_v)
581 warnx("warning: data may be inconsistent");
584 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
589 xtp = (struct xtcpcb *)xig;
590 if (xtp->xt_len != sizeof *xtp) {
591 warnx("struct xtcpcb size mismatch");
595 so = &xtp->xt_socket;
596 protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
600 xip = (struct xinpcb *)xig;
601 if (xip->xi_len != sizeof *xip) {
602 warnx("struct xinpcb size mismatch");
606 so = &xip->xi_socket;
609 errx(1, "protocol %d not supported", proto);
611 if ((inp->inp_vflag & vflag) == 0)
613 if (inp->inp_vflag & INP_IPV4) {
614 if ((inp->inp_fport == 0 && !opt_l) ||
615 (inp->inp_fport != 0 && !opt_c))
617 #define __IN_IS_ADDR_LOOPBACK(pina) \
618 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
620 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
621 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
623 #undef __IN_IS_ADDR_LOOPBACK
624 } else if (inp->inp_vflag & INP_IPV6) {
625 if ((inp->inp_fport == 0 && !opt_l) ||
626 (inp->inp_fport != 0 && !opt_c))
629 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
630 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
634 warnx("invalid vflag 0x%x", inp->inp_vflag);
637 if ((sock = calloc(1, sizeof *sock)) == NULL)
639 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
641 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
643 sock->socket = so->xso_so;
645 if (inp->inp_vflag & INP_IPV4) {
646 sock->family = AF_INET;
647 sockaddr(&laddr->address, sock->family,
648 &inp->inp_laddr, inp->inp_lport);
649 sockaddr(&faddr->address, sock->family,
650 &inp->inp_faddr, inp->inp_fport);
651 } else if (inp->inp_vflag & INP_IPV6) {
652 sock->family = AF_INET6;
653 sockaddr(&laddr->address, sock->family,
654 &inp->in6p_laddr, inp->inp_lport);
655 sockaddr(&faddr->address, sock->family,
656 &inp->in6p_faddr, inp->inp_fport);
662 sock->vflag = inp->inp_vflag;
663 sock->protoname = protoname;
664 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
665 sock->next = sockhash[hash];
666 sockhash[hash] = sock;
673 gather_unix(int proto)
675 struct xunpgen *xug, *exug;
678 struct addr *laddr, *faddr;
679 const char *varname, *protoname;
686 varname = "net.local.stream.pcblist";
687 protoname = "stream";
690 varname = "net.local.dgram.pcblist";
701 if ((buf = realloc(buf, bufsize)) == NULL)
704 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
706 if (errno != ENOMEM || len != bufsize)
707 err(1, "sysctlbyname()");
710 xug = (struct xunpgen *)buf;
711 exug = (struct xunpgen *)(void *)
712 ((char *)buf + len - sizeof *exug);
713 if (xug->xug_len != sizeof *xug ||
714 exug->xug_len != sizeof *exug) {
715 warnx("struct xinpgen size mismatch");
718 } while (xug->xug_gen != exug->xug_gen && retry--);
720 if (xug->xug_gen != exug->xug_gen && opt_v)
721 warnx("warning: data may be inconsistent");
724 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
727 xup = (struct xunpcb *)xug;
728 if (xup->xu_len != sizeof *xup) {
729 warnx("struct xunpcb size mismatch");
732 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
733 (xup->xu_unp.unp_conn != NULL && !opt_c))
735 if ((sock = calloc(1, sizeof *sock)) == NULL)
737 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
739 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
741 sock->socket = xup->xu_socket.xso_so;
742 sock->pcb = xup->xu_unpp;
744 sock->family = AF_UNIX;
745 sock->protoname = protoname;
746 if (xup->xu_unp.unp_addr != NULL)
748 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
749 else if (xup->xu_unp.unp_conn != NULL)
750 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
755 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
756 sock->next = sockhash[hash];
757 sockhash[hash] = sock;
768 olen = len = sizeof *xfiles;
769 if ((xfiles = malloc(len)) == NULL)
771 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
772 if (errno != ENOMEM || len != olen)
773 err(1, "sysctlbyname()");
775 if ((xfiles = realloc(xfiles, len)) == NULL)
778 if (len > 0 && xfiles->xf_size != sizeof *xfiles)
779 errx(1, "struct xfile size mismatch");
780 nxfiles = len / sizeof *xfiles;
784 printaddr(struct sockaddr_storage *ss)
786 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' };
787 struct sockaddr_un *sun;
788 void *addr = NULL; /* Keep compiler happy. */
791 switch (ss->ss_family) {
793 addr = &((struct sockaddr_in *)ss)->sin_addr;
794 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY)
796 port = ntohs(((struct sockaddr_in *)ss)->sin_port);
799 addr = &((struct sockaddr_in6 *)ss)->sin6_addr;
800 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr))
802 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port);
805 sun = (struct sockaddr_un *)ss;
806 off = (int)((char *)&sun->sun_path - (char *)sun);
807 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
809 if (addrstr[0] == '\0')
810 inet_ntop(ss->ss_family, addr, addrstr, sizeof addrstr);
812 return xprintf("%s:*", addrstr);
814 return xprintf("%s:%d", addrstr, port);
818 getprocname(pid_t pid)
820 static struct kinfo_proc proc;
826 mib[2] = KERN_PROC_PID;
829 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
830 /* Do not warn if the process exits before we get its name. */
835 return (proc.ki_comm);
839 getprocjid(pid_t pid)
841 static struct kinfo_proc proc;
847 mib[2] = KERN_PROC_PID;
850 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) {
851 /* Do not warn if the process exits before we get its jid. */
856 return (proc.ki_jid);
860 check_ports(struct sock *s)
867 if ((s->family != AF_INET) && (s->family != AF_INET6))
869 for (addr = s->laddr; addr != NULL; addr = addr->next) {
870 if (addr->address.ss_family == AF_INET)
871 port = ntohs(((struct sockaddr_in *)(&addr->address))->sin_port);
873 port = ntohs(((struct sockaddr_in6 *)(&addr->address))->sin6_port);
877 for (addr = s->faddr; addr != NULL; addr = addr->next) {
878 if (addr->address.ss_family == AF_INET)
879 port = ntohs(((struct sockaddr_in *)&(addr->address))->sin_port);
881 port = ntohs(((struct sockaddr_in6 *)&(addr->address))->sin6_port);
889 displaysock(struct sock *s, int pos)
893 struct addr *laddr, *faddr;
898 pos += xprintf("%s", s->protoname);
899 if (s->vflag & INP_IPV4)
900 pos += xprintf("4 ");
901 if (s->vflag & INP_IPV6)
902 pos += xprintf("6 ");
905 while (laddr != NULL || faddr != NULL) {
912 pos += printaddr(&laddr->address);
913 if (s->family == AF_INET6 && pos >= 58)
919 pos += printaddr(&faddr->address);
922 if ((laddr == NULL) || (faddr == NULL))
923 errx(1, "laddr = %p or faddr = %p is NULL",
924 (void *)laddr, (void *)faddr);
926 if (laddr->address.ss_len > 0) {
927 pos += printaddr(&laddr->address);
931 p = *(void **)&(faddr->address);
933 pos += xprintf("(not connected)");
936 pos += xprintf("-> ");
937 for (hash = 0; hash < HASHSIZE; ++hash) {
938 for (s_tmp = sockhash[hash];
947 s_tmp->laddr == NULL ||
948 s_tmp->laddr->address.ss_len == 0)
949 pos += xprintf("??");
951 pos += printaddr(&s_tmp->laddr->address);
960 if ((laddr != NULL) || (faddr != NULL)) {
976 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
977 "USER", "COMMAND", "PID", "FD", "PROTO",
978 "LOCAL ADDRESS", "FOREIGN ADDRESS");
980 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
981 if (xf->xf_data == NULL)
983 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
985 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
986 for (s = sockhash[hash]; s != NULL; s = s->next) {
987 if ((void *)s->socket != xf->xf_data)
993 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
994 pos += xprintf("%lu ", (u_long)xf->xf_uid);
996 pos += xprintf("%s ", pwd->pw_name);
999 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1001 pos += xprintf(" ");
1002 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1004 pos += xprintf(" ");
1005 pos += xprintf("%d ", xf->xf_fd);
1006 displaysock(s, pos);
1011 for (hash = 0; hash < HASHSIZE; hash++) {
1012 for (s = sockhash[hash]; s != NULL; s = s->next) {
1015 if (!check_ports(s))
1018 pos += xprintf("%-8s %-10s %-5s %-2s ",
1019 "?", "?", "?", "?");
1020 displaysock(s, pos);
1025 static int set_default_protos(void)
1027 struct protoent *prot;
1031 init_protos(default_numprotos);
1033 for (pindex = 0; pindex < default_numprotos; pindex++) {
1034 pname = default_protos[pindex];
1035 prot = getprotobyname(pname);
1037 err(1, "getprotobyname: %s", pname);
1038 protos[pindex] = prot->p_proto;
1049 "Usage: sockstat [-46cLlu] [-j jid] [-p ports] [-P protocols]\n");
1054 main(int argc, char *argv[])
1056 int protos_defined = -1;
1060 while ((o = getopt(argc, argv, "46cj:Llp:P:uv")) != -1)
1072 opt_j = atoi(optarg);
1081 parse_ports(optarg);
1084 protos_defined = parse_protos(optarg);
1102 if ((!opt_4 && !opt_6) && protos_defined != -1)
1104 if (!opt_4 && !opt_6 && !opt_u)
1105 opt_4 = opt_6 = opt_u = 1;
1106 if ((opt_4 || opt_6) && protos_defined == -1)
1107 protos_defined = set_default_protos();
1108 if (!opt_c && !opt_l)
1111 if (opt_4 || opt_6) {
1112 for (i = 0; i < protos_defined; i++)
1113 if (protos[i] == IPPROTO_SCTP)
1116 gather_inet(protos[i]);
1119 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1120 gather_unix(SOCK_STREAM);
1121 gather_unix(SOCK_DGRAM);