| 1 | #include <winsock2.h>
|
|---|
| 2 | #include <ws2tcpip.h>
|
|---|
| 3 | #include <iphlpapi.h>
|
|---|
| 4 | #include <stdio.h>
|
|---|
| 5 |
|
|---|
| 6 | int __cdecl main()
|
|---|
| 7 | {
|
|---|
| 8 | int i;
|
|---|
| 9 |
|
|---|
| 10 | /* Variables used by GetIpAddrTable */
|
|---|
| 11 | PMIB_IPADDRTABLE pIPAddrTable;
|
|---|
| 12 | DWORD dwSize = 0;
|
|---|
| 13 | DWORD dwRetVal = 0;
|
|---|
| 14 | IN_ADDR IPAddr;
|
|---|
| 15 |
|
|---|
| 16 | /* Variables used to return error message */
|
|---|
| 17 | LPVOID lpMsgBuf;
|
|---|
| 18 |
|
|---|
| 19 | // Before calling AddIPAddress we use GetIpAddrTable to get
|
|---|
| 20 | // an adapter to which we can add the IP.
|
|---|
| 21 | pIPAddrTable = (MIB_IPADDRTABLE *) malloc(sizeof (MIB_IPADDRTABLE));
|
|---|
| 22 |
|
|---|
| 23 | if (pIPAddrTable) {
|
|---|
| 24 | // Make an initial call to GetIpAddrTable to get the
|
|---|
| 25 | // necessary size into the dwSize variable
|
|---|
| 26 | if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) ==
|
|---|
| 27 | ERROR_INSUFFICIENT_BUFFER) {
|
|---|
| 28 | free(pIPAddrTable);
|
|---|
| 29 | pIPAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
|---|
| 30 |
|
|---|
| 31 | }
|
|---|
| 32 | if (pIPAddrTable == NULL) {
|
|---|
| 33 | printf("Memory allocation failed for GetIpAddrTable\n");
|
|---|
| 34 | exit(1);
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 | // Make a second call to GetIpAddrTable to get the
|
|---|
| 38 | // actual data we want
|
|---|
| 39 | if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) {
|
|---|
| 40 | printf("GetIpAddrTable failed with error %d\n", dwRetVal);
|
|---|
| 41 | if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|---|
| 42 | (LPTSTR) & lpMsgBuf, 0, NULL)) {
|
|---|
| 43 | printf("\tError: %s", lpMsgBuf);
|
|---|
| 44 | LocalFree((HLOCAL)lpMsgBuf);
|
|---|
| 45 | }
|
|---|
| 46 | exit(1);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries);
|
|---|
| 50 | for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) {
|
|---|
| 51 | printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex);
|
|---|
| 52 | IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr;
|
|---|
| 53 | printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
|
|---|
| 54 | IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask;
|
|---|
| 55 | printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
|
|---|
| 56 | IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr;
|
|---|
| 57 | printf("\tBroadCast[%d]: \t%s (%ld%)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr);
|
|---|
| 58 | printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize);
|
|---|
| 59 | #if 0
|
|---|
| 60 | printf("\tType and State[%d]:", i);
|
|---|
| 61 | if (pIPAddrTable->table[i].wType & MIB_IPADDR_PRIMARY)
|
|---|
| 62 | printf("\tPrimary IP Address");
|
|---|
| 63 | if (pIPAddrTable->table[i].wType & MIB_IPADDR_DYNAMIC)
|
|---|
| 64 | printf("\tDynamic IP Address");
|
|---|
| 65 | if (pIPAddrTable->table[i].wType & MIB_IPADDR_DISCONNECTED)
|
|---|
| 66 | printf("\tAddress is on disconnected interface");
|
|---|
| 67 | if (pIPAddrTable->table[i].wType & MIB_IPADDR_DELETED)
|
|---|
| 68 | printf("\tAddress is being deleted");
|
|---|
| 69 | if (pIPAddrTable->table[i].wType & MIB_IPADDR_TRANSIENT)
|
|---|
| 70 | printf("\tTransient address");
|
|---|
| 71 | #endif
|
|---|
| 72 | printf("\n");
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if (pIPAddrTable) {
|
|---|
| 76 | free(pIPAddrTable);
|
|---|
| 77 | pIPAddrTable = NULL;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | exit(0);
|
|---|
| 81 | }
|
|---|