rt,封包查询 包过滤除外<--..有大神知道吗,告诉我方法就行,感激不尽
以下是r3下获取 网关MAC
#include<winsock2.h>
#pragma comment(lib,"ws2_32.Lib")
#include<mswsock.h>
#pragma comment(lib,"mswsock.lib")
#include<winsock.h>
#pragma comment(lib,"wsock32.lib")
#include<ws2tcpip.h>
#include<iphlpapi.h>
#pragma comment(lib,"iphlpapi")
#include <algorithm>
DWORD MyGetIpAddrTable(PMIB_IPADDRTABLE& pIpAddrTable)
{
DWORD status = NO_ERROR;
DWORD statusRetry = NO_ERROR;
DWORD dwActualSize = 0;
// query for buffer size needed
status = GetIpAddrTable(pIpAddrTable, &dwActualSize, TRUE);
if (status == NO_ERROR)
{
printf("No error\n");
return status;
}
else if (status == ERROR_INSUFFICIENT_BUFFER)
{
// need more space
pIpAddrTable = (PMIB_IPADDRTABLE) malloc(dwActualSize);
statusRetry = GetIpAddrTable(pIpAddrTable, &dwActualSize, TRUE);
return statusRetry;
}
else
{
return status;
}
}
bool InterfaceIdxToInterfaceIp(PMIB_IPADDRTABLE pIpAddrTable, DWORD dwIndex, char str[])
{
struct in_addr inadTmp;
char* szIpAddr;
if (pIpAddrTable == NULL || str == NULL)
return FALSE;
str[0] = '\0';
for (DWORD dwIdx = 0; dwIdx < pIpAddrTable->dwNumEntries; dwIdx++)
{
if (dwIndex == pIpAddrTable->table[dwIdx].dwIndex)
{
inadTmp.s_addr = pIpAddrTable->table[dwIdx].dwAddr;
szIpAddr = inet_ntoa(inadTmp);
if (szIpAddr)
{
strcpy(str, szIpAddr);
return TRUE;
}
else
return FALSE;
}
}
return FALSE;
}
bool PhysAddrToString(BYTE PhysAddr[], DWORD PhysAddrLen, char str[])
{
if (PhysAddr == NULL || PhysAddrLen == 0 || str == NULL)
return FALSE;
str[0] = '\0';
for (DWORD dwIdx = 0; dwIdx < PhysAddrLen; dwIdx++)
{
if (dwIdx == PhysAddrLen-1)
sprintf(str+(dwIdx*3), "%02X", ((int)PhysAddr[dwIdx])&0xff);
else
sprintf(str+(dwIdx*3), "%02X-", ((int)PhysAddr[dwIdx])&0xff);
}
return TRUE;
}
void PrintIpNetTable(PMIB_IPNETTABLE pIpNetTable)
{
DWORD i, dwStatus, dwCurrIndex;
struct in_addr inadTmp;
char szPrintablePhysAddr[256];
char szType[128];
char szIpAddr[128];
PMIB_IPADDRTABLE pIpAddrTable = NULL;
if (pIpNetTable == NULL)
{
printf( "pIpNetTable == NULL in line %d\n", __LINE__);
return;
}
// get IP Address Table for mapping interface index number to ip address
if ( (dwStatus = MyGetIpAddrTable(pIpAddrTable)) != NO_ERROR)
{
printf("GetIpAddrTable returned 0x%x\n", dwStatus);
if (pIpAddrTable)
free(pIpAddrTable);
return;
}
// Note: the ARP table should be sorted in interface index
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;
}
for (i = 0; i < pIpNetTable->dwNumEntries; ++i)
{
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;
}
}
//DbgPrint("网关:%x:%x:%x:%x:%x:%x",gatewayMac[0],gatewayMac[1],gatewayMac[2],gatewayMac[3],gatewayMac[4],gatewayMac[5]);
UCHAR gatewayMac[6]={0};
memcpy(gatewayMac,pIpNetTable->table[i].bPhysAddr,6);
char msg[1024]="";
wsprintf(msg,"网关:%x:%x:%x:%x:%x:%x",gatewayMac[0],gatewayMac[1],gatewayMac[2],gatewayMac[3],gatewayMac[4],gatewayMac[5]);
MessageBox(NULL,msg,"",MB_OK);
PhysAddrToString(pIpNetTable->table[i].bPhysAddr, pIpNetTable->table[i].dwPhysAddrLen, szPrintablePhysAddr);
inadTmp.s_addr = pIpNetTable->table[i].dwAddr;
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");
}
printf(" %-16s %-17s %-11s\n", inet_ntoa(inadTmp), szPrintablePhysAddr, szType);
}
if (pIpAddrTable)
free(pIpAddrTable);
}
DWORD MyGetIpNetTable(PMIB_IPNETTABLE& pIpNetTable, bool fOrder)
{
DWORD status = NO_ERROR;
DWORD statusRetry = NO_ERROR;
DWORD dwActualSize = 0;
// query for buffer size needed
dwActualSize = 0;
status = GetIpNetTable(pIpNetTable, &dwActualSize, fOrder);
if (status == NO_ERROR)
{
return status;
}
else if (status == ERROR_INSUFFICIENT_BUFFER)
{
// need more space
pIpNetTable = (PMIB_IPNETTABLE) malloc(dwActualSize);
statusRetry = GetIpNetTable(pIpNetTable, &dwActualSize, fOrder);
if (statusRetry != NO_ERROR)
{
#ifdef _DEBUG
printf("Retry failed.\n");
#endif
return statusRetry;
}
else
{
return statusRetry;
}
}
else
{
#ifdef _DEBUG
printf("first getipnettable call failed\n");
#endif
return status;
}
}
void DoGetIpNetTable()
{
DWORD dwStatus;
PMIB_IPNETTABLE pIpArpTab = NULL;
if ( (dwStatus = MyGetIpNetTable(pIpArpTab, TRUE)) == NO_ERROR)
{
PrintIpNetTable(pIpArpTab);
free(pIpArpTab);
return;
}
else if ( dwStatus == ERROR_NO_DATA)
{
printf("No entries in arp cache.\n");
if (pIpArpTab)
free (pIpArpTab);
return;
}
else
{
if (pIpArpTab)
free (pIpArpTab);
printf("IpArp returned 0x%x\n", dwStatus);
return;
}
}
//
WSADATA wsadata;
WSAStartup(MAKEWORD(2,2),&wsadata);//
DoGetIpNetTable();
WSACleanup();
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课