-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcap-fix-linux-af-inet6.c
108 lines (98 loc) · 1.94 KB
/
pcap-fix-linux-af-inet6.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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 <getopt.h>
#include <arpa/inet.h>
#include "pcap-tools.h"
char *progname = NULL;
int
needs_fixing(const struct pcap_pkthdr *hdr, const u_char * data)
{
unsigned int family;
if (hdr->caplen < 4)
return 0;
family = nptohl(data);
if (10 == family)
return 1;
return 0;
}
void
fix(const u_char * data)
{
unsigned int family = htonl(28);
memcpy((void *) data, &family, sizeof(family));
}
void
usage(void)
{
fprintf(stderr, "usage: %s pcapfiles ...\n", progname);
exit(1);
}
int
main(int argc, char *argv[])
{
pcap_t *in = NULL;
pcap_dumper_t *out = NULL;
struct pcap_pkthdr hdr;
const u_char *data;
int i;
int nfixed = 0;
int dryrun = 0;
int dlt = 0;
progname = argv[0];
while ((i = getopt(argc, argv, "n")) != -1) {
switch (i) {
case 'n':
dryrun = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
for (i = 0; i < argc; i++) {
in = my_pcap_open_offline(argv[i]);
dlt = pcap_datalink(in);
while ((data = pcap_next(in, &hdr))) {
if (DLT_LOOP == dlt && needs_fixing(&hdr, data)) {
nfixed++;
if (dryrun)
warnx("found bad packet of at %10lld.%06lld",
(long long int) hdr.ts.tv_sec, (long long int) hdr.ts.tv_usec);
else
fix(data);
}
if (!out) {
out = pcap_dump_open(in, "-");
if (NULL == out) {
perror("stdout");
exit(1);
}
}
pcap_dump((void *) out, &hdr, data);
}
my_pcap_close_offline(in);
}
if (out)
pcap_dump_close(out);
fprintf(stderr, "%s: Fixed %d bad packets\n", progname, nfixed);
exit(0);
}
int
is_bogus(struct pcap_pkthdr *thishdr, const u_char * thisdata)
{
if (thishdr->caplen > 65535)
return 1;
if (thishdr->len > 65535)
return 1;
return 0;
}