Skip to content

Commit 9798fe8

Browse files
author
michael
committed
ported r838 from volker_dev
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@926 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
1 parent 509ea3a commit 9798fe8

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

plugin_netinfo.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ static int open_net(void)
6666
if (socknr == -3)
6767
return -1;
6868
if (socknr == -2) {
69-
socknr = socket(PF_INET, SOCK_STREAM, 0);
69+
socknr = socket(PF_INET, SOCK_DGRAM, 0);
7070
}
7171
if (socknr == -1) {
72-
error("%s: socket(PF_INET, SOCK_STREAM, 0) failed: %s", "plugin_netinfo", strerror(errno));
72+
error("%s: socket(PF_INET, SOCK_DGRAM, 0) failed: %s", "plugin_netinfo", strerror(errno));
7373
error(" deactivate plugin netinfo");
7474
socknr = -3;
7575
return -1;
@@ -81,33 +81,49 @@ static int open_net(void)
8181

8282
static void my_exists(RESULT * result, RESULT * arg1)
8383
{
84-
static int errcount = 0;
85-
struct ifreq ifreq;
86-
double value = 1.0; // netdev exists
84+
char buf[10240];
85+
struct ifconf ifcnf;
86+
struct ifreq *ifreq;
87+
int len;
88+
double value = 0.0; // netdev doesn't exists
89+
char devname[80];
8790

8891
if (socknr < 0) {
8992
/* no open socket */
90-
value = 0.0;
9193
SetResult(&result, R_NUMBER, &value);
9294
return;
9395
}
9496

95-
strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name));
96-
if (ioctl(socknr, SIOCGIFFLAGS, &ifreq) < 0) {
97-
if (errno == ENODEV) {
98-
/* device does not exists */
99-
value = 0.0;
100-
} else {
101-
errcount++;
102-
if (1 == errcount % 1000) {
103-
error("%s: ioctl(FLAGS %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno));
104-
error(" (skip next 1000 errors)");
105-
}
97+
ifcnf.ifc_len = sizeof(buf);
98+
ifcnf.ifc_buf = buf;
99+
if (ioctl(socknr, SIOCGIFCONF, &ifcnf) < 0) {
100+
/* error getting list of devices */
101+
error("%s: ioctl(IFCONF) for %s failed: %s", "plugin_netinfo", R2S(arg1), strerror(errno));
102+
SetResult(&result, R_NUMBER, &value);
103+
return;
104+
}
105+
if (0 == ifcnf.ifc_len) {
106+
/* no interfaces found */
107+
SetResult(&result, R_NUMBER, &value);
108+
return;
109+
}
110+
111+
ifreq = (struct ifreq *) buf;
112+
len = sizeof(struct ifreq);
113+
strncpy(devname, R2S(arg1), sizeof(devname));
114+
115+
while (ifreq && *((char *) ifreq) && ((char *) ifreq) < buf + ifcnf.ifc_len) {
116+
if (*((char *) ifreq) && strncmp(ifreq->ifr_name, devname, sizeof(devname)) == 0) {
117+
/* found */
118+
value = 1.0;
119+
SetResult(&result, R_NUMBER, &value);
106120
return;
107121
}
122+
123+
(*(char **) &ifreq) += len;
108124
}
109125

110-
/* device exists */
126+
/* device doesn't exists */
111127
SetResult(&result, R_NUMBER, &value);
112128
}
113129

0 commit comments

Comments
 (0)