Advertisement
Phr0zen_Penguin

subnetCalculator.hpp - Subnet Calculator Header File

Mar 12th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.60 KB | None | 0 0
  1. /**
  2.  * [subnetCalculator.hpp]
  3.  *
  4.  * Subnet Calculator header file.
  5.  *
  6.  * by Rocco Castoro.
  7.  */
  8.  
  9. #ifndef __SUBNET_CALCULATOR_H__                     // Include guard.
  10. #define __SUBNET_CALCULATOR_H__
  11.  
  12. //#include "stdafx.h"                               // M$ PCH stuff.
  13. #include <iostream>
  14. #include <string>
  15. #include <vector>
  16. #include <sstream>
  17. #include <cstdio>
  18. #include <cstdlib>
  19. #include <cmath>
  20.  
  21. using std::cin;                                     // Beats the crap outta the general std namespace use convention...
  22. using std::cout;
  23. using std::endl;
  24. using std::string;
  25. using std::vector;
  26. using std::stringstream;
  27. using std::ostringstream;                           //...
  28.  
  29. /**
  30.  * The getOctetsIP() function:
  31.  */
  32. int getOctetsIP(string ip, vector<int> &octetsIP)   // Define vector<int> octets, using reference from main.
  33. {
  34.     stringstream    sip(ip);                        // Use stringstream named sip and populate with IP.
  35.     string          temp;
  36.  
  37.     octetsIP.clear();                               // Clears the octectsIP vector, in case the main function re-runs this function.
  38.  
  39.     vector<bool>    ipInRange;
  40.  
  41.         while(getline(sip, temp, '.'))              // Every time getline() receives new stream element from sip, it saves it to temp, until it reaches the '.' delimiter...
  42.             octetsIP.push_back(atoi(temp.c_str())); // ...then push_back octet with new element.
  43.  
  44.         if(octetsIP.size() == 4)
  45.         {
  46.             for(int i = 0; i < octetsIP.size(); i++)
  47.                 if(octetsIP[i] >= 0 && octetsIP[i] <= 255)
  48.                     ipInRange.push_back(true);
  49.                 else
  50.                     ipInRange.push_back(false);
  51.  
  52.             if(ipInRange[0] == true && ipInRange[1] == true && ipInRange[2] == true && ipInRange[3] == true)
  53.                 return 0;
  54.             else
  55.             {
  56.                 cout << endl << "There are only 255 bits per octet.  Please re-enter IP." << endl << endl;
  57.  
  58.                 return 1;
  59.             }
  60.         }
  61.         else
  62.         {
  63.             cout << endl << "Please enter four octets in dot (.) notation." << endl << endl;
  64.  
  65.             return 1;
  66.         }
  67. }
  68.  
  69.  
  70. /**
  71.  * The getOctetsMask() function:
  72.  */
  73. int getOctetsMask(string mask, vector<int> &octetsMask)
  74. {
  75.     stringstream    smask(mask);
  76.     string          temp;
  77.  
  78.     octetsMask.clear();                             // Clears the octetsMask vector, in case the main function re-runs this function.
  79.  
  80.     vector<bool>    maskInRange;
  81.  
  82.         while(getline(smask, temp, '.'))
  83.             octetsMask.push_back(atoi(temp.c_str()));
  84.  
  85.         if(octetsMask.size() == 4)
  86.         {
  87.             for(int i = 0; i < octetsMask.size(); i++)
  88.                 switch(octetsMask[i])
  89.                 {
  90.                     case 0:
  91.                     case 128:
  92.                     case 192:
  93.                     case 224:
  94.                     case 240:
  95.                     case 248:
  96.                     case 252:
  97.                     case 254:
  98.                     case 255:
  99.                         maskInRange.push_back(true);
  100.                         break;
  101.                     default:
  102.                         maskInRange.push_back(false);
  103.                         break;
  104.                 }
  105.  
  106.             if(maskInRange[0] == true && maskInRange[1] == true && maskInRange[2] == true && maskInRange[3] == true)
  107.                 return 0;
  108.             else
  109.             {
  110.                 cout << endl << "Subnet masks only use 2^[0-7].  Please re-enter mask." << endl << endl;
  111.  
  112.                 return 1;
  113.             }
  114.         }
  115.         else
  116.         {
  117.             cout << endl << "Please enter four octets in dot (.) notation." << endl << endl;
  118.  
  119.             return 1;
  120.         }
  121. }
  122.  
  123.  
  124. /**
  125.  * The calcCLass() function:
  126.  */
  127. int calcClass(vector<int> &octetsIP)
  128. {
  129.     if(octetsIP[0] == 10)
  130.         return 1;                                   // Class A private address blocks.
  131.     else if(octetsIP[0] == 172 && octetsIP[1] >= 16 && octetsIP[1] <= 31)
  132.         return 2;                                   // Class B private address blocks.
  133.     else if(octetsIP[0] == 192 && octetsIP[1] == 168)
  134.         return 3;                                   // Class C private address blocks.
  135.     else if(octetsIP[0] == 127)
  136.         return 4;                                   // Loopback Address reserved address blocks.
  137.     else if(octetsIP[0] >= 0 && octetsIP[0] < 127)
  138.         return 5;
  139.     else if(octetsIP[0] > 127 && octetsIP[0] < 192)
  140.         return 6;
  141.     else if(octetsIP[0] > 191 && octetsIP[0] < 224)
  142.         return 7;
  143.     else if(octetsIP[0] > 223 && octetsIP[0] < 240)
  144.         return 8;
  145.     else if(octetsIP[0] > 239 && octetsIP[0] <= 255)
  146.         return 9;
  147.     else
  148.         return 0;                                   // Out of range.
  149. }
  150.  
  151.  
  152. /**
  153.  * The getNHBits() function:
  154.  *
  155.  * Determine binary.
  156.  */
  157. int getNHBits(vector<int> &octetsIP, vector<int> &octetsMask, vector<int> &octetsIPBits, vector<int> &octetsMaskBits)
  158. {
  159.     /**
  160.      * Get IP binary rep:
  161.      */
  162.     cout << "------------------------------------------" << endl;
  163.     cout << "///////// Binary Representation //////////" << endl;
  164.     cout << "------------------------------------------" << endl;
  165.  
  166.         for(int j = 0; j < octetsIP.size(); j++)
  167.         {
  168.             if(j > 0)
  169.                 cout << ".";
  170.  
  171.             for(int mask = 128; mask; mask >>= 1)
  172.             {
  173.                 octetsIPBits.push_back((octetsIP[j] & mask) != 0);
  174.  
  175.                 cout << ((octetsIP[j] & mask) != 0);
  176.             }
  177.         }
  178.  
  179.     cout << "  : IP Address" << endl;
  180.  
  181.     /**
  182.      * Get SUBNET binary rep:
  183.      */
  184.         for(int j = 0; j < octetsMask.size(); j++)
  185.         {
  186.             if(j > 0)
  187.                 cout << ".";
  188.  
  189.             for(int mask = 128; mask; mask >>= 1)
  190.             {
  191.                 octetsIPBits.push_back((octetsMask[j] & mask) != 0);
  192.  
  193.                 cout << ((octetsMask[j] & mask) != 0);
  194.             }
  195.         }
  196.  
  197.     cout << "  : Subnet Mask" << endl;
  198.     cout << "------------------------------------------";
  199.  
  200.     return 0;
  201. }
  202.  
  203.  
  204. /**
  205.  * The getNetID() function:
  206.  *
  207.  * Perform ANDing of IP and Subnet Mask to generate Network ID range.
  208.  */
  209. vector<int> getNetID(vector<int> &octetsIPBits, vector<int> &octetsMaskBits)
  210. {
  211.     vector<int> netID;
  212.  
  213.         for(int j = 0; j < octetsIPBits.size(); j++)
  214.         {
  215.                 if((j > 0) && (j % 8 == 0))
  216.                     cout << ".";
  217.  
  218.             netID.push_back(octetsIPBits[j] & octetsMaskBits[j]);
  219.         }
  220.  
  221.     return netID;
  222. }
  223.  
  224.  
  225. /**
  226.  * The toString() function:
  227.  *
  228.  * Turn Binary to String.
  229.  */
  230. string toString(vector<int> octets)
  231. {
  232.     ostringstream   octStrm;
  233.  
  234.         for(int j = 0; j < octets.size(); j++)
  235.         {
  236.                 if(j > 0)
  237.                     octStrm << '.';
  238.  
  239.             octStrm << octets[j];
  240.         }
  241.  
  242.     return octStrm.str();
  243. }
  244.  
  245.  
  246. /**
  247.  * The toDecimal() function:
  248.  *
  249.  * Turn String back to Decimal.
  250.  */
  251. vector<int> toDecimal(vector<int> octets, vector<int> &decimals)
  252. {
  253.     stringstream    octStrm;
  254.  
  255.     decimals.clear();
  256.  
  257.         for(int j = 0; j < octets.size(); j++)
  258.         {
  259.                 if(j > 0)
  260.                     octStrm << '.';
  261.  
  262.             octStrm << octets[j];
  263.         }
  264.  
  265.         for(string temp = ""; getline(octStrm, temp, '.'); )
  266.             decimals.push_back(atoi(temp.c_str()));
  267.  
  268.     return decimals;
  269. }
  270.  
  271.  
  272. /**
  273.  * The getIncrement() function:
  274.  *
  275.  * Get the network increment.
  276.  */
  277. int getIncrement(vector<int> decimalMask, vector<int> decimalNetID)
  278. {
  279.     int increment = 0;
  280.  
  281.         for(int i = 0; i < decimalMask.size(); i++)
  282.         {
  283.             switch(decimalMask[i])
  284.             {
  285.                 case 255:
  286.                     increment = 1;
  287.                     break;
  288.                 case 254:
  289.                     increment = 2;
  290.                     break;
  291.                 case 252:
  292.                     increment = 4;
  293.                     break;
  294.                 case 248:
  295.                     increment = 8;
  296.                     break;
  297.                 case 240:
  298.                     increment = 16;
  299.                     break;
  300.                 case 224:
  301.                     increment = 32;
  302.                     break;
  303.                 case 192:
  304.                     increment = 64;
  305.                     break;
  306.                 case 128:
  307.                     increment = 128;
  308.                     break;
  309.                 default:
  310.                     break;
  311.             }
  312.         }
  313.  
  314.     return increment;
  315. }
  316.  
  317.  
  318. /**
  319.  * The getNetIDRange() function:
  320.  *
  321.  * Get network ID range.
  322.  */
  323. vector<int> getNetIDRange(vector<int> &decimalNetID, int &netInc, vector<int> &decimalMask)
  324. {
  325.     vector<int> netIDEnd;
  326.  
  327.         for(int i = 0; i < decimalNetID.size(); i++)
  328.             if(decimalMask[i] == 255)
  329.                 netIDEnd.push_back(decimalNetID[i]);
  330.             else if(decimalMask[i] < 255 && decimalMask[i] > 0)
  331.                 netIDEnd.push_back((decimalNetID[i] + netInc) - 1);
  332.             else
  333.                 netIDEnd.push_back(255);
  334.  
  335.     return netIDEnd;
  336. }
  337.  
  338.  
  339. /**
  340.  * The getSubnets() function:
  341.  *
  342.  * Get subnets.
  343.  */
  344. int getSubnets(vector<int> &decimalMask, int &ipClass, vector<int> &subClassMask)
  345. {
  346.     int netBits = 0;
  347.  
  348.     subClassMask.clear();
  349.  
  350.         switch(ipClass)
  351.         {
  352.             case 1:
  353.                 subClassMask.push_back(255);
  354.                 subClassMask.push_back(0);
  355.                 subClassMask.push_back(0);
  356.                 subClassMask.push_back(0);
  357.                 break;
  358.             case 2:
  359.                 subClassMask.push_back(255);
  360.                 subClassMask.push_back(255);
  361.                 subClassMask.push_back(0);
  362.                 subClassMask.push_back(0);
  363.                 break;
  364.             case 3:
  365.                 subClassMask.push_back(255);
  366.                 subClassMask.push_back(255);
  367.                 subClassMask.push_back(255);
  368.                 subClassMask.push_back(0);
  369.                 break;
  370.             case 4:
  371.             case 5:
  372.                 subClassMask.push_back(decimalMask[0]);
  373.                 subClassMask.push_back(decimalMask[1]);
  374.                 subClassMask.push_back(decimalMask[2]);
  375.                 subClassMask.push_back(decimalMask[3]);
  376.                 break;
  377.             default:
  378.                 break;
  379.         }
  380.  
  381.         for(int i = 0; i < decimalMask.size(); i++)
  382.             if(decimalMask[i] != subClassMask[i])
  383.                 switch(decimalMask[i])
  384.                 {
  385.                     case 255:
  386.                         netBits += 8;
  387.                         continue;
  388.                     case 254:
  389.                         netBits += 7;
  390.                         continue;
  391.                     case 252:
  392.                         netBits += 6;
  393.                         continue;
  394.                     case 248:
  395.                         netBits += 5;
  396.                         continue;
  397.                     case 240:
  398.                         netBits += 4;
  399.                         continue;
  400.                     case 224:
  401.                         netBits += 3;
  402.                         continue;
  403.                     case 192:
  404.                         netBits += 2;
  405.                         continue;
  406.                     case 128:
  407.                         netBits += 1;
  408.                         continue;
  409.                     case 0:
  410.                         netBits += 0;
  411.                         continue;
  412.                     default:
  413.                         netBits += 0;
  414.                         break;
  415.                 }
  416.  
  417.     return (pow(2.0, netBits));
  418. }
  419.  
  420.  
  421. /**
  422.  * The getHostsPerSubnet() function:
  423.  *
  424.  * Get hosts per subnet.
  425.  */
  426. int getHostsPerSubnet(vector<int> &decimalMask)
  427. {
  428.     int hostBits = 0;
  429.  
  430.         for(int i = 0; i < decimalMask.size(); i++)
  431.             switch(decimalMask[i])
  432.             {
  433.                 case 255:
  434.                     hostBits += 0;
  435.                     continue;
  436.                 case 254:
  437.                     hostBits += 1;
  438.                     continue;
  439.                 case 252:
  440.                     hostBits += 2;
  441.                     continue;
  442.                 case 248:
  443.                     hostBits += 3;
  444.                     continue;
  445.                 case 240:
  446.                     hostBits += 4;
  447.                     continue;
  448.                 case 224:
  449.                     hostBits += 5;
  450.                     continue;
  451.                 case 192:
  452.                     hostBits += 6;
  453.                     continue;
  454.                 case 128:
  455.                     hostBits += 7;
  456.                     continue;
  457.                 case 0:
  458.                     hostBits += 8;
  459.                     continue;
  460.                 default:
  461.                     hostBits += 0;
  462.                     break;
  463.             }
  464.  
  465.     return (pow(2.0, hostBits) - 2);
  466. }
  467.  
  468. #endif // __SUBNET_CALCULATOR_H__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement