| 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_MISC) |
|---|
| 87 | misc_per_packet(packet); |
|---|
| 88 | if (reports_required & REPORT_TYPE_ERROR) |
|---|
| 89 | error_per_packet(packet); |
|---|
| 90 | if (reports_required & REPORT_TYPE_PORT) |
|---|
| 91 | port_per_packet(packet); |
|---|
| 92 | if (reports_required & REPORT_TYPE_PROTO) |
|---|
| 93 | protocol_per_packet(packet); |
|---|
| 94 | if (reports_required & REPORT_TYPE_TOS) |
|---|
| 95 | tos_per_packet(packet); |
|---|
| 96 | if (reports_required & REPORT_TYPE_TTL) |
|---|
| 97 | ttl_per_packet(packet); |
|---|
| 98 | if (reports_required & REPORT_TYPE_FLOW) |
|---|
| 99 | flow_per_packet(packet); |
|---|
| 100 | if (reports_required & REPORT_TYPE_TCPOPT) |
|---|
| 101 | tcpopt_per_packet(packet); |
|---|
| 102 | if (reports_required & REPORT_TYPE_SYNOPT) |
|---|
| 103 | synopt_per_packet(packet); |
|---|
| 104 | if (reports_required & REPORT_TYPE_NLP) |
|---|
| 105 | nlp_per_packet(packet); |
|---|
| 106 | if (reports_required & REPORT_TYPE_DIR) |
|---|
| 107 | dir_per_packet(packet); |
|---|
| 108 | if (reports_required & REPORT_TYPE_ECN) |
|---|
| 109 | ecn_per_packet(packet); |
|---|
| 110 | if (reports_required & REPORT_TYPE_TCPSEG) |
|---|
| 111 | tcpseg_per_packet(packet); |
|---|
| 112 | if (reports_required & REPORT_TYPE_LOCALITY) |
|---|
| 113 | locality_per_packet(packet); |
|---|
| 114 | } |
|---|
| 115 | trace_destroy(trace); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void usage(char *argv0) |
|---|
| 119 | { |
|---|
| 120 | fprintf(stderr,"Usage:\n" |
|---|
| 121 | "%s flags traceuri [traceuri...]\n" |
|---|
| 122 | "-f --filter=bpf \tApply BPF filter. Can be specified multiple times\n" |
|---|
| 123 | "-e --error Report packet errors (e.g. checksum failures, rxerrors)\n" |
|---|
| 124 | "-F --flow Report flows\n" |
|---|
| 125 | "-m --misc Report misc information (start/end times, duration, pps)\n" |
|---|
| 126 | "-P --protocol Report transport protocols\n" |
|---|
| 127 | "-p --port Report port numbers\n" |
|---|
| 128 | "-T --tos Report IP TOS\n" |
|---|
| 129 | "-t --ttl Report IP TTL\n" |
|---|
| 130 | "-O --tcpoptions \tReport TCP Options\n" |
|---|
| 131 | "-o --synoptions \tReport TCP Options seen on SYNs\n" |
|---|
| 132 | "-n --nlp Report network layer protocols\n" |
|---|
| 133 | "-d --direction Report direction\n" |
|---|
| 134 | "-C --ecn Report TCP ECN information\n" |
|---|
| 135 | "-s --tcpsegment \tReport TCP segment size\n" |
|---|
| 136 | "-l --locality=input \tReport traffic locality using <input> to describe local IP addresses\n" |
|---|
| 137 | "-H --help Print libtrace runtime documentation\n" |
|---|
| 138 | ,argv0); |
|---|
| 139 | exit(1); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | int main(int argc, char *argv[]) { |
|---|
| 143 | |
|---|
| 144 | int i; |
|---|
| 145 | int opt; |
|---|
| 146 | char *filterstring=NULL; |
|---|
| 147 | |
|---|
| 148 | libtrace_filter_t *filter = NULL;/*trace_bpf_setfilter(filterstring); */ |
|---|
| 149 | |
|---|
| 150 | while (1) { |
|---|
| 151 | int option_index; |
|---|
| 152 | struct option long_options[] = { |
|---|
| 153 | { "filter", 1, 0, 'f' }, |
|---|
| 154 | { "help", 0, 0, 'H' }, |
|---|
| 155 | { "error", 0, 0, 'e' }, |
|---|
| 156 | { "flow", 0, 0, 'F' }, |
|---|
| 157 | { "protocol", 0, 0, 'P' }, |
|---|
| 158 | { "port", 0, 0, 'p' }, |
|---|
| 159 | { "misc", 0, 0, 'm' }, |
|---|
| 160 | { "tos", 0, 0, 'T' }, |
|---|
| 161 | { "ttl", 0, 0, 't' }, |
|---|
| 162 | { "tcpoptions", 0, 0, 'O' }, |
|---|
| 163 | { "synoptions", 0, 0, 'o' }, |
|---|
| 164 | { "nlp", 0, 0, 'n' }, |
|---|
| 165 | { "direction", 0, 0, 'd' }, |
|---|
| 166 | { "ecn", 0, 0, 'C' }, |
|---|
| 167 | { "tcpsegment", 0, 0, 's' }, |
|---|
| 168 | { "locality", 1, 0, 'l' }, |
|---|
| 169 | { NULL, 0, 0, 0 } |
|---|
| 170 | }; |
|---|
| 171 | opt = getopt_long(argc, argv, "f:HemFPpTtOondCsl:", |
|---|
| 172 | long_options, &option_index); |
|---|
| 173 | if (opt == -1) |
|---|
| 174 | break; |
|---|
| 175 | |
|---|
| 176 | switch (opt) { |
|---|
| 177 | case 'C': |
|---|
| 178 | reports_required |= REPORT_TYPE_ECN; |
|---|
| 179 | break; |
|---|
| 180 | case 'd': |
|---|
| 181 | reports_required |= REPORT_TYPE_DIR; |
|---|
| 182 | break; |
|---|
| 183 | case 'e': |
|---|
| 184 | reports_required |= REPORT_TYPE_ERROR; |
|---|
| 185 | break; |
|---|
| 186 | case 'F': |
|---|
| 187 | reports_required |= REPORT_TYPE_FLOW; |
|---|
| 188 | break; |
|---|
| 189 | case 'f': |
|---|
| 190 | filterstring = optarg; |
|---|
| 191 | break; |
|---|
| 192 | case 'H': |
|---|
| 193 | usage(argv[0]); |
|---|
| 194 | break; |
|---|
| 195 | case 'l': |
|---|
| 196 | if (locality_init(optarg) > 0) |
|---|
| 197 | reports_required |= REPORT_TYPE_LOCALITY; |
|---|
| 198 | case 'm': |
|---|
| 199 | reports_required |= REPORT_TYPE_MISC; |
|---|
| 200 | break; |
|---|
| 201 | case 'n': |
|---|
| 202 | reports_required |= REPORT_TYPE_NLP; |
|---|
| 203 | break; |
|---|
| 204 | case 'O': |
|---|
| 205 | reports_required |= REPORT_TYPE_TCPOPT; |
|---|
| 206 | break; |
|---|
| 207 | case 'o': |
|---|
| 208 | reports_required |= REPORT_TYPE_SYNOPT; |
|---|
| 209 | break; |
|---|
| 210 | case 'P': |
|---|
| 211 | reports_required |= REPORT_TYPE_PROTO; |
|---|
| 212 | break; |
|---|
| 213 | case 'p': |
|---|
| 214 | reports_required |= REPORT_TYPE_PORT; |
|---|
| 215 | break; |
|---|
| 216 | case 's': |
|---|
| 217 | reports_required |= REPORT_TYPE_TCPSEG; |
|---|
| 218 | break; |
|---|
| 219 | case 'T': |
|---|
| 220 | reports_required |= REPORT_TYPE_TOS; |
|---|
| 221 | break; |
|---|
| 222 | case 't': |
|---|
| 223 | reports_required |= REPORT_TYPE_TTL; |
|---|
| 224 | break; |
|---|
| 225 | default: |
|---|
| 226 | usage(argv[0]); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /* Default to all reports, instead of no reports at all. It's annoying |
|---|
| 231 | * waiting for 10 minutes for a trace to process then discover you |
|---|
| 232 | * forgot to ask for any reports! |
|---|
| 233 | */ |
|---|
| 234 | if (reports_required == 0) |
|---|
| 235 | reports_required = ~0; |
|---|
| 236 | |
|---|
| 237 | if (filterstring) { |
|---|
| 238 | filter = trace_create_filter(filterstring); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | for(i=optind;i<argc;++i) { |
|---|
| 243 | /* This is handy for knowing how far through the traceset |
|---|
| 244 | * we are - printing to stderr because we use stdout for |
|---|
| 245 | * genuine output at the moment */ |
|---|
| 246 | fprintf(stderr, "Reading from trace: %s\n", argv[i]); |
|---|
| 247 | run_trace(argv[i],filter,(1<<30)); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if (reports_required & REPORT_TYPE_MISC) |
|---|
| 251 | misc_report(); |
|---|
| 252 | if (reports_required & REPORT_TYPE_ERROR) |
|---|
| 253 | error_report(); |
|---|
| 254 | if (reports_required & REPORT_TYPE_FLOW) |
|---|
| 255 | flow_report(); |
|---|
| 256 | if (reports_required & REPORT_TYPE_TOS) |
|---|
| 257 | tos_report(); |
|---|
| 258 | if (reports_required & REPORT_TYPE_PROTO) |
|---|
| 259 | protocol_report(); |
|---|
| 260 | if (reports_required & REPORT_TYPE_PORT) |
|---|
| 261 | port_report(); |
|---|
| 262 | if (reports_required & REPORT_TYPE_TTL) |
|---|
| 263 | ttl_report(); |
|---|
| 264 | if (reports_required & REPORT_TYPE_TCPOPT) |
|---|
| 265 | tcpopt_report(); |
|---|
| 266 | if (reports_required & REPORT_TYPE_SYNOPT) |
|---|
| 267 | synopt_report(); |
|---|
| 268 | if (reports_required & REPORT_TYPE_NLP) |
|---|
| 269 | nlp_report(); |
|---|
| 270 | if (reports_required & REPORT_TYPE_DIR) |
|---|
| 271 | dir_report(); |
|---|
| 272 | if (reports_required & REPORT_TYPE_ECN) |
|---|
| 273 | ecn_report(); |
|---|
| 274 | if (reports_required & REPORT_TYPE_TCPSEG) |
|---|
| 275 | tcpseg_report(); |
|---|
| 276 | if (reports_required & REPORT_TYPE_LOCALITY) |
|---|
| 277 | locality_report(); |
|---|
| 278 | return 0; |
|---|
| 279 | } |
|---|