]> git.webhop.me Git - lcd4linux.git/commitdiff
ported r838 from volker_dev
authormichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Wed, 31 Dec 2008 06:28:56 +0000 (06:28 +0000)
committermichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Wed, 31 Dec 2008 06:28:56 +0000 (06:28 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@926 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

plugin_netinfo.c

index 371c9ebe549af6a4b5b90e29b2eb10e9c8143977..c2af6fd26b0c270aaaadd380e92cf8ecc603c63b 100644 (file)
@@ -66,10 +66,10 @@ static int open_net(void)
     if (socknr == -3)
        return -1;
     if (socknr == -2) {
-       socknr = socket(PF_INET, SOCK_STREAM, 0);
+       socknr = socket(PF_INET, SOCK_DGRAM, 0);
     }
     if (socknr == -1) {
-       error("%s: socket(PF_INET, SOCK_STREAM, 0) failed: %s", "plugin_netinfo", strerror(errno));
+       error("%s: socket(PF_INET, SOCK_DGRAM, 0) failed: %s", "plugin_netinfo", strerror(errno));
        error("  deactivate plugin netinfo");
        socknr = -3;
        return -1;
@@ -81,33 +81,49 @@ static int open_net(void)
 
 static void my_exists(RESULT * result, RESULT * arg1)
 {
-    static int errcount = 0;
-    struct ifreq ifreq;
-    double value = 1.0;                // netdev exists
+    char buf[10240];
+    struct ifconf ifcnf;
+    struct ifreq *ifreq;
+    int len;
+    double value = 0.0;                // netdev doesn't exists
+    char devname[80];
 
     if (socknr < 0) {
        /* no open socket */
-       value = 0.0;
        SetResult(&result, R_NUMBER, &value);
        return;
     }
 
-    strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name));
-    if (ioctl(socknr, SIOCGIFFLAGS, &ifreq) < 0) {
-       if (errno == ENODEV) {
-           /* device does not exists */
-           value = 0.0;
-       } else {
-           errcount++;
-           if (1 == errcount % 1000) {
-               error("%s: ioctl(FLAGS %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno));
-               error("  (skip next 1000 errors)");
-           }
+    ifcnf.ifc_len = sizeof(buf);
+    ifcnf.ifc_buf = buf;
+    if (ioctl(socknr, SIOCGIFCONF, &ifcnf) < 0) {
+       /* error getting list of devices */
+       error("%s: ioctl(IFCONF) for %s failed: %s", "plugin_netinfo", R2S(arg1), strerror(errno));
+       SetResult(&result, R_NUMBER, &value);
+       return;
+    }
+    if (0 == ifcnf.ifc_len) {
+       /* no interfaces found */
+       SetResult(&result, R_NUMBER, &value);
+       return;
+    }
+
+    ifreq = (struct ifreq *) buf;
+    len = sizeof(struct ifreq);
+    strncpy(devname, R2S(arg1), sizeof(devname));
+
+    while (ifreq && *((char *) ifreq) && ((char *) ifreq) < buf + ifcnf.ifc_len) {
+       if (*((char *) ifreq) && strncmp(ifreq->ifr_name, devname, sizeof(devname)) == 0) {
+           /* found */
+           value = 1.0;
+           SetResult(&result, R_NUMBER, &value);
            return;
        }
+
+       (*(char **) &ifreq) += len;
     }
 
-    /* device exists */
+    /* device doesn't exists */
     SetResult(&result, R_NUMBER, &value);
 }