@@ -66,10 +66,10 @@ static int open_net(void)
66
66
if (socknr == -3 )
67
67
return -1 ;
68
68
if (socknr == -2 ) {
69
- socknr = socket (PF_INET , SOCK_STREAM , 0 );
69
+ socknr = socket (PF_INET , SOCK_DGRAM , 0 );
70
70
}
71
71
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 ));
73
73
error (" deactivate plugin netinfo" );
74
74
socknr = -3 ;
75
75
return -1 ;
@@ -81,33 +81,49 @@ static int open_net(void)
81
81
82
82
static void my_exists (RESULT * result , RESULT * arg1 )
83
83
{
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 ];
87
90
88
91
if (socknr < 0 ) {
89
92
/* no open socket */
90
- value = 0.0 ;
91
93
SetResult (& result , R_NUMBER , & value );
92
94
return ;
93
95
}
94
96
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 );
106
120
return ;
107
121
}
122
+
123
+ (* (char * * ) & ifreq ) += len ;
108
124
}
109
125
110
- /* device exists */
126
+ /* device doesn't exists */
111
127
SetResult (& result , R_NUMBER , & value );
112
128
}
113
129
0 commit comments