Advertisement
Pydzik45

[PPS] IpRoute Table

May 25th, 2022
469
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ARP_App_Pyda.cpp
  2. // Przemysław Pyda
  3. // 127361
  4. // Programowanie Sieciowe
  5.  
  6. #include <windows.h>
  7. #include <iphlpapi.h>
  8. #include <assert.h>
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. DWORD MyGetIpAddrTable(PMIB_IPADDRTABLE& pIpAddrTable, BOOL fOrder = FALSE);
  13. DWORD MyGetIpForwardTable(PMIB_IPFORWARDTABLE& pIpRouteTab, BOOL fOrder = FALSE);
  14. bool InterfaceIdxToInterfaceIp(PMIB_IPADDRTABLE pIpAddrTable, DWORD dwIndex, char str[]);
  15. void PrintIpForwardTable(PMIB_IPFORWARDTABLE pIpRouteTable);
  16.  
  17. void Usage(const char* pszProgramName)
  18. {
  19.     cout << pszProgramName << " -p" << " Prints route table" << endl;
  20.     WSACleanup();
  21.     exit(1);
  22. }
  23.  
  24. void DoGetIpForwardTable()
  25. {
  26.     DWORD dwStatus;
  27.     PMIB_IPFORWARDTABLE pIpRouteTab = NULL; // Ip routing table
  28.     if ((dwStatus = MyGetIpForwardTable(pIpRouteTab, TRUE)) == NO_ERROR)
  29.     {
  30.         PrintIpForwardTable(pIpRouteTab);
  31.         free(pIpRouteTab);
  32.         return;
  33.     }
  34.     else if (dwStatus == ERROR_NO_DATA)
  35.     {
  36.         printf("No entries in route table.\n");
  37.         if (pIpRouteTab)
  38.             free(pIpRouteTab);
  39.         return;
  40.     }
  41.     else
  42.     {
  43.         if (pIpRouteTab)
  44.             free(pIpRouteTab);
  45.         printf("IpRoute returned 0x%x\n", dwStatus);
  46.         return;
  47.     }
  48. }
  49.  
  50. DWORD MyGetIpForwardTable(PMIB_IPFORWARDTABLE& pIpRouteTab, BOOL fOrder)
  51. {
  52.     DWORD status = NO_ERROR;
  53.     DWORD statusRetry = NO_ERROR;
  54.     DWORD dwActualSize = 0;
  55.     status = GetIpForwardTable(pIpRouteTab, &dwActualSize, fOrder);
  56.     if (status == NO_ERROR)
  57.     {
  58.         printf("No error\n");
  59.         return status;
  60.     }
  61.     else if (status == ERROR_INSUFFICIENT_BUFFER)
  62.     {
  63.         pIpRouteTab = (PMIB_IPFORWARDTABLE)malloc(dwActualSize);
  64.         assert(pIpRouteTab);
  65.         statusRetry = GetIpForwardTable(pIpRouteTab, &dwActualSize, fOrder); return statusRetry;
  66.     }
  67.     else
  68.     {
  69.         return status;
  70.     }
  71. }
  72.  
  73. void PrintIpForwardTable(PMIB_IPFORWARDTABLE pIpRouteTable)
  74. {
  75.     DWORD i, dwStatus, dwCurrIndex;
  76.     struct in_addr inadDest;
  77.     struct in_addr inadMask;
  78.     struct in_addr inadGateway;
  79.     char szIpAddr[128];
  80.     PMIB_IPADDRTABLE pIpAddrTable = NULL;
  81.     char szDestIp[128];
  82.     char szMaskIp[128];
  83.     char szGatewayIp[128];
  84.     if (pIpRouteTable == NULL)
  85.     {
  86.         printf("pIpRouteTable == NULL in line %d\n", __LINE__);
  87.         return;
  88.     }
  89.     if ((dwStatus = MyGetIpAddrTable(pIpAddrTable)) != NO_ERROR)
  90.     {
  91.         printf("GetIpAddrTable returned 0x%x\n", dwStatus);
  92.         if (pIpAddrTable)
  93.             free(pIpAddrTable);
  94.         return;
  95.     }
  96.     assert(pIpAddrTable);
  97.     printf("Active Routes:\n\n");
  98.     printf(" Network Address Netmask Gateway Address Interface Metric\n");
  99.     for (i = 0; i < pIpRouteTable->dwNumEntries; i++)
  100.     {
  101.         dwCurrIndex = pIpRouteTable->table[i].dwForwardIfIndex;
  102.         if (InterfaceIdxToInterfaceIp(pIpAddrTable, dwCurrIndex, szIpAddr) == FALSE)
  103.         {
  104.             printf("Error: Could not convert Interface number 0x%X to IP address.\n", dwCurrIndex); if (pIpAddrTable)
  105.                 free(pIpAddrTable);
  106.             return;
  107.         }
  108.         inadDest.s_addr = pIpRouteTable->table[i].dwForwardDest;
  109.         inadMask.s_addr = pIpRouteTable->table[i].dwForwardMask;
  110.         inadGateway.s_addr = pIpRouteTable->table[i].dwForwardNextHop;
  111.         strcpy_s(szDestIp, sizeof(szDestIp), inet_ntoa(inadDest));
  112.         strcpy_s(szMaskIp, sizeof(szDestIp), inet_ntoa(inadMask));
  113.         strcpy_s(szGatewayIp, sizeof(szDestIp), inet_ntoa(inadGateway));
  114.         printf(" %15s %16s %16s %16s %7d\n",
  115.             szDestIp,
  116.             szMaskIp,
  117.             szGatewayIp,
  118.             szIpAddr,
  119.             pIpRouteTable->table[i].dwForwardMetric1);
  120.     }
  121.     if (pIpAddrTable)
  122.         free(pIpAddrTable);
  123. }
  124.  
  125. bool InterfaceIdxToInterfaceIp(PMIB_IPADDRTABLE pIpAddrTable, DWORD dwIndex, char str[]) {
  126.     struct in_addr inadTmp;
  127.     char* szIpAddr2;
  128.     if (pIpAddrTable == NULL || str == NULL)
  129.         return FALSE;
  130.     str[0] = '\0';
  131.     for (DWORD dwIdx = 0; dwIdx < pIpAddrTable->dwNumEntries; dwIdx++)
  132.     {
  133.         if (dwIndex == pIpAddrTable->table[dwIdx].dwIndex)
  134.         {
  135.             inadTmp.s_addr = pIpAddrTable->table[dwIdx].dwAddr; szIpAddr2 = inet_ntoa(inadTmp);
  136.             if (szIpAddr2)
  137.             {
  138.                 strcpy_s(str, 128, szIpAddr2);
  139.                 return TRUE;
  140.             }
  141.             else
  142.                 return FALSE;
  143.         }
  144.     }
  145.     return FALSE;
  146. }
  147.  
  148. DWORD MyGetIpAddrTable(PMIB_IPADDRTABLE& pIpAddrTable, BOOL fOrder)
  149. {
  150.     DWORD status = NO_ERROR;
  151.     DWORD statusRetry = NO_ERROR;
  152.     DWORD dwActualSize = 0;
  153.     status = GetIpAddrTable(pIpAddrTable, &dwActualSize, fOrder);
  154.     if (status == NO_ERROR)
  155.     {
  156.         printf("No error\n");
  157.         return status;
  158.     }
  159.     else if (status == ERROR_INSUFFICIENT_BUFFER)
  160.     {
  161.         pIpAddrTable = (PMIB_IPADDRTABLE)malloc(dwActualSize);
  162.         assert(pIpAddrTable);
  163.         statusRetry = GetIpAddrTable(pIpAddrTable, &dwActualSize, fOrder); return statusRetry;
  164.     }
  165.     else
  166.     {
  167.         return status;
  168.     }
  169. }
  170.  
  171. void main(int argc, char** argv)
  172. {
  173.     WORD    wVersionRequested = MAKEWORD(1, 1);
  174.     WSADATA wsaData;
  175.     int nRet;
  176.     nRet = WSAStartup(wVersionRequested, &wsaData);
  177.  
  178.     if (wsaData.wVersion != wVersionRequested) {
  179.         cerr << stderr << "Wrong version" << endl;
  180.         return;
  181.     }
  182.     if ((argc < 2) || (argv[1][0] != '-')) {
  183.         cout << "IpRoute: Bad argument" << endl;
  184.         Usage("IpRoute");
  185.     }
  186.     if (strlen(argv[1]) > 2) {
  187.         cout << "IpRoute: Bad argument" << endl;
  188.         Usage("IpRoute");
  189.     }
  190.  
  191.     switch (argv[1][1])
  192.     {
  193.     case 'p':
  194.         DoGetIpForwardTable();
  195.         break;
  196.     default:
  197.         cout << "IpRoute: Bad argument" << endl;
  198.         Usage("IpRoute");
  199.         break;
  200.     }
  201.     WSACleanup();
  202. }
Advertisement
RAW Paste Data Copied
Advertisement