-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpmerge.c
149 lines (129 loc) · 2.68 KB
/
pmerge.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdint.h>
#include <sysexits.h>
#include "pcap.h"
struct input_file {
int active;
struct pcap_file p;
struct pcap_pkthdr next;
};
int
usage(int ret)
{
fprintf(stderr, "Usage: pmerge FILE ...\n");
fprintf(stderr, "\n");
fprintf(stderr, "Merges pcap files, outputting time-ordered pcap stream\n");
return ret;
}
int
read_next(struct input_file *file)
{
if (!file->active)
return -1;
if (-1 == pcap_read_pkthdr(&file->p, &file->next)) {
pcap_close(&file->p);
file->active = 0;
file->next.ts.tv_sec = 0xffffffff;
file->next.ts.tv_usec = 0xffffffff;
return -1;
}
return 0;
}
int
main(int argc, char *argv[])
{
struct input_file files[argc - 1];
int nfiles = 0;
int nopen;
int i;
struct pcap_file out;
if (1 == argc)
return usage(0);
/*
* Open input files
*/
int32_t linktype = 0;
for (i = 0; i < argc - 1; i += 1) {
char *fn = argv[i + 1];
struct input_file *cur = &files[nfiles];
FILE *f;
if ('-' == fn[0])
return usage(0);
f = fopen(fn, "rb");
if (NULL == f) {
perror(fn);
return EX_NOINPUT;
}
if (-1 == pcap_open_in(&cur->p, f)) {
fprintf(stderr, "%s: unable to process\n", fn);
return EX_IOERR;
}
if (i == 0) {
linktype = cur->p.linktype;
} else if (linktype != cur->p.linktype) {
fprintf(stderr, "%s: incompatible linktype with first file\n", fn);
return EX_IOERR;
}
cur->active = 1;
if (0 == read_next(cur)) {
nfiles += 1;
}
}
if (-1 == pcap_open_out_linktype(&out, stdout, linktype)) {
perror("writing header");
return EX_IOERR;
}
nopen = nfiles;
while (nopen) {
struct input_file *cur = &files[0];
char frame[MAXFRAME];
size_t len;
/*
* Find next oldest frame
*/
for (i = 0; i < nfiles; i += 1) {
if (files[i].active &&
((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) ||
((files[i].next.ts.tv_sec == cur->next.ts.tv_sec) &&
(files[i].next.ts.tv_usec < cur->next.ts.tv_usec)))) {
cur = &files[i];
}
}
/*
* Make sure it'll fit
*/
if (cur->next.caplen > sizeof(frame)) {
fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int) cur->next.caplen);
return EX_SOFTWARE;
}
/*
* Read it
*/
len = fread(frame, 1, cur->next.caplen, cur->p.f);
if (len < cur->next.caplen) {
/*
* Short read
*/
cur->next.caplen = len;
pcap_close(&cur->p);
cur->active = 0;
}
/*
* Write header + frame
*/
if (len) {
if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) {
perror("error");
return EX_IOERR;
}
if (len != fwrite(frame, 1, len, stdout)) {
perror("error");
return EX_IOERR;
}
}
if (-1 == read_next(cur)) {
nopen -= 1;
}
}
return 0;
}