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