Rename WTAP_ENCAP_ATM_SNIFFER to WTAP_ENCAP_ATM_PDUS, as it's not just
[obnox/wireshark/wip.git] / wiretap / libpcap.c
1 /* libpcap.c
2  *
3  * $Id: libpcap.c,v 1.87 2003/01/03 06:45:45 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdlib.h>
28 #include <errno.h>
29 #include "wtap-int.h"
30 #include "file_wrappers.h"
31 #include "buffer.h"
32 #include "atm.h"
33 #include "libpcap.h"
34
35 #ifdef HAVE_PCAP_H
36 # ifdef HAVE_SYS_TYPES_H
37 #   include <sys/types.h>
38 # endif
39 #include <pcap.h>
40 #include "wtap-capture.h"
41 #endif
42
43 /*
44  * The link-layer header on ATM packets.
45  */
46 struct sunatm_hdr {
47         guint8  flags;          /* destination and traffic type */
48         guint8  vpi;            /* VPI */
49         guint16 vci;            /* VCI */
50 };
51
52 /* See source to the "libpcap" library for information on the "libpcap"
53    file format. */
54
55 /* On some systems, the FDDI MAC addresses are bit-swapped. */
56 #if !defined(ultrix) && !defined(__alpha) && !defined(__bsdi__)
57 #define BIT_SWAPPED_MAC_ADDRS
58 #endif
59
60 /* Try to read the first two records of the capture file. */
61 typedef enum {
62         THIS_FORMAT,            /* the reads succeeded, assume it's this format */
63         BAD_READ,               /* the file is probably not valid */
64         OTHER_FORMAT            /* the file may be valid, but not in this format */
65 } libpcap_try_t;
66 static libpcap_try_t libpcap_try(wtap *wth, int *err);
67
68 static gboolean libpcap_read(wtap *wth, int *err, long *data_offset);
69 static gboolean libpcap_seek_read(wtap *wth, long seek_off,
70     union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err);
71 static int libpcap_read_header(wtap *wth, int *err,
72     struct pcaprec_ss990915_hdr *hdr, gboolean silent);
73 static void adjust_header(wtap *wth, struct pcaprec_hdr *hdr);
74 static void libpcap_get_atm_pseudoheader(const struct sunatm_hdr *atm_phdr,
75     union wtap_pseudo_header *pseudo_header);
76 static gboolean libpcap_read_atm_pseudoheader(FILE_T fh,
77     union wtap_pseudo_header *pseudo_header, int *err);
78 static gboolean libpcap_read_rec_data(FILE_T fh, guchar *pd, int length,
79     int *err);
80 static void libpcap_close(wtap *wth);
81 static gboolean libpcap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
82     const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
83
84 /*
85  * Either LBL NRG wasn't an adequate central registry (e.g., because of
86  * the slow rate of releases from them), or nobody bothered using them
87  * as a central registry, as many different groups have patched libpcap
88  * (and BPF, on the BSDs) to add new encapsulation types, and have ended
89  * up using the same DLT_ values for different encapsulation types.
90  *
91  * For those numerical encapsulation type values that everybody uses for
92  * the same encapsulation type (which inclues those that some platforms
93  * specify different DLT_ names for but don't appear to use), we map
94  * those values to the appropriate Wiretap values.
95  *
96  * For those numerical encapsulation type values that different libpcap
97  * variants use for different encapsulation types, we check what
98  * <pcap.h> defined to determine how to interpret them, so that we
99  * interpret them the way the libpcap with which we're building
100  * Ethereal/Wiretap interprets them (which, if it doesn't support
101  * them at all, means we don't support them either - any capture files
102  * using them are foreign, and we don't hazard a guess as to which
103  * platform they came from; we could, I guess, choose the most likely
104  * platform).
105  */
106
107 static const struct {
108         int     dlt_value;
109         int     wtap_encap_value;
110 } pcap_to_wtap_map[] = {
111         /*
112          * These are the values that are almost certainly the same
113          * in all libpcaps (I've yet to find one where the values
114          * in question are used for some purpose other than the
115          * one below, but...), and that Wiretap and Ethereal
116          * currently support.
117          */
118         { 0,            WTAP_ENCAP_NULL },      /* null encapsulation */
119         { 1,            WTAP_ENCAP_ETHERNET },
120         { 6,            WTAP_ENCAP_TOKEN_RING },        /* IEEE 802 Networks - assume token ring */
121         { 7,            WTAP_ENCAP_ARCNET },
122         { 8,            WTAP_ENCAP_SLIP },
123         { 9,            WTAP_ENCAP_PPP },
124 #ifdef BIT_SWAPPED_MAC_ADDRS
125         { 10,           WTAP_ENCAP_FDDI_BITSWAPPED },
126 #else
127         { 10,           WTAP_ENCAP_FDDI },
128 #endif
129
130         /*
131          * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
132          * on BSD (at least according to standard tcpdump) has, as
133          * the first octet, an indication of whether the packet was
134          * transmitted or received (rather than having the standard
135          * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
136          * a real live PPP header there, or perhaps a Cisco PPP header
137          * as per section 4.3.1 of RFC 1547 (implementations of this
138          * exist in various BSDs in "sys/net/if_spppsubr.c", and
139          * I think also exist either in standard Linux or in
140          * various Linux patches; the implementations show how to handle
141          * Cisco keepalive packets).
142          *
143          * However, I don't see any obvious place in FreeBSD "if_ppp.c"
144          * where anything other than the standard PPP header would be
145          * passed up.  I see some stuff that sets the first octet
146          * to 0 for incoming and 1 for outgoing packets before applying
147          * a BPF filter to see whether to drop packets whose protocol
148          * field has the 0x8000 bit set, i.e. network control protocols -
149          * those are handed up to userland - but that code puts the
150          * address field back before passing the packet up.
151          *
152          * I also don't see anything immediately obvious that munges
153          * the address field for sync PPP, either.
154          *
155          * Ethereal currently assumes that if the first octet of a
156          * PPP frame is 0xFF, it's the address field and is followed
157          * by a control field and a 2-byte protocol, otherwise the
158          * address and control fields are absent and the frame begins
159          * with a protocol field.  If we ever see a BSD/OS PPP
160          * capture, we'll have to handle it differently, and we may
161          * have to handle standard BSD captures differently if, in fact,
162          * they don't have 0xff 0x03 as the first two bytes - but, as per
163          * the two paragraphs preceding this, it's not clear that
164          * the address field *is* munged into an incoming/outgoing
165          * field when the packet is handed to the BPF device.
166          *
167          * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
168          * we treat WTAP_ENCAP_PPP packets as if those beginning with
169          * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
170          * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
171          * appear to contain that unless they're Cisco frames (if we
172          * ever see a capture with them, we'd need to implement the
173          * RFC 1547 stuff, and the keepalive protocol stuff).
174          *
175          * We may have to distinguish between "PPP where if it doesn't
176          * begin with 0xff there's no HDLC encapsulation and the frame
177          * begins with the protocol field" (which is how we handle
178          * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
179          * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
180          * is) at some point.
181          *
182          * XXX - NetBSD has DLT_HDLC, which appears to be used for
183          * Cisco HDLC.  Ideally, they should use DLT_PPP_SERIAL
184          * only for real live HDLC-encapsulated PPP, not for Cisco
185          * HDLC.
186          */
187         { 50,           WTAP_ENCAP_PPP },
188
189         /*
190          * These are the values that libpcap 0.5 and later use in
191          * capture file headers, in an attempt to work around the
192          * confusion decried above, and that Wiretap and Ethereal
193          * currently support.
194          */
195         { 100,          WTAP_ENCAP_ATM_RFC1483 },
196         { 101,          WTAP_ENCAP_RAW_IP },
197 #if 0
198         /*
199          * More values used by libpcap 0.5 as DLT_ values and used by the
200          * current CVS version of libpcap in capture file headers.
201          * They are not yet handled in Ethereal.
202          * If we get a capture that contains them, we'll implement them.
203          */
204         { 102,          WTAP_ENCAP_SLIP_BSDOS },
205         { 103,          WTAP_ENCAP_PPP_BSDOS },
206 #endif
207
208         /*
209          * These ones are handled in Ethereal, though.
210          */
211         { 104,          WTAP_ENCAP_CHDLC },     /* Cisco HDLC */
212         { 105,          WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
213         { 106,          WTAP_ENCAP_LINUX_ATM_CLIP },
214         { 107,          WTAP_ENCAP_FRELAY },    /* Frame Relay */
215         { 108,          WTAP_ENCAP_NULL },      /* OpenBSD loopback */
216 #if 0
217         { 109,          WTAP_ENCAP_ENC },       /* OpenBSD IPSEC enc */
218         { 110,          WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
219         { 111,          WTAP_ENCAP_HIPPI },     /* NetBSD HIPPI */
220 #endif
221         { 112,          WTAP_ENCAP_CHDLC },     /* NetBSD HDLC framing */
222
223         /*
224          * Linux "cooked mode" captures, used by the current CVS version
225          * of libpcap.
226          */
227         { 113,          WTAP_ENCAP_SLL },       /* Linux cooked capture */
228
229         { 114,          WTAP_ENCAP_LOCALTALK }, /* Localtalk */
230
231         /*
232          * The tcpdump.org version of libpcap uses 117, rather than 17,
233          * for OpenBSD packet filter logging, so as to avoid conflicting
234          * with DLT_LANE8023 in SuSE 6.3 libpcap.
235          */
236         { 117,          WTAP_ENCAP_PFLOG },
237
238         { 118,          WTAP_ENCAP_CISCO_IOS },
239         { 119,          WTAP_ENCAP_PRISM_HEADER }, /* Prism monitor mode hdr */
240         { 121,          WTAP_ENCAP_HHDLC },     /* HiPath HDLC */
241         { 122,          WTAP_ENCAP_IP_OVER_FC },   /* RFC 2625 IP-over-FC */
242         { 123,          WTAP_ENCAP_ATM_PDUS },  /* SunATM */
243         { 127,          WTAP_ENCAP_WLAN_HEADER },  /* 802.11 plus WLAN header */
244
245         /*
246          * The following are entries for libpcap type values that have
247          * different meanings on different OSes.
248          *
249          * We put these *after* the entries for the platform-independent
250          * libpcap type values for those Wiretap encapsulation types, so
251          * that Ethereal chooses the platform-independent libpcap type
252          * value for those encapsulatioin types, not the platform-dependent
253          * one.
254          */
255
256         /*
257          * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
258          * seen that define anything other than DLT_ATM_RFC1483 as 11 are
259          * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
260          * which define it as 100, mapping the kernel's value to 100, in
261          * an attempt to hide the different values used on different
262          * platforms.
263          *
264          * If this is a platform where DLT_FR is defined as 11, we
265          * don't handle 11 at all; otherwise, we handle it as
266          * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
267          * captures from BSD/OS if running on platforms other than BSD/OS,
268          * but
269          *
270          *      1) we don't yet support DLT_FR
271          *
272          * and
273          *
274          *      2) nothing short of a heuristic would let us interpret
275          *         them correctly).
276          */
277 #if defined(DLT_FR) && (DLT_FR == 11)
278         { 11,           WTAP_ENCAP_FRELAY },
279 #else
280         { 11,           WTAP_ENCAP_ATM_RFC1483 },
281 #endif
282
283         /*
284          * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
285          * BSD/OS, and DLT_LOOP on OpenBSD.
286          *
287          * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
288          * (it's just like DLT_NULL, only with the AF_ value in network
289          * rather than host byte order - Ethereal figures out the
290          * byte order from the data, so we don't care what byte order
291          * it's in), so if DLT_LOOP is defined as 12, interpret 12
292          * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
293          * as 12, interpret it as WTAP_ENCAP_RAW_IP.
294          */
295 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
296         { 12,           WTAP_ENCAP_NULL },
297 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
298         /*
299          * Put entry for Cisco HDLC here.
300          * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
301          * start with a 4-byte Cisco HDLC header?
302          */
303 #else
304         { 12,           WTAP_ENCAP_RAW_IP },
305 #endif
306
307         /*
308          * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
309          * don't actually generate it.  I infer that BSD/OS translates
310          * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
311          * libpcap, as the BSD/OS link-layer header is different;
312          * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
313          *
314          * From this, I infer that there's no point in handling 13
315          * as DLT_SLIP_BSDOS.
316          *
317          * 13 is DLT_ATM_RFC1483 on BSD/OS.
318          *
319          * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
320          * of decrypted IPSEC traffic.
321          */
322 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
323         { 13,           WTAP_ENCAP_ATM_RFC1483 },
324 #elif defined(DLT_ENC) && (DLT_ENC == 13)
325         /* Put entry for DLT_ENC here */
326 #endif
327
328         /*
329          * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
330          * don't actually generate it.  I infer that BSD/OS translates
331          * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
332          * libpcap, as the BSD/OS link-layer header is different;
333          * however, in BSD/OS, DLT_PPP_BSDOS is 16.
334          *
335          * From this, I infer that there's no point in handling 14
336          * as DLT_PPP_BSDOS.
337          *
338          * 14 is DLT_RAW on BSD/OS and OpenBSD.
339          */
340         { 14,           WTAP_ENCAP_RAW_IP },
341
342         /*
343          * 15 is:
344          *
345          *      DLT_SLIP_BSDOS on BSD/OS;
346          *
347          *      DLT_HIPPI on NetBSD;
348          *
349          *      DLT_LANE8023 with Alexey Kuznetzov's patches for
350          *      Linux libpcap;
351          *
352          *      DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
353          *      (and on SuSE 6.3);
354          *
355          * but we don't currently handle any of those.
356          */
357
358         /*
359          * 16 is:
360          *
361          *      DLT_PPP_BSDOS on BSD/OS;
362          *
363          *      DLT_HDLC on NetBSD (Cisco HDLC);
364          *
365          *      DLT_CIP with Alexey Kuznetzov's patches for
366          *      Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
367          *
368          *      DLT_I4L_IP with the ISDN4Linux patches for libpcap
369          *      (and on SuSE 6.3).
370          */
371 #if defined(DLT_CIP) && (DLT_CIP == 16)
372         { 16,           WTAP_ENCAP_LINUX_ATM_CLIP },
373 #endif
374 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
375         { 16,           WTAP_ENCAP_CHDLC },
376 #endif
377
378         /*
379          * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
380          * handle it.
381          * It is also used as the PF (Packet Filter) logging format beginning
382          * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
383          * defined with the value 17.
384          */
385 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
386         { 17,           WTAP_ENCAP_PFLOG },
387 #endif
388
389         /*
390          * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
391          * DLT_CIP of 16 that the Alexey Kuznetzov patches for
392          * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
393          * I've not found any libpcap that uses it for any other purpose -
394          * hopefully nobody will do so in the future.
395          */
396         { 18,           WTAP_ENCAP_LINUX_ATM_CLIP },
397
398         /*
399          * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
400          * recent versions I've seen of the Linux ATM distribution;
401          * I've not yet found any libpcap that uses it for any other
402          * purpose - hopefully nobody will do so in the future.
403          */
404         { 19,           WTAP_ENCAP_LINUX_ATM_CLIP },
405 };
406 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
407
408 int wtap_pcap_encap_to_wtap_encap(int encap)
409 {
410         unsigned int i;
411
412         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
413                 if (pcap_to_wtap_map[i].dlt_value == encap)
414                         return pcap_to_wtap_map[i].wtap_encap_value;
415         }
416         return WTAP_ENCAP_UNKNOWN;
417 }
418
419
420 int libpcap_open(wtap *wth, int *err)
421 {
422         int bytes_read;
423         guint32 magic;
424         struct pcap_hdr hdr;
425         gboolean byte_swapped;
426         gboolean modified;
427         gboolean aix;
428         int file_encap;
429
430         /* Read in the number that should be at the start of a "libpcap" file */
431         errno = WTAP_ERR_CANT_READ;
432         bytes_read = file_read(&magic, 1, sizeof magic, wth->fh);
433         if (bytes_read != sizeof magic) {
434                 *err = file_error(wth->fh);
435                 if (*err != 0)
436                         return -1;
437                 return 0;
438         }
439         wth->data_offset += sizeof magic;
440
441         switch (magic) {
442
443         case PCAP_MAGIC:
444                 /* Host that wrote it has our byte order, and was running
445                    a program using either standard or ss990417 libpcap. */
446                 byte_swapped = FALSE;
447                 modified = FALSE;
448                 break;
449
450         case PCAP_MODIFIED_MAGIC:
451                 /* Host that wrote it has our byte order, and was running
452                    a program using either ss990915 or ss991029 libpcap. */
453                 byte_swapped = FALSE;
454                 modified = TRUE;
455                 break;
456
457         case PCAP_SWAPPED_MAGIC:
458                 /* Host that wrote it has a byte order opposite to ours,
459                    and was running a program using either standard or
460                    ss990417 libpcap. */
461                 byte_swapped = TRUE;
462                 modified = FALSE;
463                 break;
464
465         case PCAP_SWAPPED_MODIFIED_MAGIC:
466                 /* Host that wrote it out has a byte order opposite to
467                    ours, and was running a program using either ss990915
468                    or ss991029 libpcap. */
469                 byte_swapped = TRUE;
470                 modified = TRUE;
471                 break;
472
473         default:
474                 /* Not a "libpcap" type we know about. */
475                 return 0;
476         }
477
478         /* Read the rest of the header. */
479         errno = WTAP_ERR_CANT_READ;
480         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
481         if (bytes_read != sizeof hdr) {
482                 *err = file_error(wth->fh);
483                 if (*err != 0)
484                         return -1;
485                 return 0;
486         }
487         wth->data_offset += sizeof hdr;
488
489         if (byte_swapped) {
490                 /* Byte-swap the header fields about which we care. */
491                 hdr.version_major = BSWAP16(hdr.version_major);
492                 hdr.version_minor = BSWAP16(hdr.version_minor);
493                 hdr.snaplen = BSWAP32(hdr.snaplen);
494                 hdr.network = BSWAP32(hdr.network);
495         }
496         if (hdr.version_major < 2) {
497                 /* We only support version 2.0 and later. */
498                 g_message("pcap: major version %u unsupported",
499                     hdr.version_major);
500                 *err = WTAP_ERR_UNSUPPORTED;
501                 return -1;
502         }
503
504         /*
505          * AIX's non-standard tcpdump uses a minor version number of 2.
506          * Unfortunately, older versions of libpcap might have used
507          * that as well.
508          *
509          * The AIX libpcap uses RFC 1573 ifType values rather than
510          * DLT_ values in the header; the ifType values for LAN devices
511          * are:
512          *
513          *      Ethernet        6
514          *      Token Ring      9
515          *      FDDI            15
516          *
517          * which correspond to DLT_IEEE802 (used for Token Ring),
518          * DLT_PPP, and DLT_SLIP_BSDOS, respectively.  We shall
519          * assume that if the minor version number is 2, and
520          * the network type is 6, 9, or 15, that it's AIX libpcap.
521          *
522          * I'm assuming those older versions of libpcap didn't
523          * use DLT_IEEE802 for Token Ring, and didn't use DLT_SLIP_BSDOS
524          * as that came later.  It may have used DLT_PPP, however, in
525          * which case we're out of luck; we assume it's Token Ring
526          * in AIX libpcap rather than PPP in standard libpcap, as
527          * you're probably more likely to be handing an AIX libpcap
528          * token-ring capture than an old (pre-libpcap 0.4) PPP capture
529          * to Ethereal.
530          */
531         aix = FALSE;    /* assume it's not AIX */
532         if (hdr.version_major == 2 && hdr.version_minor == 2) {
533                 switch (hdr.network) {
534
535                 case 6:
536                         hdr.network = 1;        /* DLT_EN10MB, Ethernet */
537                         aix = TRUE;
538                         break;
539
540                 case 9:
541                         hdr.network = 6;        /* DLT_IEEE802, Token Ring */
542                         aix = TRUE;
543                         break;
544
545                 case 15:
546                         hdr.network = 10;       /* DLT_FDDI, FDDI */
547                         aix = TRUE;
548                         break;
549                 }
550         }
551         file_encap = wtap_pcap_encap_to_wtap_encap(hdr.network);
552         if (file_encap == WTAP_ENCAP_UNKNOWN) {
553                 g_message("pcap: network type %u unknown or unsupported",
554                     hdr.network);
555                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
556                 return -1;
557         }
558
559         /* This is a libpcap file */
560         wth->capture.pcap = g_malloc(sizeof(libpcap_t));
561         wth->capture.pcap->byte_swapped = byte_swapped;
562         wth->capture.pcap->version_major = hdr.version_major;
563         wth->capture.pcap->version_minor = hdr.version_minor;
564         wth->subtype_read = libpcap_read;
565         wth->subtype_seek_read = libpcap_seek_read;
566         wth->subtype_close = libpcap_close;
567         wth->file_encap = file_encap;
568         wth->snapshot_length = hdr.snaplen;
569
570         /*
571          * Is this AIX format?
572          */
573         if (aix) {
574                 /*
575                  * Yes.  Skip all the tests for other mutant formats.
576                  */
577                 wth->file_type = WTAP_FILE_PCAP_AIX;
578                 return 1;
579         }
580
581         /*
582          * No.  Let's look at the header for the first record,
583          * and see if, interpreting it as a standard header (if the
584          * magic number was standard) or a modified header (if the
585          * magic number was modified), the position where it says the
586          * header for the *second* record is contains a corrupted header.
587          *
588          * If so, then:
589          *
590          *      If this file had the standard magic number, it may be
591          *      an ss990417 capture file - in that version of Alexey's
592          *      patch, the packet header format was changed but the
593          *      magic number wasn't, and, alas, Red Hat appear to have
594          *      picked up that version of the patch for RH 6.1, meaning
595          *      RH 6.1 has a tcpdump that writes out files that can't
596          *      be read by any software that expects non-modified headers
597          *      if the magic number isn't the modified magic number (e.g.,
598          *      any normal version of tcpdump, and Ethereal if we don't
599          *      do this gross heuristic).
600          *
601          *      If this file had the modified magic number, it may be
602          *      an ss990915 capture file - in that version of Alexey's
603          *      patch, the magic number was changed, but the record
604          *      header had some extra fields, and, alas, SuSE appear
605          *      to have picked up that version of the patch for SuSE
606          *      6.3, meaning that programs expecting the standard per-
607          *      packet header in captures with the modified magic number
608          *      can't read dumps from its tcpdump.
609          *
610          * Oh, and if it has the standard magic number, it might, instead,
611          * be a Nokia libpcap file, so we may need to try that if
612          * neither normal nor ss990417 headers work.
613          */
614         if (modified) {
615                 /*
616                  * Well, we have the magic number from Alexey's
617                  * later two patches.
618                  *
619                  * Try ss991029, the last of his patches, first.
620                  */
621                 wth->file_type = WTAP_FILE_PCAP_SS991029;
622                 switch (libpcap_try(wth, err)) {
623
624                 case BAD_READ:
625                         /*
626                          * Well, we couldn't even read it.
627                          * Give up.
628                          */
629                         g_free(wth->capture.pcap);
630                         return -1;
631
632                 case THIS_FORMAT:
633                         /*
634                          * Well, it looks as if it might be 991029.
635                          * Put the seek pointer back, and return success.
636                          */
637                         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
638                                 g_free(wth->capture.pcap);
639                                 return -1;
640                         }
641                         return 1;
642
643                 case OTHER_FORMAT:
644                         /*
645                          * Try the next format.
646                          */
647                         break;
648                 }
649
650                 /*
651                  * Well, it's not completely unreadable,
652                  * but it's not ss991029.  Try ss990915;
653                  * there are no other types to try after that,
654                  * so we put the seek pointer back and treat
655                  * it as 990915.
656                  */
657                 wth->file_type = WTAP_FILE_PCAP_SS990915;
658                 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
659                         g_free(wth->capture.pcap);
660                         return -1;
661                 }
662         } else {
663                 /*
664                  * Well, we have the standard magic number.
665                  *
666                  * Try the standard format first.
667                  */
668                 wth->file_type = WTAP_FILE_PCAP;
669                 switch (libpcap_try(wth, err)) {
670
671                 case BAD_READ:
672                         /*
673                          * Well, we couldn't even read it.
674                          * Give up.
675                          */
676                         g_free(wth->capture.pcap);
677                         return -1;
678
679                 case THIS_FORMAT:
680                         /*
681                          * Well, it looks as if it might be a standard
682                          * libpcap file.
683                          * Put the seek pointer back, and return success.
684                          */
685                         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
686                                 g_free(wth->capture.pcap);
687                                 return -1;
688                         }
689                         return 1;
690
691                 case OTHER_FORMAT:
692                         /*
693                          * Try the next format.
694                          */
695                         break;
696                 }
697
698                 /*
699                  * Well, it's not completely unreadable, but it's not
700                  * a standard file.  Put the seek pointer back and try
701                  * ss990417.
702                  */
703                 wth->file_type = WTAP_FILE_PCAP_SS990417;
704                 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
705                         g_free(wth->capture.pcap);
706                         return -1;
707                 }
708                 switch (libpcap_try(wth, err)) {
709
710                 case BAD_READ:
711                         /*
712                          * Well, we couldn't even read it.
713                          * Give up.
714                          */
715                         g_free(wth->capture.pcap);
716                         return -1;
717
718                 case THIS_FORMAT:
719                         /*
720                          * Well, it looks as if it might be ss990417.
721                          * Put the seek pointer back, and return success.
722                          */
723                         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
724                                 g_free(wth->capture.pcap);
725                                 return -1;
726                         }
727                         return 1;
728
729                 case OTHER_FORMAT:
730                         /*
731                          * Try the next format.
732                          */
733                         break;
734                 }
735
736                 /*
737                  * Well, it's not completely unreadable,
738                  * but it's not a standard file *nor* is it ss990417.
739                  * Try it as a Nokia file; there are no other types
740                  * to try after that, so we put the seek pointer back
741                  * and treat it as a Nokia file.
742                  */
743                 wth->file_type = WTAP_FILE_PCAP_NOKIA;
744                 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1) {
745                         g_free(wth->capture.pcap);
746                         return -1;
747                 }
748         }
749
750         return 1;
751 }
752
753 /* Try to read the first two records of the capture file. */
754 static libpcap_try_t libpcap_try(wtap *wth, int *err)
755 {
756         /*
757          * pcaprec_ss990915_hdr is the largest header type.
758          */
759         struct pcaprec_ss990915_hdr first_rec_hdr, second_rec_hdr;
760
761         /*
762          * Attempt to read the first record's header.
763          */
764         if (libpcap_read_header(wth, err, &first_rec_hdr, TRUE) == -1) {
765                 if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
766                         /*
767                          * EOF or short read - assume the file is in this
768                          * format.
769                          * When our client tries to read the first packet
770                          * they will presumably get the same EOF or short
771                          * read.
772                          */
773                         return THIS_FORMAT;
774                 }
775
776                 if (*err == WTAP_ERR_BAD_RECORD) {
777                         /*
778                          * The first record is bogus, so this is probably
779                          * a corrupt file.  Assume the file is in this
780                          * format.  When our client tries to read the
781                          * first packet they will presumably get the
782                          * same bogus record.
783                          */
784                         return THIS_FORMAT;
785                 }
786
787                 /*
788                  * Some other error, e.g. an I/O error; just give up.
789                  */
790                 return BAD_READ;
791         }
792
793         /*
794          * Now skip over the first record's data, under the assumption
795          * that the header is sane.
796          */
797         if (file_seek(wth->fh, first_rec_hdr.hdr.incl_len, SEEK_CUR, err) == -1)
798                 return BAD_READ;
799
800         /*
801          * Now attempt to read the second record's header.
802          */
803         if (libpcap_read_header(wth, err, &second_rec_hdr, TRUE) == -1) {
804                 if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
805                         /*
806                          * EOF or short read - assume the file is in this
807                          * format.
808                          * When our client tries to read the second packet
809                          * they will presumably get the same EOF or short
810                          * read.
811                          */
812                         return THIS_FORMAT;
813                 }
814
815                 if (*err == WTAP_ERR_BAD_RECORD) {
816                         /*
817                          * The second record is bogus; maybe it's a
818                          * Capture File From Hell, and what looks like
819                          * the "header" of the next packet is actually
820                          * random junk from the middle of a packet.
821                          * Try the next format; if we run out of formats,
822                          * it probably *is* a corrupt file.
823                          */
824                         return OTHER_FORMAT;
825                 }
826
827                 /*
828                  * Some other error, e.g. an I/O error; just give up.
829                  */
830                 return BAD_READ;
831         }
832
833         /*
834          * OK, the first two records look OK; assume this is the
835          * right format.
836          */
837         return THIS_FORMAT;
838 }
839
840 /* Read the next packet */
841 static gboolean libpcap_read(wtap *wth, int *err, long *data_offset)
842 {
843         struct pcaprec_ss990915_hdr hdr;
844         guint packet_size;
845         guint orig_size;
846         int bytes_read;
847         char fddi_padding[3];
848
849         bytes_read = libpcap_read_header(wth, err, &hdr, FALSE);
850         if (bytes_read == -1) {
851                 /*
852                  * We failed to read the header.
853                  */
854                 return FALSE;
855         }
856
857         wth->data_offset += bytes_read;
858         packet_size = hdr.hdr.incl_len;
859         orig_size = hdr.hdr.orig_len;
860
861         /*
862          * AIX appears to put 3 bytes of padding in front of FDDI
863          * frames; strip that crap off.
864          */
865         if (wth->file_type == WTAP_FILE_PCAP_AIX &&
866             (wth->file_encap == WTAP_ENCAP_FDDI ||
867              wth->file_encap == WTAP_ENCAP_FDDI_BITSWAPPED)) {
868                 /*
869                  * The packet size is really a record size and includes
870                  * the padding.
871                  */
872                 packet_size -= 3;
873                 orig_size -= 3;
874                 wth->data_offset += 3;
875
876                 /*
877                  * Read the padding.
878                  */
879                 if (!libpcap_read_rec_data(wth->fh, fddi_padding, 3, err))
880                         return FALSE;   /* Read error */
881         }
882
883         *data_offset = wth->data_offset;
884
885         /*
886          * If this is an ATM packet, the first four bytes are the
887          * direction of the packet (transmit/receive), the VPI, and
888          * the VCI; read them and generate the pseudo-header from
889          * them.
890          */
891         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
892                 if (packet_size < sizeof (struct sunatm_hdr)) {
893                         /*
894                          * Uh-oh, the packet isn't big enough to even
895                          * have a pseudo-header.
896                          */
897                         g_message("libpcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
898                             packet_size);
899                         *err = WTAP_ERR_BAD_RECORD;
900                         return FALSE;
901                 }
902                 if (!libpcap_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
903                     err))
904                         return FALSE;   /* Read error */
905
906                 /*
907                  * Don't count the pseudo-header as part of the packet.
908                  */
909                 orig_size -= sizeof (struct sunatm_hdr);
910                 packet_size -= sizeof (struct sunatm_hdr);
911                 wth->data_offset += sizeof (struct sunatm_hdr);
912         }
913
914         buffer_assure_space(wth->frame_buffer, packet_size);
915         if (!libpcap_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
916             packet_size, err))
917                 return FALSE;   /* Read error */
918         wth->data_offset += packet_size;
919
920         wth->phdr.ts.tv_sec = hdr.hdr.ts_sec;
921         wth->phdr.ts.tv_usec = hdr.hdr.ts_usec;
922         wth->phdr.caplen = packet_size;
923         wth->phdr.len = orig_size;
924         wth->phdr.pkt_encap = wth->file_encap;
925
926         /*
927          * If this is ATM LANE traffic, try to guess what type of LANE
928          * traffic it is based on the packet contents.
929          */
930         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
931             wth->pseudo_header.atm.type == TRAF_LANE) {
932                 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
933                     wth->phdr.caplen, &wth->pseudo_header);
934         }
935
936         return TRUE;
937 }
938
939 static gboolean
940 libpcap_seek_read(wtap *wth, long seek_off,
941     union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err)
942 {
943         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
944                 return FALSE;
945
946         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
947                 if (!libpcap_read_atm_pseudoheader(wth->random_fh, pseudo_header,
948                     err)) {
949                         /* Read error */
950                         return FALSE;
951                 }
952         }
953
954         /*
955          * Read the packet data.
956          */
957         if (!libpcap_read_rec_data(wth->random_fh, pd, length, err))
958                 return FALSE;   /* failed */
959
960         /*
961          * If this is ATM LANE traffic, try to guess what type of LANE
962          * traffic it is based on the packet contents.
963          */
964         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
965             pseudo_header->atm.type == TRAF_LANE)
966                 atm_guess_lane_type(pd, length, pseudo_header);
967         return TRUE;
968 }
969
970 /* Read the header of the next packet; if "silent" is TRUE, don't complain
971    to the console, as we're testing to see if the file appears to be of a
972    particular type.
973
974    Return -1 on an error, or the number of bytes of header read on success. */
975 static int libpcap_read_header(wtap *wth, int *err,
976     struct pcaprec_ss990915_hdr *hdr, gboolean silent)
977 {
978         int     bytes_to_read, bytes_read;
979
980         /* Read record header. */
981         errno = WTAP_ERR_CANT_READ;
982         switch (wth->file_type) {
983
984         case WTAP_FILE_PCAP:
985         case WTAP_FILE_PCAP_AIX:
986                 bytes_to_read = sizeof (struct pcaprec_hdr);
987                 break;
988
989         case WTAP_FILE_PCAP_SS990417:
990         case WTAP_FILE_PCAP_SS991029:
991                 bytes_to_read = sizeof (struct pcaprec_modified_hdr);
992                 break;
993
994         case WTAP_FILE_PCAP_SS990915:
995                 bytes_to_read = sizeof (struct pcaprec_ss990915_hdr);
996                 break;
997
998         case WTAP_FILE_PCAP_NOKIA:
999                 bytes_to_read = sizeof (struct pcaprec_nokia_hdr);
1000                 break;
1001
1002         default:
1003                 g_assert_not_reached();
1004                 bytes_to_read = 0;
1005         }
1006         bytes_read = file_read(hdr, 1, bytes_to_read, wth->fh);
1007         if (bytes_read != bytes_to_read) {
1008                 *err = file_error(wth->fh);
1009                 if (*err == 0 && bytes_read != 0) {
1010                         *err = WTAP_ERR_SHORT_READ;
1011                 }
1012                 return -1;
1013         }
1014
1015         adjust_header(wth, &hdr->hdr);
1016
1017         if (hdr->hdr.incl_len > WTAP_MAX_PACKET_SIZE) {
1018                 /*
1019                  * Probably a corrupt capture file; return an error,
1020                  * so that our caller doesn't blow up trying to allocate
1021                  * space for an immensely-large packet, and so that
1022                  * the code to try to guess what type of libpcap file
1023                  * this is can tell when it's not the type we're guessing
1024                  * it is.
1025                  */
1026                 if (!silent) {
1027                         g_message("pcap: File has %u-byte packet, bigger than maximum of %u",
1028                             hdr->hdr.incl_len, WTAP_MAX_PACKET_SIZE);
1029                 }
1030                 *err = WTAP_ERR_BAD_RECORD;
1031                 return -1;
1032         }
1033
1034         if (hdr->hdr.orig_len > WTAP_MAX_PACKET_SIZE) {
1035                 /*
1036                  * Probably a corrupt capture file; return an error,
1037                  * so that our caller doesn't blow up trying to
1038                  * cope with a huge "real" packet length, and so that
1039                  * the code to try to guess what type of libpcap file
1040                  * this is can tell when it's not the type we're guessing
1041                  * it is.
1042                  */
1043                 if (!silent) {
1044                         g_message("pcap: File has %u-byte packet, bigger than maximum of %u",
1045                             hdr->hdr.orig_len, WTAP_MAX_PACKET_SIZE);
1046                 }
1047                 *err = WTAP_ERR_BAD_RECORD;
1048                 return -1;
1049         }
1050
1051         return bytes_read;
1052 }
1053
1054 static void
1055 adjust_header(wtap *wth, struct pcaprec_hdr *hdr)
1056 {
1057         if (wth->capture.pcap->byte_swapped) {
1058                 /* Byte-swap the record header fields. */
1059                 hdr->ts_sec = BSWAP32(hdr->ts_sec);
1060                 hdr->ts_usec = BSWAP32(hdr->ts_usec);
1061                 hdr->incl_len = BSWAP32(hdr->incl_len);
1062                 hdr->orig_len = BSWAP32(hdr->orig_len);
1063         }
1064
1065         /* If this is AIX, convert the time stamp from seconds/nanoseconds
1066            to seconds/microseconds.  */
1067         if (wth->file_type == WTAP_FILE_PCAP_AIX)
1068                 hdr->ts_usec = hdr->ts_usec/1000;
1069
1070         /* In file format version 2.3, the "incl_len" and "orig_len" fields
1071            were swapped, in order to match the BPF header layout.
1072
1073            Unfortunately, some files were, according to a comment in the
1074            "libpcap" source, written with version 2.3 in their headers
1075            but without the interchanged fields, so if "incl_len" is
1076            greater than "orig_len" - which would make no sense - we
1077            assume that we need to swap them.  */
1078         if (wth->capture.pcap->version_major == 2 &&
1079             (wth->capture.pcap->version_minor < 3 ||
1080              (wth->capture.pcap->version_minor == 3 &&
1081               hdr->incl_len > hdr->orig_len))) {
1082                 guint32 temp;
1083
1084                 temp = hdr->orig_len;
1085                 hdr->orig_len = hdr->incl_len;
1086                 hdr->incl_len = temp;
1087         }
1088 }
1089
1090 static void
1091 libpcap_get_atm_pseudoheader(const struct sunatm_hdr *atm_phdr,
1092     union wtap_pseudo_header *pseudo_header)
1093 {
1094         guint8  vpi;
1095         guint16 vci;
1096
1097         vpi = atm_phdr->vpi;
1098         vci = pntohs(&atm_phdr->vci);
1099
1100         /*
1101          * The lower 4 bits of the first byte of the header indicate
1102          * the type of traffic, as per the "atmioctl.h" header in
1103          * SunATM.
1104          */
1105         switch (atm_phdr->flags & 0x0F) {
1106
1107         case 0x01:      /* LANE */
1108                 pseudo_header->atm.aal = AAL_5;
1109                 pseudo_header->atm.type = TRAF_LANE;
1110                 break;
1111
1112         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
1113                 pseudo_header->atm.aal = AAL_5;
1114                 pseudo_header->atm.type = TRAF_LLCMX;
1115                 break;
1116
1117         case 0x05:      /* ILMI */
1118                 pseudo_header->atm.aal = AAL_5;
1119                 pseudo_header->atm.type = TRAF_ILMI;
1120                 break;
1121
1122         case 0x06:      /* Q.2931 */
1123                 pseudo_header->atm.aal = AAL_SIGNALLING;
1124                 pseudo_header->atm.type = TRAF_UNKNOWN;
1125                 break;
1126
1127         case 0x03:      /* MARS (RFC 2022) */
1128                 pseudo_header->atm.aal = AAL_5;
1129                 pseudo_header->atm.type = TRAF_UNKNOWN;
1130                 break;
1131
1132         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
1133                 pseudo_header->atm.aal = AAL_5;
1134                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
1135                 break;
1136
1137         default:
1138                 /*
1139                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
1140                  * case assume it's AAL_SIGNALLING; we know nothing more
1141                  * about it.
1142                  *
1143                  * XXX - is this necessary?  Or are we guaranteed that
1144                  * all signalling traffic has a type of 0x06?
1145                  *
1146                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
1147                  * 0x00 ("raw"), might it be non-AAL5 traffic?
1148                  */
1149                 if (vpi == 0 && vci == 5)
1150                         pseudo_header->atm.aal = AAL_SIGNALLING;
1151                 else
1152                         pseudo_header->atm.aal = AAL_5;
1153                 pseudo_header->atm.type = TRAF_UNKNOWN;
1154                 break;
1155         }
1156         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1157
1158         pseudo_header->atm.vpi = vpi;
1159         pseudo_header->atm.vci = vci;
1160         pseudo_header->atm.channel = (atm_phdr->flags & 0x80) ? 1 : 0;
1161
1162         /* We don't have this information */
1163         pseudo_header->atm.cells = 0;
1164         pseudo_header->atm.aal5t_u2u = 0;
1165         pseudo_header->atm.aal5t_len = 0;
1166         pseudo_header->atm.aal5t_chksum = 0;
1167 }
1168
1169 static gboolean
1170 libpcap_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1171     int *err)
1172 {
1173         struct sunatm_hdr atm_phdr;
1174         int     bytes_read;
1175
1176         errno = WTAP_ERR_CANT_READ;
1177         bytes_read = file_read(&atm_phdr, 1, sizeof (struct sunatm_hdr), fh);
1178         if (bytes_read != sizeof (struct sunatm_hdr)) {
1179                 *err = file_error(fh);
1180                 if (*err == 0)
1181                         *err = WTAP_ERR_SHORT_READ;
1182                 return FALSE;
1183         }
1184
1185         libpcap_get_atm_pseudoheader(&atm_phdr, pseudo_header);
1186
1187         return TRUE;
1188 }
1189
1190 static gboolean
1191 libpcap_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
1192 {
1193         int     bytes_read;
1194
1195         errno = WTAP_ERR_CANT_READ;
1196         bytes_read = file_read(pd, 1, length, fh);
1197
1198         if (bytes_read != length) {
1199                 *err = file_error(fh);
1200                 if (*err == 0)
1201                         *err = WTAP_ERR_SHORT_READ;
1202                 return FALSE;
1203         }
1204         return TRUE;
1205 }
1206
1207 static void
1208 libpcap_close(wtap *wth)
1209 {
1210         g_free(wth->capture.pcap);
1211 }
1212
1213 static int wtap_wtap_encap_to_pcap_encap(int encap)
1214 {
1215         unsigned int i;
1216
1217         /*
1218          * Special-case WTAP_ENCAP_FDDI and WTAP_ENCAP_FDDI_BITSWAPPED;
1219          * both of them get mapped to DLT_FDDI (even though that may
1220          * mean that the bit order in the FDDI MAC addresses is wrong;
1221          * so it goes - libpcap format doesn't record the byte order,
1222          * so that's not fixable).
1223          */
1224         if (encap == WTAP_ENCAP_FDDI || encap == WTAP_ENCAP_FDDI_BITSWAPPED)
1225                 return 10;      /* that's DLT_FDDI */
1226         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
1227                 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
1228                         return pcap_to_wtap_map[i].dlt_value;
1229         }
1230         return -1;
1231 }
1232
1233 #ifdef HAVE_PCAP_H
1234 /*
1235  * Given a Wiretap encapsulation type, and raw packet data and the packet
1236  * header from libpcap, process any pseudo-header in the packet,
1237  * fill in the Wiretap packet header, and return a pointer to the
1238  * beginning of the non-pseudo-header data in the packet.
1239  */
1240 const guchar *
1241 wtap_process_pcap_packet(gint linktype, const struct pcap_pkthdr *phdr,
1242     const guchar *pd, union wtap_pseudo_header *pseudo_header,
1243     struct wtap_pkthdr *whdr, int *err)
1244 {
1245         /* "phdr->ts" may not necessarily be a "struct timeval" - it may
1246            be a "struct bpf_timeval", with member sizes wired to 32
1247            bits - and we may go that way ourselves in the future, so
1248            copy the members individually. */
1249         whdr->ts.tv_sec = phdr->ts.tv_sec;
1250         whdr->ts.tv_usec = phdr->ts.tv_usec;
1251         whdr->caplen = phdr->caplen;
1252         whdr->len = phdr->len;
1253         whdr->pkt_encap = linktype;
1254
1255         /*
1256          * If this is an ATM packet, the first four bytes are the
1257          * direction of the packet (transmit/receive), the VPI, and
1258          * the VCI; read them and generate the pseudo-header from
1259          * them.
1260          */
1261         if (linktype == WTAP_ENCAP_ATM_PDUS) {
1262                 if (whdr->caplen < sizeof (struct sunatm_hdr)) {
1263                         /*
1264                          * Uh-oh, the packet isn't big enough to even
1265                          * have a pseudo-header.
1266                          */
1267                         g_message("libpcap: SunATM capture has a %u-byte packet, too small to have even an ATM pseudo-header\n",
1268                             whdr->caplen);
1269                         *err = WTAP_ERR_BAD_RECORD;
1270                         return NULL;
1271                 }
1272                 libpcap_get_atm_pseudoheader((const struct sunatm_hdr *)pd,
1273                     pseudo_header);
1274
1275                 /*
1276                  * Don't count the pseudo-header as part of the packet.
1277                  */
1278                 whdr->len -= sizeof (struct sunatm_hdr);
1279                 whdr->caplen -= sizeof (struct sunatm_hdr);
1280                 pd += sizeof (struct sunatm_hdr);
1281
1282                 /*
1283                  * If this is ATM LANE traffic, try to guess what type of
1284                  * LANE traffic it is based on the packet contents.
1285                  */
1286                 if (pseudo_header->atm.type == TRAF_LANE)
1287                         atm_guess_lane_type(pd, whdr->caplen, pseudo_header);
1288         }
1289         return pd;
1290 }
1291 #endif
1292
1293 /* Returns 0 if we could write the specified encapsulation type,
1294    an error indication otherwise. */
1295 int libpcap_dump_can_write_encap(int encap)
1296 {
1297         /* Per-packet encapsulations aren't supported. */
1298         if (encap == WTAP_ENCAP_PER_PACKET)
1299                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
1300
1301         if (wtap_wtap_encap_to_pcap_encap(encap) == -1)
1302                 return WTAP_ERR_UNSUPPORTED_ENCAP;
1303
1304         return 0;
1305 }
1306
1307 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
1308    failure */
1309 gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
1310 {
1311         guint32 magic;
1312         struct pcap_hdr file_hdr;
1313         size_t nwritten;
1314
1315         /* This is a libpcap file */
1316         wdh->subtype_write = libpcap_dump;
1317         wdh->subtype_close = NULL;
1318
1319         /* Write the file header. */
1320         switch (wdh->file_type) {
1321
1322         case WTAP_FILE_PCAP:
1323         case WTAP_FILE_PCAP_SS990417:   /* modified, but with the old magic, sigh */
1324         case WTAP_FILE_PCAP_NOKIA:      /* Nokia libpcap of some sort */
1325                 magic = PCAP_MAGIC;
1326                 break;
1327
1328         case WTAP_FILE_PCAP_SS990915:   /* new magic, extra crap */
1329         case WTAP_FILE_PCAP_SS991029:
1330                 magic = PCAP_MODIFIED_MAGIC;
1331                 break;
1332
1333         default:
1334                 /* We should never get here - our open routine
1335                    should only get called for the types above. */
1336                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
1337                 return FALSE;
1338         }
1339
1340         nwritten = fwrite(&magic, 1, sizeof magic, wdh->fh);
1341         if (nwritten != sizeof magic) {
1342                 if (nwritten == 0 && ferror(wdh->fh))
1343                         *err = errno;
1344                 else
1345                         *err = WTAP_ERR_SHORT_WRITE;
1346                 return FALSE;
1347         }
1348         wdh->bytes_dumped += sizeof magic;
1349
1350         /* current "libpcap" format is 2.4 */
1351         file_hdr.version_major = 2;
1352         file_hdr.version_minor = 4;
1353         file_hdr.thiszone = 0;  /* XXX - current offset? */
1354         file_hdr.sigfigs = 0;   /* unknown, but also apparently unused */
1355         /*
1356          * Tcpdump cannot handle capture files with a snapshot length of 0,
1357          * as BPF filters return either 0 if they fail or the snapshot length
1358          * if they succeed, and a snapshot length of 0 means success is
1359          * indistinguishable from failure and the filter expression would
1360          * reject all packets.
1361          *
1362          * A snapshot length of 0, inside Wiretap, means "snapshot length
1363          * unknown"; if the snapshot length supplied to us is 0, we make
1364          * the snapshot length in the header file WTAP_MAX_PACKET_SIZE.
1365          */
1366         file_hdr.snaplen = (wdh->snaplen != 0) ? wdh->snaplen :
1367                                                  WTAP_MAX_PACKET_SIZE;
1368         file_hdr.network = wtap_wtap_encap_to_pcap_encap(wdh->encap);
1369         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
1370         if (nwritten != sizeof file_hdr) {
1371                 if (nwritten == 0 && ferror(wdh->fh))
1372                         *err = errno;
1373                 else
1374                         *err = WTAP_ERR_SHORT_WRITE;
1375                 return FALSE;
1376         }
1377         wdh->bytes_dumped += sizeof file_hdr;
1378
1379         return TRUE;
1380 }
1381
1382 /* Write a record for a packet to a dump file.
1383    Returns TRUE on success, FALSE on failure. */
1384 static gboolean libpcap_dump(wtap_dumper *wdh,
1385         const struct wtap_pkthdr *phdr,
1386         const union wtap_pseudo_header *pseudo_header _U_,
1387         const guchar *pd, int *err)
1388 {
1389         struct pcaprec_ss990915_hdr rec_hdr;
1390         size_t hdr_size;
1391         size_t nwritten;
1392         struct sunatm_hdr atm_hdr;
1393         int atm_hdrsize;
1394
1395         if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
1396                 atm_hdrsize = sizeof (struct sunatm_hdr);
1397         else
1398                 atm_hdrsize = 0;
1399
1400         rec_hdr.hdr.ts_sec = phdr->ts.tv_sec;
1401         rec_hdr.hdr.ts_usec = phdr->ts.tv_usec;
1402         rec_hdr.hdr.incl_len = phdr->caplen + atm_hdrsize;
1403         rec_hdr.hdr.orig_len = phdr->len + atm_hdrsize;
1404         switch (wdh->file_type) {
1405
1406         case WTAP_FILE_PCAP:
1407                 hdr_size = sizeof (struct pcaprec_hdr);
1408                 break;
1409
1410         case WTAP_FILE_PCAP_SS990417:   /* modified, but with the old magic, sigh */
1411         case WTAP_FILE_PCAP_SS991029:
1412                 /* XXX - what should we supply here?
1413
1414                    Alexey's "libpcap" looks up the interface in the system's
1415                    interface list if "ifindex" is non-zero, and prints
1416                    the interface name.  It ignores "protocol", and uses
1417                    "pkt_type" to tag the packet as "host", "broadcast",
1418                    "multicast", "other host", "outgoing", or "none of the
1419                    above", but that's it.
1420
1421                    If the capture we're writing isn't a modified or
1422                    RH 6.1 capture, we'd have to do some work to
1423                    generate the packet type and interface index - and
1424                    we can't generate the interface index unless we
1425                    just did the capture ourselves in any case.
1426
1427                    I'm inclined to continue to punt; systems other than
1428                    those with the older patch can read standard "libpcap"
1429                    files, and systems with the older patch, e.g. RH 6.1,
1430                    will just have to live with this. */
1431                 rec_hdr.ifindex = 0;
1432                 rec_hdr.protocol = 0;
1433                 rec_hdr.pkt_type = 0;
1434                 hdr_size = sizeof (struct pcaprec_modified_hdr);
1435                 break;
1436
1437         case WTAP_FILE_PCAP_SS990915:   /* new magic, extra crap at the end */
1438                 rec_hdr.ifindex = 0;
1439                 rec_hdr.protocol = 0;
1440                 rec_hdr.pkt_type = 0;
1441                 rec_hdr.cpu1 = 0;
1442                 rec_hdr.cpu2 = 0;
1443                 hdr_size = sizeof (struct pcaprec_ss990915_hdr);
1444                 break;
1445
1446         case WTAP_FILE_PCAP_NOKIA:      /* old magic, extra crap at the end */
1447                 rec_hdr.ifindex = 0;
1448                 rec_hdr.protocol = 0;
1449                 rec_hdr.pkt_type = 0;
1450                 rec_hdr.cpu1 = 0;
1451                 rec_hdr.cpu2 = 0;
1452                 hdr_size = sizeof (struct pcaprec_nokia_hdr);
1453                 break;
1454
1455         default:
1456                 /* We should never get here - our open routine
1457                    should only get called for the types above. */
1458                 g_assert_not_reached();
1459                 *err = WTAP_ERR_UNSUPPORTED_FILE_TYPE;
1460                 return FALSE;
1461         }
1462
1463         nwritten = fwrite(&rec_hdr, 1, hdr_size, wdh->fh);
1464         if (nwritten != hdr_size) {
1465                 if (nwritten == 0 && ferror(wdh->fh))
1466                         *err = errno;
1467                 else
1468                         *err = WTAP_ERR_SHORT_WRITE;
1469                 return FALSE;
1470         }
1471         wdh->bytes_dumped += hdr_size;
1472
1473         if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
1474                 /*
1475                  * Write the ATM header.
1476                  */
1477                 atm_hdr.flags =
1478                     (pseudo_header->atm.channel != 0) ? 0x80 : 0x00;
1479                 switch (pseudo_header->atm.aal) {
1480
1481                 case AAL_SIGNALLING:
1482                         /* Q.2931 */
1483                         atm_hdr.flags |= 0x06;
1484                         break;
1485
1486                 case AAL_5:
1487                         switch (pseudo_header->atm.type) {
1488
1489                         case TRAF_LANE:
1490                                 /* LANE */
1491                                 atm_hdr.flags |= 0x01;
1492                                 break;
1493
1494                         case TRAF_LLCMX:
1495                                 /* RFC 1483 LLC multiplexed traffic */
1496                                 atm_hdr.flags |= 0x02;
1497                                 break;
1498
1499                         case TRAF_ILMI:
1500                                 /* ILMI */
1501                                 atm_hdr.flags |= 0x05;
1502                                 break;
1503                         }
1504                         break;
1505                 }
1506                 atm_hdr.vpi = pseudo_header->atm.vpi;
1507                 atm_hdr.vci = phtons(&pseudo_header->atm.vci);
1508                 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
1509                 if (nwritten != sizeof atm_hdr) {
1510                         if (nwritten == 0 && ferror(wdh->fh))
1511                                 *err = errno;
1512                         else
1513                                 *err = WTAP_ERR_SHORT_WRITE;
1514                         return FALSE;
1515                 }
1516                 wdh->bytes_dumped += sizeof atm_hdr;
1517         }
1518
1519         nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
1520         if (nwritten != phdr->caplen) {
1521                 if (nwritten == 0 && ferror(wdh->fh))
1522                         *err = errno;
1523                 else
1524                         *err = WTAP_ERR_SHORT_WRITE;
1525                 return FALSE;
1526         }
1527         wdh->bytes_dumped += phdr->caplen;
1528         return TRUE;
1529 }