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 #define sstosin(ss) ((struct sockaddr_in *)(ss))
64 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
65 #define sstosun(ss) ((struct sockaddr_un *)(ss))
66 #define sstosa(ss) ((struct sockaddr *)(ss))
68 static int opt_4; /* Show IPv4 sockets */
69 static int opt_6; /* Show IPv6 sockets */
70 static int opt_c; /* Show connected sockets */
71 static int opt_j; /* Show specified jail */
72 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
73 static int opt_l; /* Show listening sockets */
74 static int opt_u; /* Show Unix domain sockets */
75 static int opt_v; /* Verbose mode */
78 * Default protocols to use if no -P was defined.
80 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
81 static size_t default_numprotos = nitems(default_protos);
83 static int *protos; /* protocols to use */
84 static size_t numprotos; /* allocated size of protos[] */
88 #define INT_BIT (sizeof(int)*CHAR_BIT)
89 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
90 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
93 struct sockaddr_storage address;
104 const char *protoname;
110 #define HASHSIZE 1009
111 static struct sock *sockhash[HASHSIZE];
113 static struct xfile *xfiles;
117 xprintf(const char *fmt, ...)
123 len = vprintf(fmt, ap);
132 get_proto_type(const char *proto)
134 struct protoent *pent;
136 if (strlen(proto) == 0)
138 pent = getprotobyname(proto);
140 warn("getprotobyname");
143 return (pent->p_proto);
155 /* Find the maximum number of possible protocols. */
156 while (getprotoent() != NULL)
161 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
163 numprotos = proto_count;
168 parse_protos(char *protospec)
171 int proto_type, proto_index;
173 if (protospec == NULL)
178 while ((prot = strsep(&protospec, ",")) != NULL) {
179 if (strlen(prot) == 0)
181 proto_type = get_proto_type(prot);
182 if (proto_type != -1)
183 protos[proto_index++] = proto_type;
185 numprotos = proto_index;
186 return (proto_index);
191 parse_ports(const char *portspec)
197 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
202 errx(1, "syntax error in port range");
203 for (q = p; *q != '\0' && isdigit(*q); ++q)
205 for (port = 0; p < q; ++p)
206 port = port * 10 + digittoint(*p);
207 if (port < 0 || port > 65535)
208 errx(1, "invalid port number");
221 for (q = p; *q != '\0' && isdigit(*q); ++q)
223 for (end = 0; p < q; ++p)
224 end = end * 10 + digittoint(*p);
225 if (end < port || end > 65535)
226 errx(1, "invalid port number");
235 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
237 struct sockaddr_in *sin4;
238 struct sockaddr_in6 *sin6;
240 bzero(ss, sizeof(*ss));
244 sin4->sin_len = sizeof(*sin4);
245 sin4->sin_family = af;
246 sin4->sin_port = port;
247 sin4->sin_addr = *(struct in_addr *)addr;
251 sin6->sin6_len = sizeof(*sin6);
252 sin6->sin6_family = af;
253 sin6->sin6_port = port;
254 sin6->sin6_addr = *(struct in6_addr *)addr;
255 #define s6_addr16 __u6_addr.__u6_addr16
256 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
257 sin6->sin6_scope_id =
258 ntohs(sin6->sin6_addr.s6_addr16[1]);
259 sin6->sin6_addr.s6_addr16[1] = 0;
268 free_socket(struct sock *sock)
270 struct addr *cur, *next;
273 while (cur != NULL) {
279 while (cur != NULL) {
291 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
292 struct xsctp_inpcb *xinpcb;
293 struct xsctp_tcb *xstcb;
294 struct xsctp_raddr *xraddr;
295 struct xsctp_laddr *xladdr;
300 int no_stcb, local_all_loopback, foreign_all_loopback;
308 varname = "net.inet.sctp.assoclist";
309 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
311 err(1, "sysctlbyname()");
314 if ((buf = (char *)malloc(len)) == NULL) {
318 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
319 err(1, "sysctlbyname()");
323 xinpcb = (struct xsctp_inpcb *)(void *)buf;
324 offset = sizeof(struct xsctp_inpcb);
325 while ((offset < len) && (xinpcb->last == 0)) {
326 if ((sock = calloc(1, sizeof *sock)) == NULL)
328 sock->socket = xinpcb->socket;
329 sock->proto = IPPROTO_SCTP;
330 sock->protoname = "sctp";
331 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
332 sock->family = AF_INET6;
333 sock->vflag = INP_IPV6;
335 sock->family = AF_INET;
336 sock->vflag = INP_IPV4;
339 local_all_loopback = 1;
340 while (offset < len) {
341 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
342 offset += sizeof(struct xsctp_laddr);
343 if (xladdr->last == 1)
345 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
347 switch (xladdr->address.sa.sa_family) {
349 #define __IN_IS_ADDR_LOOPBACK(pina) \
350 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
351 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
352 local_all_loopback = 0;
353 #undef __IN_IS_ADDR_LOOPBACK
354 sockaddr(&laddr->address,
356 &xladdr->address.sin.sin_addr,
357 htons(xinpcb->local_port));
360 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
361 local_all_loopback = 0;
362 sockaddr(&laddr->address,
364 &xladdr->address.sin6.sin6_addr,
365 htons(xinpcb->local_port));
368 errx(1, "address family %d not supported",
369 xladdr->address.sa.sa_family);
372 if (prev_laddr == NULL)
375 prev_laddr->next = laddr;
378 if (sock->laddr == NULL) {
379 if ((sock->laddr = calloc(1, sizeof(struct addr))) == NULL)
381 sock->laddr->address.ss_family = sock->family;
382 if (sock->family == AF_INET)
383 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
385 sock->laddr->address.ss_len = sizeof(struct sockaddr_in);
386 local_all_loopback = 0;
388 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
390 sock->faddr->address.ss_family = sock->family;
391 if (sock->family == AF_INET)
392 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
394 sock->faddr->address.ss_len = sizeof(struct sockaddr_in);
396 while (offset < len) {
397 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
398 offset += sizeof(struct xsctp_tcb);
401 (!opt_L || !local_all_loopback) &&
402 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
403 (xstcb->last == 1))) {
404 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
405 sock->next = sockhash[hash];
406 sockhash[hash] = sock;
411 if (xstcb->last == 1)
415 if ((sock = calloc(1, sizeof *sock)) == NULL)
417 sock->socket = xinpcb->socket;
418 sock->proto = IPPROTO_SCTP;
419 sock->protoname = "sctp";
420 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
421 sock->family = AF_INET6;
422 sock->vflag = INP_IPV6;
424 sock->family = AF_INET;
425 sock->vflag = INP_IPV4;
429 local_all_loopback = 1;
430 while (offset < len) {
431 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
432 offset += sizeof(struct xsctp_laddr);
433 if (xladdr->last == 1)
437 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
439 switch (xladdr->address.sa.sa_family) {
441 #define __IN_IS_ADDR_LOOPBACK(pina) \
442 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
443 if (!__IN_IS_ADDR_LOOPBACK(&xladdr->address.sin.sin_addr))
444 local_all_loopback = 0;
445 #undef __IN_IS_ADDR_LOOPBACK
446 sockaddr(&laddr->address,
448 &xladdr->address.sin.sin_addr,
449 htons(xstcb->local_port));
452 if (!IN6_IS_ADDR_LOOPBACK(&xladdr->address.sin6.sin6_addr))
453 local_all_loopback = 0;
454 sockaddr(&laddr->address,
456 &xladdr->address.sin6.sin6_addr,
457 htons(xstcb->local_port));
460 errx(1, "address family %d not supported",
461 xladdr->address.sa.sa_family);
464 if (prev_laddr == NULL)
467 prev_laddr->next = laddr;
471 foreign_all_loopback = 1;
472 while (offset < len) {
473 xraddr = (struct xsctp_raddr *)(void *)(buf + offset);
474 offset += sizeof(struct xsctp_raddr);
475 if (xraddr->last == 1)
479 if ((faddr = calloc(1, sizeof(struct addr))) == NULL)
481 switch (xraddr->address.sa.sa_family) {
483 #define __IN_IS_ADDR_LOOPBACK(pina) \
484 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
485 if (!__IN_IS_ADDR_LOOPBACK(&xraddr->address.sin.sin_addr))
486 foreign_all_loopback = 0;
487 #undef __IN_IS_ADDR_LOOPBACK
488 sockaddr(&faddr->address,
490 &xraddr->address.sin.sin_addr,
491 htons(xstcb->remote_port));
494 if (!IN6_IS_ADDR_LOOPBACK(&xraddr->address.sin6.sin6_addr))
495 foreign_all_loopback = 0;
496 sockaddr(&faddr->address,
498 &xraddr->address.sin6.sin6_addr,
499 htons(xstcb->remote_port));
502 errx(1, "address family %d not supported",
503 xraddr->address.sa.sa_family);
506 if (prev_faddr == NULL)
509 prev_faddr->next = faddr;
513 if (!opt_L || !(local_all_loopback || foreign_all_loopback)) {
514 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
515 sock->next = sockhash[hash];
516 sockhash[hash] = sock;
522 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
523 offset += sizeof(struct xsctp_inpcb);
529 gather_inet(int proto)
531 struct xinpgen *xig, *exig;
537 struct addr *laddr, *faddr;
538 const char *varname, *protoname;
541 int hash, retry, vflag;
551 varname = "net.inet.tcp.pcblist";
555 varname = "net.inet.udp.pcblist";
559 varname = "net.inet.divert.pcblist";
563 errx(1, "protocol %d not supported", proto);
571 if ((buf = realloc(buf, bufsize)) == NULL)
574 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
578 if (errno != ENOMEM || len != bufsize)
579 err(1, "sysctlbyname()");
582 xig = (struct xinpgen *)buf;
583 exig = (struct xinpgen *)(void *)
584 ((char *)buf + len - sizeof *exig);
585 if (xig->xig_len != sizeof *xig ||
586 exig->xig_len != sizeof *exig)
587 errx(1, "struct xinpgen size mismatch");
588 } while (xig->xig_gen != exig->xig_gen && retry--);
590 if (xig->xig_gen != exig->xig_gen && opt_v)
591 warnx("warning: data may be inconsistent");
594 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
599 xtp = (struct xtcpcb *)xig;
600 if (xtp->xt_len != sizeof(*xtp)) {
601 warnx("struct xtcpcb size mismatch");
605 so = &xtp->xt_socket;
606 protoname = xtp->xt_tp.t_flags & TF_TOE ? "toe" : "tcp";
610 xip = (struct xinpcb *)xig;
611 if (xip->xi_len != sizeof(*xip)) {
612 warnx("struct xinpcb size mismatch");
616 so = &xip->xi_socket;
619 errx(1, "protocol %d not supported", proto);
621 if ((inp->inp_vflag & vflag) == 0)
623 if (inp->inp_vflag & INP_IPV4) {
624 if ((inp->inp_fport == 0 && !opt_l) ||
625 (inp->inp_fport != 0 && !opt_c))
627 #define __IN_IS_ADDR_LOOPBACK(pina) \
628 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
630 (__IN_IS_ADDR_LOOPBACK(&inp->inp_faddr) ||
631 __IN_IS_ADDR_LOOPBACK(&inp->inp_laddr)))
633 #undef __IN_IS_ADDR_LOOPBACK
634 } else if (inp->inp_vflag & INP_IPV6) {
635 if ((inp->inp_fport == 0 && !opt_l) ||
636 (inp->inp_fport != 0 && !opt_c))
639 (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) ||
640 IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr)))
644 warnx("invalid vflag 0x%x", inp->inp_vflag);
647 if ((sock = calloc(1, sizeof(*sock))) == NULL)
649 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
651 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
653 sock->socket = so->xso_so;
655 if (inp->inp_vflag & INP_IPV4) {
656 sock->family = AF_INET;
657 sockaddr(&laddr->address, sock->family,
658 &inp->inp_laddr, inp->inp_lport);
659 sockaddr(&faddr->address, sock->family,
660 &inp->inp_faddr, inp->inp_fport);
661 } else if (inp->inp_vflag & INP_IPV6) {
662 sock->family = AF_INET6;
663 sockaddr(&laddr->address, sock->family,
664 &inp->in6p_laddr, inp->inp_lport);
665 sockaddr(&faddr->address, sock->family,
666 &inp->in6p_faddr, inp->inp_fport);
672 sock->vflag = inp->inp_vflag;
673 sock->protoname = protoname;
674 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
675 sock->next = sockhash[hash];
676 sockhash[hash] = sock;
683 gather_unix(int proto)
685 struct xunpgen *xug, *exug;
688 struct addr *laddr, *faddr;
689 const char *varname, *protoname;
696 varname = "net.local.stream.pcblist";
697 protoname = "stream";
700 varname = "net.local.dgram.pcblist";
704 varname = "net.local.seqpacket.pcblist";
705 protoname = "seqpac";
715 if ((buf = realloc(buf, bufsize)) == NULL)
718 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
720 if (errno != ENOMEM || len != bufsize)
721 err(1, "sysctlbyname()");
724 xug = (struct xunpgen *)buf;
725 exug = (struct xunpgen *)(void *)
726 ((char *)buf + len - sizeof(*exug));
727 if (xug->xug_len != sizeof(*xug) ||
728 exug->xug_len != sizeof(*exug)) {
729 warnx("struct xinpgen size mismatch");
732 } while (xug->xug_gen != exug->xug_gen && retry--);
734 if (xug->xug_gen != exug->xug_gen && opt_v)
735 warnx("warning: data may be inconsistent");
738 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
741 xup = (struct xunpcb *)xug;
742 if (xup->xu_len != sizeof(*xup)) {
743 warnx("struct xunpcb size mismatch");
746 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
747 (xup->xu_unp.unp_conn != NULL && !opt_c))
749 if ((sock = calloc(1, sizeof(*sock))) == NULL)
751 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
753 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
755 sock->socket = xup->xu_socket.xso_so;
756 sock->pcb = xup->xu_unpp;
758 sock->family = AF_UNIX;
759 sock->protoname = protoname;
760 if (xup->xu_unp.unp_addr != NULL)
762 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
763 else if (xup->xu_unp.unp_conn != NULL)
764 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
769 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
770 sock->next = sockhash[hash];
771 sockhash[hash] = sock;
782 olen = len = sizeof(*xfiles);
783 if ((xfiles = malloc(len)) == NULL)
785 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
786 if (errno != ENOMEM || len != olen)
787 err(1, "sysctlbyname()");
789 if ((xfiles = realloc(xfiles, len)) == NULL)
792 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
793 errx(1, "struct xfile size mismatch");
794 nxfiles = len / sizeof(*xfiles);
798 printaddr(struct sockaddr_storage *ss)
800 struct sockaddr_un *sun;
801 char addrstr[NI_MAXHOST] = { '\0', '\0' };
802 int error, off, port = 0;
804 switch (ss->ss_family) {
806 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
808 port = ntohs(sstosin(ss)->sin_port);
811 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
813 port = ntohs(sstosin6(ss)->sin6_port);
817 off = (int)((char *)&sun->sun_path - (char *)sun);
818 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
820 if (addrstr[0] == '\0') {
821 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
822 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
824 errx(1, "getnameinfo()");
827 return xprintf("%s:*", addrstr);
829 return xprintf("%s:%d", addrstr, port);
833 getprocname(pid_t pid)
835 static struct kinfo_proc proc;
841 mib[2] = KERN_PROC_PID;
844 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
845 /* Do not warn if the process exits before we get its name. */
850 return (proc.ki_comm);
854 getprocjid(pid_t pid)
856 static struct kinfo_proc proc;
862 mib[2] = KERN_PROC_PID;
865 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
866 /* Do not warn if the process exits before we get its jid. */
871 return (proc.ki_jid);
875 check_ports(struct sock *s)
882 if ((s->family != AF_INET) && (s->family != AF_INET6))
884 for (addr = s->laddr; addr != NULL; addr = addr->next) {
885 if (s->family == AF_INET)
886 port = ntohs(sstosin(&addr->address)->sin_port);
888 port = ntohs(sstosin6(&addr->address)->sin6_port);
892 for (addr = s->faddr; addr != NULL; addr = addr->next) {
893 if (s->family == AF_INET)
894 port = ntohs(sstosin(&addr->address)->sin_port);
896 port = ntohs(sstosin6(&addr->address)->sin6_port);
904 displaysock(struct sock *s, int pos)
908 struct addr *laddr, *faddr;
913 pos += xprintf("%s", s->protoname);
914 if (s->vflag & INP_IPV4)
915 pos += xprintf("4 ");
916 if (s->vflag & INP_IPV6)
917 pos += xprintf("6 ");
920 while (laddr != NULL || faddr != NULL) {
927 pos += printaddr(&laddr->address);
928 if (s->family == AF_INET6 && pos >= 58)
934 pos += printaddr(&faddr->address);
937 if ((laddr == NULL) || (faddr == NULL))
938 errx(1, "laddr = %p or faddr = %p is NULL",
939 (void *)laddr, (void *)faddr);
941 if (laddr->address.ss_len > 0) {
942 pos += printaddr(&laddr->address);
946 p = *(void **)&(faddr->address);
948 pos += xprintf("(not connected)");
951 pos += xprintf("-> ");
952 for (hash = 0; hash < HASHSIZE; ++hash) {
953 for (s_tmp = sockhash[hash];
962 s_tmp->laddr == NULL ||
963 s_tmp->laddr->address.ss_len == 0)
964 pos += xprintf("??");
966 pos += printaddr(&s_tmp->laddr->address);
975 if ((laddr != NULL) || (faddr != NULL)) {
991 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n",
992 "USER", "COMMAND", "PID", "FD", "PROTO",
993 "LOCAL ADDRESS", "FOREIGN ADDRESS");
995 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
996 if (xf->xf_data == NULL)
998 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1000 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1001 for (s = sockhash[hash]; s != NULL; s = s->next) {
1002 if ((void *)s->socket != xf->xf_data)
1004 if (!check_ports(s))
1008 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1009 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1011 pos += xprintf("%s ", pwd->pw_name);
1013 pos += xprintf(" ");
1014 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1016 pos += xprintf(" ");
1017 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1019 pos += xprintf(" ");
1020 pos += xprintf("%d ", xf->xf_fd);
1021 displaysock(s, pos);
1026 for (hash = 0; hash < HASHSIZE; hash++) {
1027 for (s = sockhash[hash]; s != NULL; s = s->next) {
1030 if (!check_ports(s))
1033 pos += xprintf("%-8s %-10s %-5s %-2s ",
1034 "?", "?", "?", "?");
1035 displaysock(s, pos);
1040 static int set_default_protos(void)
1042 struct protoent *prot;
1046 init_protos(default_numprotos);
1048 for (pindex = 0; pindex < default_numprotos; pindex++) {
1049 pname = default_protos[pindex];
1050 prot = getprotobyname(pname);
1052 err(1, "getprotobyname: %s", pname);
1053 protos[pindex] = prot->p_proto;
1064 "Usage: sockstat [-46cLlu] [-j jid] [-p ports] [-P protocols]\n");
1069 main(int argc, char *argv[])
1071 int protos_defined = -1;
1075 while ((o = getopt(argc, argv, "46cj:Llp:P:uv")) != -1)
1087 opt_j = atoi(optarg);
1096 parse_ports(optarg);
1099 protos_defined = parse_protos(optarg);
1117 if ((!opt_4 && !opt_6) && protos_defined != -1)
1119 if (!opt_4 && !opt_6 && !opt_u)
1120 opt_4 = opt_6 = opt_u = 1;
1121 if ((opt_4 || opt_6) && protos_defined == -1)
1122 protos_defined = set_default_protos();
1123 if (!opt_c && !opt_l)
1126 if (opt_4 || opt_6) {
1127 for (i = 0; i < protos_defined; i++)
1128 if (protos[i] == IPPROTO_SCTP)
1131 gather_inet(protos[i]);
1134 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1135 gather_unix(SOCK_STREAM);
1136 gather_unix(SOCK_DGRAM);
1137 gather_unix(SOCK_SEQPACKET);