File: traceends.cc

package info (click to toggle)
libtrace3 3.0.22-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,452 kB
  • sloc: ansic: 24,574; sh: 11,372; cpp: 1,811; makefile: 460; yacc: 96; lex: 50
file content (456 lines) | stat: -rw-r--r-- 10,390 bytes parent folder | download | duplicates (4)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#define __STDC_FORMAT_MACROS

#include <libtrace.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <lt_inttypes.h>
#include <getopt.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

#include <map>

typedef struct end_counter {
	uint64_t src_bytes;
	uint64_t src_pbytes;
	uint64_t src_pkts;
	uint64_t dst_pkts;
	uint64_t dst_bytes;
	uint64_t dst_pbytes;

	double last_active;

} end_counter_t;

typedef struct mac_addr {
	uint8_t addr[6];
} mac_addr_t;

struct v6comp {
	bool operator() (const struct in6_addr &a, const struct in6_addr &b) const {
		if (memcmp(&a, &b, sizeof(struct in6_addr)) < 0)
			return true;
		return false;
	}
};
	
struct maccomp {
	bool operator() (const mac_addr_t &a, const mac_addr_t &b) const {
		if (memcmp(&a, &b, sizeof(mac_addr_t)) < 0)
			return true;
		return false;
	}
};

typedef std::map<uint32_t, end_counter_t *> IP4EndMap;
typedef std::map<struct in6_addr, end_counter_t *, v6comp> IP6EndMap;
typedef std::map<mac_addr_t, end_counter_t *, maccomp> MacEndMap;

enum {
	MODE_MAC,
	MODE_IPV4,
	MODE_IPV6
};

int mode = MODE_IPV4;

IP4EndMap ipv4;
IP6EndMap ipv6;
MacEndMap mac;

static int usage(char *argv0)
{
        printf("Usage:\n"
        "%s flags inputuri [inputuri ... ] \n"
        "-f --filter=bpf        Only output packets that match filter\n"
        "-H --help     		Print this message\n"
        "-A --address=addr     	Specifies which address type to match (mac, v4, v6)\n"
        ,argv0);
        exit(1);
}

volatile int done=0;

static void cleanup_signal(int sig)
{
        (void)sig;
        done=1;
	trace_interrupt();
}

static end_counter_t *create_counter() {
	end_counter_t *c = (end_counter_t *)malloc(sizeof(end_counter_t));

	c->src_bytes = 0;
	c->src_pbytes = 0;
	c->src_pkts = 0;
	c->dst_pkts = 0;
	c->dst_bytes = 0;
	c->dst_pbytes = 0;

	c->last_active = 0.0;
	return c;
}

static char *mac_string(mac_addr_t m, char *str) {
	
	
	snprintf(str, 80, "%02x:%02x:%02x:%02x:%02x:%02x", 
		m.addr[0], m.addr[1], m.addr[2], m.addr[3], m.addr[4],
		m.addr[5]);
	return str;
}

static void dump_mac_map() {
	MacEndMap::iterator it;
	char str[80];
	char timestr[80];
	struct tm *tm;
	time_t t;
	
	for (it = mac.begin(); it != mac.end(); it++) {
		t = (time_t)(it->second->last_active);
		tm = localtime(&t);
		strftime(timestr, 80, "%d/%m,%H:%M:%S", tm);
		printf("%18s %16s %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 "\n", 
				mac_string(it->first, str),
				timestr,
				it->second->src_pkts,
				it->second->src_bytes,
				it->second->src_pbytes,
				it->second->dst_pkts,
				it->second->dst_bytes,
				it->second->dst_pbytes);
	}
}

static void dump_ipv4_map() {
	IP4EndMap::iterator it;
	struct in_addr in;
	char timestr[80];
	struct tm *tm;
	time_t t;
	for (it = ipv4.begin(); it != ipv4.end(); it++) {
		in.s_addr = it->first;
		t = (time_t)(it->second->last_active);
		tm = localtime(&t);
		strftime(timestr, 80, "%d/%m,%H:%M:%S", tm);
		printf("%16s %16s %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 "\n", 
				inet_ntoa(in),
				timestr,
				it->second->src_pkts,
				it->second->src_bytes,
				it->second->src_pbytes,
				it->second->dst_pkts,
				it->second->dst_bytes,
				it->second->dst_pbytes);
	}
}

