Author: titmuss
Date: Thu Nov 27 11:47:28 2008
New Revision: 3456

URL: http://svn.slimdevices.com?rev=3456&root=Jive&view=rev
Log:
Bug: N/A
Description:
Improve mac address detection for linux, this now works on jive.


Modified:
    7.4/trunk/squeezeplay/src/squeezeplay/src/ui/platform_linux.c

Modified: 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/platform_linux.c
URL: 
http://svn.slimdevices.com/7.4/trunk/squeezeplay/src/squeezeplay/src/ui/platform_linux.c?rev=3456&root=Jive&r1=3455&r2=3456&view=diff
==============================================================================
--- 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/platform_linux.c (original)
+++ 7.4/trunk/squeezeplay/src/squeezeplay/src/ui/platform_linux.c Thu Nov 27 
11:47:28 2008
@@ -27,43 +27,90 @@
     return dir;
 }
 
+
+
+static char *iface_mac_address(int sock, char *name) {
+    struct ifreq ifr;
+    unsigned char *ptr;
+    char *macaddr = NULL;
+
+    strcpy(ifr.ifr_name, name);
+    if (ioctl(sock, SIOCGIFFLAGS, &ifr) != 0) {
+       return NULL;
+    }
+
+    if ((ifr.ifr_flags & IFF_LOOPBACK) == IFF_LOOPBACK) {
+       return NULL;
+    }
+
+    if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0) {
+       return NULL;
+    }
+
+    ptr = (unsigned char *) ifr.ifr_hwaddr.sa_data;
+
+    macaddr = malloc(18);
+    sprintf(macaddr, "%02x:%02x:%02x:%02x:%02x:%02x", *ptr,*(ptr+1), 
*(ptr+2),*(ptr+3), *(ptr+4), *(ptr+5));
+       
+    return macaddr;
+}
+
+
+static char *iface[] = { "eth0", "eth1", "wlan0", "wlan1" };
+
 char *platform_get_mac_address() {
-    struct ifreq ifr;
-    struct ifreq *IFR;
-    struct ifconf ifc;
-    char buf[1024];
-    char *macaddr = NULL;
-    int s, i;
+    FILE *fh;
+    char buf[512], *macaddr = NULL;
+    size_t i;
+    int sock;
 
-    s = socket(AF_INET, SOCK_DGRAM, 0);
-    if (s==-1) {
+
+    sock = socket(AF_INET, SOCK_DGRAM, 0);
+    if (sock < 0) {
         return NULL;
     }
 
-    ifc.ifc_len = sizeof(buf);
-    ifc.ifc_buf = buf;
-    ioctl(s, SIOCGIFCONF, &ifc);
-
-    IFR = ifc.ifc_req;
-    for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
-
-        strcpy(ifr.ifr_name, IFR->ifr_name);
-        if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
-            if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
-                if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
-                    //take the first found address.
-                    //todo: can we be smarter about which is the correct 
address
-                    unsigned char * ptr = (unsigned char *) 
ifr.ifr_hwaddr.sa_data;
-
-                   macaddr = malloc(18);
-                    sprintf(macaddr, "%02x:%02x:%02x:%02x:%02x:%02x", 
*ptr,*(ptr+1), *(ptr+2),*(ptr+3), *(ptr+4), *(ptr+5));
-                    break;
-                }
-            }
-        }
+    /* test the common interfaces first */
+    for (i=0; i<sizeof(iface)/sizeof(char *); i++) {
+       macaddr = iface_mac_address(sock, iface[i]);
+       if (macaddr) {
+           close(sock);
+           return macaddr;
+       }
     }
 
-    close(s);
+    /* SIOCGIFCONF does not always return interfaces without an ipv4 address
+     * so we need to parse /proc/net/dev.
+     */
+
+    fh = fopen("/proc/net/dev", "r");
+    if (!fh) {
+       return NULL;
+    }
+
+    fgets(buf, sizeof(buf), fh); /* eat line */
+    fgets(buf, sizeof(buf), fh);
+
+    while (fgets(buf, sizeof(buf), fh)) {
+       char *name, *s = buf;
+
+       while (*s && isspace(*s)) {
+           s++;
+       }
+       name = s;
+       while (*s && *s != ':') {
+           s++;
+       }
+       *s = '\0';
+
+       macaddr = iface_mac_address(sock, iface[i]);
+       if (macaddr) {
+           break;
+       }
+    }
+
+    close(sock);
+    fclose(fh);
 
     return macaddr;
 }

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to