don't use sigar->ifconf_buf in sigar_get_adapters_addresses

This commit is contained in:
Doug MacEachern 2011-04-12 15:03:06 -07:00
parent 240649b925
commit cef810dafb
1 changed files with 33 additions and 32 deletions

View File

@ -2301,9 +2301,9 @@ static int sigar_get_adapter_info(sigar_t *sigar,
static int sigar_get_adapters_addresses(sigar_t *sigar, static int sigar_get_adapters_addresses(sigar_t *sigar,
ULONG family, ULONG flags, ULONG family, ULONG flags,
PIP_ADAPTER_ADDRESSES *addrs) PIP_ADAPTER_ADDRESSES *addrs,
ULONG *size)
{ {
ULONG size = sigar->ifconf_len;
ULONG rc; ULONG rc;
DLLMOD_INIT(iphlpapi, FALSE); DLLMOD_INIT(iphlpapi, FALSE);
@ -2312,28 +2312,23 @@ static int sigar_get_adapters_addresses(sigar_t *sigar,
return SIGAR_ENOTIMPL; return SIGAR_ENOTIMPL;
} }
*addrs = (PIP_ADAPTER_ADDRESSES)sigar->ifconf_buf;
rc = sigar_GetAdaptersAddresses(family, rc = sigar_GetAdaptersAddresses(family,
flags, flags,
NULL, NULL,
*addrs, *addrs,
&size); size);
if (rc == ERROR_BUFFER_OVERFLOW) { if (rc == ERROR_BUFFER_OVERFLOW) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG, sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"GetAdaptersAddresses " "GetAdaptersAddresses realloc to %d", size);
"realloc ifconf_buf old=%d, new=%d",
sigar->ifconf_len, size); *addrs = realloc(*addrs, *size);
sigar->ifconf_len = size;
sigar->ifconf_buf = realloc(sigar->ifconf_buf,
sigar->ifconf_len);
*addrs = (PIP_ADAPTER_ADDRESSES)sigar->ifconf_buf;
rc = sigar_GetAdaptersAddresses(family, rc = sigar_GetAdaptersAddresses(family,
flags, flags,
NULL, NULL,
*addrs, (PIP_ADAPTER_ADDRESSES)*addrs,
&size); size);
} }
if (rc != ERROR_SUCCESS) { if (rc != ERROR_SUCCESS) {
@ -2673,17 +2668,15 @@ static int netif_hash(char *s)
#define IF_TYPE_IEEE80211 71 #define IF_TYPE_IEEE80211 71
#endif #endif
SIGAR_DECLARE(char *) static char *
sigar_net_interface_name_get(sigar_t *sigar, MIB_IFROW *ifr) sigar_net_interface_name_get(sigar_t *sigar, MIB_IFROW *ifr, PIP_ADAPTER_ADDRESSES address_list)
{ {
char *match = NULL; char *match = NULL;
PIP_ADAPTER_ADDRESSES address_list, iter; PIP_ADAPTER_ADDRESSES iter;
int lpc = 0; int lpc = 0;
sigar_t *peek = sigar_new(); if (address_list == NULL) {
int status = sigar_get_adapters_addresses(peek, AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, &address_list); return NULL;
if (status != SIGAR_OK) {
goto done;
} }
for (iter = address_list; iter != NULL; iter = iter->Next) { for (iter = address_list; iter != NULL; iter = iter->Next) {
@ -2697,15 +2690,10 @@ sigar_net_interface_name_get(sigar_t *sigar, MIB_IFROW *ifr)
match = malloc(MAX_INTERFACE_NAME_LEN); match = malloc(MAX_INTERFACE_NAME_LEN);
wcstombs(match, iter->FriendlyName, MAX_INTERFACE_NAME_LEN); wcstombs(match, iter->FriendlyName, MAX_INTERFACE_NAME_LEN);
match[MAX_INTERFACE_NAME_LEN-1] = 0; match[MAX_INTERFACE_NAME_LEN-1] = 0;
goto done; break;
} }
} }
done:
sigar_close(peek);
if(match == NULL) {
fprintf(stderr, "No match found\n");
}
return match; return match;
} }
@ -2716,6 +2704,14 @@ sigar_net_interface_list_get(sigar_t *sigar,
MIB_IFTABLE *ift; MIB_IFTABLE *ift;
int i, status; int i, status;
int lo=0, eth=0, la=0; int lo=0, eth=0, la=0;
PIP_ADAPTER_ADDRESSES address_list = NULL;
ULONG size = 0;
status = sigar_get_adapters_addresses(sigar, AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, &address_list, &size);
if (status != SIGAR_OK) {
address_list = NULL;
}
if (!sigar->netif_mib_rows) { if (!sigar->netif_mib_rows) {
sigar->netif_mib_rows = sigar->netif_mib_rows =
@ -2728,6 +2724,9 @@ sigar_net_interface_list_get(sigar_t *sigar,
} }
if ((status = sigar_get_if_table(sigar, &ift)) != SIGAR_OK) { if ((status = sigar_get_if_table(sigar, &ift)) != SIGAR_OK) {
if (address_list) {
free(address_list);
}
return status; return status;
} }
@ -2750,7 +2749,7 @@ sigar_net_interface_list_get(sigar_t *sigar,
sprintf(name, NETIF_LA "%d", la++); sprintf(name, NETIF_LA "%d", la++);
} }
else if (ifr->dwType == MIB_IF_TYPE_LOOPBACK) { else if (ifr->dwType == MIB_IF_TYPE_LOOPBACK) {
friendly = sigar_net_interface_name_get(sigar, ifr); friendly = sigar_net_interface_name_get(sigar, ifr, address_list);
if(friendly == NULL) { if(friendly == NULL) {
sprintf(name, "lo%d", lo++); sprintf(name, "lo%d", lo++);
} else { } else {
@ -2762,10 +2761,9 @@ sigar_net_interface_list_get(sigar_t *sigar,
else if ((ifr->dwType == MIB_IF_TYPE_ETHERNET) || else if ((ifr->dwType == MIB_IF_TYPE_ETHERNET) ||
(ifr->dwType == IF_TYPE_IEEE80211)) (ifr->dwType == IF_TYPE_IEEE80211))
{ {
if(strstr(ifr->bDescr, "Scheduler") == NULL if(strstr(ifr->bDescr, "Scheduler") == NULL
&& strstr(ifr->bDescr, "Filter") == NULL) { && strstr(ifr->bDescr, "Filter") == NULL) {
friendly = sigar_net_interface_name_get(sigar, ifr); friendly = sigar_net_interface_name_get(sigar, ifr, address_list);
} }
if(friendly == NULL) { if(friendly == NULL) {
@ -2798,6 +2796,10 @@ sigar_net_interface_list_get(sigar_t *sigar,
} }
} }
if (address_list != NULL) {
free(address_list);
}
return SIGAR_OK; return SIGAR_OK;
} }
@ -2808,10 +2810,9 @@ static int sigar_net_interface_ipv6_config_find(sigar_t *sigar, int index,
return SIGAR_ENOTIMPL; return SIGAR_ENOTIMPL;
#else #else
int status; int status;
PIP_ADAPTER_ADDRESSES aa, addrs; PIP_ADAPTER_ADDRESSES aa = (PIP_ADAPTER_ADDRESSES)sigar->ifconf_buf, addrs;
ULONG flags = GAA_FLAG_INCLUDE_PREFIX; ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
status = sigar_get_adapters_addresses(sigar, AF_UNSPEC, flags, &aa, &sigar->ifconf_len);
status = sigar_get_adapters_addresses(sigar, AF_UNSPEC, flags, &aa);
if (status != SIGAR_OK) { if (status != SIGAR_OK) {
return status; return status;