1 | /* Trivial libtrace program that counts the number of packets in a trace. |
---|
2 | * Designed to demonstrate the use of trace_read_packet() |
---|
3 | */ |
---|
4 | #include "libtrace.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <inttypes.h> |
---|
7 | #include <err.h> |
---|
8 | #include <assert.h> |
---|
9 | |
---|
10 | uint64_t count = 0; |
---|
11 | |
---|
12 | |
---|
13 | static void per_packet(libtrace_packet_t *packet) |
---|
14 | { |
---|
15 | assert(packet); |
---|
16 | /* This function turns out to be really simple, because we are just |
---|
17 | * counting the number of packets in the trace */ |
---|
18 | count += 1; |
---|
19 | } |
---|
20 | |
---|
21 | /* Due to the amount of error checking required in our main function, it |
---|
22 | * is a lot simpler and tidier to place all the calls to various libtrace |
---|
23 | * destroy functions into a separate function. |
---|
24 | */ |
---|
25 | static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) { |
---|
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 (packet) |
---|
34 | trace_destroy_packet(packet); |
---|
35 | |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | int main(int argc, char *argv[]) |
---|
40 | { |
---|
41 | libtrace_t *trace = NULL; |
---|
42 | libtrace_packet_t *packet = NULL; |
---|
43 | |
---|
44 | /* Creating and initialising a packet structure to store the packets |
---|
45 | * that we're going to read from the trace */ |
---|
46 | packet = trace_create_packet(); |
---|
47 | |
---|
48 | /* Ensure we have at least one argument after the program name */ |
---|
49 | if (argc < 2) { |
---|
50 | fprintf(stderr, "Usage: %s inputURI\n", argv[0]); |
---|
51 | return 1; |
---|
52 | } |
---|
53 | |
---|
54 | if (packet == NULL) { |
---|
55 | /* Unfortunately, trace_create_packet doesn't use the libtrace |
---|
56 | * error system. This is because libtrace errors are associated |
---|
57 | * with the trace structure, not the packet. In our case, we |
---|
58 | * haven't even created a trace at this point so we can't |
---|
59 | * really expect libtrace to set an error on it for us, can |
---|
60 | * we? |
---|
61 | */ |
---|
62 | perror("Creating libtrace packet"); |
---|
63 | libtrace_cleanup(trace, packet); |
---|
64 | return 1; |
---|
65 | } |
---|
66 | |
---|
67 | /* Opening and starting the input trace, as per createdemo.c */ |
---|
68 | trace = trace_create(argv[1]); |
---|
69 | |
---|
70 | if (trace_is_err(trace)) { |
---|
71 | trace_perror(trace,"Opening trace file"); |
---|
72 | libtrace_cleanup(trace, packet); |
---|
73 | return 1; |
---|
74 | } |
---|
75 | |
---|
76 | if (trace_start(trace) == -1) { |
---|
77 | trace_perror(trace,"Starting trace"); |
---|
78 | libtrace_cleanup(trace, packet); |
---|
79 | return 1; |
---|
80 | } |
---|
81 | |
---|
82 | /* This loop will read packets from the trace until either EOF is |
---|
83 | * reached or an error occurs (hopefully the former!) |
---|
84 | * |
---|
85 | * Remember, EOF will return 0 so we only want to continue looping |
---|
86 | * as long as the return value is greater than zero |
---|
87 | */ |
---|
88 | while (trace_read_packet(trace,packet)>0) { |
---|
89 | /* Call our per_packet function for every packet */ |
---|
90 | per_packet(packet); |
---|
91 | } |
---|
92 | |
---|
93 | /* If the trace is in an error state, then we know that we fell out of |
---|
94 | * the above loop because an error occurred rather than EOF being |
---|
95 | * reached. Therefore, we should probably tell the user that something |
---|
96 | * went wrong |
---|
97 | */ |
---|
98 | if (trace_is_err(trace)) { |
---|
99 | trace_perror(trace,"Reading packets"); |
---|
100 | libtrace_cleanup(trace, packet); |
---|
101 | return 1; |
---|
102 | } |
---|
103 | |
---|
104 | /* We've reached the end of our trace without an error so we can |
---|
105 | * print our final count. Note the use of the PRIu64 format which is |
---|
106 | * portable across 64 and 32 bit machines */ |
---|
107 | printf("Packet Count = %" PRIu64 "\n", count); |
---|
108 | |
---|
109 | libtrace_cleanup(trace, packet); |
---|
110 | |
---|
111 | return 0; |
---|
112 | } |
---|