forked from kismetwireless/kismet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_pcapfile.c
359 lines (284 loc) · 11.1 KB
/
capture_pcapfile.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* capture_pcapfile
*
* Basic capture binary, written in pure C, which interfaces via the Kismet
* simple capture protocol and feeds packets from a pcap file.
*
* This could have been implemented in C++ but serves as an example of a simple,
* very low resource capture method.
*
* This uses some of the pure-C code included in Kismet - pure-c implementations
* of the datasource protocol, a basic ringbuffer implementation, and the msgpuck
* library which is a pure-c simple msgpack library.
*
* This uses basic threading to show how to do an asynchronous read from a source;
* while a pcapfile will never stall, other sources could.
*
* The select() loop for IO with the IPC channel is performed in the primary
* thread, and an IO thread is spawned to process data from the pcap file. This
* allows us to expand to interesting options, like realtime pcap replay which
* delays the IO as if they were real packets.
*
* The DLT is automatically propagated from the pcap file, or can be overridden
* with a source command.
*
* The communications channel is a file descriptor pair, passed via command
* line arguments, --in-fd= and --out-fd=
*
* We parse additional options from the source definition itself, such as a DLT
* override, once we open the protocol
*
*/
#include <pcap.h>
#include <getopt.h>
#include <pthread.h>
#include <fcntl.h>
/* According to POSIX.1-2001, POSIX.1-2008 */
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include "config.h"
#include "simple_datasource_proto.h"
#include "capture_framework.h"
typedef struct {
pcap_t *pd;
char *pcapfname;
int datalink_type;
int override_dlt;
int realtime;
struct timeval last_ts;
} local_pcap_t;
int probe_callback(kis_capture_handler_t *caph, uint32_t seqno, char *definition,
char *msg, char **uuid, simple_cap_proto_frame_t *frame,
cf_params_interface_t **ret_interface,
cf_params_spectrum_t **ret_spectrum) {
char *placeholder = NULL;
int placeholder_len;
*uuid = NULL;
char *pcapfname = NULL;
struct stat sbuf;
char errstr[PCAP_ERRBUF_SIZE] = "";
pcap_t *pd;
*ret_spectrum = NULL;
*ret_interface = cf_params_interface_new();
*ret_spectrum = NULL;
/* pcapfile does not support channel ops */
(*ret_interface)->chanset = NULL;
(*ret_interface)->channels = NULL;
(*ret_interface)->channels_len = 0;
if ((placeholder_len = cf_parse_interface(&placeholder, definition)) <= 0) {
snprintf(msg, STATUS_MAX, "Unable to find PCAP file name in definition");
return 0;
}
pcapfname = strndup(placeholder, placeholder_len);
if (stat(pcapfname, &sbuf) < 0) {
return 0;
}
if (!S_ISREG(sbuf.st_mode)) {
snprintf(msg, STATUS_MAX, "File '%s' is not a regular file", pcapfname);
return 0;
}
pd = pcap_open_offline(pcapfname, errstr);
if (strlen(errstr) > 0) {
snprintf(msg, STATUS_MAX, "%s", errstr);
return -1;
}
pcap_close(pd);
/* Kluge a UUID out of the name */
snprintf(errstr, PCAP_ERRBUF_SIZE, "%08X-0000-0000-0000-0000%08X",
adler32_csum((unsigned char *) "kismet_cap_pcapfile",
strlen("kismet_cap_pcapfile")) & 0xFFFFFFFF,
adler32_csum((unsigned char *) pcapfname,
strlen(pcapfname)) & 0xFFFFFFFF);
*uuid = strdup(errstr);
return 1;
}
int open_callback(kis_capture_handler_t *caph, uint32_t seqno, char *definition,
char *msg, uint32_t *dlt, char **uuid, simple_cap_proto_frame_t *frame,
cf_params_interface_t **ret_interface,
cf_params_spectrum_t **ret_spectrum) {
char *placeholder = NULL;
int placeholder_len;
char *pcapfname = NULL;
struct stat sbuf;
local_pcap_t *local_pcap = (local_pcap_t *) caph->userdata;
char errstr[PCAP_ERRBUF_SIZE] = "";
/* pcapfile does not support channel ops */
*ret_interface = cf_params_interface_new();
*ret_spectrum = NULL;
*uuid = NULL;
*dlt = 0;
/* Clean up any old state */
if (local_pcap->pcapfname != NULL) {
free(local_pcap->pcapfname);
local_pcap->pcapfname = NULL;
}
if (local_pcap->pd != NULL) {
pcap_close(local_pcap->pd);
local_pcap->pd = NULL;
}
if ((placeholder_len = cf_parse_interface(&placeholder, definition)) <= 0) {
/* What was not an error during probe definitely is an error during open */
snprintf(msg, STATUS_MAX, "Unable to find PCAP file name in definition");
return -1;
}
pcapfname = strndup(placeholder, placeholder_len);
local_pcap->pcapfname = pcapfname;
if (stat(pcapfname, &sbuf) < 0) {
snprintf(msg, STATUS_MAX, "Unable to find pcapfile '%s'", pcapfname);
return -1;
}
/* We don't check for regular file during open, only probe; we don't want to
* open a fifo during probe and then cause a glitch, but we could open it during
* normal operation */
local_pcap->pd = pcap_open_offline(pcapfname, errstr);
if (strlen(errstr) > 0) {
snprintf(msg, STATUS_MAX, "%s", errstr);
return -1;
}
local_pcap->datalink_type = pcap_datalink(local_pcap->pd);
*dlt = local_pcap->datalink_type;
/* Kluge a UUID out of the name */
snprintf(errstr, PCAP_ERRBUF_SIZE, "%08X-0000-0000-0000-0000%08X",
adler32_csum((unsigned char *) "kismet_cap_pcapfile",
strlen("kismet_cap_pcapfile")) & 0xFFFFFFFF,
adler32_csum((unsigned char *) pcapfname,
strlen(pcapfname)) & 0xFFFFFFFF);
*uuid = strdup(errstr);
/* Succesful open with no channel, hop, or chanset data */
snprintf(msg, STATUS_MAX, "Opened pcapfile '%s' for playback", pcapfname);
if ((placeholder_len = cf_find_flag(&placeholder, "realtime", definition)) > 0) {
if (strncasecmp(placeholder, "true", placeholder_len) == 0) {
snprintf(errstr, PCAP_ERRBUF_SIZE,
"Pcapfile '%s' will replay in realtime", pcapfname);
cf_send_message(caph, errstr, MSGFLAG_INFO);
local_pcap->realtime = 1;
}
}
return 1;
}
void pcap_dispatch_cb(u_char *user, const struct pcap_pkthdr *header,
const u_char *data) {
kis_capture_handler_t *caph = (kis_capture_handler_t *) user;
local_pcap_t *local_pcap = (local_pcap_t *) caph->userdata;
int ret;
unsigned long delay_usec = 0;
/* If we're doing 'realtime' playback, delay accordingly based on the
* previous packet.
*
* Because we're in our own thread, we can block as long as we want - this
* simulates blocking IO for capturing from hardware, too.
*/
if (local_pcap->realtime) {
if (local_pcap->last_ts.tv_sec == 0 && local_pcap->last_ts.tv_usec == 0) {
delay_usec = 0;
} else {
/* Catch corrupt pcaps w/ inconsistent times */
if (header->ts.tv_sec < local_pcap->last_ts.tv_sec) {
delay_usec = 0;
} else {
delay_usec = (header->ts.tv_sec - local_pcap->last_ts.tv_sec) * 1000000L;
}
if (header->ts.tv_usec < local_pcap->last_ts.tv_usec) {
delay_usec += (1000000L - local_pcap->last_ts.tv_usec) +
header->ts.tv_usec;
} else {
delay_usec += header->ts.tv_usec - local_pcap->last_ts.tv_usec;
}
}
local_pcap->last_ts.tv_sec = header->ts.tv_sec;
local_pcap->last_ts.tv_usec = header->ts.tv_usec;
if (delay_usec != 0) {
usleep(delay_usec);
}
}
/* Try repeatedly to send the packet; go into a thread wait state if
* the write buffer is full & we'll be woken up as soon as it flushes
* data out in the main select() loop */
while (1) {
if ((ret = cf_send_data(caph,
NULL, NULL, NULL,
header->ts,
header->caplen, (uint8_t *) data)) < 0) {
pcap_breakloop(local_pcap->pd);
cf_send_error(caph, "unable to send DATA frame");
cf_handler_spindown(caph);
} else if (ret == 0) {
/* Go into a wait for the write buffer to get flushed */
// fprintf(stderr, "debug - pcapfile - dispatch_cb - no room in write buffer - waiting for it to have more space\n");
cf_handler_wait_ringbuffer(caph);
continue;
} else {
break;
}
}
}
void capture_thread(kis_capture_handler_t *caph) {
local_pcap_t *local_pcap = (local_pcap_t *) caph->userdata;
char errstr[PCAP_ERRBUF_SIZE];
char *pcap_errstr;
pcap_loop(local_pcap->pd, -1, pcap_dispatch_cb, (u_char *) caph);
pcap_errstr = pcap_geterr(local_pcap->pd);
snprintf(errstr, PCAP_ERRBUF_SIZE, "Pcapfile '%s' closed: %s",
local_pcap->pcapfname,
strlen(pcap_errstr) == 0 ? "end of pcapfile reached" : pcap_errstr );
cf_send_error(caph, errstr);
cf_handler_spindown(caph);
}
int main(int argc, char *argv[]) {
local_pcap_t local_pcap = {
.pd = NULL,
.pcapfname = NULL,
.datalink_type = -1,
.override_dlt = -1,
.realtime = 0,
.last_ts.tv_sec = 0,
.last_ts.tv_usec = 0
};
#if 0
/* Remap stderr so we can log debugging to a file */
FILE *sterr;
sterr = fopen("/tmp/capture_pcapfile.stderr", "a");
dup2(fileno(sterr), STDERR_FILENO);
#endif
fprintf(stderr, "CAPTURE_PCAPFILE launched on pid %d\n", getpid());
kis_capture_handler_t *caph = cf_handler_init("pcapfile");
if (caph == NULL) {
fprintf(stderr, "FATAL: Could not allocate basic handler data, your system "
"is very low on RAM or something is wrong.\n");
return -1;
}
/* Set the local data ptr */
cf_handler_set_userdata(caph, &local_pcap);
/* Set the callback for opening a pcapfile */
cf_handler_set_open_cb(caph, open_callback);
/* Set the callback for probing an interface */
cf_handler_set_probe_cb(caph, probe_callback);
/* Set the capture thread */
cf_handler_set_capture_cb(caph, capture_thread);
if (cf_handler_parse_opts(caph, argc, argv) < 1) {
cf_print_help(caph, argv[0]);
return -1;
}
cf_handler_loop(caph);
cf_handler_free(caph);
return 1;
}