]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
Add support for OpenBSD DLT_PFLOG.
[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[] =
33 "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.78 2003-03-11 06:23:55 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 packets are always written in network byte order.
61 *
62 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
63 * machine (if the file was written in little-end order).
64 */
65 #define SWAPLONG(y) \
66 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
67 #define SWAPSHORT(y) \
68 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
69
70 #define SFERR_TRUNC 1
71 #define SFERR_BADVERSION 2
72 #define SFERR_BADF 3
73 #define SFERR_EOF 4 /* not really an error, just a status */
74
75 /*
76 * We don't write DLT_* values to the capture file header, because
77 * they're not the same on all platforms.
78 *
79 * Unfortunately, the various flavors of BSD have not always used the same
80 * numerical values for the same data types, and various patches to
81 * libpcap for non-BSD OSes have added their own DLT_* codes for link
82 * layer encapsulation types seen on those OSes, and those codes have had,
83 * in some cases, values that were also used, on other platforms, for other
84 * link layer encapsulation types.
85 *
86 * This means that capture files of a type whose numerical DLT_* code
87 * means different things on different BSDs, or with different versions
88 * of libpcap, can't always be read on systems other than those like
89 * the one running on the machine on which the capture was made.
90 *
91 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
92 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
93 * codes to DLT_* codes when reading a savefile header.
94 *
95 * For those DLT_* codes that have, as far as we know, the same values on
96 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
97 * DLT_xxx; that way, captures of those types can still be read by
98 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
99 * captures of those types written by versions of libpcap that map DLT_
100 * values to LINKTYPE_ values can still be read by older versions
101 * of libpcap.
102 *
103 * The other LINKTYPE_* codes are given values starting at 100, in the
104 * hopes that no DLT_* code will be given one of those values.
105 *
106 * In order to ensure that a given LINKTYPE_* code's value will refer to
107 * the same encapsulation type on all platforms, you should not allocate
108 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
109 * The tcpdump developers will allocate a value for you, and will not
110 * subsequently allocate it to anybody else; that value will be added to
111 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
112 * libpcap release will include it.
113 *
114 * You should, if possible, also contribute patches to libpcap and tcpdump
115 * to handle the new encapsulation type, so that they can also be checked
116 * into the tcpdump.org CVS repository and so that they will appear in
117 * future libpcap and tcpdump releases.
118 */
119 #define LINKTYPE_NULL DLT_NULL
120 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
121 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
122 #define LINKTYPE_AX25 DLT_AX25
123 #define LINKTYPE_PRONET DLT_PRONET
124 #define LINKTYPE_CHAOS DLT_CHAOS
125 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
126 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
127 #define LINKTYPE_SLIP DLT_SLIP
128 #define LINKTYPE_PPP DLT_PPP
129 #define LINKTYPE_FDDI DLT_FDDI
130
131 /*
132 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
133 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
134 * field) at the beginning of the packet.
135 *
136 * This is for use when there is always such a header; the address field
137 * might be 0xff, for regular PPP, or it might be an address field for Cisco
138 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
139 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
140 *
141 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
142 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
143 * captures will be written out with a link type that NetBSD's tcpdump
144 * can read.
145 */
146 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
147
148 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
149
150 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
151 #define LINKTYPE_RAW 101 /* raw IP */
152 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
153 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
154 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
155 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
156 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
157 #define LINKTYPE_FRELAY 107 /* Frame Relay */
158 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
159 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
160
161 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
162 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
163 #define LINKTYPE_ECONET 115 /* Acorn Econet */
164
165 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
166 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
167 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
168 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
169 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
170 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
171
172 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus WLAN header */
173
174 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
175
176 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
177
178 #define LINKTYPE_JUNIPER_MLPPP 130 /* Juniper-internal chassis encapsulation */
179 #define LINKTYPE_JUNIPER_MLFR 131
180 #define LINKTYPE_JUNIPER_ES 132
181 #define LINKTYPE_JUNIPER_GGSN 133
182 #define LINKTYPE_JUNIPER_MFR 134
183 #define LINKTYPE_JUNIPER_ATM2 135
184 #define LINKTYPE_JUNIPER_SERVICES 136
185
186 /*
187 * These types are reserved for future use.
188 */
189 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
190 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
191 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
192 #define LINKTYPE_IPFILTER 116 /* IP Filter capture files */
193 #define LINKTYPE_HHDLC 121 /* Siemens HiPath HDLC */
194 #define LINKTYPE_RIO 124 /* RapidIO */
195 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
196 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
197
198 static struct linktype_map {
199 int dlt;
200 int linktype;
201 } map[] = {
202 /*
203 * These DLT_* codes have LINKTYPE_* codes with values identical
204 * to the values of the corresponding DLT_* code.
205 */
206 { DLT_NULL, LINKTYPE_NULL },
207 { DLT_EN10MB, LINKTYPE_ETHERNET },
208 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
209 { DLT_AX25, LINKTYPE_AX25 },
210 { DLT_PRONET, LINKTYPE_PRONET },
211 { DLT_CHAOS, LINKTYPE_CHAOS },
212 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
213 { DLT_ARCNET, LINKTYPE_ARCNET },
214 { DLT_SLIP, LINKTYPE_SLIP },
215 { DLT_PPP, LINKTYPE_PPP },
216 { DLT_FDDI, LINKTYPE_FDDI },
217
218 /*
219 * These DLT_* codes have different values on different
220 * platforms; we map them to LINKTYPE_* codes that
221 * have values that should never be equal to any DLT_*
222 * code.
223 */
224 #ifdef DLT_FR
225 /* BSD/OS Frame Relay */
226 { DLT_FR, LINKTYPE_FRELAY },
227 #endif
228 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
229 { DLT_RAW, LINKTYPE_RAW },
230 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
231 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
232
233 /* BSD/OS Cisco HDLC */
234 { DLT_C_HDLC, LINKTYPE_C_HDLC },
235
236 /*
237 * These DLT_* codes are not on all platforms, but, so far,
238 * there don't appear to be any platforms that define
239 * other codes with those values; we map them to
240 * different LINKTYPE_* values anyway, just in case.
241 */
242
243 /* Linux ATM Classical IP */
244 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
245
246 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
247 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
248
249 /* NetBSD PPP over Ethernet */
250 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
251
252 /* IEEE 802.11 wireless */
253 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
254
255 /* Frame Relay */
256 { DLT_FRELAY, LINKTYPE_FRELAY },
257
258 /* OpenBSD loopback */
259 { DLT_LOOP, LINKTYPE_LOOP },
260
261 /* Linux cooked socket capture */
262 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
263
264 /* Apple LocalTalk hardware */
265 { DLT_LTALK, LINKTYPE_LTALK },
266
267 /* Acorn Econet */
268 { DLT_ECONET, LINKTYPE_ECONET },
269
270 /* OpenBSD DLT_PFLOG */
271 { DLT_PFLOG, LINKTYPE_PFLOG },
272
273 /* For Cisco-internal use */
274 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
275
276 /* Prism II monitor-mode header plus 802.11 header */
277 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
278
279 /* FreeBSD Aironet driver stuff */
280 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
281
282 /* Siemens HiPath HDLC */
283 { DLT_HHDLC, LINKTYPE_HHDLC },
284
285 /* RFC 2625 IP-over-Fibre Channel */
286 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
287
288 /* Solaris+SunATM */
289 { DLT_SUNATM, LINKTYPE_SUNATM },
290
291 /* RapidIO */
292 { DLT_RIO, LINKTYPE_RIO },
293
294 /* PCI Express */
295 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
296
297 /* Xilinx Aurora link layer */
298 { DLT_AURORA, LINKTYPE_AURORA },
299
300 /* 802.11 plus WLAN header */
301 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
302
303 /* Tazmen Sniffer Protocol */
304 { DLT_TZSP, LINKTYPE_TZSP },
305
306 /* Arcnet with Linux-style link-layer headers */
307 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
308
309 /* Juniper-internal chassis encapsulation */
310 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
311 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
312 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
313 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
314 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
315 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
316 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
317
318 /*
319 * Any platform that defines additional DLT_* codes should:
320 *
321 * request a LINKTYPE_* code and value from tcpdump.org,
322 * as per the above;
323 *
324 * add, in their version of libpcap, an entry to map
325 * those DLT_* codes to the corresponding LINKTYPE_*
326 * code;
327 *
328 * redefine, in their "net/bpf.h", any DLT_* values
329 * that collide with the values used by their additional
330 * DLT_* codes, to remove those collisions (but without
331 * making them collide with any of the LINKTYPE_*
332 * values equal to 50 or above; they should also avoid
333 * defining DLT_* values that collide with those
334 * LINKTYPE_* values, either).
335 */
336 { -1, -1 }
337 };
338
339 static int
340 dlt_to_linktype(int dlt)
341 {
342 int i;
343
344 for (i = 0; map[i].dlt != -1; i++) {
345 if (map[i].dlt == dlt)
346 return (map[i].linktype);
347 }
348
349 /*
350 * If we don't have a mapping for this DLT_ code, return an
351 * error; that means that the table above needs to have an
352 * entry added.
353 */
354 return (-1);
355 }
356
357 static int
358 linktype_to_dlt(int linktype)
359 {
360 int i;
361
362 for (i = 0; map[i].linktype != -1; i++) {
363 if (map[i].linktype == linktype)
364 return (map[i].dlt);
365 }
366
367 /*
368 * If we don't have an entry for this link type, return
369 * the link type value; it may be a DLT_ value from an
370 * older version of libpcap.
371 */
372 return linktype;
373 }
374
375 static int
376 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
377 {
378 struct pcap_file_header hdr;
379
380 hdr.magic = TCPDUMP_MAGIC;
381 hdr.version_major = PCAP_VERSION_MAJOR;
382 hdr.version_minor = PCAP_VERSION_MINOR;
383
384 hdr.thiszone = thiszone;
385 hdr.snaplen = snaplen;
386 hdr.sigfigs = 0;
387 hdr.linktype = linktype;
388
389 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
390 return (-1);
391
392 return (0);
393 }
394
395 static void
396 swap_hdr(struct pcap_file_header *hp)
397 {
398 hp->version_major = SWAPSHORT(hp->version_major);
399 hp->version_minor = SWAPSHORT(hp->version_minor);
400 hp->thiszone = SWAPLONG(hp->thiszone);
401 hp->sigfigs = SWAPLONG(hp->sigfigs);
402 hp->snaplen = SWAPLONG(hp->snaplen);
403 hp->linktype = SWAPLONG(hp->linktype);
404 }
405
406 pcap_t *
407 pcap_open_offline(const char *fname, char *errbuf)
408 {
409 register pcap_t *p;
410 register FILE *fp;
411 struct pcap_file_header hdr;
412 bpf_u_int32 magic;
413 int linklen;
414
415 p = (pcap_t *)malloc(sizeof(*p));
416 if (p == NULL) {
417 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
418 return (NULL);
419 }
420
421 memset((char *)p, 0, sizeof(*p));
422 /*
423 * Set this field so we don't close stdin in pcap_close!
424 */
425 #ifndef WIN32
426 p->fd = -1;
427 #else
428 p->adapter = NULL;
429 #endif
430
431 if (fname[0] == '-' && fname[1] == '\0')
432 fp = stdin;
433 else {
434 #ifndef WIN32
435 fp = fopen(fname, "r");
436 #else
437 fp = fopen(fname, "rb");
438 #endif
439 if (fp == NULL) {
440 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
441 pcap_strerror(errno));
442 goto bad;
443 }
444 }
445 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
446 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
447 pcap_strerror(errno));
448 goto bad;
449 }
450 magic = hdr.magic;
451 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
452 magic = SWAPLONG(magic);
453 if (magic != TCPDUMP_MAGIC && magic != PATCHED_TCPDUMP_MAGIC) {
454 snprintf(errbuf, PCAP_ERRBUF_SIZE,
455 "bad dump file format");
456 goto bad;
457 }
458 p->sf.swapped = 1;
459 swap_hdr(&hdr);
460 }
461 if (magic == PATCHED_TCPDUMP_MAGIC) {
462 /*
463 * XXX - the patch that's in some versions of libpcap
464 * changes the packet header but not the magic number;
465 * we'd have to use some hacks^H^H^H^H^Hheuristics to
466 * detect that.
467 */
468 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
469 } else
470 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
471 if (hdr.version_major < PCAP_VERSION_MAJOR) {
472 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
473 goto bad;
474 }
475 p->tzoff = hdr.thiszone;
476 p->snapshot = hdr.snaplen;
477 p->linktype = linktype_to_dlt(hdr.linktype);
478 p->sf.rfile = fp;
479 #ifndef WIN32
480 p->bufsize = hdr.snaplen;
481 #else
482 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
483 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
484 #endif
485
486 /* Align link header as required for proper data alignment */
487 /* XXX should handle all types */
488 switch (p->linktype) {
489
490 case DLT_EN10MB:
491 linklen = 14;
492 break;
493
494 case DLT_FDDI:
495 linklen = 13 + 8; /* fddi_header + llc */
496 break;
497
498 case DLT_NULL:
499 default:
500 linklen = 0;
501 break;
502 }
503
504 if (p->bufsize < 0)
505 p->bufsize = BPF_MAXBUFSIZE;
506 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
507 if (p->sf.base == NULL) {
508 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
509 goto bad;
510 }
511 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
512 p->sf.version_major = hdr.version_major;
513 p->sf.version_minor = hdr.version_minor;
514 #ifdef PCAP_FDDIPAD
515 /* XXX padding only needed for kernel fcode */
516 pcap_fddipad = 0;
517 #endif
518
519 return (p);
520 bad:
521 if(fp)
522 fclose(fp);
523 free(p);
524 return (NULL);
525 }
526
527 /*
528 * Read sf_readfile and return the next packet. Return the header in hdr
529 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
530 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
531 */
532 #ifdef WIN32
533 int
534 #else
535 static int
536 #endif
537 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, int buflen)
538 {
539 struct pcap_sf_patched_pkthdr sf_hdr;
540 FILE *fp = p->sf.rfile;
541
542 /*
543 * Read the packet header; the structure we use as a buffer
544 * is the longer structure for files generated by the patched
545 * libpcap, but if the file has the magic number for an
546 * unpatched libpcap we only read as many bytes as the regular
547 * header has.
548 */
549 if (fread(&sf_hdr, p->sf.hdrsize, 1, fp) != 1) {
550 /* probably an EOF, though could be a truncated packet */
551 return (1);
552 }
553
554 if (p->sf.swapped) {
555 /* these were written in opposite byte order */
556 hdr->caplen = SWAPLONG(sf_hdr.caplen);
557 hdr->len = SWAPLONG(sf_hdr.len);
558 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
559 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
560 } else {
561 hdr->caplen = sf_hdr.caplen;
562 hdr->len = sf_hdr.len;
563 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
564 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
565 }
566 /*
567 * We interchanged the caplen and len fields at version 2.3,
568 * in order to match the bpf header layout. But unfortunately
569 * some files were written with version 2.3 in their headers
570 * but without the interchanged fields.
571 */
572 if (p->sf.version_minor < 3 ||
573 (p->sf.version_minor == 3 && hdr->caplen > hdr->len)) {
574 int t = hdr->caplen;
575 hdr->caplen = hdr->len;
576 hdr->len = t;
577 }
578
579 if (hdr->caplen > buflen) {
580 /*
581 * This can happen due to Solaris 2.3 systems tripping
582 * over the BUFMOD problem and not setting the snapshot
583 * correctly in the savefile header. If the caplen isn't
584 * grossly wrong, try to salvage.
585 */
586 static u_char *tp = NULL;
587 static int tsize = 0;
588
589 if (hdr->caplen > 65535) {
590 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
591 "bogus savefile header");
592 return (-1);
593 }
594
595 if (tsize < hdr->caplen) {
596 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
597 if (tp != NULL)
598 free((u_char *)tp);
599 tp = (u_char *)malloc(tsize);
600 if (tp == NULL) {
601 tsize = 0;
602 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
603 "BUFMOD hack malloc");
604 return (-1);
605 }
606 }
607 if (fread((char *)tp, hdr->caplen, 1, fp) != 1) {
608 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
609 "truncated dump file");
610 return (-1);
611 }
612 /*
613 * We can only keep up to buflen bytes. Since caplen > buflen
614 * is exactly how we got here, we know we can only keep the
615 * first buflen bytes and must drop the remainder. Adjust
616 * caplen accordingly, so we don't get confused later as
617 * to how many bytes we have to play with.
618 */
619 hdr->caplen = buflen;
620 memcpy((char *)buf, (char *)tp, buflen);
621
622 } else {
623 /* read the packet itself */
624
625 if (fread((char *)buf, hdr->caplen, 1, fp) != 1) {
626 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
627 "truncated dump file");
628 return (-1);
629 }
630 }
631 return (0);
632 }
633
634 /*
635 * Print out packets stored in the file initialized by sf_read_init().
636 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
637 */
638 int
639 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
640 {
641 struct bpf_insn *fcode = p->fcode.bf_insns;
642 int status = 0;
643 int n = 0;
644
645 while (status == 0) {
646 struct pcap_pkthdr h;
647
648 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
649 if (status) {
650 if (status == 1)
651 return (0);
652 return (status);
653 }
654
655 if (fcode == NULL ||
656 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
657 (*callback)(user, &h, p->buffer);
658 if (++n >= cnt && cnt > 0)
659 break;
660 }
661 }
662 /*XXX this breaks semantics tcpslice expects */
663 return (n);
664 }
665
666 /*
667 * Output a packet to the initialized dump file.
668 */
669 void
670 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
671 {
672 register FILE *f;
673 struct pcap_sf_pkthdr sf_hdr;
674
675 f = (FILE *)user;
676 sf_hdr.ts.tv_sec = h->ts.tv_sec;
677 sf_hdr.ts.tv_usec = h->ts.tv_usec;
678 sf_hdr.caplen = h->caplen;
679 sf_hdr.len = h->len;
680 /* XXX we should check the return status */
681 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
682 (void)fwrite((char *)sp, h->caplen, 1, f);
683 }
684
685 /*
686 * Initialize so that sf_write() will output to the file named 'fname'.
687 */
688 pcap_dumper_t *
689 pcap_dump_open(pcap_t *p, const char *fname)
690 {
691 FILE *f;
692 int linktype;
693
694 linktype = dlt_to_linktype(p->linktype);
695 if (linktype == -1) {
696 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
697 "%s: link-layer type %d isn't supported in savefiles",
698 fname, linktype);
699 return (NULL);
700 }
701
702 if (fname[0] == '-' && fname[1] == '\0') {
703 f = stdout;
704 #ifdef WIN32
705 _setmode(_fileno(f), _O_BINARY);
706 #endif
707 } else {
708 #ifndef WIN32
709 f = fopen(fname, "w");
710 #else
711 f = fopen(fname, "wb");
712 setbuf(f, NULL); /* XXX - why? */
713 #endif
714 if (f == NULL) {
715 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
716 fname, pcap_strerror(errno));
717 return (NULL);
718 }
719 }
720 (void)sf_write_header(f, linktype, p->tzoff, p->snapshot);
721 return ((pcap_dumper_t *)f);
722 }
723
724 int
725 pcap_dump_flush(pcap_dumper_t *p)
726 {
727
728 if (fflush((FILE *)p) == EOF)
729 return (-1);
730 else
731 return (0);
732 }
733
734 void
735 pcap_dump_close(pcap_dumper_t *p)
736 {
737
738 #ifdef notyet
739 if (ferror((FILE *)p))
740 return-an-error;
741 /* XXX should check return from fclose() too */
742 #endif
743 (void)fclose((FILE *)p);
744 }