//---------------------------------------------------------------------------- // Print out ip routing table in the following format: //Active Routes: // // Network Address Netmask Gateway Address Interface Metric // 0.0.0.0 0.0.0.0 157.54.176.1 157.54.177.149 1 // 127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1 // 157.54.176.0 255.255.252.0 157.54.177.149 157.54.177.149 1 // 157.54.177.149 255.255.255.255 127.0.0.1 127.0.0.1 1 // 157.54.255.255 255.255.255.255 157.54.177.149 157.54.177.149 1 // 224.0.0.0 224.0.0.0 157.54.177.149 157.54.177.149 1 // 255.255.255.255 255.255.255.255 157.54.177.149 157.54.177.149 1 //---------------------------------------------------------------------------- void PrintIpForwardTable(PMIB_IPFORWARDTABLE pIpRouteTable) { DWORD i, dwStatus, dwCurrIndex; struct in_addr inadDest; struct in_addr inadMask; struct in_addr inadGateway; char szIpAddr[128]; // ip address of an interface index PMIB_IPADDRTABLE pIpAddrTable = NULL; char szDestIp[128]; char szMaskIp[128]; char szGatewayIp[128]; if (pIpRouteTable == NULL) { printf( "pIpRouteTable == 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; } assert(pIpAddrTable); printf("Active Routes:\n\n"); printf(" Network Address Netmask Gateway Address Interface Metric\n"); for (i = 0; i < pIpRouteTable->dwNumEntries; i++) { dwCurrIndex = pIpRouteTable->table[i].dwForwardIfIndex; if (InterfaceIdxToInterfaceIp(pIpAddrTable, dwCurrIndex, szIpAddr) == FALSE) { printf("Error: Could not convert Interface number 0x%X to IP address.\n",dwCurrIndex); if (pIpAddrTable) free(pIpAddrTable); return; } inadDest.s_addr = pIpRouteTable->table[i].dwForwardDest; inadMask.s_addr = pIpRouteTable->table[i].dwForwardMask; inadGateway.s_addr = pIpRouteTable->table[i].dwForwardNextHop; strcpy_s(szDestIp, sizeof(szDestIp), inet_ntoa(inadDest)); strcpy_s(szMaskIp, sizeof(szDestIp), inet_ntoa(inadMask)); strcpy_s(szGatewayIp, sizeof(szDestIp), inet_ntoa(inadGateway)); printf(" %15s %16s %16s %16s %7d\n", szDestIp, szMaskIp, szGatewayIp, szIpAddr, pIpRouteTable->table[i].dwForwardMetric1); } if (pIpAddrTable) free(pIpAddrTable); }
int main() { DWORD i, dwCurrIndex; char szPrintablePhysAddr[256]; char szType[128]; char szIpAddr[128]; // 首先获取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; // 类型 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); } return 0; }