1 | /* Another implementation of tracefilter that demonstrates the use of the |
---|
2 | * configuration system for traces |
---|
3 | */ |
---|
4 | #include "libtrace.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <inttypes.h> |
---|
7 | #include <assert.h> |
---|
8 | #include <getopt.h> |
---|
9 | |
---|
10 | static int per_packet(libtrace_out_t *output, libtrace_packet_t *packet) |
---|
11 | { |
---|
12 | /* All packets that reach this function must have matched the filter |
---|
13 | * so we can write them out immediately */ |
---|
14 | if (trace_write_packet(output, packet) == -1) { |
---|
15 | trace_perror_output(output, "Writing packet"); |
---|
16 | return -1; |
---|
17 | } |
---|
18 | |
---|
19 | return 0; |
---|
20 | } |
---|
21 | |
---|
22 | /* The cleanup function has now been extended to destroy the filter and |
---|
23 | * output trace as well */ |
---|
24 | static void libtrace_cleanup(libtrace_t *trace, libtrace_out_t *output, |
---|
25 | libtrace_packet_t *packet, libtrace_filter_t *filter) { |
---|
26 | |
---|
27 | /* It's very important to ensure that we aren't trying to destroy |
---|
28 | * a NULL structure, so each of the destroy calls will only occur |
---|
29 | * if the structure exists */ |
---|
30 | if (trace) |
---|
31 | trace_destroy(trace); |
---|
32 | |
---|
33 | if (output) |
---|
34 | trace_destroy_output(output); |
---|
35 | |
---|
36 | if (packet) |
---|
37 | trace_destroy_packet(packet); |
---|
38 | |
---|
39 | if (filter) |
---|
40 | trace_destroy_filter(filter); |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | int main(int argc, char *argv[]) |
---|
45 | { |
---|
46 | /* Unlike most of the other example programs, this is not a copy and |
---|
47 | * paste job from readdemo.c as we now also have to initalise and |
---|
48 | * start an output trace too */ |
---|
49 | |
---|
50 | /* On top of that, we now have to manage a filter as well */ |
---|
51 | |
---|
52 | libtrace_t *trace = NULL; |
---|
53 | libtrace_packet_t *packet = NULL; |
---|
54 | libtrace_out_t *output = NULL; |
---|
55 | libtrace_filter_t *filter = NULL; |
---|
56 | |
---|
57 | int level = 6; |
---|
58 | trace_option_compresstype_t method = TRACE_OPTION_COMPRESSTYPE_ZLIB; |
---|
59 | |
---|
60 | /* Check that we have all the required command line arguments */ |
---|
61 | if (argc < 4) { |
---|
62 | fprintf(stderr, "Usage: %s inputURI bpffilter outputURI\n", |
---|
63 | argv[0]); |
---|
64 | return 1; |
---|
65 | } |
---|
66 | |
---|
67 | /* Creating the packet structure */ |
---|
68 | packet = trace_create_packet(); |
---|
69 | |
---|
70 | if (packet == NULL) { |
---|
71 | perror("Creating libtrace packet"); |
---|
72 | libtrace_cleanup(trace, output, packet, filter); |
---|
73 | return 1; |
---|
74 | } |
---|
75 | |
---|
76 | /* Creating the filter */ |
---|
77 | filter = trace_create_filter(argv[2]); |
---|
78 | if (filter == NULL) { |
---|
79 | fprintf(stderr, "Failed to create filter (%s)\n", argv[2]); |
---|
80 | libtrace_cleanup(trace, output, packet, filter); |
---|
81 | return 1; |
---|
82 | } |
---|
83 | |
---|
84 | /* Creating and starting the INPUT trace */ |
---|
85 | trace = trace_create(argv[1]); |
---|
86 | |
---|
87 | if (trace_is_err(trace)) { |
---|
88 | trace_perror(trace,"Opening trace file"); |
---|
89 | libtrace_cleanup(trace, output, packet, filter); |
---|
90 | return 1; |
---|
91 | } |
---|
92 | |
---|
93 | /* Use the configuration system to tell libtrace to always use this |
---|
94 | * filter when reading packets. For live captures, this means the |
---|
95 | * filter will be pushed into the kernel or hardware, improving |
---|
96 | * the performance over a software filter. |
---|
97 | * |
---|
98 | * Note that the configuration is performed BEFORE calling |
---|
99 | * trace_start(). |
---|
100 | */ |
---|
101 | |
---|
102 | if (trace_config(trace, TRACE_OPTION_FILTER, filter) == -1) { |
---|
103 | trace_perror(trace, "Configuring filter"); |
---|
104 | libtrace_cleanup(trace, output, packet, filter); |
---|
105 | return 1; |
---|
106 | } |
---|
107 | |
---|
108 | if (trace_start(trace) == -1) { |
---|
109 | trace_perror(trace,"Starting trace"); |
---|
110 | libtrace_cleanup(trace, output, packet, filter); |
---|
111 | return 1; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /* Creating and starting the OUTPUT trace */ |
---|
116 | output = trace_create_output(argv[3]); |
---|
117 | |
---|
118 | if (trace_is_err_output(output)) { |
---|
119 | trace_perror_output(output,"Opening output trace file"); |
---|
120 | libtrace_cleanup(trace, output, packet, filter); |
---|
121 | return 1; |
---|
122 | } |
---|
123 | |
---|
124 | /* We want to write compressed output, so tell libtrace which |
---|
125 | * compression format to use for our output trace. |
---|
126 | * |
---|
127 | * Not configuring this will result in uncompressed output, |
---|
128 | * regardless of whether a compression level is set or not. This |
---|
129 | * is different behaviour to earlier versions of libtrace where |
---|
130 | * the default was to produce a compressed file. |
---|
131 | */ |
---|
132 | if (trace_config_output(output, TRACE_OPTION_OUTPUT_COMPRESSTYPE, |
---|
133 | &method) == -1) { |
---|
134 | trace_perror_output(output, "Configuring compression method"); |
---|
135 | libtrace_cleanup(trace, output, packet, filter); |
---|
136 | return 1; |
---|
137 | } |
---|
138 | |
---|
139 | /* We're also going to set a compression level option for the output |
---|
140 | * trace to ensure that our traces are compressed sensibly. |
---|
141 | * |
---|
142 | * Again, this must be done before calling trace_start_output(). |
---|
143 | */ |
---|
144 | |
---|
145 | if (trace_config_output(output, TRACE_OPTION_OUTPUT_COMPRESS, &level) == -1) { |
---|
146 | trace_perror_output(output, "Configuring compression level"); |
---|
147 | libtrace_cleanup(trace, output, packet, filter); |
---|
148 | return 1; |
---|
149 | } |
---|
150 | |
---|
151 | if (trace_start_output(output) == -1) { |
---|
152 | trace_perror_output(output,"Starting output trace"); |
---|
153 | libtrace_cleanup(trace, output, packet, filter); |
---|
154 | return 1; |
---|
155 | } |
---|
156 | |
---|
157 | while (trace_read_packet(trace,packet)>0) { |
---|
158 | |
---|
159 | /* If something goes wrong when writing packets, we need to |
---|
160 | * catch that error, tidy up and exit */ |
---|
161 | if (per_packet(output, packet) == -1) { |
---|
162 | libtrace_cleanup(trace, output, packet, filter); |
---|
163 | return 1; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /* Checking for any errors that might have occurred while reading the |
---|
168 | * input trace */ |
---|
169 | if (trace_is_err(trace)) { |
---|
170 | trace_perror(trace,"Reading packets"); |
---|
171 | libtrace_cleanup(trace, output, packet, filter); |
---|
172 | return 1; |
---|
173 | } |
---|
174 | |
---|
175 | libtrace_cleanup(trace, output, packet, filter); |
---|
176 | return 0; |
---|
177 | } |
---|
178 | |
---|