changeset 1127:e0a8cbb1a5e3

The return of ifconfig cleanup.
author Rob Landley <rob@landley.net>
date Sat, 23 Nov 2013 20:02:10 -0600
parents 2131db66a732
children 9eaec92e3713
files toys/pending/ifconfig.c
diffstat 1 files changed, 31 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/ifconfig.c	Wed Nov 20 23:01:49 2013 -0600
+++ b/toys/pending/ifconfig.c	Sat Nov 23 20:02:10 2013 -0600
@@ -46,8 +46,6 @@
   } sock_u;
 } sockaddr_with_len;
 
-//for the param settings.
-
 //for ipv6 add/del
 struct ifreq_inet6 {
   struct in6_addr ifrinte6_addr;
@@ -154,7 +152,7 @@
     {ARPHRD_SIT, "IPv6-in-IPv4"}, {-1, "UNSPEC"}
   };
   int i;
-  char *p;
+  char *pp;
   FILE *fp;
   short flags;
 
@@ -181,8 +179,8 @@
   ifre.ifr_addr.sa_family = AF_INET;
   memset(&ifre.ifr_addr, 0, sizeof(ifre.ifr_addr));
   ioctl(TT.sockfd, SIOCGIFADDR, &ifre);
-  p = (char *)&ifre.ifr_addr;
-  for (i = 0; i<sizeof(ifre.ifr_addr); i++) if (p[i]) break;
+  pp = (char *)&ifre.ifr_addr;
+  for (i = 0; i<sizeof(ifre.ifr_addr); i++) if (pp[i]) break;
 
   if (i != sizeof(ifre.ifr_addr)) {
     struct sockaddr_in *si = (struct sockaddr_in *)&ifre.ifr_addr;
@@ -212,42 +210,39 @@
     xputc('\n');
   }
 
-  fp = fopen("/proc/net/if_net6", "r");
+  fp = fopen(pp = "/proc/net/if_inet6", "r");
   if (fp) {
-    char iface_name[IFNAMSIZ] = {0,};
+    char iface_name[IFNAMSIZ];
     int plen, iscope;
 
-    while(fgets(toybuf, sizeof(toybuf), fp)) {
-      int nitems = 0;
-      char ipv6_addr[40] = {0,};
+    while (fgets(toybuf, sizeof(toybuf), fp)) {
+      int nitems;
+      char ipv6_addr[40];
+
       nitems = sscanf(toybuf, "%32s %*08x %02x %02x %*02x %15s\n",
-          ipv6_addr+7, &plen, &iscope, iface_name);
-      if(nitems != 4) {
-        if((nitems < 0) && feof(fp)) break;
-        perror_exit("sscanf");
-      }
-      if(strcmp(name, iface_name) == 0) {
-        int i = 0;
-        struct sockaddr_in6 sock_in6;
-        int len = sizeof(ipv6_addr) / (sizeof ipv6_addr[0]);
-        char *ptr = ipv6_addr+7;
+                      ipv6_addr, &plen, &iscope, iface_name);
+      if (nitems<0 && feof(fp)) break;
+      if (nitems != 4) perror_exit("bad %s", pp);
 
-        while((i < len-2) && (*ptr)) {
-          ipv6_addr[i++] = *ptr++;
-          //put ':' after 4th bit
-          if(!((i+1) % 5)) ipv6_addr[i++] = ':';
-        }
-        ipv6_addr[i+1] = '\0';
-        if(inet_pton(AF_INET6, ipv6_addr, (struct sockaddr *) &sock_in6.sin6_addr) > 0) {
-          sock_in6.sin6_family = AF_INET6;
-          if(inet_ntop(AF_INET6, &sock_in6.sin6_addr, toybuf, BUFSIZ)) {
+      if (!strcmp(name, iface_name)) {
+        struct sockaddr_in6 s6;
+        char *ptr = ipv6_addr+sizeof(ipv6_addr)-1;
+
+        // convert giant hex string into colon-spearated ipv6 address by
+        // inserting ':' every 4 characters. 
+        for (i = 32; i; i--)
+          if ((*(ptr--) = ipv6_addr[i])) if (!(i&3)) *(ptr--) = ':';
+
+        // Convert to binary and back to get abbreviated :: version
+        if (inet_pton(AF_INET6, ipv6_addr, (void *)&s6.sin6_addr) > 0) {
+          if (inet_ntop(AF_INET6, &s6.sin6_addr, toybuf, sizeof(toybuf))) {
             char *scopes[] = {"Global","Host","Link","Site","Compat"},
                  *scope = "Unknown";
-            int j;
 
-            for (j=0; j < sizeof(scopes)/sizeof(*scopes); j++)
-              if (iscope == (!!j)<<(j+3)) scope = scopes[j];
-            xprintf("%10cinet6 addr: %s/%d Scope: %s\n", ' ', toybuf, plen, scope);
+            for (i=0; i < sizeof(scopes)/sizeof(*scopes); i++)
+              if (iscope == (!!i)<<(i+3)) scope = scopes[i];
+            xprintf("%10cinet6 addr: %s/%d Scope: %s\n",
+                    ' ', toybuf, plen, scope);
           }
         }
       }
@@ -265,8 +260,8 @@
       "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL
     };
 
-    for(s = str; *s; s++) {
-      if(flags & mask) xprintf("%s ", *s);
+    for (s = str; *s; s++) {
+      if (flags & mask) xprintf("%s ", *s);
       mask = mask << 1;
     }
   } else xprintf("[NO FLAGS] ");
@@ -340,7 +335,7 @@
 
     if (iface_name) {
       if (!strcmp(iface_name, name)) {
-        display_ifconfig(name, 1, val);
+        display_ifconfig(iface_name, 1, val);
 
         return;
       }