1 | /* Simple implementation of tracefilter that demonstrates the use of the |
---|
2 | * filter creation and application functions |
---|
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 | libtrace_filter_t *filter) |
---|
12 | { |
---|
13 | int ret; |
---|
14 | |
---|
15 | /* Apply the filter to the packet */ |
---|
16 | ret = trace_apply_filter(filter, packet); |
---|
17 | |
---|
18 | /* Check for any errors that occur during the filtering process */ |
---|
19 | if (ret == -1) { |
---|
20 | fprintf(stderr, "Error applying filter\n"); |
---|
21 | return -1; |
---|
22 | } |
---|
23 | |
---|
24 | /* If we get a return value of zero, the packet did not match the |
---|
25 | * filter so we want to return immediately |
---|
26 | */ |
---|
27 | if (ret == 0) |
---|
28 | return 0; |
---|
29 | |
---|
30 | /* Otherwise, the packet matched our filter so we should write it to |
---|
31 | * our output trace */ |
---|
32 | if (trace_write_packet(output, packet) == -1) { |
---|
33 | trace_perror_output(output, "Writing packet"); |
---|
34 | return -1; |
---|
35 | } |
---|
36 | |
---|
37 | return 0; |
---|
38 | } |
---|
39 | |
---|
40 | /* The cleanup function has now been extended to destroy the filter and |
---|
41 | * output trace as well */ |
---|
42 | static void libtrace_cleanup(libtrace_t *trace, libtrace_out_t *output, |
---|
43 | libtrace_packet_t *packet, libtrace_filter_t *filter) { |
---|
44 | |
---|
45 | /* It's very important to ensure that we aren't trying to destroy |
---|
46 | * a NULL structure, so each of the destroy calls will only occur |
---|
47 | * if the structure exists */ |
---|
48 | if (trace) |
---|
49 | trace_destroy(trace); |
---|
50 | |
---|
51 | if (output) |
---|
52 | trace_destroy_output(output); |
---|
53 | |
---|
54 | if (packet) |
---|
55 | trace_destroy_packet(packet); |
---|
56 | |
---|
57 | if (filter) |
---|
58 | trace_destroy_filter(filter); |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | int main(int argc, char *argv[]) |
---|
63 | { |
---|
64 | /* Unlike most of the other example programs, this is not a copy and |
---|
65 | * paste job from readdemo.c as we now also have to initalise and |
---|
66 | * start an output trace too */ |
---|
67 | |
---|
68 | /* On top of that, we now have to manage a filter as well */ |
---|
69 | |
---|
70 | libtrace_t *trace = NULL; |
---|
71 | libtrace_packet_t *packet = NULL; |
---|
72 | libtrace_out_t *output = NULL; |
---|
73 | libtrace_filter_t *filter = NULL; |
---|
74 | |
---|
75 | /* Check that we have all the required command line arguments */ |
---|
76 | if (argc < 4) { |
---|
77 | fprintf(stderr, "Usage: %s inputURI bpffilter outputURI\n", |
---|
78 | argv[0]); |
---|
79 | return 1; |
---|
80 | } |
---|
81 | |
---|
82 | /* Creating the packet structure */ |
---|
83 | packet = trace_create_packet(); |
---|
84 | |
---|
85 | if (packet == NULL) { |
---|
86 | perror("Creating libtrace packet"); |
---|
87 | libtrace_cleanup(trace, output, packet, filter); |
---|
88 | return 1; |
---|
89 | } |
---|
90 | |
---|
91 | /* Creating and starting the INPUT trace */ |
---|
92 | trace = trace_create(argv[1]); |
---|
93 | |
---|
94 | if (trace_is_err(trace)) { |
---|
95 | trace_perror(trace,"Opening trace file"); |
---|
96 | libtrace_cleanup(trace, output, packet, filter); |
---|
97 | return 1; |
---|
98 | } |
---|
99 | |
---|
100 | if (trace_start(trace) == -1) { |
---|
101 | trace_perror(trace,"Starting trace"); |
---|
102 | libtrace_cleanup(trace, output, packet, filter); |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | /* Creating the filter */ |
---|
107 | filter = trace_create_filter(argv[2]); |
---|
108 | if (filter == NULL) { |
---|
109 | fprintf(stderr, "Failed to create filter (%s)\n", argv[2]); |
---|
110 | libtrace_cleanup(trace, output, packet, filter); |
---|
111 | return 1; |
---|
112 | } |
---|
113 | |
---|
114 | /* Creating and starting the OUTPUT trace */ |
---|
115 | output = trace_create_output(argv[3]); |
---|
116 | |
---|
117 | if (trace_is_err_output(output)) { |
---|
118 | trace_perror_output(output,"Opening output trace file"); |
---|
119 | libtrace_cleanup(trace, output, packet, filter); |
---|
120 | return 1; |
---|
121 | } |
---|
122 | |
---|
123 | if (trace_start_output(output) == -1) { |
---|
124 | trace_perror_output(output,"Starting output trace"); |
---|
125 | libtrace_cleanup(trace, output, packet, filter); |
---|
126 | return 1; |
---|
127 | } |
---|
128 | |
---|
129 | while (trace_read_packet(trace,packet)>0) { |
---|
130 | |
---|
131 | /* If something goes wrong when writing packets, we need to |
---|
132 | * catch that error, tidy up and exit */ |
---|
133 | if (per_packet(output, packet, filter) == -1) { |
---|
134 | libtrace_cleanup(trace, output, packet, filter); |
---|
135 | return 1; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /* Checking for any errors that might have occurred while reading the |
---|
140 | * input trace */ |
---|
141 | if (trace_is_err(trace)) { |
---|
142 | trace_perror(trace,"Reading packets"); |
---|
143 | libtrace_cleanup(trace, output, packet, filter); |
---|
144 | return 1; |
---|
145 | } |
---|
146 | |
---|
147 | libtrace_cleanup(trace, output, packet, filter); |
---|
148 | return 0; |
---|
149 | } |
---|
150 | |
---|