Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<math.h>
- #include<sstream>
- using namespace std;
- class ipv_4
- {
- private:
- int ip_a1, ip_a2, ip_a3, ip_a4;
- char ip_class;
- int sub_no;
- int sub_bit;
- string default_subnetmask;
- string require_subnetmask;
- int unmasked_bit;
- int total_host;
- public:
- ipv_4()
- {
- string ip_address;
- cout << "Enter the IP address: ";
- cin >> ip_address;
- int i = 0;
- string j = "";
- while (ip_address[i] != '.')
- {
- j = j + ip_address[i];
- i++;
- }
- ip_a1 = stoi(j);
- i++;
- j = "";
- while (ip_address[i] != '.')
- {
- j = j + ip_address[i];
- i++;
- }
- ip_a2 = stoi(j);
- i++;
- j = "";
- while (ip_address[i] != '.')
- {
- j = j + ip_address[i];
- i++;
- }
- ip_a3 = stoi(j);
- i++;
- j = "";
- while (ip_address[i] != '\0')
- {
- j = j + ip_address[i];
- i++;
- }
- ip_a4 = stoi(j);
- determine_class_subnetmask();
- }
- void determine_class_subnetmask();
- void display_class_subnetmask();
- void read_subnet();
- int find_no_bits_borrowed();
- void find_subnetmask();
- void display_require_subnet();
- void display_range();
- };
- void ipv_4::determine_class_subnetmask()
- {
- if (ip_a1 < 128)
- {
- default_subnetmask = "255.0.0.0";
- ip_class = 'A';
- return;
- }
- if (ip_a1 < 192)
- {
- default_subnetmask = "255.255.0.0";
- ip_class = 'B';
- return;
- }
- if (ip_a1 < 224)
- {
- default_subnetmask = "255.255.255.0";
- ip_class = 'C';
- return;
- }
- ip_class = 'D';
- return;
- }
- void ipv_4::display_class_subnetmask()
- {
- cout << "Class of IP address is: " << ip_class << endl;
- cout << "Default subnet mask is: " << default_subnetmask << endl;
- }
- void ipv_4::read_subnet()
- {
- cout << "Enter no of subnets: ";
- cin >> sub_no;
- }
- int ipv_4::find_no_bits_borrowed()
- {
- int i = 0;
- if (sub_no == 1)
- {
- return 1;
- }
- while (sub_no > pow(2, i))
- {
- i++;
- }
- return i;
- }
- string itos(int temp)
- {
- stringstream ss;
- ss << temp << endl;
- return ss.str();
- }
- void ipv_4::find_subnetmask()
- {
- sub_bit = find_no_bits_borrowed();
- switch (ip_class)
- {
- case 'A':
- unmasked_bit = 32 - 8 - sub_bit;
- total_host = pow(2, unmasked_bit);
- require_subnetmask = "255.";
- if (sub_bit > 0)
- {
- if (sub_bit >= 8)
- {
- require_subnetmask = require_subnetmask + "255.";
- if (sub_bit >= 16)
- {
- require_subnetmask = require_subnetmask + "255.";
- int remaining_bit = sub_bit - 16;
- int x = 0;
- int j = 7;
- for (int i = remaining_bit; i > 0; i--)
- {
- x = x + pow(2, j);
- j--;
- }
- require_subnetmask = require_subnetmask + itos(x);
- }
- else
- {
- int remaining_bit = sub_bit - 8;
- int x = 0;
- int j = 7;
- for (int i = remaining_bit; i > 0; i--)
- {
- x = x + pow(2, j);
- j--;
- }
- require_subnetmask = require_subnetmask + itos(x) + ".0";
- }
- }
- else
- {
- int x = 0;
- int j = 7;
- for (int i = sub_bit; i > 0; i--)
- {
- x = x + pow(2, j);
- j--;
- }
- require_subnetmask = require_subnetmask + itos(x) + ".0.0";
- }
- }
- else
- {
- require_subnetmask = default_subnetmask;
- }
- break;
- case 'B':
- unmasked_bit = 32 - 16 - sub_bit;
- total_host = pow(2, unmasked_bit);
- require_subnetmask = "255.255.";
- if (sub_bit > 0)
- {
- if (sub_bit >= 8)
- {
- require_subnetmask = require_subnetmask + "255.";
- int remaining_bit = sub_bit - 8;
- int x = 0;
- int j = 7;
- for (int i = remaining_bit; i > 0; i--)
- {
- x = x + pow(2, j);
- j--;
- }
- require_subnetmask = require_subnetmask + itos(x);
- }
- else
- {
- int x = 0;
- int j = 7;
- for (int i = sub_bit; i > 0; i--)
- {
- x = x + pow(2, j);
- j--;
- }
- require_subnetmask = require_subnetmask + itos(x) + ".0";
- }
- }
- else
- {
- require_subnetmask = default_subnetmask;
- }
- break;
- case 'C':
- unmasked_bit = 32 - 24 - sub_bit;
- total_host = pow(2, unmasked_bit);
- require_subnetmask = "255.255.255.";
- if (sub_bit > 0)
- {
- if (sub_bit == 8)
- {
- require_subnetmask = require_subnetmask + "255";
- }
- else
- {
- int x = 0;
- int j = 7;
- for (int i = sub_bit; i > 0; i--)
- {
- x = x + pow(2, j);
- j--;
- }
- require_subnetmask = require_subnetmask + itos(x);
- }
- }
- else
- {
- require_subnetmask = default_subnetmask;
- }
- break;
- }
- }
- void ipv_4::display_require_subnet()
- {
- cout << "Number of subnet bit: " << sub_bit << endl;
- cout << "Required subnet mask: " << require_subnetmask << endl;
- cout << "Number of unmasked bit: " << unmasked_bit << endl;
- cout << "Number of total host: " << total_host << endl;
- }
- void ipv_4::display_range()
- {
- int total_subnet = pow(2, sub_bit);
- int x1, x2, x3;
- x1 = x2 = x3 = 0;
- switch (ip_class)
- {
- case 'C':
- for (int i = 0; i < total_subnet; i++)
- {
- cout << ip_a1 << "." << ip_a2 << "." << ip_a3 << "." << x1 << "------------->";
- x1 = x1 + total_host;
- cout << ip_a1 << "." << ip_a2 << "." << ip_a3 << "." << x1 - 1 << endl;
- }
- break;
- case 'B':
- for (int i = 0; i < total_subnet; i++)
- {
- if (i >= 1)
- {
- if (x1 == 255)
- {
- x1 = 0;
- x2++;
- }
- else
- {
- x1++;
- }
- }
- cout << ip_a1 << "." << ip_a2 << "." << x2 << "." << x1 << "------------->";
- x1 = x1 + (total_host % 256);
- if (x1 > 255)
- {
- x2 = x2 + (x1 / 256);
- }
- x2 = x2 + (total_host / 256);
- if (x1 == 0)
- {
- x1 = 255;
- x2 = x2 - 1;
- }
- else
- {
- x1--;
- }
- cout << ip_a1 << "." << ip_a2 << "." << x2 << "." << x1 << "--------------------" << i << endl;
- }
- break;
- case 'A':
- for (int i = 0; i < total_subnet; i++)
- {
- if (i > 0)
- {
- if (x1 == 255)
- {
- x1 = 0;
- if (x2 == 255)
- {
- x2 = 0;
- x3++;
- }
- else
- {
- x2++;
- }
- }
- else
- {
- x1++;
- }
- }
- cout << ip_a1 << "." << x3 << "." << x2 << "." << x1 << "------------->";
- x1 = x1 + (total_host % 256);
- if (x1 > 255)
- {
- x2 = x2 + (x1 / 256);
- x1 = x1 - 255;
- }
- x2 = x2 + ((total_host / 256) % 256);
- if (x2 > 255)
- {
- x3 = x3 + (x2 / 256);
- x2 = x2 - 255;
- }
- x3 = x3 + ((total_host / 256) / 256);
- if (x1 == 0)
- {
- x1 = 255;
- if (x2 == 0)
- {
- x2 = 255;
- x3--;
- }
- else
- {
- x2--;
- }
- }
- else
- {
- x1--;
- }
- cout << ip_a1 << "." << x3 << "." << x2 << "." << x1 << "--------------------" << i << endl;
- }
- }
- }
- int main()
- {
- ipv_4 device1;
- device1.display_class_subnetmask();
- device1.read_subnet();
- device1.find_subnetmask();
- device1.display_require_subnet();
- device1.display_range();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement