-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIpv4.h
342 lines (277 loc) Β· 10.5 KB
/
Ipv4.h
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @file Ipv4.h
* @brief Ipv4 is a lib, which has object with Ipv4 address and functions to interact with it.
*
* Copyright (c) 2020-forever Vlad Rogozin ([email protected])
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#ifndef RED_IPV4_H
#define RED_IPV4_H
#include <boost/lexical_cast.hpp>
#include <string>
#define REDIPV4_VERSION "1.0"
#define REDIPV4_ALL "all"
#define REDIPV4_PUBLIC "public"
#define REDIPV4_192 "192"
#define REDIPV4_172 "172"
#define REDIPV4_169 "169"
#define REDIPV4_127 "127"
#define REDIPV4_10 "10"
namespace Red {
class Ipv4 {
public:
//
// Ctors.
//
/// Base ctor.
Ipv4() {}
/**
* @brief Ipv4
*
* @param Mode Mode for incrementation.
* @param IpString Ip to assign.
*/
Ipv4(const std::string_view Mode, const std::string_view IpString) {
this->Mode = Mode;
this->IpString = IpString;
this->HoldedAddress = GetHoldedAddress();
}
//
// Base funcs.
//
/**
* @brief Set
*
* @param Mode Mode Mode for incrementation.
* @param IpString IpString Ip to assign.
*/
inline void Set(const std::string_view Mode, const std::string_view IpString) {
this->Mode = Mode;
this->IpString = IpString;
this->HoldedAddress = GetHoldedAddress();
}
/**
* @brief Gen
*
* This function generates ip address using Mode.
*/
inline void Gen() {
IpString.clear();
IpString += std::to_string(1 + rand() % 255);
IpString += ".";
IpString += std::to_string(0 + rand() % 255);
IpString += ".";
IpString += std::to_string(0 + rand() % 255);
IpString += ".";
IpString += std::to_string(0 + rand() % 255);
}
/**
* @brief Next
*
* Increments IpString.
*/
inline void Next() {
std::string _1Part, _2Part, _3Part, _4Part;
unsigned short int Part1, Part2, Part3, Part4;
unsigned short int PointsInt = 0;
for (size_t CountPos = 0; CountPos < IpString.length(); CountPos++) {
if (IpString[CountPos] == '.') {
PointsInt++;
} else {
if (PointsInt < 1) {
_1Part += IpString[CountPos];
} else if (PointsInt < 2) {
_2Part += IpString[CountPos];
} else if (PointsInt < 3) {
_3Part += IpString[CountPos];
} else {
_4Part += IpString[CountPos];
}
}
}
Part1 = boost::lexical_cast<unsigned short int>(_1Part);
Part2 = boost::lexical_cast<unsigned short int>(_2Part);
Part3 = boost::lexical_cast<unsigned short int>(_3Part);
Part4 = boost::lexical_cast<unsigned short int>(_4Part);
if (Part4 == 255) {
Part4 = 0;
if (Part3 == 255) {
Part3 = 0;
if (Part2 == 255) {
Part2 = 0;
if (Part1 == 255) {
Part1 = 0;
} else {
Part1++;
}
} else {
Part2++;
}
} else {
Part3++;
}
} else {
Part4++;
}
// Type of mode.
if (Mode != "all") {
if (Mode == "public") { // Public addresses only.
// Check for private addresses.
if (Part1 == 192) {
Part1++;
} else if (Part1 == 172) {
Part1++;
} else if (Part1 == 169) {
Part1++;
} else if (Part1 == 127) {
Part1++;
} else if (Part1 == 10) {
Part1++;
}
} else {
Hold = true;
HoldedAddress = boost::lexical_cast<unsigned short int>(Mode);
}
}
// Check is address holded.
if (Hold == true && Part1 != HoldedAddress) {
Part1 = HoldedAddress;
}
// Compiling to a string.
IpString = std::to_string(Part1);
IpString += ".";
IpString += std::to_string(Part2);
IpString += ".";
IpString += std::to_string(Part3);
IpString += ".";
IpString += std::to_string(Part4);
}
/**
* @brief Previous
*
* Decrements IpString.
*/
inline void Previous() {
std::string _1Part, _2Part, _3Part, _4Part;
unsigned short int Part1, Part2, Part3, Part4;
int PointsInt = 0;
for (size_t CountPos = 0; CountPos < IpString.length(); CountPos++) {
if (IpString[CountPos] == '.') {
PointsInt++;
} else {
if (PointsInt < 1) {
_1Part += IpString[CountPos];
} else if (PointsInt < 2) {
_2Part += IpString[CountPos];
} else if (PointsInt < 3) {
_3Part += IpString[CountPos];
} else {
_4Part += IpString[CountPos];
}
}
}
Part1 = boost::lexical_cast<unsigned short int>(_1Part);
Part2 = boost::lexical_cast<unsigned short int>(_2Part);
Part3 = boost::lexical_cast<unsigned short int>(_3Part);
Part4 = boost::lexical_cast<unsigned short int>(_4Part);
if (Part4 == 0) {
Part4 = 255;
if (Part3 == 0) {
Part3 = 255;
if (Part2 == 0) {
Part2 = 255;
if (Part1 == 0) {
Part1 = 255;
} else {
Part1--;
}
} else {
Part2--;
}
} else {
Part3--;
}
} else {
Part4--;
}
// Type of mode.
if (Mode != "all") {
if (Mode == "public") { // Public addresses only.
// Check for private addresses.
if (Part1 == 192) {
Part1++;
} else if (Part1 == 172) {
Part1++;
} else if (Part1 == 169) {
Part1++;
} else if (Part1 == 127) {
Part1++;
} else if (Part1 == 10) {
Part1++;
}
} else {
Hold = true;
if (Mode == "192") { // only 192.XXX.XXX.XXX.
HoldedAddress = 192;
} else if (Mode == "172") { // only 172.XXX.XXX.XXX.
HoldedAddress = 172;
} else if (Mode == "169") { // only 169.XXX.XXX.XXX.
HoldedAddress = 169;
} else if (Mode == "127") { // only 127.XXX.XXX.XXX.
HoldedAddress = 127;
} else if (Mode == "10") { // only 10.XXX.XXX.XXX.
HoldedAddress = 10;
} else { // only ???.XXX.XXX.XXX
HoldedAddress = boost::lexical_cast<unsigned short int>(Mode);
}
}
}
// Check is address holded.
if (Hold == true && Part1 != HoldedAddress) {
Part1 = HoldedAddress;
}
// Compiling to a string.
IpString = std::to_string(Part1);
IpString += ".";
IpString += std::to_string(Part2);
IpString += ".";
IpString += std::to_string(Part3);
IpString += ".";
IpString += std::to_string(Part4);
}
/**
* @brief Show
*
* @return IpString.
*/
std::string& Show() {
return IpString;
}
private:
//
// Private funcs.
//
/**
* @brief GetHoldedAddress
*
* @return Holded address.
*/
inline unsigned short int GetHoldedAddress() {
std::string Part1 = "";
for (size_t y = 0; IpString[y] != '.'; y++) {
Part1 += IpString[y];
}
return boost::lexical_cast<unsigned short int>(Part1);
}
//
// Private variables.
//
std::string Mode;
std::string IpString;
bool Hold;
unsigned short int HoldedAddress;
};
}
#endif // RED_IPV4_H