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