static void dump_ipv6_map() {
	IP6EndMap::iterator it;
	struct in6_addr in;
	char ip6_addr[128];
	char timestr[80];
	struct tm *tm;
	time_t t;

	for (it = ipv6.begin(); it != ipv6.end(); it++) {
		in = it->first;
		t = (time_t)(it->second->last_active);
		tm = localtime(&t);
		strftime(timestr, 80, "%d/%m,%H:%M:%S", tm);
		printf("%40s %16s %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 " %16" PRIu64 "\n", 
				inet_ntop(AF_INET6, &in, ip6_addr, 128),
				timestr,
				it->second->src_pkts,
				it->second->src_bytes,
				it->second->src_pbytes,
				it->second->dst_pkts,
				it->second->dst_bytes,
				it->second->dst_pbytes);
	}
}

static void update_ipv6(libtrace_ip6_t *ip, uint16_t ip_len, uint32_t rem, 
		uint32_t plen, 	double ts) {

	struct in6_addr key;
	IP6EndMap::iterator it;
	end_counter_t *c = NULL;

	if (rem < sizeof(libtrace_ip6_t))
		return;
	
	key = ip->ip_src;
	
	it = ipv6.find(key);
	if (it == ipv6.end()) {
		c = create_counter();
		ipv6[key] = c;
	} else {
		c = it->second;
	}

	c->src_pkts ++;
	c->src_pbytes += plen;
	c->src_bytes += ip_len;
	c->last_active = ts;
	
	key = ip->ip_dst;
	
	it = ipv6.find(key);
	if (it == ipv6.end()) {
		c = create_counter();
		ipv6[key] = c;
	} else {
		c = it->second;
	}

	c->dst_pkts ++;
	c->dst_pbytes += plen;
	c->dst_bytes += ip_len;
	c->last_active = ts;
}

static void update_mac(uint8_t *src, uint8_t *dst, uint16_t ip_len,
		uint32_t plen, double ts) {

	mac_addr_t key;
	end_counter_t *c = NULL;
	MacEndMap::iterator it;

	memcpy(&(key.addr), src, sizeof(key.addr));
	it = mac.find(key);
	
	if (it == mac.end()) {
		c = create_counter();
		mac[key] = c;
	} else {
		c = it->second;
	}

	c->src_pkts ++;
	c->src_pbytes += plen;
	c->src_bytes += ip_len;
	c->last_active = ts;

	memcpy(&key.addr, dst, sizeof(key.addr));
	it = mac.find(key);
	
	if (it == mac.end()) {
		c = create_counter();
		mac[key] = c;
	} else {
		c = it->second;
	}

	c->dst_pkts ++;
	c->dst_pbytes += plen;
	c->dst_bytes += ip_len;
	c->last_active = ts;
}

static void update_ipv4(libtrace_ip_t *ip, uint16_t ip_len, uint32_t rem, 
		uint32_t plen, 	double ts) {

	uint32_t key;
	IP4EndMap::iterator it;
	end_counter_t *c = NULL;

	if (rem < sizeof(libtrace_ip_t))
		return;
	
	key = ip->ip_src.s_addr;
	
	it = ipv4.find(key);
	if (it == ipv4.end()) {
		c = create_counter();
		ipv4[key] = c;
	} else {
		c = it->second;
	}

	c->src_pkts ++;
	c->src_pbytes += plen;
	c->src_bytes += ip->ip_len;
	c->last_active = ts;
	
	key = ip->ip_dst.s_addr;
	
	it = ipv4.find(key);
	if (it == ipv4.end()) {
		c = create_counter();
		ipv4[key] = c;
	} else {
		c = it->second;
	}

	c->dst_pkts ++;
	c->dst_pbytes += plen;
	c->dst_bytes += ip_len;
	c->last_active = ts;
}

