source: examples/tutorial/readdemo.c @ 8835f5a

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 8835f5a was 8835f5a, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Added the code examples from the PDCAT tutorial to the bundled examples
  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[8835f5a]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
9uint64_t count = 0;
10
11
12static void per_packet(libtrace_packet_t *packet)
13{
14        /* This function turns out to be really simple, because we are just
15         * counting the number of packets in the trace */
16        count += 1;
17}
18
19/* Due to the amount of error checking required in our main function, it
20 * is a lot simpler and tidier to place all the calls to various libtrace
21 * destroy functions into a separate function.
22 */
23static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) {
24       
25        /* It's very important to ensure that we aren't trying to destroy
26         * a NULL structure, so each of the destroy calls will only occur
27         * if the structure exists */
28        if (trace)
29                trace_destroy(trace);
30       
31        if (packet)
32                trace_destroy_packet(packet);
33
34}
35
36
37int main(int argc, char *argv[])
38{
39        libtrace_t *trace = NULL;
40        libtrace_packet_t *packet = NULL;
41       
42        /* Creating and initialising a packet structure to store the packets
43         * that we're going to read from the trace */
44        packet = trace_create_packet();
45
46        /* Ensure we have at least one argument after the program name */
47        if (argc < 2) {
48                fprintf(stderr, "Usage: %s inputURI\n", argv[0]);
49                return 1;
50        }
51       
52        if (packet == NULL) {
53                /* Unfortunately, trace_create_packet doesn't use the libtrace
54                 * error system. This is because libtrace errors are associated
55                 * with the trace structure, not the packet. In our case, we
56                 * haven't even created a trace at this point so we can't
57                 * really expect libtrace to set an error on it for us, can
58                 * we?
59                 */
60                perror("Creating libtrace packet");
61                libtrace_cleanup(trace, packet);
62                return 1;
63        }
64
65        /* Opening and starting the input trace, as per createdemo.c */
66        trace = trace_create(argv[1]);
67
68        if (trace_is_err(trace)) {
69                trace_perror(trace,"Opening trace file");
70                libtrace_cleanup(trace, packet);
71                return 1;
72        }
73
74        if (trace_start(trace) == -1) {
75                trace_perror(trace,"Starting trace");
76                libtrace_cleanup(trace, packet);
77                return 1;
78        }
79
80        /* This loop will read packets from the trace until either EOF is
81         * reached or an error occurs (hopefully the former!)
82         *
83         * Remember, EOF will return 0 so we only want to continue looping
84         * as long as the return value is greater than zero
85         */
86        while (trace_read_packet(trace,packet)>0) {
87                /* Call our per_packet function for every packet */
88                per_packet(packet);
89        }
90
91        /* If the trace is in an error state, then we know that we fell out of
92         * the above loop because an error occurred rather than EOF being
93         * reached. Therefore, we should probably tell the user that something
94         * went wrong
95         */
96        if (trace_is_err(trace)) {
97                trace_perror(trace,"Reading packets");
98                libtrace_cleanup(trace, packet);
99                return 1;
100        }
101
102        /* We've reached the end of our trace without an error so we can
103         * print our final count. Note the use of the PRIu64 format which is
104         * portable across 64 and 32 bit machines */
105        printf("Packet Count = %" PRIu64 "\n", count);
106       
107        libtrace_cleanup(trace, packet);
108
109        return 0;
110}
Note: See TracBrowser for help on using the repository browser.