Changeset 838

Show
Ignore:
Timestamp:
08/23/07 22:55:13 (15 months ago)
Author:
volker
Message:

existence of netdevice corrected (less cpu load)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/volker_dev/plugin_netinfo.c

    r835 r838  
    4949#include <sys/ioctl.h>    /* SIOCGIFNAME */ 
    5050#include <net/if.h>   /* ifreq{} */ 
    51 #include <errno.h>              /* errno */ 
    52 #include <netinet/in.h>         /* inet_ntoa() */ 
    53 #include <arpa/inet.h>          /* inet_ntoa() */ 
     51#include <errno.h>    /* errno */ 
     52#include <netinet/in.h>   /* inet_ntoa() */ 
     53#include <arpa/inet.h>    /* inet_ntoa() */ 
    5454 
    5555 
     
    6565 
    6666    if (socknr == -3) 
    67         return -1; 
     67  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)); 
    73         error("  deactivate plugin netinfo"); 
    74         socknr = -3; 
     72  error("%s: socket(PF_INET, SOCK_DGRAM, 0) failed: %s", "plugin_netinfo", strerror(errno)); 
     73  error("  deactivate plugin netinfo"); 
     74  socknr = -3; 
    7575  return -1; 
    7676    } 
    77      
     77 
    7878    return 0; 
    7979} 
     
    8282static void my_exists(RESULT * result, RESULT * arg1) 
    8383{ 
    84     static int errcount = 0; 
    85     struct ifreq ifreq; 
    86     double value = 1.0;     // netdev exists 
    87  
    88     if (socknr < 0) { 
    89         /* no open socket */ 
    90         value = 0.0; 
    91         SetResult(&result, R_NUMBER, &value); 
    92         return; 
    93     } 
    94      
    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             } 
    106             return; 
    107         } 
    108     } 
    109  
    110     /* device 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]; 
     90 
     91    if (socknr < 0) { 
     92  /* no open socket */ 
     93  SetResult(&result, R_NUMBER, &value); 
     94  return; 
     95    } 
     96 
     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); 
     120      return; 
     121  } 
     122 
     123  (*(char **) &ifreq) += len; 
     124    } 
     125 
     126    /* device doesn't exists */ 
    111127    SetResult(&result, R_NUMBER, &value); 
    112128} 
     
    120136    unsigned char *hw; 
    121137    char value[18]; 
    122      
    123     if (socknr < 0) { 
    124         /* no open socket */ 
    125         SetResult(&result, R_STRING, ""); 
    126         return; 
    127     } 
    128      
     138 
     139    if (socknr < 0) { 
     140  /* no open socket */ 
     141  SetResult(&result, R_STRING, ""); 
     142  return; 
     143    } 
     144 
    129145    strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name)); 
    130146    if (ioctl(socknr, SIOCGIFHWADDR, &ifreq) < 0) { 
    131         errcount++; 
    132         if (1 == errcount % 1000) { 
    133             error("%s: ioctl(IFHWADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
    134             error("  (skip next 1000 errors)"); 
    135         } 
    136         SetResult(&result, R_STRING, ""); 
    137         return; 
    138     } 
    139     hw = (unsigned char*)ifreq.ifr_hwaddr.sa_data; 
     147  errcount++; 
     148  if (1 == errcount % 1000) { 
     149      error("%s: ioctl(IFHWADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
     150      error("  (skip next 1000 errors)"); 
     151  } 
     152  SetResult(&result, R_STRING, ""); 
     153  return; 
     154    } 
     155    hw = (unsigned char *) ifreq.ifr_hwaddr.sa_data; 
    140156    qprintf(value, sizeof(value), "%02x:%02x:%02x:%02x:%02x:%02x", 
    141             *hw, *(hw + 1), *(hw + 2), *(hw + 3), *(hw + 4), *(hw + 5)); 
    142      
     157      *hw, *(hw + 1), *(hw + 2), *(hw + 3), *(hw + 4), *(hw + 5)); 
     158 
    143159    SetResult(&result, R_STRING, value); 
    144160} 
     
    152168    struct sockaddr_in *sin; 
    153169    char value[16]; 
    154      
    155     if (socknr < 0) { 
    156         /* no open socket */ 
    157         SetResult(&result, R_STRING, ""); 
    158         return; 
    159     } 
    160      
     170 
     171    if (socknr < 0) { 
     172  /* no open socket */ 
     173  SetResult(&result, R_STRING, ""); 
     174  return; 
     175    } 
     176 
    161177    strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name)); 
    162178    if (ioctl(socknr, SIOCGIFADDR, &ifreq) < 0) { 
    163         errcount++; 
    164         if (1 == errcount % 1000) { 
    165             error("%s: ioctl(IFADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
    166             error("  (skip next 1000 errors)"); 
    167         } 
    168         SetResult(&result, R_STRING, ""); 
    169         return; 
    170     } 
    171     sin = (struct sockaddr_in *)&ifreq.ifr_addr;  
     179  errcount++; 
     180  if (1 == errcount % 1000) { 
     181      error("%s: ioctl(IFADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
     182      error("  (skip next 1000 errors)"); 
     183  } 
     184  SetResult(&result, R_STRING, ""); 
     185  return; 
     186    } 
     187    sin = (struct sockaddr_in *) &ifreq.ifr_addr; 
    172188    qprintf(value, sizeof(value), "%s", inet_ntoa(sin->sin_addr)); 
    173      
     189 
    174190    SetResult(&result, R_STRING, value); 
    175191} 
     
    183199    struct sockaddr_in *sin; 
    184200    char value[16]; 
    185      
    186     if (socknr < 0) { 
    187         /* no open socket */ 
    188         SetResult(&result, R_STRING, ""); 
    189         return; 
    190     } 
    191      
     201 
     202    if (socknr < 0) { 
     203  /* no open socket */ 
     204  SetResult(&result, R_STRING, ""); 
     205  return; 
     206    } 
     207 
    192208    strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name)); 
    193209    if (ioctl(socknr, SIOCGIFNETMASK, &ifreq) < 0) { 
    194         errcount++; 
    195         if (1 == errcount % 1000) { 
    196             error("%s: ioctl(IFNETMASK %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
    197             error("  (skip next 1000 errors)"); 
    198         } 
    199         SetResult(&result, R_STRING, ""); 
    200         return; 
    201     } 
    202     sin = (struct sockaddr_in *)&ifreq.ifr_netmask; 
     210  errcount++; 
     211  if (1 == errcount % 1000) { 
     212      error("%s: ioctl(IFNETMASK %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
     213      error("  (skip next 1000 errors)"); 
     214  } 
     215  SetResult(&result, R_STRING, ""); 
     216  return; 
     217    } 
     218    sin = (struct sockaddr_in *) &ifreq.ifr_netmask; 
    203219    qprintf(value, sizeof(value), "%s", inet_ntoa(sin->sin_addr)); 
    204      
     220 
    205221    SetResult(&result, R_STRING, value); 
    206222} 
     
    214230    struct sockaddr_in *sin; 
    215231    char value[16]; 
    216      
    217     if (socknr < 0) { 
    218         /* no open socket */ 
    219         SetResult(&result, R_STRING, ""); 
    220         return; 
    221     } 
    222      
     232 
     233    if (socknr < 0) { 
     234  /* no open socket */ 
     235  SetResult(&result, R_STRING, ""); 
     236  return; 
     237    } 
     238 
    223239    strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name)); 
    224240    if (ioctl(socknr, SIOCGIFBRDADDR, &ifreq) < 0) { 
    225         errcount++; 
    226         if (1 == errcount % 1000) { 
    227             error("%s: ioctl(IFBRDADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
    228             error("  (skip next 1000 errors)"); 
    229         } 
    230         SetResult(&result, R_STRING, ""); 
    231         return; 
    232     } 
    233     sin = (struct sockaddr_in *)&ifreq.ifr_broadaddr; 
     241  errcount++; 
     242  if (1 == errcount % 1000) { 
     243      error("%s: ioctl(IFBRDADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno)); 
     244      error("  (skip next 1000 errors)"); 
     245  } 
     246  SetResult(&result, R_STRING, ""); 
     247  return; 
     248    } 
     249    sin = (struct sockaddr_in *) &ifreq.ifr_broadaddr; 
    234250    qprintf(value, sizeof(value), "%s", inet_ntoa(sin->sin_addr)); 
    235      
     251 
    236252    SetResult(&result, R_STRING, value); 
    237253} 
     
    241257{ 
    242258    open_net(); 
    243      
     259 
    244260    AddFunction("netinfo::exists", 1, my_exists); 
    245261    AddFunction("netinfo::hwaddr", 1, my_hwaddr);