static int per_packet(libtrace_packet_t *packet) {

	void *header;
	uint16_t ethertype;
	uint32_t rem;
	uint16_t ip_len = 0;
	uint32_t plen = trace_get_payload_length(packet);
	double ts = trace_get_seconds(packet);
	libtrace_ip_t *ip = NULL;
	libtrace_ip6_t *ip6 = NULL;
	uint8_t *src_mac, *dst_mac;

	header = trace_get_layer3(packet, &ethertype, &rem);

	if (header == NULL || rem == 0)
		return 1;
	
	if (ethertype == TRACE_ETHERTYPE_IP) {
		ip = (libtrace_ip_t *)header;
		if (rem < sizeof(libtrace_ip_t))
			return 1;
		ip_len = ntohs(ip->ip_len);
		if (mode == MODE_IPV4 && ip) {
			update_ipv4(ip, ip_len, rem, plen, ts);
			return 1;
		}
	}

	if (ethertype == TRACE_ETHERTYPE_IPV6) {
		ip6 = (libtrace_ip6_t *)header;
		if (rem < sizeof(libtrace_ip6_t))
			return 1;
		ip_len = ntohs(ip6->plen) + sizeof(libtrace_ip6_t);
		if (mode == MODE_IPV6 && ip6) {
			update_ipv6(ip6, ip_len, rem, plen, ts);
			return 1;
		}
	}

	if (mode == MODE_MAC) {
		src_mac = trace_get_source_mac(packet);
		dst_mac = trace_get_destination_mac(packet);

		if (src_mac == NULL || dst_mac == NULL)
			return 1;
		update_mac(src_mac, dst_mac, ip_len, plen, ts);
	}

	return 1;
}

int main(int argc, char *argv[]) {

        int i;
        struct sigaction sigact;
	struct libtrace_filter_t *filter=NULL;
        struct libtrace_t *input = NULL;
        struct libtrace_packet_t *packet = trace_create_packet();

        while(1) {
                int option_index;
                struct option long_options[] = {
                        { "filter",        1, 0, 'f' },
                        { "help", 	   0, 0, 'H' },
			{ "addresses", 	   1, 0, 'A' },	
                        { NULL,            0, 0, 0   },
                };

                int c=getopt_long(argc, argv, "A:f:H",
                                long_options, &option_index);

                if (c==-1)
                        break;
		switch (c) {
			case 'A':
				if (strncmp(optarg, "mac", 3) == 0)
					mode = MODE_MAC;
				else if (strncmp(optarg, "v4", 2) == 0)
					mode = MODE_IPV4;
				else if (strncmp(optarg, "v6", 2) == 0)
					mode = MODE_IPV6;
				else {
					fprintf(stderr, "Invalid address type, must be either mac, v4 or v6\n");
					return 1;
				}
				break;

                        case 'f': filter=trace_create_filter(optarg);
                        	break;
			case 'H':
                                usage(argv[0]);
                                break;
			default:
                                fprintf(stderr,"Unknown option: %c\n",c);
                                usage(argv[0]);
                                return 1;
		}

	}
        sigact.sa_handler = cleanup_signal;
        sigemptyset(&sigact.sa_mask);
        sigact.sa_flags = SA_RESTART;

        sigaction(SIGINT, &sigact, NULL);
        sigaction(SIGTERM, &sigact, NULL);
        sigaction(SIGPIPE, &sigact, NULL);
        sigaction(SIGHUP, &sigact, NULL);

	for (i = optind; i < argc; i++) {
		input = trace_create(argv[i]);

                if (trace_is_err(input)) {
                        trace_perror(input,"%s",argv[i]);
                        return 1;
                }

                if (filter && trace_config(input, TRACE_OPTION_FILTER, filter) == 1) {
                        trace_perror(input, "Configuring filter for %s",
                                        argv[i]);
                        return 1;
                }

                if (trace_start(input)==-1) {
                        trace_perror(input,"%s",argv[i]);
                        return 1;
                }

		while (trace_read_packet(input,packet)>0) {
                        if (per_packet(packet) < 1)
                                done = 1;
                        if (done)
                                break;
                }

                if (done)
                        break;

                if (trace_is_err(input)) {
                        trace_perror(input,"Reading packets");
                        trace_destroy(input);
                        break;
                }

                trace_destroy(input);
        }

	/* Dump results */
	if (mode == MODE_IPV4)
		dump_ipv4_map();
	if (mode == MODE_IPV6)
		dump_ipv6_map();
	if (mode == MODE_MAC)
		dump_mac_map();
	return 0;
}