1 | /* Trivial libtrace program that counts the number of MPLS packets in a trace. |
---|
2 | * Designed to demonstrate the use of trace_get_payload_from_layer2() |
---|
3 | */ |
---|
4 | #include "libtrace.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <inttypes.h> |
---|
7 | #include <assert.h> |
---|
8 | #include <getopt.h> |
---|
9 | |
---|
10 | uint64_t count = 0; |
---|
11 | |
---|
12 | |
---|
13 | static void per_packet(libtrace_packet_t *packet) |
---|
14 | { |
---|
15 | void *l2 = NULL; |
---|
16 | void *l2_payload = NULL; |
---|
17 | libtrace_linktype_t link_type; |
---|
18 | uint16_t ethertype; |
---|
19 | uint32_t remaining; |
---|
20 | |
---|
21 | /* Jump straight to the link layer header */ |
---|
22 | l2 = trace_get_layer2(packet, &link_type, &remaining); |
---|
23 | |
---|
24 | /* No layer 2 header */ |
---|
25 | if (l2 == NULL) |
---|
26 | return; |
---|
27 | |
---|
28 | /* XXX We could check if link_type is ethernet here, but this won't |
---|
29 | * affect our results in any way */ |
---|
30 | |
---|
31 | |
---|
32 | /* Skip past the initial layer 2 header to look for an MPLS header */ |
---|
33 | l2_payload = trace_get_payload_from_layer2(l2, link_type, ðertype, |
---|
34 | &remaining); |
---|
35 | |
---|
36 | /* Incomplete layer 2 header */ |
---|
37 | if (l2_payload == NULL) |
---|
38 | return; |
---|
39 | |
---|
40 | /* Zero bytes of payload remaining so there is no useful header |
---|
41 | * available */ |
---|
42 | if (remaining == 0) |
---|
43 | return; |
---|
44 | |
---|
45 | /* If the returned header is not an MPLS header, we want to return |
---|
46 | * without incrementing our counter */ |
---|
47 | if (ethertype != 0x8847) |
---|
48 | return; |
---|
49 | |
---|
50 | /* Otherwise, we must have an MPLS tag - increment the counter */ |
---|
51 | count += 1; |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) { |
---|
56 | |
---|
57 | /* It's very important to ensure that we aren't trying to destroy |
---|
58 | * a NULL structure, so each of the destroy calls will only occur |
---|
59 | * if the structure exists */ |
---|
60 | if (trace) |
---|
61 | trace_destroy(trace); |
---|
62 | |
---|
63 | if (packet) |
---|
64 | trace_destroy_packet(packet); |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | int main(int argc, char *argv[]) |
---|
70 | { |
---|
71 | /* This is essentially the same main function from readdemo.c */ |
---|
72 | |
---|
73 | libtrace_t *trace = NULL; |
---|
74 | libtrace_packet_t *packet = NULL; |
---|
75 | |
---|
76 | /* Ensure we have at least one argument after the program name */ |
---|
77 | if (argc < 2) { |
---|
78 | fprintf(stderr, "Usage: %s inputURI\n", argv[0]); |
---|
79 | return 1; |
---|
80 | } |
---|
81 | |
---|
82 | packet = trace_create_packet(); |
---|
83 | |
---|
84 | if (packet == NULL) { |
---|
85 | perror("Creating libtrace packet"); |
---|
86 | libtrace_cleanup(trace, packet); |
---|
87 | return 1; |
---|
88 | } |
---|
89 | |
---|
90 | trace = trace_create(argv[1]); |
---|
91 | |
---|
92 | if (trace_is_err(trace)) { |
---|
93 | trace_perror(trace,"Opening trace file"); |
---|
94 | libtrace_cleanup(trace, packet); |
---|
95 | return 1; |
---|
96 | } |
---|
97 | |
---|
98 | if (trace_start(trace) == -1) { |
---|
99 | trace_perror(trace,"Starting trace"); |
---|
100 | libtrace_cleanup(trace, packet); |
---|
101 | return 1; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | while (trace_read_packet(trace,packet)>0) { |
---|
106 | per_packet(packet); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | if (trace_is_err(trace)) { |
---|
111 | trace_perror(trace,"Reading packets"); |
---|
112 | libtrace_cleanup(trace, packet); |
---|
113 | return 1; |
---|
114 | } |
---|
115 | |
---|
116 | printf("Packet Count = %" PRIu64 "\n", count); |
---|
117 | |
---|
118 | libtrace_cleanup(trace, packet); |
---|
119 | return 0; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|