1 | /* Trivial libtrace program that prints all the MPLS tags within a packet. |
---|
2 | * Designed to demonstrate the use of trace_get_payload_from_mpls() |
---|
3 | */ |
---|
4 | #include "libtrace.h" |
---|
5 | #include <stdio.h> |
---|
6 | #include <inttypes.h> |
---|
7 | #include <assert.h> |
---|
8 | #include <getopt.h> |
---|
9 | |
---|
10 | static void print_mpls_label(void *mpls, uint32_t remaining) { |
---|
11 | uint32_t label = 0; |
---|
12 | if (remaining < 4) |
---|
13 | return; |
---|
14 | |
---|
15 | /* Cast MPLS header into a 32 bit number */ |
---|
16 | label = *(uint32_t *)mpls; |
---|
17 | /* Label number is in network byte order */ |
---|
18 | label = ntohl(label); |
---|
19 | /* MPLS labels are comprise only 20 of the 32 bits in the header */ |
---|
20 | label = (label >> 12) & 0x000fffff; |
---|
21 | |
---|
22 | printf("%u ", label); |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | static void per_packet(libtrace_packet_t *packet) |
---|
27 | { |
---|
28 | void *l2 = NULL; |
---|
29 | void *l2_payload = NULL; |
---|
30 | libtrace_linktype_t link_type; |
---|
31 | uint16_t ethertype; |
---|
32 | uint32_t remaining; |
---|
33 | |
---|
34 | l2 = trace_get_layer2(packet, &link_type, &remaining); |
---|
35 | |
---|
36 | /* No layer 2 header */ |
---|
37 | if (l2 == NULL) |
---|
38 | return; |
---|
39 | |
---|
40 | |
---|
41 | /* XXX We could check if link_type is ethernet here, but this won't |
---|
42 | * affect our results in any way */ |
---|
43 | l2_payload = trace_get_payload_from_layer2(l2, link_type, ðertype, |
---|
44 | &remaining); |
---|
45 | |
---|
46 | /* Incomplete layer 2 header */ |
---|
47 | if (l2_payload == NULL) |
---|
48 | return; |
---|
49 | |
---|
50 | /* No actual payload after the layer 2 header */ |
---|
51 | if (remaining == 0) |
---|
52 | return; |
---|
53 | |
---|
54 | /* Only look for MPLS labels as long as we have payload available |
---|
55 | * to look at! */ |
---|
56 | while (l2_payload != NULL && remaining > 0) { |
---|
57 | |
---|
58 | /* 0x8847 is the ethertype for MPLS headers */ |
---|
59 | if (ethertype == 0x8847) { |
---|
60 | print_mpls_label(l2_payload, remaining); |
---|
61 | /* Move onto the next header */ |
---|
62 | l2_payload = trace_get_payload_from_mpls(l2_payload, |
---|
63 | ðertype, &remaining); |
---|
64 | } else |
---|
65 | /* Stop as soon as we encounter a non-MPLS header */ |
---|
66 | break; |
---|
67 | } |
---|
68 | |
---|
69 | printf("\n"); |
---|
70 | |
---|
71 | } |
---|
72 | static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) { |
---|
73 | |
---|
74 | /* It's very important to ensure that we aren't trying to destroy |
---|
75 | * a NULL structure, so each of the destroy calls will only occur |
---|
76 | * if the structure exists */ |
---|
77 | if (trace) |
---|
78 | trace_destroy(trace); |
---|
79 | |
---|
80 | if (packet) |
---|
81 | trace_destroy_packet(packet); |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | int main(int argc, char *argv[]) |
---|
86 | { |
---|
87 | /* This is essentially the same main function from readdemo.c */ |
---|
88 | |
---|
89 | libtrace_t *trace = NULL; |
---|
90 | libtrace_packet_t *packet = NULL; |
---|
91 | |
---|
92 | /* Ensure we have at least one argument after the program name */ |
---|
93 | if (argc < 2) { |
---|
94 | fprintf(stderr, "Usage: %s inputURI\n", argv[0]); |
---|
95 | return 1; |
---|
96 | } |
---|
97 | |
---|
98 | packet = trace_create_packet(); |
---|
99 | |
---|
100 | if (packet == NULL) { |
---|
101 | perror("Creating libtrace packet"); |
---|
102 | libtrace_cleanup(trace, packet); |
---|
103 | return 1; |
---|
104 | } |
---|
105 | |
---|
106 | trace = trace_create(argv[1]); |
---|
107 | |
---|
108 | if (trace_is_err(trace)) { |
---|
109 | trace_perror(trace,"Opening trace file"); |
---|
110 | libtrace_cleanup(trace, packet); |
---|
111 | return 1; |
---|
112 | } |
---|
113 | |
---|
114 | if (trace_start(trace) == -1) { |
---|
115 | trace_perror(trace,"Starting trace"); |
---|
116 | libtrace_cleanup(trace, packet); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | while (trace_read_packet(trace,packet)>0) { |
---|
122 | per_packet(packet); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | if (trace_is_err(trace)) { |
---|
127 | trace_perror(trace,"Reading packets"); |
---|
128 | libtrace_cleanup(trace, packet); |
---|
129 | return 1; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | libtrace_cleanup(trace, packet); |
---|
134 | return 0; |
---|
135 | } |
---|
136 | |
---|