我对C不是太懂,为了完成上级的任务,组合了几个函数,目的是监控ARP缓存,对ARP条目的删除时出现了问题,删除不了想要删除的IP地址,麻烦各位帮指导一下
int main()
{
DWORD i, dwCurrIndex;
char szPrintablePhysAddr[256];
char szType[128];
char szIpAddr[128];
GetGlobalData();
// 首先获取ARP表
PMIB_IPNETTABLE pIpNetTable = MyGetIpNetTable(true);
if (pIpNetTable == NULL)
{
printf( "pIpNetTable == NULL in line %d/n", __LINE__);
return -1;
}
// 获取IP地址表,以便根据它将ARP表项中的接口索引转化为IP地址
PMIB_IPADDRTABLE pIpAddrTable = MyGetIpAddrTable(TRUE);
// 当前的适配器索引。注意,ARP表应该按照接口索引排序
dwCurrIndex = pIpNetTable->table[0].dwIndex;
if(InterfaceIdxToInterfaceIp(pIpAddrTable, dwCurrIndex, szIpAddr))
{
printf("/nInterface: %s on Interface 0x%X/n", szIpAddr, dwCurrIndex);
printf(" Internet Address Physical Address Type 通信情况/n");
}
else
{
printf("Error: Could not convert Interface number 0x%X to IP address./n",
pIpNetTable->table[0].dwIndex);
return -1;
}
// 打印出索引为dwCurrIndex的适配器上的ARP表项
for(i = 0; i < pIpNetTable->dwNumEntries; ++i)
{
// 不相等则说明要打印下一个适配器上的ARP表项了
if(pIpNetTable->table[i].dwIndex != dwCurrIndex)
{
dwCurrIndex = pIpNetTable->table[i].dwIndex;
if (InterfaceIdxToInterfaceIp(pIpAddrTable, dwCurrIndex, szIpAddr))
{
printf("Interface: %s on Interface 0x%X/n", szIpAddr, dwCurrIndex);
printf("Internet Address Physical Address Type/n");
}
else
{
printf("Error: Could not convert Interface number 0x%X to IP address./n",
pIpNetTable->table[0].dwIndex);
return -1;
}
}
// 打印出此ARP表项中的数据
// MAC地址
u_char *p = pIpNetTable->table[i].bPhysAddr;
wsprintf(szPrintablePhysAddr, "%02X-%02X-%02X-%02X-%02X-%02X", p[0], p[1], p[2], p[3], p[4], p[5]);
// IP地址
struct in_addr inadTmp;
inadTmp.s_addr = pIpNetTable->table[i].dwAddr;
printf("xxyy1=%ld/n",pIpNetTable->table[i].dwIndex );
// 类型
switch (pIpNetTable->table[i].dwType)
{
case 1:
strcpy(szType,"other");
break;
case 2:
strcpy(szType,"invalidated");
break;
case 3:
strcpy(szType,"dynamic");
break;
case 4:
strcpy(szType,"static");
break;
default:
strcpy(szType,"invalidType");
}
xy=pingdestip(inet_ntoa(inadTmp));
if(xy==-1)
{ printf(" %-16s %-17s %-11s 受阻,删除/n",inet_ntoa(inadTmp), szPrintablePhysAddr, szType);
if(DeleteIpNetEntry (pIpNetTable->table )== NO_ERROR) //主要是这时使用删除,但是没有成功
printf("A IP Address Deleted./n");
}
else
printf(" %-16s %-17s %-11s 正常/n",inet_ntoa(inadTmp), szPrintablePhysAddr, szType);
return 0;
}
// 获取IP地址到适配器的映射关系,即ARP表
PMIB_IPNETTABLE MyGetIpNetTable(BOOL bOrder)
{
PMIB_IPNETTABLE pIpNetTable = NULL;
DWORD dwActualSize = 0;
// 查询所需缓冲区的大小
if(::GetIpNetTable(pIpNetTable,
&dwActualSize, bOrder) == ERROR_INSUFFICIENT_BUFFER)
{
// 为MIB_IPNETTABLE结构申请内存
pIpNetTable = (PMIB_IPNETTABLE)::GlobalAlloc(GPTR, dwActualSize);
// 获取ARP表
if(::GetIpNetTable(pIpNetTable,
&dwActualSize, bOrder) == NO_ERROR)
{
return pIpNetTable;
}
::GlobalFree(pIpNetTable);
}
return NULL;
}
void MyFreeIpNetTable(PMIB_IPNETTABLE pIpNetTable)
{
if(pIpNetTable != NULL)
::GlobalFree(pIpNetTable);
}
PMIB_IPADDRTABLE MyGetIpAddrTable(BOOL bOrder)
{
PMIB_IPADDRTABLE pIpAddrTable = NULL;
DWORD dwActualSize = 0;
// 查询所需缓冲区的大小
if(::GetIpAddrTable(pIpAddrTable,
&dwActualSize, bOrder) == ERROR_INSUFFICIENT_BUFFER)
{
// 为MIB_IPADDRTABLE结构申请内存
pIpAddrTable = (PMIB_IPADDRTABLE)::GlobalAlloc(GPTR, dwActualSize);
// 获取IP地址表
if(::GetIpAddrTable(pIpAddrTable,
&dwActualSize, bOrder) == NO_ERROR)
return pIpAddrTable;
::GlobalFree(pIpAddrTable);
}
return NULL;
}
void MyFreeIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable)
{
if(pIpAddrTable != NULL)
::GlobalFree(pIpAddrTable);
}