| 1 | /* |
|---|
| 2 | * This file is part of libtrace |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand. |
|---|
| 5 | * Authors: Daniel Lawson |
|---|
| 6 | * Perry Lorier |
|---|
| 7 | * Josef Vodanovich |
|---|
| 8 | * |
|---|
| 9 | * All rights reserved. |
|---|
| 10 | * |
|---|
| 11 | * This code has been developed by the University of Waikato WAND |
|---|
| 12 | * research group. For further information please see http://www.wand.net.nz/ |
|---|
| 13 | * |
|---|
| 14 | * libtrace is free software; you can redistribute it and/or modify |
|---|
| 15 | * it under the terms of the GNU General Public License as published by |
|---|
| 16 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 17 | * (at your option) any later version. |
|---|
| 18 | * |
|---|
| 19 | * libtrace is distributed in the hope that it will be useful, |
|---|
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 22 | * GNU General Public License for more details. |
|---|
| 23 | * |
|---|
| 24 | * You should have received a copy of the GNU General Public License |
|---|
| 25 | * along with libtrace; if not, write to the Free Software |
|---|
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 27 | * |
|---|
| 28 | * $Id$ |
|---|
| 29 | * |
|---|
| 30 | */ |
|---|
| 31 | |
|---|
| 32 | /* This program takes a series of traces and bpf filters and outputs how many |
|---|
| 33 | * bytes/packets |
|---|
| 34 | */ |
|---|
| 35 | |
|---|
| 36 | #include <stdio.h> |
|---|
| 37 | #include <stdlib.h> |
|---|
| 38 | #include <assert.h> |
|---|
| 39 | #include <string.h> |
|---|
| 40 | #include <sys/time.h> |
|---|
| 41 | #include <sys/types.h> |
|---|
| 42 | |
|---|
| 43 | #include <getopt.h> |
|---|
| 44 | #include <inttypes.h> |
|---|
| 45 | //#include "lt_inttypes.h" |
|---|
| 46 | |
|---|
| 47 | #include "libtrace.h" |
|---|
| 48 | #include "tracereport.h" |
|---|
| 49 | #include "report.h" |
|---|
| 50 | |
|---|
| 51 | struct libtrace_t *trace; |
|---|
| 52 | uint32_t reports_required = 0; |
|---|
| 53 | |
|---|
| 54 | /* Process a trace, counting packets that match filter(s) */ |
|---|
| 55 | void run_trace(char *uri, libtrace_filter_t *filter, int count) |
|---|
| 56 | { |
|---|
| 57 | struct libtrace_packet_t *packet = trace_create_packet(); |
|---|
| 58 | |
|---|
| 59 | trace = trace_create(uri); |
|---|
| 60 | |
|---|
| 61 | if (trace_is_err(trace)) { |
|---|
| 62 | trace_perror(trace,"trace_create"); |
|---|
| 63 | return; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | if (filter) { |
|---|
| 67 | trace_config(trace,TRACE_OPTION_FILTER,filter); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | if (trace_start(trace)==-1) { |
|---|
| 71 | trace_perror(trace,"trace_start"); |
|---|
| 72 | return; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | for (;;) { |
|---|
| 76 | int psize; |
|---|
| 77 | |
|---|
| 78 | /* Not convinced we need this count business */ |
|---|
| 79 | /* |
|---|
| 80 | if (count--<1) |
|---|
| 81 | break; |
|---|
| 82 | */ |
|---|
| 83 | if ((psize = trace_read_packet(trace, packet)) <1) { |
|---|
| 84 | break; |
|---|
| 85 | } |
|---|
| 86 | if (reports_required & REPORT_TYPE_ERROR) |
|---|
| 87 | error_per_packet(packet); |
|---|
| 88 | if (reports_required & REPORT_TYPE_PORT) |
|---|
| 89 | port_per_packet(packet); |
|---|
| 90 | if (reports_required & REPORT_TYPE_PROTO) |
|---|
| 91 | protocol_per_packet(packet); |
|---|
| 92 | if (reports_required & REPORT_TYPE_TOS) |
|---|
| 93 | tos_per_packet(packet); |
|---|
| 94 | if (reports_required & REPORT_TYPE_TTL) |
|---|
| 95 | ttl_per_packet(packet); |
|---|
| 96 | if (reports_required & REPORT_TYPE_FLOW) |
|---|
| 97 | flow_per_packet(packet); |
|---|
| 98 | if (reports_required & REPORT_TYPE_TCPOPT) |
|---|
| 99 | tcpopt_per_packet(packet); |
|---|
| 100 | if (reports_required & REPORT_TYPE_NLP) |
|---|
| 101 | nlp_per_packet(packet); |
|---|
| 102 | if (reports_required & REPORT_TYPE_DIR) |
|---|
| 103 | dir_per_packet(packet); |
|---|
| 104 | if (reports_required & REPORT_TYPE_ECN) |
|---|
| 105 | ecn_per_packet(packet); |
|---|
| 106 | if (reports_required & REPORT_TYPE_TCPSEG) |
|---|
| 107 | tcpseg_per_packet(packet); |
|---|
| 108 | } |
|---|
| 109 | trace_destroy(trace); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void usage(char *argv0) |
|---|
| 113 | { |
|---|
| 114 | fprintf(stderr,"Usage:\n" |
|---|
| 115 | "%s flags traceuri [traceuri...]\n" |
|---|
| 116 | "-f --filter=bpf \tApply BPF filter. Can be specified multiple times\n" |
|---|
| 117 | "-e --error Report packet errors (e.g. checksum failures, rxerrors)\n" |
|---|
| 118 | "-F --flow Report flows\n" |
|---|
| 119 | "-P --protocol Report transport protocols\n" |
|---|
| 120 | "-p --port Report port numbers\n" |
|---|
| 121 | "-T --tos Report IP TOS\n" |
|---|
| 122 | "-t --ttl Report IP TTL\n" |
|---|
| 123 | "-O --tcpoptions \tReport TCP Options\n" |
|---|
| 124 | "-n --nlp Report network layer protocols\n" |
|---|
| 125 | "-d --direction Report direction\n" |
|---|
| 126 | "-C --ecn Report TCP ECN information\n" |
|---|
| 127 | "-s --tcpsegment \tReport TCP segment size\n" |
|---|
| 128 | "-H --help Print libtrace runtime documentation\n" |
|---|
| 129 | ,argv0); |
|---|
| 130 | exit(1); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | int main(int argc, char *argv[]) { |
|---|
| 134 | |
|---|
| 135 | int i; |
|---|
| 136 | int opt; |
|---|
| 137 | char *filterstring=NULL; |
|---|
| 138 | |
|---|
| 139 | libtrace_filter_t *filter = NULL;/*trace_bpf_setfilter(filterstring); */ |
|---|
| 140 | |
|---|
| 141 | while (1) { |
|---|
| 142 | int option_index; |
|---|
| 143 | struct option long_options[] = { |
|---|
| 144 | { "filter", 1, 0, 'f' }, |
|---|
| 145 | { "help", 0, 0, 'H' }, |
|---|
| 146 | { "error", 0, 0, 'e' }, |
|---|
| 147 | { "flow", 0, 0, 'F' }, |
|---|
| 148 | { "protocol", 0, 0, 'P' }, |
|---|
| 149 | { "port", 0, 0, 'p' }, |
|---|
| 150 | { "tos", 0, 0, 'T' }, |
|---|
| 151 | { "ttl", 0, 0, 't' }, |
|---|
| 152 | { "tcpoptions", 0, 0, 'O' }, |
|---|
| 153 | { "nlp", 0, 0, 'n' }, |
|---|
| 154 | { "direction", 0, 0, 'd' }, |
|---|
| 155 | { "ecn", 0, 0, 'C' }, |
|---|
| 156 | { "tcpsegment", 0, 0, 's' }, |
|---|
| 157 | { NULL, 0, 0, 0 } |
|---|
| 158 | }; |
|---|
| 159 | opt = getopt_long(argc, argv, "f:HeFPpTtOndCs", long_options, |
|---|
| 160 | &option_index); |
|---|
| 161 | if (opt == -1) |
|---|
| 162 | break; |
|---|
| 163 | |
|---|
| 164 | switch (opt) { |
|---|
| 165 | case 'C': |
|---|
| 166 | reports_required |= REPORT_TYPE_ECN; |
|---|
| 167 | break; |
|---|
| 168 | case 'd': |
|---|
| 169 | reports_required |= REPORT_TYPE_DIR; |
|---|
| 170 | break; |
|---|
| 171 | case 'e': |
|---|
| 172 | reports_required |= REPORT_TYPE_ERROR; |
|---|
| 173 | break; |
|---|
| 174 | case 'F': |
|---|
| 175 | reports_required |= REPORT_TYPE_FLOW; |
|---|
| 176 | break; |
|---|
| 177 | case 'f': |
|---|
| 178 | filterstring = optarg; |
|---|
| 179 | break; |
|---|
| 180 | case 'H': |
|---|
| 181 | usage(argv[0]); |
|---|
| 182 | break; |
|---|
| 183 | case 'n': |
|---|
| 184 | reports_required |= REPORT_TYPE_NLP; |
|---|
| 185 | break; |
|---|
| 186 | case 'O': |
|---|
| 187 | reports_required |= REPORT_TYPE_TCPOPT; |
|---|
| 188 | break; |
|---|
| 189 | case 'P': |
|---|
| 190 | reports_required |= REPORT_TYPE_PROTO; |
|---|
| 191 | break; |
|---|
| 192 | case 'p': |
|---|
| 193 | reports_required |= REPORT_TYPE_PORT; |
|---|
| 194 | break; |
|---|
| 195 | case 's': |
|---|
| 196 | reports_required |= REPORT_TYPE_TCPSEG; |
|---|
| 197 | break; |
|---|
| 198 | case 'T': |
|---|
| 199 | reports_required |= REPORT_TYPE_TOS; |
|---|
| 200 | break; |
|---|
| 201 | case 't': |
|---|
| 202 | reports_required |= REPORT_TYPE_TTL; |
|---|
| 203 | break; |
|---|
| 204 | default: |
|---|
| 205 | usage(argv[0]); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | if (filterstring) { |
|---|
| 210 | filter = trace_create_filter(filterstring); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | for(i=optind;i<argc;++i) { |
|---|
| 215 | /* This is handy for knowing how far through the traceset |
|---|
| 216 | * we are - printing to stderr because we use stdout for |
|---|
| 217 | * genuine output at the moment */ |
|---|
| 218 | fprintf(stderr, "Reading from trace: %s\n", argv[i]); |
|---|
| 219 | run_trace(argv[i],filter,(1<<30)); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | if (reports_required & REPORT_TYPE_ERROR) |
|---|
| 223 | error_report(); |
|---|
| 224 | if (reports_required & REPORT_TYPE_FLOW) |
|---|
| 225 | flow_report(); |
|---|
| 226 | if (reports_required & REPORT_TYPE_TOS) |
|---|
| 227 | tos_report(); |
|---|
| 228 | if (reports_required & REPORT_TYPE_PROTO) |
|---|
| 229 | protocol_report(); |
|---|
| 230 | if (reports_required & REPORT_TYPE_PORT) |
|---|
| 231 | port_report(); |
|---|
| 232 | if (reports_required & REPORT_TYPE_TTL) |
|---|
| 233 | ttl_report(); |
|---|
| 234 | if (reports_required & REPORT_TYPE_TCPOPT) |
|---|
| 235 | tcpopt_report(); |
|---|
| 236 | if (reports_required & REPORT_TYPE_NLP) |
|---|
| 237 | nlp_report(); |
|---|
| 238 | if (reports_required & REPORT_TYPE_DIR) |
|---|
| 239 | dir_report(); |
|---|
| 240 | if (reports_required & REPORT_TYPE_ECN) |
|---|
| 241 | ecn_report(); |
|---|
| 242 | if (reports_required & REPORT_TYPE_TCPSEG) |
|---|
| 243 | tcpseg_report(); |
|---|
| 244 | return 0; |
|---|
| 245 | } |
|---|