source: examples/tutorial/configdemo.c @ af27241

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since af27241 was 8835f5a, checked in by Shane Alcock <salcock@…>, 11 years ago
  • Added the code examples from the PDCAT tutorial to the bundled examples
  • Property mode set to 100644
File size: 4.8 KB
Line 
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
10static 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 */
24static 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
44int 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
59        /* Check that we have all the required command line arguments */
60        if (argc < 4) {
61                fprintf(stderr, "Usage: %s inputURI bpffilter outputURI\n", 
62                        argv[0]);
63                return 1;
64        }
65       
66        /* Creating the packet structure */
67        packet = trace_create_packet();
68
69        if (packet == NULL) {
70                perror("Creating libtrace packet");
71                libtrace_cleanup(trace, output, packet, filter);
72                return 1;
73        }
74
75        /* Creating the filter */
76        filter = trace_create_filter(argv[2]);
77        if (filter == NULL) {
78                fprintf(stderr, "Failed to create filter (%s)\n", argv[2]);
79                libtrace_cleanup(trace, output, packet, filter);
80                return 1;
81        }
82       
83        /* Creating and starting the INPUT trace */
84        trace = trace_create(argv[1]);
85
86        if (trace_is_err(trace)) {
87                trace_perror(trace,"Opening trace file");
88                libtrace_cleanup(trace, output, packet, filter);
89                return 1;
90        }
91
92        /* Use the configuration system to tell libtrace to always use this
93         * filter when reading packets. For live captures, this means the
94         * filter will be pushed into the kernel or hardware, improving
95         * the performance over a software filter.
96         *
97         * Note that the configuration is performed BEFORE calling
98         * trace_start().
99         */
100
101        if (trace_config(trace, TRACE_OPTION_FILTER, filter) == -1) {
102                trace_perror(trace, "Configuring filter");
103                libtrace_cleanup(trace, output, packet, filter);
104                return 1;
105        }
106
107        if (trace_start(trace) == -1) {
108                trace_perror(trace,"Starting trace");
109                libtrace_cleanup(trace, output, packet, filter);
110                return 1;
111        }
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        /* We're also going to set a compression level option for the output
124         * trace to ensure that our traces are compressed sensibly.
125         *
126         * Again, this must be done before calling trace_start_output().
127         */
128       
129        if (trace_config_output(output, TRACE_OPTION_OUTPUT_COMPRESS, &level) == -1) {
130                trace_perror_output(output, "Configuring compression level");
131                libtrace_cleanup(trace, output, packet, filter);
132                return 1;
133        }
134
135        if (trace_start_output(output) == -1) {
136                trace_perror_output(output,"Starting output trace");
137                libtrace_cleanup(trace, output, packet, filter);
138                return 1;
139        }
140       
141        while (trace_read_packet(trace,packet)>0) {
142               
143                /* If something goes wrong when writing packets, we need to
144                 * catch that error, tidy up and exit */
145                if (per_packet(output, packet) == -1) {
146                        libtrace_cleanup(trace, output, packet, filter);
147                        return 1;
148                }
149        }
150
151        /* Checking for any errors that might have occurred while reading the
152         * input trace */
153        if (trace_is_err(trace)) {
154                trace_perror(trace,"Reading packets");
155                libtrace_cleanup(trace, output, packet, filter);
156                return 1;
157        }
158
159        libtrace_cleanup(trace, output, packet, filter);
160        return 0;
161}
162
Note: See TracBrowser for help on using the repository browser.