iphlpapi: Reimplement GetIpNetTable to avoid parsing the same information three times.

This commit is contained in:
Alexandre Julliard 2009-03-02 12:45:58 +01:00
parent 53d522bc54
commit add9e025c2
2 changed files with 121 additions and 146 deletions

View File

@ -1318,31 +1318,17 @@ DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSi
*/ */
DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder) DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOrder)
{ {
DWORD ret; DWORD ret;
PMIB_IPNETTABLE table;
TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, TRACE("pIpNetTable %p, pdwSize %p, bOrder %d\n", pIpNetTable, pdwSize, bOrder);
(DWORD)bOrder);
if (!pdwSize)
ret = ERROR_INVALID_PARAMETER;
else {
DWORD numEntries = getNumArpEntries();
ULONG size = sizeof(MIB_IPNETTABLE);
if (numEntries > 1) if (!pdwSize) return ERROR_INVALID_PARAMETER;
size += (numEntries - 1) * sizeof(MIB_IPNETROW);
if (!pIpNetTable || *pdwSize < size) {
*pdwSize = size;
ret = ERROR_INSUFFICIENT_BUFFER;
}
else {
PMIB_IPNETTABLE table;
ret = getArpTable(&table, GetProcessHeap(), 0); ret = getArpTable(&table, GetProcessHeap(), 0);
if (!ret) { if (!ret) {
size = sizeof(MIB_IPNETTABLE); DWORD size = FIELD_OFFSET( MIB_IPNETTABLE, table[table->dwNumEntries] );
if (table->dwNumEntries > 1) if (!pIpNetTable || *pdwSize < size) {
size += (table->dwNumEntries - 1) * sizeof(MIB_IPNETROW);
if (*pdwSize < size) {
*pdwSize = size; *pdwSize = size;
ret = ERROR_INSUFFICIENT_BUFFER; ret = ERROR_INSUFFICIENT_BUFFER;
} }
@ -1352,14 +1338,11 @@ DWORD WINAPI GetIpNetTable(PMIB_IPNETTABLE pIpNetTable, PULONG pdwSize, BOOL bOr
if (bOrder) if (bOrder)
qsort(pIpNetTable->table, pIpNetTable->dwNumEntries, qsort(pIpNetTable->table, pIpNetTable->dwNumEntries,
sizeof(MIB_IPNETROW), IpNetTableSorter); sizeof(MIB_IPNETROW), IpNetTableSorter);
ret = NO_ERROR;
} }
HeapFree(GetProcessHeap(), 0, table); HeapFree(GetProcessHeap(), 0, table);
}
} }
} TRACE("returning %d\n", ret);
TRACE("returning %d\n", ret); return ret;
return ret;
} }

View File

