| 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 ecn_stat[3][4] = {{{0,0}}} ; |
|---|
| 9 | |
|---|
| 10 | void ecn_per_packet(struct libtrace_packet_t *packet) |
|---|
| 11 | { |
|---|
| 12 | struct libtrace_ip *ip = trace_get_ip(packet); |
|---|
| 13 | libtrace_direction_t dir = trace_get_direction(packet); |
|---|
| 14 | int ecn; |
|---|
| 15 | |
|---|
| 16 | if (!ip) |
|---|
| 17 | return; |
|---|
| 18 | |
|---|
| 19 | if (dir != TRACE_DIR_INCOMING && dir != TRACE_DIR_OUTGOING) |
|---|
| 20 | dir = TRACE_DIR_OTHER; |
|---|
| 21 | |
|---|
| 22 | ecn = ip->ip_tos & 0x2; |
|---|
| 23 | ecn_stat[dir][ecn].count++; |
|---|
| 24 | ecn_stat[dir][ecn].bytes+=trace_get_wire_length(packet); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | void ecn_report(void) |
|---|
| 28 | { |
|---|
| 29 | int i,j; |
|---|
| 30 | |
|---|
| 31 | FILE *out = fopen("ecn.out", "w"); |
|---|
| 32 | if (!out) { |
|---|
| 33 | perror("fopen"); |
|---|
| 34 | return; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /* Put some headings up for human-readability */ |
|---|
| 38 | fprintf(out, "%-12s\t%10s\t%16s %16s\n", |
|---|
| 39 | "ECN", |
|---|
| 40 | "DIRECTION", |
|---|
| 41 | "BYTES", |
|---|
| 42 | "PACKETS"); |
|---|
| 43 | |
|---|
| 44 | for(i=0;i<4;++i) { |
|---|
| 45 | if (ecn_stat[0][i].count==0 && |
|---|
| 46 | ecn_stat[1][i].count==0 && ecn_stat[2][i].count==0) |
|---|
| 47 | continue; |
|---|
| 48 | switch(i){ |
|---|
| 49 | case 1: |
|---|
| 50 | fprintf(out, "%12s", "ECN CAPE |"); |
|---|
| 51 | break; |
|---|
| 52 | case 2: |
|---|
| 53 | fprintf(out, "%12s", "ECN CAPE |"); |
|---|
| 54 | break; |
|---|
| 55 | case 3: |
|---|
| 56 | fprintf(out, "%12s", "CONG EXP |"); |
|---|
| 57 | break; |
|---|
| 58 | default: |
|---|
| 59 | fprintf(out, "%12s", "NO ECN |"); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | for(j=0;j<3;j++){ |
|---|
| 63 | if (j != 0) { |
|---|
| 64 | fprintf(out, "%12s", " |"); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | switch (j) { |
|---|
| 68 | case 0: |
|---|
| 69 | fprintf(out, "\t%10s", "Outbound"); |
|---|
| 70 | break; |
|---|
| 71 | case 1: |
|---|
| 72 | fprintf(out, "\t%10s", "Inbound"); |
|---|
| 73 | break; |
|---|
| 74 | case 2: |
|---|
| 75 | fprintf(out, "\t%10s", "Unknown"); |
|---|
| 76 | break; |
|---|
| 77 | } |
|---|
| 78 | fprintf(out, "\t%16llu %16llu\n", |
|---|
| 79 | ecn_stat[j][i].bytes, |
|---|
| 80 | ecn_stat[j][i].count); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int total = 0; |
|---|
| 85 | for(i=0;i<4;i++){ |
|---|
| 86 | for(j=1;j<4;j++) |
|---|
| 87 | total += ecn_stat[i][j].count; |
|---|
| 88 | } |
|---|
| 89 | fprintf(out, "%s: %i\n", "Total ECN", total); |
|---|
| 90 | fclose(out); |
|---|
| 91 | } |
|---|