-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap-to-dlt-en10mb.c
89 lines (80 loc) · 1.87 KB
/
pcap-to-dlt-en10mb.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pcap.h>
#include <err.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <arpa/inet.h>
#include <pcap_layers.h>
#include "pcap-tools.h"
#ifndef ETHERTYPE_IPV6
#define ETHERTYPE_IPV6 0x86dd
#endif
const char *progname;
struct pcap_pkthdr out_hdr;
u_char out_data[65536];
/*
* this will only be called if 'ip' is a complete IPv header
*/
int
my_ip4_handler(const struct ip *ip4, int len, void *userdata)
{
out_hdr.len = out_hdr.caplen = len + 14;
htonps(&out_data[12], ETHERTYPE_IP);
memcpy(&out_data[14], ip4, len);
return 0;
}
int
my_ip6_handler(const struct ip6_hdr *ip6, int len, void *userdata)
{
out_hdr.len = out_hdr.caplen = len + 14;
htonps(&out_data[12], ETHERTYPE_IPV6);
memcpy(&out_data[14], ip6, len);
return 0;
}
int
main(int argc, char *argv[])
{
pcap_t *in = NULL;
struct pcap_pkthdr hdr;
const u_char *data;
pcap_dumper_t *out = NULL;
pcap_t *dead;
memset(out_data, 0, sizeof(out_data));
progname = argv[0];
if (argc < 2) {
fprintf(stderr, "usage: %s pcapfiles\n", progname);
exit(1);
}
in = my_pcap_open_offline(argv[1]);
dead = pcap_open_dead(DLT_EN10MB, 65536);
if (NULL == dead) {
perror("pcap_open_dead");
exit(1);
}
out = pcap_dump_open(dead, "-");
if (NULL == out) {
perror("stdout");
exit(1);
}
pcap_layers_init(pcap_datalink(in), 0);
callback_ipv4 = my_ip4_handler;
callback_ipv6 = my_ip6_handler;
while ((data = pcap_next(in, &hdr))) {
out_hdr.ts = hdr.ts;
out_hdr.len = 0;
out_hdr.caplen = 0;
handle_pcap(NULL, &hdr, data);
if (out_hdr.len)
pcap_dump((void *) out, &out_hdr, out_data);
}
my_pcap_close_offline(in);
pcap_dump_close(out);
exit(0);
}