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