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