From: <mik...@us...> - 2008-08-14 22:44:46
|
Revision: 853 http://omc.svn.sourceforge.net/omc/?rev=853&view=rev Author: mike-brasher Date: 2008-08-14 22:44:56 +0000 (Thu, 14 Aug 2008) Log Message: ----------- Switched from "ioctl()" to "ifconfig" to find interfaces. Modified Paths: -------------- cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.c cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.h Modified: cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.c =================================================================== --- cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.c 2008-08-14 17:52:42 UTC (rev 852) +++ cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.c 2008-08-14 22:44:56 UTC (rev 853) @@ -13,8 +13,6 @@ #include "common.h" #include <ifaddrs.h> -#define MAX_IFREQ 64 - struct EthtoolInfo { /* Non-zero if full duplex */ @@ -35,7 +33,7 @@ const char ETHTOOL[] = "/usr/sbin/ethtool"; char buf[64]; char line[128]; - FILE* os; + FILE* is; int matches = 0; /* Clear info structure */ @@ -54,12 +52,12 @@ strlcat(buf, name, sizeof(buf)); strlcat(buf, " 2> /dev/null", sizeof(buf)); - if ((os = popen(buf, "r")) == NULL) + if ((is = popen(buf, "r")) == NULL) return -1; /* Extract selected fields */ - while (fgets(line, sizeof(line), os) != NULL) + while (fgets(line, sizeof(line), is) != NULL) { char* p = line; char* q = p + strlen(line); @@ -102,7 +100,7 @@ } /* Close stream */ - pclose(os); + pclose(is); return matches ? 0 : -1; } @@ -167,24 +165,6 @@ &((struct sockaddr_in6*)p->ifa_addr)->sin6_addr, info->inet6_addr, sizeof(info->inet6_addr) - 1); - - if(p->ifa_netmask) - { - inet_ntop( - AF_INET6, - &((struct sockaddr_in6*)p->ifa_netmask)->sin6_addr, - info->inet6_netmask, - sizeof(info->inet6_netmask) - 1); - } - - if (p->ifa_broadaddr && p->ifa_flags & IFF_BROADCAST) - { - inet_ntop( - AF_INET6, - &((struct sockaddr_in6*)p->ifa_broadaddr)->sin6_addr, - info->inet6_broadcast, - sizeof(info->inet6_broadcast) - 1); - } } else if(family == AF_INET) { @@ -199,8 +179,8 @@ inet_ntop( AF_INET, &((struct sockaddr_in*)p->ifa_netmask)->sin_addr, - info->inet_netmask, - sizeof(info->inet_netmask) - 1); + info->inet_mask, + sizeof(info->inet_mask) - 1); } if (p->ifa_broadaddr && p->ifa_flags & IFF_BROADCAST) @@ -208,8 +188,8 @@ inet_ntop( AF_INET, &((struct sockaddr_in*)p->ifa_broadaddr)->sin_addr, - info->inet_broadcast, - sizeof(info->inet_broadcast) - 1); + info->inet_bcast, + sizeof(info->inet_bcast) - 1); } } } @@ -219,8 +199,172 @@ return 0; } +static int _get(const char* in, const char* name, char* data, size_t size) +{ + char* p; + const char* start; + + if (!(p = strstr(in, name))) + return -1; + + p += strlen(name); + + while (*p && isspace(*p)) + p++; + + for (start = p; *p && !isspace(*p); p++) + ; + + if (p - start >= size) + return -1; + + strncat(data, start, p - start); + return 0; +} + struct EthernetPortResource* GetEthernetPortResources() { + struct EthernetPortResource* head = NULL; + struct EthernetPortResource* tail = NULL; + char data[1024] = { '\0' }; + FILE* is; + char line[1024]; + struct EthernetPortResource info; + struct EthernetPortResource* node; + + /* Run "inconfig -a" to get interface information */ + + if ((is = popen("/sbin/ifconfig -a", "r")) == NULL) + return NULL; + + /* For each line */ + + while (fgets(line, sizeof(line), is) != NULL) + { + char* p = line; + struct EthtoolInfo eti; + + /* Skip leading whitespace */ + + while (isspace(*p)) + p++; + + /* Interface information is separated by blank lines */ + + if (*p == '\0') + { + p = data; + + /* Initialize new info structure */ + memset(&info, 0, sizeof(info)); + + /* Get EthernetPortResource.name */ + + while (*p && !isspace(*p)) + p++; + + *info.name = '\0'; + strncat(info.name, data, p - data); + + /* Skip non-ethernet interfaces */ + + if (!(p = strstr(data, "encap:Ethernet"))) + { + data[0] = '\0'; + continue; + } + + /* Get EthernetPortResource.index */ + + info.index = if_nametoindex(info.name); + + /* Get EthernetPortResource.mac "HWaddr 00:50:56:C0:00:01" */ + + if ((p = strstr(data, "HWaddr "))) + { + p+= 7; + sprintf(info.mac, "%c%c%c%c%c%c%c%c%c%c%c%c", p[0], p[1], p[3], + p[4], p[6], p[7], p[9], p[10], p[12], p[13], p[15], p[16]); + } + + /* Get EthernetPortResource.inet_addr */ + _get(data, "inet addr:", info.inet_addr, sizeof(info.inet_addr)); + + /* Get EthernetPortResource.inet_mask */ + _get(data, "Mask:", info.inet_mask, sizeof(info.inet_mask)); + + /* Get EthernetPortResource.inet_bcast */ + _get(data, "Bcast:", info.inet_bcast, sizeof(info.inet_bcast)); + + /* Get EthernetPortResource.inet6_addr */ + _get(data, "inet6 addr:", info.inet6_addr,sizeof(info.inet6_addr)); + + /* Get EthernetPortResource.is_up */ + info.is_up = strstr(data, " UP ") ? 1 : 0; + + /* Get EthernetPortResource.is_bcast */ + info.is_bcast = strstr(data, " BROADCAST ") ? 1 : 0; + + /* Get EthernetPortResource.is_multicast */ + info.is_multicast = strstr(data, " MULTICAST ") ? 1 : 0; + + /* Get additional information using the ethtool program */ + + if (_ethtool(&eti, info.name) == 0) + { + info.speed_mbps = eti.speed_mbps; + info.full_duplex = eti.full_duplex; + info.supports_wake_on = eti.supports_wake_on; + info.wake_on = eti.wake_on; + } + + /* Append to list */ + + node = (struct EthernetPortResource*)malloc( + sizeof(struct EthernetPortResource)); + + if (!node) + { + pclose(is); + return NULL; + } + + memcpy(node, &info, sizeof(info)); + + if (tail) + { + tail->next = node; + tail = node; + } + else + { + tail = node; + head = node; + } + + /* Zero out data and info for next one */ + + data[0] = '\0'; + memset(&info, 0, sizeof(info)); + continue; + } + + /* Save data */ + strlcat(data, line, sizeof(data)); + } + + /* Close stream */ + pclose(is); + + /* Set address info for all elements in this list */ + _fixup_addrs(head); + + return head; +} + +#if 0 +struct EthernetPortResource* GetEthernetPortResources() +{ int sock; int len; int prev_len; @@ -350,7 +494,7 @@ /* Set the flags */ info.is_up = flags & IFF_UP ? 1 : 0; - info.is_broadcast = flags & IFF_BROADCAST ? 1 : 0; + info.is_bcast = flags & IFF_BROADCAST ? 1 : 0; info.is_multicast = flags & IFF_MULTICAST ? 1 : 0; info.is_loopback = flags & IFF_LOOPBACK ? 1 : 0; info.is_loopback = flags & IFF_LOOPBACK ? 1 : 0; @@ -418,7 +562,7 @@ in = (struct sockaddr_in*)&tmp.ifr_addr; inet_ntop(tmp.ifr_addr.sa_family, &in->sin_addr, str, sizeof(str)); - strlcpy(info.inet_broadcast, str, sizeof(info.inet_broadcast)); + strlcpy(info.inet_bcast, str, sizeof(info.inet_bcast)); } /* Get EthernetPortResource.netmask */ @@ -438,7 +582,7 @@ in = (struct sockaddr_in*)&tmp.ifr_addr; inet_ntop(tmp.ifr_addr.sa_family, &in->sin_addr, str, sizeof(str)); - strlcpy(info.inet_netmask, str, sizeof(info.inet_netmask)); + strlcpy(info.inet_mask, str, sizeof(info.inet_mask)); } #endif @@ -484,6 +628,7 @@ return head; } +#endif void FreeEthernetPortResources(struct EthernetPortResource* head) { @@ -509,16 +654,12 @@ printf(" index{%d}\n", p->index); printf(" mac{%s}\n", p->mac); printf(" inet_addr{%s}\n", p->inet_addr); - printf(" inet_netmask{%s}\n", p->inet_netmask); - printf(" inet_broadcast{%s}\n", p->inet_broadcast); + printf(" inet_mask{%s}\n", p->inet_mask); + printf(" inet_bcast{%s}\n", p->inet_bcast); printf(" inet6_addr{%s}\n", p->inet6_addr); - printf(" inet6_netmask{%s}\n", p->inet6_netmask); - printf(" inet6_broadcast{%s}\n", p->inet6_broadcast); printf(" is_up{%d}\n", p->is_up); - printf(" is_broadcast{%d}\n", p->is_broadcast); + printf(" is_bcast{%d}\n", p->is_bcast); printf(" is_multicast{%d}\n", p->is_multicast); - printf(" is_loopback{%d}\n", p->is_loopback); - printf(" is_p2p{%d}\n", p->is_p2p); printf(" full_duplex{%d}\n", p->full_duplex); printf(" speed_mbps{%d}\n", p->speed_mbps); printf(" supports_wake_on{%d}\n", p->supports_wake_on); Modified: cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.h =================================================================== --- cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.h 2008-08-14 17:52:42 UTC (rev 852) +++ cmpiprofiles/sblim-cmpi-ethernet_port_profile/trunk/src/Resource.h 2008-08-14 22:44:56 UTC (rev 853) @@ -26,35 +26,23 @@ int is_up; /* Non-zero if broadcast */ - int is_broadcast; + int is_bcast; /* Non-zero if multicast */ int is_multicast ; - /* Non-zero if loopback */ - int is_loopback; - - /* Non-zero if p2p */ - int is_p2p; - /* ASCII IPV4 address */ char inet_addr[128]; /* ASCII IPV4 broadcast address */ - char inet_broadcast[128]; + char inet_bcast[128]; /* ASCII IPV4 Netmask */ - char inet_netmask[128]; + char inet_mask[128]; /* ASCII IPV6 address */ char inet6_addr[128]; - /* ASCII IPV6 broadcast address */ - char inet6_broadcast[128]; - - /* ASCII IPV6 Netmask */ - char inet6_netmask[128]; - /* Non-zero if full duplex */ int full_duplex; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |