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