1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | #include <libtrace.h> |
---|
29 | #include <err.h> |
---|
30 | #include <time.h> |
---|
31 | #include "libpacketdump.h" |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <getopt.h> |
---|
35 | |
---|
36 | void usage(char *argv0) |
---|
37 | { |
---|
38 | fprintf(stderr,"Usage:\n" |
---|
39 | "%s flags inputfile\n" |
---|
40 | "-f --filter=expr BPF filter specification, quoted\n" |
---|
41 | "-c --count=num terminate after num packets\n" |
---|
42 | "-H --libtrace-help Print libtrace runtime documentation\n" |
---|
43 | ,argv0); |
---|
44 | exit(0); |
---|
45 | } |
---|
46 | |
---|
47 | int main(int argc,char **argv) |
---|
48 | { |
---|
49 | struct libtrace_t *trace = NULL; |
---|
50 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
51 | struct libtrace_filter_t *filter=NULL; |
---|
52 | uint64_t count=0; |
---|
53 | uint64_t numpackets=0; |
---|
54 | |
---|
55 | |
---|
56 | if (argc<2) |
---|
57 | usage(argv[0]); |
---|
58 | |
---|
59 | while(1) { |
---|
60 | int option_index; |
---|
61 | struct option long_options[] = { |
---|
62 | { "filter", 1, 0, 'f' }, |
---|
63 | { "count", 1, 0, 'c' }, |
---|
64 | { "libtrace-help", 0, 0, 'H' }, |
---|
65 | { NULL, 0, 0, 0 }, |
---|
66 | }; |
---|
67 | |
---|
68 | int c=getopt_long(argc,argv,"f:c:H", |
---|
69 | long_options, &option_index); |
---|
70 | if (c == -1) |
---|
71 | break; |
---|
72 | switch(c) { |
---|
73 | case 'f': |
---|
74 | if (filter!=NULL) { |
---|
75 | fprintf(stderr,"You can only have one filter\n"); |
---|
76 | usage(argv[0]); |
---|
77 | } |
---|
78 | filter=trace_create_filter(optarg); |
---|
79 | break; |
---|
80 | case 'c': count=atol(optarg); break; |
---|
81 | case 'H': |
---|
82 | trace_help(); |
---|
83 | exit(1); |
---|
84 | break; |
---|
85 | default: |
---|
86 | printf("unknown option: %c\n",c); |
---|
87 | usage(argv[0]); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | while(optind <argc) { |
---|
94 | trace = trace_create(argv[optind]); |
---|
95 | optind ++; |
---|
96 | numpackets = 0; |
---|
97 | if (trace_is_err(trace)) { |
---|
98 | trace_perror(trace,"trace_create"); |
---|
99 | trace_destroy(trace); |
---|
100 | continue; |
---|
101 | } |
---|
102 | |
---|
103 | trace_start(trace); |
---|
104 | if (trace_is_err(trace)) { |
---|
105 | trace_perror(trace,"trace_start"); |
---|
106 | trace_destroy(trace); |
---|
107 | continue; |
---|
108 | } |
---|
109 | while(trace_read_packet(trace,packet)> 0 ){ |
---|
110 | if (filter && !trace_apply_filter(filter,packet)) |
---|
111 | continue; |
---|
112 | if (packet->type < TRACE_RT_DATA_SIMPLE) |
---|
113 | /* Ignore RT messages */ |
---|
114 | continue; |
---|
115 | trace_dump_packet(packet); |
---|
116 | |
---|
117 | if(count) { |
---|
118 | numpackets++; |
---|
119 | if (numpackets == count) |
---|
120 | break; |
---|
121 | } |
---|
122 | } |
---|
123 | printf("\n"); |
---|
124 | |
---|
125 | if (trace_is_err(trace)) { |
---|
126 | trace_perror(trace, "trace_read_packet"); |
---|
127 | } |
---|
128 | trace_destroy(trace); |
---|
129 | } |
---|
130 | return 0; |
---|
131 | } |
---|