[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[patch] netcfg get_all_ifs()



This patch substitutes Linux-specific code in get_all_ifs() using getifaddrs().


I tested this patch on (linux-)i386 trunk monolithic.
It builds and seems to work without regressions.
I only tested on default qemu, and the network seems to behave as
usual (I didn't complete the installation, but it seems to
download/unpack/configure downloaded packages).

I tested this patch in a draft hackish port of netcfg on kfreebsd-i386
(see d-i kfreebsd branch), and it builds.
netcfg is not working on kfreebsd-i386, but at least now it creates a
saner /etc/network/interfaces (i.e. with ed0, not eth0).


Cheers,
Luca Favatella


P.S.
At the moment I'm trying to port netcfg on GNU/kFreeBSD.
If reading the code of netcfg you find some Linux-specific code that
could be easily rewritten to be more portable (at least on GNU/k*BSD),
please try to get it in trunk or drop me a patch.
Index: debian/changelog
===================================================================
--- debian/changelog	(.../trunk/packages/netcfg)	(revisione 59247)
+++ debian/changelog	(.../branches/d-i/kfreebsd/packages/netcfg)	(revisione 59252)
@@ -8,6 +8,7 @@
   * If wireless is disabled, don't build and link wireless related stuff.
   * Disable by default wireless on non-linux architectures.
   * Inverse the logic about building without wireless support (WIRELESS=0).
+  * Substitute Linux-specific code in get_all_ifs() using getifaddrs().
 
   [ Colin Watson ]
   * check_kill_switch is Linux-specific; provide a stub implementation for
Index: netcfg-common.c
===================================================================
--- netcfg-common.c	(.../trunk/packages/netcfg)	(revisione 59247)
+++ netcfg-common.c	(.../branches/d-i/kfreebsd/packages/netcfg)	(revisione 59252)
@@ -45,6 +45,8 @@
 #include <time.h>
 #include <netdb.h>
 
+#include <ifaddrs.h>
+
 /* Set if there is currently a progress bar displayed. */
 int netcfg_progress_displayed = 0;
 
@@ -227,53 +233,24 @@
     return ((ifr.ifr_flags & IFF_UP) ? 1 : 0);
 }
 
-void get_name(char *name, char *p)
-{
-    while (isspace(*p))
-        p++;
-    while (*p) {
-        if (isspace(*p))
-            break;
-        if (*p == ':') {	/* could be an alias */
-            char *dot = p, *dotname = name;
-            *name++ = *p++;
-            while (isdigit(*p))
-                *name++ = *p++;
-            if (*p != ':') {	/* it wasn't, backup */
-                p = dot;
-                name = dotname;
-            }
-            if (*p == '\0')
-                return;
-            p++;
-            break;
-        }
-        *name++ = *p++;
-    }
-    *name++ = '\0';
-    return;
-}
-
 int get_all_ifs (int all, char*** ptr)
 {
-    FILE *ifs = NULL;
-    char ibuf[512], rbuf[512];
+    struct ifaddrs *ifap, *ifa;
+    char ibuf[512];
     char** list = NULL;
     size_t len = 0;
     
-    if ((ifs = fopen("/proc/net/dev", "r")) != NULL) {
-        fgets(ibuf, sizeof(ibuf), ifs); /* eat header */
-        fgets(ibuf, sizeof(ibuf), ifs); /* ditto */
-    }
-    else
+    if (getifaddrs(&ifap) == -1)
         return 0;
 
-    while (fgets(rbuf, sizeof(rbuf), ifs) != NULL) {
-        get_name(ibuf, rbuf);
-        if (!strcmp(ibuf, "lo"))        /* ignore the loopback */
+    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+        strncpy(ibuf, ifa->ifa_name, sizeof(ibuf));
+        if (!strncmp(ibuf, "lo", 2))        /* ignore the loopback */
             continue;
+#if defined(__linux__)
         if (!strncmp(ibuf, "sit", 3))        /* ignore tunnel devices */
             continue;
+#endif
 #if defined(WIRELESS)
         if (is_raw_80211(ibuf))
             continue;
@@ -290,7 +267,7 @@
         list = realloc(list, sizeof(char*) * (len + 1));
         list[len] = NULL;
     }
-    fclose (ifs);
+    freeifaddrs(ifap);
     
     *ptr = list;
     

Reply to: