]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
aef9fb14c2405dfcbb0c48890dbafb63eeb30889
[libpcap] / savefile.c
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 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <pcap-types.h>
36 #ifdef _WIN32
37 #include <io.h>
38 #include <fcntl.h>
39 #endif /* _WIN32 */
40
41 #include <errno.h>
42 #include <memory.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <limits.h> /* for INT_MAX */
47
48 #include "pcap-int.h"
49
50 #ifdef HAVE_OS_PROTO_H
51 #include "os-proto.h"
52 #endif
53
54 #include "sf-pcap.h"
55 #include "sf-pcapng.h"
56 #include "pcap-common.h"
57
58 #ifdef _WIN32
59 /*
60 * This isn't exported on Windows, because it would only work if both
61 * WinPcap/Npcap and the code using it were to use the Universal CRT; otherwise,
62 * a FILE structure in WinPcap/Npcap and a FILE structure in the code using it
63 * could be different if they're using different versions of the C runtime.
64 *
65 * Instead, pcap/pcap.h defines it as a macro that wraps the hopen version,
66 * with the wrapper calling _fileno() and _get_osfhandle() themselves,
67 * so that it convert the appropriate CRT version's FILE structure to
68 * a HANDLE (which is OS-defined, not CRT-defined, and is part of the Win32
69 * and Win64 ABIs).
70 */
71 static pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
72 #endif
73
74 /*
75 * Setting O_BINARY on DOS/Windows is a bit tricky
76 */
77 #if defined(_WIN32)
78 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
79 #elif defined(MSDOS)
80 #if defined(__HIGHC__)
81 #define SET_BINMODE(f) setmode(f, O_BINARY)
82 #else
83 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
84 #endif
85 #endif
86
87 static int
88 sf_getnonblock(pcap_t *p _U_)
89 {
90 /*
91 * This is a savefile, not a live capture file, so never say
92 * it's in non-blocking mode.
93 */
94 return (0);
95 }
96
97 static int
98 sf_setnonblock(pcap_t *p, int nonblock _U_)
99 {
100 /*
101 * This is a savefile, not a live capture file, so reject
102 * requests to put it in non-blocking mode. (If it's a
103 * pipe, it could be put in non-blocking mode, but that
104 * would significantly complicate the code to read packets,
105 * as it would have to handle reading partial packets and
106 * keeping the state of the read.)
107 */
108 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
109 "Savefiles cannot be put into non-blocking mode");
110 return (-1);
111 }
112
113 static int
114 sf_stats(pcap_t *p, struct pcap_stat *ps _U_)
115 {
116 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
117 "Statistics aren't available from savefiles");
118 return (-1);
119 }
120
121 #ifdef _WIN32
122 static struct pcap_stat *
123 sf_stats_ex(pcap_t *p, int *size _U_)
124 {
125 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
126 "Statistics aren't available from savefiles");
127 return (NULL);
128 }
129
130 static int
131 sf_setbuff(pcap_t *p, int dim _U_)
132 {
133 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
134 "The kernel buffer size cannot be set while reading from a file");
135 return (-1);
136 }
137
138 static int
139 sf_setmode(pcap_t *p, int mode _U_)
140 {
141 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
142 "impossible to set mode while reading from a file");
143 return (-1);
144 }
145
146 static int
147 sf_setmintocopy(pcap_t *p, int size _U_)
148 {
149 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
150 "The mintocopy parameter cannot be set while reading from a file");
151 return (-1);
152 }
153
154 static HANDLE
155 sf_getevent(pcap_t *pcap)
156 {
157 (void)snprintf(pcap->errbuf, sizeof(pcap->errbuf),
158 "The read event cannot be retrieved while reading from a file");
159 return (INVALID_HANDLE_VALUE);
160 }
161
162 static int
163 sf_oid_get_request(pcap_t *p, bpf_u_int32 oid _U_, void *data _U_,
164 size_t *lenp _U_)
165 {
166 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
167 "An OID get request cannot be performed on a file");
168 return (PCAP_ERROR);
169 }
170
171 static int
172 sf_oid_set_request(pcap_t *p, bpf_u_int32 oid _U_, const void *data _U_,
173 size_t *lenp _U_)
174 {
175 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
176 "An OID set request cannot be performed on a file");
177 return (PCAP_ERROR);
178 }
179
180 static u_int
181 sf_sendqueue_transmit(pcap_t *p, pcap_send_queue *queue _U_, int sync _U_)
182 {
183 pcap_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
184 PCAP_ERRBUF_SIZE);
185 return (0);
186 }
187
188 static int
189 sf_setuserbuffer(pcap_t *p, int size _U_)
190 {
191 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
192 "The user buffer cannot be set when reading from a file");
193 return (-1);
194 }
195
196 static int
197 sf_live_dump(pcap_t *p, char *filename _U_, int maxsize _U_, int maxpacks _U_)
198 {
199 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
200 "Live packet dumping cannot be performed when reading from a file");
201 return (-1);
202 }
203
204 static int
205 sf_live_dump_ended(pcap_t *p, int sync _U_)
206 {
207 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
208 "Live packet dumping cannot be performed on a pcap_open_dead pcap_t");
209 return (-1);
210 }
211
212 static PAirpcapHandle
213 sf_get_airpcap_handle(pcap_t *pcap _U_)
214 {
215 return (NULL);
216 }
217 #endif
218
219 static int
220 sf_inject(pcap_t *p, const void *buf _U_, int size _U_)
221 {
222 pcap_strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
223 PCAP_ERRBUF_SIZE);
224 return (-1);
225 }
226
227 /*
228 * Set direction flag: Which packets do we accept on a forwarding
229 * single device? IN, OUT or both?
230 */
231 static int
232 sf_setdirection(pcap_t *p, pcap_direction_t d _U_)
233 {
234 snprintf(p->errbuf, sizeof(p->errbuf),
235 "Setting direction is not supported on savefiles");
236 return (-1);
237 }
238
239 void
240 sf_cleanup(pcap_t *p)
241 {
242 if (p->rfile != stdin)
243 (void)fclose(p->rfile);
244 if (p->buffer != NULL)
245 free(p->buffer);
246 pcap_freecode(&p->fcode);
247 }
248
249 pcap_t *
250 pcap_open_offline_with_tstamp_precision(const char *fname, u_int precision,
251 char *errbuf)
252 {
253 FILE *fp;
254 pcap_t *p;
255
256 if (fname == NULL) {
257 snprintf(errbuf, PCAP_ERRBUF_SIZE,
258 "A null pointer was supplied as the file name");
259 return (NULL);
260 }
261 if (fname[0] == '-' && fname[1] == '\0')
262 {
263 fp = stdin;
264 if (stdin == NULL) {
265 snprintf(errbuf, PCAP_ERRBUF_SIZE,
266 "The standard input is not open");
267 return (NULL);
268 }
269 #if defined(_WIN32) || defined(MSDOS)
270 /*
271 * We're reading from the standard input, so put it in binary
272 * mode, as savefiles are binary files.
273 */
274 SET_BINMODE(fp);
275 #endif
276 }
277 else {
278 /*
279 * "b" is supported as of C90, so *all* UN*Xes should
280 * support it, even though it does nothing. It's
281 * required on Windows, as the file is a binary file
282 * and must be read in binary mode.
283 */
284 fp = fopen(fname, "rb");
285 if (fp == NULL) {
286 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
287 errno, "%s", fname);
288 return (NULL);
289 }
290 }
291 p = pcap_fopen_offline_with_tstamp_precision(fp, precision, errbuf);
292 if (p == NULL) {
293 if (fp != stdin)
294 fclose(fp);
295 }
296 return (p);
297 }
298
299 pcap_t *
300 pcap_open_offline(const char *fname, char *errbuf)
301 {
302 return (pcap_open_offline_with_tstamp_precision(fname,
303 PCAP_TSTAMP_PRECISION_MICRO, errbuf));
304 }
305
306 #ifdef _WIN32
307 pcap_t* pcap_hopen_offline_with_tstamp_precision(intptr_t osfd, u_int precision,
308 char *errbuf)
309 {
310 int fd;
311 FILE *file;
312
313 fd = _open_osfhandle(osfd, _O_RDONLY);
314 if ( fd < 0 )
315 {
316 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
317 errno, "_open_osfhandle");
318 return NULL;
319 }
320
321 file = _fdopen(fd, "rb");
322 if ( file == NULL )
323 {
324 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
325 errno, "_fdopen");
326 _close(fd);
327 return NULL;
328 }
329
330 return pcap_fopen_offline_with_tstamp_precision(file, precision,
331 errbuf);
332 }
333
334 pcap_t* pcap_hopen_offline(intptr_t osfd, char *errbuf)
335 {
336 return pcap_hopen_offline_with_tstamp_precision(osfd,
337 PCAP_TSTAMP_PRECISION_MICRO, errbuf);
338 }
339 #endif
340
341 /*
342 * Given a link-layer header type and snapshot length, return a
343 * snapshot length to use when reading the file; it's guaranteed
344 * to be > 0 and <= INT_MAX.
345 *
346 * XXX - the only reason why we limit it to <= INT_MAX is so that
347 * it fits in p->snapshot, and the only reason that p->snapshot is
348 * signed is that pcap_snapshot() returns an int, not an unsigned int.
349 */
350 bpf_u_int32
351 pcap_adjust_snapshot(bpf_u_int32 linktype, bpf_u_int32 snaplen)
352 {
353 if (snaplen == 0 || snaplen > INT_MAX) {
354 /*
355 * Bogus snapshot length; use the maximum for this
356 * link-layer type as a fallback.
357 *
358 * XXX - we don't clamp snapshot lengths that are
359 * <= INT_MAX but > max_snaplen_for_dlt(linktype),
360 * so a capture file could cause us to allocate
361 * a Really Big Buffer.
362 */
363 snaplen = max_snaplen_for_dlt(linktype);
364 }
365 return snaplen;
366 }
367
368 static pcap_t *(*check_headers[])(const uint8_t *, FILE *, u_int, char *, int *) = {
369 pcap_check_header,
370 pcap_ng_check_header
371 };
372
373 #define N_FILE_TYPES (sizeof check_headers / sizeof check_headers[0])
374
375 #ifdef _WIN32
376 static
377 #endif
378 pcap_t *
379 pcap_fopen_offline_with_tstamp_precision(FILE *fp, u_int precision,
380 char *errbuf)
381 {
382 register pcap_t *p;
383 uint8_t magic[4];
384 size_t amt_read;
385 u_int i;
386 int err;
387
388 /*
389 * Fail if we were passed a NULL fp.
390 *
391 * That shouldn't happen if we're opening with a path name, but
392 * it could happen if buggy code is opening with a FILE * and
393 * didn't bother to make sure the FILE * isn't null.
394 */
395 if (fp == NULL) {
396 snprintf(errbuf, PCAP_ERRBUF_SIZE,
397 "Null FILE * pointer provided to savefile open routine");
398 return (NULL);
399 }
400
401 /*
402 * Read the first 4 bytes of the file; the network analyzer dump
403 * file formats we support (pcap and pcapng), and several other
404 * formats we might support in the future (such as snoop, DOS and
405 * Windows Sniffer, and Microsoft Network Monitor) all have magic
406 * numbers that are unique in their first 4 bytes.
407 */
408 amt_read = fread(&magic, 1, sizeof(magic), fp);
409 if (amt_read != sizeof(magic)) {
410 if (ferror(fp)) {
411 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
412 errno, "error reading dump file");
413 } else {
414 snprintf(errbuf, PCAP_ERRBUF_SIZE,
415 "truncated dump file; tried to read %zu file header bytes, only got %zu",
416 sizeof(magic), amt_read);
417 }
418 return (NULL);
419 }
420
421 /*
422 * Try all file types.
423 */
424 for (i = 0; i < N_FILE_TYPES; i++) {
425 p = (*check_headers[i])(magic, fp, precision, errbuf, &err);
426 if (p != NULL) {
427 /* Yup, that's it. */
428 goto found;
429 }
430 if (err) {
431 /*
432 * Error trying to read the header.
433 */
434 return (NULL);
435 }
436 }
437
438 /*
439 * Well, who knows what this mess is....
440 */
441 snprintf(errbuf, PCAP_ERRBUF_SIZE, "unknown file format");
442 return (NULL);
443
444 found:
445 p->rfile = fp;
446
447 /* Padding only needed for live capture fcode */
448 p->fddipad = 0;
449
450 #if !defined(_WIN32) && !defined(MSDOS)
451 /*
452 * You can do "select()" and "poll()" on plain files on most
453 * platforms, and should be able to do so on pipes.
454 *
455 * You can't do "select()" on anything other than sockets in
456 * Windows, so, on Win32 systems, we don't have "selectable_fd".
457 */
458 p->selectable_fd = fileno(fp);
459 #endif
460
461 p->read_op = pcap_offline_read;
462 p->inject_op = sf_inject;
463 p->setfilter_op = install_bpf_program;
464 p->setdirection_op = sf_setdirection;
465 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
466 p->getnonblock_op = sf_getnonblock;
467 p->setnonblock_op = sf_setnonblock;
468 p->stats_op = sf_stats;
469 #ifdef _WIN32
470 p->stats_ex_op = sf_stats_ex;
471 p->setbuff_op = sf_setbuff;
472 p->setmode_op = sf_setmode;
473 p->setmintocopy_op = sf_setmintocopy;
474 p->getevent_op = sf_getevent;
475 p->oid_get_request_op = sf_oid_get_request;
476 p->oid_set_request_op = sf_oid_set_request;
477 p->sendqueue_transmit_op = sf_sendqueue_transmit;
478 p->setuserbuffer_op = sf_setuserbuffer;
479 p->live_dump_op = sf_live_dump;
480 p->live_dump_ended_op = sf_live_dump_ended;
481 p->get_airpcap_handle_op = sf_get_airpcap_handle;
482 #endif
483
484 /*
485 * For offline captures, the standard one-shot callback can
486 * be used for pcap_next()/pcap_next_ex().
487 */
488 p->oneshot_callback = pcap_oneshot;
489
490 /*
491 * Default breakloop operation.
492 */
493 p->breakloop_op = pcap_breakloop_common;
494
495 /*
496 * Savefiles never require special BPF code generation.
497 */
498 p->bpf_codegen_flags = 0;
499
500 p->activated = 1;
501
502 return (p);
503 }
504
505 /*
506 * This isn't needed on Windows; we #define pcap_fopen_offline() as
507 * a wrapper around pcap_hopen_offline(), and we don't call it from
508 * inside this file, so it's unused.
509 */
510 #ifndef _WIN32
511 pcap_t *
512 pcap_fopen_offline(FILE *fp, char *errbuf)
513 {
514 return (pcap_fopen_offline_with_tstamp_precision(fp,
515 PCAP_TSTAMP_PRECISION_MICRO, errbuf));
516 }
517 #endif
518
519 /*
520 * Read packets from a capture file, and call the callback for each
521 * packet.
522 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
523 */
524 int
525 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
526 {
527 struct bpf_insn *fcode;
528 int status = 0;
529 int n = 0;
530 u_char *data;
531
532 while (status == 0) {
533 struct pcap_pkthdr h;
534
535 /*
536 * Has "pcap_breakloop()" been called?
537 * If so, return immediately - if we haven't read any
538 * packets, clear the flag and return -2 to indicate
539 * that we were told to break out of the loop, otherwise
540 * leave the flag set, so that the *next* call will break
541 * out of the loop without having read any packets, and
542 * return the number of packets we've processed so far.
543 */
544 if (p->break_loop) {
545 if (n == 0) {
546 p->break_loop = 0;
547 return (-2);
548 } else
549 return (n);
550 }
551
552 status = p->next_packet_op(p, &h, &data);
553 if (status) {
554 if (status == 1)
555 return (0);
556 return (status);
557 }
558
559 if ((fcode = p->fcode.bf_insns) == NULL ||
560 pcap_filter(fcode, data, h.len, h.caplen)) {
561 (*callback)(user, &h, data);
562 if (++n >= cnt && cnt > 0)
563 break;
564 }
565 }
566 /*XXX this breaks semantics tcpslice expects */
567 return (n);
568 }