-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
167 lines (154 loc) · 4.26 KB
/
main.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
160
161
162
163
164
165
166
167
#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
bool isPowerofTwo(int n)
{
if (n <= 0)
return 0;
if ((n & (~(n - 1))) == n)
return 1;
return 0;
}
int getValidIP(int* IP, char* chk, int& prf){
cout << "Enter a Network IP form like 192.168.20.0/24\n";
for(int i = 0 ; i < 4; i++){
cin >> IP[i]; cin.clear(); cin >> chk[i];
}
cin >> prf;
if(prf > 32 || prf <= 0 || chk[3] != '/'){
return 0;
}
for(int i = 0 ;i < 4; i++){
if(IP[i] > 255 || IP[i] < 0){
return 0;
}
if(i < 3 && chk[i] != '.'){
return 0;
}
}
if(prf == 32){
cout << "the network has only a single IPv4 address.\n";
return 2;
}
return 1;
}
void PrintIP(vector<vector<int>>& v){
for(int j = 0; j < v.size(); j++){
for(int k = 0; k < 4; k++){
cout << v[j][k];
if(k < 3) cout << '.';
}
cout << '/' << v[j][4] << '\n';
}
cout << '\n';
}
vector<vector<int>> TraditionalAddr(int* IP, int& prf, int& subnets, int& subnetPortion){
vector<vector<int>> ret(subnets, vector<int>(5));
for(auto& i : ret){
for(int j = 0; j < 4; j++){
i[j] = IP[j];
}
}
int it = 1;
int stidx = (prf+subnetPortion)/8;
int ed = prf % 8;
int st = (prf+subnetPortion)%8;
int comp = (subnetPortion - (st + ed))/8;
int n = 2 + comp;
ret[0][4] = prf + subnetPortion;
for(int i = 1; i < (1<<subnetPortion); i++){
for(int j = 0; j < n; j++){
for(int k = 0; k < st; k++){
if((1<<k)&i){
ret[it][stidx] |= (1<<(8-st+k));
}
}
for(int k = 0; k < ed; k++){
if(((1<<(k+st+8*comp))&i)){
ret[it][stidx-n+1] |= (1<<k);
}
}
for(int k = 0; k < 8; k++){
if(((1<<(k+st+(j-1)*8))&i)){
ret[it][stidx-j] |= (1<<k);
}
}
}
ret[it][4] = prf + subnetPortion;
it++;
}
return ret;
}
void VLSM(int* IP, int* h, int* frq, int& n, vector<vector<vector<int>>>& v, int prf){
for(int i = 32; i >= 0; i--){
int p = frq[i];
if(!p) continue;
int en = p;
int mx = 32-prf;
int step = max(mx - en, 1);
int subnets = (1<<step);
int subnetPortion = step;
vector<vector<int>> ret = TraditionalAddr(IP, prf, subnets, subnetPortion);
prf += subnetPortion;
for(int j = 0; j < 4; j++){
IP[j] = ret[1][j];
}
v[p] = ret;
}
}
int main(){
int IP[4];
char chk[4];
int prf = 0;
while(true){
int ret = getValidIP(IP, chk, prf);
if(ret == 1) break;
else if(ret == 2) return 0;
else cout << "Not valid IP!" << endl;
}
int op = 0;
while(op != 1 && op != 2){
cout << "1. Traditional subnetting\n2. VLSM\nChoose (1 or 2)\n";
cin >> op;
}
if(op == 1){
int mxsubnets = 1<<(32-prf);
int subnets = 0;
cout << "Enter number of Subnets\n";
cin >> subnets;
while(mxsubnets < subnets || !isPowerofTwo(subnets)){
cout << "Number must be power of 2 and less or equal than " << mxsubnets << "take:" << '\n';
cin >> subnets;
}
int subnetPortion = log2(subnets);
vector<vector<int>> res = TraditionalAddr(IP, prf, subnets, subnetPortion);
PrintIP(res);
}
else{
int n;
cout << "Enter number of networks\n";
cin >> n;
cout << "Enter number of Hosts in each network\n";
int h[n];
int frq[35] = {0};
for(int i = 0 ;i < n; i++){
cin >> h[i];
int temp = log2(h[i])+1;
frq[temp] = temp;
}
sort(h, h+n);
reverse(h, h+n);
vector<vector<vector<int>>> v(35);
VLSM(IP, h, frq, n, v, prf);
for(int i = 32 ;i >= 0; i--){
int p = frq[i];
if(!p) continue;
cout << "For hosts from " << (1<<(p-1))-2 << " to " << (1<<p)-2 << " take:\n";
PrintIP(v[p]);
}
}
}