source: examples/tutorial/getpayloaddemo.c @ af27241

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since af27241 was af27241, checked in by Shane Alcock <salcock@…>, 8 years ago
  • Added a couple of new examples that show off trace_get_transport (as an example of a layer access function) and trace_get_payload_from_ip (as an example of a get payload from X function)
  • Property mode set to 100644
File size: 5.2 KB
Line 
1/* Trivial libtrace program that prints the fields in the header for all
2 * UDP packets in a trace. Instead of using trace_get_transport, we're going
3 * to jump to the IP layer and use trace_get_payload_from_ip to find the UDP
4 * header.
5 *
6 * Designed to demonstrate the use of trace_get_payload_from_X functions
7 */
8#include "libtrace.h"
9#include <stdio.h>
10#include <inttypes.h>
11#include <assert.h>
12#include <getopt.h>
13#include <arpa/inet.h>
14
15uint64_t packet_count = 0;
16uint64_t capture_count = 0;
17uint64_t wire_count = 0;
18uint32_t next_report = 0;
19
20static void per_packet(libtrace_packet_t *packet)
21{
22        uint8_t proto;
23        uint16_t ethertype;
24        uint32_t rem;
25        void *ltheader = NULL;
26        libtrace_udp_t *udp = NULL;
27        libtrace_ip_t *ip = NULL;
28        libtrace_ip6_t *ip6 = NULL;
29
30        /* OK, this is slightly tricky so pay attention.
31         *
32         * ethertype and rem are used as 'output' parameters here. What this
33         * means is that their current values are ignored by the function
34         * but they are updated to contain the protocol and amount of
35         * payload remaining for the header that is being returned.
36         *
37         * We need to pass in the address of both these parameters, so
38         * that they can be modified by the function. This is why there
39         * is an '&' before both parameters.
40         */
41        ltheader = trace_get_layer3(packet, &ethertype, &rem);
42
43        /* If there was no layer 3 header, ignore the packet */
44        if (ltheader == NULL)
45                return;
46
47        /* If there is no packet remaining, there is no point in going any
48         * further */
49        if (rem == 0)
50                return;
51
52        /* Ok, we've got a layer 3 header - let's cast it to the appropriate
53         * type and use the appropriate get_payload function to find the next
54         * header.
55         *
56         */
57        if (ethertype == TRACE_ETHERTYPE_IP) {
58                /* Our layer 3 header is IPv4 */
59               
60                /* Cast the returned header to a libtrace_ip_t */
61                ip = (libtrace_ip_t *)ltheader;
62
63                /* Use the get_payload_from_ip function to skip past the IPv4
64                 * header. The key thing here is that rem needs to contain
65                 * the same value that resulted from the earlier call to
66                 * trace_get_layer3 */
67
68                ltheader = trace_get_payload_from_ip(ip, &proto, &rem);
69
70        } else if (ethertype == TRACE_ETHERTYPE_IPV6) {
71                /* Our layer 3 header is IPv6 */
72               
73                /* Cast the returned header to a libtrace_ip6_t */
74                ip6 = (libtrace_ip6_t *)ltheader;
75
76                /* Use the get_payload_from_ip6 function to skip past the IPv6
77                 * header. The key thing here is that rem needs to contain
78                 * the same value that resulted from the earlier call to
79                 * trace_get_layer3 */
80
81                ltheader = trace_get_payload_from_ip6(ip6, &proto, &rem);
82
83        } else {
84                /* Let's ignore any other Layer 3 headers for now */
85                return;
86        }
87
88        /* Check if there was a header present after the layer 3 header. */
89        if (ltheader == NULL)
90                return;
91
92        /* Check if the protocol is UDP, using the defined value for UDP
93         * that is defined in libtrace.h (search for libtrace_ipproto_t for
94         * a full list) */
95        if (proto != TRACE_IPPROTO_UDP)
96                return;
97
98        /* One last check - make sure we have a full UDP header before
99         * trying to grab fields out of it. */
100        if (rem < sizeof(libtrace_udp_t))
101                return;
102
103        /* Now, cast the returned header to the appropriate header type */
104        udp = (libtrace_udp_t *)ltheader;
105
106        /* Dump each field to standard output. Be careful to byteswap any
107         * fields that are larger than one byte, as these will be in network
108         * byte order */
109        printf("UDP: source=%u dest=%u len=%u checksum=%u\n",
110                        ntohs(udp->source), ntohs(udp->dest),
111                        ntohs(udp->len), ntohs(udp->check));
112}
113
114
115/* Due to the amount of error checking required in our main function, it
116 * is a lot simpler and tidier to place all the calls to various libtrace
117 * destroy functions into a separate function.
118 */
119static void libtrace_cleanup(libtrace_t *trace, libtrace_packet_t *packet) {
120       
121        /* It's very important to ensure that we aren't trying to destroy
122         * a NULL structure, so each of the destroy calls will only occur
123         * if the structure exists */
124        if (trace)
125                trace_destroy(trace);
126
127        if (packet)
128                trace_destroy_packet(packet);
129
130}
131
132int main(int argc, char *argv[])
133{
134        /* This is essentially the same main function from readdemo.c */
135       
136        libtrace_t *trace = NULL;
137        libtrace_packet_t *packet = NULL;
138
139        /* Ensure we have at least one argument after the program name */
140        if (argc < 2) {
141                fprintf(stderr, "Usage: %s inputURI\n", argv[0]);
142                return 1;
143        }       
144       
145        packet = trace_create_packet();
146
147        if (packet == NULL) {
148                perror("Creating libtrace packet");
149                libtrace_cleanup(trace, packet);
150                return 1;
151        }
152
153        trace = trace_create(argv[1]);
154
155        if (trace_is_err(trace)) {
156                trace_perror(trace,"Opening trace file");
157                libtrace_cleanup(trace, packet);
158                return 1;
159        }
160
161        if (trace_start(trace) == -1) {
162                trace_perror(trace,"Starting trace");
163                libtrace_cleanup(trace, packet);
164                return 1;
165        }
166
167
168        while (trace_read_packet(trace,packet)>0) {
169                per_packet(packet);
170        }
171
172
173        if (trace_is_err(trace)) {
174                trace_perror(trace,"Reading packets");
175                libtrace_cleanup(trace, packet);
176                return 1;
177        }
178
179        /* Print the stats for the final reporting period that was probably
180         * not complete when the trace finished */
181        printf("%u\t", next_report);
182        printf("%.2f\t\t", ((double)wire_count) / packet_count);
183        printf("%.2f\n", ((double)capture_count) / packet_count);
184
185        libtrace_cleanup(trace, packet);
186        return 0;
187}
Note: See TracBrowser for help on using the repository browser.