1 | #include <netdb.h> |
---|
2 | #include <inttypes.h> |
---|
3 | #include <lt_inttypes.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include "libtrace.h" |
---|
6 | #include "tracereport.h" |
---|
7 | |
---|
8 | static stat_t nlp_stat[3][65536] = {{{0,0}}} ; |
---|
9 | |
---|
10 | void nlp_per_packet(struct libtrace_packet_t *packet) |
---|
11 | { |
---|
12 | char *link=(char *)trace_get_link(packet); |
---|
13 | uint16_t type = htons(*(uint16_t*)(link+12)); |
---|
14 | libtrace_direction_t dir = trace_get_direction(packet); |
---|
15 | |
---|
16 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
---|
17 | dir = TRACE_DIR_OTHER; |
---|
18 | |
---|
19 | nlp_stat[dir][type].count++; |
---|
20 | nlp_stat[dir][type].bytes+=trace_get_wire_length(packet); |
---|
21 | } |
---|
22 | |
---|
23 | void nlp_report(void){ |
---|
24 | int i,j; |
---|
25 | |
---|
26 | FILE *out = fopen("nlp.rpt", "w"); |
---|
27 | if (!out) { |
---|
28 | perror("fopen"); |
---|
29 | return; |
---|
30 | } |
---|
31 | |
---|
32 | /* Put some headings up for human-readability */ |
---|
33 | fprintf(out, "%-12s\t%10s\t%16s %16s\n", |
---|
34 | "NETWORK LAYER", |
---|
35 | "DIRECTION", |
---|
36 | "BYTES", |
---|
37 | "PACKETS"); |
---|
38 | for(i = 0; i < 65536; i++){ |
---|
39 | if (nlp_stat[0][i].count==0 && |
---|
40 | nlp_stat[1][i].count==0 && nlp_stat[2][i].count==0) |
---|
41 | continue; |
---|
42 | switch(i){ |
---|
43 | case 0x0800: |
---|
44 | fprintf(out, "%12s", "IPv4 |"); |
---|
45 | break; |
---|
46 | case 0x0806: |
---|
47 | fprintf(out, "%12s", "ARP |"); |
---|
48 | break; |
---|
49 | case 0x8137: |
---|
50 | fprintf(out, "%12s", "IPX |"); |
---|
51 | break; |
---|
52 | case 0x814C: |
---|
53 | fprintf(out, "%12s", "SNMP |"); |
---|
54 | break; |
---|
55 | case 0x86DD: |
---|
56 | fprintf(out, "%12s", "IPv6 |"); |
---|
57 | break; |
---|
58 | case 0x880B: |
---|
59 | fprintf(out, "%12s", "PPP |"); |
---|
60 | break; |
---|
61 | default: |
---|
62 | fprintf(out, "%10i |",i); |
---|
63 | } |
---|
64 | for(j=0;j<3;j++){ |
---|
65 | if (j != 0) { |
---|
66 | fprintf(out, "%12s", " |"); |
---|
67 | } |
---|
68 | |
---|
69 | switch (j) { |
---|
70 | case 0: |
---|
71 | fprintf(out, "\t%10s", "Outbound"); |
---|
72 | break; |
---|
73 | case 1: |
---|
74 | fprintf(out, "\t%10s", "Inbound"); |
---|
75 | break; |
---|
76 | case 2: |
---|
77 | fprintf(out, "\t%10s", "Unknown"); |
---|
78 | break; |
---|
79 | } |
---|
80 | |
---|
81 | fprintf(out, "\t%16llu %16llu\n", |
---|
82 | nlp_stat[j][i].bytes, |
---|
83 | nlp_stat[j][i].count); |
---|
84 | } |
---|
85 | } |
---|
86 | fclose(out); |
---|
87 | } |
---|