1 | /* Trivial libtrace program that prints a count of the number of packets |
---|
2 | * observed every 10 seconds in a trace. |
---|
3 | * Designed to demonstrate the use of trace_get_timeval() |
---|
4 | */ |
---|
5 | #include "libtrace.h" |
---|
6 | #include <stdio.h> |
---|
7 | #include <inttypes.h> |
---|
8 | #include <assert.h> |
---|
9 | #include <getopt.h> |
---|
10 | |
---|
11 | uint64_t count = 0; |
---|
12 | uint32_t next_report = 0; |
---|
13 | |
---|
14 | static void per_packet(libtrace_packet_t *packet) |
---|
15 | { |
---|
16 | struct timeval ts; |
---|
17 | |
---|
18 | /* Get the timestamp for the current packet */ |
---|
19 | ts = trace_get_timeval(packet); |
---|
20 | |
---|
21 | /* If next_report is zero, then this is the first packet from the |
---|
22 | * trace so we need to determine the time at which the first report |
---|
23 | * must occur, i.e. 10 seconds from now. */ |
---|
24 | if (next_report == 0) { |
---|
25 | next_report = ts.tv_sec + 10; |
---|
26 | |
---|
27 | /* This is also a good opportunity to print column headings */ |
---|
28 | printf("Time\t\tPackets\n"); |
---|
29 | } |
---|
30 | |
---|
31 | /* Check whether we need to report a packet count or not. |
---|
32 | * |
---|
33 | * If the timestamp for the current packet is beyond the time when the |
---|
34 | * next report was due then we have to output our current count and |
---|
35 | * reset it to zero. |
---|
36 | * |
---|
37 | * Note that I use a while loop here to ensure that we correctly deal |
---|
38 | * with periods in which no packets are observed. |
---|
39 | */ |
---|
40 | while ((uint32_t)ts.tv_sec > next_report) { |
---|
41 | /* Print a timestamp for the report and the packet count */ |
---|
42 | printf("%u \t%" PRIu64 "\n", next_report, count); |
---|
43 | /* Reset the counter */ |
---|
44 | count = 0; |
---|
45 | /* Determine when the next report is due */ |
---|
46 | next_report += 10; |
---|
47 | } |
---|
48 | |
---|
49 | /* No matter what else happens during this function call, we still |
---|
50 | * need to increment our counter */ |
---|
51 | count += 1; |
---|
52 | } |
---|
53 | |
---|
54 | /* Due to the amount of error checking required in our main function, it |
---|
55 | * is a lot simpler and tidier to place all the calls to various libtrace |
---|
56 | * destroy functions into a separate function. |
---|
57 | */ |
---|
58 | static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) { |
---|
59 | |
---|
60 | /* It's very important to ensure that we aren't trying to destroy |
---|
61 | * a NULL structure, so each of the destroy calls will only occur |
---|
62 | * if the structure exists */ |
---|
63 | if (trace) |
---|
64 | trace_destroy(trace); |
---|
65 | |
---|
66 | if (packet) |
---|
67 | trace_destroy_packet(packet); |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | int main(int argc, char *argv[]) |
---|
72 | { |
---|
73 | /* This is essentially the same main function from readdemo.c */ |
---|
74 | |
---|
75 | libtrace_t *trace = NULL; |
---|
76 | libtrace_packet_t *packet = NULL; |
---|
77 | |
---|
78 | /* Ensure we have at least one argument after the program name */ |
---|
79 | if (argc < 2) { |
---|
80 | fprintf(stderr, "Usage: %s inputURI\n", argv[0]); |
---|
81 | return 1; |
---|
82 | } |
---|
83 | |
---|
84 | packet = trace_create_packet(); |
---|
85 | |
---|
86 | if (packet == NULL) { |
---|
87 | perror("Creating libtrace packet"); |
---|
88 | libtrace_cleanup(trace, packet); |
---|
89 | return 1; |
---|
90 | } |
---|
91 | |
---|
92 | trace = trace_create(argv[1]); |
---|
93 | |
---|
94 | if (trace_is_err(trace)) { |
---|
95 | trace_perror(trace,"Opening trace file"); |
---|
96 | libtrace_cleanup(trace, packet); |
---|
97 | return 1; |
---|
98 | } |
---|
99 | |
---|
100 | if (trace_start(trace) == -1) { |
---|
101 | trace_perror(trace,"Starting trace"); |
---|
102 | libtrace_cleanup(trace, packet); |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | while (trace_read_packet(trace,packet)>0) { |
---|
108 | per_packet(packet); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | if (trace_is_err(trace)) { |
---|
113 | trace_perror(trace,"Reading packets"); |
---|
114 | libtrace_cleanup(trace, packet); |
---|
115 | return 1; |
---|
116 | } |
---|
117 | |
---|
118 | printf("%u \t%" PRIu64 "\n", next_report, count); |
---|
119 | |
---|
120 | libtrace_cleanup(trace, packet); |
---|
121 | return 0; |
---|
122 | } |
---|