2 * ss.c "sockstat", socket statistics
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
24 #include <arpa/inet.h>
32 #include "libnetlink.h"
35 #include <netinet/tcp.h>
36 #include <linux/sock_diag.h>
37 #include <linux/inet_diag.h>
38 #include <linux/unix_diag.h>
40 int resolve_hosts = 0;
41 int resolve_services = 1;
42 int preferred_family = AF_UNSPEC;
56 static const char *TCP_PROTO = "tcp";
57 static const char *UDP_PROTO = "udp";
58 static const char *RAW_PROTO = "raw";
59 static const char *dg_proto = NULL;
75 #define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
76 #define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB))
77 #define ALL_DB ((1<<MAX_DB)-1)
95 #define SS_ALL ((1<<SS_MAX)-1)
107 struct filter default_filter = {
109 .states = SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
110 .families= (1<<AF_INET)|(1<<AF_INET6),
113 struct filter current_filter;
115 static FILE *generic_proc_open(const char *env, const char *name)
117 const char *p = getenv(env);
121 p = getenv("PROC_ROOT") ? : "/proc";
122 snprintf(store, sizeof(store)-1, "%s/%s", p, name);
126 return fopen(p, "r");
129 static FILE *net_tcp_open(void)
131 return generic_proc_open("PROC_NET_TCP", "net/tcp");
134 static FILE *net_tcp6_open(void)
136 return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
139 static FILE *net_udp_open(void)
141 return generic_proc_open("PROC_NET_UDP", "net/udp");
144 static FILE *net_udp6_open(void)
146 return generic_proc_open("PROC_NET_UDP6", "net/udp6");
149 static FILE *net_raw_open(void)
151 return generic_proc_open("PROC_NET_RAW", "net/raw");
154 static FILE *net_raw6_open(void)
156 return generic_proc_open("PROC_NET_RAW6", "net/raw6");
159 static FILE *net_unix_open(void)
161 return generic_proc_open("PROC_NET_UNIX", "net/unix");
164 static FILE *net_packet_open(void)
166 return generic_proc_open("PROC_NET_PACKET", "net/packet");
169 static FILE *net_netlink_open(void)
171 return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
174 static FILE *slabinfo_open(void)
176 return generic_proc_open("PROC_SLABINFO", "slabinfo");
179 static FILE *net_sockstat_open(void)
181 return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
184 static FILE *net_sockstat6_open(void)
186 return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
189 static FILE *net_snmp_open(void)
191 return generic_proc_open("PROC_NET_SNMP", "net/snmp");
194 static FILE *ephemeral_ports_open(void)
196 return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
200 struct user_ent *next;
207 #define USER_ENT_HASH_SIZE 256
208 struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
210 static int user_ent_hashfn(unsigned int ino)
212 int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
214 return val & (USER_ENT_HASH_SIZE - 1);
217 static void user_ent_add(unsigned int ino, const char *process, int pid, int fd)
219 struct user_ent *p, **pp;
222 str_len = strlen(process) + 1;
223 p = malloc(sizeof(struct user_ent) + str_len);
230 strcpy(p->process, process);
232 pp = &user_ent_hash[user_ent_hashfn(ino)];
237 static void user_ent_hash_build(void)
239 const char *root = getenv("PROC_ROOT") ? : "/proc/";
246 if (strlen(name) == 0 || name[strlen(name)-1] != '/')
249 nameoff = strlen(name);
255 while ((d = readdir(dir)) != NULL) {
262 if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
265 sprintf(name + nameoff, "%d/fd/", pid);
267 if ((dir1 = opendir(name)) == NULL)
272 while ((d1 = readdir(dir1)) != NULL) {
273 const char *pattern = "socket:[";
279 if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
282 sprintf(name+pos, "%d", fd);
284 link_len = readlink(name, lnk, sizeof(lnk)-1);
287 lnk[link_len] = '\0';
289 if (strncmp(lnk, pattern, strlen(pattern)))
292 sscanf(lnk, "socket:[%u]", &ino);
294 if (process[0] == '\0') {
298 snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid);
299 if ((fp = fopen(tmp, "r")) != NULL) {
300 int ret = fscanf(fp, "%*d (%[^)])", process);
302 fprintf(stderr, "Error: failed to read from input file stream.\n");
307 user_ent_add(ino, process, pid, fd);
314 static int find_users(unsigned ino, char *buf, int buflen)
323 p = user_ent_hash[user_ent_hashfn(ino)];
329 if (ptr - buf >= buflen - 1)
332 snprintf(ptr, buflen - (ptr - buf),
334 p->process, p->pid, p->fd);
348 /* Get stats from slab */
359 struct slabstat slabstat;
361 static const char *slabstat_ids[] =
370 static int get_slabstat(struct slabstat *s)
376 memset(s, 0, sizeof(*s));
378 fp = slabinfo_open();
382 cnt = sizeof(*s)/sizeof(int);
384 if (fgets(buf, sizeof(buf), fp) == NULL)
386 while(fgets(buf, sizeof(buf), fp) != NULL) {
388 for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
389 if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
390 sscanf(buf, "%*s%d", ((int *)s) + i);
404 static const char *sstate_name[] = {
406 [TCP_ESTABLISHED] = "ESTAB",
407 [TCP_SYN_SENT] = "SYN-SENT",
408 [TCP_SYN_RECV] = "SYN-RECV",
409 [TCP_FIN_WAIT1] = "FIN-WAIT-1",
410 [TCP_FIN_WAIT2] = "FIN-WAIT-2",
411 [TCP_TIME_WAIT] = "TIME-WAIT",
412 [TCP_CLOSE] = "UNCONN",
413 [TCP_CLOSE_WAIT] = "CLOSE-WAIT",
414 [TCP_LAST_ACK] = "LAST-ACK",
415 [TCP_LISTEN] = "LISTEN",
416 [TCP_CLOSING] = "CLOSING",
419 static const char *sstate_namel[] = {
421 [TCP_ESTABLISHED] = "established",
422 [TCP_SYN_SENT] = "syn-sent",
423 [TCP_SYN_RECV] = "syn-recv",
424 [TCP_FIN_WAIT1] = "fin-wait-1",
425 [TCP_FIN_WAIT2] = "fin-wait-2",
426 [TCP_TIME_WAIT] = "time-wait",
427 [TCP_CLOSE] = "unconnected",
428 [TCP_CLOSE_WAIT] = "close-wait",
429 [TCP_LAST_ACK] = "last-ack",
430 [TCP_LISTEN] = "listening",
431 [TCP_CLOSING] = "closing",
449 unsigned long long sk;
450 int rto, ato, qack, cwnd, ssthresh;
453 static const char *tmr_name[] = {
462 static const char *print_ms_timer(int timeout)
465 int secs, msecs, minutes;
471 msecs = timeout%1000;
475 snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
482 sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
485 sprintf(buf+strlen(buf), "%03dms", msecs);
489 static const char *print_hz_timer(int timeout)
491 int hz = get_user_hz();
492 return print_ms_timer(((timeout*1000) + hz-1)/hz);
503 struct scache *rlist;
505 static void init_service_resolver(void)
508 FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
510 if (fgets(buf, sizeof(buf), fp) == NULL)
511 fprintf(stderr, "Error: failed to read from input file stream.\n");
512 while (fgets(buf, sizeof(buf), fp) != NULL) {
513 unsigned int progn, port;
514 char proto[128], prog[128];
515 if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
516 &port, prog+4) == 4) {
517 struct scache *c = malloc(sizeof(*c));
520 memcpy(prog, "rpc.", 4);
521 c->name = strdup(prog);
522 if (strcmp(proto, TCP_PROTO) == 0)
523 c->proto = TCP_PROTO;
524 else if (strcmp(proto, UDP_PROTO) == 0)
525 c->proto = UDP_PROTO;
537 static int ip_local_port_min, ip_local_port_max;
539 /* Even do not try default linux ephemeral port ranges:
540 * default /etc/services contains so much of useless crap
541 * wouldbe "allocated" to this area that resolution
542 * is really harmful. I shrug each time when seeing
543 * "socks" or "cfinger" in dumps.
545 static int is_ephemeral(int port)
547 if (!ip_local_port_min) {
548 FILE *f = ephemeral_ports_open();
550 int ret = fscanf(f, "%d %d",
551 &ip_local_port_min, &ip_local_port_max);
553 fprintf(stderr, "Error: failed to read from input file stream.\n");
556 ip_local_port_min = 1024;
557 ip_local_port_max = 4999;
561 return (port >= ip_local_port_min && port<= ip_local_port_max);
565 static const char *__resolve_service(int port)
569 for (c = rlist; c; c = c->next) {
570 if (c->port == port && c->proto == dg_proto)
574 if (!is_ephemeral(port)) {
581 se = getservbyport(htons(port), dg_proto);
590 static const char *resolve_service(int port)
592 static char buf[128];
593 static struct scache cache[256];
601 if (resolve_services) {
602 if (dg_proto == RAW_PROTO) {
603 return inet_proto_n2a(port, buf, sizeof(buf));
607 int hash = (port^(((unsigned long)dg_proto)>>2))&255;
609 for (c = &cache[hash]; c; c = c->next) {
610 if (c->port == port &&
611 c->proto == dg_proto) {
618 if ((res = __resolve_service(port)) != NULL) {
619 if ((c = malloc(sizeof(*c))) == NULL)
630 c->name = strdup(res);
631 c->next = cache[hash].next;
632 cache[hash].next = c;
640 sprintf(buf, "%u", port);
644 static void formatted_print(const inet_prefix *a, int port)
647 const char *ap = buf;
650 est_len = addr_width;
652 if (a->family == AF_INET) {
653 if (a->data[0] == 0) {
657 ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
660 ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
661 est_len = strlen(ap);
662 if (est_len <= addr_width)
663 est_len = addr_width;
665 est_len = addr_width + ((est_len-addr_width+3)/4)*4;
667 printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
674 struct aafilter *next;
677 static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p,
680 if (!inet_addr_match(a, p, plen))
683 /* Cursed "v4 mapped" addresses: v4 mapped socket matches
684 * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
686 if (p->family == AF_INET && a->family == AF_INET6) {
687 if (a->data[0] == 0 && a->data[1] == 0 &&
688 a->data[2] == htonl(0xffff)) {
689 inet_prefix tmp = *a;
690 tmp.data[0] = a->data[3];
691 return inet_addr_match(&tmp, p, plen);
697 static int unix_match(const inet_prefix *a, const inet_prefix *p)
699 char *addr, *pattern;
700 memcpy(&addr, a->data, sizeof(addr));
701 memcpy(&pattern, p->data, sizeof(pattern));
706 return !fnmatch(pattern, addr, 0);
709 static int run_ssfilter(struct ssfilter *f, struct tcpstat *s)
714 static int low, high=65535;
716 if (s->local.family == AF_UNIX) {
718 memcpy(&p, s->local.data, sizeof(p));
719 return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
720 strspn(p+1, "0123456789abcdef") == 5);
722 if (s->local.family == AF_PACKET)
723 return s->lport == 0 && s->local.data == 0;
724 if (s->local.family == AF_NETLINK)
728 FILE *fp = ephemeral_ports_open();
730 int ret = fscanf(fp, "%d%d", &low, &high);
732 fprintf(stderr, "Error: failed to read from input file stream.\n");
736 return s->lport >= low && s->lport <= high;
740 struct aafilter *a = (void*)f->pred;
741 if (a->addr.family == AF_UNIX)
742 return unix_match(&s->remote, &a->addr);
743 if (a->port != -1 && a->port != s->rport)
745 if (a->addr.bitlen) {
747 if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
749 } while ((a = a->next) != NULL);
756 struct aafilter *a = (void*)f->pred;
757 if (a->addr.family == AF_UNIX)
758 return unix_match(&s->local, &a->addr);
759 if (a->port != -1 && a->port != s->lport)
761 if (a->addr.bitlen) {
763 if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
765 } while ((a = a->next) != NULL);
772 struct aafilter *a = (void*)f->pred;
773 return s->rport >= a->port;
777 struct aafilter *a = (void*)f->pred;
778 return s->rport <= a->port;
782 struct aafilter *a = (void*)f->pred;
783 return s->lport >= a->port;
787 struct aafilter *a = (void*)f->pred;
788 return s->lport <= a->port;
791 /* Yup. It is recursion. Sorry. */
793 return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
795 return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
797 return !run_ssfilter(f->pred, s);
803 /* Relocate external jumps by reloc. */
804 static void ssfilter_patch(char *a, int len, int reloc)
807 struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a;
817 static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
822 if (!(*bytecode=malloc(4))) abort();
823 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
829 struct aafilter *a = (void*)f->pred;
832 int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
835 for (b=a; b; b=b->next) {
836 len += 4 + sizeof(struct inet_diag_hostcond);
837 if (a->addr.family == AF_INET6)
844 if (!(ptr = malloc(len))) abort();
846 for (b=a; b; b=b->next) {
847 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
848 int alen = (a->addr.family == AF_INET6 ? 16 : 4);
849 int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
850 struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4);
852 *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
853 cond->family = a->addr.family;
854 cond->port = a->port;
855 cond->prefix_len = a->addr.bitlen;
856 memcpy(cond->addr, a->addr.data, alen);
859 op = (struct inet_diag_bc_op *)ptr;
860 *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
864 return ptr - *bytecode;
868 struct aafilter *x = (void*)f->pred;
869 if (!(*bytecode=malloc(8))) abort();
870 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
871 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
876 struct aafilter *x = (void*)f->pred;
877 if (!(*bytecode=malloc(8))) abort();
878 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
879 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
884 struct aafilter *x = (void*)f->pred;
885 if (!(*bytecode=malloc(8))) abort();
886 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
887 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
892 struct aafilter *x = (void*)f->pred;
893 if (!(*bytecode=malloc(8))) abort();
894 ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
895 ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
901 char *a1, *a2, *a, l1, l2;
902 l1 = ssfilter_bytecompile(f->pred, &a1);
903 l2 = ssfilter_bytecompile(f->post, &a2);
904 if (!(a = malloc(l1+l2))) abort();
906 memcpy(a+l1, a2, l2);
908 ssfilter_patch(a, l1, l2);
914 char *a1, *a2, *a, l1, l2;
915 l1 = ssfilter_bytecompile(f->pred, &a1);
916 l2 = ssfilter_bytecompile(f->post, &a2);
917 if (!(a = malloc(l1+l2+4))) abort();
919 memcpy(a+l1+4, a2, l2);
921 *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
928 l1 = ssfilter_bytecompile(f->pred, &a1);
929 if (!(a = malloc(l1+4))) abort();
932 *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
941 static int remember_he(struct aafilter *a, struct hostent *he)
943 char **ptr = he->h_addr_list;
947 if (he->h_addrtype == AF_INET)
949 else if (he->h_addrtype == AF_INET6)
955 struct aafilter *b = a;
956 if (a->addr.bitlen) {
957 if ((b = malloc(sizeof(*b))) == NULL)
963 memcpy(b->addr.data, *ptr, len);
964 b->addr.bytelen = len;
965 b->addr.bitlen = len*8;
966 b->addr.family = he->h_addrtype;
973 static int get_dns_host(struct aafilter *a, const char *addr, int fam)
984 he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
986 cnt = remember_he(a, he);
987 if (fam == AF_UNSPEC) {
988 he = gethostbyname2(addr, AF_INET6);
990 cnt += remember_he(a, he);
995 static int xll_initted = 0;
997 static void xll_init(void)
999 struct rtnl_handle rth;
1006 static const char *xll_index_to_name(int index)
1010 return ll_index_to_name(index);
1013 static int xll_name_to_index(const char *dev)
1017 return ll_name_to_index(dev);
1020 void *parse_hostcond(char *addr)
1024 struct aafilter *res;
1025 int fam = preferred_family;
1027 memset(&a, 0, sizeof(a));
1030 if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
1032 a.addr.family = AF_UNIX;
1033 if (strncmp(addr, "unix:", 5) == 0)
1036 a.addr.bitlen = 8*strlen(p);
1037 memcpy(a.addr.data, &p, sizeof(p));
1041 if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
1042 a.addr.family = AF_PACKET;
1044 if (strncmp(addr, "link:", 5) == 0)
1046 port = strchr(addr, ':');
1049 if (port[1] && strcmp(port+1, "*")) {
1050 if (get_integer(&a.port, port+1, 0)) {
1051 if ((a.port = xll_name_to_index(port+1)) <= 0)
1056 if (addr[0] && strcmp(addr, "*")) {
1059 if (ll_proto_a2n(&tmp, addr))
1061 a.addr.data[0] = ntohs(tmp);
1066 if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
1067 a.addr.family = AF_NETLINK;
1069 if (strncmp(addr, "netlink:", 8) == 0)
1071 port = strchr(addr, ':');
1074 if (port[1] && strcmp(port+1, "*")) {
1075 if (get_integer(&a.port, port+1, 0)) {
1076 if (strcmp(port+1, "kernel") == 0)
1083 if (addr[0] && strcmp(addr, "*")) {
1085 if (get_u32(a.addr.data, addr, 0)) {
1086 if (strcmp(addr, "rtnl") == 0)
1088 else if (strcmp(addr, "fw") == 0)
1090 else if (strcmp(addr, "tcpdiag") == 0)
1099 if (strncmp(addr, "inet:", 5) == 0) {
1102 } else if (strncmp(addr, "inet6:", 6) == 0) {
1107 /* URL-like literal [] */
1108 if (addr[0] == '[') {
1110 if ((port = strchr(addr, ']')) == NULL)
1113 } else if (addr[0] == '*') {
1116 port = strrchr(strchr(addr, '/') ? : addr, ':');
1118 if (port && *port) {
1122 if (*port && *port != '*') {
1123 if (get_integer(&a.port, port, 0)) {
1124 struct servent *se1 = NULL;
1125 struct servent *se2 = NULL;
1126 if (current_filter.dbs&(1<<UDP_DB))
1127 se1 = getservbyname(port, UDP_PROTO);
1128 if (current_filter.dbs&(1<<TCP_DB))
1129 se2 = getservbyname(port, TCP_PROTO);
1130 if (se1 && se2 && se1->s_port != se2->s_port) {
1131 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1137 a.port = ntohs(se1->s_port);
1140 for (s = rlist; s; s = s->next) {
1141 if ((s->proto == UDP_PROTO &&
1142 (current_filter.dbs&(1<<UDP_DB))) ||
1143 (s->proto == TCP_PROTO &&
1144 (current_filter.dbs&(1<<TCP_DB)))) {
1145 if (s->name && strcmp(s->name, port) == 0) {
1146 if (a.port > 0 && a.port != s->port) {
1147 fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
1155 fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
1162 if (addr && *addr && *addr != '*') {
1163 if (get_prefix_1(&a.addr, addr, fam)) {
1164 if (get_dns_host(&a, addr, fam)) {
1165 fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
1172 res = malloc(sizeof(*res));
1174 memcpy(res, &a, sizeof(a));
1178 static int tcp_show_line(char *line, const struct filter *f, int family)
1181 char *loc, *rem, *data;
1186 if ((p = strchr(line, ':')) == NULL)
1190 if ((p = strchr(loc, ':')) == NULL)
1195 if ((p = strchr(rem, ':')) == NULL)
1201 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1203 if (!(f->states & (1<<state)))
1207 s.local.family = s.remote.family = family;
1208 if (family == AF_INET) {
1209 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1210 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1211 s.local.bytelen = s.remote.bytelen = 4;
1213 sscanf(loc, "%08x%08x%08x%08x:%x",
1219 sscanf(rem, "%08x%08x%08x%08x:%x",
1225 s.local.bytelen = s.remote.bytelen = 16;
1228 if (f->f && run_ssfilter(f->f, &s) == 0)
1232 n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
1233 &s.state, &s.wq, &s.rq,
1234 &s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino,
1235 &s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack,
1236 &s.cwnd, &s.ssthresh, opt);
1249 printf("%-*s ", netid_width, "tcp");
1251 printf("%-*s ", state_width, sstate_name[s.state]);
1253 printf("%-6d %-6d ", s.rq, s.wq);
1255 formatted_print(&s.local, s.lport);
1256 formatted_print(&s.remote, s.rport);
1262 printf(" timer:(%s,%s,%d)",
1264 print_hz_timer(s.timeout),
1265 s.timer != 1 ? s.probes : s.retrs);
1269 int hz = get_user_hz();
1270 if (s.rto && s.rto != 3*hz)
1271 printf(" rto:%g", (double)s.rto/hz);
1273 printf(" ato:%g", (double)s.ato/hz);
1275 printf(" cwnd:%d", s.cwnd);
1276 if (s.ssthresh != -1)
1277 printf(" ssthresh:%d", s.ssthresh);
1279 printf(" qack:%d", s.qack/2);
1285 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1286 printf(" users:(%s)", ubuf);
1290 printf(" uid:%u", (unsigned)s.uid);
1291 printf(" ino:%u", s.ino);
1292 printf(" sk:%llx", s.sk);
1294 printf(" opt:\"%s\"", opt);
1301 static int generic_record_read(FILE *fp,
1302 int (*worker)(char*, const struct filter *, int),
1303 const struct filter *f, int fam)
1308 if (fgets(line, sizeof(line), fp) == NULL)
1311 while (fgets(line, sizeof(line), fp) != NULL) {
1312 int n = strlen(line);
1313 if (n == 0 || line[n-1] != '\n') {
1319 if (worker(line, f, fam) < 0)
1324 return ferror(fp) ? -1 : 0;
1327 static char *sprint_bw(char *buf, double bw)
1330 sprintf(buf,"%.1fM", bw / 1000000.);
1331 else if (bw > 1000.)
1332 sprintf(buf,"%.1fK", bw / 1000.);
1334 sprintf(buf, "%g", bw);
1339 static void print_skmeminfo(struct rtattr *tb[], int attrtype)
1341 const __u32 *skmeminfo;
1344 skmeminfo = RTA_DATA(tb[attrtype]);
1346 printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u",
1347 skmeminfo[SK_MEMINFO_RMEM_ALLOC],
1348 skmeminfo[SK_MEMINFO_RCVBUF],
1349 skmeminfo[SK_MEMINFO_WMEM_ALLOC],
1350 skmeminfo[SK_MEMINFO_SNDBUF],
1351 skmeminfo[SK_MEMINFO_FWD_ALLOC],
1352 skmeminfo[SK_MEMINFO_WMEM_QUEUED],
1353 skmeminfo[SK_MEMINFO_OPTMEM]);
1355 if (RTA_PAYLOAD(tb[attrtype]) >=
1356 (SK_MEMINFO_BACKLOG + 1) * sizeof(__u32))
1357 printf(",bl%u", skmeminfo[SK_MEMINFO_BACKLOG]);
1362 static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r)
1364 struct rtattr * tb[INET_DIAG_MAX+1];
1368 parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1),
1369 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
1371 if (tb[INET_DIAG_SKMEMINFO]) {
1372 print_skmeminfo(tb, INET_DIAG_SKMEMINFO);
1373 } else if (tb[INET_DIAG_MEMINFO]) {
1374 const struct inet_diag_meminfo *minfo
1375 = RTA_DATA(tb[INET_DIAG_MEMINFO]);
1376 printf(" mem:(r%u,w%u,f%u,t%u)",
1383 if (tb[INET_DIAG_INFO]) {
1384 struct tcp_info *info;
1385 int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
1387 /* workaround for older kernels with less fields */
1388 if (len < sizeof(*info)) {
1389 info = alloca(sizeof(*info));
1390 memset(info, 0, sizeof(*info));
1391 memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
1393 info = RTA_DATA(tb[INET_DIAG_INFO]);
1396 if (info->tcpi_options & TCPI_OPT_TIMESTAMPS)
1398 if (info->tcpi_options & TCPI_OPT_SACK)
1400 if (info->tcpi_options & TCPI_OPT_ECN)
1402 if (info->tcpi_options & TCPI_OPT_ECN_SEEN)
1406 if (tb[INET_DIAG_CONG])
1407 printf(" %s", rta_getattr_str(tb[INET_DIAG_CONG]));
1409 if (info->tcpi_options & TCPI_OPT_WSCALE)
1410 printf(" wscale:%d,%d", info->tcpi_snd_wscale,
1411 info->tcpi_rcv_wscale);
1412 if (info->tcpi_rto && info->tcpi_rto != 3000000)
1413 printf(" rto:%g", (double)info->tcpi_rto/1000);
1415 printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000,
1416 (double)info->tcpi_rttvar/1000);
1418 printf(" ato:%g", (double)info->tcpi_ato/1000);
1419 if (info->tcpi_snd_mss)
1420 printf(" mss:%d", info->tcpi_snd_mss);
1421 if (info->tcpi_snd_cwnd != 2)
1422 printf(" cwnd:%d", info->tcpi_snd_cwnd);
1423 if (info->tcpi_snd_ssthresh < 0xFFFF)
1424 printf(" ssthresh:%d", info->tcpi_snd_ssthresh);
1426 rtt = (double) info->tcpi_rtt;
1427 if (tb[INET_DIAG_VEGASINFO]) {
1428 const struct tcpvegas_info *vinfo
1429 = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
1431 if (vinfo->tcpv_enabled &&
1432 vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
1433 rtt = vinfo->tcpv_rtt;
1436 if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
1437 printf(" send %sbps",
1438 sprint_bw(b1, (double) info->tcpi_snd_cwnd *
1439 (double) info->tcpi_snd_mss * 8000000.
1443 if (info->tcpi_rcv_rtt)
1444 printf(" rcv_rtt:%g", (double) info->tcpi_rcv_rtt/1000);
1445 if (info->tcpi_rcv_space)
1446 printf(" rcv_space:%d", info->tcpi_rcv_space);
1451 static int inet_show_sock(struct nlmsghdr *nlh, struct filter *f)
1453 struct inet_diag_msg *r = NLMSG_DATA(nlh);
1456 s.state = r->idiag_state;
1457 s.local.family = s.remote.family = r->idiag_family;
1458 s.lport = ntohs(r->id.idiag_sport);
1459 s.rport = ntohs(r->id.idiag_dport);
1460 if (s.local.family == AF_INET) {
1461 s.local.bytelen = s.remote.bytelen = 4;
1463 s.local.bytelen = s.remote.bytelen = 16;
1465 memcpy(s.local.data, r->id.idiag_src, s.local.bytelen);
1466 memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen);
1468 if (f && f->f && run_ssfilter(f->f, &s) == 0)
1472 printf("%-*s ", netid_width, "tcp");
1474 printf("%-*s ", state_width, sstate_name[s.state]);
1476 printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue);
1478 formatted_print(&s.local, s.lport);
1479 formatted_print(&s.remote, s.rport);
1482 if (r->idiag_timer) {
1483 if (r->idiag_timer > 4)
1485 printf(" timer:(%s,%s,%d)",
1486 tmr_name[r->idiag_timer],
1487 print_ms_timer(r->idiag_expires),
1493 if (find_users(r->idiag_inode, ubuf, sizeof(ubuf)) > 0)
1494 printf(" users:(%s)", ubuf);
1498 printf(" uid:%u", (unsigned)r->idiag_uid);
1499 printf(" ino:%u", r->idiag_inode);
1501 if (r->id.idiag_cookie[1] != 0)
1502 printf("%08x", r->id.idiag_cookie[1]);
1503 printf("%08x", r->id.idiag_cookie[0]);
1505 if (show_mem || show_tcpinfo) {
1507 tcp_show_info(nlh, r);
1515 static int tcpdiag_send(int fd, int protocol, struct filter *f)
1517 struct sockaddr_nl nladdr;
1519 struct nlmsghdr nlh;
1520 struct inet_diag_req r;
1526 struct iovec iov[3];
1528 if (protocol == IPPROTO_UDP)
1531 memset(&nladdr, 0, sizeof(nladdr));
1532 nladdr.nl_family = AF_NETLINK;
1534 req.nlh.nlmsg_len = sizeof(req);
1535 if (protocol == IPPROTO_TCP)
1536 req.nlh.nlmsg_type = TCPDIAG_GETSOCK;
1538 req.nlh.nlmsg_type = DCCPDIAG_GETSOCK;
1539 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
1540 req.nlh.nlmsg_pid = 0;
1541 req.nlh.nlmsg_seq = 123456;
1542 memset(&req.r, 0, sizeof(req.r));
1543 req.r.idiag_family = AF_INET;
1544 req.r.idiag_states = f->states;
1546 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
1547 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
1551 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
1552 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
1553 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
1556 iov[0] = (struct iovec){
1558 .iov_len = sizeof(req)
1561 bclen = ssfilter_bytecompile(f->f, &bc);
1562 rta.rta_type = INET_DIAG_REQ_BYTECODE;
1563 rta.rta_len = RTA_LENGTH(bclen);
1564 iov[1] = (struct iovec){ &rta, sizeof(rta) };
1565 iov[2] = (struct iovec){ bc, bclen };
1566 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
1569 msg = (struct msghdr) {
1570 .msg_name = (void*)&nladdr,
1571 .msg_namelen = sizeof(nladdr),
1573 .msg_iovlen = f->f ? 3 : 1,
1576 if (sendmsg(fd, &msg, 0) < 0) {
1584 static int sockdiag_send(int family, int fd, int protocol, struct filter *f)
1586 struct sockaddr_nl nladdr;
1588 struct nlmsghdr nlh;
1589 struct inet_diag_req_v2 r;
1595 struct iovec iov[3];
1597 if (family == PF_UNSPEC)
1598 return tcpdiag_send(fd, protocol, f);
1600 memset(&nladdr, 0, sizeof(nladdr));
1601 nladdr.nl_family = AF_NETLINK;
1603 req.nlh.nlmsg_len = sizeof(req);
1604 req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
1605 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
1606 req.nlh.nlmsg_pid = 0;
1607 req.nlh.nlmsg_seq = 123456;
1608 memset(&req.r, 0, sizeof(req.r));
1609 req.r.sdiag_family = family;
1610 req.r.sdiag_protocol = protocol;
1611 req.r.idiag_states = f->states;
1613 req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
1614 req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
1618 req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
1619 req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
1620 req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
1623 iov[0] = (struct iovec){
1625 .iov_len = sizeof(req)
1628 bclen = ssfilter_bytecompile(f->f, &bc);
1629 rta.rta_type = INET_DIAG_REQ_BYTECODE;
1630 rta.rta_len = RTA_LENGTH(bclen);
1631 iov[1] = (struct iovec){ &rta, sizeof(rta) };
1632 iov[2] = (struct iovec){ bc, bclen };
1633 req.nlh.nlmsg_len += RTA_LENGTH(bclen);
1636 msg = (struct msghdr) {
1637 .msg_name = (void*)&nladdr,
1638 .msg_namelen = sizeof(nladdr),
1640 .msg_iovlen = f->f ? 3 : 1,
1643 if (sendmsg(fd, &msg, 0) < 0) {
1651 static int inet_show_netlink(struct filter *f, FILE *dump_fp, int protocol)
1654 struct sockaddr_nl nladdr;
1657 struct iovec iov[3];
1659 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
1664 if (sockdiag_send(family, fd, protocol, f))
1667 memset(&nladdr, 0, sizeof(nladdr));
1668 nladdr.nl_family = AF_NETLINK;
1670 iov[0] = (struct iovec){
1672 .iov_len = sizeof(buf)
1679 msg = (struct msghdr) {
1680 (void*)&nladdr, sizeof(nladdr),
1686 status = recvmsg(fd, &msg, 0);
1695 fprintf(stderr, "EOF on netlink\n");
1701 fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
1703 h = (struct nlmsghdr*)buf;
1704 while (NLMSG_OK(h, status)) {
1706 struct inet_diag_msg *r = NLMSG_DATA(h);
1708 if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
1709 h->nlmsg_seq != 123456)
1712 if (h->nlmsg_type == NLMSG_DONE)
1715 if (h->nlmsg_type == NLMSG_ERROR) {
1716 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1717 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1718 fprintf(stderr, "ERROR truncated\n");
1720 if (family != PF_UNSPEC) {
1725 errno = -err->error;
1726 if (errno == EOPNOTSUPP) {
1730 perror("TCPDIAG answers");
1736 if (!(f->families & (1<<r->idiag_family))) {
1737 h = NLMSG_NEXT(h, status);
1740 err = inet_show_sock(h, NULL);
1748 h = NLMSG_NEXT(h, status);
1750 if (msg.msg_flags & MSG_TRUNC) {
1751 fprintf(stderr, "Message truncated\n");
1755 fprintf(stderr, "!!!Remnant of size %d\n", status);
1760 if (family == PF_INET) {
1769 static int tcp_show_netlink_file(struct filter *f)
1774 if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
1775 perror("fopen($TCPDIAG_FILE)");
1781 struct nlmsghdr *h = (struct nlmsghdr*)buf;
1783 status = fread(buf, 1, sizeof(*h), fp);
1785 perror("Reading header from $TCPDIAG_FILE");
1788 if (status != sizeof(*h)) {
1789 perror("Unexpected EOF reading $TCPDIAG_FILE");
1793 status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
1796 perror("Reading $TCPDIAG_FILE");
1799 if (status + sizeof(*h) < h->nlmsg_len) {
1800 perror("Unexpected EOF reading $TCPDIAG_FILE");
1804 /* The only legal exit point */
1805 if (h->nlmsg_type == NLMSG_DONE)
1808 if (h->nlmsg_type == NLMSG_ERROR) {
1809 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
1810 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
1811 fprintf(stderr, "ERROR truncated\n");
1813 errno = -err->error;
1814 perror("TCPDIAG answered");
1819 err = inet_show_sock(h, f);
1825 static int tcp_show(struct filter *f, int socktype)
1829 int bufsize = 64*1024;
1831 dg_proto = TCP_PROTO;
1833 if (getenv("TCPDIAG_FILE"))
1834 return tcp_show_netlink_file(f);
1836 if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
1837 && inet_show_netlink(f, NULL, socktype) == 0)
1840 /* Sigh... We have to parse /proc/net/tcp... */
1843 /* Estimate amount of sockets and try to allocate
1844 * huge buffer to read all the table at one read.
1845 * Limit it by 16MB though. The assumption is: as soon as
1846 * kernel was able to hold information about N connections,
1847 * it is able to give us some memory for snapshot.
1850 int guess = slabstat.socks+slabstat.tcp_syns;
1851 if (f->states&(1<<SS_TIME_WAIT))
1852 guess += slabstat.tcp_tws;
1853 if (guess > (16*1024*1024)/128)
1854 guess = (16*1024*1024)/128;
1856 if (guess > bufsize)
1859 while (bufsize >= 64*1024) {
1860 if ((buf = malloc(bufsize)) != NULL)
1869 if (f->families & (1<<AF_INET)) {
1870 if ((fp = net_tcp_open()) == NULL)
1873 setbuffer(fp, buf, bufsize);
1874 if (generic_record_read(fp, tcp_show_line, f, AF_INET))
1879 if ((f->families & (1<<AF_INET6)) &&
1880 (fp = net_tcp6_open()) != NULL) {
1881 setbuffer(fp, buf, bufsize);
1882 if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
1892 int saved_errno = errno;
1897 errno = saved_errno;
1903 static int dgram_show_line(char *line, const struct filter *f, int family)
1906 char *loc, *rem, *data;
1911 if ((p = strchr(line, ':')) == NULL)
1915 if ((p = strchr(loc, ':')) == NULL)
1920 if ((p = strchr(rem, ':')) == NULL)
1926 int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
1928 if (!(f->states & (1<<state)))
1932 s.local.family = s.remote.family = family;
1933 if (family == AF_INET) {
1934 sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
1935 sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
1936 s.local.bytelen = s.remote.bytelen = 4;
1938 sscanf(loc, "%08x%08x%08x%08x:%x",
1944 sscanf(rem, "%08x%08x%08x%08x:%x",
1950 s.local.bytelen = s.remote.bytelen = 16;
1953 if (f->f && run_ssfilter(f->f, &s) == 0)
1957 n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
1958 &s.state, &s.wq, &s.rq,
1960 &s.refcnt, &s.sk, opt);
1966 printf("%-*s ", netid_width, dg_proto);
1968 printf("%-*s ", state_width, sstate_name[s.state]);
1970 printf("%-6d %-6d ", s.rq, s.wq);
1972 formatted_print(&s.local, s.lport);
1973 formatted_print(&s.remote, s.rport);
1977 if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
1978 printf(" users:(%s)", ubuf);
1983 printf(" uid=%u", (unsigned)s.uid);
1984 printf(" ino=%u", s.ino);
1985 printf(" sk=%llx", s.sk);
1987 printf(" opt:\"%s\"", opt);
1995 static int udp_show(struct filter *f)
1999 if (!getenv("PROC_NET_UDP") && !getenv("PROC_ROOT")
2000 && inet_show_netlink(f, NULL, IPPROTO_UDP) == 0)
2003 dg_proto = UDP_PROTO;
2005 if (f->families&(1<<AF_INET)) {
2006 if ((fp = net_udp_open()) == NULL)
2008 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
2013 if ((f->families&(1<<AF_INET6)) &&
2014 (fp = net_udp6_open()) != NULL) {
2015 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
2023 int saved_errno = errno;
2026 errno = saved_errno;
2031 static int raw_show(struct filter *f)
2035 dg_proto = RAW_PROTO;
2037 if (f->families&(1<<AF_INET)) {
2038 if ((fp = net_raw_open()) == NULL)
2040 if (generic_record_read(fp, dgram_show_line, f, AF_INET))
2045 if ((f->families&(1<<AF_INET6)) &&
2046 (fp = net_raw6_open()) != NULL) {
2047 if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
2055 int saved_errno = errno;
2058 errno = saved_errno;
2066 struct unixstat *next;
2078 int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
2079 SS_ESTABLISHED, SS_CLOSING };
2082 #define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat))
2084 static void unix_list_free(struct unixstat *list)
2087 struct unixstat *s = list;
2095 static void unix_list_print(struct unixstat *list, struct filter *f)
2100 for (s = list; s; s = s->next) {
2101 if (!(f->states & (1<<s->state)))
2103 if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
2105 if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
2111 for (p = list; p; p = p->next) {
2112 if (s->peer == p->ino)
2118 peer = p->name ? : "*";
2124 tst.local.family = AF_UNIX;
2125 tst.remote.family = AF_UNIX;
2126 memcpy(tst.local.data, &s->name, sizeof(s->name));
2127 if (strcmp(peer, "*") == 0)
2128 memset(tst.remote.data, 0, sizeof(peer));
2130 memcpy(tst.remote.data, &peer, sizeof(peer));
2131 if (run_ssfilter(f->f, &tst) == 0)
2136 printf("%-*s ", netid_width,
2137 s->type == SOCK_STREAM ? "u_str" : "u_dgr");
2139 printf("%-*s ", state_width, sstate_name[s->state]);
2140 printf("%-6d %-6d ", s->rq, s->wq);
2141 printf("%*s %-*d %*s %-*d",
2142 addr_width, s->name ? : "*", serv_width, s->ino,
2143 addr_width, peer, serv_width, s->peer);
2146 if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0)
2147 printf(" users:(%s)", ubuf);
2153 static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f)
2155 struct unix_diag_msg *r = NLMSG_DATA(nlh);
2156 struct rtattr *tb[UNIX_DIAG_MAX+1];
2161 parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
2162 nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
2165 printf("%-*s ", netid_width,
2166 r->udiag_type == SOCK_STREAM ? "u_str" : "u_dgr");
2168 printf("%-*s ", state_width, sstate_name[r->udiag_state]);
2170 if (tb[UNIX_DIAG_RQLEN]) {
2171 struct unix_diag_rqlen *rql = RTA_DATA(tb[UNIX_DIAG_RQLEN]);
2172 rqlen = rql->udiag_rqueue;
2173 wqlen = rql->udiag_wqueue;
2179 printf("%-6u %-6u ", rqlen, wqlen);
2181 if (tb[UNIX_DIAG_NAME]) {
2182 int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
2184 memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
2186 if (name[0] == '\0')
2191 if (tb[UNIX_DIAG_PEER])
2192 peer_ino = rta_getattr_u32(tb[UNIX_DIAG_PEER]);
2196 printf("%*s %-*d %*s %-*d",
2198 serv_width, r->udiag_ino,
2199 addr_width, "*", /* FIXME */
2200 serv_width, peer_ino);
2204 if (find_users(r->udiag_ino, ubuf, sizeof(ubuf)) > 0)
2205 printf(" users:(%s)", ubuf);
2210 print_skmeminfo(tb, UNIX_DIAG_MEMINFO);
2218 static int unix_show_netlink(struct filter *f, FILE *dump_fp)
2222 struct nlmsghdr nlh;
2223 struct unix_diag_req r;
2227 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
2230 memset(&req, 0, sizeof(req));
2231 req.nlh.nlmsg_len = sizeof(req);
2232 req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
2233 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
2234 req.nlh.nlmsg_seq = 123456;
2236 req.r.sdiag_family = AF_UNIX;
2237 req.r.udiag_states = f->states;
2238 req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
2240 req.r.udiag_show |= UDIAG_SHOW_MEMINFO;
2242 if (send(fd, &req, sizeof(req), 0) < 0) {
2250 struct sockaddr_nl nladdr;
2251 socklen_t slen = sizeof(nladdr);
2253 status = recvfrom(fd, buf, sizeof(buf), 0,
2254 (struct sockaddr *) &nladdr, &slen);
2262 fprintf(stderr, "EOF on netlink\n");
2267 fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
2269 h = (struct nlmsghdr*)buf;
2270 while (NLMSG_OK(h, status)) {
2273 if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
2274 h->nlmsg_seq != 123456)
2277 if (h->nlmsg_type == NLMSG_DONE)
2280 if (h->nlmsg_type == NLMSG_ERROR) {
2281 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
2282 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
2283 fprintf(stderr, "ERROR truncated\n");
2285 errno = -err->error;
2286 if (errno != ENOENT)
2287 fprintf(stderr, "UDIAG answers %d\n", errno);
2293 err = unix_show_sock(h, f);
2301 h = NLMSG_NEXT(h, status);
2305 fprintf(stderr, "!!!Remnant of size %zd\n", status);
2315 static int unix_show(struct filter *f)
2322 struct unixstat *list = NULL;
2324 if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
2325 && unix_show_netlink(f, NULL) == 0)
2328 if ((fp = net_unix_open()) == NULL)
2330 if (fgets(buf, sizeof(buf)-1, fp) == NULL)
2333 if (memcmp(buf, "Peer", 4) == 0)
2337 while (fgets(buf, sizeof(buf)-1, fp)) {
2338 struct unixstat *u, **insp;
2341 if (!(u = malloc(sizeof(*u))))
2345 if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
2346 &u->peer, &u->rq, &u->wq, &flags, &u->type,
2347 &u->state, &u->ino, name) < 8)
2350 if (flags&(1<<16)) {
2351 u->state = SS_LISTEN;
2353 u->state = unix_state_map[u->state-1];
2354 if (u->type == SOCK_DGRAM &&
2355 u->state == SS_CLOSE &&
2357 u->state = SS_ESTABLISHED;
2368 if (u->type < (*insp)->type ||
2369 (u->type == (*insp)->type &&
2370 u->ino < (*insp)->ino))
2372 insp = &(*insp)->next;
2378 if ((u->name = malloc(strlen(name)+1)) == NULL)
2380 strcpy(u->name, name);
2382 if (++cnt > MAX_UNIX_REMEMBER) {
2383 unix_list_print(list, f);
2384 unix_list_free(list);
2391 unix_list_print(list, f);
2392 unix_list_free(list);
2401 static int packet_show(struct filter *f)
2412 unsigned long long sk;
2414 if (!(f->states & (1<<SS_CLOSE)))
2417 if ((fp = net_packet_open()) == NULL)
2419 if (fgets(buf, sizeof(buf)-1, fp) == NULL)
2420 fprintf(stderr, "Error: failed to read from input file stream.\n");
2422 while (fgets(buf, sizeof(buf)-1, fp)) {
2423 sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
2425 &type, &prot, &iface, &state,
2428 if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
2430 if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
2434 tst.local.family = AF_PACKET;
2435 tst.remote.family = AF_PACKET;
2438 tst.local.data[0] = prot;
2439 tst.remote.data[0] = 0;
2440 if (run_ssfilter(f->f, &tst) == 0)
2445 printf("%-*s ", netid_width,
2446 type == SOCK_RAW ? "p_raw" : "p_dgr");
2448 printf("%-*s ", state_width, "UNCONN");
2449 printf("%-6d %-6d ", rq, 0);
2451 printf("%*s:", addr_width, "*");
2454 printf("%*s:", addr_width,
2455 ll_proto_n2a(htons(prot), tb, sizeof(tb)));
2458 printf("%-*s ", serv_width, "*");
2460 printf("%-*s ", serv_width, xll_index_to_name(iface));
2463 addr_width, "", serv_width, "");
2467 if (find_users(ino, ubuf, sizeof(ubuf)) > 0)
2468 printf(" users:(%s)", ubuf);
2471 printf(" ino=%u uid=%u sk=%llx", ino, uid, sk);
2479 static int netlink_show(struct filter *f)
2486 unsigned long long sk, cb;
2488 if (!(f->states & (1<<SS_CLOSE)))
2491 if ((fp = net_netlink_open()) == NULL)
2493 if (fgets(buf, sizeof(buf)-1, fp) == NULL)
2494 fprintf(stderr, "Error: failed to read from input file stream.\n");
2496 while (fgets(buf, sizeof(buf)-1, fp)) {
2497 sscanf(buf, "%llx %d %d %x %d %d %llx %d",
2499 &prot, &pid, &groups, &rq, &wq, &cb, &rc);
2503 tst.local.family = AF_NETLINK;
2504 tst.remote.family = AF_NETLINK;
2507 tst.local.data[0] = prot;
2508 tst.remote.data[0] = 0;
2509 if (run_ssfilter(f->f, &tst) == 0)
2514 printf("%-*s ", netid_width, "nl");
2516 printf("%-*s ", state_width, "UNCONN");
2517 printf("%-6d %-6d ", rq, wq);
2518 if (resolve_services && prot == 0)
2519 printf("%*s:", addr_width, "rtnl");
2520 else if (resolve_services && prot == 3)
2521 printf("%*s:", addr_width, "fw");
2522 else if (resolve_services && prot == 4)
2523 printf("%*s:", addr_width, "tcpdiag");
2525 printf("%*d:", addr_width, prot);
2527 printf("%-*s ", serv_width, "*");
2528 } else if (resolve_services) {
2532 printf("%-*s ", serv_width, "kernel");
2533 } else if (pid > 0) {
2536 sprintf(procname, "%s/%d/stat",
2537 getenv("PROC_ROOT") ? : "/proc", pid);
2538 if ((fp = fopen(procname, "r")) != NULL) {
2539 if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
2540 sprintf(procname+strlen(procname), "/%d", pid);
2541 printf("%-*s ", serv_width, procname);
2548 printf("%-*d ", serv_width, pid);
2550 printf("%-*d ", serv_width, pid);
2553 addr_width, "", serv_width, "");
2556 printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
2569 static int get_snmp_int(char *proto, char *key, int *result)
2573 int protolen = strlen(proto);
2574 int keylen = strlen(key);
2578 if ((fp = net_snmp_open()) == NULL)
2581 while (fgets(buf, sizeof(buf), fp) != NULL) {
2584 if (memcmp(buf, proto, protolen))
2586 while ((p = strchr(p, ' ')) != NULL) {
2589 if (memcmp(p, key, keylen) == 0 &&
2590 (p[keylen] == ' ' || p[keylen] == '\n'))
2593 if (fgets(buf, sizeof(buf), fp) == NULL)
2595 if (memcmp(buf, proto, protolen))
2598 while ((p = strchr(p, ' ')) != NULL) {
2601 sscanf(p, "%d", result);
2614 /* Get stats from sockstat */
2635 static void get_sockstat_line(char *line, struct sockstat *s)
2637 char id[256], rem[256];
2639 if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
2642 if (strcmp(id, "sockets:") == 0)
2643 sscanf(rem, "%*s%d", &s->socks);
2644 else if (strcmp(id, "UDP:") == 0)
2645 sscanf(rem, "%*s%d", &s->udp4);
2646 else if (strcmp(id, "UDP6:") == 0)
2647 sscanf(rem, "%*s%d", &s->udp6);
2648 else if (strcmp(id, "RAW:") == 0)
2649 sscanf(rem, "%*s%d", &s->raw4);
2650 else if (strcmp(id, "RAW6:") == 0)
2651 sscanf(rem, "%*s%d", &s->raw6);
2652 else if (strcmp(id, "TCP6:") == 0)
2653 sscanf(rem, "%*s%d", &s->tcp6_hashed);
2654 else if (strcmp(id, "FRAG:") == 0)
2655 sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
2656 else if (strcmp(id, "FRAG6:") == 0)
2657 sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
2658 else if (strcmp(id, "TCP:") == 0)
2659 sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
2661 &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
2664 static int get_sockstat(struct sockstat *s)
2669 memset(s, 0, sizeof(*s));
2671 if ((fp = net_sockstat_open()) == NULL)
2673 while(fgets(buf, sizeof(buf), fp) != NULL)
2674 get_sockstat_line(buf, s);
2677 if ((fp = net_sockstat6_open()) == NULL)
2679 while(fgets(buf, sizeof(buf), fp) != NULL)
2680 get_sockstat_line(buf, s);
2686 static int print_summary(void)
2691 if (get_sockstat(&s) < 0)
2692 perror("ss: get_sockstat");
2693 if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
2694 perror("ss: get_snmpstat");
2696 printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
2698 printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
2699 s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
2701 s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
2704 s.tcp_tws, slabstat.tcp_tws,
2709 printf("Transport Total IP IPv6\n");
2710 printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
2711 printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
2712 printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
2713 printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
2714 printf("INET %-9d %-9d %-9d\n",
2715 s.raw4+s.udp4+s.tcp4_hashed+
2716 s.raw6+s.udp6+s.tcp6_hashed,
2717 s.raw4+s.udp4+s.tcp4_hashed,
2718 s.raw6+s.udp6+s.tcp6_hashed);
2719 printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
2726 static void _usage(FILE *dest)
2729 "Usage: ss [ OPTIONS ]\n"
2730 " ss [ OPTIONS ] [ FILTER ]\n"
2731 " -h, --help this message\n"
2732 " -V, --version output version information\n"
2733 " -n, --numeric don't resolve service names\n"
2734 " -r, --resolve resolve host names\n"
2735 " -a, --all display all sockets\n"
2736 " -l, --listening display listening sockets\n"
2737 " -o, --options show timer information\n"
2738 " -e, --extended show detailed socket information\n"
2739 " -m, --memory show socket memory usage\n"
2740 " -p, --processes show process using socket\n"
2741 " -i, --info show internal TCP information\n"
2742 " -s, --summary show socket usage summary\n"
2744 " -4, --ipv4 display only IP version 4 sockets\n"
2745 " -6, --ipv6 display only IP version 6 sockets\n"
2746 " -0, --packet display PACKET sockets\n"
2747 " -t, --tcp display only TCP sockets\n"
2748 " -u, --udp display only UDP sockets\n"
2749 " -d, --dccp display only DCCP sockets\n"
2750 " -w, --raw display only RAW sockets\n"
2751 " -x, --unix display only Unix domain sockets\n"
2752 " -f, --family=FAMILY display sockets of type FAMILY\n"
2754 " -A, --query=QUERY, --socket=QUERY\n"
2755 " QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n"
2757 " -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
2758 " -F, --filter=FILE read filter information from FILE\n"
2759 " FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n"
2763 static void help(void) __attribute__((noreturn));
2764 static void help(void)
2770 static void usage(void) __attribute__((noreturn));
2771 static void usage(void)
2778 static int scan_state(const char *state)
2781 if (strcasecmp(state, "close") == 0 ||
2782 strcasecmp(state, "closed") == 0)
2783 return (1<<SS_CLOSE);
2784 if (strcasecmp(state, "syn-rcv") == 0)
2785 return (1<<SS_SYN_RECV);
2786 if (strcasecmp(state, "established") == 0)
2787 return (1<<SS_ESTABLISHED);
2788 if (strcasecmp(state, "all") == 0)
2790 if (strcasecmp(state, "connected") == 0)
2791 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
2792 if (strcasecmp(state, "synchronized") == 0)
2793 return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
2794 if (strcasecmp(state, "bucket") == 0)
2795 return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
2796 if (strcasecmp(state, "big") == 0)
2797 return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
2798 for (i=0; i<SS_MAX; i++) {
2799 if (strcasecmp(state, sstate_namel[i]) == 0)
2805 static const struct option long_opts[] = {
2806 { "numeric", 0, 0, 'n' },
2807 { "resolve", 0, 0, 'r' },
2808 { "options", 0, 0, 'o' },
2809 { "extended", 0, 0, 'e' },
2810 { "memory", 0, 0, 'm' },
2811 { "info", 0, 0, 'i' },
2812 { "processes", 0, 0, 'p' },
2813 { "dccp", 0, 0, 'd' },
2814 { "tcp", 0, 0, 't' },
2815 { "udp", 0, 0, 'u' },
2816 { "raw", 0, 0, 'w' },
2817 { "unix", 0, 0, 'x' },
2818 { "all", 0, 0, 'a' },
2819 { "listening", 0, 0, 'l' },
2820 { "ipv4", 0, 0, '4' },
2821 { "ipv6", 0, 0, '6' },
2822 { "packet", 0, 0, '0' },
2823 { "family", 1, 0, 'f' },
2824 { "socket", 1, 0, 'A' },
2825 { "query", 1, 0, 'A' },
2826 { "summary", 0, 0, 's' },
2827 { "diag", 1, 0, 'D' },
2828 { "filter", 1, 0, 'F' },
2829 { "version", 0, 0, 'V' },
2830 { "help", 0, 0, 'h' },
2835 int main(int argc, char *argv[])
2841 const char *dump_tcpdiag = NULL;
2842 FILE *filter_fp = NULL;
2845 memset(¤t_filter, 0, sizeof(current_filter));
2847 current_filter.states = default_filter.states;
2849 while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spf:miA:D:F:vV",
2850 long_opts, NULL)) != EOF) {
2853 resolve_services = 0;
2873 user_ent_hash_build();
2876 current_filter.dbs |= (1<<DCCP_DB);
2880 current_filter.dbs |= (1<<TCP_DB);
2884 current_filter.dbs |= (1<<UDP_DB);
2888 current_filter.dbs |= (1<<RAW_DB);
2892 current_filter.dbs |= UNIX_DBM;
2896 current_filter.states = SS_ALL;
2899 current_filter.states = (1<<SS_LISTEN) | (1<<SS_CLOSE);
2902 preferred_family = AF_INET;
2905 preferred_family = AF_INET6;
2908 preferred_family = AF_PACKET;
2911 if (strcmp(optarg, "inet") == 0)
2912 preferred_family = AF_INET;
2913 else if (strcmp(optarg, "inet6") == 0)
2914 preferred_family = AF_INET6;
2915 else if (strcmp(optarg, "link") == 0)
2916 preferred_family = AF_PACKET;
2917 else if (strcmp(optarg, "unix") == 0)
2918 preferred_family = AF_UNIX;
2919 else if (strcmp(optarg, "netlink") == 0)
2920 preferred_family = AF_NETLINK;
2921 else if (strcmp(optarg, "help") == 0)
2924 fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg);
2932 current_filter.dbs = 0;
2938 if ((p1 = strchr(p, ',')) != NULL)
2940 if (strcmp(p, "all") == 0) {
2941 current_filter.dbs = ALL_DB;
2942 } else if (strcmp(p, "inet") == 0) {
2943 current_filter.dbs |= (1<<TCP_DB)|(1<<DCCP_DB)|(1<<UDP_DB)|(1<<RAW_DB);
2944 } else if (strcmp(p, "udp") == 0) {
2945 current_filter.dbs |= (1<<UDP_DB);
2946 } else if (strcmp(p, "dccp") == 0) {
2947 current_filter.dbs |= (1<<DCCP_DB);
2948 } else if (strcmp(p, "tcp") == 0) {
2949 current_filter.dbs |= (1<<TCP_DB);
2950 } else if (strcmp(p, "raw") == 0) {
2951 current_filter.dbs |= (1<<RAW_DB);
2952 } else if (strcmp(p, "unix") == 0) {
2953 current_filter.dbs |= UNIX_DBM;
2954 } else if (strcasecmp(p, "unix_stream") == 0 ||
2955 strcmp(p, "u_str") == 0) {
2956 current_filter.dbs |= (1<<UNIX_ST_DB);
2957 } else if (strcasecmp(p, "unix_dgram") == 0 ||
2958 strcmp(p, "u_dgr") == 0) {
2959 current_filter.dbs |= (1<<UNIX_DG_DB);
2960 } else if (strcmp(p, "packet") == 0) {
2961 current_filter.dbs |= PACKET_DBM;
2962 } else if (strcmp(p, "packet_raw") == 0 ||
2963 strcmp(p, "p_raw") == 0) {
2964 current_filter.dbs |= (1<<PACKET_R_DB);
2965 } else if (strcmp(p, "packet_dgram") == 0 ||
2966 strcmp(p, "p_dgr") == 0) {
2967 current_filter.dbs |= (1<<PACKET_DG_DB);
2968 } else if (strcmp(p, "netlink") == 0) {
2969 current_filter.dbs |= (1<<NETLINK_DB);
2971 fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
2982 dump_tcpdiag = optarg;
2986 fprintf(stderr, "More than one filter file\n");
2989 if (optarg[0] == '-')
2992 filter_fp = fopen(optarg, "r");
2994 perror("fopen filter file");
3000 printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
3013 get_slabstat(&slabstat);
3017 if (do_default && argc == 0)
3022 current_filter.dbs = default_filter.dbs;
3024 if (preferred_family == AF_UNSPEC) {
3025 if (!(current_filter.dbs&~UNIX_DBM))
3026 preferred_family = AF_UNIX;
3027 else if (!(current_filter.dbs&~PACKET_DBM))
3028 preferred_family = AF_PACKET;
3029 else if (!(current_filter.dbs&~(1<<NETLINK_DB)))
3030 preferred_family = AF_NETLINK;
3033 if (preferred_family != AF_UNSPEC) {
3035 if (preferred_family == AF_INET ||
3036 preferred_family == AF_INET6) {
3037 mask2= current_filter.dbs;
3038 } else if (preferred_family == AF_PACKET) {
3040 } else if (preferred_family == AF_UNIX) {
3042 } else if (preferred_family == AF_NETLINK) {
3043 mask2 = (1<<NETLINK_DB);
3049 current_filter.dbs = mask2;
3051 current_filter.dbs &= mask2;
3052 current_filter.families = (1<<preferred_family);
3055 current_filter.families = ~0;
3057 current_filter.families = default_filter.families;
3059 if (current_filter.dbs == 0) {
3060 fprintf(stderr, "ss: no socket tables to show with such filter.\n");
3063 if (current_filter.families == 0) {
3064 fprintf(stderr, "ss: no families to show with such filter.\n");
3068 if (resolve_services && resolve_hosts &&
3069 (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
3070 init_service_resolver();
3072 /* Now parse filter... */
3073 if (argc == 0 && filter_fp) {
3074 if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp))
3079 if (strcmp(*argv, "state") == 0) {
3082 current_filter.states = 0;
3083 current_filter.states |= scan_state(*argv);
3085 } else if (strcmp(*argv, "exclude") == 0 ||
3086 strcmp(*argv, "excl") == 0) {
3089 current_filter.states = SS_ALL;
3090 current_filter.states &= ~scan_state(*argv);
3093 if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp))
3100 if (current_filter.states == 0) {
3101 fprintf(stderr, "ss: no socket states to show with such filter.\n");
3106 FILE *dump_fp = stdout;
3107 if (!(current_filter.dbs & (1<<TCP_DB))) {
3108 fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
3111 if (dump_tcpdiag[0] != '-') {
3112 dump_fp = fopen(dump_tcpdiag, "w");
3113 if (!dump_tcpdiag) {
3114 perror("fopen dump file");
3118 inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP);
3124 if (current_filter.dbs&(current_filter.dbs-1))
3128 if (current_filter.states&(current_filter.states-1))
3132 if (isatty(STDOUT_FILENO)) {
3135 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
3137 screen_width = w.ws_col;
3141 addrp_width = screen_width;
3142 addrp_width -= netid_width+1;
3143 addrp_width -= state_width+1;
3146 if (addrp_width&1) {
3149 else if (state_width)
3156 serv_width = resolve_services ? 7 : 5;
3158 if (addrp_width < 15+serv_width+1)
3159 addrp_width = 15+serv_width+1;
3161 addr_width = addrp_width - serv_width - 1;
3164 printf("%-*s ", netid_width, "Netid");
3166 printf("%-*s ", state_width, "State");
3167 printf("%-6s %-6s ", "Recv-Q", "Send-Q");
3169 printf("%*s:%-*s %*s:%-*s\n",
3170 addr_width, "Local Address", serv_width, "Port",
3171 addr_width, "Peer Address", serv_width, "Port");
3175 if (current_filter.dbs & (1<<NETLINK_DB))
3176 netlink_show(¤t_filter);
3177 if (current_filter.dbs & PACKET_DBM)
3178 packet_show(¤t_filter);
3179 if (current_filter.dbs & UNIX_DBM)
3180 unix_show(¤t_filter);
3181 if (current_filter.dbs & (1<<RAW_DB))
3182 raw_show(¤t_filter);
3183 if (current_filter.dbs & (1<<UDP_DB))
3184 udp_show(¤t_filter);
3185 if (current_filter.dbs & (1<<TCP_DB))
3186 tcp_show(¤t_filter, IPPROTO_TCP);
3187 if (current_filter.dbs & (1<<DCCP_DB))
3188 tcp_show(¤t_filter, IPPROTO_DCCP);