Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1993, 1994, 1995, 1996, 1997 |
3 | | * The Regents of the University of California. All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that: (1) source code distributions |
7 | | * retain the above copyright notice and this paragraph in its entirety, (2) |
8 | | * distributions including binary code include the above copyright notice and |
9 | | * this paragraph in its entirety in the documentation or other materials |
10 | | * provided with the distribution, and (3) all advertising materials mentioning |
11 | | * features or use of this software display the following acknowledgement: |
12 | | * ``This product includes software developed by the University of California, |
13 | | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
14 | | * the University nor the names of its contributors may be used to endorse |
15 | | * or promote products derived from this software without specific prior |
16 | | * written permission. |
17 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
18 | | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
19 | | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
20 | | * |
21 | | * savefile.c - supports offline use of tcpdump |
22 | | * Extraction/creation by Jeffrey Mogul, DECWRL |
23 | | * Modified by Steve McCanne, LBL. |
24 | | * |
25 | | * Used to save the received packet headers, after filtering, to |
26 | | * a file, and then read them later. |
27 | | * The first record in the file contains saved values for the machine |
28 | | * dependent values so we can print the dump file on any architecture. |
29 | | */ |
30 | | |
31 | | #include <config.h> |
32 | | |
33 | | #include <pcap-types.h> |
34 | | #ifdef _WIN32 |
35 | | #include <io.h> |
36 | | #include <fcntl.h> |
37 | | #endif /* _WIN32 */ |
38 | | |
39 | | #include <errno.h> |
40 | | #include <memory.h> |
41 | | #include <stdio.h> |
42 | | #include <stdlib.h> |
43 | | #include <string.h> |
44 | | #include <limits.h> /* for INT_MAX */ |
45 | | |
46 | | #include "pcap-int.h" |
47 | | |
48 | | #ifdef HAVE_OS_PROTO_H |
49 | | #include "os-proto.h" |
50 | | #endif |
51 | | |
52 | | #include "sf-pcap.h" |
53 | | #include "sf-pcapng.h" |
54 | | #include "pcap-common.h" |
55 | | #include "charconv.h" |
56 | | |
57 | | #ifdef _WIN32 |
58 | | /* |
59 | | * This isn't exported on Windows, because it would only work if both |
60 | | * WinPcap/Npcap and the code using it were to use the Universal CRT; otherwise, |
61 | | * a FILE structure in WinPcap/Npcap and a FILE structure in the code using it |
62 | | * could be different if they're using different versions of the C runtime. |
63 | | * |
64 | | * Instead, pcap/pcap.h defines it as a macro that wraps the hopen version, |
65 | | * with the wrapper calling _fileno() and _get_osfhandle() themselves, |
66 | | * so that it convert the appropriate CRT version's FILE structure to |
67 | | * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32 |
68 | | * and Win64 ABIs). |
69 | | */ |
70 | | static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *); |
71 | | #endif |
72 | | |
73 | | /* |
74 | | * Setting O_BINARY on Windows is a bit tricky. |
75 | | */ |
76 | | #if defined(_WIN32) |
77 | | #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY) |
78 | | #endif |
79 | | |
80 | | static int |
81 | | sf_getnonblock(pcap_t *p _U_) |
82 | 0 | { |
83 | | /* |
84 | | * This is a savefile, not a live capture file, so never say |
85 | | * it's in non-blocking mode. |
86 | | */ |
87 | 0 | return (0); |
88 | 0 | } |
89 | | |
90 | | static int |
91 | | sf_setnonblock(pcap_t *p, int nonblock _U_) |
92 | 0 | { |
93 | | /* |
94 | | * This is a savefile, not a live capture file, so reject |
95 | | * requests to put it in non-blocking mode. (If it's a |
96 | | * pipe, it could be put in non-blocking mode, but that |
97 | | * would significantly complicate the code to read packets, |
98 | | * as it would have to handle reading partial packets and |
99 | | * keeping the state of the read.) |
100 | | */ |
101 | 0 | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
102 | 0 | "Savefiles cannot be put into non-blocking mode"); |
103 | 0 | return (-1); |
104 | 0 | } |
105 | | |
106 | | static int |
107 | | sf_cant_set_rfmon(pcap_t *p _U_) |
108 | 0 | { |
109 | | /* |
110 | | * This is a savefile, not a device on which you can capture, |
111 | | * so never say it supports being put into monitor mode. |
112 | | */ |
113 | 0 | return (0); |
114 | 0 | } |
115 | | |
116 | | static int |
117 | | sf_stats(pcap_t *p, struct pcap_stat *ps _U_) |
118 | 3.55k | { |
119 | 3.55k | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
120 | 3.55k | "Statistics aren't available from savefiles"); |
121 | 3.55k | return (-1); |
122 | 3.55k | } |
123 | | |
124 | | #ifdef _WIN32 |
125 | | static struct pcap_stat * |
126 | | sf_stats_ex(pcap_t *p, int *size _U_) |
127 | | { |
128 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
129 | | "Statistics aren't available from savefiles"); |
130 | | return (NULL); |
131 | | } |
132 | | |
133 | | static int |
134 | | sf_setbuff(pcap_t *p, int dim _U_) |
135 | | { |
136 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
137 | | "The kernel buffer size cannot be set while reading from a file"); |
138 | | return (-1); |
139 | | } |
140 | | |
141 | | static int |
142 | | sf_setmode(pcap_t *p, int mode _U_) |
143 | | { |
144 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
145 | | "impossible to set mode while reading from a file"); |
146 | | return (-1); |
147 | | } |
148 | | |
149 | | static int |
150 | | sf_setmintocopy(pcap_t *p, int size _U_) |
151 | | { |
152 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
153 | | "The mintocopy parameter cannot be set while reading from a file"); |
154 | | return (-1); |
155 | | } |
156 | | |
157 | | static HANDLE |
158 | | sf_getevent(pcap_t *pcap) |
159 | | { |
160 | | (void)snprintf(pcap->errbuf, sizeof(pcap->errbuf), |
161 | | "The read event cannot be retrieved while reading from a file"); |
162 | | return (INVALID_HANDLE_VALUE); |
163 | | } |
164 | | |
165 | | static int |
166 | | sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_, |
167 | | size_t *lenp _U_) |
168 | | { |
169 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
170 | | "An OID get request cannot be performed on a file"); |
171 | | return (PCAP_ERROR); |
172 | | } |
173 | | |
174 | | static int |
175 | | sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_, |
176 | | size_t *lenp _U_) |
177 | | { |
178 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
179 | | "An OID set request cannot be performed on a file"); |
180 | | return (PCAP_ERROR); |
181 | | } |
182 | | |
183 | | static u_int |
184 | | sf_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue _U_, int sync _U_) |
185 | | { |
186 | | pcapint_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles", |
187 | | PCAP_ERRBUF_SIZE); |
188 | | return (0); |
189 | | } |
190 | | |
191 | | static int |
192 | | sf_setuserbuffer(pcap_t *p, int size _U_) |
193 | | { |
194 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
195 | | "The user buffer cannot be set when reading from a file"); |
196 | | return (-1); |
197 | | } |
198 | | |
199 | | static int |
200 | | sf_live_dump(pcap_t *p, char *filename _U_, int maxsize _U_, int maxpacks _U_) |
201 | | { |
202 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
203 | | "Live packet dumping cannot be performed when reading from a file"); |
204 | | return (-1); |
205 | | } |
206 | | |
207 | | static int |
208 | | sf_live_dump_ended(pcap_t *p, int sync _U_) |
209 | | { |
210 | | snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
211 | | "Live packet dumping cannot be performed on a pcap_open_dead pcap_t"); |
212 | | return (-1); |
213 | | } |
214 | | |
215 | | static PAirpcapHandle |
216 | | sf_get_airpcap_handle(pcap_t *pcap _U_) |
217 | | { |
218 | | return (NULL); |
219 | | } |
220 | | #endif |
221 | | |
222 | | static int |
223 | | sf_inject(pcap_t *p, const void *buf _U_, int size _U_) |
224 | 0 | { |
225 | 0 | pcapint_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles", |
226 | 0 | PCAP_ERRBUF_SIZE); |
227 | 0 | return (-1); |
228 | 0 | } |
229 | | |
230 | | /* |
231 | | * Set direction flag: Which packets do we accept on a forwarding |
232 | | * single device? IN, OUT or both? |
233 | | */ |
234 | | static int |
235 | | sf_setdirection(pcap_t *p, pcap_direction_t d _U_) |
236 | 0 | { |
237 | 0 | snprintf(p->errbuf, sizeof(p->errbuf), |
238 | 0 | "Setting direction is not supported on savefiles"); |
239 | 0 | return (-1); |
240 | 0 | } |
241 | | |
242 | | void |
243 | | pcapint_sf_cleanup(pcap_t *p) |
244 | 17.6k | { |
245 | 17.6k | if (p->rfile != stdin) |
246 | 17.6k | (void)fclose(p->rfile); |
247 | 17.6k | if (p->buffer != NULL) |
248 | 17.6k | free(p->buffer); |
249 | 17.6k | pcap_freecode(&p->fcode); |
250 | 17.6k | } |
251 | | |
252 | | #ifdef _WIN32 |
253 | | /* |
254 | | * Wrapper for fopen() and _wfopen(). |
255 | | * |
256 | | * If we're in UTF-8 mode, map the pathname from UTF-8 to UTF-16LE and |
257 | | * call _wfopen(). |
258 | | * |
259 | | * If we're not, just use fopen(); that'll treat it as being in the |
260 | | * local code page. |
261 | | */ |
262 | | FILE * |
263 | | pcapint_charset_fopen(const char *path, const char *mode) |
264 | | { |
265 | | wchar_t *utf16_path; |
266 | | #define MAX_MODE_LEN 16 |
267 | | wchar_t utf16_mode[MAX_MODE_LEN+1]; |
268 | | int i; |
269 | | char c; |
270 | | FILE *fp; |
271 | | int save_errno; |
272 | | |
273 | | if (pcapint_utf_8_mode) { |
274 | | /* |
275 | | * Map from UTF-8 to UTF-16LE. |
276 | | * Fail if there are invalid characters in the input |
277 | | * string, rather than converting them to REPLACEMENT |
278 | | * CHARACTER; the latter is appropriate for strings |
279 | | * to be displayed to the user, but for file names |
280 | | * you just want the attempt to open the file to fail. |
281 | | */ |
282 | | utf16_path = cp_to_utf_16le(CP_UTF8, path, |
283 | | MB_ERR_INVALID_CHARS); |
284 | | if (utf16_path == NULL) { |
285 | | /* |
286 | | * Error. Assume errno has been set. |
287 | | * |
288 | | * XXX - what about Windows errors? |
289 | | */ |
290 | | return (NULL); |
291 | | } |
292 | | |
293 | | /* |
294 | | * Now convert the mode to UTF-16LE as well. |
295 | | * We assume the mode is ASCII, and that |
296 | | * it's short, so that's easy. |
297 | | */ |
298 | | for (i = 0; (c = *mode) != '\0'; i++, mode++) { |
299 | | if (c > 0x7F) { |
300 | | /* Not an ASCII character; fail with EINVAL. */ |
301 | | free(utf16_path); |
302 | | errno = EINVAL; |
303 | | return (NULL); |
304 | | } |
305 | | if (i >= MAX_MODE_LEN) { |
306 | | /* The mode string is longer than we allow. */ |
307 | | free(utf16_path); |
308 | | errno = EINVAL; |
309 | | return (NULL); |
310 | | } |
311 | | utf16_mode[i] = c; |
312 | | } |
313 | | utf16_mode[i] = '\0'; |
314 | | |
315 | | /* |
316 | | * OK, we have UTF-16LE strings; hand them to |
317 | | * _wfopen(). |
318 | | */ |
319 | | fp = _wfopen(utf16_path, utf16_mode); |
320 | | |
321 | | /* |
322 | | * Make sure freeing the UTF-16LE string doesn't |
323 | | * overwrite the error code we got from _wfopen(). |
324 | | */ |
325 | | save_errno = errno; |
326 | | free(utf16_path); |
327 | | errno = save_errno; |
328 | | |
329 | | return (fp); |
330 | | } else { |
331 | | /* |
332 | | * This takes strings in the local code page as an |
333 | | * argument. |
334 | | */ |
335 | | return (fopen(path, mode)); |
336 | | } |
337 | | } |
338 | | #endif |
339 | | |
340 | | pcap_t * |
341 | | pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision, |
342 | | char *errbuf) |
343 | 19.1k | { |
344 | 19.1k | FILE *fp; |
345 | 19.1k | pcap_t *p; |
346 | | |
347 | 19.1k | if (fname == NULL) { |
348 | 0 | snprintf(errbuf, PCAP_ERRBUF_SIZE, |
349 | 0 | "A null pointer was supplied as the file name"); |
350 | 0 | return (NULL); |
351 | 0 | } |
352 | 19.1k | if (fname[0] == '-' && fname[1] == '\0') |
353 | 0 | { |
354 | 0 | fp = stdin; |
355 | 0 | if (fp == NULL) { |
356 | 0 | snprintf(errbuf, PCAP_ERRBUF_SIZE, |
357 | 0 | "The standard input is not open"); |
358 | 0 | return (NULL); |
359 | 0 | } |
360 | | #if defined(_WIN32) |
361 | | /* |
362 | | * We're reading from the standard input, so put it in binary |
363 | | * mode, as savefiles are binary files. |
364 | | */ |
365 | | SET_BINMODE(fp); |
366 | | #endif |
367 | 0 | } |
368 | 19.1k | else { |
369 | | /* |
370 | | * Use pcapint_charset_fopen(); on Windows, it tests whether we're |
371 | | * in "local code page" or "UTF-8" mode, and treats the |
372 | | * pathname appropriately, and on other platforms, it just |
373 | | * wraps fopen(). |
374 | | * |
375 | | * "b" is supported as of C90, so *all* UN*Xes should |
376 | | * support it, even though it does nothing. |
377 | | */ |
378 | 19.1k | fp = pcapint_charset_fopen(fname, "rb"); |
379 | 19.1k | if (fp == NULL) { |
380 | 0 | pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
381 | 0 | errno, "%s", fname); |
382 | 0 | return (NULL); |
383 | 0 | } |
384 | 19.1k | } |
385 | 19.1k | p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf); |
386 | 19.1k | if (p == NULL) { |
387 | 1.55k | if (fp != stdin) |
388 | 1.55k | fclose(fp); |
389 | 1.55k | } |
390 | 19.1k | return (p); |
391 | 19.1k | } |
392 | | |
393 | | pcap_t * |
394 | | pcap_open_offline(const char *fname, char *errbuf) |
395 | 19.1k | { |
396 | 19.1k | return (pcap_open_offline_with_tstamp_precision(fname, |
397 | 19.1k | PCAP_TSTAMP_PRECISION_MICRO, errbuf)); |
398 | 19.1k | } |
399 | | |
400 | | #ifdef _WIN32 |
401 | | pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision, |
402 | | char *errbuf) |
403 | | { |
404 | | int fd; |
405 | | FILE *file; |
406 | | |
407 | | fd = _open_osfhandle(osfd, _O_RDONLY); |
408 | | if ( fd < 0 ) |
409 | | { |
410 | | pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
411 | | errno, "_open_osfhandle"); |
412 | | return NULL; |
413 | | } |
414 | | |
415 | | file = _fdopen(fd, "rb"); |
416 | | if ( file == NULL ) |
417 | | { |
418 | | pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
419 | | errno, "_fdopen"); |
420 | | _close(fd); |
421 | | return NULL; |
422 | | } |
423 | | |
424 | | return pcap_fopen_offline_with_tstamp_precision(file, precision, |
425 | | errbuf); |
426 | | } |
427 | | |
428 | | pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf) |
429 | | { |
430 | | return pcap_hopen_offline_with_tstamp_precision(osfd, |
431 | | PCAP_TSTAMP_PRECISION_MICRO, errbuf); |
432 | | } |
433 | | #endif |
434 | | |
435 | | /* |
436 | | * Given a link-layer header type and snapshot length, return a |
437 | | * snapshot length to use when reading the file; it's guaranteed |
438 | | * to be > 0 and <= INT_MAX. |
439 | | * |
440 | | * XXX - the only reason why we limit it to <= INT_MAX is so that |
441 | | * it fits in p->snapshot, and the only reason that p->snapshot is |
442 | | * signed is that pcap_snapshot() returns an int, not an unsigned int. |
443 | | */ |
444 | | bpf_u_int32 |
445 | | pcapint_adjust_snapshot(bpf_u_int32 linktype, bpf_u_int32 snaplen) |
446 | 29.7k | { |
447 | 29.7k | if (snaplen == 0 || snaplen > INT_MAX) { |
448 | | /* |
449 | | * Bogus snapshot length; use the maximum for this |
450 | | * link-layer type as a fallback. |
451 | | * |
452 | | * XXX - we don't clamp snapshot lengths that are |
453 | | * <= INT_MAX but > max_snaplen_for_dlt(linktype), |
454 | | * so a capture file could cause us to allocate |
455 | | * a Really Big Buffer. |
456 | | */ |
457 | 12.6k | snaplen = max_snaplen_for_dlt(linktype); |
458 | 12.6k | } |
459 | 29.7k | return snaplen; |
460 | 29.7k | } |
461 | | |
462 | | static pcap_t *(*check_headers[])(const uint8_t *, FILE *, u_int, char *, int *) = { |
463 | | pcap_check_header, |
464 | | pcap_ng_check_header |
465 | | }; |
466 | | |
467 | 22.5k | #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0]) |
468 | | |
469 | | #ifdef _WIN32 |
470 | | static |
471 | | #endif |
472 | | pcap_t * |
473 | | pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision, |
474 | | char *errbuf) |
475 | 19.1k | { |
476 | 19.1k | register pcap_t *p; |
477 | 19.1k | uint8_t magic[4]; |
478 | 19.1k | size_t amt_read; |
479 | 19.1k | u_int i; |
480 | 19.1k | int err; |
481 | | |
482 | | /* |
483 | | * Fail if we were passed a NULL fp. |
484 | | * |
485 | | * That shouldn't happen if we're opening with a path name, but |
486 | | * it could happen if buggy code is opening with a FILE * and |
487 | | * didn't bother to make sure the FILE * isn't null. |
488 | | */ |
489 | 19.1k | if (fp == NULL) { |
490 | 0 | snprintf(errbuf, PCAP_ERRBUF_SIZE, |
491 | 0 | "Null FILE * pointer provided to savefile open routine"); |
492 | 0 | return (NULL); |
493 | 0 | } |
494 | | |
495 | | /* |
496 | | * Read the first 4 bytes of the file; the network analyzer dump |
497 | | * file formats we support (pcap and pcapng), and several other |
498 | | * formats we might support in the future (such as snoop, DOS and |
499 | | * Windows Sniffer, and Microsoft Network Monitor) all have magic |
500 | | * numbers that are unique in their first 4 bytes. |
501 | | */ |
502 | 19.1k | amt_read = fread(&magic, 1, sizeof(magic), fp); |
503 | 19.1k | if (amt_read != sizeof(magic)) { |
504 | 9 | if (ferror(fp)) { |
505 | 0 | pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
506 | 0 | errno, "error reading dump file"); |
507 | 9 | } else { |
508 | 9 | snprintf(errbuf, PCAP_ERRBUF_SIZE, |
509 | 9 | "truncated dump file; tried to read %zu file header bytes, only got %zu", |
510 | 9 | sizeof(magic), amt_read); |
511 | 9 | } |
512 | 9 | return (NULL); |
513 | 9 | } |
514 | | |
515 | | /* |
516 | | * Try all file types. |
517 | | */ |
518 | 22.5k | for (i = 0; i < N_FILE_TYPES; i++) { |
519 | 21.9k | p = (*check_headers[i])(magic, fp, precision, errbuf, &err); |
520 | 21.9k | if (p != NULL) { |
521 | | /* Yup, that's it. */ |
522 | 17.6k | goto found; |
523 | 17.6k | } |
524 | 4.32k | if (err) { |
525 | | /* |
526 | | * Error trying to read the header. |
527 | | */ |
528 | 899 | return (NULL); |
529 | 899 | } |
530 | 4.32k | } |
531 | | |
532 | | /* |
533 | | * Well, who knows what this mess is.... |
534 | | */ |
535 | 646 | snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format"); |
536 | 646 | return (NULL); |
537 | | |
538 | 17.6k | found: |
539 | 17.6k | p->rfile = fp; |
540 | | |
541 | | /* Padding only needed for live capture fcode */ |
542 | 17.6k | p->fddipad = 0; |
543 | | |
544 | 17.6k | #if !defined(_WIN32) |
545 | | /* |
546 | | * You can do "select()" and "poll()" on plain files on most |
547 | | * platforms, and should be able to do so on pipes. |
548 | | * |
549 | | * You can't do "select()" on anything other than sockets in |
550 | | * Windows, so, on Win32 systems, we don't have "selectable_fd". |
551 | | */ |
552 | 17.6k | p->selectable_fd = fileno(fp); |
553 | 17.6k | #endif |
554 | | |
555 | 17.6k | p->can_set_rfmon_op = sf_cant_set_rfmon; |
556 | 17.6k | p->read_op = pcapint_offline_read; |
557 | 17.6k | p->inject_op = sf_inject; |
558 | 17.6k | p->setfilter_op = pcapint_install_bpf_program; |
559 | 17.6k | p->setdirection_op = sf_setdirection; |
560 | 17.6k | p->set_datalink_op = NULL; /* we don't support munging link-layer headers */ |
561 | 17.6k | p->getnonblock_op = sf_getnonblock; |
562 | 17.6k | p->setnonblock_op = sf_setnonblock; |
563 | 17.6k | p->stats_op = sf_stats; |
564 | | #ifdef _WIN32 |
565 | | p->stats_ex_op = sf_stats_ex; |
566 | | p->setbuff_op = sf_setbuff; |
567 | | p->setmode_op = sf_setmode; |
568 | | p->setmintocopy_op = sf_setmintocopy; |
569 | | p->getevent_op = sf_getevent; |
570 | | p->oid_get_request_op = sf_oid_get_request; |
571 | | p->oid_set_request_op = sf_oid_set_request; |
572 | | p->sendqueue_transmit_op = sf_sendqueue_transmit; |
573 | | p->setuserbuffer_op = sf_setuserbuffer; |
574 | | p->live_dump_op = sf_live_dump; |
575 | | p->live_dump_ended_op = sf_live_dump_ended; |
576 | | p->get_airpcap_handle_op = sf_get_airpcap_handle; |
577 | | #endif |
578 | | |
579 | | /* |
580 | | * For offline captures, the standard one-shot callback can |
581 | | * be used for pcap_next()/pcap_next_ex(). |
582 | | */ |
583 | 17.6k | p->oneshot_callback = pcapint_oneshot; |
584 | | |
585 | | /* |
586 | | * Default breakloop operation. |
587 | | */ |
588 | 17.6k | p->breakloop_op = pcapint_breakloop_common; |
589 | | |
590 | | /* |
591 | | * Savefiles never require special BPF code generation. |
592 | | */ |
593 | 17.6k | p->bpf_codegen_flags = 0; |
594 | | |
595 | 17.6k | p->activated = 1; |
596 | | |
597 | 17.6k | return (p); |
598 | 19.1k | } |
599 | | |
600 | | /* |
601 | | * This isn't needed on Windows; we #define pcap_fopen_offline() as |
602 | | * a wrapper around pcap_hopen_offline(), and we don't call it from |
603 | | * inside this file, so it's unused. |
604 | | */ |
605 | | #ifndef _WIN32 |
606 | | pcap_t * |
607 | | pcap_fopen_offline(FILE *fp, char *errbuf) |
608 | 0 | { |
609 | 0 | return (pcap_fopen_offline_with_tstamp_precision(fp, |
610 | 0 | PCAP_TSTAMP_PRECISION_MICRO, errbuf)); |
611 | 0 | } |
612 | | #endif |
613 | | |
614 | | /* |
615 | | * Read packets from a capture file, and call the callback for each |
616 | | * packet. |
617 | | * If cnt > 0, return after 'cnt' packets, otherwise continue until eof. |
618 | | */ |
619 | | int |
620 | | pcapint_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) |
621 | 60.4k | { |
622 | 60.4k | struct bpf_insn *fcode; |
623 | 60.4k | int n = 0; |
624 | 60.4k | u_char *data; |
625 | | |
626 | | /* |
627 | | * This can conceivably process more than INT_MAX packets, |
628 | | * which would overflow the packet count, causing it either |
629 | | * to look like a negative number, and thus cause us to |
630 | | * return a value that looks like an error, or overflow |
631 | | * back into positive territory, and thus cause us to |
632 | | * return a too-low count. |
633 | | * |
634 | | * Therefore, if the packet count is unlimited, we clip |
635 | | * it at INT_MAX; this routine is not expected to |
636 | | * process packets indefinitely, so that's not an issue. |
637 | | */ |
638 | 60.4k | if (PACKET_COUNT_IS_UNLIMITED(cnt)) |
639 | 0 | cnt = INT_MAX; |
640 | | |
641 | 60.4k | for (;;) { |
642 | 60.4k | struct pcap_pkthdr h; |
643 | 60.4k | int status; |
644 | | |
645 | | /* |
646 | | * Has "pcap_breakloop()" been called? |
647 | | * If so, return immediately - if we haven't read any |
648 | | * packets, clear the flag and return -2 to indicate |
649 | | * that we were told to break out of the loop, otherwise |
650 | | * leave the flag set, so that the *next* call will break |
651 | | * out of the loop without having read any packets, and |
652 | | * return the number of packets we've processed so far. |
653 | | */ |
654 | 60.4k | if (p->break_loop) { |
655 | 0 | if (n == 0) { |
656 | 0 | p->break_loop = 0; |
657 | 0 | return (-2); |
658 | 0 | } else |
659 | 0 | return (n); |
660 | 0 | } |
661 | | |
662 | 60.4k | status = p->next_packet_op(p, &h, &data); |
663 | 60.4k | if (status < 0) { |
664 | | /* |
665 | | * Error. Pass it back to the caller. |
666 | | */ |
667 | 1.62k | return (status); |
668 | 1.62k | } |
669 | 58.8k | if (status == 0) { |
670 | | /* |
671 | | * EOF. Nothing more to process; |
672 | | */ |
673 | 10.4k | break; |
674 | 10.4k | } |
675 | | |
676 | | /* |
677 | | * OK, we've read a packet; run it through the filter |
678 | | * and, if it passes, process it. |
679 | | */ |
680 | 48.3k | if ((fcode = p->fcode.bf_insns) == NULL || |
681 | 48.3k | pcapint_filter(fcode, data, h.len, h.caplen)) { |
682 | 48.3k | (*callback)(user, &h, data); |
683 | 48.3k | n++; /* count the packet */ |
684 | 48.3k | if (n >= cnt) |
685 | 48.3k | break; |
686 | 48.3k | } |
687 | 48.3k | } |
688 | | /*XXX this breaks semantics tcpslice expects */ |
689 | 58.8k | return (n); |
690 | 60.4k | } |