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