]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
some getaddrinfo(3) can return multiple address against SOCK_STREAM (like tcp
[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 #ifndef lint
32 static const char rcsid[] _U_ =
33 "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.100 2003-12-21 21:58:50 guy Exp $ (LBL)";
34 #endif
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <errno.h>
41 #include <memory.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "pcap-int.h"
47
48 #ifdef HAVE_OS_PROTO_H
49 #include "os-proto.h"
50 #endif
51
52 #define TCPDUMP_MAGIC 0xa1b2c3d4
53 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34
54
55 /*
56 * We use the "receiver-makes-right" approach to byte order,
57 * because time is at a premium when we are writing the file.
58 * In other words, the pcap_file_header and pcap_pkthdr,
59 * records are written in host byte order.
60 * Note that the bytes of packet data are written out in the order in
61 * which they were received, so multi-byte fields in packets are not
62 * written in host byte order, they're written in whatever order the
63 * sending machine put them in.
64 *
65 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
66 * machine (if the file was written in little-end order).
67 */
68 #define SWAPLONG(y) \
69 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
70 #define SWAPSHORT(y) \
71 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
72
73 #define SFERR_TRUNC 1
74 #define SFERR_BADVERSION 2
75 #define SFERR_BADF 3
76 #define SFERR_EOF 4 /* not really an error, just a status */
77
78 /*
79 * We don't write DLT_* values to the capture file header, because
80 * they're not the same on all platforms.
81 *
82 * Unfortunately, the various flavors of BSD have not always used the same
83 * numerical values for the same data types, and various patches to
84 * libpcap for non-BSD OSes have added their own DLT_* codes for link
85 * layer encapsulation types seen on those OSes, and those codes have had,
86 * in some cases, values that were also used, on other platforms, for other
87 * link layer encapsulation types.
88 *
89 * This means that capture files of a type whose numerical DLT_* code
90 * means different things on different BSDs, or with different versions
91 * of libpcap, can't always be read on systems other than those like
92 * the one running on the machine on which the capture was made.
93 *
94 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
95 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
96 * codes to DLT_* codes when reading a savefile header.
97 *
98 * For those DLT_* codes that have, as far as we know, the same values on
99 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
100 * DLT_xxx; that way, captures of those types can still be read by
101 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
102 * captures of those types written by versions of libpcap that map DLT_
103 * values to LINKTYPE_ values can still be read by older versions
104 * of libpcap.
105 *
106 * The other LINKTYPE_* codes are given values starting at 100, in the
107 * hopes that no DLT_* code will be given one of those values.
108 *
109 * In order to ensure that a given LINKTYPE_* code's value will refer to
110 * the same encapsulation type on all platforms, you should not allocate
111 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
112 * The tcpdump developers will allocate a value for you, and will not
113 * subsequently allocate it to anybody else; that value will be added to
114 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
115 * libpcap release will include it.
116 *
117 * You should, if possible, also contribute patches to libpcap and tcpdump
118 * to handle the new encapsulation type, so that they can also be checked
119 * into the tcpdump.org CVS repository and so that they will appear in
120 * future libpcap and tcpdump releases.
121 */
122 #define LINKTYPE_NULL DLT_NULL
123 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
124 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
125 #define LINKTYPE_AX25 DLT_AX25
126 #define LINKTYPE_PRONET DLT_PRONET
127 #define LINKTYPE_CHAOS DLT_CHAOS
128 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
129 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
130 #define LINKTYPE_SLIP DLT_SLIP
131 #define LINKTYPE_PPP DLT_PPP
132 #define LINKTYPE_FDDI DLT_FDDI
133
134 /*
135 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
136 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
137 * field) at the beginning of the packet.
138 *
139 * This is for use when there is always such a header; the address field
140 * might be 0xff, for regular PPP, or it might be an address field for Cisco
141 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
142 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
143 *
144 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
145 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
146 * captures will be written out with a link type that NetBSD's tcpdump
147 * can read.
148 */
149 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
150
151 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
152
153 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
154 #define LINKTYPE_RAW 101 /* raw IP */
155 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
156 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
157 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
158 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
159 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
160 #define LINKTYPE_FRELAY 107 /* Frame Relay */
161 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
162 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
163
164 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
165 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
166 #define LINKTYPE_ECONET 115 /* Acorn Econet */
167
168 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
169 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
170 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
171 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
172 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
173 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
174
175 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus WLAN header */
176
177 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
178
179 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
180
181 #define LINKTYPE_JUNIPER_MLPPP 130 /* Juniper-internal chassis encapsulation */
182 #define LINKTYPE_JUNIPER_MLFR 131
183 #define LINKTYPE_JUNIPER_ES 132
184 #define LINKTYPE_JUNIPER_GGSN 133
185 #define LINKTYPE_JUNIPER_MFR 134
186 #define LINKTYPE_JUNIPER_ATM2 135
187 #define LINKTYPE_JUNIPER_SERVICES 136
188 #define LINKTYPE_JUNIPER_ATM1 137
189
190 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
191
192 #define LINKTYPE_RAWSS7 139 /* see rawss7.h for */
193 #define LINKTYPE_RAWSS7_MTP2 140 /* information on these */
194 #define LINKTYPE_RAWSS7_MTP3 141 /* definitions */
195 #define LINKTYPE_RAWSS7_SCCP 142
196
197 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
198
199 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
200
201 /*
202 * These types are reserved for future use.
203 */
204 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
205 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
206 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
207 #define LINKTYPE_IPFILTER 116 /* IP Filter capture files */
208 #define LINKTYPE_HHDLC 121 /* Siemens HiPath HDLC */
209 #define LINKTYPE_RIO 124 /* RapidIO */
210 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
211 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
212 #define LINKTYPE_IBM_SP 145 /* IBM SP switch */
213 #define LINKTYPE_IBM_SN 146 /* IBM Next Federation switch */
214
215 static struct linktype_map {
216 int dlt;
217 int linktype;
218 } map[] = {
219 /*
220 * These DLT_* codes have LINKTYPE_* codes with values identical
221 * to the values of the corresponding DLT_* code.
222 */
223 { DLT_NULL, LINKTYPE_NULL },
224 { DLT_EN10MB, LINKTYPE_ETHERNET },
225 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
226 { DLT_AX25, LINKTYPE_AX25 },
227 { DLT_PRONET, LINKTYPE_PRONET },
228 { DLT_CHAOS, LINKTYPE_CHAOS },
229 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
230 { DLT_ARCNET, LINKTYPE_ARCNET },
231 { DLT_SLIP, LINKTYPE_SLIP },
232 { DLT_PPP, LINKTYPE_PPP },
233 { DLT_FDDI, LINKTYPE_FDDI },
234
235 /*
236 * These DLT_* codes have different values on different
237 * platforms; we map them to LINKTYPE_* codes that
238 * have values that should never be equal to any DLT_*
239 * code.
240 */
241 #ifdef DLT_FR
242 /* BSD/OS Frame Relay */
243 { DLT_FR, LINKTYPE_FRELAY },
244 #endif
245 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
246 { DLT_RAW, LINKTYPE_RAW },
247 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
248 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
249
250 /* BSD/OS Cisco HDLC */
251 { DLT_C_HDLC, LINKTYPE_C_HDLC },
252
253 /*
254 * These DLT_* codes are not on all platforms, but, so far,
255 * there don't appear to be any platforms that define
256 * other codes with those values; we map them to
257 * different LINKTYPE_* values anyway, just in case.
258 */
259
260 /* Linux ATM Classical IP */
261 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
262
263 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
264 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
265
266 /* NetBSD PPP over Ethernet */
267 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
268
269 /* IEEE 802.11 wireless */
270 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
271
272 /* Frame Relay */
273 { DLT_FRELAY, LINKTYPE_FRELAY },
274
275 /* OpenBSD loopback */
276 { DLT_LOOP, LINKTYPE_LOOP },
277
278 /* Linux cooked socket capture */
279 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
280
281 /* Apple LocalTalk hardware */
282 { DLT_LTALK, LINKTYPE_LTALK },
283
284 /* Acorn Econet */
285 { DLT_ECONET, LINKTYPE_ECONET },
286
287 /* OpenBSD DLT_PFLOG */
288 { DLT_PFLOG, LINKTYPE_PFLOG },
289
290 /* For Cisco-internal use */
291 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
292
293 /* Prism II monitor-mode header plus 802.11 header */
294 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
295
296 /* FreeBSD Aironet driver stuff */
297 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
298
299 /* Siemens HiPath HDLC */
300 { DLT_HHDLC, LINKTYPE_HHDLC },
301
302 /* RFC 2625 IP-over-Fibre Channel */
303 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
304
305 /* Solaris+SunATM */
306 { DLT_SUNATM, LINKTYPE_SUNATM },
307
308 /* RapidIO */
309 { DLT_RIO, LINKTYPE_RIO },
310
311 /* PCI Express */
312 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
313
314 /* Xilinx Aurora link layer */
315 { DLT_AURORA, LINKTYPE_AURORA },
316
317 /* 802.11 plus WLAN header */
318 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
319
320 /* Tazmen Sniffer Protocol */
321 { DLT_TZSP, LINKTYPE_TZSP },
322
323 /* Arcnet with Linux-style link-layer headers */
324 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
325
326 /* Juniper-internal chassis encapsulation */
327 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
328 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
329 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
330 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
331 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
332 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
333 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
334 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 },
335
336 /* Apple IP-over-IEEE 1394 cooked header */
337 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 },
338
339 /* DOCSIS MAC frames */
340 { DLT_DOCSIS, LINKTYPE_DOCSIS },
341
342 /* IrDA IrLAP packets + Linux-cooked header */
343 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA },
344
345 /* IBM SP and Next Federation switches */
346 { DLT_IBM_SP, LINKTYPE_IBM_SP },
347 { DLT_IBM_SN, LINKTYPE_IBM_SN },
348
349 /*
350 * Any platform that defines additional DLT_* codes should:
351 *
352 * request a LINKTYPE_* code and value from tcpdump.org,
353 * as per the above;
354 *
355 * add, in their version of libpcap, an entry to map
356 * those DLT_* codes to the corresponding LINKTYPE_*
357 * code;
358 *
359 * redefine, in their "net/bpf.h", any DLT_* values
360 * that collide with the values used by their additional
361 * DLT_* codes, to remove those collisions (but without
362 * making them collide with any of the LINKTYPE_*
363 * values equal to 50 or above; they should also avoid
364 * defining DLT_* values that collide with those
365 * LINKTYPE_* values, either).
366 */
367 { -1, -1 }
368 };
369
370 static int
371 dlt_to_linktype(int dlt)
372 {
373 int i;
374
375 for (i = 0; map[i].dlt != -1; i++) {
376 if (map[i].dlt == dlt)
377 return (map[i].linktype);
378 }
379
380 /*
381 * If we don't have a mapping for this DLT_ code, return an
382 * error; that means that the table above needs to have an
383 * entry added.
384 */
385 return (-1);
386 }
387
388 static int
389 linktype_to_dlt(int linktype)
390 {
391 int i;
392
393 for (i = 0; map[i].linktype != -1; i++) {
394 if (map[i].linktype == linktype)
395 return (map[i].dlt);
396 }
397
398 /*
399 * If we don't have an entry for this link type, return
400 * the link type value; it may be a DLT_ value from an
401 * older version of libpcap.
402 */
403 return linktype;
404 }
405
406 static int
407 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
408 {
409 struct pcap_file_header hdr;
410
411 hdr.magic = TCPDUMP_MAGIC;
412 hdr.version_major = PCAP_VERSION_MAJOR;
413 hdr.version_minor = PCAP_VERSION_MINOR;
414
415 hdr.thiszone = thiszone;
416 hdr.snaplen = snaplen;
417 hdr.sigfigs = 0;
418 hdr.linktype = linktype;
419
420 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
421 return (-1);
422
423 return (0);
424 }
425
426 static void
427 swap_hdr(struct pcap_file_header *hp)
428 {
429 hp->version_major = SWAPSHORT(hp->version_major);
430 hp->version_minor = SWAPSHORT(hp->version_minor);
431 hp->thiszone = SWAPLONG(hp->thiszone);
432 hp->sigfigs = SWAPLONG(hp->sigfigs);
433 hp->snaplen = SWAPLONG(hp->snaplen);
434 hp->linktype = SWAPLONG(hp->linktype);
435 }
436
437 static int
438 sf_getnonblock(pcap_t *p, char *errbuf)
439 {
440 /*
441 * This is a savefile, not a live capture file, so never say
442 * it's in non-blocking mode.
443 */
444 return (0);
445 }
446
447 static int
448 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
449 {
450 /*
451 * This is a savefile, not a live capture file, so ignore
452 * requests to put it in non-blocking mode.
453 */
454 return (0);
455 }
456
457 static int
458 sf_stats(pcap_t *p, struct pcap_stat *ps)
459 {
460 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
461 "Statistics aren't available from savefiles");
462 return (-1);
463 }
464
465 static void
466 sf_close(pcap_t *p)
467 {
468 if (p->sf.rfile != stdin)
469 (void)fclose(p->sf.rfile);
470 if (p->sf.base != NULL)
471 free(p->sf.base);
472 }
473
474 pcap_t *
475 pcap_open_offline(const char *fname, char *errbuf)
476 {
477 register pcap_t *p;
478 register FILE *fp;
479 struct pcap_file_header hdr;
480 bpf_u_int32 magic;
481 int linklen;
482
483 p = (pcap_t *)malloc(sizeof(*p));
484 if (p == NULL) {
485 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
486 return (NULL);
487 }
488
489 memset((char *)p, 0, sizeof(*p));
490
491 if (fname[0] == '-' && fname[1] == '\0')
492 fp = stdin;
493 else {
494 #ifndef WIN32
495 fp = fopen(fname, "r");
496 #else
497 fp = fopen(fname, "rb");
498 #endif
499 if (fp == NULL) {
500 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
501 pcap_strerror(errno));
502 goto bad;
503 }
504 }
505 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
506 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
507 pcap_strerror(errno));
508 goto bad;
509 }
510 magic = hdr.magic;
511 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
512 magic = SWAPLONG(magic);
513 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
514 snprintf(errbuf, PCAP_ERRBUF_SIZE,
515 "bad dump file format");
516 goto bad;
517 }
518 p->sf.swapped = 1;
519 swap_hdr(&hdr);
520 }
521 if (magic == PATCHED_TCPDUMP_MAGIC) {
522 /*
523 * XXX - the patch that's in some versions of libpcap
524 * changes the packet header but not the magic number;
525 * we'd have to use some hacks^H^H^H^H^Hheuristics to
526 * detect that.
527 */
528 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
529 } else
530 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
531 if (hdr.version_major < PCAP_VERSION_MAJOR) {
532 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
533 goto bad;
534 }
535 p->tzoff = hdr.thiszone;
536 p->snapshot = hdr.snaplen;
537 p->linktype = linktype_to_dlt(hdr.linktype);
538 p->sf.rfile = fp;
539 #ifndef WIN32
540 p->bufsize = hdr.snaplen;
541 #else
542 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
543 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
544 #endif
545
546 /* Align link header as required for proper data alignment */
547 /* XXX should handle all types */
548 switch (p->linktype) {
549
550 case DLT_EN10MB:
551 linklen = 14;
552 break;
553
554 case DLT_FDDI:
555 linklen = 13 + 8; /* fddi_header + llc */
556 break;
557
558 case DLT_NULL:
559 default:
560 linklen = 0;
561 break;
562 }
563
564 if (p->bufsize < 0)
565 p->bufsize = BPF_MAXBUFSIZE;
566 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
567 if (p->sf.base == NULL) {
568 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
569 goto bad;
570 }
571 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
572 p->sf.version_major = hdr.version_major;
573 p->sf.version_minor = hdr.version_minor;
574 #ifdef PCAP_FDDIPAD
575 /* XXX padding only needed for kernel fcode */
576 pcap_fddipad = 0;
577 #endif
578
579 /*
580 * We interchanged the caplen and len fields at version 2.3,
581 * in order to match the bpf header layout. But unfortunately
582 * some files were written with version 2.3 in their headers
583 * but without the interchanged fields.
584 *
585 * In addition, DG/UX tcpdump writes out files with a version
586 * number of 543.0, and with the caplen and len fields in the
587 * pre-2.3 order.
588 */
589 switch (hdr.version_major) {
590
591 case 2:
592 if (hdr.version_minor < 3)
593 p->sf.lengths_swapped = SWAPPED;
594 else if (hdr.version_minor == 3)
595 p->sf.lengths_swapped = MAYBE_SWAPPED;
596 else
597 p->sf.lengths_swapped = NOT_SWAPPED;
598 break;
599
600 case 543:
601 p->sf.lengths_swapped = SWAPPED;
602 break;
603
604 default:
605 p->sf.lengths_swapped = NOT_SWAPPED;
606 break;
607 }
608
609 #ifndef WIN32
610 /*
611 * You can do "select()" and "poll()" on plain files on most
612 * platforms, and should be able to do so on pipes.
613 *
614 * You can't do "select()" on anything other than sockets in
615 * Windows, so, on Win32 systems, we don't have "selectable_fd".
616 */
617 p->selectable_fd = fileno(fp);
618 #endif
619
620 p->read_op = pcap_offline_read;
621 p->setfilter_op = install_bpf_program;
622 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
623 p->getnonblock_op = sf_getnonblock;
624 p->setnonblock_op = sf_setnonblock;
625 p->stats_op = sf_stats;
626 p->close_op = sf_close;
627
628 return (p);
629 bad:
630 if(fp)
631 fclose(fp);
632 free(p);
633 return (NULL);
634 }
635
636 /*
637 * Read sf_readfile and return the next packet. Return the header in hdr
638 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
639 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
640 */
641 static int
642 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen)
643 {
644 struct pcap_sf_patched_pkthdr sf_hdr;
645 FILE *fp = p->sf.rfile;
646 size_t amt_read;
647 bpf_u_int32 t;
648
649 /*
650 * Read the packet header; the structure we use as a buffer
651 * is the longer structure for files generated by the patched
652 * libpcap, but if the file has the magic number for an
653 * unpatched libpcap we only read as many bytes as the regular
654 * header has.
655 */
656 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp);
657 if (amt_read != p->sf.hdrsize) {
658 if (ferror(fp)) {
659 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
660 "error reading dump file: %s",
661 pcap_strerror(errno));
662 return (-1);
663 } else {
664 if (amt_read != 0) {
665 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
666 "truncated dump file; tried to read %d header bytes, only got %lu",
667 p->sf.hdrsize, (unsigned long)amt_read);
668 return (-1);
669 }
670 /* EOF */
671 return (1);
672 }
673 }
674
675 if (p->sf.swapped) {
676 /* these were written in opposite byte order */
677 hdr->caplen = SWAPLONG(sf_hdr.caplen);
678 hdr->len = SWAPLONG(sf_hdr.len);
679 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
680 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
681 } else {
682 hdr->caplen = sf_hdr.caplen;
683 hdr->len = sf_hdr.len;
684 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
685 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
686 }
687 /* Swap the caplen and len fields, if necessary. */
688 switch (p->sf.lengths_swapped) {
689
690 case NOT_SWAPPED:
691 break;
692
693 case MAYBE_SWAPPED:
694 if (hdr->caplen <= hdr->len) {
695 /*
696 * The captured length is <= the actual length,
697 * so presumably they weren't swapped.
698 */
699 break;
700 }
701 /* FALLTHROUGH */
702
703 case SWAPPED:
704 t = hdr->caplen;
705 hdr->caplen = hdr->len;
706 hdr->len = t;
707 break;
708 }
709
710 if (hdr->caplen > buflen) {
711 /*
712 * This can happen due to Solaris 2.3 systems tripping
713 * over the BUFMOD problem and not setting the snapshot
714 * correctly in the savefile header. If the caplen isn't
715 * grossly wrong, try to salvage.
716 */
717 static u_char *tp = NULL;
718 static size_t tsize = 0;
719
720 if (hdr->caplen > 65535) {
721 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
722 "bogus savefile header");
723 return (-1);
724 }
725
726 if (tsize < hdr->caplen) {
727 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
728 if (tp != NULL)
729 free((u_char *)tp);
730 tp = (u_char *)malloc(tsize);
731 if (tp == NULL) {
732 tsize = 0;
733 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
734 "BUFMOD hack malloc");
735 return (-1);
736 }
737 }
738 amt_read = fread((char *)tp, 1, hdr->caplen, fp);
739 if (amt_read != hdr->caplen) {
740 if (ferror(fp)) {
741 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
742 "error reading dump file: %s",
743 pcap_strerror(errno));
744 } else {
745 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
746 "truncated dump file; tried to read %u captured bytes, only got %lu",
747 hdr->caplen, (unsigned long)amt_read);
748 }
749 return (-1);
750 }
751 /*
752 * We can only keep up to buflen bytes. Since caplen > buflen
753 * is exactly how we got here, we know we can only keep the
754 * first buflen bytes and must drop the remainder. Adjust
755 * caplen accordingly, so we don't get confused later as
756 * to how many bytes we have to play with.
757 */
758 hdr->caplen = buflen;
759 memcpy((char *)buf, (char *)tp, buflen);
760
761 } else {
762 /* read the packet itself */
763 amt_read = fread((char *)buf, 1, hdr->caplen, fp);
764 if (amt_read != hdr->caplen) {
765 if (ferror(fp)) {
766 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
767 "error reading dump file: %s",
768 pcap_strerror(errno));
769 } else {
770 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
771 "truncated dump file; tried to read %u captured bytes, only got %lu",
772 hdr->caplen, (unsigned long)amt_read);
773 }
774 return (-1);
775 }
776 }
777 return (0);
778 }
779
780 /*
781 * Print out packets stored in the file initialized by sf_read_init().
782 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
783 */
784 int
785 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
786 {
787 struct bpf_insn *fcode = p->fcode.bf_insns;
788 int status = 0;
789 int n = 0;
790
791 while (status == 0) {
792 struct pcap_pkthdr h;
793
794 /*
795 * Has "pcap_breakloop()" been called?
796 * If so, return immediately - if we haven't read any
797 * packets, clear the flag and return -2 to indicate
798 * that we were told to break out of the loop, otherwise
799 * leave the flag set, so that the *next* call will break
800 * out of the loop without having read any packets, and
801 * return the number of packets we've processed so far.
802 */
803 if (p->break_loop) {
804 if (n == 0) {
805 p->break_loop = 0;
806 return (-2);
807 } else
808 return (n);
809 }
810
811 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
812 if (status) {
813 if (status == 1)
814 return (0);
815 return (status);
816 }
817
818 if (fcode == NULL ||
819 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
820 (*callback)(user, &h, p->buffer);
821 if (++n >= cnt && cnt > 0)
822 break;
823 }
824 }
825 /*XXX this breaks semantics tcpslice expects */
826 return (n);
827 }
828
829 /*
830 * Output a packet to the initialized dump file.
831 */
832 void
833 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
834 {
835 register FILE *f;
836 struct pcap_sf_pkthdr sf_hdr;
837
838 f = (FILE *)user;
839 sf_hdr.ts.tv_sec = h->ts.tv_sec;
840 sf_hdr.ts.tv_usec = h->ts.tv_usec;
841 sf_hdr.caplen = h->caplen;
842 sf_hdr.len = h->len;
843 /* XXX we should check the return status */
844 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
845 (void)fwrite((char *)sp, h->caplen, 1, f);
846 }
847
848 /*
849 * Initialize so that sf_write() will output to the file named 'fname'.
850 */
851 pcap_dumper_t *
852 pcap_dump_open(pcap_t *p, const char *fname)
853 {
854 FILE *f;
855 int linktype;
856
857 linktype = dlt_to_linktype(p->linktype);
858 if (linktype == -1) {
859 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
860 "%s: link-layer type %d isn't supported in savefiles",
861 fname, linktype);
862 return (NULL);
863 }
864
865 if (fname[0] == '-' && fname[1] == '\0') {
866 f = stdout;
867 #ifdef WIN32
868 _setmode(_fileno(f), _O_BINARY);
869 #endif
870 } else {
871 #ifndef WIN32
872 f = fopen(fname, "w");
873 #else
874 f = fopen(fname, "wb");
875 setbuf(f, NULL); /* XXX - why? */
876 #endif
877 if (f == NULL) {
878 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
879 fname, pcap_strerror(errno));
880 return (NULL);
881 }
882 }
883 (void)sf_write_header(f, linktype, p->tzoff, p->snapshot);
884 return ((pcap_dumper_t *)f);
885 }
886
887 FILE *
888 pcap_dump_file(pcap_dumper_t *p)
889 {
890 return ((FILE *)p);
891 }
892
893 int
894 pcap_dump_flush(pcap_dumper_t *p)
895 {
896
897 if (fflush((FILE *)p) == EOF)
898 return (-1);
899 else
900 return (0);
901 }
902
903 void
904 pcap_dump_close(pcap_dumper_t *p)
905 {
906
907 #ifdef notyet
908 if (ferror((FILE *)p))
909 return-an-error;
910 /* XXX should check return from fclose() too */
911 #endif
912 (void)fclose((FILE *)p);
913 }