File: protocol_report.c

package info (click to toggle)
libtrace3 3.0.7-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,676 kB
  • ctags: 3,140
  • sloc: ansic: 20,551; sh: 10,125; cpp: 1,384; makefile: 415; yacc: 96; lex: 50
file content (79 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download | duplicates (6)
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
#include <netdb.h>
#include <inttypes.h>
#include <lt_inttypes.h>
#include <stdio.h>
#include "libtrace.h"
#include "tracereport.h"
#include "report.h"

static stat_t prot_stat[3][256] = {{{0,0}}} ;
static bool suppress[3] = {true,true,true};

void protocol_per_packet(struct libtrace_packet_t *packet)
{
	uint8_t proto;
	libtrace_direction_t dir = trace_get_direction(packet);
	
	if (trace_get_transport(packet,&proto,NULL)==NULL)
		return;
		
	if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING)
		dir = TRACE_DIR_OTHER;
	
	prot_stat[dir][proto].count++;
	prot_stat[dir][proto].bytes+=trace_get_wire_length(packet);
	suppress[dir] = false;
}

void protocol_report(void)
{
	int i,j;
	FILE *out = fopen("protocol.rpt", "w");
	if (!out) {
		perror("fopen");
		return;
	}
	fprintf(out, "%-16s\t%10s\t%16s %16s\n",
			"PROTOCOL",
			"DIRECTION",
			"BYTES",
			"PACKETS");
	
	setprotoent(1);
	for(i=0;i<256;++i) {
		struct protoent *prot;
		if (prot_stat[0][i].count==0 && 
			prot_stat[1][i].count==0 && prot_stat[2][i].count==0)
			continue;
		prot = getprotobynumber(i);
		if (prot) {
			fprintf(out, "%16s",prot->p_name);
		}
		else {
			fprintf(out, "%16i:",i);
		}
		for (j=0; j < 3; j++) {
			if (j != 0) {
				fprintf(out, "%16s", " ");
			}
			switch (j) {
				case 0:
                                        fprintf(out, "\t%10s", "Outbound");
                                        break;
                                case 1:
                                        fprintf(out, "\t%10s", "Inbound");
                                        break;
                                case 2:
                                        fprintf(out, "\t%10s", "Unknown");
                                        break;
                        }

                        fprintf(out, "\t%16" PRIu64 " %16" PRIu64 "\n",
                                        prot_stat[j][i].bytes,
                                        prot_stat[j][i].count);
                }
	}

	setprotoent(0);
	fclose(out);
}