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