-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap-remove-dupe.c
100 lines (91 loc) · 2.06 KB
/
pcap-remove-dupe.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
#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/stat.h>
#include "pcap-tools.h"
#ifdef __GLIBC__
#include <openssl/md5.h>
#define MD5Init MD5_Init
#define MD5Update MD5_Update
#define MD5Final MD5_Final
#else
#include <md5.h>
#endif
/*
* Remove duplicated packets from a pcap file
*/
int is_dupe(struct pcap_pkthdr *thishdr, const u_char * thisdata);
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 ndupes = 0;
if (argc < 2) {
fprintf(stderr, "usage: pcap-remove-dupe pcapfiles ...");
exit(1);
}
for (i = 1; i < argc; i++) {
in = my_pcap_open_offline(argv[i]);
while ((data = pcap_next(in, &hdr))) {
if (is_dupe(&hdr, data)) {
ndupes++;
continue;
}
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, "Removed %d dupes\n", ndupes);
exit(0);
}
int
is_dupe(struct pcap_pkthdr *thishdr, const u_char * thisdata)
{
static struct pcap_pkthdr lasthdr;
static char lastdata[4096];
MD5_CTX md5;
static unsigned int thisdigest[4];
static unsigned int lastdigest[4];
int rc = 0;
/*
* sigh, must compute MD5 for every packet
*/
MD5Init(&md5);
MD5Update(&md5, thisdata, thishdr->len);
MD5Final((u_char *) thisdigest, &md5);
if (thishdr->ts.tv_usec != lasthdr.ts.tv_usec)
(void) 0;
else if (thishdr->ts.tv_sec != lasthdr.ts.tv_sec)
(void) 0;
else if (thishdr->caplen != lasthdr.caplen)
(void) 0;
else if (thishdr->len != lasthdr.len)
(void) 0;
else {
if (memcmp(thisdigest, lastdigest, 16) == 0)
rc = 1;
}
memcpy(lastdigest, thisdigest, 16);
lasthdr = *thishdr;
memcpy(lastdata, thisdata, thishdr->len);
return rc;
}