make GetIfTable wrapper reusable and re-use allocated buffer

This commit is contained in:
Doug MacEachern 2005-12-20 00:09:32 +00:00
parent 258ed193ca
commit 39c1337f71
1 changed files with 35 additions and 20 deletions

View File

@ -1813,12 +1813,42 @@ SIGAR_DECLARE(int) sigar_net_route_list_get(sigar_t *sigar,
return SIGAR_OK; return SIGAR_OK;
} }
static int sigar_get_if_table(sigar_t *sigar)
{
ULONG size = sigar->ifconf_len;
DWORD rc;
if (!sigar->get_if_table) {
return SIGAR_ENOTIMPL;
}
rc = sigar->get_if_table((PMIB_IFTABLE)sigar->ifconf_buf,
&size, FALSE);
if (rc == ERROR_INSUFFICIENT_BUFFER) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"realloc ifconf_buf old=%d, new=%d",
sigar->ifconf_len, size);
sigar->ifconf_len = size;
sigar->ifconf_buf = realloc(sigar->ifconf_buf,
sigar->ifconf_len);
rc = sigar->get_if_table((PMIB_IFTABLE)sigar->ifconf_buf,
&size, FALSE);
}
if (rc != NO_ERROR) {
return GetLastError();
}
else {
return SIGAR_OK;
}
}
SIGAR_DECLARE(int) SIGAR_DECLARE(int)
sigar_net_interface_stat_get(sigar_t *sigar, const char *name, sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
sigar_net_interface_stat_t *ifstat) sigar_net_interface_stat_t *ifstat)
{ {
char *buffer = NULL;
ULONG buf_size = 0;
DWORD rc, i; DWORD rc, i;
MIB_IFTABLE *ift; MIB_IFTABLE *ift;
MIB_IFROW *ifr; MIB_IFROW *ifr;
@ -1829,23 +1859,11 @@ sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
return status; return status;
} }
if (!sigar->get_if_table) { if ((status = sigar_get_if_table(sigar)) != SIGAR_OK) {
return SIGAR_ENOTIMPL; return status;
} }
rc = (*(sigar->get_if_table))((PMIB_IFTABLE)buffer, &buf_size, FALSE); ift = (MIB_IFTABLE *)sigar->ifconf_buf;
if (rc != ERROR_INSUFFICIENT_BUFFER) {
return GetLastError();
}
buffer = malloc(buf_size);
rc = (*(sigar->get_if_table))((PMIB_IFTABLE)buffer, &buf_size, FALSE);
if (rc != NO_ERROR) {
free(buffer);
return GetLastError();
}
ift = (MIB_IFTABLE *)buffer;
for (i=0; i<ift->dwNumEntries; i++) { for (i=0; i<ift->dwNumEntries; i++) {
ifr = ift->table + i; ifr = ift->table + i;
@ -1871,7 +1889,6 @@ sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
} }
if (!ifr) { if (!ifr) {
free(buffer);
return ENOENT; return ENOENT;
} }
@ -1890,8 +1907,6 @@ sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
ifstat->tx_collisions = SIGAR_FIELD_NOTIMPL; ifstat->tx_collisions = SIGAR_FIELD_NOTIMPL;
ifstat->tx_carrier = SIGAR_FIELD_NOTIMPL; ifstat->tx_carrier = SIGAR_FIELD_NOTIMPL;
free(buffer);
return SIGAR_OK; return SIGAR_OK;
} }