-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ip.cpp
159 lines (132 loc) · 4.41 KB
/
Ip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <bits/stdc++.h>
using namespace std;
int main()
{
string ip_address;
cout << "Enter the IP Address" << endl;
cin >> ip_address;
vector<int> ip;
istringstream ss(ip_address);
string token;
while (getline(ss, token, '.'))
{
ip.push_back(stoi(token));
}
char classs = 'A';
if (ip[0] >= 0 && ip[0] < 128)
{
classs = 'A';
}
else if (ip[0] >= 128 && ip[0] < 192)
{
classs = 'B';
}
else if (ip[0] >= 192 && ip[0] < 223)
{
classs = 'C';
}
cout << "This IP address belongs to the class : " << classs << endl;
int subnets;
cout << "Enter the no of subnets" << endl;
cin >> subnets;
int adjSub = 0;
int numIP = 0;
int add = 0;
int addL = 0;
int addH = 0;
int in;
switch (classs)
{
case 'A':
cout << "Network mask is 255.0.0.0" << endl;
cout << "No. of IP Address available are 2^24" << endl;
for (int i = 0; i < 24; i++)
{
if (pow(2, i) >= subnets)
{
adjSub = pow(2, i);
in = i;
break;
}
}
numIP = pow(2, 24) / adjSub;
add = 256 / adjSub;
addL = 0;
addH = addL + add - 1;
cout << endl;
cout << "Total no of IP Address Possible in each subnet : " << numIP << endl;
cout << "Total no. of bits in Subnet id: " << in << endl;
for (int i = 1; i <= subnets; i++, addL = addL + add, addH = addH + add)
{
cout << "For Subnet " << i << endl;
cout << "Subnet Address: " << ip[0] << "." << addL << ".0.0" << endl;
cout << "Broadcast Address: " << ip[0] << "." << addH << ".255.255" << endl;
cout << "Valid Range: " << ip[0] << "." << addL << ".0.1"
<< " to " << ip[0] << "." << addH << ".255.254" << endl;
cout << endl;
}
break;
case 'B':
cout << "Network mask is 255.255.0.0" << endl;
cout << "No. of IP Address available are 2^16" << endl;
for (int i = 0; i < 16; i++)
{
if (pow(2, i) >= subnets)
{
adjSub = pow(2, i);
in = i;
break;
}
}
numIP = pow(2, 16) / adjSub;
add = 256 / adjSub;
addL = 0;
addH = addL + add - 1;
cout << endl;
cout << "Total no of IP Address Possible in each subnet : " << numIP << endl;
cout << "Total no. of bits in Subnet id: " << in << endl;
for (int i = 1; i <= subnets; i++, addL = addL + add, addH = addH + add)
{
cout << "For Subnet " << i << endl;
cout << "Subnet Address: " << ip[0] << "." << ip[1] << "." << addL << ".0" << endl;
cout << "Broadcast Address: " << ip[0] << "." << ip[1] << "." << addH << ".255" << endl;
cout << "Valid Range: " << ip[0] << "." << ip[1] << "." << addL << ".1"
<< " to " << ip[0] << "." << ip[1] << "." << addH << ".254" << endl;
cout << endl;
}
break;
case 'C':
cout << "Network mask is 255.255.0.0" << endl;
cout << "No. of IP Address available are 2^16" << endl;
for (int i = 0; i < 8; i++)
{
if (pow(2, i) >= subnets)
{
adjSub = pow(2, i);
in = i;
break;
}
}
numIP = pow(2, 8) / adjSub;
add = 256 / adjSub;
addL = 0;
addH = addL + add - 1;
cout << endl;
cout << "Total no of IP Address Possible in each subnet : " << numIP << endl;
cout << "Total no. of bits in Subnet id: " << in << endl;
for (int i = 1; i <= subnets; i++, addL = addL + add, addH = addH + add)
{
cout << "For Subnet " << i << endl;
cout << "Subnet Address: " << ip[0] << "." << ip[1] << "." << ip[2] << "." << addL << endl;
cout << "Broadcast Address: " << ip[0] << "." << ip[1] << "." << ip[2] << "." << addH << endl;
cout << "Valid Range: " << ip[0] << "." << ip[1] << "." << ip[2] << "." << addL + 1
<< " to " << ip[0] << "." << ip[1] << "." << ip[2] << "." << addH - 1 << endl;
cout << endl;
}
break;
default:
cout << "Belong to Class D or E or Invalid IP Address" << endl;
break;
}
return 0;
}