-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetaggregate.hpp
More file actions
379 lines (346 loc) · 10.2 KB
/
netaggregate.hpp
File metadata and controls
379 lines (346 loc) · 10.2 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#ifndef NETAGGREGATE_H
#define NETAGGREGATE_H
#include <algorithm>
#include <iostream>
#include <vector>
void vstrjoin(const std::vector<std::string> &v, char c, std::string &s) {
s.clear();
for (auto p = v.begin(); p != v.end(); ++p) {
s += *p;
if (p != v.end() - 1)
s += c;
}
}
std::string inttoipv6(unsigned __int128 a) {
unsigned int a1 = (a >> 112);
unsigned int a2 = (a << 16 >> 112);
unsigned int a3 = (a << 32 >> 112);
unsigned int a4 = (a << 48 >> 112);
unsigned int a5 = (a << 64 >> 112);
unsigned int a6 = (a << 80 >> 112);
unsigned int a7 = (a << 96 >> 112);
unsigned int a8 = (a << 112 >> 112);
char s[41];
sprintf(s, "%x:%x:%x:%x:%x:%x:%x:%x", a1, a2, a3, a4, a5, a6, a7, a8);
std::string str(s);
return str;
}
std::string inttoipv4(uint32_t a) {
unsigned int a1 = (a >> 24);
unsigned int a2 = (a << 8 >> 24);
unsigned int a3 = (a << 16 >> 24);
unsigned int a4 = (a << 24 >> 24);
char s[17];
sprintf(s, "%u.%u.%u.%u", a1, a2, a3, a4);
std::string str(s);
return str;
}
std::string compressipv6(std::string s) {
size_t pos;
std::string result;
std::vector<std::string> patterns{":0:0:0:0:0:0:0", ":0:0:0:0:0:0",
":0:0:0:0:0", ":0:0:0:0",
":0:0:0", ":0:0"};
for (auto i = patterns.begin(); i != patterns.end(); i++) {
// std::cout << "pattern is " << *i << std::endl;
pos = s.find(*i);
// std::cout << "pos is " << pos << std::endl;
if (pos != std::string::npos) {
// std::cout << "found " << *i << std::endl;
auto count = i->length();
if (pos != 0) {
result =
s.substr(0, pos) + ":" + s.substr(pos + count, std::string::npos);
} else {
result = ":" + s.substr(count, std::string::npos);
}
if (pos + count == s.length()) {
result += ":";
}
break;
}
}
if (result.length() == 0) {
result = s;
}
return result;
}
/*
std::ostream& operator<<(std::ostream& os, const unsigned __int128 i) noexcept
{
std::ostream::sentry s(os);
if (s) {
unsigned __int128 tmp = i < 0 ? -i : i;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (i < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (os.rdbuf()->sputn(d, len) != len) {
os.setstate(std::ios_base::badbit);
}
}
return os;
}
*/
class IPv6Net {
public:
IPv6Net(const std::string & = std::string("::"));
IPv6Net(unsigned __int128, int);
std::string net() const;
std::string mask() const;
std::string laddr();
unsigned __int128 intfaddr();
unsigned __int128 intladdr();
unsigned int intmask();
bool operator<(const IPv6Net &net) const;
private:
unsigned __int128 address;
unsigned __int128 first_address;
unsigned __int128 last_address;
unsigned int networklen;
std::string expanded_address_part;
};
bool IPv6Net::operator<(const IPv6Net &net) const {
if (first_address == net.first_address) {
return networklen < net.networklen;
}
return (first_address < net.first_address);
}
std::string IPv6Net::net() const {
return compressipv6(inttoipv6(first_address));
// return inttoipv6(first_address);
}
std::string IPv6Net::laddr() {
return compressipv6(inttoipv6(last_address));
// return inttoipv6(last_address);
}
std::string IPv6Net::mask() const {
return std::to_string(IPv6Net::networklen);
}
unsigned __int128 IPv6Net::intfaddr() { return first_address; }
unsigned __int128 IPv6Net::intladdr() { return last_address; }
unsigned int IPv6Net::intmask() { return networklen; }
IPv6Net::IPv6Net(unsigned __int128 a, int m) {
address = a;
networklen = m;
if (networklen == 128) {
first_address = address;
last_address = address;
} else {
first_address = address >> (128 - networklen) << (128 - networklen);
last_address = address | (((unsigned __int128)1 << (128 - networklen)) - 1);
}
}
IPv6Net::IPv6Net(const std::string &s) {
std::string address_part;
auto n = s.find("/");
if (n != std::string::npos) {
address_part = s.substr(0, n);
networklen = stoi(s.substr(n + 1));
} else {
address_part = std::string(s);
networklen = 128;
}
// expand ::
for (int i = 0, scc = 0; i < address_part.length();) {
if (address_part[i] != ':') {
expanded_address_part += address_part[i];
i++;
} else if (address_part[i] == ':' and address_part[i + 1] == ':') {
auto repeat = 6 - scc;
for (int j = 0; j < repeat; j++) {
expanded_address_part += ":0";
}
expanded_address_part += ":";
i += 2;
} else if (address_part[i] == ':' and address_part[i + 1] != ':') {
expanded_address_part += ":";
scc++;
i++;
}
}
if (expanded_address_part[expanded_address_part.length() - 1] == ':') {
expanded_address_part += "0";
}
if (expanded_address_part[0] == ':') {
expanded_address_part = "0" + expanded_address_part;
}
// expand leading zeroes and convert to __int128
std::size_t current, previous = 0;
std::vector<std::string> cont;
current = expanded_address_part.find(":");
while (current != std::string::npos) {
cont.push_back(expanded_address_part.substr(previous, current - previous));
previous = current + 1;
current = expanded_address_part.find(":", previous);
}
cont.push_back(expanded_address_part.substr(previous, current - previous));
unsigned __int128 num = 0;
unsigned int bitsnum = 112;
address = 0;
for (auto i = cont.begin(); i != cont.end(); ++i) {
if (i->length() < 4) {
auto repeat = 4 - i->length();
for (int j = 0; j < repeat; j++) {
*i = "0" + *i;
}
}
num = std::stoi(*i, 0, 16);
address = address + (num << bitsnum);
bitsnum -= 16;
}
vstrjoin(cont, ':', expanded_address_part);
if (networklen == 128) {
first_address = address;
last_address = address;
} else {
first_address = address >> (128 - networklen) << (128 - networklen);
last_address = address | (((unsigned __int128)1 << (128 - networklen)) - 1);
}
}
template <class MyNet> std::vector<MyNet> MergeNets(std::vector<MyNet> orig) {
std::vector<MyNet> mnets{};
bool to_merge = true;
while (to_merge) {
// std::cout << "merge iteration " << std::endl;
mnets.clear();
to_merge = false;
size_t rl = orig.size();
size_t r = 0;
for (auto i = orig.begin(); r < rl;) {
if ((i + 1) == orig.end()) {
// std::cout << "last element " << std::endl;
mnets.push_back(*i);
break;
}
if (i->intmask() == (i + 1)->intmask()) {
// std::cout << "merging " << i->net() << " and " << (i+1)->net() <<
// std::endl;
auto mnet = MyNet(i->intfaddr(), i->intmask() - 1);
if (mnet.intfaddr() == i->intfaddr() &&
mnet.intladdr() == (i + 1)->intladdr()) {
mnets.push_back(mnet);
to_merge = true;
i += 2;
r += 2;
} else {
mnets.push_back(*i);
i++;
r++;
}
} else {
// std::cout << "not merging" << std::endl;
auto mnet = MyNet(i->intfaddr(), i->intmask());
mnets.push_back(mnet);
i++;
r++;
}
}
orig = mnets;
}
return mnets;
}
template <class MyNet> std::vector<MyNet> CollapsNets(std::vector<MyNet> orig) {
auto w = orig.begin();
auto r = orig.begin() + 1;
for (; w < orig.end() && r < orig.end();) {
// std::cout << "working on " << i->net() << "/" << i->mask() << " and " <<
// (i+1)->net() << "/" << (i+1)->mask() << std::endl;
if (w->intfaddr() <= r->intfaddr() && w->intladdr() >= r->intladdr()) {
r++;
} else {
w++;
*w = MyNet(r->intfaddr(), r->intmask());
r++;
}
}
if (w + 1 < orig.end()) {
orig.erase(w + 1, orig.end());
}
return orig;
}
class IPv4Net {
public:
IPv4Net(const std::string & = std::string("0.0.0.0"));
IPv4Net(uint32_t, unsigned int);
std::string net() const;
std::string mask() const;
std::string laddr();
uint32_t intfaddr();
uint32_t intladdr();
unsigned int intmask();
bool operator<(const IPv4Net &net) const;
private:
uint32_t address;
uint32_t first_address;
uint32_t last_address;
unsigned int networklen;
};
IPv4Net::IPv4Net(const std::string &s) {
std::string address_part;
auto n = s.find("/");
if (n != std::string::npos) {
address_part = s.substr(0, n);
networklen = std::stoi(s.substr(n + 1));
} else {
address_part = std::string(s);
networklen = 32;
}
std::size_t current, previous = 0;
std::vector<std::string> cont;
current = address_part.find(".");
while (current != std::string::npos) {
cont.push_back(address_part.substr(previous, current - previous));
previous = current + 1;
current = address_part.find(".", previous);
}
cont.push_back(address_part.substr(previous, current - previous));
uint32_t num = 0;
unsigned int bitsnum = 24;
address = 0;
for (auto i = cont.begin(); i != cont.end(); ++i) {
num = std::stoi(*i);
address = address + (num << bitsnum);
bitsnum -= 8;
}
if (networklen == 32) {
first_address = address;
last_address = address;
} else {
first_address = address >> (32 - networklen) << (32 - networklen);
last_address = address | (((uint32_t)1 << (32 - networklen)) - 1);
}
}
IPv4Net::IPv4Net(uint32_t a, unsigned int m) {
address = a;
networklen = m;
if (networklen == 32) {
first_address = address;
last_address = address;
} else {
first_address = address >> (32 - networklen) << (32 - networklen);
last_address = address | (((uint32_t)1 << (32 - networklen)) - 1);
}
}
std::string IPv4Net::net() const { return inttoipv4(first_address); }
std::string IPv4Net::mask() const {
return std::to_string(IPv4Net::networklen);
}
bool IPv4Net::operator<(const IPv4Net &net) const {
if (first_address == net.first_address) {
return networklen < net.networklen;
}
return (first_address < net.first_address);
}
uint32_t IPv4Net::intfaddr() { return first_address; }
uint32_t IPv4Net::intladdr() { return last_address; }
unsigned int IPv4Net::intmask() { return networklen; }
#endif // NETAGGREGATE_H