Package: net-tools
Severity: enhancement

----- Forwarded message from Varun Chandramohan <[EMAIL PROTECTED]> -----

Envelope-to: [EMAIL PROTECTED]
Delivery-date: Thu, 29 Nov 2007 03:06:26 +0100
From: Varun Chandramohan <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: Patch to support ipv6 stats in netstat
X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on 
        calista.eckenfels.net
X-Spam-Level: 
X-Spam-Status: No, score=-1.3 required=4.7 tests=AWL,BAYES_00 autolearn=ham 
        version=3.0.3

Hi Bernd,

     Here is the patch for enabling ipv6 stats in netstat. Let me know if
the patch applies well. This patch should scale well and have full stats
suport.
Let me know if its fine.

Regards,
Varun

diff -uprN net-tools-1.60/netstat.c net-tools-1.60.modi/netstat.c
--- net-tools-1.60/netstat.c    2001-04-15 20:11:17.000000000 +0530
+++ net-tools-1.60.modi/netstat.c    2007-07-03 16:15:13.000000000 +0530
@@ -104,6 +104,8 @@
 /* prototypes for statistics.c */
 void parsesnmp(int, int, int);
 void inittab(void);
+void parsesnmp6(int, int, int);
+void inittab6(void);

 typedef enum {
     SS_FREE = 0,        /* not allocated                */
@@ -1688,9 +1690,29 @@ int main
     }

     if (flag_sta) {
-        inittab();
-    parsesnmp(flag_raw, flag_tcp, flag_udp);
-    exit(0);
+        char *tmp1, *tmp2;
+        char buf[256];
+        if (!afname[0]) {
+            inittab();
+            parsesnmp(flag_raw, flag_tcp, flag_udp);
+        } else {
+            safe_strncpy(buf, afname, sizeof(buf));
+            tmp1 = buf;
+            if ((tmp2 = index(tmp1, ',')))
+                 printf("Multiple interface\n");
+            else if(!strncmp(buf,"inet6",5)) {
+#if HAVE_AFINET6
+                 inittab6();
+                 parsesnmp6(flag_raw, flag_tcp, flag_udp);
+#else
+                 printf("Address type not supported for stats\n");
+#endif
+            }
+            else
+                 printf("Address type not supported for stats\n");
+        }
+        exit(1);
+
     }

     if (flag_rou) {
diff -uprN net-tools-1.60/statistics.c net-tools-1.60.modi/statistics.c
--- net-tools-1.60/statistics.c    2001-02-02 23:31:23.000000000 +0530
+++ net-tools-1.60.modi/statistics.c    2007-07-03 16:22:29.000000000 +0530
@@ -83,6 +83,32 @@ struct entry Iptab[] =
     {"FragCreates", N_("%u fragments created"), opt_number}
 };

+struct entry Ip6tab[] =
+{
+    {"Ip6InReceives", N_("%u total packets received"), number},
+    {"Ip6InHdrErrors", N_("%u with invalid headers"), opt_number},
+    {"Ip6InTooBigErrors", N_("%u with packets too big"), opt_number},
+    {"Ip6InNoRoutes", N_("%u incoming packets with no route"), opt_number},
+    {"Ip6InAddrErrors", N_("%u with invalid addresses"), opt_number},
+    {"Ip6InUnknownProtos", N_("%u with unknown protocol"), opt_number},
+    {"Ip6InTruncatedPkts", N_("%u with truncated packets"), opt_number},
+    {"Ip6InDiscards", N_("%u incoming packets discarded"), number},
+    {"Ip6InDelivers", N_("%u incoming packets delivered"), number},
+    {"Ip6OutForwDatagrams", N_("%u forwarded"), number},
+    {"Ip6OutRequests", N_("%u requests sent out"), number},     /*? */
+    {"Ip6OutDiscards", N_("%u outgoing packets dropped"), opt_number},
+    {"Ip6OutNoRoutes", N_("%u dropped because of missing route"),
opt_number},
+    {"Ip6ReasmTimeout", N_("%u fragments dropped after timeout"),
opt_number},
+    {"Ip6ReasmReqds", N_("%u reassemblies required"), opt_number},
/* ? */
+    {"Ip6ReasmOKs", N_("%u packets reassembled ok"), opt_number},
+    {"Ip6ReasmFails", N_("%u packet reassembles failed"), opt_number},
+    {"Ip6FragOKs", N_("%u fragments received ok"), opt_number},
+    {"Ip6FragFails", N_("%u fragments failed"), opt_number},
+    {"Ip6FragCreates", N_("%u fragments created"), opt_number},
+    {"Ip6InMcastPkts", N_("%u incoming multicast packets"), opt_number},
+    {"Ip6OutMcastPkts", N_("%u outgoing multicast packets"), opt_number}
+};
+
 struct entry Icmptab[] =
 {
     {"InMsgs", N_("%u ICMP messages received"), number},
@@ -113,6 +139,41 @@ struct entry Icmptab[] =
     {"OutAddrMaskReps", N_("address mask replies: %u"), i_outp_icmp |
I_TITLE},
 };

