source: examples/tutorial/getpayloaddemo.c @ ee0805a

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