]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
Linktype for IEEE 802.15.4, with address fields padded as done by Linux
[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.152 2007-04-03 07:18:27 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 #include "pcap/usb.h"
48
49 #ifdef HAVE_OS_PROTO_H
50 #include "os-proto.h"
51 #endif
52
53 /*
54 * Standard libpcap format.
55 */
56 #define TCPDUMP_MAGIC 0xa1b2c3d4
57
58 /*
59 * Alexey Kuznetzov's modified libpcap format.
60 */
61 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
62
63 /*
64 * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
65 * for another modified format.
66 */
67 #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
68
69 /*
70 * Navtel Communcations' format, with nanosecond timestamps,
71 * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>.
72 */
73 #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d
74
75 /*
76 * Normal libpcap format, except for seconds/nanoseconds timestamps,
77 * as per a request by Ulf Lamping <ulf.lamping@web.de>
78 */
79 #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
80
81 /*
82 * We use the "receiver-makes-right" approach to byte order,
83 * because time is at a premium when we are writing the file.
84 * In other words, the pcap_file_header and pcap_pkthdr,
85 * records are written in host byte order.
86 * Note that the bytes of packet data are written out in the order in
87 * which they were received, so multi-byte fields in packets are not
88 * written in host byte order, they're written in whatever order the
89 * sending machine put them in.
90 *
91 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
92 * machine (if the file was written in little-end order).
93 */
94 #define SWAPLONG(y) \
95 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
96 #define SWAPSHORT(y) \
97 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
98
99 #define SFERR_TRUNC 1
100 #define SFERR_BADVERSION 2
101 #define SFERR_BADF 3
102 #define SFERR_EOF 4 /* not really an error, just a status */
103
104 /*
105 * Setting O_BINARY on DOS/Windows is a bit tricky
106 */
107 #if defined(WIN32)
108 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
109 #elif defined(MSDOS)
110 #if defined(__HIGHC__)
111 #define SET_BINMODE(f) setmode(f, O_BINARY)
112 #else
113 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
114 #endif
115 #endif
116
117 /*
118 * We don't write DLT_* values to the capture file header, because
119 * they're not the same on all platforms.
120 *
121 * Unfortunately, the various flavors of BSD have not always used the same
122 * numerical values for the same data types, and various patches to
123 * libpcap for non-BSD OSes have added their own DLT_* codes for link
124 * layer encapsulation types seen on those OSes, and those codes have had,
125 * in some cases, values that were also used, on other platforms, for other
126 * link layer encapsulation types.
127 *
128 * This means that capture files of a type whose numerical DLT_* code
129 * means different things on different BSDs, or with different versions
130 * of libpcap, can't always be read on systems other than those like
131 * the one running on the machine on which the capture was made.
132 *
133 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
134 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
135 * codes to DLT_* codes when reading a savefile header.
136 *
137 * For those DLT_* codes that have, as far as we know, the same values on
138 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
139 * DLT_xxx; that way, captures of those types can still be read by
140 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
141 * captures of those types written by versions of libpcap that map DLT_
142 * values to LINKTYPE_ values can still be read by older versions
143 * of libpcap.
144 *
145 * The other LINKTYPE_* codes are given values starting at 100, in the
146 * hopes that no DLT_* code will be given one of those values.
147 *
148 * In order to ensure that a given LINKTYPE_* code's value will refer to
149 * the same encapsulation type on all platforms, you should not allocate
150 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
151 * The tcpdump developers will allocate a value for you, and will not
152 * subsequently allocate it to anybody else; that value will be added to
153 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
154 * libpcap release will include it.
155 *
156 * You should, if possible, also contribute patches to libpcap and tcpdump
157 * to handle the new encapsulation type, so that they can also be checked
158 * into the tcpdump.org CVS repository and so that they will appear in
159 * future libpcap and tcpdump releases.
160 *
161 * Do *NOT* assume that any values after the largest value in this file
162 * are available; you might not have the most up-to-date version of this
163 * file, and new values after that one might have been assigned. Also,
164 * do *NOT* use any values below 100 - those might already have been
165 * taken by one (or more!) organizations.
166 */
167 #define LINKTYPE_NULL DLT_NULL
168 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
169 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
170 #define LINKTYPE_AX25 DLT_AX25
171 #define LINKTYPE_PRONET DLT_PRONET
172 #define LINKTYPE_CHAOS DLT_CHAOS
173 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
174 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
175 #define LINKTYPE_SLIP DLT_SLIP
176 #define LINKTYPE_PPP DLT_PPP
177 #define LINKTYPE_FDDI DLT_FDDI
178
179 /*
180 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
181 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
182 * field) at the beginning of the packet.
183 *
184 * This is for use when there is always such a header; the address field
185 * might be 0xff, for regular PPP, or it might be an address field for Cisco
186 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
187 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
188 *
189 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
190 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
191 * captures will be written out with a link type that NetBSD's tcpdump
192 * can read.
193 */
194 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
195
196 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
197
198 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */
199
200 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
201 #define LINKTYPE_RAW 101 /* raw IP */
202 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
203 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
204 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
205 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
206 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
207 #define LINKTYPE_FRELAY 107 /* Frame Relay */
208 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
209 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
210
211 /*
212 * These three types are reserved for future use.
213 */
214 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
215 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
216 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
217
218 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
219 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
220 #define LINKTYPE_ECONET 115 /* Acorn Econet */
221
222 /*
223 * Reserved for use with OpenBSD ipfilter.
224 */
225 #define LINKTYPE_IPFILTER 116
226
227 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
228 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
229 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
230 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
231
232 /*
233 * Reserved for Siemens HiPath HDLC.
234 */
235 #define LINKTYPE_HHDLC 121
236
237 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
238 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
239
240 /*
241 * Reserved as per request from Kent Dahlgren <kent@praesum.com>
242 * for private use.
243 */
244 #define LINKTYPE_RIO 124 /* RapidIO */
245 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
246 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
247
248 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */
249
250 /*
251 * Reserved for the TZSP encapsulation, as per request from
252 * Chris Waters <chris.waters@networkchemistry.com>
253 * TZSP is a generic encapsulation for any other link type,
254 * which includes a means to include meta-information
255 * with the packet, e.g. signal strength and channel
256 * for 802.11 packets.
257 */
258 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
259
260 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
261
262 /*
263 * Juniper-private data link types, as per request from
264 * Hannes Gredler <hannes@juniper.net>. The corresponding
265 * DLT_s are used for passing on chassis-internal
266 * metainformation such as QOS profiles, etc..
267 */
268 #define LINKTYPE_JUNIPER_MLPPP 130
269 #define LINKTYPE_JUNIPER_MLFR 131
270 #define LINKTYPE_JUNIPER_ES 132
271 #define LINKTYPE_JUNIPER_GGSN 133
272 #define LINKTYPE_JUNIPER_MFR 134
273 #define LINKTYPE_JUNIPER_ATM2 135
274 #define LINKTYPE_JUNIPER_SERVICES 136
275 #define LINKTYPE_JUNIPER_ATM1 137
276
277 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
278
279 #define LINKTYPE_MTP2_WITH_PHDR 139
280 #define LINKTYPE_MTP2 140
281 #define LINKTYPE_MTP3 141
282 #define LINKTYPE_SCCP 142
283
284 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
285
286 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
287
288 /*
289 * Reserved for IBM SP switch and IBM Next Federation switch.
290 */
291 #define LINKTYPE_IBM_SP 145
292 #define LINKTYPE_IBM_SN 146
293
294 /*
295 * Reserved for private use. If you have some link-layer header type
296 * that you want to use within your organization, with the capture files
297 * using that link-layer header type not ever be sent outside your
298 * organization, you can use these values.
299 *
300 * No libpcap release will use these for any purpose, nor will any
301 * tcpdump release use them, either.
302 *
303 * Do *NOT* use these in capture files that you expect anybody not using
304 * your private versions of capture-file-reading tools to read; in
305 * particular, do *NOT* use them in products, otherwise you may find that
306 * people won't be able to use tcpdump, or snort, or Ethereal, or... to
307 * read capture files from your firewall/intrusion detection/traffic
308 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
309 * and you may also find that the developers of those applications will
310 * not accept patches to let them read those files.
311 *
312 * Also, do not use them if somebody might send you a capture using them
313 * for *their* private type and tools using them for *your* private type
314 * would have to read them.
315 *
316 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_
317 * and LINKTYPE_ value, as per the comment in pcap/bpf.h, and use the type
318 * you're given.
319 */
320 #define LINKTYPE_USER0 147
321 #define LINKTYPE_USER1 148
322 #define LINKTYPE_USER2 149
323 #define LINKTYPE_USER3 150
324 #define LINKTYPE_USER4 151
325 #define LINKTYPE_USER5 152
326 #define LINKTYPE_USER6 153
327 #define LINKTYPE_USER7 154
328 #define LINKTYPE_USER8 155
329 #define LINKTYPE_USER9 156
330 #define LINKTYPE_USER10 157
331 #define LINKTYPE_USER11 158
332 #define LINKTYPE_USER12 159
333 #define LINKTYPE_USER13 160
334 #define LINKTYPE_USER14 161
335 #define LINKTYPE_USER15 162
336
337 /*
338 * For future use with 802.11 captures - defined by AbsoluteValue
339 * Systems to store a number of bits of link-layer information
340 * including radio information:
341 *
342 * http://www.shaftnet.org/~pizza/software/capturefrm.txt
343 *
344 * but could and arguably should also be used by non-AVS Linux
345 * 802.11 drivers; that may happen in the future.
346 */
347 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */
348
349 /*
350 * Juniper-private data link type, as per request from
351 * Hannes Gredler <hannes@juniper.net>. The corresponding
352 * DLT_s are used for passing on chassis-internal
353 * metainformation such as QOS profiles, etc..
354 */
355 #define LINKTYPE_JUNIPER_MONITOR 164
356
357 /*
358 * Reserved for BACnet MS/TP.
359 */
360 #define LINKTYPE_BACNET_MS_TP 165
361
362 /*
363 * Another PPP variant as per request from Karsten Keil <kkeil@suse.de>.
364 *
365 * This is used in some OSes to allow a kernel socket filter to distinguish
366 * between incoming and outgoing packets, on a socket intended to
367 * supply pppd with outgoing packets so it can do dial-on-demand and
368 * hangup-on-lack-of-demand; incoming packets are filtered out so they
369 * don't cause pppd to hold the connection up (you don't want random
370 * input packets such as port scans, packets from old lost connections,
371 * etc. to force the connection to stay up).
372 *
373 * The first byte of the PPP header (0xff03) is modified to accomodate
374 * the direction - 0x00 = IN, 0x01 = OUT.
375 */
376 #define LINKTYPE_PPP_PPPD 166
377
378 /*
379 * Juniper-private data link type, as per request from
380 * Hannes Gredler <hannes@juniper.net>. The DLT_s are used
381 * for passing on chassis-internal metainformation such as
382 * QOS profiles, cookies, etc..
383 */
384 #define LINKTYPE_JUNIPER_PPPOE 167
385 #define LINKTYPE_JUNIPER_PPPOE_ATM 168
386
387 #define LINKTYPE_GPRS_LLC 169 /* GPRS LLC */
388 #define LINKTYPE_GPF_T 170 /* GPF-T (ITU-T G.7041/Y.1303) */
389 #define LINKTYPE_GPF_F 171 /* GPF-T (ITU-T G.7041/Y.1303) */
390
391 /*
392 * Requested by Oolan Zimmer <oz@gcom.com> for use in Gcom's T1/E1 line
393 * monitoring equipment.
394 */
395 #define LINKTYPE_GCOM_T1E1 172
396 #define LINKTYPE_GCOM_SERIAL 173
397
398 /*
399 * Juniper-private data link type, as per request from
400 * Hannes Gredler <hannes@juniper.net>. The DLT_ is used
401 * for internal communication to Physical Interface Cards (PIC)
402 */
403 #define LINKTYPE_JUNIPER_PIC_PEER 174
404
405 /*
406 * Link types requested by Gregor Maier <gregor@endace.com> of Endace
407 * Measurement Systems. They add an ERF header (see
408 * http://www.endace.com/support/EndaceRecordFormat.pdf) in front of
409 * the link-layer header.
410 */
411 #define LINKTYPE_ERF_ETH 175 /* Ethernet */
412 #define LINKTYPE_ERF_POS 176 /* Packet-over-SONET */
413
414 /*
415 * Requested by Daniele Orlandi <daniele@orlandi.com> for raw LAPD
416 * for vISDN (http://www.orlandi.com/visdn/). Its link-layer header
417 * includes additional information before the LAPD header, so it's
418 * not necessarily a generic LAPD header.
419 */
420 #define LINKTYPE_LINUX_LAPD 177
421
422 /*
423 * Juniper-private data link type, as per request from
424 * Hannes Gredler <hannes@juniper.net>.
425 * The Link Types are used for prepending meta-information
426 * like interface index, interface name
427 * before standard Ethernet, PPP, Frelay & C-HDLC Frames
428 */
429 #define LINKTYPE_JUNIPER_ETHER 178
430 #define LINKTYPE_JUNIPER_PPP 179
431 #define LINKTYPE_JUNIPER_FRELAY 180
432 #define LINKTYPE_JUNIPER_CHDLC 181
433
434 /*
435 * Multi Link Frame Relay (FRF.16)
436 */
437 #define LINKTYPE_MFR 182
438
439 /*
440 * Juniper-private data link type, as per request from
441 * Hannes Gredler <hannes@juniper.net>.
442 * The DLT_ is used for internal communication with a
443 * voice Adapter Card (PIC)
444 */
445 #define LINKTYPE_JUNIPER_VP 183
446
447 /*
448 * Arinc 429 frames.
449 * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
450 * Every frame contains a 32bit A429 label.
451 * More documentation on Arinc 429 can be found at
452 * http://www.condoreng.com/support/downloads/tutorials/ARINCTutorial.pdf
453 */
454 #define LINKTYPE_A429 184
455
456 /*
457 * Arinc 653 Interpartition Communication messages.
458 * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
459 * Please refer to the A653-1 standard for more information.
460 */
461 #define LINKTYPE_A653_ICM 185
462
463 /*
464 * USB packets, beginning with a USB setup header; requested by
465 * Paolo Abeni <paolo.abeni@email.it>.
466 */
467 #define LINKTYPE_USB 186
468
469 /*
470 * Bluetooth HCI UART transport layer (part H:4); requested by
471 * Paolo Abeni.
472 */
473 #define LINKTYPE_BLUETOOTH_HCI_H4 187
474
475 /*
476 * IEEE 802.16 MAC Common Part Sublayer; requested by Maria Cruz
477 * <cruz_petagay@bah.com>.
478 */
479 #define LINKTYPE_IEEE802_16_MAC_CPS 188
480
481 /*
482 * USB packets, beginning with a Linux USB header; requested by
483 * Paolo Abeni <paolo.abeni@email.it>.
484 */
485 #define LINKTYPE_USB_LINUX 189
486
487 /*
488 * Controller Area Network (CAN) v. 2.0B packets.
489 * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
490 * Used to dump CAN packets coming from a CAN Vector board.
491 * More documentation on the CAN v2.0B frames can be found at
492 * http://www.can-cia.org/downloads/?269
493 */
494 #define LINKTYPE_CAN20B 190
495
496 /*
497 * IEEE 802.15.4, with address fields padded, as is done by Linux
498 * drivers; requested by Juergen Schimmer.
499 */
500 #define LINKTYPE_IEEE802_15_4_LINUX 191
501
502 static struct linktype_map {
503 int dlt;
504 int linktype;
505 } map[] = {
506 /*
507 * These DLT_* codes have LINKTYPE_* codes with values identical
508 * to the values of the corresponding DLT_* code.
509 */
510 { DLT_NULL, LINKTYPE_NULL },
511 { DLT_EN10MB, LINKTYPE_ETHERNET },
512 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
513 { DLT_AX25, LINKTYPE_AX25 },
514 { DLT_PRONET, LINKTYPE_PRONET },
515 { DLT_CHAOS, LINKTYPE_CHAOS },
516 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
517 { DLT_ARCNET, LINKTYPE_ARCNET },
518 { DLT_SLIP, LINKTYPE_SLIP },
519 { DLT_PPP, LINKTYPE_PPP },
520 { DLT_FDDI, LINKTYPE_FDDI },
521
522 /*
523 * These DLT_* codes have different values on different
524 * platforms; we map them to LINKTYPE_* codes that
525 * have values that should never be equal to any DLT_*
526 * code.
527 */
528 #ifdef DLT_FR
529 /* BSD/OS Frame Relay */
530 { DLT_FR, LINKTYPE_FRELAY },
531 #endif
532
533 { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL },
534 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
535 { DLT_RAW, LINKTYPE_RAW },
536 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
537 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
538
539 /* BSD/OS Cisco HDLC */
540 { DLT_C_HDLC, LINKTYPE_C_HDLC },
541
542 /*
543 * These DLT_* codes are not on all platforms, but, so far,
544 * there don't appear to be any platforms that define
545 * other codes with those values; we map them to
546 * different LINKTYPE_* values anyway, just in case.
547 */
548
549 /* Linux ATM Classical IP */
550 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
551
552 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
553 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
554
555 /* NetBSD PPP over Ethernet */
556 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
557
558 /* IEEE 802.11 wireless */
559 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
560
561 /* Frame Relay */
562 { DLT_FRELAY, LINKTYPE_FRELAY },
563
564 /* OpenBSD loopback */
565 { DLT_LOOP, LINKTYPE_LOOP },
566
567 /* Linux cooked socket capture */
568 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
569
570 /* Apple LocalTalk hardware */
571 { DLT_LTALK, LINKTYPE_LTALK },
572
573 /* Acorn Econet */
574 { DLT_ECONET, LINKTYPE_ECONET },
575
576 /* OpenBSD DLT_PFLOG */
577 { DLT_PFLOG, LINKTYPE_PFLOG },
578
579 /* For Cisco-internal use */
580 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
581
582 /* Prism II monitor-mode header plus 802.11 header */
583 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
584
585 /* FreeBSD Aironet driver stuff */
586 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
587
588 /* Siemens HiPath HDLC */
589 { DLT_HHDLC, LINKTYPE_HHDLC },
590
591 /* RFC 2625 IP-over-Fibre Channel */
592 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
593
594 /* Solaris+SunATM */
595 { DLT_SUNATM, LINKTYPE_SUNATM },
596
597 /* RapidIO */
598 { DLT_RIO, LINKTYPE_RIO },
599
600 /* PCI Express */
601 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
602
603 /* Xilinx Aurora link layer */
604 { DLT_AURORA, LINKTYPE_AURORA },
605
606 /* 802.11 plus BSD radio header */
607 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
608
609 /* Tazmen Sniffer Protocol */
610 { DLT_TZSP, LINKTYPE_TZSP },
611
612 /* Arcnet with Linux-style link-layer headers */
613 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
614
615 /* Juniper-internal chassis encapsulation */
616 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
617 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
618 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
619 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
620 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
621 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
622 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
623 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 },
624
625 /* Apple IP-over-IEEE 1394 cooked header */
626 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 },
627
628 /* SS7 */
629 { DLT_MTP2_WITH_PHDR, LINKTYPE_MTP2_WITH_PHDR },
630 { DLT_MTP2, LINKTYPE_MTP2 },
631 { DLT_MTP3, LINKTYPE_MTP3 },
632 { DLT_SCCP, LINKTYPE_SCCP },
633
634 /* DOCSIS MAC frames */
635 { DLT_DOCSIS, LINKTYPE_DOCSIS },
636
637 /* IrDA IrLAP packets + Linux-cooked header */
638 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA },
639
640 /* IBM SP and Next Federation switches */
641 { DLT_IBM_SP, LINKTYPE_IBM_SP },
642 { DLT_IBM_SN, LINKTYPE_IBM_SN },
643
644 /* 802.11 plus AVS radio header */
645 { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS },
646
647 /*
648 * Any platform that defines additional DLT_* codes should:
649 *
650 * request a LINKTYPE_* code and value from tcpdump.org,
651 * as per the above;
652 *
653 * add, in their version of libpcap, an entry to map
654 * those DLT_* codes to the corresponding LINKTYPE_*
655 * code;
656 *
657 * redefine, in their "net/bpf.h", any DLT_* values
658 * that collide with the values used by their additional
659 * DLT_* codes, to remove those collisions (but without
660 * making them collide with any of the LINKTYPE_*
661 * values equal to 50 or above; they should also avoid
662 * defining DLT_* values that collide with those
663 * LINKTYPE_* values, either).
664 */
665
666 /* Juniper-internal chassis encapsulation */
667 { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR },
668
669 /* BACnet MS/TP */
670 { DLT_BACNET_MS_TP, LINKTYPE_BACNET_MS_TP },
671
672 /* PPP for pppd, with direction flag in the PPP header */
673 { DLT_PPP_PPPD, LINKTYPE_PPP_PPPD},
674
675 /* Juniper-internal chassis encapsulation */
676 { DLT_JUNIPER_PPPOE, LINKTYPE_JUNIPER_PPPOE },
677 { DLT_JUNIPER_PPPOE_ATM,LINKTYPE_JUNIPER_PPPOE_ATM },
678
679 /* GPRS LLC */
680 { DLT_GPRS_LLC, LINKTYPE_GPRS_LLC },
681
682 /* Transparent Generic Framing Procedure (ITU-T G.7041/Y.1303) */
683 { DLT_GPF_T, LINKTYPE_GPF_T },
684
685 /* Framed Generic Framing Procedure (ITU-T G.7041/Y.1303) */
686 { DLT_GPF_F, LINKTYPE_GPF_F },
687
688 { DLT_GCOM_T1E1, LINKTYPE_GCOM_T1E1 },
689 { DLT_GCOM_SERIAL, LINKTYPE_GCOM_SERIAL },
690
691 /* Juniper-internal chassis encapsulation */
692 { DLT_JUNIPER_PIC_PEER, LINKTYPE_JUNIPER_PIC_PEER },
693
694 /* Endace types */
695 { DLT_ERF_ETH, LINKTYPE_ERF_ETH },
696 { DLT_ERF_POS, LINKTYPE_ERF_POS },
697
698 /* viSDN LAPD */
699 { DLT_LINUX_LAPD, LINKTYPE_LINUX_LAPD },
700
701 /* Juniper meta-information before Ether, PPP, Frame Relay, C-HDLC Frames */
702 { DLT_JUNIPER_ETHER, LINKTYPE_JUNIPER_ETHER },
703 { DLT_JUNIPER_PPP, LINKTYPE_JUNIPER_PPP },
704 { DLT_JUNIPER_FRELAY, LINKTYPE_JUNIPER_FRELAY },
705 { DLT_JUNIPER_CHDLC, LINKTYPE_JUNIPER_CHDLC },
706
707 /* Multi Link Frame Relay (FRF.16) */
708 { DLT_MFR, LINKTYPE_MFR },
709
710 /* Juniper Voice PIC */
711 { DLT_JUNIPER_VP, LINKTYPE_JUNIPER_VP },
712
713 /* Controller Area Network (CAN) v2.0B */
714 { DLT_A429, LINKTYPE_A429 },
715
716 /* Arinc 653 Interpartition Communication messages */
717 { DLT_A653_ICM, LINKTYPE_A653_ICM },
718
719 /* USB */
720 { DLT_USB, LINKTYPE_USB },
721
722 /* Bluetooth HCI UART transport layer */
723 { DLT_BLUETOOTH_HCI_H4, LINKTYPE_BLUETOOTH_HCI_H4 },
724
725 /* IEEE 802.16 MAC Common Part Sublayer */
726 { DLT_IEEE802_16_MAC_CPS, LINKTYPE_IEEE802_16_MAC_CPS },
727
728 /* USB with Linux header */
729 { DLT_USB_LINUX, LINKTYPE_USB_LINUX },
730
731 /* Controller Area Network (CAN) v2.0B */
732 { DLT_CAN20B, LINKTYPE_CAN20B },
733
734 /* IEEE 802.15.4 with address fields padded */
735 { DLT_IEEE802_15_4_LINUX, LINKTYPE_IEEE802_15_4_LINUX },
736
737 { -1, -1 }
738 };
739
740 static int
741 dlt_to_linktype(int dlt)
742 {
743 int i;
744
745 for (i = 0; map[i].dlt != -1; i++) {
746 if (map[i].dlt == dlt)
747 return (map[i].linktype);
748 }
749
750 /*
751 * If we don't have a mapping for this DLT_ code, return an
752 * error; that means that the table above needs to have an
753 * entry added.
754 */
755 return (-1);
756 }
757
758 static int
759 linktype_to_dlt(int linktype)
760 {
761 int i;
762
763 for (i = 0; map[i].linktype != -1; i++) {
764 if (map[i].linktype == linktype)
765 return (map[i].dlt);
766 }
767
768 /*
769 * If we don't have an entry for this link type, return
770 * the link type value; it may be a DLT_ value from an
771 * older version of libpcap.
772 */
773 return linktype;
774 }
775
776 static int
777 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
778 {
779 struct pcap_file_header hdr;
780
781 hdr.magic = TCPDUMP_MAGIC;
782 hdr.version_major = PCAP_VERSION_MAJOR;
783 hdr.version_minor = PCAP_VERSION_MINOR;
784
785 hdr.thiszone = thiszone;
786 hdr.snaplen = snaplen;
787 hdr.sigfigs = 0;
788 hdr.linktype = linktype;
789
790 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
791 return (-1);
792
793 return (0);
794 }
795
796 static void
797 swap_hdr(struct pcap_file_header *hp)
798 {
799 hp->version_major = SWAPSHORT(hp->version_major);
800 hp->version_minor = SWAPSHORT(hp->version_minor);
801 hp->thiszone = SWAPLONG(hp->thiszone);
802 hp->sigfigs = SWAPLONG(hp->sigfigs);
803 hp->snaplen = SWAPLONG(hp->snaplen);
804 hp->linktype = SWAPLONG(hp->linktype);
805 }
806
807 static int
808 sf_getnonblock(pcap_t *p, char *errbuf)
809 {
810 /*
811 * This is a savefile, not a live capture file, so never say
812 * it's in non-blocking mode.
813 */
814 return (0);
815 }
816
817 static int
818 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
819 {
820 /*
821 * This is a savefile, not a live capture file, so ignore
822 * requests to put it in non-blocking mode.
823 */
824 return (0);
825 }
826
827 static int
828 sf_stats(pcap_t *p, struct pcap_stat *ps)
829 {
830 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
831 "Statistics aren't available from savefiles");
832 return (-1);
833 }
834
835 static int
836 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
837 {
838 strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
839 PCAP_ERRBUF_SIZE);
840 return (-1);
841 }
842
843 /*
844 * Set direction flag: Which packets do we accept on a forwarding
845 * single device? IN, OUT or both?
846 */
847 static int
848 sf_setdirection(pcap_t *p, pcap_direction_t d)
849 {
850 snprintf(p->errbuf, sizeof(p->errbuf),
851 "Setting direction is not supported on savefiles");
852 return (-1);
853 }
854
855 static void
856 sf_close(pcap_t *p)
857 {
858 if (p->sf.rfile != stdin)
859 (void)fclose(p->sf.rfile);
860 if (p->sf.base != NULL)
861 free(p->sf.base);
862 }
863
864 pcap_t *
865 pcap_open_offline(const char *fname, char *errbuf)
866 {
867 FILE *fp;
868 pcap_t *p;
869
870 if (fname[0] == '-' && fname[1] == '\0')
871 {
872 fp = stdin;
873 #if defined(WIN32) || defined(MSDOS)
874 /*
875 * We're reading from the standard input, so put it in binary
876 * mode, as savefiles are binary files.
877 */
878 SET_BINMODE(fp);
879 #endif
880 }
881 else {
882 #if !defined(WIN32) && !defined(MSDOS)
883 fp = fopen(fname, "r");
884 #else
885 fp = fopen(fname, "rb");
886 #endif
887 if (fp == NULL) {
888 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
889 pcap_strerror(errno));
890 return (NULL);
891 }
892 }
893 p = pcap_fopen_offline(fp, errbuf);
894 if (p == NULL) {
895 if (fp != stdin)
896 fclose(fp);
897 }
898 return (p);
899 }
900
901 pcap_t *
902 pcap_fopen_offline(FILE *fp, char *errbuf)
903 {
904 register pcap_t *p;
905 struct pcap_file_header hdr;
906 size_t amt_read;
907 bpf_u_int32 magic;
908 int linklen;
909
910 p = (pcap_t *)malloc(sizeof(*p));
911 if (p == NULL) {
912 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
913 return (NULL);
914 }
915
916 memset((char *)p, 0, sizeof(*p));
917
918 amt_read = fread((char *)&hdr, 1, sizeof(hdr), fp);
919 if (amt_read != sizeof(hdr)) {
920 if (ferror(fp)) {
921 snprintf(errbuf, PCAP_ERRBUF_SIZE,
922 "error reading dump file: %s",
923 pcap_strerror(errno));
924 } else {
925 snprintf(errbuf, PCAP_ERRBUF_SIZE,
926 "truncated dump file; tried to read %lu file header bytes, only got %lu",
927 (unsigned long)sizeof(hdr),
928 (unsigned long)amt_read);
929 }
930 goto bad;
931 }
932 magic = hdr.magic;
933 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
934 magic = SWAPLONG(magic);
935 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
936 snprintf(errbuf, PCAP_ERRBUF_SIZE,
937 "bad dump file format");
938 goto bad;
939 }
940 p->sf.swapped = 1;
941 swap_hdr(&hdr);
942 }
943 if (magic == KUZNETZOV_TCPDUMP_MAGIC) {
944 /*
945 * XXX - the patch that's in some versions of libpcap
946 * changes the packet header but not the magic number,
947 * and some other versions with this magic number have
948 * some extra debugging information in the packet header;
949 * we'd have to use some hacks^H^H^H^H^Hheuristics to
950 * detect those variants.
951 *
952 * Ethereal does that, but it does so by trying to read
953 * the first two packets of the file with each of the
954 * record header formats. That currently means it seeks
955 * backwards and retries the reads, which doesn't work
956 * on pipes. We want to be able to read from a pipe, so
957 * that strategy won't work; we'd have to buffer some
958 * data ourselves and read from that buffer in order to
959 * make that work.
960 */
961 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
962 } else
963 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
964 if (hdr.version_major < PCAP_VERSION_MAJOR) {
965 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
966 goto bad;
967 }
968 p->tzoff = hdr.thiszone;
969 p->snapshot = hdr.snaplen;
970 p->linktype = linktype_to_dlt(hdr.linktype);
971 if (magic == KUZNETZOV_TCPDUMP_MAGIC && p->linktype == DLT_EN10MB) {
972 /*
973 * This capture might have been done in raw mode or cooked
974 * mode.
975 *
976 * If it was done in cooked mode, p->snapshot was passed
977 * to recvfrom() as the buffer size, meaning that the
978 * most packet data that would be copied would be
979 * p->snapshot. However, a faked Ethernet header would
980 * then have been added to it, so the most data that would
981 * be in a packet in the file would be p->snapshot + 14.
982 *
983 * We can't easily tell whether the capture was done in
984 * raw mode or cooked mode, so we'll assume it was
985 * cooked mode, and add 14 to the snapshot length. That
986 * means that, for a raw capture, the snapshot length will
987 * be misleading if you use it to figure out why a capture
988 * doesn't have all the packet data, but there's not much
989 * we can do to avoid that.
990 */
991 p->snapshot += 14;
992 }
993 p->sf.rfile = fp;
994 #ifndef WIN32
995 p->bufsize = hdr.snaplen;
996 #else
997 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
998 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
999 #endif
1000
1001 /* Align link header as required for proper data alignment */
1002 /* XXX should handle all types */
1003 switch (p->linktype) {
1004
1005 case DLT_EN10MB:
1006 linklen = 14;
1007 break;
1008
1009 case DLT_FDDI:
1010 linklen = 13 + 8; /* fddi_header + llc */
1011 break;
1012
1013 case DLT_NULL:
1014 default:
1015 linklen = 0;
1016 break;
1017 }
1018
1019 if (p->bufsize < 0)
1020 p->bufsize = BPF_MAXBUFSIZE;
1021 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
1022 if (p->sf.base == NULL) {
1023 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
1024 goto bad;
1025 }
1026 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
1027 p->sf.version_major = hdr.version_major;
1028 p->sf.version_minor = hdr.version_minor;
1029 #ifdef PCAP_FDDIPAD
1030 /* Padding only needed for live capture fcode */
1031 p->fddipad = 0;
1032 #endif
1033
1034 /*
1035 * We interchanged the caplen and len fields at version 2.3,
1036 * in order to match the bpf header layout. But unfortunately
1037 * some files were written with version 2.3 in their headers
1038 * but without the interchanged fields.
1039 *
1040 * In addition, DG/UX tcpdump writes out files with a version
1041 * number of 543.0, and with the caplen and len fields in the
1042 * pre-2.3 order.
1043 */
1044 switch (hdr.version_major) {
1045
1046 case 2:
1047 if (hdr.version_minor < 3)
1048 p->sf.lengths_swapped = SWAPPED;
1049 else if (hdr.version_minor == 3)
1050 p->sf.lengths_swapped = MAYBE_SWAPPED;
1051 else
1052 p->sf.lengths_swapped = NOT_SWAPPED;
1053 break;
1054
1055 case 543:
1056 p->sf.lengths_swapped = SWAPPED;
1057 break;
1058
1059 default:
1060 p->sf.lengths_swapped = NOT_SWAPPED;
1061 break;
1062 }
1063
1064 #if !defined(WIN32) && !defined(MSDOS)
1065 /*
1066 * You can do "select()" and "poll()" on plain files on most
1067 * platforms, and should be able to do so on pipes.
1068 *
1069 * You can't do "select()" on anything other than sockets in
1070 * Windows, so, on Win32 systems, we don't have "selectable_fd".
1071 */
1072 p->selectable_fd = fileno(fp);
1073 #endif
1074
1075 p->read_op = pcap_offline_read;
1076 p->inject_op = sf_inject;
1077 p->setfilter_op = install_bpf_program;
1078 p->setdirection_op = sf_setdirection;
1079 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
1080 p->getnonblock_op = sf_getnonblock;
1081 p->setnonblock_op = sf_setnonblock;
1082 p->stats_op = sf_stats;
1083 p->close_op = sf_close;
1084
1085 return (p);
1086 bad:
1087 free(p);
1088 return (NULL);
1089 }
1090
1091 /*
1092 * Read sf_readfile and return the next packet. Return the header in hdr
1093 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
1094 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
1095 */
1096 static int
1097 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen)
1098 {
1099 struct pcap_sf_patched_pkthdr sf_hdr;
1100 FILE *fp = p->sf.rfile;
1101 size_t amt_read;
1102 bpf_u_int32 t;
1103
1104 /*
1105 * Read the packet header; the structure we use as a buffer
1106 * is the longer structure for files generated by the patched
1107 * libpcap, but if the file has the magic number for an
1108 * unpatched libpcap we only read as many bytes as the regular
1109 * header has.
1110 */
1111 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp);
1112 if (amt_read != p->sf.hdrsize) {
1113 if (ferror(fp)) {
1114 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1115 "error reading dump file: %s",
1116 pcap_strerror(errno));
1117 return (-1);
1118 } else {
1119 if (amt_read != 0) {
1120 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1121 "truncated dump file; tried to read %d header bytes, only got %lu",
1122 p->sf.hdrsize, (unsigned long)amt_read);
1123 return (-1);
1124 }
1125 /* EOF */
1126 return (1);
1127 }
1128 }
1129
1130 if (p->sf.swapped) {
1131 /* these were written in opposite byte order */
1132 hdr->caplen = SWAPLONG(sf_hdr.caplen);
1133 hdr->len = SWAPLONG(sf_hdr.len);
1134 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
1135 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
1136 } else {
1137 hdr->caplen = sf_hdr.caplen;
1138 hdr->len = sf_hdr.len;
1139 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
1140 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
1141 }
1142 /* Swap the caplen and len fields, if necessary. */
1143 switch (p->sf.lengths_swapped) {
1144
1145 case NOT_SWAPPED:
1146 break;
1147
1148 case MAYBE_SWAPPED:
1149 if (hdr->caplen <= hdr->len) {
1150 /*
1151 * The captured length is <= the actual length,
1152 * so presumably they weren't swapped.
1153 */
1154 break;
1155 }
1156 /* FALLTHROUGH */
1157
1158 case SWAPPED:
1159 t = hdr->caplen;
1160 hdr->caplen = hdr->len;
1161 hdr->len = t;
1162 break;
1163 }
1164
1165 if (hdr->caplen > buflen) {
1166 /*
1167 * This can happen due to Solaris 2.3 systems tripping
1168 * over the BUFMOD problem and not setting the snapshot
1169 * correctly in the savefile header. If the caplen isn't
1170 * grossly wrong, try to salvage.
1171 */
1172 static u_char *tp = NULL;
1173 static size_t tsize = 0;
1174
1175 if (hdr->caplen > 65535) {
1176 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1177 "bogus savefile header");
1178 return (-1);
1179 }
1180
1181 if (tsize < hdr->caplen) {
1182 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
1183 if (tp != NULL)
1184 free((u_char *)tp);
1185 tp = (u_char *)malloc(tsize);
1186 if (tp == NULL) {
1187 tsize = 0;
1188 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1189 "BUFMOD hack malloc");
1190 return (-1);
1191 }
1192 }
1193 amt_read = fread((char *)tp, 1, hdr->caplen, fp);
1194 if (amt_read != hdr->caplen) {
1195 if (ferror(fp)) {
1196 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1197 "error reading dump file: %s",
1198 pcap_strerror(errno));
1199 } else {
1200 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1201 "truncated dump file; tried to read %u captured bytes, only got %lu",
1202 hdr->caplen, (unsigned long)amt_read);
1203 }
1204 return (-1);
1205 }
1206 /*
1207 * We can only keep up to buflen bytes. Since caplen > buflen
1208 * is exactly how we got here, we know we can only keep the
1209 * first buflen bytes and must drop the remainder. Adjust
1210 * caplen accordingly, so we don't get confused later as
1211 * to how many bytes we have to play with.
1212 */
1213 hdr->caplen = buflen;
1214 memcpy((char *)buf, (char *)tp, buflen);
1215
1216 } else {
1217 /* read the packet itself */
1218 amt_read = fread((char *)buf, 1, hdr->caplen, fp);
1219 if (amt_read != hdr->caplen) {
1220 if (ferror(fp)) {
1221 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1222 "error reading dump file: %s",
1223 pcap_strerror(errno));
1224 } else {
1225 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1226 "truncated dump file; tried to read %u captured bytes, only got %lu",
1227 hdr->caplen, (unsigned long)amt_read);
1228 }
1229 return (-1);
1230 }
1231 }
1232
1233 /*
1234 * The DLT_USB_LINUX header is in host byte order when capturing
1235 * (it's supplied directly from a memory-mapped buffer shared
1236 * by the kernel).
1237 *
1238 * When reading a DLT_USB_LINUX capture file, we need to convert
1239 * it from the capturing host's byte order to the reading host's
1240 * byte order.
1241 */
1242 if (p->sf.swapped && p->linktype == DLT_USB_LINUX) {
1243 pcap_usb_header* uhdr = (pcap_usb_header*) buf;
1244 /*
1245 * The URB id is a totally opaque value; do we really need to
1246 * converte it to the reading host's byte order???
1247 */
1248 if (hdr->caplen < 8)
1249 return 0;
1250 uhdr->id = SWAPLL(uhdr->id);
1251 if (hdr->caplen < 14)
1252 return 0;
1253 uhdr->bus_id = SWAPSHORT(uhdr->bus_id);
1254 if (hdr->caplen < 24)
1255 return 0;
1256 uhdr->ts_sec = SWAPLL(uhdr->ts_sec);
1257 if (hdr->caplen < 28)
1258 return 0;
1259 uhdr->ts_usec = SWAPLONG(uhdr->ts_usec);
1260 if (hdr->caplen < 32)
1261 return 0;
1262 uhdr->status = SWAPLONG(uhdr->status);
1263 if (hdr->caplen < 36)
1264 return 0;
1265 uhdr->urb_len = SWAPLONG(uhdr->urb_len);
1266 if (hdr->caplen < 40)
1267 return 0;
1268 uhdr->data_len = SWAPLONG(uhdr->data_len);
1269 }
1270 return (0);
1271 }
1272
1273 /*
1274 * Print out packets stored in the file initialized by sf_read_init().
1275 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
1276 */
1277 int
1278 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
1279 {
1280 struct bpf_insn *fcode;
1281 int status = 0;
1282 int n = 0;
1283
1284 while (status == 0) {
1285 struct pcap_pkthdr h;
1286
1287 /*
1288 * Has "pcap_breakloop()" been called?
1289 * If so, return immediately - if we haven't read any
1290 * packets, clear the flag and return -2 to indicate
1291 * that we were told to break out of the loop, otherwise
1292 * leave the flag set, so that the *next* call will break
1293 * out of the loop without having read any packets, and
1294 * return the number of packets we've processed so far.
1295 */
1296 if (p->break_loop) {
1297 if (n == 0) {
1298 p->break_loop = 0;
1299 return (-2);
1300 } else
1301 return (n);
1302 }
1303
1304 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
1305 if (status) {
1306 if (status == 1)
1307 return (0);
1308 return (status);
1309 }
1310
1311 if ((fcode = p->fcode.bf_insns) == NULL ||
1312 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
1313 (*callback)(user, &h, p->buffer);
1314 if (++n >= cnt && cnt > 0)
1315 break;
1316 }
1317 }
1318 /*XXX this breaks semantics tcpslice expects */
1319 return (n);
1320 }
1321
1322 /*
1323 * Output a packet to the initialized dump file.
1324 */
1325 void
1326 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1327 {
1328 register FILE *f;
1329 struct pcap_sf_pkthdr sf_hdr;
1330
1331 f = (FILE *)user;
1332 sf_hdr.ts.tv_sec = h->ts.tv_sec;
1333 sf_hdr.ts.tv_usec = h->ts.tv_usec;
1334 sf_hdr.caplen = h->caplen;
1335 sf_hdr.len = h->len;
1336 /* XXX we should check the return status */
1337 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
1338 (void)fwrite((char *)sp, h->caplen, 1, f);
1339 }
1340
1341 static pcap_dumper_t *
1342 pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
1343 {
1344
1345 #if defined(WIN32) || defined(MSDOS)
1346 /*
1347 * If we're writing to the standard output, put it in binary
1348 * mode, as savefiles are binary files.
1349 *
1350 * Otherwise, we turn off buffering.
1351 * XXX - why? And why not on the standard output?
1352 */
1353 if (f == stdout)
1354 SET_BINMODE(f);
1355 else
1356 setbuf(f, NULL);
1357 #endif
1358 if (sf_write_header(f, linktype, p->tzoff, p->snapshot) == -1) {
1359 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s",
1360 fname, pcap_strerror(errno));
1361 if (f != stdout)
1362 (void)fclose(f);
1363 return (NULL);
1364 }
1365 return ((pcap_dumper_t *)f);
1366 }
1367
1368 /*
1369 * Initialize so that sf_write() will output to the file named 'fname'.
1370 */
1371 pcap_dumper_t *
1372 pcap_dump_open(pcap_t *p, const char *fname)
1373 {
1374 FILE *f;
1375 int linktype;
1376
1377 linktype = dlt_to_linktype(p->linktype);
1378 if (linktype == -1) {
1379 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1380 "%s: link-layer type %d isn't supported in savefiles",
1381 fname, linktype);
1382 return (NULL);
1383 }
1384
1385 if (fname[0] == '-' && fname[1] == '\0') {
1386 f = stdout;
1387 fname = "standard output";
1388 } else {
1389 #if !defined(WIN32) && !defined(MSDOS)
1390 f = fopen(fname, "w");
1391 #else
1392 f = fopen(fname, "wb");
1393 #endif
1394 if (f == NULL) {
1395 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
1396 fname, pcap_strerror(errno));
1397 return (NULL);
1398 }
1399 }
1400 return (pcap_setup_dump(p, linktype, f, fname));
1401 }
1402
1403 /*
1404 * Initialize so that sf_write() will output to the given stream.
1405 */
1406 pcap_dumper_t *
1407 pcap_dump_fopen(pcap_t *p, FILE *f)
1408 {
1409 int linktype;
1410
1411 linktype = dlt_to_linktype(p->linktype);
1412 if (linktype == -1) {
1413 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1414 "stream: link-layer type %d isn't supported in savefiles",
1415 linktype);
1416 return (NULL);
1417 }
1418
1419 return (pcap_setup_dump(p, linktype, f, "stream"));
1420 }
1421
1422 FILE *
1423 pcap_dump_file(pcap_dumper_t *p)
1424 {
1425 return ((FILE *)p);
1426 }
1427
1428 long
1429 pcap_dump_ftell(pcap_dumper_t *p)
1430 {
1431 return (ftell((FILE *)p));
1432 }
1433
1434 int
1435 pcap_dump_flush(pcap_dumper_t *p)
1436 {
1437
1438 if (fflush((FILE *)p) == EOF)
1439 return (-1);
1440 else
1441 return (0);
1442 }
1443
1444 void
1445 pcap_dump_close(pcap_dumper_t *p)
1446 {
1447
1448 #ifdef notyet
1449 if (ferror((FILE *)p))
1450 return-an-error;
1451 /* XXX should check return from fclose() too */
1452 #endif
1453 (void)fclose((FILE *)p);
1454 }