+struct entry Icmp6tab[] =
+{
+    {"Icmp6InMsgs", N_("%u ICMP messages received"), number},
+    {"Icmp6InErrors", N_("%u input ICMP message failed."), number},
+    {"Icmp6InDestUnreachs", N_("destination unreachable: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InPktTooBigs", N_("packets too big: %u"), i_inp_icmp | I_TITLE},
+    {"Icmp6InTimeExcds", N_("received ICMPv6 time exceeded: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InParmProblems", N_("parameter problem: %u"), i_inp_icmp |
I_TITLE},
+    {"Icmp6InEchos", N_("echo requests: %u"), i_inp_icmp | I_TITLE},
+    {"Icmp6InEchoReplies", N_("echo replies: %u"), i_inp_icmp | I_TITLE},
+    {"Icmp6InGroupMembQueries", N_("group member queries: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InGroupMembResponses", N_("group member responses: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InGroupMembReductions", N_("group member reductions: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InRouterSolicits", N_("router solicits: %u"), i_inp_icmp |
I_TITLE},
+    {"Icmp6InRouterAdvertisements", N_("router advertisement: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InNeighborSolicits", N_("neighbour solicits: %u"),
i_inp_icmp | I_TITLE},
+    {"Icmp6InNeighborAdvertisements", N_("neighbour advertisement:
%u"), i_inp_icmp | I_TITLE},
+    {"Icmp6InRedirects", N_("redirects: %u"), i_inp_icmp | I_TITLE},
+    {"Icmp6OutMsgs", N_("%u ICMP messages sent"), number},
+    {"Icmp6OutDestUnreachs", N_("destination unreachable: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutPktTooBigs", N_("packets too big: %u"), i_outp_icmp |
I_TITLE},
+    {"Icmp6OutTimeExcds", N_("sent ICMPv6 time exceeded: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutParmProblems", N_("parameter problem: %u"), i_outp_icmp |
I_TITLE},
+    {"Icmp6OutEchos", N_("echo requests: %u"), i_outp_icmp | I_TITLE},
+    {"Icmp6OutEchoReplies", N_("echo replies: %u"), i_outp_icmp | I_TITLE},
+    {"Icmp6OutGroupMembQueries", N_("group member queries: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutGroupMembResponses", N_("group member responses: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutGroupMembReductions", N_("group member reductions: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutRouterSolicits", N_("router solicits: %u"), i_outp_icmp |
I_TITLE},
+    {"Icmp6OutRouterAdvertisements ", N_("router advertisement: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutNeighborSolicits", N_("neighbor solicits: %u"),
i_outp_icmp | I_TITLE},
+    {"Icmp6OutNeighborAdvertisements", N_("neighbor advertisements:
%u"), i_outp_icmp | I_TITLE},
+    {"Icmp6OutRedirects", N_("redirects: %u"), i_outp_icmp | I_TITLE},
+};
+
 struct entry Tcptab[] =
 {
     {"RtoAlgorithm", N_("RTO algorithm is %s"), i_rto_alg | I_STATIC},
@@ -139,6 +200,14 @@ struct entry Udptab[] =
     {"OutDatagrams", N_("%u packets sent"), number},
 };

+struct entry Udp6tab[] =
+{
+    {"Udp6InDatagrams", N_("%u packets received"), number},
+    {"Udp6NoPorts", N_("%u packets to unknown port received."), number},
+    {"Udp6InErrors", N_("%u packet receive errors"), number},
+    {"Udp6OutDatagrams", N_("%u packets sent"), number},
+};
+
 struct entry Tcpexttab[] =
 {
     {"SyncookiesSent", N_("%u SYN cookies sent"), opt_number},
@@ -204,6 +273,15 @@ struct tabtab snmptabs[] =
     {NULL}
 };

+struct tabtab snmp6tabs[] =
+{
+    {"Ip6", Ip6tab, sizeof(Ip6tab), &f_raw},
+    {"Icmp6", Icmp6tab, sizeof(Icmp6tab), &f_raw},
+    {"Udp6", Udp6tab, sizeof(Udp6tab), &f_udp},
+    {"Tcp6", Tcptab, sizeof(Tcptab), &f_tcp},
+    {NULL}
+};
+
 /* XXX IGMP */

 int cmpentries(const void *a, const void *b)
@@ -289,7 +367,7 @@ struct tabtab *newtable(struct tabtab *t
     return &dummytab;
 }

-void process_fd(FILE *f)
+void process_fd(FILE *f, int all, char *filter)
 {
     char buf1[1024], buf2[1024];
     char *sp, *np, *p;
@@ -304,6 +382,10 @@ void process_fd(FILE *f)
     if (!np || !sp)
         goto formaterr;
     *sp = '\0';
+
+        if (!all)
+           if (strncmp(buf1, filter, strlen(filter)))
+               continue;

     tab = newtable(snmptabs, buf1);
     if (tab == NULL) {
@@ -337,6 +419,41 @@ formaterr:
   return;
 }

+void cpytitle(char *original, char *new)
+{
+     char *ptr = original;
+     while(*ptr != '6' && *ptr != '\0') {
+           *new = *ptr;
+            new++;
+            ptr++;
+     }
+    *new = *ptr;
+    new++;
+    *new = '\0';
+}
+
+void process6_fd(FILE *f)
+{
+   char buf1[1024],buf2[50],buf3[1024];
+   unsigned long val;
+   struct tabtab *tab = NULL;
+   int cpflg = 0;
+
+   while (fgets(buf1, sizeof buf1, f)) {
+          sscanf(buf1, "%s %lu", buf2, &val);
+          if(!cpflg) {
+             cpytitle(buf2, buf3);
+             tab = newtable(snmp6tabs, buf3);
+             cpflg = 1;
+          }
+          if(!strstr(buf2, buf3)) {
+             cpytitle(buf2, buf3);
+             tab = newtable(snmp6tabs, buf3);
+          }
+          printval(tab, buf2, val);
+   }
+
+}

 void parsesnmp(int flag_raw, int flag_tcp, int flag_udp)
 {
@@ -349,7 +466,7 @@ void parsesnmp(int flag_raw, int flag_tc
     perror(_("cannot open /proc/net/snmp"));
     return;
     }
-    process_fd(f);
+    process_fd(f, 1, NULL);

     if (ferror(f))
     perror("/proc/net/snmp");
@@ -359,7 +476,7 @@ void parsesnmp(int flag_raw, int flag_tc
     f = fopen("/proc/net/netstat", "r");

     if (f) {
-        process_fd(f);
+        process_fd(f, 1, NULL);

         if (ferror(f))
         perror("/proc/net/netstat");
@@ -369,6 +486,33 @@ void parsesnmp(int flag_raw, int flag_tc
     return;
 }

+void parsesnmp6(int flag_raw, int flag_tcp, int flag_udp)
+{
+    FILE *f;
+
+    f_raw = flag_raw; f_tcp = flag_tcp; f_udp = flag_udp;
+
+    f = fopen("/proc/net/snmp6", "r");
+    if (!f) {
+        perror(_("cannot open /proc/net/snmp6"));
+        return;
+    }
+    process6_fd(f);
+    if (ferror(f))
+        perror("/proc/net/snmp6");
+
+    fclose(f);
+    f = fopen("/proc/net/snmp", "r");
+    if (!f) {
+        perror(_("cannot open /proc/net/snmp"));
+        return;
+    }
+    process_fd(f, 0, "Tcp");
+    if (ferror(f))
+        perror("/proc/net/snmp");
+
+    fclose(f);
+}

 void inittab(void)
 {
@@ -379,3 +523,13 @@ void inittab(void)
     qsort(t->tab, t->size / sizeof(struct entry),
           sizeof(struct entry), cmpentries);
 }
+
+void inittab6(void)
+{
+    struct tabtab *t;
+
+    for (t = snmp6tabs; t->title; t++)
+        qsort(t->tab, t->size / sizeof(struct entry),
+              sizeof(struct entry), cmpentries);
+}
+



----- End forwarded message -----

-- 
  (OO)     -- [EMAIL PROTECTED] --
 ( .. )    [EMAIL PROTECTED],linux.de,debian.org}  http://www.eckes.org/
  o--o   1024D/E383CD7E  [EMAIL PROTECTED]  v:+497211603874  f:+49721151516129
(O____O)  When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl!



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to