1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2015 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id: test-rtclient.c,v 1.2 2006/02/27 03:41:12 perry Exp $ |
---|
28 | * |
---|
29 | */ |
---|
30 | #ifndef WIN32 |
---|
31 | # include <sys/time.h> |
---|
32 | # include <netinet/in.h> |
---|
33 | # include <netinet/in_systm.h> |
---|
34 | # include <netinet/tcp.h> |
---|
35 | # include <netinet/ip.h> |
---|
36 | # include <netinet/ip_icmp.h> |
---|
37 | # include <arpa/inet.h> |
---|
38 | # include <sys/socket.h> |
---|
39 | #endif |
---|
40 | #include <stdio.h> |
---|
41 | #include <stdlib.h> |
---|
42 | #include <assert.h> |
---|
43 | #include <string.h> |
---|
44 | #include <sys/types.h> |
---|
45 | #include <time.h> |
---|
46 | #include <string.h> |
---|
47 | |
---|
48 | #include "dagformat.h" |
---|
49 | #include "libtrace.h" |
---|
50 | #include "libpacketdump.h" |
---|
51 | |
---|
52 | void iferr(libtrace_t *trace) |
---|
53 | { |
---|
54 | libtrace_err_t err = trace_get_err(trace); |
---|
55 | if (err.err_num==0) |
---|
56 | return; |
---|
57 | printf("Error: %s\n",err.problem); |
---|
58 | exit(1); |
---|
59 | } |
---|
60 | |
---|
61 | int main(int argc, char *argv[]) { |
---|
62 | int psize = 0; |
---|
63 | int error = 0; |
---|
64 | int ip_count = 0; |
---|
65 | int arp_count = 0; |
---|
66 | libtrace_t *trace; |
---|
67 | libtrace_packet_t *packet; |
---|
68 | |
---|
69 | (void)argc; |
---|
70 | (void)argv; |
---|
71 | |
---|
72 | trace = trace_create("pcapfile:traces/vxlan.pcap"); |
---|
73 | iferr(trace); |
---|
74 | |
---|
75 | trace_start(trace); |
---|
76 | iferr(trace); |
---|
77 | |
---|
78 | packet=trace_create_packet(); |
---|
79 | for (;;) { |
---|
80 | uint8_t proto; |
---|
81 | uint32_t remaining; |
---|
82 | void *transport; |
---|
83 | void *layer2; |
---|
84 | void *vxlan; |
---|
85 | |
---|
86 | if ((psize = trace_read_packet(trace, packet)) < 0) { |
---|
87 | error = 1; |
---|
88 | iferr(trace); |
---|
89 | break; |
---|
90 | } |
---|
91 | if (psize == 0) { |
---|
92 | break; |
---|
93 | } |
---|
94 | |
---|
95 | transport = trace_get_transport(packet, &proto, &remaining); |
---|
96 | if (proto != TRACE_IPPROTO_UDP) { |
---|
97 | printf("Failed to find a UDP header\n"); |
---|
98 | error = 1; |
---|
99 | continue; |
---|
100 | } |
---|
101 | |
---|
102 | vxlan = trace_get_vxlan_from_udp(transport, &remaining); |
---|
103 | if (!vxlan) { |
---|
104 | printf("Failed to find a VXLAN header\n"); |
---|
105 | error = 1; |
---|
106 | continue; |
---|
107 | } |
---|
108 | |
---|
109 | layer2 = trace_get_payload_from_vxlan(vxlan, &remaining); |
---|
110 | |
---|
111 | switch (ntohs(((libtrace_ether_t *)layer2)->ether_type)) { |
---|
112 | case 0x0800: |
---|
113 | ip_count++; |
---|
114 | break; |
---|
115 | case 0x0806: |
---|
116 | arp_count++; |
---|
117 | break; |
---|
118 | default: |
---|
119 | fprintf(stderr, "Unexpected vxlan ethertype: %08x\n", |
---|
120 | ntohs(((libtrace_ether_t *)layer2)->ether_type)); |
---|
121 | error = 1; |
---|
122 | continue; |
---|
123 | } |
---|
124 | } |
---|
125 | trace_destroy_packet(packet); |
---|
126 | if (ip_count != 8 || arp_count != 2) { |
---|
127 | fprintf(stderr, "Incorrect number of ip/arp packets\n"); |
---|
128 | error = 1; |
---|
129 | } |
---|
130 | if (error == 0) { |
---|
131 | printf("success\n"); |
---|
132 | } else { |
---|
133 | iferr(trace); |
---|
134 | } |
---|
135 | trace_destroy(trace); |
---|
136 | return error; |
---|
137 | } |
---|