1 | /* Trivial libtrace program that writes all SMTP packets from a trace into |
---|
2 | * a new separate trace. |
---|
3 | * |
---|
4 | * Designed to demonstrate the use of the libtrace output API |
---|
5 | */ |
---|
6 | #include "libtrace.h" |
---|
7 | #include <stdio.h> |
---|
8 | #include <inttypes.h> |
---|
9 | #include <assert.h> |
---|
10 | #include <getopt.h> |
---|
11 | |
---|
12 | static int per_packet(libtrace_out_t *output, libtrace_packet_t *packet) |
---|
13 | { |
---|
14 | libtrace_tcp_t *tcp; |
---|
15 | |
---|
16 | /* Get the TCP header using trace_get_tcp() */ |
---|
17 | tcp = trace_get_tcp(packet); |
---|
18 | |
---|
19 | /* If the packet does not have a TCP header, skip it */ |
---|
20 | if (tcp == NULL) |
---|
21 | return 0; |
---|
22 | |
---|
23 | /* Check if either port in the TCP header is 25. Note that we |
---|
24 | * have to byteswap the port numbers because we are reading directly |
---|
25 | * out of the zero-copied packet */ |
---|
26 | if ((ntohs(tcp->source) == 25) || (ntohs(tcp->dest) == 25)) { |
---|
27 | /* If we have a match, write the packet to our output trace, |
---|
28 | * being sure to check for errors */ |
---|
29 | if (trace_write_packet(output, packet) == -1) { |
---|
30 | trace_perror_output(output, "Writing packet"); |
---|
31 | return -1; |
---|
32 | } |
---|
33 | } |
---|
34 | return 0; |
---|
35 | } |
---|
36 | |
---|
37 | /* The cleanup function has now been extended to destroy the output trace as |
---|
38 | * well */ |
---|
39 | static void libtrace_cleanup(libtrace_t *trace, libtrace_out_t *output, |
---|
40 | libtrace_packet_t *packet) { |
---|
41 | |
---|
42 | /* It's very important to ensure that we aren't trying to destroy |
---|
43 | * a NULL structure, so each of the destroy calls will only occur |
---|
44 | * if the structure exists */ |
---|
45 | if (trace) |
---|
46 | trace_destroy(trace); |
---|
47 | |
---|
48 | if (output) |
---|
49 | trace_destroy_output(output); |
---|
50 | |
---|
51 | if (packet) |
---|
52 | trace_destroy_packet(packet); |
---|
53 | |
---|
54 | } |
---|
55 | |
---|
56 | int main(int argc, char *argv[]) |
---|
57 | { |
---|
58 | /* Unlike most of the other example programs, this is not a copy and |
---|
59 | * paste job from readdemo.c as we now also have to initalise and |
---|
60 | * start an output trace too */ |
---|
61 | |
---|
62 | libtrace_t *trace = NULL; |
---|
63 | libtrace_packet_t *packet = NULL; |
---|
64 | libtrace_out_t *output = NULL; |
---|
65 | |
---|
66 | /* Check that we have all the required command line arguments */ |
---|
67 | if (argc < 3) { |
---|
68 | fprintf(stderr, "Usage: %s inputURI outputURI\n", argv[0]); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | /* Creating the packet structure */ |
---|
73 | packet = trace_create_packet(); |
---|
74 | |
---|
75 | if (packet == NULL) { |
---|
76 | perror("Creating libtrace packet"); |
---|
77 | libtrace_cleanup(trace, output, packet); |
---|
78 | return 1; |
---|
79 | } |
---|
80 | |
---|
81 | /* Creating and starting the INPUT trace */ |
---|
82 | trace = trace_create(argv[1]); |
---|
83 | |
---|
84 | if (trace_is_err(trace)) { |
---|
85 | trace_perror(trace,"Opening trace file"); |
---|
86 | libtrace_cleanup(trace, output, packet); |
---|
87 | return 1; |
---|
88 | } |
---|
89 | |
---|
90 | if (trace_start(trace) == -1) { |
---|
91 | trace_perror(trace,"Starting trace"); |
---|
92 | libtrace_cleanup(trace, output, packet); |
---|
93 | return 1; |
---|
94 | } |
---|
95 | |
---|
96 | /* Creating and starting the OUTPUT trace */ |
---|
97 | output = trace_create_output(argv[2]); |
---|
98 | |
---|
99 | if (trace_is_err_output(output)) { |
---|
100 | trace_perror_output(output,"Opening output trace file"); |
---|
101 | libtrace_cleanup(trace, output, packet); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | if (trace_start_output(output) == -1) { |
---|
106 | trace_perror_output(output,"Starting output trace"); |
---|
107 | libtrace_cleanup(trace, output, packet); |
---|
108 | return 1; |
---|
109 | } |
---|
110 | |
---|
111 | while (trace_read_packet(trace,packet)>0) { |
---|
112 | |
---|
113 | /* If something goes wrong when writing packets, we need to |
---|
114 | * catch that error, tidy up and exit */ |
---|
115 | if (per_packet(output, packet) == -1) { |
---|
116 | libtrace_cleanup(trace, output, packet); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /* Checking for any errors that might have occurred while reading the |
---|
122 | * input trace */ |
---|
123 | if (trace_is_err(trace)) { |
---|
124 | trace_perror(trace,"Reading packets"); |
---|
125 | libtrace_cleanup(trace, output, packet); |
---|
126 | return 1; |
---|
127 | } |
---|
128 | |
---|
129 | libtrace_cleanup(trace, output, packet); |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|