-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.cpp
385 lines (369 loc) · 11.8 KB
/
flow.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
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
380
381
382
383
384
385
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <unordered_map>
#include <map>
#include <iostream>
#include <string>
#define _XOPEN_SOURCE 700
// flow structure
struct flow {
uint32_t sa_family;
struct in6_addr src_addr;
struct in6_addr dst_addr;
uint16_t src_port;
uint16_t dst_port;
uint64_t packets;
uint64_t bytes;
};
// applies given mask to a ipv4 address
void apply_ipv4_mask(struct in6_addr *addr, uint16_t mask, char *ipstr) {
uint8_t limit = 255;
if (mask != 32) {
for (int i = 12; i < 16; i++) {
if (mask < 8) {
addr->s6_addr[i] = addr->s6_addr[i] & (limit << (8-mask));
mask = 0;
}
else {
mask = mask - 8;
}
}
}
inet_ntop(AF_INET, &addr->s6_addr[12], ipstr, INET6_ADDRSTRLEN);
return;
}
// applies given mask to a ipv6 address
void apply_ipv6_mask(struct in6_addr *addr, uint16_t mask, char *ipstr) {
uint8_t limit = 255;
if (mask != 128) {
for (int i = 0; i < 16; i++) {
if (mask < 8) {
addr->s6_addr[i] = addr->s6_addr[i] & (limit << (8-mask));
mask = 0;
}
else {
mask = mask - 8;
}
}
}
inet_ntop(AF_INET6, addr, ipstr, INET6_ADDRSTRLEN);
return;
}
// function used to read data from the given file and agregate them using std::unordered_map
void load_and_agreg(std::unordered_map<std::string, struct flow> *map, std::string dirname, DIR *dir, uint8_t ipv4mask, uint8_t ipv6mask, int agrflg) {
dirent *dp = NULL;
while ((dp = readdir(dir)) != NULL) {
std::string filename = dp->d_name;
if (filename == "." || filename == "..")
// not useful
continue;
filename = dirname + "/" + dp->d_name;
if (dp->d_type == DT_DIR) {
//recursion
DIR *newdir = opendir(filename.c_str());
load_and_agreg(map, filename, newdir, ipv4mask, ipv6mask, agrflg);
continue;
}
std::cout << filename << std::endl;
FILE *fp = fopen(filename.c_str(), "rb");
char ip[INET6_ADDRSTRLEN];
struct flow fl;
size_t n = 0;
while ((n = fread(&fl, sizeof(struct flow), 1, fp)) != 0) {
std::string index;
// check which agreg flag for the field by which to agregate
switch (agrflg) {
case 1:
index = std::to_string(ntohs(fl.src_port));
break;
case 2:
index = std::to_string(ntohs(fl.dst_port));
break;
// agregation using ip field, needs to apply a mask
case 3:
if (ntohl(fl.sa_family) == AF_INET6) {
apply_ipv6_mask(&fl.src_addr, ipv6mask, ip);
index = ip;
}
else
continue;
break;
case 4:
if (ntohl(fl.sa_family) == AF_INET6) {
apply_ipv6_mask(&fl.dst_addr, ipv6mask, ip);
index = ip;
}
else
continue;
break;
case 5:
if (ntohl(fl.sa_family) == AF_INET) {
apply_ipv4_mask(&fl.src_addr, ipv4mask, ip);
index = ip;
}
else
continue;
break;
case 6:
if (ntohl(fl.sa_family) == AF_INET) {
apply_ipv4_mask(&fl.dst_addr, ipv4mask, ip);
index = ip;
}
else
continue;
break;
case 7:
if (ntohl(fl.sa_family) == AF_INET6) {
apply_ipv6_mask(&fl.src_addr, ipv6mask, ip);
index = ip;
}
else {
apply_ipv4_mask(&fl.src_addr, ipv4mask, ip);
index = ip;
}
break;
case 8:
if (ntohl(fl.sa_family) == AF_INET6) {
apply_ipv6_mask(&fl.dst_addr, ipv6mask, ip);
index = ip;
}
else {
apply_ipv4_mask(&fl.dst_addr, ipv4mask, ip);
index = ip;
}
break;
default:
fprintf(stderr, "Error\n");
return;
}
std::unordered_map<std::string, struct flow>::iterator iterator = map->find(index);
fl.src_port = ntohs(fl.src_port);
fl.dst_port = ntohs(fl.dst_port);
fl.packets = __builtin_bswap64(fl.packets);
fl.bytes = __builtin_bswap64(fl.bytes);
if (iterator == map->end()) {
map->operator[](index) = fl;
}
else {
fl.packets += iterator->second.packets;
fl.bytes += iterator->second.bytes;
map->operator[](index) = fl;
}
}
fclose(fp);
}
}
int main(int argc, char *argv[])
{
int opt;
int agrflg = 0; // agregation flag
std::string agrname;
uint8_t ipv4mask = 32;
uint8_t ipv6mask = 128;
char *filename;
char ip[INET6_ADDRSTRLEN];
std::string agreg;
std::string sort;
if (argc < 7) {
fprintf(stderr, "Usage: flow -f directory -a aggregation -s sort\n");
return (EXIT_FAILURE);
}
// cmdline arguments
while ((opt = getopt(argc, argv, "f:a:s:")) != -1) {
switch (opt) {
case 'f':
filename = optarg;
break;
case 'a':
agreg = optarg;
break;
case 's':
sort = optarg;
break;
default:
fprintf(stderr, "Usage: flow -f directory -a aggregation -s sort\n");
return (EXIT_FAILURE);
}
}
// setup agrflag
if (agreg.compare("srcport") == 0) {
agrflg = 1;
agrname = "srcport";
}
else if (agreg.compare("dstport") == 0) {
agrflg = 2;
agrname = "dstport";
}
else if (agreg.compare("srcip") == 0) {
agrflg = 7;
agrname = "srcip";
}
else if (agreg.compare("dstip") == 0) {
agrflg = 8;
agrname = "dstip";
}
else {
// can be masked
uint8_t mask = 0;
if (agreg.find('/') != std::string::npos)
mask = atoi(agreg.substr(agreg.find('/')).erase(0,1).c_str());
if (agreg.find("ip6") != std::string::npos) {
// ipv6 agregation
if (mask != 0)
ipv6mask = mask;
if (agreg.find("srcip") != std::string::npos) {
agrflg = 3;
agrname = "srcip";
}
else if (agreg.find("dstip") != std::string::npos) {
agrflg = 4;
agrname = "dstip";
}
}
else if (agreg.find("ip4") != std::string::npos) {
// ipv4 agregation
if (mask != 0)
ipv4mask = mask;
if (agreg.find("srcip") != std::string::npos) {
agrflg = 5;
agrname = "srcip";
}
else if (agreg.find("dstip") != std::string::npos) {
agrflg = 6;
agrname = "dstip";
}
}
else {
fprintf(stderr, "Error\n");
return (EXIT_FAILURE);
}
}
DIR *dir = opendir(filename);
if (dir == NULL) {
std::cout << "Error: -f requires a directory." << std::endl;
return EXIT_FAILURE;
}
std::string dirname = filename;
std::unordered_map<std::string, struct flow> map;
load_and_agreg(&map, dirname, dir, ipv4mask, ipv6mask, agrflg);
// sort using std::multimap
std::multimap<uint64_t, struct flow> ordered;
for ( auto it = map.begin(); it != map.end(); ++it ) {
if (sort.compare("packets") == 0)
ordered.insert(std::pair<uint64_t,struct flow>(it->second.packets,it->second));
else if (sort.compare("bytes") == 0)
ordered.insert(std::pair<uint64_t,struct flow>(it->second.bytes,it->second));
}
// output
map.clear();
std::string key;
std::cout << "#" << agrname << ",packets,bytes" << std::endl;
for ( auto it = prev(ordered.end()); it != ordered.begin(); it = prev(it)) {
switch (agrflg) {
case 1:
key = std::to_string(it->second.src_port);
break;
case 2:
key = std::to_string(it->second.dst_port);
break;
case 3:
apply_ipv6_mask(&it->second.src_addr, ipv6mask, ip);
key = ip;
break;
case 4:
apply_ipv6_mask(&it->second.dst_addr, ipv6mask, ip);
key = ip;;
break;
case 5:
apply_ipv4_mask(&it->second.src_addr, ipv4mask, ip);
key = ip;
break;
case 6:
apply_ipv4_mask(&it->second.dst_addr, ipv4mask, ip);
key = ip;
break;
case 7:
if (ntohl(it->second.sa_family) == AF_INET6) {
apply_ipv6_mask(&it->second.src_addr, ipv6mask, ip);
key = ip;
}
else {
apply_ipv4_mask(&it->second.src_addr, ipv4mask, ip);
key = ip;
}
break;
case 8:
if (ntohl(it->second.sa_family) == AF_INET6) {
apply_ipv6_mask(&it->second.dst_addr, ipv6mask, ip);
key = ip;
}
else {
apply_ipv4_mask(&it->second.dst_addr, ipv4mask, ip);
key = ip;
}
break;
default:
fprintf(stderr, "Error\n");
return (EXIT_FAILURE);
}
std::cout << key << "," << it->second.packets << "," << it->second.bytes<< std::endl;
}
// last line of output has to be taken care of seperately
auto it = ordered.begin();
switch (agrflg) {
case 1:
key = std::to_string(it->second.src_port);
break;
case 2:
key = std::to_string(it->second.dst_port);
break;
case 3:
apply_ipv6_mask(&it->second.src_addr, ipv6mask, ip);
key = ip;
break;
case 4:
apply_ipv6_mask(&it->second.dst_addr, ipv6mask, ip);
key = ip;;
break;
case 5:
apply_ipv4_mask(&it->second.src_addr, ipv4mask, ip);
key = ip;
break;
case 6:
apply_ipv4_mask(&it->second.dst_addr, ipv4mask, ip);
key = ip;
break;
case 7:
if (ntohl(it->second.sa_family) == AF_INET6) {
apply_ipv6_mask(&it->second.src_addr, ipv6mask, ip);
key = ip;
}
else {
apply_ipv4_mask(&it->second.src_addr, ipv4mask, ip);
key = ip;
}
break;
case 8:
if (ntohl(it->second.sa_family) == AF_INET6) {
apply_ipv6_mask(&it->second.dst_addr, ipv6mask, ip);
key = ip;
}
else {
apply_ipv4_mask(&it->second.dst_addr, ipv4mask, ip);
key = ip;
}
break;
default:
fprintf(stderr, "Error\n");
return (EXIT_FAILURE);
}
std::cout << key << "," << it->second.packets << "," << it->second.bytes<< std::endl;
return (EXIT_SUCCESS);
}