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