Advertisement
gggamergg

subnett

Oct 7th, 2021
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.33 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<math.h>
  4. #include<sstream>
  5. using namespace std;
  6. class ipv_4
  7. {
  8. private:
  9.     int ip_a1, ip_a2, ip_a3, ip_a4;
  10.     char ip_class;
  11.     int sub_no;
  12.     int sub_bit;
  13.     string default_subnetmask;
  14.     string require_subnetmask;
  15.     int unmasked_bit;
  16.     int total_host;
  17. public:
  18.     ipv_4()
  19.     {
  20.         string ip_address;
  21.         cout << "Enter the IP address: ";
  22.         cin >> ip_address;
  23.         int i = 0;
  24.         string j = "";
  25.         while (ip_address[i] != '.')
  26.         {
  27.             j = j + ip_address[i];
  28.             i++;
  29.         }
  30.         ip_a1 = stoi(j);
  31.         i++;
  32.         j = "";
  33.         while (ip_address[i] != '.')
  34.         {
  35.             j = j + ip_address[i];
  36.             i++;
  37.         }
  38.         ip_a2 = stoi(j);
  39.         i++;
  40.         j = "";
  41.         while (ip_address[i] != '.')
  42.         {
  43.             j = j + ip_address[i];
  44.             i++;
  45.         }
  46.         ip_a3 = stoi(j);
  47.         i++;
  48.         j = "";
  49.         while (ip_address[i] != '\0')
  50.         {
  51.             j = j + ip_address[i];
  52.             i++;
  53.         }
  54.         ip_a4 = stoi(j);
  55.         determine_class_subnetmask();
  56.     }
  57.     void determine_class_subnetmask();
  58.     void display_class_subnetmask();
  59.     void read_subnet();
  60.     int find_no_bits_borrowed();
  61.     void find_subnetmask();
  62.     void display_require_subnet();
  63.     void display_range();
  64. };
  65. void ipv_4::determine_class_subnetmask()
  66. {
  67.     if (ip_a1 < 128)
  68.     {
  69.         default_subnetmask = "255.0.0.0";
  70.         ip_class = 'A';
  71.         return;
  72.     }
  73.     if (ip_a1 < 192)
  74.     {
  75.         default_subnetmask = "255.255.0.0";
  76.         ip_class = 'B';
  77.         return;
  78.     }
  79.     if (ip_a1 < 224)
  80.     {
  81.         default_subnetmask = "255.255.255.0";
  82.         ip_class = 'C';
  83.         return;
  84.     }
  85.     ip_class = 'D';
  86.     return;
  87. }
  88. void ipv_4::display_class_subnetmask()
  89. {
  90.     cout << "Class of IP address is: " << ip_class << endl;
  91.     cout << "Default subnet mask is: " << default_subnetmask << endl;
  92. }
  93. void ipv_4::read_subnet()
  94. {
  95.     cout << "Enter no of subnets: ";
  96.     cin >> sub_no;
  97. }
  98. int ipv_4::find_no_bits_borrowed()
  99. {
  100.     int i = 0;
  101.     if (sub_no == 1)
  102.     {
  103.         return 1;
  104.     }
  105.     while (sub_no > pow(2, i))
  106.     {
  107.         i++;
  108.     }
  109.     return i;
  110. }
  111. string itos(int temp)
  112. {
  113.     stringstream ss;
  114.     ss << temp << endl;
  115.     return ss.str();
  116. }
  117. void ipv_4::find_subnetmask()
  118. {
  119.     sub_bit = find_no_bits_borrowed();
  120.     switch (ip_class)
  121.     {
  122.     case 'A':
  123.         unmasked_bit = 32 - 8 - sub_bit;
  124.         total_host = pow(2, unmasked_bit);
  125.         require_subnetmask = "255.";
  126.         if (sub_bit > 0)
  127.         {
  128.             if (sub_bit >= 8)
  129.             {
  130.                 require_subnetmask = require_subnetmask + "255.";
  131.                 if (sub_bit >= 16)
  132.                 {
  133.                     require_subnetmask = require_subnetmask + "255.";
  134.                     int remaining_bit = sub_bit - 16;
  135.                     int x = 0;
  136.                     int j = 7;
  137.                     for (int i = remaining_bit; i > 0; i--)
  138.                     {
  139.                         x = x + pow(2, j);
  140.                         j--;
  141.                     }
  142.                     require_subnetmask = require_subnetmask + itos(x);
  143.                 }
  144.                 else
  145.                 {
  146.                     int remaining_bit = sub_bit - 8;
  147.                     int x = 0;
  148.                     int j = 7;
  149.                     for (int i = remaining_bit; i > 0; i--)
  150.                     {
  151.                         x = x + pow(2, j);
  152.                         j--;
  153.                     }
  154.                     require_subnetmask = require_subnetmask + itos(x) + ".0";
  155.                 }
  156.             }
  157.             else
  158.             {
  159.                 int x = 0;
  160.                 int j = 7;
  161.                 for (int i = sub_bit; i > 0; i--)
  162.                 {
  163.                     x = x + pow(2, j);
  164.                     j--;
  165.                 }
  166.                 require_subnetmask = require_subnetmask + itos(x) + ".0.0";
  167.             }
  168.         }
  169.         else
  170.         {
  171.             require_subnetmask = default_subnetmask;
  172.         }
  173.         break;
  174.     case 'B':
  175.         unmasked_bit = 32 - 16 - sub_bit;
  176.         total_host = pow(2, unmasked_bit);
  177.         require_subnetmask = "255.255.";
  178.         if (sub_bit > 0)
  179.         {
  180.             if (sub_bit >= 8)
  181.             {
  182.                 require_subnetmask = require_subnetmask + "255.";
  183.                 int remaining_bit = sub_bit - 8;
  184.                 int x = 0;
  185.                 int j = 7;
  186.                 for (int i = remaining_bit; i > 0; i--)
  187.                 {
  188.                     x = x + pow(2, j);
  189.                     j--;
  190.                 }
  191.                 require_subnetmask = require_subnetmask + itos(x);
  192.             }
  193.             else
  194.             {
  195.                 int x = 0;
  196.                 int j = 7;
  197.                 for (int i = sub_bit; i > 0; i--)
  198.                 {
  199.                     x = x + pow(2, j);
  200.                     j--;
  201.                 }
  202.                 require_subnetmask = require_subnetmask + itos(x) + ".0";
  203.             }
  204.         }
  205.         else
  206.         {
  207.             require_subnetmask = default_subnetmask;
  208.         }
  209.         break;
  210.     case 'C':
  211.         unmasked_bit = 32 - 24 - sub_bit;
  212.         total_host = pow(2, unmasked_bit);
  213.         require_subnetmask = "255.255.255.";
  214.         if (sub_bit > 0)
  215.         {
  216.             if (sub_bit == 8)
  217.             {
  218.                 require_subnetmask = require_subnetmask + "255";
  219.             }
  220.             else
  221.             {
  222.                 int x = 0;
  223.                 int j = 7;
  224.                 for (int i = sub_bit; i > 0; i--)
  225.                 {
  226.                     x = x + pow(2, j);
  227.                     j--;
  228.                 }
  229.                 require_subnetmask = require_subnetmask + itos(x);
  230.             }
  231.         }
  232.         else
  233.         {
  234.             require_subnetmask = default_subnetmask;
  235.         }
  236.         break;
  237.     }
  238. }
  239. void ipv_4::display_require_subnet()
  240. {
  241.     cout << "Number of subnet bit: " << sub_bit << endl;
  242.     cout << "Required subnet mask: " << require_subnetmask << endl;
  243.     cout << "Number of unmasked bit: " << unmasked_bit << endl;
  244.     cout << "Number of total host: " << total_host << endl;
  245. }
  246. void ipv_4::display_range()
  247. {
  248.     int total_subnet = pow(2, sub_bit);
  249.     int x1, x2, x3;
  250.     x1 = x2 = x3 = 0;
  251.     switch (ip_class)
  252.     {
  253.     case 'C':
  254.         for (int i = 0; i < total_subnet; i++)
  255.         {
  256.             cout << ip_a1 << "." << ip_a2 << "." << ip_a3 << "." << x1 << "------------->";
  257.             x1 = x1 + total_host;
  258.             cout << ip_a1 << "." << ip_a2 << "." << ip_a3 << "." << x1 - 1 << endl;
  259.         }
  260.         break;
  261.     case 'B':
  262.         for (int i = 0; i < total_subnet; i++)
  263.         {
  264.             if (i >= 1)
  265.             {
  266.                 if (x1 == 255)
  267.                 {
  268.                     x1 = 0;
  269.                     x2++;
  270.                 }
  271.                 else
  272.                 {
  273.                     x1++;
  274.                 }
  275.             }
  276.             cout << ip_a1 << "." << ip_a2 << "." << x2 << "." << x1 << "------------->";
  277.             x1 = x1 + (total_host % 256);
  278.             if (x1 > 255)
  279.             {
  280.                 x2 = x2 + (x1 / 256);
  281.             }
  282.             x2 = x2 + (total_host / 256);
  283.             if (x1 == 0)
  284.             {
  285.                 x1 = 255;
  286.                 x2 = x2 - 1;
  287.             }
  288.             else
  289.             {
  290.                 x1--;
  291.             }
  292.             cout << ip_a1 << "." << ip_a2 << "." << x2 << "." << x1 << "--------------------" << i << endl;
  293.         }
  294.         break;
  295.     case 'A':
  296.         for (int i = 0; i < total_subnet; i++)
  297.         {
  298.             if (i > 0)
  299.             {
  300.                 if (x1 == 255)
  301.                 {
  302.                     x1 = 0;
  303.                     if (x2 == 255)
  304.                     {
  305.                         x2 = 0;
  306.                         x3++;
  307.                     }
  308.                     else
  309.                     {
  310.                         x2++;
  311.                     }
  312.                 }
  313.                 else
  314.                 {
  315.                     x1++;
  316.                 }
  317.             }
  318.             cout << ip_a1 << "." << x3 << "." << x2 << "." << x1 << "------------->";
  319.             x1 = x1 + (total_host % 256);
  320.             if (x1 > 255)
  321.             {
  322.                 x2 = x2 + (x1 / 256);
  323.                 x1 = x1 - 255;
  324.             }
  325.             x2 = x2 + ((total_host / 256) % 256);
  326.             if (x2 > 255)
  327.             {
  328.                 x3 = x3 + (x2 / 256);
  329.                 x2 = x2 - 255;
  330.             }
  331.             x3 = x3 + ((total_host / 256) / 256);
  332.             if (x1 == 0)
  333.             {
  334.                 x1 = 255;
  335.                 if (x2 == 0)
  336.                 {
  337.                     x2 = 255;
  338.                     x3--;
  339.                 }
  340.                 else
  341.                 {
  342.                     x2--;
  343.                 }
  344.             }
  345.             else
  346.             {
  347.                 x1--;
  348.             }
  349.             cout << ip_a1 << "." << x3 << "." << x2 << "." << x1 << "--------------------" << i << endl;
  350.         }
  351.     }
  352. }
  353. int main()
  354. {
  355.     ipv_4 device1;
  356.     device1.display_class_subnetmask();
  357.     device1.read_subnet();
  358.     device1.find_subnetmask();
  359.     device1.display_require_subnet();
  360.     device1.display_range();
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement