[T106][ZXW-22]7520V3SCV2.01.01.02P42U09_VEC_V0.8_AP_VEC origin source commit
Change-Id: Ic6e05d89ecd62fc34f82b23dcf306c93764aec4b
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/Makefile b/ap/app/iproute2/iproute2-3.4.0/misc/Makefile
new file mode 100755
index 0000000..d0a33e8
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/Makefile
@@ -0,0 +1,35 @@
+SSOBJ=ss.o ssfilter.o
+LNSTATOBJ=lnstat.o lnstat_util.o
+
+TARGETS=ss nstat ifstat rtacct lnstat
+
+include ../Config
+
+all: $(TARGETS)
+
+ss: $(SSOBJ)
+
+nstat: nstat.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o nstat nstat.c -lm
+
+ifstat: ifstat.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o ifstat ifstat.c $(LIBNETLINK) -lm
+
+rtacct: rtacct.c
+ $(CC) $(CFLAGS) $(LDFLAGS) -o rtacct rtacct.c $(LIBNETLINK) -lm
+
+arpd: arpd.c
+ $(CC) $(CFLAGS) -I$(DBM_INCLUDE) $(LDFLAGS) -o arpd arpd.c $(LIBNETLINK) -ldb -lpthread
+
+ssfilter.c: ssfilter.y
+ bison ssfilter.y -o ssfilter.c
+
+lnstat: $(LNSTATOBJ)
+
+install: all
+ install -m 0755 $(TARGETS) $(DESTDIR)$(SBINDIR)
+ ln -sf lnstat $(DESTDIR)$(SBINDIR)/rtstat
+ ln -sf lnstat $(DESTDIR)$(SBINDIR)/ctstat
+
+clean:
+ rm -f *.o $(TARGETS) ssfilter.c
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/arpd.c b/ap/app/iproute2/iproute2-3.4.0/misc/arpd.c
new file mode 100755
index 0000000..dd1de80
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/arpd.c
@@ -0,0 +1,837 @@
+/*
+ * arpd.c ARP helper daemon.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ */
+
+#include <stdio.h>
+#include <syslog.h>
+#include <malloc.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <netdb.h>
+#include <db_185.h>
+#include <sys/ioctl.h>
+#include <sys/poll.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <time.h>
+#include <signal.h>
+#include <linux/if.h>
+#include <linux/if_ether.h>
+#include <linux/if_arp.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <linux/if_packet.h>
+#include <linux/filter.h>
+
+#include "libnetlink.h"
+#include "utils.h"
+
+int resolve_hosts;
+
+DB *dbase;
+char *dbname = "/var/lib/arpd/arpd.db";
+
+int ifnum;
+int *ifvec;
+char **ifnames;
+
+struct dbkey
+{
+ __u32 iface;
+ __u32 addr;
+};
+
+#define IS_NEG(x) (((__u8*)(x))[0] == 0xFF)
+#define NEG_TIME(x) (((x)[2]<<24)|((x)[3]<<16)|((x)[4]<<8)|(x)[5])
+#define NEG_AGE(x) ((__u32)time(NULL) - NEG_TIME((__u8*)x))
+#define NEG_VALID(x) (NEG_AGE(x) < negative_timeout)
+#define NEG_CNT(x) (((__u8*)(x))[1])
+
+struct rtnl_handle rth;
+
+struct pollfd pset[2];
+int udp_sock = -1;
+
+volatile int do_exit;
+volatile int do_sync;
+volatile int do_stats;
+
+struct {
+ unsigned long arp_new;
+ unsigned long arp_change;
+
+ unsigned long app_recv;
+ unsigned long app_success;
+ unsigned long app_bad;
+ unsigned long app_neg;
+ unsigned long app_suppressed;
+
+ unsigned long kern_neg;
+ unsigned long kern_new;
+ unsigned long kern_change;
+
+ unsigned long probes_sent;
+ unsigned long probes_suppressed;
+} stats;
+
+int active_probing;
+int negative_timeout = 60;
+int no_kernel_broadcasts;
+int broadcast_rate = 1000;
+int broadcast_burst = 3000;
+int poll_timeout = 30000;
+
+void usage(void)
+{
+ fprintf(stderr,
+ "Usage: arpd [ -lkh? ] [ -a N ] [ -b dbase ] [ -B number ]"
+ " [ -f file ] [ -n time ] [-p interval ] [ -R rate ] [ interfaces ]\n");
+ exit(1);
+}
+
+int handle_if(int ifindex)
+{
+ int i;
+
+ if (ifnum == 0)
+ return 1;
+
+ for (i=0; i<ifnum; i++)
+ if (ifvec[i] == ifindex)
+ return 1;
+ return 0;
+}
+
+int sysctl_adjusted;
+
+void do_sysctl_adjustments(void)
+{
+ int i;
+
+ if (!ifnum)
+ return;
+
+ for (i=0; i<ifnum; i++) {
+ char buf[128];
+ FILE *fp;
+
+ if (active_probing) {
+ sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/mcast_solicit", ifnames[i]);
+ if ((fp = fopen(buf, "w")) != NULL) {
+ if (no_kernel_broadcasts)
+ strcpy(buf, "0\n");
+ else
+ sprintf(buf, "%d\n", active_probing>=2 ? 1 : 3-active_probing);
+ fputs(buf, fp);
+ fclose(fp);
+ }
+ }
+
+ sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]);
+ if ((fp = fopen(buf, "w")) != NULL) {
+ sprintf(buf, "%d\n", active_probing<=1 ? 1 : active_probing);
+ fputs(buf, fp);
+ fclose(fp);
+ }
+ }
+ sysctl_adjusted = 1;
+}
+
+void undo_sysctl_adjustments(void)
+{
+ int i;
+
+ if (!sysctl_adjusted)
+ return;
+
+ for (i=0; i<ifnum; i++) {
+ char buf[128];
+ FILE *fp;
+
+ if (active_probing) {
+ sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/mcast_solicit", ifnames[i]);
+ if ((fp = fopen(buf, "w")) != NULL) {
+ strcpy(buf, "3\n");
+ fputs(buf, fp);
+ fclose(fp);
+ }
+ }
+ sprintf(buf, "/proc/sys/net/ipv4/neigh/%s/app_solicit", ifnames[i]);
+ if ((fp = fopen(buf, "w")) != NULL) {
+ strcpy(buf, "0\n");
+ fputs(buf, fp);
+ fclose(fp);
+ }
+ }
+ sysctl_adjusted = 0;
+}
+
+
+int send_probe(int ifindex, __u32 addr)
+{
+ struct ifreq ifr;
+ struct sockaddr_in dst;
+ socklen_t len;
+ unsigned char buf[256];
+ struct arphdr *ah = (struct arphdr*)buf;
+ unsigned char *p = (unsigned char *)(ah+1);
+ struct sockaddr_ll sll;
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_ifindex = ifindex;
+ if (ioctl(udp_sock, SIOCGIFNAME, &ifr))
+ return -1;
+ if (ioctl(udp_sock, SIOCGIFHWADDR, &ifr))
+ return -1;
+ if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
+ return -1;
+ if (setsockopt(udp_sock, SOL_SOCKET, SO_BINDTODEVICE, ifr.ifr_name, strlen(ifr.ifr_name)+1) < 0)
+ return -1;
+
+ dst.sin_family = AF_INET;
+ dst.sin_port = htons(1025);
+ dst.sin_addr.s_addr = addr;
+ if (connect(udp_sock, (struct sockaddr*)&dst, sizeof(dst)) < 0)
+ return -1;
+ len = sizeof(dst);
+ if (getsockname(udp_sock, (struct sockaddr*)&dst, &len) < 0)
+ return -1;
+
+ ah->ar_hrd = htons(ifr.ifr_hwaddr.sa_family);
+ ah->ar_pro = htons(ETH_P_IP);
+ ah->ar_hln = 6;
+ ah->ar_pln = 4;
+ ah->ar_op = htons(ARPOP_REQUEST);
+
+ memcpy(p, ifr.ifr_hwaddr.sa_data, ah->ar_hln);
+ p += ah->ar_hln;
+
+ memcpy(p, &dst.sin_addr, 4);
+ p+=4;
+
+ sll.sll_family = AF_PACKET;
+ memset(sll.sll_addr, 0xFF, sizeof(sll.sll_addr));
+ sll.sll_ifindex = ifindex;
+ sll.sll_protocol = htons(ETH_P_ARP);
+ memcpy(p, &sll.sll_addr, ah->ar_hln);
+ p+=ah->ar_hln;
+
+ memcpy(p, &addr, 4);
+ p+=4;
+
+ if (sendto(pset[0].fd, buf, p-buf, 0, (struct sockaddr*)&sll, sizeof(sll)) < 0)
+ return -1;
+ stats.probes_sent++;
+ return 0;
+}
+
+/* Be very tough on sending probes: 1 per second with burst of 3. */
+
+int queue_active_probe(int ifindex, __u32 addr)
+{
+ static struct timeval prev;
+ static int buckets;
+ struct timeval now;
+
+ gettimeofday(&now, NULL);
+ if (prev.tv_sec) {
+ int diff = (now.tv_sec-prev.tv_sec)*1000+(now.tv_usec-prev.tv_usec)/1000;
+ buckets += diff;
+ } else {
+ buckets = broadcast_burst;
+ }
+ if (buckets > broadcast_burst)
+ buckets = broadcast_burst;
+ if (buckets >= broadcast_rate && !send_probe(ifindex, addr)) {
+ buckets -= broadcast_rate;
+ prev = now;
+ return 0;
+ }
+ stats.probes_suppressed++;
+ return -1;
+}
+
+int respond_to_kernel(int ifindex, __u32 addr, char *lla, int llalen)
+{
+ struct {
+ struct nlmsghdr n;
+ struct ndmsg ndm;
+ char buf[256];
+ } req;
+
+ memset(&req.n, 0, sizeof(req.n));
+ memset(&req.ndm, 0, sizeof(req.ndm));
+
+ req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
+ req.n.nlmsg_flags = NLM_F_REQUEST;
+ req.n.nlmsg_type = RTM_NEWNEIGH;
+ req.ndm.ndm_family = AF_INET;
+ req.ndm.ndm_state = NUD_STALE;
+ req.ndm.ndm_ifindex = ifindex;
+ req.ndm.ndm_type = RTN_UNICAST;
+
+ addattr_l(&req.n, sizeof(req), NDA_DST, &addr, 4);
+ addattr_l(&req.n, sizeof(req), NDA_LLADDR, lla, llalen);
+ return rtnl_send(&rth, &req, req.n.nlmsg_len) <= 0;
+}
+
+void prepare_neg_entry(__u8 *ndata, __u32 stamp)
+{
+ ndata[0] = 0xFF;
+ ndata[1] = 0;
+ ndata[2] = stamp>>24;
+ ndata[3] = stamp>>16;
+ ndata[4] = stamp>>8;
+ ndata[5] = stamp;
+}
+
+
+int do_one_request(struct nlmsghdr *n)
+{
+ struct ndmsg *ndm = NLMSG_DATA(n);
+ int len = n->nlmsg_len;
+ struct rtattr * tb[NDA_MAX+1];
+ struct dbkey key;
+ DBT dbkey, dbdat;
+ int do_acct = 0;
+
+ if (n->nlmsg_type == NLMSG_DONE) {
+ dbase->sync(dbase, 0);
+
+ /* Now we have at least mirror of kernel db, so that
+ * may start real resolution.
+ */
+ do_sysctl_adjustments();
+ return 0;
+ }
+
+ if (n->nlmsg_type != RTM_GETNEIGH && n->nlmsg_type != RTM_NEWNEIGH)
+ return 0;
+
+ len -= NLMSG_LENGTH(sizeof(*ndm));
+ if (len < 0)
+ return -1;
+
+ if (ndm->ndm_family != AF_INET ||
+ (ifnum && !handle_if(ndm->ndm_ifindex)) ||
+ ndm->ndm_flags ||
+ ndm->ndm_type != RTN_UNICAST ||
+ !(ndm->ndm_state&~NUD_NOARP))
+ return 0;
+
+ parse_rtattr(tb, NDA_MAX, NDA_RTA(ndm), len);
+
+ if (!tb[NDA_DST])
+ return 0;
+
+ key.iface = ndm->ndm_ifindex;
+ memcpy(&key.addr, RTA_DATA(tb[NDA_DST]), 4);
+ dbkey.data = &key;
+ dbkey.size = sizeof(key);
+
+ if (dbase->get(dbase, &dbkey, &dbdat, 0) != 0) {
+ dbdat.data = 0;
+ dbdat.size = 0;
+ }
+
+ if (n->nlmsg_type == RTM_GETNEIGH) {
+ if (!(n->nlmsg_flags&NLM_F_REQUEST))
+ return 0;
+
+ if (!(ndm->ndm_state&(NUD_PROBE|NUD_INCOMPLETE))) {
+ stats.app_bad++;
+ return 0;
+ }
+
+ if (ndm->ndm_state&NUD_PROBE) {
+ /* If we get this, kernel still has some valid
+ * address, but unicast probing failed and host
+ * is either dead or changed its mac address.
+ * Kernel is going to initiate broadcast resolution.
+ * OK, we invalidate our information as well.
+ */
+ if (dbdat.data && !IS_NEG(dbdat.data))
+ stats.app_neg++;
+
+ dbase->del(dbase, &dbkey, 0);
+ } else {
+ /* If we get this kernel does not have any information.
+ * If we have something tell this to kernel. */
+ stats.app_recv++;
+ if (dbdat.data && !IS_NEG(dbdat.data)) {
+ stats.app_success++;
+ respond_to_kernel(key.iface, key.addr, dbdat.data, dbdat.size);
+ return 0;
+ }
+
+ /* Sheeit! We have nothing to tell. */
+ /* If we have recent negative entry, be silent. */
+ if (dbdat.data && NEG_VALID(dbdat.data)) {
+ if (NEG_CNT(dbdat.data) >= active_probing) {
+ stats.app_suppressed++;
+ return 0;
+ }
+ do_acct = 1;
+ }
+ }
+
+ if (active_probing &&
+ queue_active_probe(ndm->ndm_ifindex, key.addr) == 0 &&
+ do_acct) {
+ NEG_CNT(dbdat.data)++;
+ dbase->put(dbase, &dbkey, &dbdat, 0);
+ }
+ } else if (n->nlmsg_type == RTM_NEWNEIGH) {
+ if (n->nlmsg_flags&NLM_F_REQUEST)
+ return 0;
+
+ if (ndm->ndm_state&NUD_FAILED) {
+ /* Kernel was not able to resolve. Host is dead.
+ * Create negative entry if it is not present
+ * or renew it if it is too old. */
+ if (!dbdat.data ||
+ !IS_NEG(dbdat.data) ||
+ !NEG_VALID(dbdat.data)) {
+ __u8 ndata[6];
+ stats.kern_neg++;
+ prepare_neg_entry(ndata, time(NULL));
+ dbdat.data = ndata;
+ dbdat.size = sizeof(ndata);
+ dbase->put(dbase, &dbkey, &dbdat, 0);
+ }
+ } else if (tb[NDA_LLADDR]) {
+ if (dbdat.data && !IS_NEG(dbdat.data)) {
+ if (memcmp(RTA_DATA(tb[NDA_LLADDR]), dbdat.data, dbdat.size) == 0)
+ return 0;
+ stats.kern_change++;
+ } else {
+ stats.kern_new++;
+ }
+ dbdat.data = RTA_DATA(tb[NDA_LLADDR]);
+ dbdat.size = RTA_PAYLOAD(tb[NDA_LLADDR]);
+ dbase->put(dbase, &dbkey, &dbdat, 0);
+ }
+ }
+ return 0;
+}
+
+void load_initial_table(void)
+{
+ rtnl_wilddump_request(&rth, AF_INET, RTM_GETNEIGH);
+}
+
+void get_kern_msg(void)
+{
+ int status;
+ struct nlmsghdr *h;
+ struct sockaddr_nl nladdr;
+ struct iovec iov;
+ char buf[8192];
+ struct msghdr msg = {
+ (void*)&nladdr, sizeof(nladdr),
+ &iov, 1,
+ NULL, 0,
+ 0
+ };
+
+ memset(&nladdr, 0, sizeof(nladdr));
+
+ iov.iov_base = buf;
+ iov.iov_len = sizeof(buf);
+
+ status = recvmsg(rth.fd, &msg, MSG_DONTWAIT);
+
+ if (status <= 0)
+ return;
+
+ if (msg.msg_namelen != sizeof(nladdr))
+ return;
+
+ if (nladdr.nl_pid)
+ return;
+
+ for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
+ int len = h->nlmsg_len;
+ int l = len - sizeof(*h);
+
+ if (l < 0 || len > status)
+ return;
+
+ if (do_one_request(h) < 0)
+ return;
+
+ status -= NLMSG_ALIGN(len);
+ h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
+ }
+}
+
+/* Receive gratuitous ARP messages and store them, that's all. */
+void get_arp_pkt(void)
+{
+ unsigned char buf[1024];
+ struct sockaddr_ll sll;
+ socklen_t sll_len = sizeof(sll);
+ struct arphdr *a = (struct arphdr*)buf;
+ struct dbkey key;
+ DBT dbkey, dbdat;
+ int n;
+
+ n = recvfrom(pset[0].fd, buf, sizeof(buf), MSG_DONTWAIT,
+ (struct sockaddr*)&sll, &sll_len);
+ if (n < 0) {
+ if (errno != EINTR && errno != EAGAIN)
+ syslog(LOG_ERR, "recvfrom: %m");
+ return;
+ }
+
+ if (ifnum && !handle_if(sll.sll_ifindex))
+ return;
+
+ /* Sanity checks */
+
+ if (n < sizeof(*a) ||
+ (a->ar_op != htons(ARPOP_REQUEST) &&
+ a->ar_op != htons(ARPOP_REPLY)) ||
+ a->ar_pln != 4 ||
+ a->ar_pro != htons(ETH_P_IP) ||
+ a->ar_hln != sll.sll_halen ||
+ sizeof(*a) + 2*4 + 2*a->ar_hln > n)
+ return;
+
+ key.iface = sll.sll_ifindex;
+ memcpy(&key.addr, (char*)(a+1) + a->ar_hln, 4);
+
+ /* DAD message, ignore. */
+ if (key.addr == 0)
+ return;
+
+ dbkey.data = &key;
+ dbkey.size = sizeof(key);
+
+ if (dbase->get(dbase, &dbkey, &dbdat, 0) == 0 && !IS_NEG(dbdat.data)) {
+ if (memcmp(dbdat.data, a+1, dbdat.size) == 0)
+ return;
+ stats.arp_change++;
+ } else {
+ stats.arp_new++;
+ }
+
+ dbdat.data = a+1;
+ dbdat.size = a->ar_hln;
+ dbase->put(dbase, &dbkey, &dbdat, 0);
+}
+
+void catch_signal(int sig, void (*handler)(int))
+{
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = handler;
+#ifdef SA_INTERRUPT
+ sa.sa_flags = SA_INTERRUPT;
+#endif
+ sigaction(sig, &sa, NULL);
+}
+
+#include <setjmp.h>
+sigjmp_buf env;
+volatile int in_poll;
+
+void sig_exit(int signo)
+{
+ do_exit = 1;
+ if (in_poll)
+ siglongjmp(env, 1);
+}
+
+void sig_sync(int signo)
+{
+ do_sync = 1;
+ if (in_poll)
+ siglongjmp(env, 1);
+}
+
+void sig_stats(int signo)
+{
+ do_sync = 1;
+ do_stats = 1;
+ if (in_poll)
+ siglongjmp(env, 1);
+}
+
+void send_stats(void)
+{
+ syslog(LOG_INFO, "arp_rcv: n%lu c%lu app_rcv: tot %lu hits %lu bad %lu neg %lu sup %lu",
+ stats.arp_new, stats.arp_change,
+
+ stats.app_recv, stats.app_success,
+ stats.app_bad, stats.app_neg, stats.app_suppressed
+ );
+ syslog(LOG_INFO, "kern: n%lu c%lu neg %lu arp_send: %lu rlim %lu",
+ stats.kern_new, stats.kern_change, stats.kern_neg,
+
+ stats.probes_sent, stats.probes_suppressed
+ );
+ do_stats = 0;
+}
+
+
+int main(int argc, char **argv)
+{
+ int opt;
+ int do_list = 0;
+ char *do_load = NULL;
+
+ while ((opt = getopt(argc, argv, "h?b:lf:a:n:p:kR:B:")) != EOF) {
+ switch (opt) {
+ case 'b':
+ dbname = optarg;
+ break;
+ case 'f':
+ if (do_load) {
+ fprintf(stderr, "Duplicate option -f\n");
+ usage();
+ }
+ do_load = optarg;
+ break;
+ case 'l':
+ do_list = 1;
+ break;
+ case 'a':
+ active_probing = atoi(optarg);
+ break;
+ case 'n':
+ negative_timeout = atoi(optarg);
+ break;
+ case 'k':
+ no_kernel_broadcasts = 1;
+ break;
+ case 'p':
+ if ((poll_timeout = 1000 * strtod(optarg, NULL)) < 100) {
+ fprintf(stderr,"Invalid poll timeout\n");
+ exit(-1);
+ }
+ break;
+ case 'R':
+ if ((broadcast_rate = atoi(optarg)) <= 0 ||
+ (broadcast_rate = 1000/broadcast_rate) <= 0) {
+ fprintf(stderr, "Invalid ARP rate\n");
+ exit(-1);
+ }
+ break;
+ case 'B':
+ if ((broadcast_burst = atoi(optarg)) <= 0 ||
+ (broadcast_burst = 1000*broadcast_burst) <= 0) {
+ fprintf(stderr, "Invalid ARP burst\n");
+ exit(-1);
+ }
+ break;
+ case 'h':
+ case '?':
+ default:
+ usage();
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc > 0) {
+ ifnum = argc;
+ ifnames = argv;
+ ifvec = malloc(argc*sizeof(int));
+ if (!ifvec) {
+ perror("malloc");
+ exit(-1);
+ }
+ }
+
+ if ((udp_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+ perror("socket");
+ exit(-1);
+ }
+
+ if (ifnum) {
+ int i;
+ struct ifreq ifr;
+ memset(&ifr, 0, sizeof(ifr));
+ for (i=0; i<ifnum; i++) {
+ strncpy(ifr.ifr_name, ifnames[i], IFNAMSIZ);
+ if (ioctl(udp_sock, SIOCGIFINDEX, &ifr)) {
+ perror("ioctl(SIOCGIFINDEX)");
+ exit(-1);;
+ }
+ ifvec[i] = ifr.ifr_ifindex;
+ }
+ }
+
+ dbase = dbopen(dbname, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
+ if (dbase == NULL) {
+ perror("db_open");
+ exit(-1);
+ }
+
+ if (do_load) {
+ char buf[128];
+ FILE *fp;
+ struct dbkey k;
+ DBT dbkey, dbdat;
+
+ dbkey.data = &k;
+ dbkey.size = sizeof(k);
+
+ if (strcmp(do_load, "-") == 0 || strcmp(do_load, "--") == 0) {
+ fp = stdin;
+ } else if ((fp = fopen(do_load, "r")) == NULL) {
+ perror("fopen");
+ goto do_abort;
+ }
+
+ buf[sizeof(buf)-1] = 0;
+ while (fgets(buf, sizeof(buf)-1, fp)) {
+ __u8 b1[6];
+ char ipbuf[128];
+ char macbuf[128];
+
+ if (buf[0] == '#')
+ continue;
+
+ if (sscanf(buf, "%u%s%s", &k.iface, ipbuf, macbuf) != 3) {
+ fprintf(stderr, "Wrong format of input file \"%s\"\n", do_load);
+ goto do_abort;
+ }
+ if (strncmp(macbuf, "FAILED:", 7) == 0)
+ continue;
+ if (!inet_aton(ipbuf, (struct in_addr*)&k.addr)) {
+ fprintf(stderr, "Invalid IP address: \"%s\"\n", ipbuf);
+ goto do_abort;
+ }
+
+ dbdat.data = hexstring_a2n(macbuf, b1, 6);
+ if (dbdat.data == NULL)
+ goto do_abort;
+ dbdat.size = 6;
+
+ if (dbase->put(dbase, &dbkey, &dbdat, 0)) {
+ perror("hash->put");
+ goto do_abort;
+ }
+ }
+ dbase->sync(dbase, 0);
+ if (fp != stdin)
+ fclose(fp);
+ }
+
+ if (do_list) {
+ DBT dbkey, dbdat;
+ printf("%-8s %-15s %s\n", "#Ifindex", "IP", "MAC");
+ while (dbase->seq(dbase, &dbkey, &dbdat, R_NEXT) == 0) {
+ struct dbkey *key = dbkey.data;
+ if (handle_if(key->iface)) {
+ if (!IS_NEG(dbdat.data)) {
+ char b1[18];
+ printf("%-8d %-15s %s\n",
+ key->iface,
+ inet_ntoa(*(struct in_addr*)&key->addr),
+ hexstring_n2a(dbdat.data, 6, b1, 18));
+ } else {
+ printf("%-8d %-15s FAILED: %dsec ago\n",
+ key->iface,
+ inet_ntoa(*(struct in_addr*)&key->addr),
+ NEG_AGE(dbdat.data));
+ }
+ }
+ }
+ }
+
+ if (do_load || do_list)
+ goto out;
+
+ pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0);
+ if (pset[0].fd < 0) {
+ perror("socket");
+ exit(-1);
+ }
+
+ if (1) {
+ struct sockaddr_ll sll;
+ memset(&sll, 0, sizeof(sll));
+ sll.sll_family = AF_PACKET;
+ sll.sll_protocol = htons(ETH_P_ARP);
+ sll.sll_ifindex = (ifnum == 1 ? ifvec[0] : 0);
+ if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) {
+ perror("bind");
+ goto do_abort;
+ }
+ }
+
+ if (rtnl_open(&rth, RTMGRP_NEIGH) < 0) {
+ perror("rtnl_open");
+ goto do_abort;
+ }
+ pset[1].fd = rth.fd;
+
+ load_initial_table();
+
+ if (daemon(0, 0)) {
+ perror("arpd: daemon");
+ goto do_abort;
+ }
+
+ openlog("arpd", LOG_PID | LOG_CONS, LOG_DAEMON);
+ catch_signal(SIGINT, sig_exit);
+ catch_signal(SIGTERM, sig_exit);
+ catch_signal(SIGHUP, sig_sync);
+ catch_signal(SIGUSR1, sig_stats);
+
+#define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP)
+ pset[0].events = EVENTS;
+ pset[0].revents = 0;
+ pset[1].events = EVENTS;
+ pset[1].revents = 0;
+
+ sigsetjmp(env, 1);
+
+ for (;;) {
+ in_poll = 1;
+
+ if (do_exit)
+ break;
+ if (do_sync) {
+ in_poll = 0;
+ dbase->sync(dbase, 0);
+ do_sync = 0;
+ in_poll = 1;
+ }
+ if (do_stats)
+ send_stats();
+ if (poll(pset, 2, poll_timeout) > 0) {
+ in_poll = 0;
+ if (pset[0].revents&EVENTS)
+ get_arp_pkt();
+ if (pset[1].revents&EVENTS)
+ get_kern_msg();
+ } else {
+ do_sync = 1;
+ }
+ }
+
+ undo_sysctl_adjustments();
+out:
+ dbase->close(dbase);
+ exit(0);
+
+do_abort:
+ dbase->close(dbase);
+ exit(-1);
+}
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/ifstat.c b/ap/app/iproute2/iproute2-3.4.0/misc/ifstat.c
new file mode 100755
index 0000000..e7fbaa8
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/ifstat.c
@@ -0,0 +1,767 @@
+/*
+ * ifstat.c handy utility to read net interface statistics
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <sys/time.h>
+#include <fnmatch.h>
+#include <sys/file.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/poll.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <signal.h>
+#include <math.h>
+#include <getopt.h>
+
+#include <libnetlink.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+
+#include <SNAPSHOT.h>
+
+int dump_zeros = 0;
+int reset_history = 0;
+int ignore_history = 0;
+int no_output = 0;
+int no_update = 0;
+int scan_interval = 0;
+int time_constant = 0;
+int show_errors = 0;
+double W;
+char **patterns;
+int npatterns;
+
+char info_source[128];
+int source_mismatch;
+
+#define MAXS (sizeof(struct rtnl_link_stats)/sizeof(__u32))
+
+struct ifstat_ent
+{
+ struct ifstat_ent *next;
+ char *name;
+ int ifindex;
+ unsigned long long val[MAXS];
+ double rate[MAXS];
+ __u32 ival[MAXS];
+};
+
+struct ifstat_ent *kern_db;
+struct ifstat_ent *hist_db;
+
+static int match(const char *id)
+{
+ int i;
+
+ if (npatterns == 0)
+ return 1;
+
+ for (i=0; i<npatterns; i++) {
+ if (!fnmatch(patterns[i], id, 0))
+ return 1;
+ }
+ return 0;
+}
+
+static int get_nlmsg(const struct sockaddr_nl *who,
+ struct nlmsghdr *m, void *arg)
+{
+ struct ifinfomsg *ifi = NLMSG_DATA(m);
+ struct rtattr * tb[IFLA_MAX+1];
+ int len = m->nlmsg_len;
+ struct ifstat_ent *n;
+ int i;
+
+ if (m->nlmsg_type != RTM_NEWLINK)
+ return 0;
+
+ len -= NLMSG_LENGTH(sizeof(*ifi));
+ if (len < 0)
+ return -1;
+
+ if (!(ifi->ifi_flags&IFF_UP))
+ return 0;
+
+ parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
+ if (tb[IFLA_IFNAME] == NULL || tb[IFLA_STATS] == NULL)
+ return 0;
+
+ n = malloc(sizeof(*n));
+ if (!n)
+ abort();
+ n->ifindex = ifi->ifi_index;
+ n->name = strdup(RTA_DATA(tb[IFLA_IFNAME]));
+ memcpy(&n->ival, RTA_DATA(tb[IFLA_STATS]), sizeof(n->ival));
+ memset(&n->rate, 0, sizeof(n->rate));
+ for (i=0; i<MAXS; i++)
+ n->val[i] = n->ival[i];
+ n->next = kern_db;
+ kern_db = n;
+ return 0;
+}
+
+void load_info(void)
+{
+ struct ifstat_ent *db, *n;
+ struct rtnl_handle rth;
+
+ if (rtnl_open(&rth, 0) < 0)
+ exit(1);
+
+ if (rtnl_wilddump_request(&rth, AF_INET, RTM_GETLINK) < 0) {
+ perror("Cannot send dump request");
+ exit(1);
+ }
+
+ if (rtnl_dump_filter(&rth, get_nlmsg, NULL) < 0) {
+ fprintf(stderr, "Dump terminated\n");
+ exit(1);
+ }
+
+ rtnl_close(&rth);
+
+ db = kern_db;
+ kern_db = NULL;
+
+ while (db) {
+ n = db;
+ db = db->next;
+ n->next = kern_db;
+ kern_db = n;
+ }
+}
+
+void load_raw_table(FILE *fp)
+{
+ char buf[4096];
+ struct ifstat_ent *db = NULL;
+ struct ifstat_ent *n;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ char *p;
+ char *next;
+ int i;
+
+ if (buf[0] == '#') {
+ buf[strlen(buf)-1] = 0;
+ if (info_source[0] && strcmp(info_source, buf+1))
+ source_mismatch = 1;
+ strncpy(info_source, buf+1, sizeof(info_source)-1);
+ continue;
+ }
+ if ((n = malloc(sizeof(*n))) == NULL)
+ abort();
+
+ if (!(p = strchr(buf, ' ')))
+ abort();
+ *p++ = 0;
+
+ if (sscanf(buf, "%d", &n->ifindex) != 1)
+ abort();
+ if (!(next = strchr(p, ' ')))
+ abort();
+ *next++ = 0;
+
+ n->name = strdup(p);
+ p = next;
+
+ for (i=0; i<MAXS; i++) {
+ unsigned rate;
+ if (!(next = strchr(p, ' ')))
+ abort();
+ *next++ = 0;
+ if (sscanf(p, "%llu", n->val+i) != 1)
+ abort();
+ n->ival[i] = (__u32)n->val[i];
+ p = next;
+ if (!(next = strchr(p, ' ')))
+ abort();
+ *next++ = 0;
+ if (sscanf(p, "%u", &rate) != 1)
+ abort();
+ n->rate[i] = rate;
+ p = next;
+ }
+ n->next = db;
+ db = n;
+ }
+
+ while (db) {
+ n = db;
+ db = db->next;
+ n->next = kern_db;
+ kern_db = n;
+ }
+}
+
+void dump_raw_db(FILE *fp, int to_hist)
+{
+ struct ifstat_ent *n, *h;
+ h = hist_db;
+ fprintf(fp, "#%s\n", info_source);
+
+ for (n=kern_db; n; n=n->next) {
+ int i;
+ unsigned long long *vals = n->val;
+ double *rates = n->rate;
+ if (!match(n->name)) {
+ struct ifstat_ent *h1;
+ if (!to_hist)
+ continue;
+ for (h1 = h; h1; h1 = h1->next) {
+ if (h1->ifindex == n->ifindex) {
+ vals = h1->val;
+ rates = h1->rate;
+ h = h1->next;
+ break;
+ }
+ }
+ }
+ fprintf(fp, "%d %s ", n->ifindex, n->name);
+ for (i=0; i<MAXS; i++)
+ fprintf(fp, "%llu %u ", vals[i], (unsigned)rates[i]);
+ fprintf(fp, "\n");
+ }
+}
+
+/* use communication definitions of meg/kilo etc */
+static const unsigned long long giga = 1000000000ull;
+static const unsigned long long mega = 1000000;
+static const unsigned long long kilo = 1000;
+
+void format_rate(FILE *fp, unsigned long long *vals, double *rates, int i)
+{
+ char temp[64];
+ if (vals[i] > giga)
+ fprintf(fp, "%7lluM ", vals[i]/mega);
+ else if (vals[i] > mega)
+ fprintf(fp, "%7lluK ", vals[i]/kilo);
+ else
+ fprintf(fp, "%8llu ", vals[i]);
+
+ if (rates[i] > mega) {
+ sprintf(temp, "%uM", (unsigned)(rates[i]/mega));
+ fprintf(fp, "%-6s ", temp);
+ } else if (rates[i] > kilo) {
+ sprintf(temp, "%uK", (unsigned)(rates[i]/kilo));
+ fprintf(fp, "%-6s ", temp);
+ } else
+ fprintf(fp, "%-6u ", (unsigned)rates[i]);
+}
+
+void format_pair(FILE *fp, unsigned long long *vals, int i, int k)
+{
+ char temp[64];
+ if (vals[i] > giga)
+ fprintf(fp, "%7lluM ", vals[i]/mega);
+ else if (vals[i] > mega)
+ fprintf(fp, "%7lluK ", vals[i]/kilo);
+ else
+ fprintf(fp, "%8llu ", vals[i]);
+
+ if (vals[k] > giga) {
+ sprintf(temp, "%uM", (unsigned)(vals[k]/mega));
+ fprintf(fp, "%-6s ", temp);
+ } else if (vals[k] > mega) {
+ sprintf(temp, "%uK", (unsigned)(vals[k]/kilo));
+ fprintf(fp, "%-6s ", temp);
+ } else
+ fprintf(fp, "%-6u ", (unsigned)vals[k]);
+}
+
+void print_head(FILE *fp)
+{
+ fprintf(fp, "#%s\n", info_source);
+ fprintf(fp, "%-15s ", "Interface");
+
+ fprintf(fp, "%8s/%-6s ", "RX Pkts", "Rate");
+ fprintf(fp, "%8s/%-6s ", "TX Pkts", "Rate");
+ fprintf(fp, "%8s/%-6s ", "RX Data", "Rate");
+ fprintf(fp, "%8s/%-6s\n","TX Data", "Rate");
+
+ if (!show_errors) {
+ fprintf(fp, "%-15s ", "");
+ fprintf(fp, "%8s/%-6s ", "RX Errs", "Drop");
+ fprintf(fp, "%8s/%-6s ", "TX Errs", "Drop");
+ fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
+ fprintf(fp, "%8s/%-6s\n","TX Coll", "Rate");
+ } else {
+ fprintf(fp, "%-15s ", "");
+ fprintf(fp, "%8s/%-6s ", "RX Errs", "Rate");
+ fprintf(fp, "%8s/%-6s ", "RX Drop", "Rate");
+ fprintf(fp, "%8s/%-6s ", "RX Over", "Rate");
+ fprintf(fp, "%8s/%-6s\n","RX Leng", "Rate");
+
+ fprintf(fp, "%-15s ", "");
+ fprintf(fp, "%8s/%-6s ", "RX Crc", "Rate");
+ fprintf(fp, "%8s/%-6s ", "RX Frm", "Rate");
+ fprintf(fp, "%8s/%-6s ", "RX Fifo", "Rate");
+ fprintf(fp, "%8s/%-6s\n","RX Miss", "Rate");
+
+ fprintf(fp, "%-15s ", "");
+ fprintf(fp, "%8s/%-6s ", "TX Errs", "Rate");
+ fprintf(fp, "%8s/%-6s ", "TX Drop", "Rate");
+ fprintf(fp, "%8s/%-6s ", "TX Coll", "Rate");
+ fprintf(fp, "%8s/%-6s\n","TX Carr", "Rate");
+
+ fprintf(fp, "%-15s ", "");
+ fprintf(fp, "%8s/%-6s ", "TX Abrt", "Rate");
+ fprintf(fp, "%8s/%-6s ", "TX Fifo", "Rate");
+ fprintf(fp, "%8s/%-6s ", "TX Hear", "Rate");
+ fprintf(fp, "%8s/%-6s\n","TX Wind", "Rate");
+ }
+}
+
+void print_one_if(FILE *fp, struct ifstat_ent *n, unsigned long long *vals)
+{
+ int i;
+ fprintf(fp, "%-15s ", n->name);
+ for (i=0; i<4; i++)
+ format_rate(fp, vals, n->rate, i);
+ fprintf(fp, "\n");
+
+ if (!show_errors) {
+ fprintf(fp, "%-15s ", "");
+ format_pair(fp, vals, 4, 6);
+ format_pair(fp, vals, 5, 7);
+ format_rate(fp, vals, n->rate, 11);
+ format_rate(fp, vals, n->rate, 9);
+ fprintf(fp, "\n");
+ } else {
+ fprintf(fp, "%-15s ", "");
+ format_rate(fp, vals, n->rate, 4);
+ format_rate(fp, vals, n->rate, 6);
+ format_rate(fp, vals, n->rate, 11);
+ format_rate(fp, vals, n->rate, 10);
+ fprintf(fp, "\n");
+
+ fprintf(fp, "%-15s ", "");
+ format_rate(fp, vals, n->rate, 12);
+ format_rate(fp, vals, n->rate, 13);
+ format_rate(fp, vals, n->rate, 14);
+ format_rate(fp, vals, n->rate, 15);
+ fprintf(fp, "\n");
+
+ fprintf(fp, "%-15s ", "");
+ format_rate(fp, vals, n->rate, 5);
+ format_rate(fp, vals, n->rate, 7);
+ format_rate(fp, vals, n->rate, 9);
+ format_rate(fp, vals, n->rate, 17);
+ fprintf(fp, "\n");
+
+ fprintf(fp, "%-15s ", "");
+ format_rate(fp, vals, n->rate, 16);
+ format_rate(fp, vals, n->rate, 18);
+ format_rate(fp, vals, n->rate, 19);
+ format_rate(fp, vals, n->rate, 20);
+ fprintf(fp, "\n");
+ }
+}
+
+
+void dump_kern_db(FILE *fp)
+{
+ struct ifstat_ent *n;
+
+ print_head(fp);
+
+ for (n=kern_db; n; n=n->next) {
+ if (!match(n->name))
+ continue;
+ print_one_if(fp, n, n->val);
+ }
+}
+
+
+void dump_incr_db(FILE *fp)
+{
+ struct ifstat_ent *n, *h;
+ h = hist_db;
+
+ print_head(fp);
+
+ for (n=kern_db; n; n=n->next) {
+ int i;
+ unsigned long long vals[MAXS];
+ struct ifstat_ent *h1;
+
+ memcpy(vals, n->val, sizeof(vals));
+
+ for (h1 = h; h1; h1 = h1->next) {
+ if (h1->ifindex == n->ifindex) {
+ for (i = 0; i < MAXS; i++)
+ vals[i] -= h1->val[i];
+ h = h1->next;
+ break;
+ }
+ }
+ if (!match(n->name))
+ continue;
+ print_one_if(fp, n, vals);
+ }
+}
+
+
+static int children;
+
+void sigchild(int signo)
+{
+}
+
+void update_db(int interval)
+{
+ struct ifstat_ent *n, *h;
+
+ n = kern_db;
+ kern_db = NULL;
+
+ load_info();
+
+ h = kern_db;
+ kern_db = n;
+
+ for (n = kern_db; n; n = n->next) {
+ struct ifstat_ent *h1;
+ for (h1 = h; h1; h1 = h1->next) {
+ if (h1->ifindex == n->ifindex) {
+ int i;
+ for (i = 0; i < MAXS; i++) {
+ if ((long)(h1->ival[i] - n->ival[i]) < 0) {
+ memset(n->ival, 0, sizeof(n->ival));
+ break;
+ }
+ }
+ for (i = 0; i < MAXS; i++) {
+ double sample;
+ unsigned long incr = h1->ival[i] - n->ival[i];
+ n->val[i] += incr;
+ n->ival[i] = h1->ival[i];
+ sample = (double)(incr*1000)/interval;
+ if (interval >= scan_interval) {
+ n->rate[i] += W*(sample-n->rate[i]);
+ } else if (interval >= 1000) {
+ if (interval >= time_constant) {
+ n->rate[i] = sample;
+ } else {
+ double w = W*(double)interval/scan_interval;
+ n->rate[i] += w*(sample-n->rate[i]);
+ }
+ }
+ }
+
+ while (h != h1) {
+ struct ifstat_ent *tmp = h;
+ h = h->next;
+ free(tmp->name);
+ free(tmp);
+ };
+ h = h1->next;
+ free(h1->name);
+ free(h1);
+ break;
+ }
+ }
+ }
+}
+
+#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
+
+
+void server_loop(int fd)
+{
+ struct timeval snaptime = { 0 };
+ struct pollfd p;
+ p.fd = fd;
+ p.events = p.revents = POLLIN;
+
+ sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
+ getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
+
+ load_info();
+
+ for (;;) {
+ int status;
+ int tdiff;
+ struct timeval now;
+
+ gettimeofday(&now, NULL);
+ tdiff = T_DIFF(now, snaptime);
+ if (tdiff >= scan_interval) {
+ update_db(tdiff);
+ snaptime = now;
+ tdiff = 0;
+ }
+
+ if (poll(&p, 1, tdiff + scan_interval) > 0
+ && (p.revents&POLLIN)) {
+ int clnt = accept(fd, NULL, NULL);
+ if (clnt >= 0) {
+ pid_t pid;
+ if (children >= 5) {
+ close(clnt);
+ } else if ((pid = fork()) != 0) {
+ if (pid>0)
+ children++;
+ close(clnt);
+ } else {
+ FILE *fp = fdopen(clnt, "w");
+ if (fp) {
+ if (tdiff > 0)
+ update_db(tdiff);
+ dump_raw_db(fp, 0);
+ }
+ exit(0);
+ }
+ }
+ }
+ while (children && waitpid(-1, &status, WNOHANG) > 0)
+ children--;
+ }
+}
+
+int verify_forging(int fd)
+{
+ struct ucred cred;
+ socklen_t olen = sizeof(cred);
+
+ if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
+ olen < sizeof(cred))
+ return -1;
+ if (cred.uid == getuid() || cred.uid == 0)
+ return 0;
+ return -1;
+}
+
+static void usage(void) __attribute__((noreturn));
+
+static void usage(void)
+{
+ fprintf(stderr,
+"Usage: ifstat [OPTION] [ PATTERN [ PATTERN ] ]\n"
+" -h, --help this message\n"
+" -a, --ignore ignore history\n"
+" -d, --scan=SECS sample every statistics every SECS\n"
+" -e, --errors show errors\n"
+" -n, --nooutput do history only\n"
+" -r, --reset reset history\n"
+" -s, --noupdate don;t update history\n"
+" -t, --interval=SECS report average over the last SECS\n"
+" -V, --version output version information\n"
+" -z, --zeros show entries with zero activity\n");
+
+ exit(-1);
+}
+
+static const struct option longopts[] = {
+ { "help", 0, 0, 'h' },
+ { "ignore", 0, 0, 'a' },
+ { "scan", 1, 0, 'd'},
+ { "errors", 0, 0, 'e' },
+ { "nooutput", 0, 0, 'n' },
+ { "reset", 0, 0, 'r' },
+ { "noupdate", 0, 0, 's' },
+ { "interval", 1, 0, 't' },
+ { "version", 0, 0, 'V' },
+ { "zeros", 0, 0, 'z' },
+ { 0 }
+};
+
+int main(int argc, char *argv[])
+{
+ char hist_name[128];
+ struct sockaddr_un sun;
+ FILE *hist_fp = NULL;
+ int ch;
+ int fd;
+
+ while ((ch = getopt_long(argc, argv, "hvVzrnasd:t:eK",
+ longopts, NULL)) != EOF) {
+ switch(ch) {
+ case 'z':
+ dump_zeros = 1;
+ break;
+ case 'r':
+ reset_history = 1;
+ break;
+ case 'a':
+ ignore_history = 1;
+ break;
+ case 's':
+ no_update = 1;
+ break;
+ case 'n':
+ no_output = 1;
+ break;
+ case 'e':
+ show_errors = 1;
+ break;
+ case 'd':
+ scan_interval = atoi(optarg) * 1000;
+ if (scan_interval <= 0) {
+ fprintf(stderr, "ifstat: invalid scan interval\n");
+ exit(-1);
+ }
+ break;
+ case 't':
+ time_constant = atoi(optarg);
+ if (time_constant <= 0) {
+ fprintf(stderr, "ifstat: invalid time constant divisor\n");
+ exit(-1);
+ }
+ break;
+ case 'v':
+ case 'V':
+ printf("ifstat utility, iproute2-ss%s\n", SNAPSHOT);
+ exit(0);
+ case 'h':
+ case '?':
+ default:
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ sun.sun_family = AF_UNIX;
+ sun.sun_path[0] = 0;
+ sprintf(sun.sun_path+1, "ifstat%d", getuid());
+
+ if (scan_interval > 0) {
+ if (time_constant == 0)
+ time_constant = 60;
+ time_constant *= 1000;
+ W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ perror("ifstat: socket");
+ exit(-1);
+ }
+ if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
+ perror("ifstat: bind");
+ exit(-1);
+ }
+ if (listen(fd, 5) < 0) {
+ perror("ifstat: listen");
+ exit(-1);
+ }
+ if (daemon(0, 0)) {
+ perror("ifstat: daemon");
+ exit(-1);
+ }
+ signal(SIGPIPE, SIG_IGN);
+ signal(SIGCHLD, sigchild);
+ server_loop(fd);
+ exit(0);
+ }
+
+ patterns = argv;
+ npatterns = argc;
+
+ if (getenv("IFSTAT_HISTORY"))
+ snprintf(hist_name, sizeof(hist_name),
+ "%s", getenv("IFSTAT_HISTORY"));
+ else
+ snprintf(hist_name, sizeof(hist_name),
+ "%s/.ifstat.u%d", P_tmpdir, getuid());
+
+ if (reset_history)
+ unlink(hist_name);
+
+ if (!ignore_history || !no_update) {
+ struct stat stb;
+
+ fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
+ if (fd < 0) {
+ perror("ifstat: open history file");
+ exit(-1);
+ }
+ if ((hist_fp = fdopen(fd, "r+")) == NULL) {
+ perror("ifstat: fdopen history file");
+ exit(-1);
+ }
+ if (flock(fileno(hist_fp), LOCK_EX)) {
+ perror("ifstat: flock history file");
+ exit(-1);
+ }
+ if (fstat(fileno(hist_fp), &stb) != 0) {
+ perror("ifstat: fstat history file");
+ exit(-1);
+ }
+ if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
+ fprintf(stderr, "ifstat: something is so wrong with history file, that I prefer not to proceed.\n");
+ exit(-1);
+ }
+ if (!ignore_history) {
+ FILE *tfp;
+ long uptime = -1;
+ if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
+ if (fscanf(tfp, "%ld", &uptime) != 1)
+ uptime = -1;
+ fclose(tfp);
+ }
+ if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
+ fprintf(stderr, "ifstat: history is aged out, resetting\n");
+ ftruncate(fileno(hist_fp), 0);
+ }
+ }
+
+ load_raw_table(hist_fp);
+
+ hist_db = kern_db;
+ kern_db = NULL;
+ }
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
+ (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
+ || (strcpy(sun.sun_path+1, "ifstat0"),
+ connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
+ && verify_forging(fd) == 0) {
+ FILE *sfp = fdopen(fd, "r");
+ load_raw_table(sfp);
+ if (hist_db && source_mismatch) {
+ fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ }
+ fclose(sfp);
+ } else {
+ if (fd >= 0)
+ close(fd);
+ if (hist_db && info_source[0] && strcmp(info_source, "kernel")) {
+ fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ info_source[0] = 0;
+ }
+ load_info();
+ if (info_source[0] == 0)
+ strcpy(info_source, "kernel");
+ }
+
+ if (!no_output) {
+ if (ignore_history || hist_db == NULL)
+ dump_kern_db(stdout);
+ else
+ dump_incr_db(stdout);
+ }
+ if (!no_update) {
+ ftruncate(fileno(hist_fp), 0);
+ rewind(hist_fp);
+ dump_raw_db(hist_fp, 1);
+ fflush(hist_fp);
+ }
+ exit(0);
+}
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/lnstat.c b/ap/app/iproute2/iproute2-3.4.0/misc/lnstat.c
new file mode 100755
index 0000000..bd19cc1
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/lnstat.c
@@ -0,0 +1,351 @@
+/* lnstat - Unified linux network statistics
+ *
+ * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
+ *
+ * Development of this code was funded by Astaro AG, http://www.astaro.com/
+ *
+ * Based on original concept and ideas from predecessor rtstat.c:
+ *
+ * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
+ * Uppsala University, Sweden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ */
+
+/* Maximum number of fields that can be displayed */
+#define MAX_FIELDS 128
+
+/* Maximum number of header lines */
+#define HDR_LINES 10
+
+/* default field width if none specified */
+#define FIELD_WIDTH_DEFAULT 8
+#define FIELD_WIDTH_MAX 20
+
+#define DEFAULT_INTERVAL 2
+
+#define HDR_LINE_LENGTH (MAX_FIELDS*FIELD_WIDTH_MAX)
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+
+#include "lnstat.h"
+
+static struct option opts[] = {
+ { "version", 0, NULL, 'V' },
+ { "count", 1, NULL, 'c' },
+ { "dump", 1, NULL, 'd' },
+ { "file", 1, NULL, 'f' },
+ { "help", 0, NULL, 'h' },
+ { "interval", 1, NULL, 'i' },
+ { "keys", 1, NULL, 'k' },
+ { "subject", 1, NULL, 's' },
+ { "width", 1, NULL, 'w' },
+};
+
+static int usage(char *name, int exit_code)
+{
+ fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
+ fprintf(stderr, "Copyright (C) 2004 by Harald Welte "
+ "<laforge@gnumonks.org>\n");
+ fprintf(stderr, "This program is free software licensed under GNU GPLv2"
+ "\nwith ABSOLUTELY NO WARRANTY.\n\n");
+ fprintf(stderr, "Parameters:\n");
+ fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n");
+ fprintf(stderr, "\t-c --count <count>\t"
+ "Print <count> number of intervals\n");
+ fprintf(stderr, "\t-d --dump\t\t"
+ "Dump list of available files/keys\n");
+ fprintf(stderr, "\t-f --file <file>\tStatistics file to use\n");
+ fprintf(stderr, "\t-h --help\t\tThis help message\n");
+ fprintf(stderr, "\t-i --interval <intv>\t"
+ "Set interval to 'intv' seconds\n");
+ fprintf(stderr, "\t-k --keys k,k,k,...\tDisplay only keys specified\n");
+ fprintf(stderr, "\t-s --subject [0-2]\t?\n");
+ fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n");
+ fprintf(stderr, "\n");
+
+ exit(exit_code);
+}
+
+struct field_param {
+ const char *name;
+ struct lnstat_field *lf;
+ struct {
+ unsigned int width;
+ } print;
+};
+
+struct field_params {
+ unsigned int num;
+ struct field_param params[MAX_FIELDS];
+};
+
+static void print_line(FILE *of, const struct lnstat_file *lnstat_files,
+ const struct field_params *fp)
+{
+ int i;
+
+ for (i = 0; i < fp->num; i++) {
+ struct lnstat_field *lf = fp->params[i].lf;
+ char formatbuf[255];
+
+ snprintf(formatbuf, sizeof(formatbuf)-1, "%%%ulu|",
+ fp->params[i].print.width);
+ fprintf(of, formatbuf, lf->result);
+ }
+ fputc('\n', of);
+}
+
+/* find lnstat_field according to user specification */
+static int map_field_params(struct lnstat_file *lnstat_files,
+ struct field_params *fps, int interval)
+{
+ int i, j = 0;
+ struct lnstat_file *lf;
+
+ /* no field specification on commandline, need to build default */
+ if (!fps->num) {
+ for (lf = lnstat_files; lf; lf = lf->next) {
+ for (i = 0; i < lf->num_fields; i++) {
+ fps->params[j].lf = &lf->fields[i];
+ fps->params[j].lf->file->interval.tv_sec =
+ interval;
+ if (!fps->params[j].print.width)
+ fps->params[j].print.width =
+ FIELD_WIDTH_DEFAULT;
+
+ if (++j >= MAX_FIELDS - 1) {
+ fprintf(stderr,
+ "WARN: MAX_FIELDS (%d) reached,"
+ " truncating number of keys\n",
+ MAX_FIELDS);
+ goto full;
+ }
+ }
+ }
+ full:
+ fps->num = j;
+ return 1;
+ }
+
+ for (i = 0; i < fps->num; i++) {
+ fps->params[i].lf = lnstat_find_field(lnstat_files,
+ fps->params[i].name);
+ if (!fps->params[i].lf) {
+ fprintf(stderr, "Field `%s' unknown\n",
+ fps->params[i].name);
+ return 0;
+ }
+ fps->params[i].lf->file->interval.tv_sec = interval;
+ if (!fps->params[i].print.width)
+ fps->params[i].print.width = FIELD_WIDTH_DEFAULT;
+ }
+ return 1;
+}
+
+struct table_hdr {
+ int num_lines;
+ char *hdr[HDR_LINES];
+};
+
+static struct table_hdr *build_hdr_string(struct lnstat_file *lnstat_files,
+ struct field_params *fps,
+ int linewidth)
+{
+ int h,i;
+ static struct table_hdr th;
+ int ofs = 0;
+
+ for (i = 0; i < HDR_LINES; i++) {
+ th.hdr[i] = malloc(HDR_LINE_LENGTH);
+ memset(th.hdr[i], 0, sizeof(th.hdr[i]));
+ }
+
+ for (i = 0; i < fps->num; i++) {
+ char *cname, *fname = fps->params[i].lf->name;
+ char fmt[12];
+ unsigned int width = fps->params[i].print.width;
+
+ snprintf(fmt, sizeof(fmt)-1, "%%%u.%us|", width, width);
+
+ snprintf(th.hdr[0]+ofs, width+2, fmt,
+ fps->params[i].lf->file->basename);
+
+ cname = fname;
+ for (h = 1; h < HDR_LINES; h++) {
+ if (cname - fname >= strlen(fname))
+ snprintf(th.hdr[h]+ofs, width+2, fmt, "");
+ else {
+ th.num_lines = h+1;
+ snprintf(th.hdr[h]+ofs, width+2, fmt, cname);
+ }
+ cname += width;
+ }
+ ofs += width+1;
+ }
+ /* fill in spaces */
+ for (h = 1; h <= th.num_lines; h++) {
+ for (i = 0; i < ofs; i++) {
+ if (th.hdr[h][i] == '\0')
+ th.hdr[h][i] = ' ';
+ }
+ }
+
+ return &th;
+}
+
+static int print_hdr(FILE *of, struct table_hdr *th)
+{
+ int i;
+
+ for (i = 0; i < th->num_lines; i++) {
+ fputs(th->hdr[i], of);
+ fputc('\n', of);
+ }
+ return 0;
+}
+
+
+int main(int argc, char **argv)
+{
+ struct lnstat_file *lnstat_files;
+ const char *basename;
+ int c;
+ int interval = DEFAULT_INTERVAL;
+ int hdr = 2;
+ enum {
+ MODE_DUMP,
+ MODE_NORMAL,
+ } mode = MODE_NORMAL;
+
+ unsigned long count = 1;
+ static struct field_params fp;
+ int num_req_files = 0;
+ char *req_files[LNSTAT_MAX_FILES];
+
+ /* backwards compatibility mode for old tools */
+ basename = strrchr(argv[0], '/');
+ if (basename)
+ basename += 1; /* name after slash */
+ else
+ basename = argv[0]; /* no slash */
+
+ if (!strcmp(basename, "rtstat")) {
+ /* rtstat compatibility mode */
+ req_files[0] = "rt_cache";
+ num_req_files = 1;
+ } else if (!strcmp(basename, "ctstat")) {
+ /* ctstat compatibility mode */
+ req_files[0] = "ip_conntrack";
+ num_req_files = 1;
+ }
+
+ while ((c = getopt_long(argc, argv,"Vc:df:h?i:k:s:w:",
+ opts, NULL)) != -1) {
+ int i, len = 0;
+ char *tmp, *tok;
+
+ switch (c) {
+ case 'c':
+ count = strtoul(optarg, NULL, 0);
+ break;
+ case 'd':
+ mode = MODE_DUMP;
+ break;
+ case 'f':
+ req_files[num_req_files++] = strdup(optarg);
+ break;
+ case '?':
+ case 'h':
+ usage(argv[0], 0);
+ break;
+ case 'i':
+ sscanf(optarg, "%u", &interval);
+ break;
+ case 'k':
+ tmp = strdup(optarg);
+ if (!tmp)
+ break;
+ for (tok = strtok(tmp, ",");
+ tok;
+ tok = strtok(NULL, ",")) {
+ if (fp.num >= MAX_FIELDS) {
+ fprintf(stderr,
+ "WARN: too many keys"
+ " requested: (%d max)\n",
+ MAX_FIELDS);
+ break;
+ }
+ fp.params[fp.num++].name = tok;
+ }
+ break;
+ case 's':
+ sscanf(optarg, "%u", &hdr);
+ break;
+ case 'w':
+ tmp = strdup(optarg);
+ if (!tmp)
+ break;
+ i = 0;
+ for (tok = strtok(tmp, ",");
+ tok;
+ tok = strtok(NULL, ",")) {
+ len = strtoul(tok, NULL, 0);
+ if (len > FIELD_WIDTH_MAX)
+ len = FIELD_WIDTH_MAX;
+ fp.params[i].print.width = len;
+ i++;
+ }
+ if (i == 1) {
+ for (i = 0; i < MAX_FIELDS; i++)
+ fp.params[i].print.width = len;
+ }
+ break;
+ default:
+ usage(argv[0], 1);
+ break;
+ }
+ }
+
+ lnstat_files = lnstat_scan_dir(PROC_NET_STAT, num_req_files,
+ (const char **) req_files);
+
+ switch (mode) {
+ int i;
+ struct table_hdr *header;
+ case MODE_DUMP:
+ lnstat_dump(stderr, lnstat_files);
+ break;
+ case MODE_NORMAL:
+
+ if (!map_field_params(lnstat_files, &fp, interval))
+ exit(1);
+
+ header = build_hdr_string(lnstat_files, &fp, 80);
+ if (!header)
+ exit(1);
+
+ if (interval < 1 )
+ interval=1;
+
+ for (i = 0; i < count; i++) {
+ if ((hdr > 1 && (! (i % 20))) || (hdr == 1 && i == 0))
+ print_hdr(stdout, header);
+ lnstat_update(lnstat_files);
+ print_line(stdout, lnstat_files, &fp);
+ fflush(stdout);
+ sleep(interval);
+ }
+ }
+
+ return 1;
+}
+
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/lnstat.h b/ap/app/iproute2/iproute2-3.4.0/misc/lnstat.h
new file mode 100755
index 0000000..06774ab
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/lnstat.h
@@ -0,0 +1,43 @@
+#ifndef _LNSTAT_H
+#define _LNSTAT_H
+
+#include <limits.h>
+
+#define LNSTAT_VERSION "0.02 041002"
+
+#define PROC_NET_STAT "/proc/net/stat"
+
+#define LNSTAT_MAX_FILES 32
+#define LNSTAT_MAX_FIELDS_PER_LINE 32
+#define LNSTAT_MAX_FIELD_NAME_LEN 32
+
+struct lnstat_file;
+
+struct lnstat_field {
+ struct lnstat_file *file;
+ unsigned int num; /* field number in line */
+ char name[LNSTAT_MAX_FIELD_NAME_LEN+1];
+ unsigned long values[2]; /* two buffers for values */
+ unsigned long result;
+};
+
+struct lnstat_file {
+ struct lnstat_file *next;
+ char path[PATH_MAX+1];
+ char basename[NAME_MAX+1];
+ struct timeval last_read; /* last time of read */
+ struct timeval interval; /* interval */
+ int compat; /* 1 == backwards compat mode */
+ FILE *fp;
+ unsigned int num_fields; /* number of fields */
+ struct lnstat_field fields[LNSTAT_MAX_FIELDS_PER_LINE];
+};
+
+
+struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
+ const char **req_files);
+int lnstat_update(struct lnstat_file *lnstat_files);
+int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files);
+struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
+ const char *name);
+#endif /* _LNSTAT_H */
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/lnstat_util.c b/ap/app/iproute2/iproute2-3.4.0/misc/lnstat_util.c
new file mode 100755
index 0000000..9492baf
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/lnstat_util.c
@@ -0,0 +1,329 @@
+/* lnstat.c: Unified linux network statistics
+ *
+ * Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>
+ *
+ * Development of this code was funded by Astaro AG, http://www.astaro.com/
+ *
+ * Based on original concept and ideas from predecessor rtstat.c:
+ *
+ * Copyright 2001 by Robert Olsson <robert.olsson@its.uu.se>
+ * Uppsala University, Sweden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <limits.h>
+#include <time.h>
+
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include "lnstat.h"
+
+/* size of temp buffer used to read lines from procfiles */
+#define FGETS_BUF_SIZE 1024
+
+
+#define RTSTAT_COMPAT_LINE "entries in_hit in_slow_tot in_no_route in_brd in_martian_dst in_martian_src out_hit out_slow_tot out_slow_mc gc_total gc_ignored gc_goal_miss gc_dst_overflow in_hlist_search out_hlist_search\n"
+
+/* Read (and summarize for SMP) the different stats vars. */
+static int scan_lines(struct lnstat_file *lf, int i)
+{
+ int j, num_lines = 0;
+
+ for (j = 0; j < lf->num_fields; j++)
+ lf->fields[j].values[i] = 0;
+
+ while(!feof(lf->fp)) {
+ char buf[FGETS_BUF_SIZE];
+ char *ptr = buf;
+
+ num_lines++;
+
+ fgets(buf, sizeof(buf)-1, lf->fp);
+ gettimeofday(&lf->last_read, NULL);
+
+ for (j = 0; j < lf->num_fields; j++) {
+ unsigned long f = strtoul(ptr, &ptr, 16);
+ if (j == 0)
+ lf->fields[j].values[i] = f;
+ else
+ lf->fields[j].values[i] += f;
+ }
+ }
+ return num_lines;
+}
+
+static int time_after(struct timeval *last,
+ struct timeval *tout,
+ struct timeval *now)
+{
+ if (now->tv_sec > last->tv_sec + tout->tv_sec)
+ return 1;
+
+ if (now->tv_sec == last->tv_sec + tout->tv_sec) {
+ if (now->tv_usec > last->tv_usec + tout->tv_usec)
+ return 1;
+ }
+
+ return 0;
+}
+
+int lnstat_update(struct lnstat_file *lnstat_files)
+{
+ struct lnstat_file *lf;
+ char buf[FGETS_BUF_SIZE];
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+
+ for (lf = lnstat_files; lf; lf = lf->next) {
+ if (time_after(&lf->last_read, &lf->interval, &tv)) {
+ int i;
+ struct lnstat_field *lfi;
+
+ rewind(lf->fp);
+ if (!lf->compat) {
+ /* skip first line */
+ fgets(buf, sizeof(buf)-1, lf->fp);
+ }
+ scan_lines(lf, 1);
+
+ for (i = 0, lfi = &lf->fields[i];
+ i < lf->num_fields; i++, lfi = &lf->fields[i]) {
+ if (i == 0)
+ lfi->result = lfi->values[1];
+ else
+ lfi->result = (lfi->values[1]-lfi->values[0])
+ / lf->interval.tv_sec;
+ }
+
+ rewind(lf->fp);
+ fgets(buf, sizeof(buf)-1, lf->fp);
+ scan_lines(lf, 0);
+ }
+ }
+
+ return 0;
+}
+
+/* scan first template line and fill in per-field data structures */
+static int __lnstat_scan_fields(struct lnstat_file *lf, char *buf)
+{
+ char *tok;
+ int i;
+
+ tok = strtok(buf, " \t\n");
+ for (i = 0; i < LNSTAT_MAX_FIELDS_PER_LINE; i++) {
+ lf->fields[i].file = lf;
+ strncpy(lf->fields[i].name, tok, LNSTAT_MAX_FIELD_NAME_LEN);
+ /* has to be null-terminate since we initialize to zero
+ * and field size is NAME_LEN + 1 */
+ tok = strtok(NULL, " \t\n");
+ if (!tok) {
+ lf->num_fields = i+1;
+ return 0;
+ }
+ }
+ return 0;
+}
+
+static int lnstat_scan_fields(struct lnstat_file *lf)
+{
+ char buf[FGETS_BUF_SIZE];
+
+ rewind(lf->fp);
+ fgets(buf, sizeof(buf)-1, lf->fp);
+
+ return __lnstat_scan_fields(lf, buf);
+}
+
+/* fake function emulating lnstat_scan_fields() for old kernels */
+static int lnstat_scan_compat_rtstat_fields(struct lnstat_file *lf)
+{
+ char buf[FGETS_BUF_SIZE];
+
+ strncpy(buf, RTSTAT_COMPAT_LINE, sizeof(buf)-1);
+
+ return __lnstat_scan_fields(lf, buf);
+}
+
+/* find out whether string 'name; is in given string array */
+static int name_in_array(const int num, const char **arr, const char *name)
+{
+ int i;
+ for (i = 0; i < num; i++) {
+ if (!strcmp(arr[i], name))
+ return 1;
+ }
+ return 0;
+}
+
+/* allocate lnstat_file and open given file */
+static struct lnstat_file *alloc_and_open(const char *path, const char *file)
+{
+ struct lnstat_file *lf;
+
+ /* allocate */
+ lf = malloc(sizeof(*lf));
+ if (!lf)
+ return NULL;
+
+ /* initialize */
+ memset(lf, 0, sizeof(*lf));
+
+ /* de->d_name is guaranteed to be <= NAME_MAX */
+ strcpy(lf->basename, file);
+ strcpy(lf->path, path);
+ strcat(lf->path, "/");
+ strcat(lf->path, lf->basename);
+
+ /* initialize to default */
+ lf->interval.tv_sec = 1;
+
+ /* open */
+ lf->fp = fopen(lf->path, "r");
+ if (!lf->fp) {
+ free(lf);
+ return NULL;
+ }
+
+ return lf;
+}
+
+
+/* lnstat_scan_dir - find and parse all available statistics files/fields */
+struct lnstat_file *lnstat_scan_dir(const char *path, const int num_req_files,
+ const char **req_files)
+{
+ DIR *dir;
+ struct lnstat_file *lnstat_files = NULL;
+ struct dirent *de;
+
+ if (!path)
+ path = PROC_NET_STAT;
+
+ dir = opendir(path);
+ if (!dir) {
+ struct lnstat_file *lf;
+ /* Old kernel, before /proc/net/stat was introduced */
+ fprintf(stderr, "Your kernel doesn't have lnstat support. ");
+
+ /* we only support rtstat, not multiple files */
+ if (num_req_files >= 2) {
+ fputc('\n', stderr);
+ return NULL;
+ }
+
+ /* we really only accept rt_cache */
+ if (num_req_files && !name_in_array(num_req_files,
+ req_files, "rt_cache")) {
+ fputc('\n', stderr);
+ return NULL;
+ }
+
+ fprintf(stderr, "Fallback to old rtstat-only operation\n");
+
+ lf = alloc_and_open("/proc/net", "rt_cache_stat");
+ if (!lf)
+ return NULL;
+ lf->compat = 1;
+ strncpy(lf->basename, "rt_cache", sizeof(lf->basename));
+
+ /* FIXME: support for old files */
+ if (lnstat_scan_compat_rtstat_fields(lf) < 0)
+ return NULL;
+
+ lf->next = lnstat_files;
+ lnstat_files = lf;
+ return lnstat_files;
+ }
+
+ while ((de = readdir(dir))) {
+ struct lnstat_file *lf;
+
+ if (de->d_type != DT_REG)
+ continue;
+
+ if (num_req_files && !name_in_array(num_req_files,
+ req_files, de->d_name))
+ continue;
+
+ lf = alloc_and_open(path, de->d_name);
+ if (!lf)
+ return NULL;
+
+ /* fill in field structure */
+ if (lnstat_scan_fields(lf) < 0)
+ return NULL;
+
+ /* prepend to global list */
+ lf->next = lnstat_files;
+ lnstat_files = lf;
+ }
+ closedir(dir);
+
+ return lnstat_files;
+}
+
+int lnstat_dump(FILE *outfd, struct lnstat_file *lnstat_files)
+{
+ struct lnstat_file *lf;
+
+ for (lf = lnstat_files; lf; lf = lf->next) {
+ int i;
+
+ fprintf(outfd, "%s:\n", lf->path);
+
+ for (i = 0; i < lf->num_fields; i++)
+ fprintf(outfd, "\t%2u: %s\n", i+1, lf->fields[i].name);
+
+ }
+ return 0;
+}
+
+struct lnstat_field *lnstat_find_field(struct lnstat_file *lnstat_files,
+ const char *name)
+{
+ struct lnstat_file *lf;
+ struct lnstat_field *ret = NULL;
+ const char *colon = strchr(name, ':');
+ char *file;
+ const char *field;
+
+ if (colon) {
+ file = strndup(name, colon-name);
+ field = colon+1;
+ } else {
+ file = NULL;
+ field = name;
+ }
+
+ for (lf = lnstat_files; lf; lf = lf->next) {
+ int i;
+
+ if (file && strcmp(file, lf->basename))
+ continue;
+
+ for (i = 0; i < lf->num_fields; i++) {
+ if (!strcmp(field, lf->fields[i].name)) {
+ ret = &lf->fields[i];
+ goto out;
+ }
+ }
+ }
+out:
+ if (file)
+ free(file);
+
+ return ret;
+}
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/nstat.c b/ap/app/iproute2/iproute2-3.4.0/misc/nstat.c
new file mode 100755
index 0000000..2f06ffd
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/nstat.c
@@ -0,0 +1,621 @@
+/*
+ * nstat.c handy utility to read counters /proc/net/netstat and snmp
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <sys/time.h>
+#include <fnmatch.h>
+#include <sys/file.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/poll.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <signal.h>
+#include <math.h>
+
+#include <SNAPSHOT.h>
+
+int dump_zeros = 0;
+int reset_history = 0;
+int ignore_history = 0;
+int no_output = 0;
+int no_update = 0;
+int scan_interval = 0;
+int time_constant = 0;
+double W;
+char **patterns;
+int npatterns;
+
+char info_source[128];
+int source_mismatch;
+
+static int generic_proc_open(const char *env, char *name)
+{
+ char store[128];
+ char *p = getenv(env);
+ if (!p) {
+ p = getenv("PROC_ROOT") ? : "/proc";
+ snprintf(store, sizeof(store)-1, "%s/%s", p, name);
+ p = store;
+ }
+ return open(p, O_RDONLY);
+}
+
+int net_netstat_open(void)
+{
+ return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
+}
+
+int net_snmp_open(void)
+{
+ return generic_proc_open("PROC_NET_SNMP", "net/snmp");
+}
+
+int net_snmp6_open(void)
+{
+ return generic_proc_open("PROC_NET_SNMP6", "net/snmp6");
+}
+
+struct nstat_ent
+{
+ struct nstat_ent *next;
+ char *id;
+ unsigned long long val;
+ unsigned long ival;
+ double rate;
+};
+
+struct nstat_ent *kern_db;
+struct nstat_ent *hist_db;
+
+char *useless_numbers[] = {
+"IpForwarding", "IpDefaultTTL",
+"TcpRtoAlgorithm", "TcpRtoMin", "TcpRtoMax",
+"TcpMaxConn", "TcpCurrEstab"
+};
+
+int useless_number(char *id)
+{
+ int i;
+ for (i=0; i<sizeof(useless_numbers)/sizeof(*useless_numbers); i++)
+ if (strcmp(id, useless_numbers[i]) == 0)
+ return 1;
+ return 0;
+}
+
+int match(char *id)
+{
+ int i;
+
+ if (npatterns == 0)
+ return 1;
+
+ for (i=0; i<npatterns; i++) {
+ if (!fnmatch(patterns[i], id, 0))
+ return 1;
+ }
+ return 0;
+}
+
+void load_good_table(FILE *fp)
+{
+ char buf[4096];
+ struct nstat_ent *db = NULL;
+ struct nstat_ent *n;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ int nr;
+ unsigned long long val;
+ double rate;
+ char idbuf[sizeof(buf)];
+ if (buf[0] == '#') {
+ buf[strlen(buf)-1] = 0;
+ if (info_source[0] && strcmp(info_source, buf+1))
+ source_mismatch = 1;
+ info_source[0] = 0;
+ strncat(info_source, buf+1, sizeof(info_source)-1);
+ continue;
+ }
+ /* idbuf is as big as buf, so this is safe */
+ nr = sscanf(buf, "%s%llu%lg", idbuf, &val, &rate);
+ if (nr < 2)
+ abort();
+ if (nr < 3)
+ rate = 0;
+ if (useless_number(idbuf))
+ continue;
+ if ((n = malloc(sizeof(*n))) == NULL)
+ abort();
+ n->id = strdup(idbuf);
+ n->ival = (unsigned long)val;
+ n->val = val;
+ n->rate = rate;
+ n->next = db;
+ db = n;
+ }
+
+ while (db) {
+ n = db;
+ db = db->next;
+ n->next = kern_db;
+ kern_db = n;
+ }
+}
+
+
+void load_ugly_table(FILE *fp)
+{
+ char buf[4096];
+ struct nstat_ent *db = NULL;
+ struct nstat_ent *n;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ char idbuf[sizeof(buf)];
+ int off;
+ char *p;
+
+ p = strchr(buf, ':');
+ if (!p)
+ abort();
+ *p = 0;
+ idbuf[0] = 0;
+ strncat(idbuf, buf, sizeof(idbuf) - 1);
+ off = p - buf;
+ p += 2;
+
+ while (*p) {
+ char *next;
+ if ((next = strchr(p, ' ')) != NULL)
+ *next++ = 0;
+ else if ((next = strchr(p, '\n')) != NULL)
+ *next++ = 0;
+ if (off < sizeof(idbuf)) {
+ idbuf[off] = 0;
+ strncat(idbuf, p, sizeof(idbuf) - off - 1);
+ }
+ n = malloc(sizeof(*n));
+ if (!n)
+ abort();
+ n->id = strdup(idbuf);
+ n->rate = 0;
+ n->next = db;
+ db = n;
+ p = next;
+ }
+ n = db;
+ if (fgets(buf, sizeof(buf), fp) == NULL)
+ abort();
+ do {
+ p = strrchr(buf, ' ');
+ if (!p)
+ abort();
+ *p = 0;
+ if (sscanf(p+1, "%lu", &n->ival) != 1)
+ abort();
+ n->val = n->ival;
+ /* Trick to skip "dummy" trailing ICMP MIB in 2.4 */
+ if (strcmp(idbuf, "IcmpOutAddrMaskReps") == 0)
+ idbuf[5] = 0;
+ else
+ n = n->next;
+ } while (p > buf + off + 2);
+ }
+
+ while (db) {
+ n = db;
+ db = db->next;
+ if (useless_number(n->id)) {
+ free(n->id);
+ free(n);
+ } else {
+ n->next = kern_db;
+ kern_db = n;
+ }
+ }
+}
+
+void load_snmp(void)
+{
+ FILE *fp = fdopen(net_snmp_open(), "r");
+ if (fp) {
+ load_ugly_table(fp);
+ fclose(fp);
+ }
+}
+
+void load_snmp6(void)
+{
+ FILE *fp = fdopen(net_snmp6_open(), "r");
+ if (fp) {
+ load_good_table(fp);
+ fclose(fp);
+ }
+}
+
+void load_netstat(void)
+{
+ FILE *fp = fdopen(net_netstat_open(), "r");
+ if (fp) {
+ load_ugly_table(fp);
+ fclose(fp);
+ }
+}
+
+void dump_kern_db(FILE *fp, int to_hist)
+{
+ struct nstat_ent *n, *h;
+ h = hist_db;
+ fprintf(fp, "#%s\n", info_source);
+ for (n=kern_db; n; n=n->next) {
+ unsigned long long val = n->val;
+ if (!dump_zeros && !val && !n->rate)
+ continue;
+ if (!match(n->id)) {
+ struct nstat_ent *h1;
+ if (!to_hist)
+ continue;
+ for (h1 = h; h1; h1 = h1->next) {
+ if (strcmp(h1->id, n->id) == 0) {
+ val = h1->val;
+ h = h1->next;
+ break;
+ }
+ }
+ }
+ fprintf(fp, "%-32s%-16llu%6.1f\n", n->id, val, n->rate);
+ }
+}
+
+void dump_incr_db(FILE *fp)
+{
+ struct nstat_ent *n, *h;
+ h = hist_db;
+ fprintf(fp, "#%s\n", info_source);
+ for (n=kern_db; n; n=n->next) {
+ int ovfl = 0;
+ unsigned long long val = n->val;
+ struct nstat_ent *h1;
+ for (h1 = h; h1; h1 = h1->next) {
+ if (strcmp(h1->id, n->id) == 0) {
+ if (val < h1->val) {
+ ovfl = 1;
+ val = h1->val;
+ }
+ val -= h1->val;
+ h = h1->next;
+ break;
+ }
+ }
+ if (!dump_zeros && !val && !n->rate)
+ continue;
+ if (!match(n->id))
+ continue;
+ fprintf(fp, "%-32s%-16llu%6.1f%s\n", n->id, val,
+ n->rate, ovfl?" (overflow)":"");
+ }
+}
+
+static int children;
+
+void sigchild(int signo)
+{
+}
+
+void update_db(int interval)
+{
+ struct nstat_ent *n, *h;
+
+ n = kern_db;
+ kern_db = NULL;
+
+ load_netstat();
+ load_snmp6();
+ load_snmp();
+
+ h = kern_db;
+ kern_db = n;
+
+ for (n = kern_db; n; n = n->next) {
+ struct nstat_ent *h1;
+ for (h1 = h; h1; h1 = h1->next) {
+ if (strcmp(h1->id, n->id) == 0) {
+ double sample;
+ unsigned long incr = h1->ival - n->ival;
+ n->val += incr;
+ n->ival = h1->ival;
+ sample = (double)(incr*1000)/interval;
+ if (interval >= scan_interval) {
+ n->rate += W*(sample-n->rate);
+ } else if (interval >= 1000) {
+ if (interval >= time_constant) {
+ n->rate = sample;
+ } else {
+ double w = W*(double)interval/scan_interval;
+ n->rate += w*(sample-n->rate);
+ }
+ }
+
+ while (h != h1) {
+ struct nstat_ent *tmp = h;
+ h = h->next;
+ free(tmp->id);
+ free(tmp);
+ };
+ h = h1->next;
+ free(h1->id);
+ free(h1);
+ break;
+ }
+ }
+ }
+}
+
+#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
+
+
+void server_loop(int fd)
+{
+ struct timeval snaptime = { 0 };
+ struct pollfd p;
+ p.fd = fd;
+ p.events = p.revents = POLLIN;
+
+ sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
+ getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);
+
+ load_netstat();
+ load_snmp6();
+ load_snmp();
+
+ for (;;) {
+ int status;
+ int tdiff;
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ tdiff = T_DIFF(now, snaptime);
+ if (tdiff >= scan_interval) {
+ update_db(tdiff);
+ snaptime = now;
+ tdiff = 0;
+ }
+ if (poll(&p, 1, tdiff + scan_interval) > 0
+ && (p.revents&POLLIN)) {
+ int clnt = accept(fd, NULL, NULL);
+ if (clnt >= 0) {
+ pid_t pid;
+ if (children >= 5) {
+ close(clnt);
+ } else if ((pid = fork()) != 0) {
+ if (pid>0)
+ children++;
+ close(clnt);
+ } else {
+ FILE *fp = fdopen(clnt, "w");
+ if (fp) {
+ if (tdiff > 0)
+ update_db(tdiff);
+ dump_kern_db(fp, 0);
+ }
+ exit(0);
+ }
+ }
+ }
+ while (children && waitpid(-1, &status, WNOHANG) > 0)
+ children--;
+ }
+}
+
+int verify_forging(int fd)
+{
+ struct ucred cred;
+ socklen_t olen = sizeof(cred);
+
+ if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
+ olen < sizeof(cred))
+ return -1;
+ if (cred.uid == getuid() || cred.uid == 0)
+ return 0;
+ return -1;
+}
+
+static void usage(void) __attribute__((noreturn));
+
+static void usage(void)
+{
+ fprintf(stderr,
+"Usage: nstat [ -h?vVzrnasd:t: ] [ PATTERN [ PATTERN ] ]\n"
+ );
+ exit(-1);
+}
+
+
+int main(int argc, char *argv[])
+{
+ char *hist_name;
+ struct sockaddr_un sun;
+ FILE *hist_fp = NULL;
+ int ch;
+ int fd;
+
+ while ((ch = getopt(argc, argv, "h?vVzrnasd:t:")) != EOF) {
+ switch(ch) {
+ case 'z':
+ dump_zeros = 1;
+ break;
+ case 'r':
+ reset_history = 1;
+ break;
+ case 'a':
+ ignore_history = 1;
+ break;
+ case 's':
+ no_update = 1;
+ break;
+ case 'n':
+ no_output = 1;
+ break;
+ case 'd':
+ scan_interval = 1000*atoi(optarg);
+ break;
+ case 't':
+ if (sscanf(optarg, "%d", &time_constant) != 1 ||
+ time_constant <= 0) {
+ fprintf(stderr, "nstat: invalid time constant divisor\n");
+ exit(-1);
+ }
+ break;
+ case 'v':
+ case 'V':
+ printf("nstat utility, iproute2-ss%s\n", SNAPSHOT);
+ exit(0);
+ case 'h':
+ case '?':
+ default:
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ sun.sun_family = AF_UNIX;
+ sun.sun_path[0] = 0;
+ sprintf(sun.sun_path+1, "nstat%d", getuid());
+
+ if (scan_interval > 0) {
+ if (time_constant == 0)
+ time_constant = 60;
+ time_constant *= 1000;
+ W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ perror("nstat: socket");
+ exit(-1);
+ }
+ if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
+ perror("nstat: bind");
+ exit(-1);
+ }
+ if (listen(fd, 5) < 0) {
+ perror("nstat: listen");
+ exit(-1);
+ }
+ if (daemon(0, 0)) {
+ perror("nstat: daemon");
+ exit(-1);
+ }
+ signal(SIGPIPE, SIG_IGN);
+ signal(SIGCHLD, sigchild);
+ server_loop(fd);
+ exit(0);
+ }
+
+ patterns = argv;
+ npatterns = argc;
+
+ if ((hist_name = getenv("NSTAT_HISTORY")) == NULL) {
+ hist_name = malloc(128);
+ sprintf(hist_name, "/tmp/.nstat.u%d", getuid());
+ }
+
+ if (reset_history)
+ unlink(hist_name);
+
+ if (!ignore_history || !no_update) {
+ struct stat stb;
+
+ fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
+ if (fd < 0) {
+ perror("nstat: open history file");
+ exit(-1);
+ }
+ if ((hist_fp = fdopen(fd, "r+")) == NULL) {
+ perror("nstat: fdopen history file");
+ exit(-1);
+ }
+ if (flock(fileno(hist_fp), LOCK_EX)) {
+ perror("nstat: flock history file");
+ exit(-1);
+ }
+ if (fstat(fileno(hist_fp), &stb) != 0) {
+ perror("nstat: fstat history file");
+ exit(-1);
+ }
+ if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
+ fprintf(stderr, "nstat: something is so wrong with history file, that I prefer not to proceed.\n");
+ exit(-1);
+ }
+ if (!ignore_history) {
+ FILE *tfp;
+ long uptime = -1;
+ if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
+ if (fscanf(tfp, "%ld", &uptime) != 1)
+ uptime = -1;
+ fclose(tfp);
+ }
+ if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
+ fprintf(stderr, "nstat: history is aged out, resetting\n");
+ ftruncate(fileno(hist_fp), 0);
+ }
+ }
+
+ load_good_table(hist_fp);
+
+ hist_db = kern_db;
+ kern_db = NULL;
+ }
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
+ (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
+ || (strcpy(sun.sun_path+1, "nstat0"),
+ connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
+ && verify_forging(fd) == 0) {
+ FILE *sfp = fdopen(fd, "r");
+ load_good_table(sfp);
+ if (hist_db && source_mismatch) {
+ fprintf(stderr, "nstat: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ }
+ fclose(sfp);
+ } else {
+ if (fd >= 0)
+ close(fd);
+ if (hist_db && info_source[0] && strcmp(info_source, "kernel")) {
+ fprintf(stderr, "nstat: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ info_source[0] = 0;
+ }
+ load_netstat();
+ load_snmp6();
+ load_snmp();
+ if (info_source[0] == 0)
+ strcpy(info_source, "kernel");
+ }
+
+ if (!no_output) {
+ if (ignore_history || hist_db == NULL)
+ dump_kern_db(stdout, 0);
+ else
+ dump_incr_db(stdout);
+ }
+ if (!no_update) {
+ ftruncate(fileno(hist_fp), 0);
+ rewind(hist_fp);
+ dump_kern_db(hist_fp, 1);
+ fflush(hist_fp);
+ }
+ exit(0);
+}
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/rtacct.c b/ap/app/iproute2/iproute2-3.4.0/misc/rtacct.c
new file mode 100755
index 0000000..49168bd
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/rtacct.c
@@ -0,0 +1,631 @@
+/*
+ * rtacct.c Applet to display contents of /proc/net/rt_acct.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <sys/time.h>
+#include <fnmatch.h>
+#include <sys/file.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/poll.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <signal.h>
+#include <math.h>
+
+#include "rt_names.h"
+
+#include <SNAPSHOT.h>
+
+int reset_history = 0;
+int ignore_history = 0;
+int no_output = 0;
+int no_update = 0;
+int scan_interval = 0;
+int time_constant = 0;
+int dump_zeros = 0;
+unsigned long magic_number = 0;
+double W;
+
+static int generic_proc_open(const char *env, const char *name)
+{
+ char store[1024];
+ char *p = getenv(env);
+ if (!p) {
+ p = getenv("PROC_ROOT") ? : "/proc";
+ snprintf(store, sizeof(store)-1, "%s/%s", p, name);
+ p = store;
+ }
+ return open(p, O_RDONLY);
+}
+
+int net_rtacct_open(void)
+{
+ return generic_proc_open("PROC_NET_RTACCT", "net/rt_acct");
+}
+
+__u32 rmap[256/4];
+
+struct rtacct_data
+{
+ __u32 ival[256*4];
+
+ unsigned long long val[256*4];
+ double rate[256*4];
+ char signature[128];
+};
+
+struct rtacct_data kern_db_static;
+
+struct rtacct_data *kern_db = &kern_db_static;
+struct rtacct_data *hist_db;
+
+void nread(int fd, char *buf, int tot)
+{
+ int count = 0;
+
+ while (count < tot) {
+ int n = read(fd, buf+count, tot-count);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ exit(-1);
+ }
+ if (n == 0)
+ exit(-1);
+ count += n;
+ }
+}
+
+
+__u32 *read_kern_table(__u32 *tbl)
+{
+ static __u32 *tbl_ptr;
+ int fd;
+
+ if (magic_number) {
+ if (tbl_ptr != NULL)
+ return tbl_ptr;
+
+ fd = open("/dev/mem", O_RDONLY);
+ if (fd < 0) {
+ perror("magic open");
+ exit(-1);
+ }
+ tbl_ptr = mmap(NULL, 4096,
+ PROT_READ,
+ MAP_SHARED,
+ fd, magic_number);
+ if ((unsigned long)tbl_ptr == ~0UL) {
+ perror("magic mmap");
+ exit(-1);
+ }
+ close(fd);
+ return tbl_ptr;
+ }
+
+ fd = net_rtacct_open();
+ if (fd >= 0) {
+ nread(fd, (char*)tbl, 256*16);
+ close(fd);
+ } else {
+ memset(tbl, 0, 256*16);
+ }
+ return tbl;
+}
+
+void format_rate(FILE *fp, double rate)
+{
+ char temp[64];
+
+ if (rate > 1024*1024) {
+ sprintf(temp, "%uM", (unsigned)rint(rate/(1024*1024)));
+ fprintf(fp, " %-10s", temp);
+ } else if (rate > 1024) {
+ sprintf(temp, "%uK", (unsigned)rint(rate/1024));
+ fprintf(fp, " %-10s", temp);
+ } else
+ fprintf(fp, " %-10u", (unsigned)rate);
+}
+
+void format_count(FILE *fp, unsigned long long val)
+{
+ if (val > 1024*1024*1024)
+ fprintf(fp, " %10lluM", val/(1024*1024));
+ else if (val > 1024*1024)
+ fprintf(fp, " %10lluK", val/1024);
+ else
+ fprintf(fp, " %10llu", val);
+}
+
+void dump_abs_db(FILE *fp)
+{
+ int realm;
+ char b1[16];
+
+ if (!no_output) {
+ fprintf(fp, "#%s\n", kern_db->signature);
+ fprintf(fp,
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"\n"
+ , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
+ fprintf(fp,
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"\n"
+ , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
+
+ }
+
+ for (realm=0; realm<256; realm++) {
+ int i;
+ unsigned long long *val;
+ double *rate;
+
+ if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
+ continue;
+
+ val = &kern_db->val[realm*4];
+ rate = &kern_db->rate[realm*4];
+
+ if (!dump_zeros &&
+ !val[0] && !rate[0] &&
+ !val[1] && !rate[1] &&
+ !val[2] && !rate[2] &&
+ !val[3] && !rate[3])
+ continue;
+
+ if (hist_db) {
+ memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
+ }
+
+ if (no_output)
+ continue;
+
+ fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
+ for (i = 0; i < 4; i++)
+ format_count(fp, val[i]);
+ fprintf(fp, "\n%-10s", "");
+ for (i = 0; i < 4; i++)
+ format_rate(fp, rate[i]);
+ fprintf(fp, "\n");
+ }
+}
+
+
+void dump_incr_db(FILE *fp)
+{
+ int k, realm;
+ char b1[16];
+
+ if (!no_output) {
+ fprintf(fp, "#%s\n", kern_db->signature);
+ fprintf(fp,
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"\n"
+ , "Realm", "BytesTo", "PktsTo", "BytesFrom", "PktsFrom");
+ fprintf(fp,
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"%-10s "
+"\n"
+ , "", "BPSTo", "PPSTo", "BPSFrom", "PPSFrom");
+ }
+
+ for (realm=0; realm<256; realm++) {
+ int ovfl = 0;
+ int i;
+ unsigned long long *val;
+ double *rate;
+ unsigned long long rval[4];
+
+ if (!(rmap[realm>>5] & (1<<(realm&0x1f))))
+ continue;
+
+ val = &kern_db->val[realm*4];
+ rate = &kern_db->rate[realm*4];
+
+ for (k=0; k<4; k++) {
+ rval[k] = val[k];
+ if (rval[k] < hist_db->val[realm*4+k])
+ ovfl = 1;
+ else
+ rval[k] -= hist_db->val[realm*4+k];
+ }
+ if (ovfl) {
+ for (k=0; k<4; k++)
+ rval[k] = val[k];
+ }
+ if (hist_db) {
+ memcpy(&hist_db->val[realm*4], val, sizeof(*val)*4);
+ }
+
+ if (no_output)
+ continue;
+
+ if (!dump_zeros &&
+ !rval[0] && !rate[0] &&
+ !rval[1] && !rate[1] &&
+ !rval[2] && !rate[2] &&
+ !rval[3] && !rate[3])
+ continue;
+
+
+ fprintf(fp, "%-10s", rtnl_rtrealm_n2a(realm, b1, sizeof(b1)));
+ for (i = 0; i < 4; i++)
+ format_count(fp, rval[i]);
+ fprintf(fp, "\n%-10s", "");
+ for (i = 0; i < 4; i++)
+ format_rate(fp, rate[i]);
+ fprintf(fp, "\n");
+ }
+}
+
+
+static int children;
+
+void sigchild(int signo)
+{
+}
+
+/* Server side only: read kernel data, update tables, calculate rates. */
+
+void update_db(int interval)
+{
+ int i;
+ __u32 *ival;
+ __u32 _ival[256*4];
+
+ ival = read_kern_table(_ival);
+
+ for (i=0; i<256*4; i++) {
+ double sample;
+ __u32 incr = ival[i] - kern_db->ival[i];
+
+ if (ival[i] == 0 && incr == 0 &&
+ kern_db->val[i] == 0 && kern_db->rate[i] == 0)
+ continue;
+
+ kern_db->val[i] += incr;
+ kern_db->ival[i] = ival[i];
+ sample = (double)(incr*1000)/interval;
+ if (interval >= scan_interval) {
+ kern_db->rate[i] += W*(sample-kern_db->rate[i]);
+ } else if (interval >= 1000) {
+ if (interval >= time_constant) {
+ kern_db->rate[i] = sample;
+ } else {
+ double w = W*(double)interval/scan_interval;
+ kern_db->rate[i] += w*(sample-kern_db->rate[i]);
+ }
+ }
+ }
+}
+
+void send_db(int fd)
+{
+ int tot = 0;
+
+ while (tot < sizeof(*kern_db)) {
+ int n = write(fd, ((char*)kern_db) + tot, sizeof(*kern_db)-tot);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ return;
+ }
+ tot += n;
+ }
+}
+
+
+
+#define T_DIFF(a,b) (((a).tv_sec-(b).tv_sec)*1000 + ((a).tv_usec-(b).tv_usec)/1000)
+
+
+void pad_kern_table(struct rtacct_data *dat, __u32 *ival)
+{
+ int i;
+ memset(dat->rate, 0, sizeof(dat->rate));
+ if (dat->ival != ival)
+ memcpy(dat->ival, ival, sizeof(dat->ival));
+ for (i=0; i<256*4; i++)
+ dat->val[i] = ival[i];
+}
+
+void server_loop(int fd)
+{
+ struct timeval snaptime = { 0 };
+ struct pollfd p;
+ p.fd = fd;
+ p.events = p.revents = POLLIN;
+
+ sprintf(kern_db->signature,
+ "%u.%lu sampling_interval=%d time_const=%d",
+ (unsigned) getpid(), (unsigned long)random(),
+ scan_interval/1000, time_constant/1000);
+
+ pad_kern_table(kern_db, read_kern_table(kern_db->ival));
+
+ for (;;) {
+ int status;
+ int tdiff;
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ tdiff = T_DIFF(now, snaptime);
+ if (tdiff >= scan_interval) {
+ update_db(tdiff);
+ snaptime = now;
+ tdiff = 0;
+ }
+ if (poll(&p, 1, tdiff + scan_interval) > 0
+ && (p.revents&POLLIN)) {
+ int clnt = accept(fd, NULL, NULL);
+ if (clnt >= 0) {
+ pid_t pid;
+ if (children >= 5) {
+ close(clnt);
+ } else if ((pid = fork()) != 0) {
+ if (pid>0)
+ children++;
+ close(clnt);
+ } else {
+ if (tdiff > 0)
+ update_db(tdiff);
+ send_db(clnt);
+ exit(0);
+ }
+ }
+ }
+ while (children && waitpid(-1, &status, WNOHANG) > 0)
+ children--;
+ }
+}
+
+int verify_forging(int fd)
+{
+ struct ucred cred;
+ socklen_t olen = sizeof(cred);
+
+ if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, (void*)&cred, &olen) ||
+ olen < sizeof(cred))
+ return -1;
+ if (cred.uid == getuid() || cred.uid == 0)
+ return 0;
+ return -1;
+}
+
+static void usage(void) __attribute__((noreturn));
+
+static void usage(void)
+{
+ fprintf(stderr,
+"Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]\n"
+ );
+ exit(-1);
+}
+
+int main(int argc, char *argv[])
+{
+ char hist_name[128];
+ struct sockaddr_un sun;
+ int ch;
+ int fd;
+
+ while ((ch = getopt(argc, argv, "h?vVzrM:nasd:t:")) != EOF) {
+ switch(ch) {
+ case 'z':
+ dump_zeros = 1;
+ break;
+ case 'r':
+ reset_history = 1;
+ break;
+ case 'a':
+ ignore_history = 1;
+ break;
+ case 's':
+ no_update = 1;
+ break;
+ case 'n':
+ no_output = 1;
+ break;
+ case 'd':
+ scan_interval = 1000*atoi(optarg);
+ break;
+ case 't':
+ if (sscanf(optarg, "%d", &time_constant) != 1 ||
+ time_constant <= 0) {
+ fprintf(stderr, "rtacct: invalid time constant divisor\n");
+ exit(-1);
+ }
+ break;
+ case 'v':
+ case 'V':
+ printf("rtacct utility, iproute2-ss%s\n", SNAPSHOT);
+ exit(0);
+ case 'M':
+ /* Some secret undocumented option, nobody
+ * is expected to ask about its sense. See?
+ */
+ sscanf(optarg, "%lx", &magic_number);
+ break;
+ case 'h':
+ case '?':
+ default:
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc) {
+ while (argc > 0) {
+ __u32 realm;
+ if (rtnl_rtrealm_a2n(&realm, argv[0])) {
+ fprintf(stderr, "Warning: realm \"%s\" does not exist.\n", argv[0]);
+ exit(-1);
+ }
+ rmap[realm>>5] |= (1<<(realm&0x1f));
+ argc--; argv++;
+ }
+ } else {
+ memset(rmap, ~0, sizeof(rmap));
+ /* Always suppress zeros. */
+ dump_zeros = 0;
+ }
+
+ sun.sun_family = AF_UNIX;
+ sun.sun_path[0] = 0;
+ sprintf(sun.sun_path+1, "rtacct%d", getuid());
+
+ if (scan_interval > 0) {
+ if (time_constant == 0)
+ time_constant = 60;
+ time_constant *= 1000;
+ W = 1 - 1/exp(log(10)*(double)scan_interval/time_constant);
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ perror("rtacct: socket");
+ exit(-1);
+ }
+ if (bind(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) < 0) {
+ perror("rtacct: bind");
+ exit(-1);
+ }
+ if (listen(fd, 5) < 0) {
+ perror("rtacct: listen");
+ exit(-1);
+ }
+ if (daemon(0, 0)) {
+ perror("rtacct: daemon");
+ exit(-1);
+ }
+ signal(SIGPIPE, SIG_IGN);
+ signal(SIGCHLD, sigchild);
+ server_loop(fd);
+ exit(0);
+ }
+
+ if (getenv("RTACCT_HISTORY"))
+ snprintf(hist_name, sizeof(hist_name), "%s", getenv("RTACCT_HISTORY"));
+ else
+ sprintf(hist_name, "/tmp/.rtacct.u%d", getuid());
+
+ if (reset_history)
+ unlink(hist_name);
+
+ if (!ignore_history || !no_update) {
+ struct stat stb;
+
+ fd = open(hist_name, O_RDWR|O_CREAT|O_NOFOLLOW, 0600);
+ if (fd < 0) {
+ perror("rtacct: open history file");
+ exit(-1);
+ }
+ if (flock(fd, LOCK_EX)) {
+ perror("rtacct: flock history file");
+ exit(-1);
+ }
+ if (fstat(fd, &stb) != 0) {
+ perror("rtacct: fstat history file");
+ exit(-1);
+ }
+ if (stb.st_nlink != 1 || stb.st_uid != getuid()) {
+ fprintf(stderr, "rtacct: something is so wrong with history file, that I prefer not to proceed.\n");
+ exit(-1);
+ }
+ if (stb.st_size != sizeof(*hist_db))
+ if (write(fd, kern_db, sizeof(*hist_db)) < 0) {
+ perror("rtacct: write history file");
+ exit(-1);
+ }
+
+ hist_db = mmap(NULL, sizeof(*hist_db),
+ PROT_READ|PROT_WRITE,
+ no_update ? MAP_PRIVATE : MAP_SHARED,
+ fd, 0);
+
+ if ((unsigned long)hist_db == ~0UL) {
+ perror("mmap");
+ exit(-1);
+ }
+
+ if (!ignore_history) {
+ FILE *tfp;
+ long uptime = -1;
+ if ((tfp = fopen("/proc/uptime", "r")) != NULL) {
+ if (fscanf(tfp, "%ld", &uptime) != 1)
+ uptime = -1;
+ fclose(tfp);
+ }
+
+ if (uptime >= 0 && time(NULL) >= stb.st_mtime+uptime) {
+ fprintf(stderr, "rtacct: history is aged out, resetting\n");
+ memset(hist_db, 0, sizeof(*hist_db));
+ }
+ }
+
+ close(fd);
+ }
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0 &&
+ (connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0
+ || (strcpy(sun.sun_path+1, "rtacct0"),
+ connect(fd, (struct sockaddr*)&sun, 2+1+strlen(sun.sun_path+1)) == 0))
+ && verify_forging(fd) == 0) {
+ nread(fd, (char*)kern_db, sizeof(*kern_db));
+ if (hist_db && hist_db->signature[0] &&
+ strcmp(kern_db->signature, hist_db->signature)) {
+ fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ }
+ close(fd);
+ } else {
+ if (fd >= 0)
+ close(fd);
+
+ if (hist_db && hist_db->signature[0] &&
+ strcmp(hist_db->signature, "kernel")) {
+ fprintf(stderr, "rtacct: history is stale, ignoring it.\n");
+ hist_db = NULL;
+ }
+
+ pad_kern_table(kern_db, read_kern_table(kern_db->ival));
+ strcpy(kern_db->signature, "kernel");
+ }
+
+ if (ignore_history || hist_db == NULL)
+ dump_abs_db(stdout);
+ else
+ dump_incr_db(stdout);
+
+ exit(0);
+}
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/ss.c b/ap/app/iproute2/iproute2-3.4.0/misc/ss.c
new file mode 100755
index 0000000..cf529ef
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/ss.c
@@ -0,0 +1,3046 @@
+/*
+ * ss.c "sockstat", socket statistics
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <syslog.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#include <netinet/in.h>
+#include <string.h>
+#include <errno.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <resolv.h>
+#include <dirent.h>
+#include <fnmatch.h>
+#include <getopt.h>
+
+#include "utils.h"
+#include "rt_names.h"
+#include "ll_map.h"
+#include "libnetlink.h"
+#include "SNAPSHOT.h"
+
+#include <netinet/tcp.h>
+#include <linux/sock_diag.h>
+#include <linux/inet_diag.h>
+#include <linux/unix_diag.h>
+
+int resolve_hosts = 0;
+int resolve_services = 1;
+int preferred_family = AF_UNSPEC;
+int show_options = 0;
+int show_details = 0;
+int show_users = 0;
+int show_mem = 0;
+int show_tcpinfo = 0;
+
+int netid_width;
+int state_width;
+int addrp_width;
+int addr_width;
+int serv_width;
+int screen_width;
+
+static const char *TCP_PROTO = "tcp";
+static const char *UDP_PROTO = "udp";
+static const char *RAW_PROTO = "raw";
+static const char *dg_proto = NULL;
+
+enum
+{
+ TCP_DB,
+ DCCP_DB,
+ UDP_DB,
+ RAW_DB,
+ UNIX_DG_DB,
+ UNIX_ST_DB,
+ PACKET_DG_DB,
+ PACKET_R_DB,
+ NETLINK_DB,
+ MAX_DB
+};
+
+#define PACKET_DBM ((1<<PACKET_DG_DB)|(1<<PACKET_R_DB))
+#define UNIX_DBM ((1<<UNIX_DG_DB)|(1<<UNIX_ST_DB))
+#define ALL_DB ((1<<MAX_DB)-1)
+
+enum {
+ SS_UNKNOWN,
+ SS_ESTABLISHED,
+ SS_SYN_SENT,
+ SS_SYN_RECV,
+ SS_FIN_WAIT1,
+ SS_FIN_WAIT2,
+ SS_TIME_WAIT,
+ SS_CLOSE,
+ SS_CLOSE_WAIT,
+ SS_LAST_ACK,
+ SS_LISTEN,
+ SS_CLOSING,
+ SS_MAX
+};
+
+#define SS_ALL ((1<<SS_MAX)-1)
+
+#include "ssfilter.h"
+
+struct filter
+{
+ int dbs;
+ int states;
+ int families;
+ struct ssfilter *f;
+};
+
+struct filter default_filter = {
+ .dbs = (1<<TCP_DB),
+ .states = SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
+ .families= (1<<AF_INET)|(1<<AF_INET6),
+};
+
+struct filter current_filter;
+
+static FILE *generic_proc_open(const char *env, const char *name)
+{
+ const char *p = getenv(env);
+ char store[128];
+
+ if (!p) {
+ p = getenv("PROC_ROOT") ? : "/proc";
+ snprintf(store, sizeof(store)-1, "%s/%s", p, name);
+ p = store;
+ }
+
+ return fopen(p, "r");
+}
+
+static FILE *net_tcp_open(void)
+{
+ return generic_proc_open("PROC_NET_TCP", "net/tcp");
+}
+
+static FILE *net_tcp6_open(void)
+{
+ return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
+}
+
+static FILE *net_udp_open(void)
+{
+ return generic_proc_open("PROC_NET_UDP", "net/udp");
+}
+
+static FILE *net_udp6_open(void)
+{
+ return generic_proc_open("PROC_NET_UDP6", "net/udp6");
+}
+
+static FILE *net_raw_open(void)
+{
+ return generic_proc_open("PROC_NET_RAW", "net/raw");
+}
+
+static FILE *net_raw6_open(void)
+{
+ return generic_proc_open("PROC_NET_RAW6", "net/raw6");
+}
+
+static FILE *net_unix_open(void)
+{
+ return generic_proc_open("PROC_NET_UNIX", "net/unix");
+}
+
+static FILE *net_packet_open(void)
+{
+ return generic_proc_open("PROC_NET_PACKET", "net/packet");
+}
+
+static FILE *net_netlink_open(void)
+{
+ return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
+}
+
+static FILE *slabinfo_open(void)
+{
+ return generic_proc_open("PROC_SLABINFO", "slabinfo");
+}
+
+static FILE *net_sockstat_open(void)
+{
+ return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
+}
+
+static FILE *net_sockstat6_open(void)
+{
+ return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
+}
+
+static FILE *net_snmp_open(void)
+{
+ return generic_proc_open("PROC_NET_SNMP", "net/snmp");
+}
+
+static FILE *ephemeral_ports_open(void)
+{
+ return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
+}
+
+struct user_ent {
+ struct user_ent *next;
+ unsigned int ino;
+ int pid;
+ int fd;
+ char process[0];
+};
+
+#define USER_ENT_HASH_SIZE 256
+struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE];
+
+static int user_ent_hashfn(unsigned int ino)
+{
+ int val = (ino >> 24) ^ (ino >> 16) ^ (ino >> 8) ^ ino;
+
+ return val & (USER_ENT_HASH_SIZE - 1);
+}
+
+static void user_ent_add(unsigned int ino, const char *process, int pid, int fd)
+{
+ struct user_ent *p, **pp;
+ int str_len;
+
+ str_len = strlen(process) + 1;
+ p = malloc(sizeof(struct user_ent) + str_len);
+ if (!p)
+ abort();
+ p->next = NULL;
+ p->ino = ino;
+ p->pid = pid;
+ p->fd = fd;
+ strcpy(p->process, process);
+
+ pp = &user_ent_hash[user_ent_hashfn(ino)];
+ p->next = *pp;
+ *pp = p;
+}
+
+static void user_ent_hash_build(void)
+{
+ const char *root = getenv("PROC_ROOT") ? : "/proc/";
+ struct dirent *d;
+ char name[1024];
+ int nameoff;
+ DIR *dir;
+
+ strcpy(name, root);
+ if (strlen(name) == 0 || name[strlen(name)-1] != '/')
+ strcat(name, "/");
+
+ nameoff = strlen(name);
+
+ dir = opendir(name);
+ if (!dir)
+ return;
+
+ while ((d = readdir(dir)) != NULL) {
+ struct dirent *d1;
+ char process[16];
+ int pid, pos;
+ DIR *dir1;
+ char crap;
+
+ if (sscanf(d->d_name, "%d%c", &pid, &crap) != 1)
+ continue;
+
+ sprintf(name + nameoff, "%d/fd/", pid);
+ pos = strlen(name);
+ if ((dir1 = opendir(name)) == NULL)
+ continue;
+
+ process[0] = '\0';
+
+ while ((d1 = readdir(dir1)) != NULL) {
+ const char *pattern = "socket:[";
+ unsigned int ino;
+ char lnk[64];
+ int fd;
+ ssize_t link_len;
+
+ if (sscanf(d1->d_name, "%d%c", &fd, &crap) != 1)
+ continue;
+
+ sprintf(name+pos, "%d", fd);
+
+ link_len = readlink(name, lnk, sizeof(lnk)-1);
+ if (link_len == -1)
+ continue;
+ lnk[link_len] = '\0';
+
+ if (strncmp(lnk, pattern, strlen(pattern)))
+ continue;
+
+ sscanf(lnk, "socket:[%u]", &ino);
+
+ if (process[0] == '\0') {
+ char tmp[1024];
+ FILE *fp;
+
+ snprintf(tmp, sizeof(tmp), "%s/%d/stat", root, pid);
+ if ((fp = fopen(tmp, "r")) != NULL) {
+ fscanf(fp, "%*d (%[^)])", process);
+ fclose(fp);
+ }
+ }
+
+ user_ent_add(ino, process, pid, fd);
+ }
+ closedir(dir1);
+ }
+ closedir(dir);
+}
+
+int find_users(unsigned ino, char *buf, int buflen)
+{
+ struct user_ent *p;
+ int cnt = 0;
+ char *ptr;
+
+ if (!ino)
+ return 0;
+
+ p = user_ent_hash[user_ent_hashfn(ino)];
+ ptr = buf;
+ while (p) {
+ if (p->ino != ino)
+ goto next;
+
+ if (ptr - buf >= buflen - 1)
+ break;
+
+ snprintf(ptr, buflen - (ptr - buf),
+ "(\"%s\",%d,%d),",
+ p->process, p->pid, p->fd);
+ ptr += strlen(ptr);
+ cnt++;
+
+ next:
+ p = p->next;
+ }
+
+ if (ptr != buf)
+ ptr[-1] = '\0';
+
+ return cnt;
+}
+
+/* Get stats from slab */
+
+struct slabstat
+{
+ int socks;
+ int tcp_ports;
+ int tcp_tws;
+ int tcp_syns;
+ int skbs;
+};
+
+struct slabstat slabstat;
+
+static const char *slabstat_ids[] =
+{
+ "sock",
+ "tcp_bind_bucket",
+ "tcp_tw_bucket",
+ "tcp_open_request",
+ "skbuff_head_cache",
+};
+
+int get_slabstat(struct slabstat *s)
+{
+ char buf[256];
+ FILE *fp;
+ int cnt;
+
+ memset(s, 0, sizeof(*s));
+
+ fp = slabinfo_open();
+ if (!fp)
+ return -1;
+
+ cnt = sizeof(*s)/sizeof(int);
+
+ fgets(buf, sizeof(buf), fp);
+ while(fgets(buf, sizeof(buf), fp) != NULL) {
+ int i;
+ for (i=0; i<sizeof(slabstat_ids)/sizeof(slabstat_ids[0]); i++) {
+ if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) {
+ sscanf(buf, "%*s%d", ((int *)s) + i);
+ cnt--;
+ break;
+ }
+ }
+ if (cnt <= 0)
+ break;
+ }
+
+ fclose(fp);
+ return 0;
+}
+
+static const char *sstate_name[] = {
+ "UNKNOWN",
+ [TCP_ESTABLISHED] = "ESTAB",
+ [TCP_SYN_SENT] = "SYN-SENT",
+ [TCP_SYN_RECV] = "SYN-RECV",
+ [TCP_FIN_WAIT1] = "FIN-WAIT-1",
+ [TCP_FIN_WAIT2] = "FIN-WAIT-2",
+ [TCP_TIME_WAIT] = "TIME-WAIT",
+ [TCP_CLOSE] = "UNCONN",
+ [TCP_CLOSE_WAIT] = "CLOSE-WAIT",
+ [TCP_LAST_ACK] = "LAST-ACK",
+ [TCP_LISTEN] = "LISTEN",
+ [TCP_CLOSING] = "CLOSING",
+};
+
+static const char *sstate_namel[] = {
+ "UNKNOWN",
+ [TCP_ESTABLISHED] = "established",
+ [TCP_SYN_SENT] = "syn-sent",
+ [TCP_SYN_RECV] = "syn-recv",
+ [TCP_FIN_WAIT1] = "fin-wait-1",
+ [TCP_FIN_WAIT2] = "fin-wait-2",
+ [TCP_TIME_WAIT] = "time-wait",
+ [TCP_CLOSE] = "unconnected",
+ [TCP_CLOSE_WAIT] = "close-wait",
+ [TCP_LAST_ACK] = "last-ack",
+ [TCP_LISTEN] = "listening",
+ [TCP_CLOSING] = "closing",
+};
+
+struct tcpstat
+{
+ inet_prefix local;
+ inet_prefix remote;
+ int lport;
+ int rport;
+ int state;
+ int rq, wq;
+ int timer;
+ int timeout;
+ int retrs;
+ unsigned ino;
+ int probes;
+ unsigned uid;
+ int refcnt;
+ unsigned long long sk;
+ int rto, ato, qack, cwnd, ssthresh;
+};
+
+static const char *tmr_name[] = {
+ "off",
+ "on",
+ "keepalive",
+ "timewait",
+ "persist",
+ "unknown"
+};
+
+const char *print_ms_timer(int timeout)
+{
+ static char buf[64];
+ int secs, msecs, minutes;
+ if (timeout < 0)
+ timeout = 0;
+ secs = timeout/1000;
+ minutes = secs/60;
+ secs = secs%60;
+ msecs = timeout%1000;
+ buf[0] = 0;
+ if (minutes) {
+ msecs = 0;
+ snprintf(buf, sizeof(buf)-16, "%dmin", minutes);
+ if (minutes > 9)
+ secs = 0;
+ }
+ if (secs) {
+ if (secs > 9)
+ msecs = 0;
+ sprintf(buf+strlen(buf), "%d%s", secs, msecs ? "." : "sec");
+ }
+ if (msecs)
+ sprintf(buf+strlen(buf), "%03dms", msecs);
+ return buf;
+}
+
+const char *print_hz_timer(int timeout)
+{
+ int hz = get_user_hz();
+ return print_ms_timer(((timeout*1000) + hz-1)/hz);
+}
+
+struct scache
+{
+ struct scache *next;
+ int port;
+ char *name;
+ const char *proto;
+};
+
+struct scache *rlist;
+
+void init_service_resolver(void)
+{
+ char buf[128];
+ FILE *fp = popen("/usr/sbin/rpcinfo -p 2>/dev/null", "r");
+ if (fp) {
+ fgets(buf, sizeof(buf), fp);
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ unsigned int progn, port;
+ char proto[128], prog[128];
+ if (sscanf(buf, "%u %*d %s %u %s", &progn, proto,
+ &port, prog+4) == 4) {
+ struct scache *c = malloc(sizeof(*c));
+ if (c) {
+ c->port = port;
+ memcpy(prog, "rpc.", 4);
+ c->name = strdup(prog);
+ if (strcmp(proto, TCP_PROTO) == 0)
+ c->proto = TCP_PROTO;
+ else if (strcmp(proto, UDP_PROTO) == 0)
+ c->proto = UDP_PROTO;
+ else
+ c->proto = NULL;
+ c->next = rlist;
+ rlist = c;
+ }
+ }
+ }
+ pclose(fp);
+ }
+}
+
+static int ip_local_port_min, ip_local_port_max;
+
+/* Even do not try default linux ephemeral port ranges:
+ * default /etc/services contains so much of useless crap
+ * wouldbe "allocated" to this area that resolution
+ * is really harmful. I shrug each time when seeing
+ * "socks" or "cfinger" in dumps.
+ */
+static int is_ephemeral(int port)
+{
+ if (!ip_local_port_min) {
+ FILE *f = ephemeral_ports_open();
+ if (f) {
+ fscanf(f, "%d %d",
+ &ip_local_port_min, &ip_local_port_max);
+ fclose(f);
+ } else {
+ ip_local_port_min = 1024;
+ ip_local_port_max = 4999;
+ }
+ }
+
+ return (port >= ip_local_port_min && port<= ip_local_port_max);
+}
+
+
+const char *__resolve_service(int port)
+{
+ struct scache *c;
+
+ for (c = rlist; c; c = c->next) {
+ if (c->port == port && c->proto == dg_proto)
+ return c->name;
+ }
+
+ if (!is_ephemeral(port)) {
+ static int notfirst;
+ struct servent *se;
+ if (!notfirst) {
+ setservent(1);
+ notfirst = 1;
+ }
+ se = getservbyport(htons(port), dg_proto);
+ if (se)
+ return se->s_name;
+ }
+
+ return NULL;
+}
+
+
+const char *resolve_service(int port)
+{
+ static char buf[128];
+ static struct scache cache[256];
+
+ if (port == 0) {
+ buf[0] = '*';
+ buf[1] = 0;
+ return buf;
+ }
+
+ if (resolve_services) {
+ if (dg_proto == RAW_PROTO) {
+ return inet_proto_n2a(port, buf, sizeof(buf));
+ } else {
+ struct scache *c;
+ const char *res;
+ int hash = (port^(((unsigned long)dg_proto)>>2))&255;
+
+ for (c = &cache[hash]; c; c = c->next) {
+ if (c->port == port &&
+ c->proto == dg_proto) {
+ if (c->name)
+ return c->name;
+ goto do_numeric;
+ }
+ }
+
+ if ((res = __resolve_service(port)) != NULL) {
+ if ((c = malloc(sizeof(*c))) == NULL)
+ goto do_numeric;
+ } else {
+ c = &cache[hash];
+ if (c->name)
+ free(c->name);
+ }
+ c->port = port;
+ c->name = NULL;
+ c->proto = dg_proto;
+ if (res) {
+ c->name = strdup(res);
+ c->next = cache[hash].next;
+ cache[hash].next = c;
+ }
+ if (c->name)
+ return c->name;
+ }
+ }
+
+ do_numeric:
+ sprintf(buf, "%u", port);
+ return buf;
+}
+
+void formatted_print(const inet_prefix *a, int port)
+{
+ char buf[1024];
+ const char *ap = buf;
+ int est_len;
+
+ est_len = addr_width;
+
+ if (a->family == AF_INET) {
+ if (a->data[0] == 0) {
+ buf[0] = '*';
+ buf[1] = 0;
+ } else {
+ ap = format_host(AF_INET, 4, a->data, buf, sizeof(buf));
+ }
+ } else {
+ ap = format_host(a->family, 16, a->data, buf, sizeof(buf));
+ est_len = strlen(ap);
+ if (est_len <= addr_width)
+ est_len = addr_width;
+ else
+ est_len = addr_width + ((est_len-addr_width+3)/4)*4;
+ }
+ printf("%*s:%-*s ", est_len, ap, serv_width, resolve_service(port));
+}
+
+struct aafilter
+{
+ inet_prefix addr;
+ int port;
+ struct aafilter *next;
+};
+
+int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, int plen)
+{
+ if (!inet_addr_match(a, p, plen))
+ return 0;
+
+ /* Cursed "v4 mapped" addresses: v4 mapped socket matches
+ * pure IPv4 rule, but v4-mapped rule selects only v4-mapped
+ * sockets. Fair? */
+ if (p->family == AF_INET && a->family == AF_INET6) {
+ if (a->data[0] == 0 && a->data[1] == 0 &&
+ a->data[2] == htonl(0xffff)) {
+ inet_prefix tmp = *a;
+ tmp.data[0] = a->data[3];
+ return inet_addr_match(&tmp, p, plen);
+ }
+ }
+ return 1;
+}
+
+int unix_match(const inet_prefix *a, const inet_prefix *p)
+{
+ char *addr, *pattern;
+ memcpy(&addr, a->data, sizeof(addr));
+ memcpy(&pattern, p->data, sizeof(pattern));
+ if (pattern == NULL)
+ return 1;
+ if (addr == NULL)
+ addr = "";
+ return !fnmatch(pattern, addr, 0);
+}
+
+int run_ssfilter(struct ssfilter *f, struct tcpstat *s)
+{
+ switch (f->type) {
+ case SSF_S_AUTO:
+ {
+ static int low, high=65535;
+
+ if (s->local.family == AF_UNIX) {
+ char *p;
+ memcpy(&p, s->local.data, sizeof(p));
+ return p == NULL || (p[0] == '@' && strlen(p) == 6 &&
+ strspn(p+1, "0123456789abcdef") == 5);
+ }
+ if (s->local.family == AF_PACKET)
+ return s->lport == 0 && s->local.data == 0;
+ if (s->local.family == AF_NETLINK)
+ return s->lport < 0;
+
+ if (!low) {
+ FILE *fp = ephemeral_ports_open();
+ if (fp) {
+ fscanf(fp, "%d%d", &low, &high);
+ fclose(fp);
+ }
+ }
+ return s->lport >= low && s->lport <= high;
+ }
+ case SSF_DCOND:
+ {
+ struct aafilter *a = (void*)f->pred;
+ if (a->addr.family == AF_UNIX)
+ return unix_match(&s->remote, &a->addr);
+ if (a->port != -1 && a->port != s->rport)
+ return 0;
+ if (a->addr.bitlen) {
+ do {
+ if (!inet2_addr_match(&s->remote, &a->addr, a->addr.bitlen))
+ return 1;
+ } while ((a = a->next) != NULL);
+ return 0;
+ }
+ return 1;
+ }
+ case SSF_SCOND:
+ {
+ struct aafilter *a = (void*)f->pred;
+ if (a->addr.family == AF_UNIX)
+ return unix_match(&s->local, &a->addr);
+ if (a->port != -1 && a->port != s->lport)
+ return 0;
+ if (a->addr.bitlen) {
+ do {
+ if (!inet2_addr_match(&s->local, &a->addr, a->addr.bitlen))
+ return 1;
+ } while ((a = a->next) != NULL);
+ return 0;
+ }
+ return 1;
+ }
+ case SSF_D_GE:
+ {
+ struct aafilter *a = (void*)f->pred;
+ return s->rport >= a->port;
+ }
+ case SSF_D_LE:
+ {
+ struct aafilter *a = (void*)f->pred;
+ return s->rport <= a->port;
+ }
+ case SSF_S_GE:
+ {
+ struct aafilter *a = (void*)f->pred;
+ return s->lport >= a->port;
+ }
+ case SSF_S_LE:
+ {
+ struct aafilter *a = (void*)f->pred;
+ return s->lport <= a->port;
+ }
+
+ /* Yup. It is recursion. Sorry. */
+ case SSF_AND:
+ return run_ssfilter(f->pred, s) && run_ssfilter(f->post, s);
+ case SSF_OR:
+ return run_ssfilter(f->pred, s) || run_ssfilter(f->post, s);
+ case SSF_NOT:
+ return !run_ssfilter(f->pred, s);
+ default:
+ abort();
+ }
+}
+
+/* Relocate external jumps by reloc. */
+static void ssfilter_patch(char *a, int len, int reloc)
+{
+ while (len > 0) {
+ struct inet_diag_bc_op *op = (struct inet_diag_bc_op*)a;
+ if (op->no == len+4)
+ op->no += reloc;
+ len -= op->yes;
+ a += op->yes;
+ }
+ if (len < 0)
+ abort();
+}
+
+static int ssfilter_bytecompile(struct ssfilter *f, char **bytecode)
+{
+ switch (f->type) {
+ case SSF_S_AUTO:
+ {
+ if (!(*bytecode=malloc(4))) abort();
+ ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_AUTO, 4, 8 };
+ return 4;
+ }
+ case SSF_DCOND:
+ case SSF_SCOND:
+ {
+ struct aafilter *a = (void*)f->pred;
+ struct aafilter *b;
+ char *ptr;
+ int code = (f->type == SSF_DCOND ? INET_DIAG_BC_D_COND : INET_DIAG_BC_S_COND);
+ int len = 0;
+
+ for (b=a; b; b=b->next) {
+ len += 4 + sizeof(struct inet_diag_hostcond);
+ if (a->addr.family == AF_INET6)
+ len += 16;
+ else
+ len += 4;
+ if (b->next)
+ len += 4;
+ }
+ if (!(ptr = malloc(len))) abort();
+ *bytecode = ptr;
+ for (b=a; b; b=b->next) {
+ struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)ptr;
+ int alen = (a->addr.family == AF_INET6 ? 16 : 4);
+ int oplen = alen + 4 + sizeof(struct inet_diag_hostcond);
+ struct inet_diag_hostcond *cond = (struct inet_diag_hostcond*)(ptr+4);
+
+ *op = (struct inet_diag_bc_op){ code, oplen, oplen+4 };
+ cond->family = a->addr.family;
+ cond->port = a->port;
+ cond->prefix_len = a->addr.bitlen;
+ memcpy(cond->addr, a->addr.data, alen);
+ ptr += oplen;
+ if (b->next) {
+ op = (struct inet_diag_bc_op *)ptr;
+ *op = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, len - (ptr-*bytecode)};
+ ptr += 4;
+ }
+ }
+ return ptr - *bytecode;
+ }
+ case SSF_D_GE:
+ {
+ struct aafilter *x = (void*)f->pred;
+ if (!(*bytecode=malloc(8))) abort();
+ ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_GE, 8, 12 };
+ ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
+ return 8;
+ }
+ case SSF_D_LE:
+ {
+ struct aafilter *x = (void*)f->pred;
+ if (!(*bytecode=malloc(8))) abort();
+ ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_D_LE, 8, 12 };
+ ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
+ return 8;
+ }
+ case SSF_S_GE:
+ {
+ struct aafilter *x = (void*)f->pred;
+ if (!(*bytecode=malloc(8))) abort();
+ ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_GE, 8, 12 };
+ ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
+ return 8;
+ }
+ case SSF_S_LE:
+ {
+ struct aafilter *x = (void*)f->pred;
+ if (!(*bytecode=malloc(8))) abort();
+ ((struct inet_diag_bc_op*)*bytecode)[0] = (struct inet_diag_bc_op){ INET_DIAG_BC_S_LE, 8, 12 };
+ ((struct inet_diag_bc_op*)*bytecode)[1] = (struct inet_diag_bc_op){ 0, 0, x->port };
+ return 8;
+ }
+
+ case SSF_AND:
+ {
+ char *a1, *a2, *a, l1, l2;
+ l1 = ssfilter_bytecompile(f->pred, &a1);
+ l2 = ssfilter_bytecompile(f->post, &a2);
+ if (!(a = malloc(l1+l2))) abort();
+ memcpy(a, a1, l1);
+ memcpy(a+l1, a2, l2);
+ free(a1); free(a2);
+ ssfilter_patch(a, l1, l2);
+ *bytecode = a;
+ return l1+l2;
+ }
+ case SSF_OR:
+ {
+ char *a1, *a2, *a, l1, l2;
+ l1 = ssfilter_bytecompile(f->pred, &a1);
+ l2 = ssfilter_bytecompile(f->post, &a2);
+ if (!(a = malloc(l1+l2+4))) abort();
+ memcpy(a, a1, l1);
+ memcpy(a+l1+4, a2, l2);
+ free(a1); free(a2);
+ *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, l2+4 };
+ *bytecode = a;
+ return l1+l2+4;
+ }
+ case SSF_NOT:
+ {
+ char *a1, *a, l1;
+ l1 = ssfilter_bytecompile(f->pred, &a1);
+ if (!(a = malloc(l1+4))) abort();
+ memcpy(a, a1, l1);
+ free(a1);
+ *(struct inet_diag_bc_op*)(a+l1) = (struct inet_diag_bc_op){ INET_DIAG_BC_JMP, 4, 8 };
+ *bytecode = a;
+ return l1+4;
+ }
+ default:
+ abort();
+ }
+}
+
+static int remember_he(struct aafilter *a, struct hostent *he)
+{
+ char **ptr = he->h_addr_list;
+ int cnt = 0;
+ int len;
+
+ if (he->h_addrtype == AF_INET)
+ len = 4;
+ else if (he->h_addrtype == AF_INET6)
+ len = 16;
+ else
+ return 0;
+
+ while (*ptr) {
+ struct aafilter *b = a;
+ if (a->addr.bitlen) {
+ if ((b = malloc(sizeof(*b))) == NULL)
+ return cnt;
+ *b = *a;
+ b->next = a->next;
+ a->next = b;
+ }
+ memcpy(b->addr.data, *ptr, len);
+ b->addr.bytelen = len;
+ b->addr.bitlen = len*8;
+ b->addr.family = he->h_addrtype;
+ ptr++;
+ cnt++;
+ }
+ return cnt;
+}
+
+static int get_dns_host(struct aafilter *a, const char *addr, int fam)
+{
+ static int notfirst;
+ int cnt = 0;
+ struct hostent *he;
+
+ a->addr.bitlen = 0;
+ if (!notfirst) {
+ sethostent(1);
+ notfirst = 1;
+ }
+ he = gethostbyname2(addr, fam == AF_UNSPEC ? AF_INET : fam);
+ if (he)
+ cnt = remember_he(a, he);
+ if (fam == AF_UNSPEC) {
+ he = gethostbyname2(addr, AF_INET6);
+ if (he)
+ cnt += remember_he(a, he);
+ }
+ return !cnt;
+}
+
+static int xll_initted = 0;
+
+static void xll_init(void)
+{
+ struct rtnl_handle rth;
+ rtnl_open(&rth, 0);
+ ll_init_map(&rth);
+ rtnl_close(&rth);
+ xll_initted = 1;
+}
+
+static const char *xll_index_to_name(int index)
+{
+ if (!xll_initted)
+ xll_init();
+ return ll_index_to_name(index);
+}
+
+static int xll_name_to_index(const char *dev)
+{
+ if (!xll_initted)
+ xll_init();
+ return ll_name_to_index(dev);
+}
+
+void *parse_hostcond(char *addr)
+{
+ char *port = NULL;
+ struct aafilter a;
+ struct aafilter *res;
+ int fam = preferred_family;
+
+ memset(&a, 0, sizeof(a));
+ a.port = -1;
+
+ if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
+ char *p;
+ a.addr.family = AF_UNIX;
+ if (strncmp(addr, "unix:", 5) == 0)
+ addr+=5;
+ p = strdup(addr);
+ a.addr.bitlen = 8*strlen(p);
+ memcpy(a.addr.data, &p, sizeof(p));
+ goto out;
+ }
+
+ if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
+ a.addr.family = AF_PACKET;
+ a.addr.bitlen = 0;
+ if (strncmp(addr, "link:", 5) == 0)
+ addr+=5;
+ port = strchr(addr, ':');
+ if (port) {
+ *port = 0;
+ if (port[1] && strcmp(port+1, "*")) {
+ if (get_integer(&a.port, port+1, 0)) {
+ if ((a.port = xll_name_to_index(port+1)) <= 0)
+ return NULL;
+ }
+ }
+ }
+ if (addr[0] && strcmp(addr, "*")) {
+ unsigned short tmp;
+ a.addr.bitlen = 32;
+ if (ll_proto_a2n(&tmp, addr))
+ return NULL;
+ a.addr.data[0] = ntohs(tmp);
+ }
+ goto out;
+ }
+
+ if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
+ a.addr.family = AF_NETLINK;
+ a.addr.bitlen = 0;
+ if (strncmp(addr, "netlink:", 8) == 0)
+ addr+=8;
+ port = strchr(addr, ':');
+ if (port) {
+ *port = 0;
+ if (port[1] && strcmp(port+1, "*")) {
+ if (get_integer(&a.port, port+1, 0)) {
+ if (strcmp(port+1, "kernel") == 0)
+ a.port = 0;
+ else
+ return NULL;
+ }
+ }
+ }
+ if (addr[0] && strcmp(addr, "*")) {
+ a.addr.bitlen = 32;
+ if (get_u32(a.addr.data, addr, 0)) {
+ if (strcmp(addr, "rtnl") == 0)
+ a.addr.data[0] = 0;
+ else if (strcmp(addr, "fw") == 0)
+ a.addr.data[0] = 3;
+ else if (strcmp(addr, "tcpdiag") == 0)
+ a.addr.data[0] = 4;
+ else
+ return NULL;
+ }
+ }
+ goto out;
+ }
+
+ if (strncmp(addr, "inet:", 5) == 0) {
+ addr += 5;
+ fam = AF_INET;
+ } else if (strncmp(addr, "inet6:", 6) == 0) {
+ addr += 6;
+ fam = AF_INET6;
+ }
+
+ /* URL-like literal [] */
+ if (addr[0] == '[') {
+ addr++;
+ if ((port = strchr(addr, ']')) == NULL)
+ return NULL;
+ *port++ = 0;
+ } else if (addr[0] == '*') {
+ port = addr+1;
+ } else {
+ port = strrchr(strchr(addr, '/') ? : addr, ':');
+ }
+ if (port && *port) {
+ if (*port != ':')
+ return NULL;
+ *port++ = 0;
+ if (*port && *port != '*') {
+ if (get_integer(&a.port, port, 0)) {
+ struct servent *se1 = NULL;
+ struct servent *se2 = NULL;
+ if (current_filter.dbs&(1<<UDP_DB))
+ se1 = getservbyname(port, UDP_PROTO);
+ if (current_filter.dbs&(1<<TCP_DB))
+ se2 = getservbyname(port, TCP_PROTO);
+ if (se1 && se2 && se1->s_port != se2->s_port) {
+ fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
+ return NULL;
+ }
+ if (!se1)
+ se1 = se2;
+ if (se1) {
+ a.port = ntohs(se1->s_port);
+ } else {
+ struct scache *s;
+ for (s = rlist; s; s = s->next) {
+ if ((s->proto == UDP_PROTO &&
+ (current_filter.dbs&(1<<UDP_DB))) ||
+ (s->proto == TCP_PROTO &&
+ (current_filter.dbs&(1<<TCP_DB)))) {
+ if (s->name && strcmp(s->name, port) == 0) {
+ if (a.port > 0 && a.port != s->port) {
+ fprintf(stderr, "Error: ambiguous port \"%s\".\n", port);
+ return NULL;
+ }
+ a.port = s->port;
+ }
+ }
+ }
+ if (a.port <= 0) {
+ fprintf(stderr, "Error: \"%s\" does not look like a port.\n", port);
+ return NULL;
+ }
+ }
+ }
+ }
+ }
+ if (addr && *addr && *addr != '*') {
+ if (get_prefix_1(&a.addr, addr, fam)) {
+ if (get_dns_host(&a, addr, fam)) {
+ fprintf(stderr, "Error: an inet prefix is expected rather than \"%s\".\n", addr);
+ return NULL;
+ }
+ }
+ }
+
+ out:
+ res = malloc(sizeof(*res));
+ if (res)
+ memcpy(res, &a, sizeof(a));
+ return res;
+}
+
+static int tcp_show_line(char *line, const struct filter *f, int family)
+{
+ struct tcpstat s;
+ char *loc, *rem, *data;
+ char opt[256];
+ int n;
+ char *p;
+
+ if ((p = strchr(line, ':')) == NULL)
+ return -1;
+ loc = p+2;
+
+ if ((p = strchr(loc, ':')) == NULL)
+ return -1;
+ p[5] = 0;
+ rem = p+6;
+
+ if ((p = strchr(rem, ':')) == NULL)
+ return -1;
+ p[5] = 0;
+ data = p+6;
+
+ do {
+ int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
+
+ if (!(f->states & (1<<state)))
+ return 0;
+ } while (0);
+
+ s.local.family = s.remote.family = family;
+ if (family == AF_INET) {
+ sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
+ sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
+ s.local.bytelen = s.remote.bytelen = 4;
+ } else {
+ sscanf(loc, "%08x%08x%08x%08x:%x",
+ s.local.data,
+ s.local.data+1,
+ s.local.data+2,
+ s.local.data+3,
+ &s.lport);
+ sscanf(rem, "%08x%08x%08x%08x:%x",
+ s.remote.data,
+ s.remote.data+1,
+ s.remote.data+2,
+ s.remote.data+3,
+ &s.rport);
+ s.local.bytelen = s.remote.bytelen = 16;
+ }
+
+ if (f->f && run_ssfilter(f->f, &s) == 0)
+ return 0;
+
+ opt[0] = 0;
+ n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
+ &s.state, &s.wq, &s.rq,
+ &s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino,
+ &s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack,
+ &s.cwnd, &s.ssthresh, opt);
+
+ if (n < 17)
+ opt[0] = 0;
+
+ if (n < 12) {
+ s.rto = 0;
+ s.cwnd = 2;
+ s.ssthresh = -1;
+ s.ato = s.qack = 0;
+ }
+
+ if (netid_width)
+ printf("%-*s ", netid_width, "tcp");
+ if (state_width)
+ printf("%-*s ", state_width, sstate_name[s.state]);
+
+ printf("%-6d %-6d ", s.rq, s.wq);
+
+ formatted_print(&s.local, s.lport);
+ formatted_print(&s.remote, s.rport);
+
+ if (show_options) {
+ if (s.timer) {
+ if (s.timer > 4)
+ s.timer = 5;
+ printf(" timer:(%s,%s,%d)",
+ tmr_name[s.timer],
+ print_hz_timer(s.timeout),
+ s.timer != 1 ? s.probes : s.retrs);
+ }
+ }
+ if (show_tcpinfo) {
+ int hz = get_user_hz();
+ if (s.rto && s.rto != 3*hz)
+ printf(" rto:%g", (double)s.rto/hz);
+ if (s.ato)
+ printf(" ato:%g", (double)s.ato/hz);
+ if (s.cwnd != 2)
+ printf(" cwnd:%d", s.cwnd);
+ if (s.ssthresh != -1)
+ printf(" ssthresh:%d", s.ssthresh);
+ if (s.qack/2)
+ printf(" qack:%d", s.qack/2);
+ if (s.qack&1)
+ printf(" bidir");
+ }
+ if (show_users) {
+ char ubuf[4096];
+ if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
+ printf(" users:(%s)", ubuf);
+ }
+ if (show_details) {
+ if (s.uid)
+ printf(" uid:%u", (unsigned)s.uid);
+ printf(" ino:%u", s.ino);
+ printf(" sk:%llx", s.sk);
+ if (opt[0])
+ printf(" opt:\"%s\"", opt);
+ }
+ printf("\n");
+
+ return 0;
+}
+
+static int generic_record_read(FILE *fp,
+ int (*worker)(char*, const struct filter *, int),
+ const struct filter *f, int fam)
+{
+ char line[256];
+
+ /* skip header */
+ if (fgets(line, sizeof(line), fp) == NULL)
+ goto outerr;
+
+ while (fgets(line, sizeof(line), fp) != NULL) {
+ int n = strlen(line);
+ if (n == 0 || line[n-1] != '\n') {
+ errno = -EINVAL;
+ return -1;
+ }
+ line[n-1] = 0;
+
+ if (worker(line, f, fam) < 0)
+ return 0;
+ }
+outerr:
+
+ return ferror(fp) ? -1 : 0;
+}
+
+static char *sprint_bw(char *buf, double bw)
+{
+ if (bw > 1000000.)
+ sprintf(buf,"%.1fM", bw / 1000000.);
+ else if (bw > 1000.)
+ sprintf(buf,"%.1fK", bw / 1000.);
+ else
+ sprintf(buf, "%g", bw);
+
+ return buf;
+}
+
+static void tcp_show_info(const struct nlmsghdr *nlh, struct inet_diag_msg *r)
+{
+ struct rtattr * tb[INET_DIAG_MAX+1];
+ char b1[64];
+ double rtt = 0;
+
+ parse_rtattr(tb, INET_DIAG_MAX, (struct rtattr*)(r+1),
+ nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
+
+ if (tb[INET_DIAG_SKMEMINFO]) {
+ const __u32 *skmeminfo = RTA_DATA(tb[INET_DIAG_SKMEMINFO]);
+ printf(" skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u)",
+ skmeminfo[SK_MEMINFO_RMEM_ALLOC],
+ skmeminfo[SK_MEMINFO_RCVBUF],
+ skmeminfo[SK_MEMINFO_WMEM_ALLOC],
+ skmeminfo[SK_MEMINFO_SNDBUF],
+ skmeminfo[SK_MEMINFO_FWD_ALLOC],
+ skmeminfo[SK_MEMINFO_WMEM_QUEUED],
+ skmeminfo[SK_MEMINFO_OPTMEM]);
+ }else if (tb[INET_DIAG_MEMINFO]) {
+ const struct inet_diag_meminfo *minfo
+ = RTA_DATA(tb[INET_DIAG_MEMINFO]);
+ printf(" mem:(r%u,w%u,f%u,t%u)",
+ minfo->idiag_rmem,
+ minfo->idiag_wmem,
+ minfo->idiag_fmem,
+ minfo->idiag_tmem);
+ }
+
+ if (tb[INET_DIAG_INFO]) {
+ struct tcp_info *info;
+ int len = RTA_PAYLOAD(tb[INET_DIAG_INFO]);
+
+ /* workaround for older kernels with less fields */
+ if (len < sizeof(*info)) {
+ info = alloca(sizeof(*info));
+ memset(info, 0, sizeof(*info));
+ memcpy(info, RTA_DATA(tb[INET_DIAG_INFO]), len);
+ } else
+ info = RTA_DATA(tb[INET_DIAG_INFO]);
+
+ if (show_options) {
+ if (info->tcpi_options & TCPI_OPT_TIMESTAMPS)
+ printf(" ts");
+ if (info->tcpi_options & TCPI_OPT_SACK)
+ printf(" sack");
+ if (info->tcpi_options & TCPI_OPT_ECN)
+ printf(" ecn");
+ if (info->tcpi_options & TCPI_OPT_ECN_SEEN)
+ printf(" ecnseen");
+ }
+
+ if (tb[INET_DIAG_CONG])
+ printf(" %s", rta_getattr_str(tb[INET_DIAG_CONG]));
+
+ if (info->tcpi_options & TCPI_OPT_WSCALE)
+ printf(" wscale:%d,%d", info->tcpi_snd_wscale,
+ info->tcpi_rcv_wscale);
+ if (info->tcpi_rto && info->tcpi_rto != 3000000)
+ printf(" rto:%g", (double)info->tcpi_rto/1000);
+ if (info->tcpi_rtt)
+ printf(" rtt:%g/%g", (double)info->tcpi_rtt/1000,
+ (double)info->tcpi_rttvar/1000);
+ if (info->tcpi_ato)
+ printf(" ato:%g", (double)info->tcpi_ato/1000);
+ if (info->tcpi_snd_cwnd != 2)
+ printf(" cwnd:%d", info->tcpi_snd_cwnd);
+ if (info->tcpi_snd_ssthresh < 0xFFFF)
+ printf(" ssthresh:%d", info->tcpi_snd_ssthresh);
+
+ rtt = (double) info->tcpi_rtt;
+ if (tb[INET_DIAG_VEGASINFO]) {
+ const struct tcpvegas_info *vinfo
+ = RTA_DATA(tb[INET_DIAG_VEGASINFO]);
+
+ if (vinfo->tcpv_enabled &&
+ vinfo->tcpv_rtt && vinfo->tcpv_rtt != 0x7fffffff)
+ rtt = vinfo->tcpv_rtt;
+ }
+
+ if (rtt > 0 && info->tcpi_snd_mss && info->tcpi_snd_cwnd) {
+ printf(" send %sbps",
+ sprint_bw(b1, (double) info->tcpi_snd_cwnd *
+ (double) info->tcpi_snd_mss * 8000000.
+ / rtt));
+ }
+
+ if (info->tcpi_rcv_rtt)
+ printf(" rcv_rtt:%g", (double) info->tcpi_rcv_rtt/1000);
+ if (info->tcpi_rcv_space)
+ printf(" rcv_space:%d", info->tcpi_rcv_space);
+
+ }
+}
+
+static int tcp_show_sock(struct nlmsghdr *nlh, struct filter *f)
+{
+ struct inet_diag_msg *r = NLMSG_DATA(nlh);
+ struct tcpstat s;
+
+ s.state = r->idiag_state;
+ s.local.family = s.remote.family = r->idiag_family;
+ s.lport = ntohs(r->id.idiag_sport);
+ s.rport = ntohs(r->id.idiag_dport);
+ if (s.local.family == AF_INET) {
+ s.local.bytelen = s.remote.bytelen = 4;
+ } else {
+ s.local.bytelen = s.remote.bytelen = 16;
+ }
+ memcpy(s.local.data, r->id.idiag_src, s.local.bytelen);
+ memcpy(s.remote.data, r->id.idiag_dst, s.local.bytelen);
+
+ if (f && f->f && run_ssfilter(f->f, &s) == 0)
+ return 0;
+
+ if (netid_width)
+ printf("%-*s ", netid_width, "tcp");
+ if (state_width)
+ printf("%-*s ", state_width, sstate_name[s.state]);
+
+ printf("%-6d %-6d ", r->idiag_rqueue, r->idiag_wqueue);
+
+ formatted_print(&s.local, s.lport);
+ formatted_print(&s.remote, s.rport);
+
+ if (show_options) {
+ if (r->idiag_timer) {
+ if (r->idiag_timer > 4)
+ r->idiag_timer = 5;
+ printf(" timer:(%s,%s,%d)",
+ tmr_name[r->idiag_timer],
+ print_ms_timer(r->idiag_expires),
+ r->idiag_retrans);
+ }
+ }
+ if (show_users) {
+ char ubuf[4096];
+ if (find_users(r->idiag_inode, ubuf, sizeof(ubuf)) > 0)
+ printf(" users:(%s)", ubuf);
+ }
+ if (show_details) {
+ if (r->idiag_uid)
+ printf(" uid:%u", (unsigned)r->idiag_uid);
+ printf(" ino:%u", r->idiag_inode);
+ printf(" sk:");
+ if (r->id.idiag_cookie[1] != 0)
+ printf("%08x", r->id.idiag_cookie[1]);
+ printf("%08x", r->id.idiag_cookie[0]);
+ }
+ if (show_mem || show_tcpinfo) {
+ printf("\n\t");
+ tcp_show_info(nlh, r);
+ }
+
+ printf("\n");
+
+ return 0;
+}
+
+static int tcp_show_netlink(struct filter *f, FILE *dump_fp, int socktype)
+{
+ int fd;
+ struct sockaddr_nl nladdr;
+ struct {
+ struct nlmsghdr nlh;
+ struct inet_diag_req r;
+ } req;
+ char *bc = NULL;
+ int bclen;
+ struct msghdr msg;
+ struct rtattr rta;
+ char buf[8192];
+ struct iovec iov[3];
+
+ if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
+ return -1;
+
+ memset(&nladdr, 0, sizeof(nladdr));
+ nladdr.nl_family = AF_NETLINK;
+
+ req.nlh.nlmsg_len = sizeof(req);
+ req.nlh.nlmsg_type = socktype;
+ req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
+ req.nlh.nlmsg_pid = 0;
+ req.nlh.nlmsg_seq = 123456;
+ memset(&req.r, 0, sizeof(req.r));
+ req.r.idiag_family = AF_INET;
+ req.r.idiag_states = f->states;
+ if (show_mem) {
+ req.r.idiag_ext |= (1<<(INET_DIAG_MEMINFO-1));
+ req.r.idiag_ext |= (1<<(INET_DIAG_SKMEMINFO-1));
+ }
+
+ if (show_tcpinfo) {
+ req.r.idiag_ext |= (1<<(INET_DIAG_INFO-1));
+ req.r.idiag_ext |= (1<<(INET_DIAG_VEGASINFO-1));
+ req.r.idiag_ext |= (1<<(INET_DIAG_CONG-1));
+ }
+
+ iov[0] = (struct iovec){
+ .iov_base = &req,
+ .iov_len = sizeof(req)
+ };
+ if (f->f) {
+ bclen = ssfilter_bytecompile(f->f, &bc);
+ rta.rta_type = INET_DIAG_REQ_BYTECODE;
+ rta.rta_len = RTA_LENGTH(bclen);
+ iov[1] = (struct iovec){ &rta, sizeof(rta) };
+ iov[2] = (struct iovec){ bc, bclen };
+ req.nlh.nlmsg_len += RTA_LENGTH(bclen);
+ }
+
+ msg = (struct msghdr) {
+ .msg_name = (void*)&nladdr,
+ .msg_namelen = sizeof(nladdr),
+ .msg_iov = iov,
+ .msg_iovlen = f->f ? 3 : 1,
+ };
+
+ if (sendmsg(fd, &msg, 0) < 0) {
+ close(fd);
+ return -1;
+ }
+
+ iov[0] = (struct iovec){
+ .iov_base = buf,
+ .iov_len = sizeof(buf)
+ };
+
+ while (1) {
+ int status;
+ struct nlmsghdr *h;
+
+ msg = (struct msghdr) {
+ (void*)&nladdr, sizeof(nladdr),
+ iov, 1,
+ NULL, 0,
+ 0
+ };
+
+ status = recvmsg(fd, &msg, 0);
+
+ if (status < 0) {
+ if (errno == EINTR)
+ continue;
+ perror("OVERRUN");
+ continue;
+ }
+ if (status == 0) {
+ fprintf(stderr, "EOF on netlink\n");
+ close(fd);
+ return 0;
+ }
+
+ if (dump_fp)
+ fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
+
+ h = (struct nlmsghdr*)buf;
+ while (NLMSG_OK(h, status)) {
+ int err;
+ struct inet_diag_msg *r = NLMSG_DATA(h);
+
+ if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
+ h->nlmsg_seq != 123456)
+ goto skip_it;
+
+ if (h->nlmsg_type == NLMSG_DONE) {
+ close(fd);
+ return 0;
+ }
+ if (h->nlmsg_type == NLMSG_ERROR) {
+ struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
+ if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
+ fprintf(stderr, "ERROR truncated\n");
+ } else {
+ errno = -err->error;
+ if (errno == EOPNOTSUPP) {
+ close(fd);
+ return -1;
+ }
+ perror("TCPDIAG answers");
+ }
+ close(fd);
+ return 0;
+ }
+ if (!dump_fp) {
+ if (!(f->families & (1<<r->idiag_family))) {
+ h = NLMSG_NEXT(h, status);
+ continue;
+ }
+ err = tcp_show_sock(h, NULL);
+ if (err < 0) {
+ close(fd);
+ return err;
+ }
+ }
+
+skip_it:
+ h = NLMSG_NEXT(h, status);
+ }
+ if (msg.msg_flags & MSG_TRUNC) {
+ fprintf(stderr, "Message truncated\n");
+ continue;
+ }
+ if (status) {
+ fprintf(stderr, "!!!Remnant of size %d\n", status);
+ exit(1);
+ }
+ }
+ close(fd);
+ return 0;
+}
+
+static int tcp_show_netlink_file(struct filter *f)
+{
+ FILE *fp;
+ char buf[8192];
+
+ if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
+ perror("fopen($TCPDIAG_FILE)");
+ return -1;
+ }
+
+ while (1) {
+ int status, err;
+ struct nlmsghdr *h = (struct nlmsghdr*)buf;
+
+ status = fread(buf, 1, sizeof(*h), fp);
+ if (status < 0) {
+ perror("Reading header from $TCPDIAG_FILE");
+ return -1;
+ }
+ if (status != sizeof(*h)) {
+ perror("Unexpected EOF reading $TCPDIAG_FILE");
+ return -1;
+ }
+
+ status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
+
+ if (status < 0) {
+ perror("Reading $TCPDIAG_FILE");
+ return -1;
+ }
+ if (status + sizeof(*h) < h->nlmsg_len) {
+ perror("Unexpected EOF reading $TCPDIAG_FILE");
+ return -1;
+ }
+
+ /* The only legal exit point */
+ if (h->nlmsg_type == NLMSG_DONE)
+ return 0;
+
+ if (h->nlmsg_type == NLMSG_ERROR) {
+ struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
+ if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
+ fprintf(stderr, "ERROR truncated\n");
+ } else {
+ errno = -err->error;
+ perror("TCPDIAG answered");
+ }
+ return -1;
+ }
+
+ err = tcp_show_sock(h, f);
+ if (err < 0)
+ return err;
+ }
+}
+
+static int tcp_show(struct filter *f, int socktype)
+{
+ FILE *fp = NULL;
+ char *buf = NULL;
+ int bufsize = 64*1024;
+
+ dg_proto = TCP_PROTO;
+
+ if (getenv("TCPDIAG_FILE"))
+ return tcp_show_netlink_file(f);
+
+ if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
+ && tcp_show_netlink(f, NULL, socktype) == 0)
+ return 0;
+
+ /* Sigh... We have to parse /proc/net/tcp... */
+
+
+ /* Estimate amount of sockets and try to allocate
+ * huge buffer to read all the table at one read.
+ * Limit it by 16MB though. The assumption is: as soon as
+ * kernel was able to hold information about N connections,
+ * it is able to give us some memory for snapshot.
+ */
+ if (1) {
+ int guess = slabstat.socks+slabstat.tcp_syns;
+ if (f->states&(1<<SS_TIME_WAIT))
+ guess += slabstat.tcp_tws;
+ if (guess > (16*1024*1024)/128)
+ guess = (16*1024*1024)/128;
+ guess *= 128;
+ if (guess > bufsize)
+ bufsize = guess;
+ }
+ while (bufsize >= 64*1024) {
+ if ((buf = malloc(bufsize)) != NULL)
+ break;
+ bufsize /= 2;
+ }
+ if (buf == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ if (f->families & (1<<AF_INET)) {
+ if ((fp = net_tcp_open()) == NULL)
+ goto outerr;
+
+ setbuffer(fp, buf, bufsize);
+ if (generic_record_read(fp, tcp_show_line, f, AF_INET))
+ goto outerr;
+ fclose(fp);
+ }
+
+ if ((f->families & (1<<AF_INET6)) &&
+ (fp = net_tcp6_open()) != NULL) {
+ setbuffer(fp, buf, bufsize);
+ if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
+ goto outerr;
+ fclose(fp);
+ }
+
+ free(buf);
+ return 0;
+
+outerr:
+ do {
+ int saved_errno = errno;
+ if (buf)
+ free(buf);
+ if (fp)
+ fclose(fp);
+ errno = saved_errno;
+ return -1;
+ } while (0);
+}
+
+
+int dgram_show_line(char *line, const struct filter *f, int family)
+{
+ struct tcpstat s;
+ char *loc, *rem, *data;
+ char opt[256];
+ int n;
+ char *p;
+
+ if ((p = strchr(line, ':')) == NULL)
+ return -1;
+ loc = p+2;
+
+ if ((p = strchr(loc, ':')) == NULL)
+ return -1;
+ p[5] = 0;
+ rem = p+6;
+
+ if ((p = strchr(rem, ':')) == NULL)
+ return -1;
+ p[5] = 0;
+ data = p+6;
+
+ do {
+ int state = (data[1] >= 'A') ? (data[1] - 'A' + 10) : (data[1] - '0');
+
+ if (!(f->states & (1<<state)))
+ return 0;
+ } while (0);
+
+ s.local.family = s.remote.family = family;
+ if (family == AF_INET) {
+ sscanf(loc, "%x:%x", s.local.data, (unsigned*)&s.lport);
+ sscanf(rem, "%x:%x", s.remote.data, (unsigned*)&s.rport);
+ s.local.bytelen = s.remote.bytelen = 4;
+ } else {
+ sscanf(loc, "%08x%08x%08x%08x:%x",
+ s.local.data,
+ s.local.data+1,
+ s.local.data+2,
+ s.local.data+3,
+ &s.lport);
+ sscanf(rem, "%08x%08x%08x%08x:%x",
+ s.remote.data,
+ s.remote.data+1,
+ s.remote.data+2,
+ s.remote.data+3,
+ &s.rport);
+ s.local.bytelen = s.remote.bytelen = 16;
+ }
+
+ if (f->f && run_ssfilter(f->f, &s) == 0)
+ return 0;
+
+ opt[0] = 0;
+ n = sscanf(data, "%x %x:%x %*x:%*x %*x %d %*d %u %d %llx %[^\n]\n",
+ &s.state, &s.wq, &s.rq,
+ &s.uid, &s.ino,
+ &s.refcnt, &s.sk, opt);
+
+ if (n < 9)
+ opt[0] = 0;
+
+ if (netid_width)
+ printf("%-*s ", netid_width, dg_proto);
+ if (state_width)
+ printf("%-*s ", state_width, sstate_name[s.state]);
+
+ printf("%-6d %-6d ", s.rq, s.wq);
+
+ formatted_print(&s.local, s.lport);
+ formatted_print(&s.remote, s.rport);
+
+ if (show_users) {
+ char ubuf[4096];
+ if (find_users(s.ino, ubuf, sizeof(ubuf)) > 0)
+ printf(" users:(%s)", ubuf);
+ }
+
+ if (show_details) {
+ if (s.uid)
+ printf(" uid=%u", (unsigned)s.uid);
+ printf(" ino=%u", s.ino);
+ printf(" sk=%llx", s.sk);
+ if (opt[0])
+ printf(" opt:\"%s\"", opt);
+ }
+ printf("\n");
+
+ return 0;
+}
+
+
+int udp_show(struct filter *f)
+{
+ FILE *fp = NULL;
+
+ dg_proto = UDP_PROTO;
+
+ if (f->families&(1<<AF_INET)) {
+ if ((fp = net_udp_open()) == NULL)
+ goto outerr;
+ if (generic_record_read(fp, dgram_show_line, f, AF_INET))
+ goto outerr;
+ fclose(fp);
+ }
+
+ if ((f->families&(1<<AF_INET6)) &&
+ (fp = net_udp6_open()) != NULL) {
+ if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
+ goto outerr;
+ fclose(fp);
+ }
+ return 0;
+
+outerr:
+ do {
+ int saved_errno = errno;
+ if (fp)
+ fclose(fp);
+ errno = saved_errno;
+ return -1;
+ } while (0);
+}
+
+int raw_show(struct filter *f)
+{
+ FILE *fp = NULL;
+
+ dg_proto = RAW_PROTO;
+
+ if (f->families&(1<<AF_INET)) {
+ if ((fp = net_raw_open()) == NULL)
+ goto outerr;
+ if (generic_record_read(fp, dgram_show_line, f, AF_INET))
+ goto outerr;
+ fclose(fp);
+ }
+
+ if ((f->families&(1<<AF_INET6)) &&
+ (fp = net_raw6_open()) != NULL) {
+ if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
+ goto outerr;
+ fclose(fp);
+ }
+ return 0;
+
+outerr:
+ do {
+ int saved_errno = errno;
+ if (fp)
+ fclose(fp);
+ errno = saved_errno;
+ return -1;
+ } while (0);
+}
+
+
+struct unixstat
+{
+ struct unixstat *next;
+ int ino;
+ int peer;
+ int rq;
+ int wq;
+ int state;
+ int type;
+ char *name;
+};
+
+
+
+int unix_state_map[] = { SS_CLOSE, SS_SYN_SENT,
+ SS_ESTABLISHED, SS_CLOSING };
+
+
+#define MAX_UNIX_REMEMBER (1024*1024/sizeof(struct unixstat))
+
+void unix_list_free(struct unixstat *list)
+{
+ while (list) {
+ struct unixstat *s = list;
+ list = list->next;
+ if (s->name)
+ free(s->name);
+ free(s);
+ }
+}
+
+void unix_list_print(struct unixstat *list, struct filter *f)
+{
+ struct unixstat *s;
+ char *peer;
+
+ for (s = list; s; s = s->next) {
+ if (!(f->states & (1<<s->state)))
+ continue;
+ if (s->type == SOCK_STREAM && !(f->dbs&(1<<UNIX_ST_DB)))
+ continue;
+ if (s->type == SOCK_DGRAM && !(f->dbs&(1<<UNIX_DG_DB)))
+ continue;
+
+ peer = "*";
+ if (s->peer) {
+ struct unixstat *p;
+ for (p = list; p; p = p->next) {
+ if (s->peer == p->ino)
+ break;
+ }
+ if (!p) {
+ peer = "?";
+ } else {
+ peer = p->name ? : "*";
+ }
+ }
+
+ if (f->f) {
+ struct tcpstat tst;
+ tst.local.family = AF_UNIX;
+ tst.remote.family = AF_UNIX;
+ memcpy(tst.local.data, &s->name, sizeof(s->name));
+ if (strcmp(peer, "*") == 0)
+ memset(tst.remote.data, 0, sizeof(peer));
+ else
+ memcpy(tst.remote.data, &peer, sizeof(peer));
+ if (run_ssfilter(f->f, &tst) == 0)
+ continue;
+ }
+
+ if (netid_width)
+ printf("%-*s ", netid_width,
+ s->type == SOCK_STREAM ? "u_str" : "u_dgr");
+ if (state_width)
+ printf("%-*s ", state_width, sstate_name[s->state]);
+ printf("%-6d %-6d ", s->rq, s->wq);
+ printf("%*s %-*d %*s %-*d",
+ addr_width, s->name ? : "*", serv_width, s->ino,
+ addr_width, peer, serv_width, s->peer);
+ if (show_users) {
+ char ubuf[4096];
+ if (find_users(s->ino, ubuf, sizeof(ubuf)) > 0)
+ printf(" users:(%s)", ubuf);
+ }
+ printf("\n");
+ }
+}
+
+static int unix_show_sock(struct nlmsghdr *nlh, struct filter *f)
+{
+ struct unix_diag_msg *r = NLMSG_DATA(nlh);
+ struct rtattr *tb[UNIX_DIAG_MAX+1];
+ char name[128];
+ int peer_ino;
+ int rqlen;
+
+ parse_rtattr(tb, UNIX_DIAG_MAX, (struct rtattr*)(r+1),
+ nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
+
+ if (netid_width)
+ printf("%-*s ", netid_width,
+ r->udiag_type == SOCK_STREAM ? "u_str" : "u_dgr");
+ if (state_width)
+ printf("%-*s ", state_width, sstate_name[r->udiag_state]);
+
+ if (tb[UNIX_DIAG_RQLEN])
+ rqlen = *(int *)RTA_DATA(tb[UNIX_DIAG_RQLEN]);
+ else
+ rqlen = 0;
+
+ printf("%-6d %-6d ", rqlen, 0);
+
+ if (tb[UNIX_DIAG_NAME]) {
+ int len = RTA_PAYLOAD(tb[UNIX_DIAG_NAME]);
+
+ memcpy(name, RTA_DATA(tb[UNIX_DIAG_NAME]), len);
+ name[len] = '\0';
+ if (name[0] == '\0')
+ name[0] = '@';
+ } else
+ sprintf(name, "*");
+
+ if (tb[UNIX_DIAG_PEER])
+ peer_ino = *(int *)RTA_DATA(tb[UNIX_DIAG_PEER]);
+ else
+ peer_ino = 0;
+
+ printf("%*s %-*d %*s %-*d",
+ addr_width, name,
+ serv_width, r->udiag_ino,
+ addr_width, "*", /* FIXME */
+ serv_width, peer_ino);
+
+ if (show_users) {
+ char ubuf[4096];
+ if (find_users(r->udiag_ino, ubuf, sizeof(ubuf)) > 0)
+ printf(" users:(%s)", ubuf);
+ }
+
+ printf("\n");
+
+ return 0;
+}
+
+static int unix_show_netlink(struct filter *f, FILE *dump_fp)
+{
+ int fd;
+ struct {
+ struct nlmsghdr nlh;
+ struct unix_diag_req r;
+ } req;
+ char buf[8192];
+
+ if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG)) < 0)
+ return -1;
+
+ memset(&req, 0, sizeof(req));
+ req.nlh.nlmsg_len = sizeof(req);
+ req.nlh.nlmsg_type = SOCK_DIAG_BY_FAMILY;
+ req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
+ req.nlh.nlmsg_seq = 123456;
+
+ req.r.sdiag_family = AF_UNIX;
+ req.r.udiag_states = f->states;
+ req.r.udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER | UDIAG_SHOW_RQLEN;
+
+ if (send(fd, &req, sizeof(req), 0) < 0) {
+ close(fd);
+ return -1;
+ }
+
+ while (1) {
+ ssize_t status;
+ struct nlmsghdr *h;
+ struct sockaddr_nl nladdr;
+ socklen_t slen = sizeof(nladdr);
+
+ status = recvfrom(fd, buf, sizeof(buf), 0,
+ (struct sockaddr *) &nladdr, &slen);
+ if (status < 0) {
+ if (errno == EINTR)
+ continue;
+ perror("OVERRUN");
+ continue;
+ }
+ if (status == 0) {
+ fprintf(stderr, "EOF on netlink\n");
+ goto close_it;
+ }
+
+ if (dump_fp)
+ fwrite(buf, 1, NLMSG_ALIGN(status), dump_fp);
+
+ h = (struct nlmsghdr*)buf;
+ while (NLMSG_OK(h, status)) {
+ int err;
+
+ if (/*h->nlmsg_pid != rth->local.nl_pid ||*/
+ h->nlmsg_seq != 123456)
+ goto skip_it;
+
+ if (h->nlmsg_type == NLMSG_DONE)
+ goto close_it;
+
+ if (h->nlmsg_type == NLMSG_ERROR) {
+ struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
+ if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
+ fprintf(stderr, "ERROR truncated\n");
+ } else {
+ errno = -err->error;
+ if (errno != ENOENT)
+ fprintf(stderr, "UDIAG answers %d\n", errno);
+ }
+ close(fd);
+ return -1;
+ }
+ if (!dump_fp) {
+ err = unix_show_sock(h, f);
+ if (err < 0) {
+ close(fd);
+ return err;
+ }
+ }
+
+skip_it:
+ h = NLMSG_NEXT(h, status);
+ }
+
+ if (status) {
+ fprintf(stderr, "!!!Remnant of size %zd\n", status);
+ exit(1);
+ }
+ }
+
+close_it:
+ close(fd);
+ return 0;
+}
+
+int unix_show(struct filter *f)
+{
+ FILE *fp;
+ char buf[256];
+ char name[128];
+ int newformat = 0;
+ int cnt;
+ struct unixstat *list = NULL;
+
+ if (!getenv("PROC_NET_UNIX") && !getenv("PROC_ROOT")
+ && unix_show_netlink(f, NULL) == 0)
+ return 0;
+
+ if ((fp = net_unix_open()) == NULL)
+ return -1;
+ fgets(buf, sizeof(buf)-1, fp);
+
+ if (memcmp(buf, "Peer", 4) == 0)
+ newformat = 1;
+ cnt = 0;
+
+ while (fgets(buf, sizeof(buf)-1, fp)) {
+ struct unixstat *u, **insp;
+ int flags;
+
+ if (!(u = malloc(sizeof(*u))))
+ break;
+ u->name = NULL;
+
+ if (sscanf(buf, "%x: %x %x %x %x %x %d %s",
+ &u->peer, &u->rq, &u->wq, &flags, &u->type,
+ &u->state, &u->ino, name) < 8)
+ name[0] = 0;
+
+ if (flags&(1<<16)) {
+ u->state = SS_LISTEN;
+ } else {
+ u->state = unix_state_map[u->state-1];
+ if (u->type == SOCK_DGRAM &&
+ u->state == SS_CLOSE &&
+ u->peer)
+ u->state = SS_ESTABLISHED;
+ }
+
+ if (!newformat) {
+ u->peer = 0;
+ u->rq = 0;
+ u->wq = 0;
+ }
+
+ insp = &list;
+ while (*insp) {
+ if (u->type < (*insp)->type ||
+ (u->type == (*insp)->type &&
+ u->ino < (*insp)->ino))
+ break;
+ insp = &(*insp)->next;
+ }
+ u->next = *insp;
+ *insp = u;
+
+ if (name[0]) {
+ if ((u->name = malloc(strlen(name)+1)) == NULL)
+ break;
+ strcpy(u->name, name);
+ }
+ if (++cnt > MAX_UNIX_REMEMBER) {
+ unix_list_print(list, f);
+ unix_list_free(list);
+ list = NULL;
+ cnt = 0;
+ }
+ }
+ fclose(fp);
+ if (list) {
+ unix_list_print(list, f);
+ unix_list_free(list);
+ list = NULL;
+ cnt = 0;
+ }
+
+ return 0;
+}
+
+
+int packet_show(struct filter *f)
+{
+ FILE *fp;
+ char buf[256];
+ int type;
+ int prot;
+ int iface;
+ int state;
+ int rq;
+ int uid;
+ int ino;
+ unsigned long long sk;
+
+ if (!(f->states & (1<<SS_CLOSE)))
+ return 0;
+
+ if ((fp = net_packet_open()) == NULL)
+ return -1;
+ fgets(buf, sizeof(buf)-1, fp);
+
+ while (fgets(buf, sizeof(buf)-1, fp)) {
+ sscanf(buf, "%llx %*d %d %x %d %d %u %u %u",
+ &sk,
+ &type, &prot, &iface, &state,
+ &rq, &uid, &ino);
+
+ if (type == SOCK_RAW && !(f->dbs&(1<<PACKET_R_DB)))
+ continue;
+ if (type == SOCK_DGRAM && !(f->dbs&(1<<PACKET_DG_DB)))
+ continue;
+ if (f->f) {
+ struct tcpstat tst;
+ tst.local.family = AF_PACKET;
+ tst.remote.family = AF_PACKET;
+ tst.rport = 0;
+ tst.lport = iface;
+ tst.local.data[0] = prot;
+ tst.remote.data[0] = 0;
+ if (run_ssfilter(f->f, &tst) == 0)
+ continue;
+ }
+
+ if (netid_width)
+ printf("%-*s ", netid_width,
+ type == SOCK_RAW ? "p_raw" : "p_dgr");
+ if (state_width)
+ printf("%-*s ", state_width, "UNCONN");
+ printf("%-6d %-6d ", rq, 0);
+ if (prot == 3) {
+ printf("%*s:", addr_width, "*");
+ } else {
+ char tb[16];
+ printf("%*s:", addr_width,
+ ll_proto_n2a(htons(prot), tb, sizeof(tb)));
+ }
+ if (iface == 0) {
+ printf("%-*s ", serv_width, "*");
+ } else {
+ printf("%-*s ", serv_width, xll_index_to_name(iface));
+ }
+ printf("%*s*%-*s",
+ addr_width, "", serv_width, "");
+
+ if (show_users) {
+ char ubuf[4096];
+ if (find_users(ino, ubuf, sizeof(ubuf)) > 0)
+ printf(" users:(%s)", ubuf);
+ }
+ if (show_details) {
+ printf(" ino=%u uid=%u sk=%llx", ino, uid, sk);
+ }
+ printf("\n");
+ }
+
+ return 0;
+}
+
+int netlink_show(struct filter *f)
+{
+ FILE *fp;
+ char buf[256];
+ int prot, pid;
+ unsigned groups;
+ int rq, wq, rc;
+ unsigned long long sk, cb;
+
+ if (!(f->states & (1<<SS_CLOSE)))
+ return 0;
+
+ if ((fp = net_netlink_open()) == NULL)
+ return -1;
+ fgets(buf, sizeof(buf)-1, fp);
+
+ while (fgets(buf, sizeof(buf)-1, fp)) {
+ sscanf(buf, "%llx %d %d %x %d %d %llx %d",
+ &sk,
+ &prot, &pid, &groups, &rq, &wq, &cb, &rc);
+
+ if (f->f) {
+ struct tcpstat tst;
+ tst.local.family = AF_NETLINK;
+ tst.remote.family = AF_NETLINK;
+ tst.rport = -1;
+ tst.lport = pid;
+ tst.local.data[0] = prot;
+ tst.remote.data[0] = 0;
+ if (run_ssfilter(f->f, &tst) == 0)
+ continue;
+ }
+
+ if (netid_width)
+ printf("%-*s ", netid_width, "nl");
+ if (state_width)
+ printf("%-*s ", state_width, "UNCONN");
+ printf("%-6d %-6d ", rq, wq);
+ if (resolve_services && prot == 0)
+ printf("%*s:", addr_width, "rtnl");
+ else if (resolve_services && prot == 3)
+ printf("%*s:", addr_width, "fw");
+ else if (resolve_services && prot == 4)
+ printf("%*s:", addr_width, "tcpdiag");
+ else
+ printf("%*d:", addr_width, prot);
+ if (pid == -1) {
+ printf("%-*s ", serv_width, "*");
+ } else if (resolve_services) {
+ int done = 0;
+ if (!pid) {
+ done = 1;
+ printf("%-*s ", serv_width, "kernel");
+ } else if (pid > 0) {
+ char procname[64];
+ FILE *fp;
+ sprintf(procname, "%s/%d/stat",
+ getenv("PROC_ROOT") ? : "/proc", pid);
+ if ((fp = fopen(procname, "r")) != NULL) {
+ if (fscanf(fp, "%*d (%[^)])", procname) == 1) {
+ sprintf(procname+strlen(procname), "/%d", pid);
+ printf("%-*s ", serv_width, procname);
+ done = 1;
+ }
+ fclose(fp);
+ }
+ }
+ if (!done)
+ printf("%-*d ", serv_width, pid);
+ } else {
+ printf("%-*d ", serv_width, pid);
+ }
+ printf("%*s*%-*s",
+ addr_width, "", serv_width, "");
+
+ if (show_details) {
+ printf(" sk=%llx cb=%llx groups=0x%08x", sk, cb, groups);
+ }
+ printf("\n");
+ }
+
+ return 0;
+}
+
+struct snmpstat
+{
+ int tcp_estab;
+};
+
+int get_snmp_int(char *proto, char *key, int *result)
+{
+ char buf[1024];
+ FILE *fp;
+ int protolen = strlen(proto);
+ int keylen = strlen(key);
+
+ *result = 0;
+
+ if ((fp = net_snmp_open()) == NULL)
+ return -1;
+
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ char *p = buf;
+ int pos = 0;
+ if (memcmp(buf, proto, protolen))
+ continue;
+ while ((p = strchr(p, ' ')) != NULL) {
+ pos++;
+ p++;
+ if (memcmp(p, key, keylen) == 0 &&
+ (p[keylen] == ' ' || p[keylen] == '\n'))
+ break;
+ }
+ if (fgets(buf, sizeof(buf), fp) == NULL)
+ break;
+ if (memcmp(buf, proto, protolen))
+ break;
+ p = buf;
+ while ((p = strchr(p, ' ')) != NULL) {
+ p++;
+ if (--pos == 0) {
+ sscanf(p, "%d", result);
+ fclose(fp);
+ return 0;
+ }
+ }
+ }
+
+ fclose(fp);
+ errno = ESRCH;
+ return -1;
+}
+
+
+/* Get stats from sockstat */
+
+struct sockstat
+{
+ int socks;
+ int tcp_mem;
+ int tcp_total;
+ int tcp_orphans;
+ int tcp_tws;
+ int tcp4_hashed;
+ int udp4;
+ int raw4;
+ int frag4;
+ int frag4_mem;
+ int tcp6_hashed;
+ int udp6;
+ int raw6;
+ int frag6;
+ int frag6_mem;
+};
+
+static void get_sockstat_line(char *line, struct sockstat *s)
+{
+ char id[256], rem[256];
+
+ if (sscanf(line, "%[^ ] %[^\n]\n", id, rem) != 2)
+ return;
+
+ if (strcmp(id, "sockets:") == 0)
+ sscanf(rem, "%*s%d", &s->socks);
+ else if (strcmp(id, "UDP:") == 0)
+ sscanf(rem, "%*s%d", &s->udp4);
+ else if (strcmp(id, "UDP6:") == 0)
+ sscanf(rem, "%*s%d", &s->udp6);
+ else if (strcmp(id, "RAW:") == 0)
+ sscanf(rem, "%*s%d", &s->raw4);
+ else if (strcmp(id, "RAW6:") == 0)
+ sscanf(rem, "%*s%d", &s->raw6);
+ else if (strcmp(id, "TCP6:") == 0)
+ sscanf(rem, "%*s%d", &s->tcp6_hashed);
+ else if (strcmp(id, "FRAG:") == 0)
+ sscanf(rem, "%*s%d%*s%d", &s->frag4, &s->frag4_mem);
+ else if (strcmp(id, "FRAG6:") == 0)
+ sscanf(rem, "%*s%d%*s%d", &s->frag6, &s->frag6_mem);
+ else if (strcmp(id, "TCP:") == 0)
+ sscanf(rem, "%*s%d%*s%d%*s%d%*s%d%*s%d",
+ &s->tcp4_hashed,
+ &s->tcp_orphans, &s->tcp_tws, &s->tcp_total, &s->tcp_mem);
+}
+
+int get_sockstat(struct sockstat *s)
+{
+ char buf[256];
+ FILE *fp;
+
+ memset(s, 0, sizeof(*s));
+
+ if ((fp = net_sockstat_open()) == NULL)
+ return -1;
+ while(fgets(buf, sizeof(buf), fp) != NULL)
+ get_sockstat_line(buf, s);
+ fclose(fp);
+
+ if ((fp = net_sockstat6_open()) == NULL)
+ return 0;
+ while(fgets(buf, sizeof(buf), fp) != NULL)
+ get_sockstat_line(buf, s);
+ fclose(fp);
+
+ return 0;
+}
+
+int print_summary(void)
+{
+ struct sockstat s;
+ struct snmpstat sn;
+
+ if (get_sockstat(&s) < 0)
+ perror("ss: get_sockstat");
+ if (get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab) < 0)
+ perror("ss: get_snmpstat");
+
+ printf("Total: %d (kernel %d)\n", s.socks, slabstat.socks);
+
+ printf("TCP: %d (estab %d, closed %d, orphaned %d, synrecv %d, timewait %d/%d), ports %d\n",
+ s.tcp_total + slabstat.tcp_syns + s.tcp_tws,
+ sn.tcp_estab,
+ s.tcp_total - (s.tcp4_hashed+s.tcp6_hashed-s.tcp_tws),
+ s.tcp_orphans,
+ slabstat.tcp_syns,
+ s.tcp_tws, slabstat.tcp_tws,
+ slabstat.tcp_ports
+ );
+
+ printf("\n");
+ printf("Transport Total IP IPv6\n");
+ printf("* %-9d %-9s %-9s\n", slabstat.socks, "-", "-");
+ printf("RAW %-9d %-9d %-9d\n", s.raw4+s.raw6, s.raw4, s.raw6);
+ printf("UDP %-9d %-9d %-9d\n", s.udp4+s.udp6, s.udp4, s.udp6);
+ printf("TCP %-9d %-9d %-9d\n", s.tcp4_hashed+s.tcp6_hashed, s.tcp4_hashed, s.tcp6_hashed);
+ printf("INET %-9d %-9d %-9d\n",
+ s.raw4+s.udp4+s.tcp4_hashed+
+ s.raw6+s.udp6+s.tcp6_hashed,
+ s.raw4+s.udp4+s.tcp4_hashed,
+ s.raw6+s.udp6+s.tcp6_hashed);
+ printf("FRAG %-9d %-9d %-9d\n", s.frag4+s.frag6, s.frag4, s.frag6);
+
+ printf("\n");
+
+ return 0;
+}
+
+static void _usage(FILE *dest)
+{
+ fprintf(dest,
+"Usage: ss [ OPTIONS ]\n"
+" ss [ OPTIONS ] [ FILTER ]\n"
+" -h, --help this message\n"
+" -V, --version output version information\n"
+" -n, --numeric don't resolve service names\n"
+" -r, --resolve resolve host names\n"
+" -a, --all display all sockets\n"
+" -l, --listening display listening sockets\n"
+" -o, --options show timer information\n"
+" -e, --extended show detailed socket information\n"
+" -m, --memory show socket memory usage\n"
+" -p, --processes show process using socket\n"
+" -i, --info show internal TCP information\n"
+" -s, --summary show socket usage summary\n"
+"\n"
+" -4, --ipv4 display only IP version 4 sockets\n"
+" -6, --ipv6 display only IP version 6 sockets\n"
+" -0, --packet display PACKET sockets\n"
+" -t, --tcp display only TCP sockets\n"
+" -u, --udp display only UDP sockets\n"
+" -d, --dccp display only DCCP sockets\n"
+" -w, --raw display only RAW sockets\n"
+" -x, --unix display only Unix domain sockets\n"
+" -f, --family=FAMILY display sockets of type FAMILY\n"
+"\n"
+" -A, --query=QUERY, --socket=QUERY\n"
+" QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]\n"
+"\n"
+" -D, --diag=FILE Dump raw information about TCP sockets to FILE\n"
+" -F, --filter=FILE read filter information from FILE\n"
+" FILTER := [ state TCP-STATE ] [ EXPRESSION ]\n"
+ );
+}
+
+static void help(void) __attribute__((noreturn));
+static void help(void)
+{
+ _usage(stdout);
+ exit(0);
+}
+
+static void usage(void) __attribute__((noreturn));
+static void usage(void)
+{
+ _usage(stderr);
+ exit(-1);
+}
+
+
+int scan_state(const char *state)
+{
+ int i;
+ if (strcasecmp(state, "close") == 0 ||
+ strcasecmp(state, "closed") == 0)
+ return (1<<SS_CLOSE);
+ if (strcasecmp(state, "syn-rcv") == 0)
+ return (1<<SS_SYN_RECV);
+ if (strcasecmp(state, "established") == 0)
+ return (1<<SS_ESTABLISHED);
+ if (strcasecmp(state, "all") == 0)
+ return SS_ALL;
+ if (strcasecmp(state, "connected") == 0)
+ return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN));
+ if (strcasecmp(state, "synchronized") == 0)
+ return SS_ALL & ~((1<<SS_CLOSE)|(1<<SS_LISTEN)|(1<<SS_SYN_SENT));
+ if (strcasecmp(state, "bucket") == 0)
+ return (1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT);
+ if (strcasecmp(state, "big") == 0)
+ return SS_ALL & ~((1<<SS_SYN_RECV)|(1<<SS_TIME_WAIT));
+ for (i=0; i<SS_MAX; i++) {
+ if (strcasecmp(state, sstate_namel[i]) == 0)
+ return (1<<i);
+ }
+ return 0;
+}
+
+static const struct option long_opts[] = {
+ { "numeric", 0, 0, 'n' },
+ { "resolve", 0, 0, 'r' },
+ { "options", 0, 0, 'o' },
+ { "extended", 0, 0, 'e' },
+ { "memory", 0, 0, 'm' },
+ { "info", 0, 0, 'i' },
+ { "processes", 0, 0, 'p' },
+ { "dccp", 0, 0, 'd' },
+ { "tcp", 0, 0, 't' },
+ { "udp", 0, 0, 'u' },
+ { "raw", 0, 0, 'w' },
+ { "unix", 0, 0, 'x' },
+ { "all", 0, 0, 'a' },
+ { "listening", 0, 0, 'l' },
+ { "ipv4", 0, 0, '4' },
+ { "ipv6", 0, 0, '6' },
+ { "packet", 0, 0, '0' },
+ { "family", 1, 0, 'f' },
+ { "socket", 1, 0, 'A' },
+ { "query", 1, 0, 'A' },
+ { "summary", 0, 0, 's' },
+ { "diag", 1, 0, 'D' },
+ { "filter", 1, 0, 'F' },
+ { "version", 0, 0, 'V' },
+ { "help", 0, 0, 'h' },
+ { 0 }
+
+};
+
+int main(int argc, char *argv[])
+{
+ int do_default = 1;
+ int saw_states = 0;
+ int saw_query = 0;
+ int do_summary = 0;
+ const char *dump_tcpdiag = NULL;
+ FILE *filter_fp = NULL;
+ int ch;
+
+ memset(¤t_filter, 0, sizeof(current_filter));
+
+ current_filter.states = default_filter.states;
+
+ while ((ch = getopt_long(argc, argv, "dhaletuwxnro460spf:miA:D:F:vV",
+ long_opts, NULL)) != EOF) {
+ switch(ch) {
+ case 'n':
+ resolve_services = 0;
+ break;
+ case 'r':
+ resolve_hosts = 1;
+ break;
+ case 'o':
+ show_options = 1;
+ break;
+ case 'e':
+ show_options = 1;
+ show_details++;
+ break;
+ case 'm':
+ show_mem = 1;
+ break;
+ case 'i':
+ show_tcpinfo = 1;
+ break;
+ case 'p':
+ show_users++;
+ user_ent_hash_build();
+ break;
+ case 'd':
+ current_filter.dbs |= (1<<DCCP_DB);
+ do_default = 0;
+ break;
+ case 't':
+ current_filter.dbs |= (1<<TCP_DB);
+ do_default = 0;
+ break;
+ case 'u':
+ current_filter.dbs |= (1<<UDP_DB);
+ do_default = 0;
+ break;
+ case 'w':
+ current_filter.dbs |= (1<<RAW_DB);
+ do_default = 0;
+ break;
+ case 'x':
+ current_filter.dbs |= UNIX_DBM;
+ do_default = 0;
+ break;
+ case 'a':
+ current_filter.states = SS_ALL;
+ break;
+ case 'l':
+ current_filter.states = (1<<SS_LISTEN) | (1<<SS_CLOSE);
+ break;
+ case '4':
+ preferred_family = AF_INET;
+ break;
+ case '6':
+ preferred_family = AF_INET6;
+ break;
+ case '0':
+ preferred_family = AF_PACKET;
+ break;
+ case 'f':
+ if (strcmp(optarg, "inet") == 0)
+ preferred_family = AF_INET;
+ else if (strcmp(optarg, "inet6") == 0)
+ preferred_family = AF_INET6;
+ else if (strcmp(optarg, "link") == 0)
+ preferred_family = AF_PACKET;
+ else if (strcmp(optarg, "unix") == 0)
+ preferred_family = AF_UNIX;
+ else if (strcmp(optarg, "netlink") == 0)
+ preferred_family = AF_NETLINK;
+ else if (strcmp(optarg, "help") == 0)
+ help();
+ else {
+ fprintf(stderr, "ss: \"%s\" is invalid family\n", optarg);
+ usage();
+ }
+ break;
+ case 'A':
+ {
+ char *p, *p1;
+ if (!saw_query) {
+ current_filter.dbs = 0;
+ saw_query = 1;
+ do_default = 0;
+ }
+ p = p1 = optarg;
+ do {
+ if ((p1 = strchr(p, ',')) != NULL)
+ *p1 = 0;
+ if (strcmp(p, "all") == 0) {
+ current_filter.dbs = ALL_DB;
+ } else if (strcmp(p, "inet") == 0) {
+ current_filter.dbs |= (1<<TCP_DB)|(1<<DCCP_DB)|(1<<UDP_DB)|(1<<RAW_DB);
+ } else if (strcmp(p, "udp") == 0) {
+ current_filter.dbs |= (1<<UDP_DB);
+ } else if (strcmp(p, "dccp") == 0) {
+ current_filter.dbs |= (1<<DCCP_DB);
+ } else if (strcmp(p, "tcp") == 0) {
+ current_filter.dbs |= (1<<TCP_DB);
+ } else if (strcmp(p, "raw") == 0) {
+ current_filter.dbs |= (1<<RAW_DB);
+ } else if (strcmp(p, "unix") == 0) {
+ current_filter.dbs |= UNIX_DBM;
+ } else if (strcasecmp(p, "unix_stream") == 0 ||
+ strcmp(p, "u_str") == 0) {
+ current_filter.dbs |= (1<<UNIX_ST_DB);
+ } else if (strcasecmp(p, "unix_dgram") == 0 ||
+ strcmp(p, "u_dgr") == 0) {
+ current_filter.dbs |= (1<<UNIX_DG_DB);
+ } else if (strcmp(p, "packet") == 0) {
+ current_filter.dbs |= PACKET_DBM;
+ } else if (strcmp(p, "packet_raw") == 0 ||
+ strcmp(p, "p_raw") == 0) {
+ current_filter.dbs |= (1<<PACKET_R_DB);
+ } else if (strcmp(p, "packet_dgram") == 0 ||
+ strcmp(p, "p_dgr") == 0) {
+ current_filter.dbs |= (1<<PACKET_DG_DB);
+ } else if (strcmp(p, "netlink") == 0) {
+ current_filter.dbs |= (1<<NETLINK_DB);
+ } else {
+ fprintf(stderr, "ss: \"%s\" is illegal socket table id\n", p);
+ usage();
+ }
+ p = p1 + 1;
+ } while (p1);
+ break;
+ }
+ case 's':
+ do_summary = 1;
+ break;
+ case 'D':
+ dump_tcpdiag = optarg;
+ break;
+ case 'F':
+ if (filter_fp) {
+ fprintf(stderr, "More than one filter file\n");
+ exit(-1);
+ }
+ if (optarg[0] == '-')
+ filter_fp = stdin;
+ else
+ filter_fp = fopen(optarg, "r");
+ if (!filter_fp) {
+ perror("fopen filter file");
+ exit(-1);
+ }
+ break;
+ case 'v':
+ case 'V':
+ printf("ss utility, iproute2-ss%s\n", SNAPSHOT);
+ exit(0);
+ case 'h':
+ case '?':
+ help();
+ default:
+ usage();
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ get_slabstat(&slabstat);
+
+ if (do_summary) {
+ print_summary();
+ if (do_default && argc == 0)
+ exit(0);
+ }
+
+ if (do_default)
+ current_filter.dbs = default_filter.dbs;
+
+ if (preferred_family == AF_UNSPEC) {
+ if (!(current_filter.dbs&~UNIX_DBM))
+ preferred_family = AF_UNIX;
+ else if (!(current_filter.dbs&~PACKET_DBM))
+ preferred_family = AF_PACKET;
+ else if (!(current_filter.dbs&~(1<<NETLINK_DB)))
+ preferred_family = AF_NETLINK;
+ }
+
+ if (preferred_family != AF_UNSPEC) {
+ int mask2;
+ if (preferred_family == AF_INET ||
+ preferred_family == AF_INET6) {
+ mask2= current_filter.dbs;
+ } else if (preferred_family == AF_PACKET) {
+ mask2 = PACKET_DBM;
+ } else if (preferred_family == AF_UNIX) {
+ mask2 = UNIX_DBM;
+ } else if (preferred_family == AF_NETLINK) {
+ mask2 = (1<<NETLINK_DB);
+ } else {
+ mask2 = 0;
+ }
+
+ if (do_default)
+ current_filter.dbs = mask2;
+ else
+ current_filter.dbs &= mask2;
+ current_filter.families = (1<<preferred_family);
+ } else {
+ if (!do_default)
+ current_filter.families = ~0;
+ else
+ current_filter.families = default_filter.families;
+ }
+ if (current_filter.dbs == 0) {
+ fprintf(stderr, "ss: no socket tables to show with such filter.\n");
+ exit(0);
+ }
+ if (current_filter.families == 0) {
+ fprintf(stderr, "ss: no families to show with such filter.\n");
+ exit(0);
+ }
+
+ if (resolve_services && resolve_hosts &&
+ (current_filter.dbs&(UNIX_DBM|(1<<TCP_DB)|(1<<UDP_DB)|(1<<DCCP_DB))))
+ init_service_resolver();
+
+ /* Now parse filter... */
+ if (argc == 0 && filter_fp) {
+ if (ssfilter_parse(¤t_filter.f, 0, NULL, filter_fp))
+ usage();
+ }
+
+ while (argc > 0) {
+ if (strcmp(*argv, "state") == 0) {
+ NEXT_ARG();
+ if (!saw_states)
+ current_filter.states = 0;
+ current_filter.states |= scan_state(*argv);
+ saw_states = 1;
+ } else if (strcmp(*argv, "exclude") == 0 ||
+ strcmp(*argv, "excl") == 0) {
+ NEXT_ARG();
+ if (!saw_states)
+ current_filter.states = SS_ALL;
+ current_filter.states &= ~scan_state(*argv);
+ saw_states = 1;
+ } else {
+ if (ssfilter_parse(¤t_filter.f, argc, argv, filter_fp))
+ usage();
+ break;
+ }
+ argc--; argv++;
+ }
+
+ if (current_filter.states == 0) {
+ fprintf(stderr, "ss: no socket states to show with such filter.\n");
+ exit(0);
+ }
+
+ if (dump_tcpdiag) {
+ FILE *dump_fp = stdout;
+ if (!(current_filter.dbs & (1<<TCP_DB))) {
+ fprintf(stderr, "ss: tcpdiag dump requested and no tcp in filter.\n");
+ exit(0);
+ }
+ if (dump_tcpdiag[0] != '-') {
+ dump_fp = fopen(dump_tcpdiag, "w");
+ if (!dump_tcpdiag) {
+ perror("fopen dump file");
+ exit(-1);
+ }
+ }
+ tcp_show_netlink(¤t_filter, dump_fp, TCPDIAG_GETSOCK);
+ fflush(dump_fp);
+ exit(0);
+ }
+
+ netid_width = 0;
+ if (current_filter.dbs&(current_filter.dbs-1))
+ netid_width = 5;
+
+ state_width = 0;
+ if (current_filter.states&(current_filter.states-1))
+ state_width = 10;
+
+ screen_width = 80;
+ if (isatty(STDOUT_FILENO)) {
+ struct winsize w;
+
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) {
+ if (w.ws_col > 0)
+ screen_width = w.ws_col;
+ }
+ }
+
+ addrp_width = screen_width;
+ addrp_width -= netid_width+1;
+ addrp_width -= state_width+1;
+ addrp_width -= 14;
+
+ if (addrp_width&1) {
+ if (netid_width)
+ netid_width++;
+ else if (state_width)
+ state_width++;
+ }
+
+ addrp_width /= 2;
+ addrp_width--;
+
+ serv_width = resolve_services ? 7 : 5;
+
+ if (addrp_width < 15+serv_width+1)
+ addrp_width = 15+serv_width+1;
+
+ addr_width = addrp_width - serv_width - 1;
+
+ if (netid_width)
+ printf("%-*s ", netid_width, "Netid");
+ if (state_width)
+ printf("%-*s ", state_width, "State");
+ printf("%-6s %-6s ", "Recv-Q", "Send-Q");
+
+ printf("%*s:%-*s %*s:%-*s\n",
+ addr_width, "Local Address", serv_width, "Port",
+ addr_width, "Peer Address", serv_width, "Port");
+
+ fflush(stdout);
+
+ if (current_filter.dbs & (1<<NETLINK_DB))
+ netlink_show(¤t_filter);
+ if (current_filter.dbs & PACKET_DBM)
+ packet_show(¤t_filter);
+ if (current_filter.dbs & UNIX_DBM)
+ unix_show(¤t_filter);
+ if (current_filter.dbs & (1<<RAW_DB))
+ raw_show(¤t_filter);
+ if (current_filter.dbs & (1<<UDP_DB))
+ udp_show(¤t_filter);
+ if (current_filter.dbs & (1<<TCP_DB))
+ tcp_show(¤t_filter, TCPDIAG_GETSOCK);
+ if (current_filter.dbs & (1<<DCCP_DB))
+ tcp_show(¤t_filter, DCCPDIAG_GETSOCK);
+ return 0;
+}
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/ssfilter.h b/ap/app/iproute2/iproute2-3.4.0/misc/ssfilter.h
new file mode 100755
index 0000000..00b92e3
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/ssfilter.h
@@ -0,0 +1,21 @@
+#define SSF_DCOND 0
+#define SSF_SCOND 1
+#define SSF_OR 2
+#define SSF_AND 3
+#define SSF_NOT 4
+#define SSF_D_GE 5
+#define SSF_D_LE 6
+#define SSF_S_GE 7
+#define SSF_S_LE 8
+#define SSF_S_AUTO 9
+
+struct ssfilter
+{
+ int type;
+ struct ssfilter *post;
+ struct ssfilter *pred;
+};
+
+int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp);
+void *parse_hostcond(char*);
+
diff --git a/ap/app/iproute2/iproute2-3.4.0/misc/ssfilter.y b/ap/app/iproute2/iproute2-3.4.0/misc/ssfilter.y
new file mode 100755
index 0000000..2e9d962
--- /dev/null
+++ b/ap/app/iproute2/iproute2-3.4.0/misc/ssfilter.y
@@ -0,0 +1,275 @@
+%{
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <malloc.h>
+#include <string.h>
+#include "ssfilter.h"
+
+typedef struct ssfilter * ssfilter_t;
+
+#define YYSTYPE ssfilter_t
+
+static struct ssfilter * alloc_node(int type, void *pred)
+{
+ struct ssfilter *n = malloc(sizeof(*n));
+ if (n == NULL)
+ abort();
+ n->type = type;
+ n->pred = pred;
+ n->post = NULL;
+ return n;
+}
+
+static char **yy_argv;
+static int yy_argc;
+static FILE *yy_fp;
+static ssfilter_t *yy_ret;
+
+static int yylex(void);
+
+static void yyerror(char *s)
+{
+ fprintf(stderr, "ss: bison bellows (while parsing filter): \"%s!\"", s);
+}
+
+%}
+
+%token HOSTCOND DCOND SCOND DPORT SPORT LEQ GEQ NEQ AUTOBOUND
+%left '|'
+%left '&'
+%nonassoc '!'
+
+%%
+applet: null expr
+ {
+ *yy_ret = $2;
+ $$ = $2;
+ }
+ | null
+ ;
+null: /* NOTHING */ { $$ = NULL; }
+ ;
+expr: DCOND HOSTCOND
+ {
+ $$ = alloc_node(SSF_DCOND, $2);
+ }
+ | SCOND HOSTCOND
+ {
+ $$ = alloc_node(SSF_SCOND, $2);
+ }
+ | DPORT GEQ HOSTCOND
+ {
+ $$ = alloc_node(SSF_D_GE, $3);
+ }
+ | DPORT LEQ HOSTCOND
+ {
+ $$ = alloc_node(SSF_D_LE, $3);
+ }
+ | DPORT '>' HOSTCOND
+ {
+ $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_LE, $3));
+ }
+ | DPORT '<' HOSTCOND
+ {
+ $$ = alloc_node(SSF_NOT, alloc_node(SSF_D_GE, $3));
+ }
+ | DPORT '=' HOSTCOND
+ {
+ $$ = alloc_node(SSF_DCOND, $3);
+ }
+ | DPORT NEQ HOSTCOND
+ {
+ $$ = alloc_node(SSF_NOT, alloc_node(SSF_DCOND, $3));
+ }
+
+ | SPORT GEQ HOSTCOND
+ {
+ $$ = alloc_node(SSF_S_GE, $3);
+ }
+ | SPORT LEQ HOSTCOND
+ {
+ $$ = alloc_node(SSF_S_LE, $3);
+ }
+ | SPORT '>' HOSTCOND
+ {
+ $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_LE, $3));
+ }
+ | SPORT '<' HOSTCOND
+ {
+ $$ = alloc_node(SSF_NOT, alloc_node(SSF_S_GE, $3));
+ }
+ | SPORT '=' HOSTCOND
+ {
+ $$ = alloc_node(SSF_SCOND, $3);
+ }
+ | SPORT NEQ HOSTCOND
+ {
+ $$ = alloc_node(SSF_NOT, alloc_node(SSF_SCOND, $3));
+ }
+
+ | AUTOBOUND
+ {
+ $$ = alloc_node(SSF_S_AUTO, NULL);
+ }
+ | expr '|' expr
+ {
+ $$ = alloc_node(SSF_OR, $1);
+ $$->post = $3;
+ }
+ | expr expr
+ {
+ $$ = alloc_node(SSF_AND, $1);
+ $$->post = $2;
+ }
+ | expr '&' expr
+
+ {
+ $$ = alloc_node(SSF_AND, $1);
+ $$->post = $3;
+ }
+ | '!' expr
+ {
+ $$ = alloc_node(SSF_NOT, $2);
+ }
+ | '(' expr ')'
+ {
+ $$ = $2;
+ }
+;
+%%
+
+static char *get_token_from_line(char **ptr)
+{
+ char *tok, *cp = *ptr;
+
+ while (*cp == ' ' || *cp == '\t') cp++;
+
+ if (*cp == 0) {
+ *ptr = cp;
+ return NULL;
+ }
+
+ tok = cp;
+
+ while (*cp != 0 && *cp != ' ' && *cp != '\t') {
+ /* Backslash escapes everything. */
+ if (*cp == '\\') {
+ char *tp;
+ for (tp = cp; tp != tok; tp--)
+ *tp = *(tp-1);
+ cp++;
+ tok++;
+ if (*cp == 0)
+ break;
+ }
+ cp++;
+ }
+ if (*cp)
+ *cp++ = 0;
+ *ptr = cp;
+ return tok;
+}
+
+int yylex(void)
+{
+ static char argbuf[1024];
+ static char *tokptr = argbuf;
+ static int argc;
+ char *curtok;
+
+ do {
+ while (*tokptr == 0) {
+ tokptr = NULL;
+ if (argc < yy_argc) {
+ tokptr = yy_argv[argc];
+ argc++;
+ } else if (yy_fp) {
+ while (tokptr == NULL) {
+ if (fgets(argbuf, sizeof(argbuf)-1, yy_fp) == NULL)
+ return 0;
+ argbuf[sizeof(argbuf)-1] = 0;
+ if (strlen(argbuf) == sizeof(argbuf) - 1) {
+ fprintf(stderr, "Too long line in filter");
+ exit(-1);
+ }
+ if (argbuf[strlen(argbuf)-1] == '\n')
+ argbuf[strlen(argbuf)-1] = 0;
+ if (argbuf[0] == '#' || argbuf[0] == '0')
+ continue;
+ tokptr = argbuf;
+ }
+ } else {
+ return 0;
+ }
+ }
+ } while ((curtok = get_token_from_line(&tokptr)) == NULL);
+
+ if (strcmp(curtok, "!") == 0 ||
+ strcmp(curtok, "not") == 0)
+ return '!';
+ if (strcmp(curtok, "&") == 0 ||
+ strcmp(curtok, "&&") == 0 ||
+ strcmp(curtok, "and") == 0)
+ return '&';
+ if (strcmp(curtok, "|") == 0 ||
+ strcmp(curtok, "||") == 0 ||
+ strcmp(curtok, "or") == 0)
+ return '|';
+ if (strcmp(curtok, "(") == 0)
+ return '(';
+ if (strcmp(curtok, ")") == 0)
+ return ')';
+ if (strcmp(curtok, "dst") == 0)
+ return DCOND;
+ if (strcmp(curtok, "src") == 0)
+ return SCOND;
+ if (strcmp(curtok, "dport") == 0)
+ return DPORT;
+ if (strcmp(curtok, "sport") == 0)
+ return SPORT;
+ if (strcmp(curtok, ">=") == 0 ||
+ strcmp(curtok, "ge") == 0 ||
+ strcmp(curtok, "geq") == 0)
+ return GEQ;
+ if (strcmp(curtok, "<=") == 0 ||
+ strcmp(curtok, "le") == 0 ||
+ strcmp(curtok, "leq") == 0)
+ return LEQ;
+ if (strcmp(curtok, "!=") == 0 ||
+ strcmp(curtok, "ne") == 0 ||
+ strcmp(curtok, "neq") == 0)
+ return NEQ;
+ if (strcmp(curtok, "=") == 0 ||
+ strcmp(curtok, "==") == 0 ||
+ strcmp(curtok, "eq") == 0)
+ return '=';
+ if (strcmp(curtok, ">") == 0 ||
+ strcmp(curtok, "gt") == 0)
+ return '>';
+ if (strcmp(curtok, "<") == 0 ||
+ strcmp(curtok, "lt") == 0)
+ return '<';
+ if (strcmp(curtok, "autobound") == 0)
+ return AUTOBOUND;
+ yylval = (void*)parse_hostcond(curtok);
+ if (yylval == NULL) {
+ fprintf(stderr, "Cannot parse dst/src address.\n");
+ exit(1);
+ }
+ return HOSTCOND;
+}
+
+int ssfilter_parse(struct ssfilter **f, int argc, char **argv, FILE *fp)
+{
+ yy_argc = argc;
+ yy_argv = argv;
+ yy_fp = fp;
+ yy_ret = f;
+
+ if (yyparse()) {
+ fprintf(stderr, " Sorry.\n");
+ return -1;
+ }
+ return 0;
+}