-
Notifications
You must be signed in to change notification settings - Fork 1
/
decap_vxlan.c
60 lines (50 loc) · 1.74 KB
/
decap_vxlan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <pcap.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#define VXLAN_PORT 4789
#define VXLAN_HDR_SIZE 8
int main(int argc, char *argv[]) {
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header;
const u_char *packet;
pcap_t *handle;
pcap_dumper_t *outfile;
handle = pcap_open_offline("-", errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open pcap file: %s\n", errbuf);
return(2);
}
outfile = pcap_dump_open(handle, "-");
if (outfile == NULL) {
fprintf(stderr, "Couldn't open output file: %s\n", pcap_geterr(handle));
return(2);
}
while ((packet = pcap_next(handle, &header))) {
struct ether_header *eth_header;
struct ip *ip_header;
struct udphdr *udp_header;
eth_header = (struct ether_header *) packet;
if (ntohs(eth_header->ether_type) != ETHERTYPE_IP) {
continue;
}
ip_header = (struct ip *)(packet + ETHER_HDR_LEN);
if (ip_header->ip_p != IPPROTO_UDP) {
continue;
}
udp_header = (struct udphdr *)(packet + ETHER_HDR_LEN + ip_header->ip_hl * 4);
if (ntohs(udp_header->uh_dport) != VXLAN_PORT) {
continue;
}
u_char *vxlan_packet = (u_char *)(packet + ETHER_HDR_LEN + ip_header->ip_hl * 4 + sizeof(struct udphdr) + VXLAN_HDR_SIZE);
struct pcap_pkthdr vxlan_header = header;
vxlan_header.caplen -= (vxlan_packet - packet);
vxlan_header.len -= (vxlan_packet - packet);
pcap_dump((u_char *)outfile, &vxlan_header, vxlan_packet);
}
pcap_dump_close(outfile);
pcap_close(handle);
return(0);
}