@ -1312,47 +1312,116 @@ DWORD getNumArpEntries(void)
return getNumWithOneHeader("/proc/net/arp"); return getNumWithOneHeader("/proc/net/arp");
} }
static MIB_IPNETTABLE *append_ipnet_row( HANDLE heap, DWORD flags, MIB_IPNETTABLE *table,
DWORD *count, const MIB_IPNETROW *row )
{
if (table->dwNumEntries >= *count)
{
MIB_IPNETTABLE *new_table;
DWORD new_count = table->dwNumEntries * 2;
if (!(new_table = HeapReAlloc( heap, flags, table,
FIELD_OFFSET(MIB_IPNETTABLE, table[new_count] ))))
{
HeapFree( heap, 0, table );
return NULL;
}
*count = new_count;
table = new_table;
}
memcpy( &table->table[table->dwNumEntries++], row, sizeof(*row) );
return table;
}
DWORD getArpTable(PMIB_IPNETTABLE *ppIpNetTable, HANDLE heap, DWORD flags) DWORD getArpTable(PMIB_IPNETTABLE *ppIpNetTable, HANDLE heap, DWORD flags)
{ {
DWORD ret = NO_ERROR; MIB_IPNETTABLE *table;
if (!ppIpNetTable) MIB_IPNETROW row;
ret = ERROR_INVALID_PARAMETER; DWORD ret = NO_ERROR, count = 16;
else {
DWORD numEntries = getNumArpEntries();
DWORD size = sizeof(MIB_IPNETTABLE);
PMIB_IPNETTABLE table;
if (numEntries > 1) if (!ppIpNetTable) return ERROR_INVALID_PARAMETER;
size += (numEntries - 1) * sizeof(MIB_IPNETROW);
table = HeapAlloc(heap, flags, size); if (!(table = HeapAlloc( heap, flags, FIELD_OFFSET(MIB_IPNETTABLE, table[count] ))))
#if defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP) return ERROR_OUTOFMEMORY;
if (table)
table->dwNumEntries = 0;
#ifdef __linux__
{
FILE *fp;
if ((fp = fopen("/proc/net/arp", "r")))
{
char buf[512], *ptr;
DWORD flags;
/* skip header line */
ptr = fgets(buf, sizeof(buf), fp);
while ((ptr = fgets(buf, sizeof(buf), fp)))
{
memset( &row, 0, sizeof(row) );
row.dwAddr = inet_addr(ptr);
while (*ptr && !isspace(*ptr)) ptr++;
strtoul(ptr + 1, &ptr, 16); /* hw type (skip) */
flags = strtoul(ptr + 1, &ptr, 16);
#ifdef ATF_COM
if (flags & ATF_COM) row.dwType = MIB_IPNET_TYPE_DYNAMIC;
else
#endif
#ifdef ATF_PERM
if (flags & ATF_PERM) row.dwType = MIB_IPNET_TYPE_STATIC;
else
#endif
row.dwType = MIB_IPNET_TYPE_OTHER;
while (*ptr && isspace(*ptr)) ptr++;
while (*ptr && !isspace(*ptr))
{
row.bPhysAddr[row.dwPhysAddrLen++] = strtoul(ptr, &ptr, 16);
if (*ptr) ptr++;
}
while (*ptr && isspace(*ptr)) ptr++;
while (*ptr && !isspace(*ptr)) ptr++; /* mask (skip) */
while (*ptr && isspace(*ptr)) ptr++;
getInterfaceIndexByName(ptr, &row.dwIndex);
if (!(table = append_ipnet_row( heap, flags, table, &count, &row )))
break;
}
fclose(fp);
}
else ret = ERROR_NOT_SUPPORTED;
}
#elif defined(HAVE_SYS_SYSCTL_H) && defined(NET_RT_DUMP)
{ {
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_LLINFO}; int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_FLAGS, RTF_LLINFO};
#define MIB_LEN (sizeof(mib) / sizeof(mib[0])) #define MIB_LEN (sizeof(mib) / sizeof(mib[0]))
size_t needed; size_t needed;
char *buf, *lim, *next; char *buf = NULL, *lim, *next;
struct rt_msghdr *rtm; struct rt_msghdr *rtm;
struct sockaddr_inarp *sinarp; struct sockaddr_inarp *sinarp;
struct sockaddr_dl *sdl; struct sockaddr_dl *sdl;
*ppIpNetTable = table;
table->dwNumEntries = 0;
if (sysctl (mib, MIB_LEN, NULL, &needed, NULL, 0) == -1) if (sysctl (mib, MIB_LEN, NULL, &needed, NULL, 0) == -1)
{ {
ERR ("failed to get size of arp table\n"); ERR ("failed to get arp table\n");
return ERROR_NOT_SUPPORTED; ret = ERROR_NOT_SUPPORTED;
goto done;
} }
buf = HeapAlloc (GetProcessHeap (), 0, needed); buf = HeapAlloc (GetProcessHeap (), 0, needed);
if (!buf) return ERROR_OUTOFMEMORY; if (!buf)
{
ret = ERROR_OUTOFMEMORY;
goto done;
}
if (sysctl (mib, MIB_LEN, buf, &needed, NULL, 0) == -1) if (sysctl (mib, MIB_LEN, buf, &needed, NULL, 0) == -1)
{ {
ERR ("failed to get arp table\n"); ret = ERROR_NOT_SUPPORTED;
HeapFree (GetProcessHeap (), 0, buf); goto done;
return ERROR_NOT_SUPPORTED;
} }
lim = buf + needed; lim = buf + needed;
@ -1364,110 +1433,33 @@ DWORD getArpTable(PMIB_IPNETTABLE *ppIpNetTable, HANDLE heap, DWORD flags)
sdl = (struct sockaddr_dl *)((char *)sinarp + ROUNDUP(sinarp->sin_len)); sdl = (struct sockaddr_dl *)((char *)sinarp + ROUNDUP(sinarp->sin_len));
if(sdl->sdl_alen) /* arp entry */ if(sdl->sdl_alen) /* arp entry */
{ {
DWORD byte = strtoul(&sdl->sdl_data[sdl->sdl_alen], NULL, 16); memset( &row, 0, sizeof(row) );
memset(&table->table[table->dwNumEntries], 0, sizeof(MIB_IPNETROW)); row.dwAddr = sinarp->sin_addr.s_addr;
table->table[table->dwNumEntries].dwAddr = sinarp->sin_addr.s_addr; row.dwIndex = sdl->sdl_index;
table->table[table->dwNumEntries].dwIndex = sdl->sdl_index; row.dwPhysAddrLen = min( 8, sdl->sdl_alen );
table->table[table->dwNumEntries].dwPhysAddrLen = sdl->sdl_alen; memcpy( row.bPhysAddr, &sdl->sdl_data[sdl->sdl_nlen], row.dwPhysAddrLen );
if(rtm->rtm_rmx.rmx_expire == 0) row.dwType = MIB_IPNET_TYPE_STATIC;
else if(sinarp->sin_other & SIN_PROXY) row.dwType = MIB_IPNET_TYPE_OTHER;
else if(rtm->rtm_rmx.rmx_expire != 0) row.dwType = MIB_IPNET_TYPE_DYNAMIC;
else row.dwType = MIB_IPNET_TYPE_INVALID;
table->table[table->dwNumEntries].bPhysAddr[ if (!(table = append_ipnet_row( heap, flags, table, &count, &row )))
table->table[table->dwNumEntries].dwPhysAddrLen++] = break;
byte & 0x0ff;
if(rtm->rtm_rmx.rmx_expire == 0)
table->table[table->dwNumEntries].dwType = MIB_IPNET_TYPE_STATIC;
else if(sinarp->sin_other & SIN_PROXY)
table->table[table->dwNumEntries].dwType = MIB_IPNET_TYPE_OTHER;
else if(rtm->rtm_rmx.rmx_expire != 0)
table->table[table->dwNumEntries].dwType = MIB_IPNET_TYPE_DYNAMIC;
else
table->table[table->dwNumEntries].dwType = MIB_IPNET_TYPE_INVALID;
table->dwNumEntries++;
} }
next += rtm->rtm_msglen; next += rtm->rtm_msglen;
} }
HeapFree (GetProcessHeap (), 0, buf); done:
HeapFree( GetProcessHeap (), 0, buf );
} }
else #else
ret = ERROR_OUTOFMEMORY; FIXME( "not implemented\n" );
return ret; ret = ERROR_NOT_SUPPORTED;
#endif #endif
if (table) { if (!table) return ERROR_OUTOFMEMORY;
FILE *fp; if (!ret) *ppIpNetTable = table;
*ppIpNetTable = table; else HeapFree( heap, flags, table );
table->dwNumEntries = 0; return ret;
/* get from /proc/net/arp, no error if can't */
fp = fopen("/proc/net/arp", "r");
if (fp) {
char buf[512] = { 0 }, *ptr;
/* skip header line */
ptr = fgets(buf, sizeof(buf), fp);
while (ptr && table->dwNumEntries < numEntries) {
ptr = fgets(buf, sizeof(buf), fp);
if (ptr) {
char *endPtr;
memset(&table->table[table->dwNumEntries], 0, sizeof(MIB_IPNETROW));
table->table[table->dwNumEntries].dwAddr = inet_addr(ptr);
while (ptr && *ptr && !isspace(*ptr))
ptr++;
if (ptr && *ptr) {
strtoul(ptr, &endPtr, 16); /* hw type (skip) */
ptr = endPtr;
}
if (ptr && *ptr) {
DWORD flags = strtoul(ptr, &endPtr, 16);
#ifdef ATF_COM
if (flags & ATF_COM)
table->table[table->dwNumEntries].dwType =
MIB_IPNET_TYPE_DYNAMIC;
else
#endif
#ifdef ATF_PERM
if (flags & ATF_PERM)
table->table[table->dwNumEntries].dwType =
MIB_IPNET_TYPE_STATIC;
else
#endif
table->table[table->dwNumEntries].dwType = MIB_IPNET_TYPE_OTHER;
ptr = endPtr;
}
while (ptr && *ptr && isspace(*ptr))
ptr++;
while (ptr && *ptr && !isspace(*ptr)) {
DWORD byte = strtoul(ptr, &endPtr, 16);
if (endPtr && *endPtr) {
endPtr++;
table->table[table->dwNumEntries].bPhysAddr[
table->table[table->dwNumEntries].dwPhysAddrLen++] =
byte & 0x0ff;
}
ptr = endPtr;
}
if (ptr && *ptr) {
strtoul(ptr, &endPtr, 16); /* mask (skip) */
ptr = endPtr;
}
getInterfaceIndexByName(ptr,
&table->table[table->dwNumEntries].dwIndex);
table->dwNumEntries++;
}
}
fclose(fp);
}
else
ret = ERROR_NOT_SUPPORTED;
}
else
ret = ERROR_OUTOFMEMORY;
}
return ret;
} }
DWORD getNumUdpEntries(void) DWORD getNumUdpEntries(void)