-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpCalculator.cpp
207 lines (197 loc) · 6.77 KB
/
IpCalculator.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <cmath>
#include <regex>
#include <string.h>
using namespace std;
string binary(unsigned long n) //int to binary converter
{
char result[(sizeof(unsigned long) * 8) + 1];
unsigned index = sizeof(unsigned long) * 8;
result[index] = '\0';
do
result[--index] = '0' + (n & 1);
while (n >>= 1);
return string(result + index);
}
//----------------------------------------------------------------------------------------------------
string ipToBinary(string ip[]) //ip to binary without formatting
{
string output = "";
for (int i = 0; i < 4; i++)
{
string out = binary(stoi(ip[i]));
int aux = 8 - out.size();
string out2 = "";
for (int j = 0; j < aux; j++)
{
out2 += '0';
}
output += out2 + out;
}
return output;
}
//----------------------------------------------------------------------------------------------------
string BinaryToIp(string entrada, int alterer) //binary string to ip string with formatting
{
string output = "";
for (size_t i = 0; i < 4; i++)
{
if (i == 3)
output += to_string(stoi(entrada.substr(8 * i, 8), nullptr, 2) + (alterer));
else
output += to_string(stoi(entrada.substr(8 * i, 8), nullptr, 2)) + ".";
}
return output;
}
//----------------------------------------------------------------------------------------------------
string networkAddress(string ip[], int CIDR) //calculates a network address for a given ip address
{
string ipArray = ipToBinary(ip);
for (int i = CIDR; i < 32; i++)
{
ipArray[i] = '0';
}
return ipArray;
}
//----------------------------------------------------------------------------------------------------
string broadcastAddress(string ip[], int CIDR) //calculates a broadcast address for a given ip address
{
string ipArray = ipToBinary(ip);
for (int i = CIDR; i < 32; i++)
{
ipArray[i] = '1';
}
return ipArray;
}
//----------------------------------------------------------------------------------------------------
string CIDRtoDecimal(int CIDR) //CIDR to decimal mask converter
{
string output = "11111111111111111111111111111111";
for (int i = CIDR; i < 32; i++)
{
output[i] = '0';
}
return output;
}
//----------------------------------------------------------------------------------------------------
int DecimalToCIDR(string mask[]) //Decimal mask to CIDR Converter
{
int CIDR = 0;
string aux = ipToBinary(mask);
for (int j = 0; j < 32; j++)
{
if (aux[j] == '1')
CIDR++;
}
return CIDR;
}
//----------------------------------------------------------------------------------------------------
int CIDRforIPWithClass(string firstOctet)//calculates a CIDR for a ip with class
{
string input = binary(stoi(firstOctet, nullptr, 10));
int aux = 8 - input.size();
string output = "";
if (aux)
{ for (int j = 0; j < aux; j++)
output += '0';
}
output += input;
int j = 0;
for (j; j < 8; j++)
if(output[j]=='0')
break;
switch (j)
{
case 0:
cout << "Class A \n";
return 8;
case 1:
cout << "Class B \n";
return 16;
case 2:
cout << "Class C \n";
return 24;
case 3:
cout << "Class D \n";
return -1;
default:
cout << "Class E \n";
return -1;
}
}
//-----------------------------------------------------------------------------------------------------
void callInfo(string ip[], int CIDR) //Executes the flow of tasks
{
cout << "Number of hosts available: " << pow(2, (32 - CIDR)) - 2 << "\n";
cout << "Network address: " << BinaryToIp(networkAddress(ip, CIDR), 0) << "\n";
cout << "Broadcast address: " << BinaryToIp(broadcastAddress(ip, CIDR), 0) << "\n";
cout << "First usable address: " << BinaryToIp(networkAddress(ip, CIDR), 1) << "\n";
cout << "Last usable address: " << BinaryToIp(broadcastAddress(ip, CIDR), (-1)) << "\n";
}
//-----------------------------------------------------------------------------------------------------
void ipWithMaskDecimal(string ip[], string mask[])
{
int CIDR = DecimalToCIDR(mask);
cout << "CIDR MASK: " << CIDR << "\n";
callInfo(ip, CIDR);
}
//------------------------------------------------------------------------------------------------------
void ipWithClass(string ip[])
{
int CIDR = CIDRforIPWithClass(ip[0]);
if(CIDR==(-1))
cout<<"Not applicable \n";
else
callInfo(ip, CIDR);
}
//------------------------------------------------------------------------------------------------------
void ipWithMaskCIDR(string ip[], string CIDRstring)
{
int CIDR = stoi(CIDRstring, nullptr, 10);
cout << "Decimal Mask: " << BinaryToIp(CIDRtoDecimal(CIDR), 0) << "\n";
callInfo(ip, CIDR);
}
//------------------------------------------------------------------------------------------------------
int main()
{
string input = "191.10.20.30";
cout<< "Please enter an input as given in one of the examples: 126.10.10.10 OR 152.168.1.75/28 OR 152.168.1.75/255.255.255.255 \nType here:";
cin>>input;
cout<<"\n";
regex ipRxWithMaskDecimal("((\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}))\\/((\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}))");
regex ipRxWithClass("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3}$)");
regex ipRxWithMaskCIDR("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\/(\\d{1,2})");
smatch matches;
//end main declarations
if (regex_match(input, ipRxWithMaskDecimal))
{
regex_search(input, matches, ipRxWithMaskDecimal);
string mask[4] = {matches[7].str(), matches[8].str(), matches[9].str(), matches[10].str()};
string ip[4] = {matches[2].str(), matches[3].str(), matches[4].str(), matches[5].str()};
ipWithMaskDecimal(ip, mask);
}
else
{
if (regex_match(input, ipRxWithClass))
{
regex_search(input, matches, ipRxWithClass);
string ip[4] = {matches[1].str(), matches[2].str(), matches[3].str(), matches[4].str()};
ipWithClass(ip);
}
else
{
if (regex_match(input, ipRxWithMaskCIDR))
{
regex_search(input, matches, ipRxWithMaskCIDR);
string mask = matches[5].str();
string ip[4] = {matches[1].str(), matches[2].str(), matches[3].str(), matches[4].str()};
ipWithMaskCIDR(ip, mask);
}
else
{
cout << "Invalid Expression, please give the input as given on the example: \n 152.168.1.75/28 or 152.168.1.75 or 152.168.1.75/255.255.255.255";
}
}
}
return main();
}