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