Ethereal->Wireshark
[obnox/wireshark/wip.git] / wiretap / snoop.c
1 /* snoop.c
2  *
3  * $Id$
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 #include <errno.h>
27 #include <string.h>
28 #include "wtap-int.h"
29 #include "file_wrappers.h"
30 #include "buffer.h"
31 #include "atm.h"
32 #include "snoop.h"
33 /* See RFC 1761 for a description of the "snoop" file format. */
34
35 /* Magic number in "snoop" files. */
36 static const char snoop_magic[] = {
37         's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
38 };
39
40 /* "snoop" file header (minus magic number). */
41 struct snoop_hdr {
42         guint32 version;        /* version number (should be 2) */
43         guint32 network;        /* network type */
44 };
45
46 /* "snoop" record header. */
47 struct snooprec_hdr {
48         guint32 orig_len;       /* actual length of packet */
49         guint32 incl_len;       /* number of octets captured in file */
50         guint32 rec_len;        /* length of record */
51         guint32 cum_drops;      /* cumulative number of dropped packets */
52         guint32 ts_sec;         /* timestamp seconds */
53         guint32 ts_usec;        /* timestamp microseconds */
54 };
55
56 /*
57  * The link-layer header on ATM packets.
58  */
59 struct snoop_atm_hdr {
60         guint8  flags;          /* destination and traffic type */
61         guint8  vpi;            /* VPI */
62         guint16 vci;            /* VCI */
63 };
64
65 /*
66  * Extra information stuffed into the padding in Shomiti/Finisar Surveyor
67  * captures.
68  */
69 struct shomiti_trailer {
70         guint16 phy_rx_length;  /* length on the wire, including FCS? */
71         guint16 phy_rx_status;  /* status flags */
72         guint32 ts_40_ns_lsb;   /* 40 ns time stamp, low-order bytes? */
73         guint32 ts_40_ns_msb;   /* 40 ns time stamp, low-order bytes? */
74         gint32  frame_id;       /* "FrameID"? */
75 };
76
77 /*
78  * phy_rx_status flags.
79  */
80 #define RX_STATUS_OVERFLOW              0x8000  /* overflow error */
81 #define RX_STATUS_BAD_CRC               0x4000  /* CRC error */
82 #define RX_STATUS_DRIBBLE_NIBBLE        0x2000  /* dribble/nibble bits? */
83 #define RX_STATUS_SHORT_FRAME           0x1000  /* frame < 64 bytes */
84 #define RX_STATUS_OVERSIZE_FRAME        0x0800  /* frame > 1518 bytes */
85 #define RX_STATUS_GOOD_FRAME            0x0400  /* frame OK */
86 #define RX_STATUS_N12_BYTES_RECEIVED    0x0200  /* first 12 bytes of frame received? */
87 #define RX_STATUS_RXABORT               0x0100  /* RXABORT during reception */
88 #define RX_STATUS_FIFO_ERROR            0x0080  /* receive FIFO error */
89 #define RX_STATUS_TRIGGERED             0x0001  /* frame did trigger */
90
91 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
92     long *data_offset);
93 static gboolean snoop_seek_read(wtap *wth, long seek_off,
94     union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
95     int *err, gchar **err_info);
96 static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
97     union wtap_pseudo_header *pseudo_header, int *err);
98 static gboolean snoop_read_rec_data(FILE_T fh, guchar *pd, int length,
99     int *err);
100 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
101     const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
102
103 /*
104  * See
105  *
106  *      http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
107  *
108  * for the "dlpi.h" header file specified by The Open Group, which lists
109  * the DL_ values for various protocols; Solaris 7 uses the same values.
110  *
111  * The page at
112  *
113  *      http://mrpink.lerc.nasa.gov/118x/support.html
114  *
115  * had links to modified versions of "tcpdump" and "libpcap" for SUNatm
116  * DLPI support; they suggested that the 3.0 verson of SUNatm uses those
117  * values.  The Wayback Machine archived that page, but not the stuff
118  * to which it linked, unfortunately.
119  *
120  * It also has a link to "convert.c", which is a program to convert files
121  * from the format written by the "atmsnoop" program that comes with the
122  * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
123  * to be DL_FDDI (don't ask why).  SunATM 3.0 claims to be DL_IPATM, which
124  * is 0x12".
125  *
126  * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
127  * that in an "atmsnoop" file, the header contains 2 bytes (direction and
128  * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
129  * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
130  * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
131  * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
132  * 1 byte of VPI, and 2 bytes of VCI.
133  *
134  * The aforementioned page also has a link to some capture files from
135  * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
136  *
137  * Source to an "atmdump" package, which includes a modified version of
138  * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
139  * also includes "atmdump", which is a modified "tcpdump", is available
140  * at
141  *
142  *      ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
143  *
144  * and that code also indicates that DL_IPATM is used, and that an
145  * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
146  * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
147  * and then the ATM PDU, and suggests that the direction flag is 0x80 for
148  * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
149  * "received" (presumably meaning DCE->DTE).  That code was used as the
150  * basis for the SunATM support in current CVS versions of libpcap and
151  * tcpdump, and it works.
152  *
153  * In fact, the "direction" byte appears to have some other stuff, perhaps
154  * a traffic type, in the lower 7 bits, with the 8th bit indicating the
155  * direction.  That appears to be the case.
156  *
157  * I don't know what the encapsulation of any of the other types is, so I
158  * leave them all as WTAP_ENCAP_UNKNOWN, except for those for which Brian
159  * Ginsbach has supplied information about the way UNICOS/mp uses them.
160  * I also don't know whether "snoop" can handle any of them (it presumably
161  * can't handle ATM, otherwise Sun wouldn't have supplied "atmsnoop"; even
162  * if it can't, this may be useful reference information for anybody doing
163  * code to use DLPI to do raw packet captures on those network types.
164  *
165  * See
166  *
167  *      http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
168  *
169  * for information on Shomiti's mutant flavor of snoop.  For some unknown
170  * unknown reason, they decided not to just Go With The DLPI Flow, and
171  * instead used the types unspecified in RFC 1461 for their own nefarious
172  * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
173  * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
174  * of them from the "Shomiti" versions of same.
175  */
176 int snoop_open(wtap *wth, int *err, gchar **err_info)
177 {
178         int bytes_read;
179         char magic[sizeof snoop_magic];
180         struct snoop_hdr hdr;
181         struct snooprec_hdr rec_hdr;
182         guint padbytes;
183         gboolean is_shomiti;
184         static const int snoop_encap[] = {
185                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
186                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
187                 WTAP_ENCAP_TOKEN_RING,
188                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
189                 WTAP_ENCAP_ETHERNET,
190                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
191                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
192                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
193                 WTAP_ENCAP_FDDI_BITSWAPPED,
194                 WTAP_ENCAP_NULL,        /* Other */
195                 WTAP_ENCAP_UNKNOWN,     /* Frame Relay LAPF */
196                 WTAP_ENCAP_UNKNOWN,     /* Multi-protocol over Frame Relay */
197                 WTAP_ENCAP_UNKNOWN,     /* Character Async (e.g., SLIP and PPP?) */
198                 WTAP_ENCAP_UNKNOWN,     /* X.25 Classical IP */
199                 WTAP_ENCAP_NULL,        /* software loopback */
200                 WTAP_ENCAP_UNKNOWN,     /* not defined in "dlpi.h" */
201                 WTAP_ENCAP_IP_OVER_FC,  /* Fibre Channel */
202                 WTAP_ENCAP_UNKNOWN,     /* ATM */
203                 WTAP_ENCAP_ATM_PDUS,    /* ATM Classical IP */
204                 WTAP_ENCAP_UNKNOWN,     /* X.25 LAPB */
205                 WTAP_ENCAP_UNKNOWN,     /* ISDN */
206                 WTAP_ENCAP_UNKNOWN,     /* HIPPI */
207                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Ethernet */
208                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Token Ring */
209                 WTAP_ENCAP_UNKNOWN,     /* "ISO 8802/3 and Ethernet" */
210                 WTAP_ENCAP_UNKNOWN,     /* 100BaseT (but that's just Ethernet) */
211         };
212         #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
213         static const int shomiti_encap[] = {
214                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
215                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
216                 WTAP_ENCAP_TOKEN_RING,
217                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
218                 WTAP_ENCAP_ETHERNET,
219                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
220                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
221                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
222                 WTAP_ENCAP_FDDI_BITSWAPPED,
223                 WTAP_ENCAP_UNKNOWN,     /* Other */
224                 WTAP_ENCAP_ETHERNET,    /* Fast Ethernet */
225                 WTAP_ENCAP_TOKEN_RING,  /* 4MB 802.5 token ring */
226                 WTAP_ENCAP_ETHERNET,    /* Gigabit Ethernet */
227                 WTAP_ENCAP_TOKEN_RING,  /* "IEEE 802.5 Shomiti" */
228                 WTAP_ENCAP_TOKEN_RING,  /* "4MB IEEE 802.5 Shomiti" */
229         };
230         #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
231         int file_encap;
232
233         /* Read in the string that should be at the start of a "snoop" file */
234         errno = WTAP_ERR_CANT_READ;
235         bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
236         if (bytes_read != sizeof magic) {
237                 *err = file_error(wth->fh);
238                 if (*err != 0)
239                         return -1;
240                 return 0;
241         }
242         wth->data_offset += sizeof magic;
243
244         if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
245                 return 0;
246         }
247
248         /* Read the rest of the header. */
249         errno = WTAP_ERR_CANT_READ;
250         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
251         if (bytes_read != sizeof hdr) {
252                 *err = file_error(wth->fh);
253                 if (*err != 0)
254                         return -1;
255                 return 0;
256         }
257         wth->data_offset += sizeof hdr;
258
259         /*
260          * Make sure it's a version we support.
261          */
262         hdr.version = g_ntohl(hdr.version);
263         switch (hdr.version) {
264
265         case 2:         /* Solaris 2.x and later snoop, and Shomiti
266                            Surveyor prior to 3.0, or 3.0 and later
267                            with NDIS card */
268         case 3:         /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
269         case 4:         /* Surveyor 3.0 and later, with Shomiti GAM hardware */
270         case 5:         /* Surveyor 3.0 and later, with Shomiti THG hardware */
271                 break;
272
273         default:
274                 *err = WTAP_ERR_UNSUPPORTED;
275                 *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
276                 return -1;
277         }
278
279         /*
280          * Oh, this is lovely.
281          *
282          * I suppose Shomiti could give a bunch of lawyerly noise about
283          * how "well, RFC 1761 said they were unassigned, and that's
284          * the standard, not the DLPI header file, so it's perfectly OK
285          * for us to use them, blah blah blah", but it's still irritating
286          * as hell that they used the unassigned-in-RFC-1761 values for
287          * their own purposes - especially given that Sun also used
288          * one of them in atmsnoop.
289          *
290          * We can't determine whether it's a Shomiti capture based on
291          * the version number, as, according to their documentation on
292          * their capture file format, Shomiti uses a version number of 2
293          * if the data "was captured using an NDIS card", which presumably
294          * means "captured with an ordinary boring network card via NDIS"
295          * as opposed to "captured with our whizzo special capture
296          * hardware".
297          *
298          * The only way I can see to determine that is to check how much
299          * padding there is in the first packet - if there's enough
300          * padding for a Shomiti trailer, it's probably a Shomiti
301          * capture, and otherwise, it's probably from Snoop.
302          */
303
304         /*
305          * Start out assuming it's not a Shomiti capture.
306          */
307         is_shomiti = FALSE;
308
309         /* Read first record header. */
310         errno = WTAP_ERR_CANT_READ;
311         bytes_read = file_read(&rec_hdr, 1, sizeof rec_hdr, wth->fh);
312         if (bytes_read != sizeof rec_hdr) {
313                 *err = file_error(wth->fh);
314                 if (*err == 0 && bytes_read != 0)
315                         *err = WTAP_ERR_SHORT_READ;
316                 if (*err != 0) {
317                         /*
318                          * A real-live error.
319                          */
320                         return -1;
321                 }
322
323                 /*
324                  * The file ends after the record header, which means this
325                  * is a capture with no packets.
326                  *
327                  * We assume it's a snoop file; the actual type of file is
328                  * irrelevant, as there are no records in it, and thus no
329                  * extra information if it's a Shomiti capture, and no
330                  * link-layer headers whose type we have to know, and no
331                  * Ethernet frames that might have an FCS.
332                  */
333         } else {
334                 /*
335                  * Compute the number of bytes of padding in the
336                  * record.  If it's at least the size of a Shomiti
337                  * trailer record, we assume this is a Shomiti
338                  * capture.  (Some atmsnoop captures appear
339                  * to have 4 bytes of padding, and at least one
340                  * snoop capture appears to have 6 bytes of padding;
341                  * the Shomiti header is larger than either of those.)
342                  */
343                 if (g_ntohl(rec_hdr.rec_len) >
344                     (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
345                         /*
346                          * Well, we have padding; how much?
347                          */
348                         padbytes = g_ntohl(rec_hdr.rec_len) -
349                             (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
350
351                         /*
352                          * Is it at least the size of a Shomiti trailer?
353                          */
354                         is_shomiti =
355                             (padbytes >= sizeof (struct shomiti_trailer));
356                 }
357         }
358
359         /*
360          * Seek back to the beginning of the first record.
361          */
362         if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1)
363                 return -1;
364
365         hdr.network = g_ntohl(hdr.network);
366         if (is_shomiti) {
367                 if (hdr.network >= NUM_SHOMITI_ENCAPS
368                     || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
369                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
370                         *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
371                             hdr.network);
372                         return -1;
373                 }
374                 file_encap = shomiti_encap[hdr.network];
375
376                 /* This is a Shomiti file */
377                 wth->file_type = WTAP_FILE_SHOMITI;
378         } else {
379                 if (hdr.network >= NUM_SNOOP_ENCAPS
380                     || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
381                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
382                         *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
383                             hdr.network);
384                         return -1;
385                 }
386                 file_encap = snoop_encap[hdr.network];
387
388                 /* This is a snoop file */
389                 wth->file_type = WTAP_FILE_SNOOP;
390         }
391
392         /*
393          * We don't currently use the extra information in Shomiti
394          * records, so we use the same routines to read snoop and
395          * Shomiti files.
396          */
397         wth->subtype_read = snoop_read;
398         wth->subtype_seek_read = snoop_seek_read;
399         wth->file_encap = file_encap;
400         wth->snapshot_length = 0;       /* not available in header */
401         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
402         return 1;
403 }
404
405 /* Read the next packet */
406 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
407     long *data_offset)
408 {
409         guint32 rec_size;
410         guint32 packet_size;
411         guint32 orig_size;
412         int     bytes_read;
413         struct snooprec_hdr hdr;
414         char    padbuf[4];
415         guint   padbytes;
416         int     bytes_to_read;
417
418         /* Read record header. */
419         errno = WTAP_ERR_CANT_READ;
420         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
421         if (bytes_read != sizeof hdr) {
422                 *err = file_error(wth->fh);
423                 if (*err == 0 && bytes_read != 0)
424                         *err = WTAP_ERR_SHORT_READ;
425                 return FALSE;
426         }
427         wth->data_offset += sizeof hdr;
428
429         rec_size = g_ntohl(hdr.rec_len);
430         orig_size = g_ntohl(hdr.orig_len);
431         packet_size = g_ntohl(hdr.incl_len);
432         if (packet_size > WTAP_MAX_PACKET_SIZE) {
433                 /*
434                  * Probably a corrupt capture file; don't blow up trying
435                  * to allocate space for an immensely-large packet.
436                  */
437                 *err = WTAP_ERR_BAD_RECORD;
438                 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
439                     packet_size, WTAP_MAX_PACKET_SIZE);
440                 return FALSE;
441         }
442         if (packet_size > rec_size) {
443                 /*
444                  * Probably a corrupt capture file.
445                  */
446                 *err = WTAP_ERR_BAD_RECORD;
447                 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
448                     packet_size, rec_size);
449                 return FALSE;
450         }
451
452         *data_offset = wth->data_offset;
453
454         /*
455          * If this is an ATM packet, the first four bytes are the
456          * direction of the packet (transmit/receive), the VPI, and
457          * the VCI; read them and generate the pseudo-header from
458          * them.
459          */
460         switch (wth->file_encap) {
461
462         case WTAP_ENCAP_ATM_PDUS:
463                 if (packet_size < sizeof (struct snoop_atm_hdr)) {
464                         /*
465                          * Uh-oh, the packet isn't big enough to even
466                          * have a pseudo-header.
467                          */
468                         *err = WTAP_ERR_BAD_RECORD;
469                         *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
470                             packet_size);
471                         return FALSE;
472                 }
473                 if (!snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
474                     err))
475                         return FALSE;   /* Read error */
476
477                 /*
478                  * Don't count the pseudo-header as part of the packet.
479                  */
480                 rec_size -= sizeof (struct snoop_atm_hdr);
481                 orig_size -= sizeof (struct snoop_atm_hdr);
482                 packet_size -= sizeof (struct snoop_atm_hdr);
483                 wth->data_offset += sizeof (struct snoop_atm_hdr);
484                 break;
485
486         case WTAP_ENCAP_ETHERNET:
487                 /*
488                  * If this is a snoop file, we assume there's no FCS in
489                  * this frame; if this is a Shomit file, we assume there
490                  * is.  (XXX - or should we treat it a "maybe"?)
491                  */
492                 if (wth->file_type == WTAP_FILE_SHOMITI)
493                         wth->pseudo_header.eth.fcs_len = 4;
494                 else
495                         wth->pseudo_header.eth.fcs_len = 0;
496                 break;
497         }
498
499         buffer_assure_space(wth->frame_buffer, packet_size);
500         if (!snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
501             packet_size, err))
502                 return FALSE;   /* Read error */
503         wth->data_offset += packet_size;
504
505         wth->phdr.ts.secs = g_ntohl(hdr.ts_sec);
506         wth->phdr.ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
507         wth->phdr.caplen = packet_size;
508         wth->phdr.len = orig_size;
509
510         /*
511          * If this is ATM LANE traffic, try to guess what type of LANE
512          * traffic it is based on the packet contents.
513          */
514         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
515             wth->pseudo_header.atm.type == TRAF_LANE) {
516                 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
517                     wth->phdr.caplen, &wth->pseudo_header);
518         }
519
520         /*
521          * Skip over the padding (don't "fseek()", as the standard
522          * I/O library on some platforms discards buffered data if
523          * you do that, which means it does a lot more reads).
524          * There's probably not much padding (it's probably padded only
525          * to a 4-byte boundary), so we probably need only do one read.
526          */
527         if (rec_size < (sizeof hdr + packet_size)) {
528                 /*
529                  * What, *negative* padding?  Bogus.
530                  */
531                 *err = WTAP_ERR_BAD_RECORD;
532                 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
533                     rec_size, packet_size);
534                 return FALSE;
535         }
536         padbytes = rec_size - (sizeof hdr + packet_size);
537         while (padbytes != 0) {
538                 bytes_to_read = padbytes;
539                 if ((unsigned)bytes_to_read > sizeof padbuf)
540                         bytes_to_read = sizeof padbuf;
541                 errno = WTAP_ERR_CANT_READ;
542                 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
543                 if (bytes_read != bytes_to_read) {
544                         *err = file_error(wth->fh);
545                         if (*err == 0)
546                                 *err = WTAP_ERR_SHORT_READ;
547                         return FALSE;
548                 }
549                 wth->data_offset += bytes_read;
550                 padbytes -= bytes_read;
551         }
552
553         return TRUE;
554 }
555
556 static gboolean
557 snoop_seek_read(wtap *wth, long seek_off,
558     union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
559     int *err, gchar **err_info _U_)
560 {
561         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
562                 return FALSE;
563
564         switch (wth->file_encap) {
565
566         case WTAP_ENCAP_ATM_PDUS:
567                 if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
568                     err)) {
569                         /* Read error */
570                         return FALSE;
571                 }
572                 break;
573
574         case WTAP_ENCAP_ETHERNET:
575                 /*
576                  * If this is a snoop file, we assume there's no FCS in
577                  * this frame; if this is a Shomit file, we assume there
578                  * is.  (XXX - or should we treat it a "maybe"?)
579                  */
580                 if (wth->file_type == WTAP_FILE_SHOMITI)
581                         pseudo_header->eth.fcs_len = 4;
582                 else
583                         pseudo_header->eth.fcs_len = 0;
584                 break;
585         }
586
587         /*
588          * Read the packet data.
589          */
590         if (!snoop_read_rec_data(wth->random_fh, pd, length, err))
591                 return FALSE;   /* failed */
592
593         /*
594          * If this is ATM LANE traffic, try to guess what type of LANE
595          * traffic it is based on the packet contents.
596          */
597         if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
598             pseudo_header->atm.type == TRAF_LANE)
599                 atm_guess_lane_type(pd, length, pseudo_header);
600         return TRUE;
601 }
602
603 static gboolean
604 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
605     int *err)
606 {
607         struct snoop_atm_hdr atm_phdr;
608         int     bytes_read;
609         guint8  vpi;
610         guint16 vci;
611
612         errno = WTAP_ERR_CANT_READ;
613         bytes_read = file_read(&atm_phdr, 1, sizeof (struct snoop_atm_hdr), fh);
614         if (bytes_read != sizeof (struct snoop_atm_hdr)) {
615                 *err = file_error(fh);
616                 if (*err == 0)
617                         *err = WTAP_ERR_SHORT_READ;
618                 return FALSE;
619         }
620
621         vpi = atm_phdr.vpi;
622         vci = pntohs(&atm_phdr.vci);
623
624         /*
625          * The lower 4 bits of the first byte of the header indicate
626          * the type of traffic, as per the "atmioctl.h" header in
627          * SunATM.
628          */
629         switch (atm_phdr.flags & 0x0F) {
630
631         case 0x01:      /* LANE */
632                 pseudo_header->atm.aal = AAL_5;
633                 pseudo_header->atm.type = TRAF_LANE;
634                 break;
635
636         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
637                 pseudo_header->atm.aal = AAL_5;
638                 pseudo_header->atm.type = TRAF_LLCMX;
639                 break;
640
641         case 0x05:      /* ILMI */
642                 pseudo_header->atm.aal = AAL_5;
643                 pseudo_header->atm.type = TRAF_ILMI;
644                 break;
645
646         case 0x06:      /* Signalling AAL */
647                 pseudo_header->atm.aal = AAL_SIGNALLING;
648                 pseudo_header->atm.type = TRAF_UNKNOWN;
649                 break;
650
651         case 0x03:      /* MARS (RFC 2022) */
652                 pseudo_header->atm.aal = AAL_5;
653                 pseudo_header->atm.type = TRAF_UNKNOWN;
654                 break;
655
656         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
657                 pseudo_header->atm.aal = AAL_5;
658                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
659                 break;
660
661         default:
662                 /*
663                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
664                  * case assume it's AAL_SIGNALLING; we know nothing more
665                  * about it.
666                  *
667                  * XXX - is this necessary?  Or are we guaranteed that
668                  * all signalling traffic has a type of 0x06?
669                  *
670                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
671                  * 0x00 ("raw"), might it be non-AAL5 traffic?
672                  */
673                 if (vpi == 0 && vci == 5)
674                         pseudo_header->atm.aal = AAL_SIGNALLING;
675                 else
676                         pseudo_header->atm.aal = AAL_5;
677                 pseudo_header->atm.type = TRAF_UNKNOWN;
678                 break;
679         }
680         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
681
682         pseudo_header->atm.vpi = vpi;
683         pseudo_header->atm.vci = vci;
684         pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
685
686         /* We don't have this information */
687         pseudo_header->atm.flags = 0;
688         pseudo_header->atm.cells = 0;
689         pseudo_header->atm.aal5t_u2u = 0;
690         pseudo_header->atm.aal5t_len = 0;
691         pseudo_header->atm.aal5t_chksum = 0;
692
693         return TRUE;
694 }
695
696 static gboolean
697 snoop_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
698 {
699         int     bytes_read;
700
701         errno = WTAP_ERR_CANT_READ;
702         bytes_read = file_read(pd, 1, length, fh);
703
704         if (bytes_read != length) {
705                 *err = file_error(fh);
706                 if (*err == 0)
707                         *err = WTAP_ERR_SHORT_READ;
708                 return FALSE;
709         }
710         return TRUE;
711 }
712
713 static const int wtap_encap[] = {
714         -1,             /* WTAP_ENCAP_UNKNOWN -> unsupported */
715         0x04,           /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
716         0x02,           /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
717         -1,             /* WTAP_ENCAP_SLIP -> unsupported */
718         -1,             /* WTAP_ENCAP_PPP -> unsupported */
719         0x08,           /* WTAP_ENCAP_FDDI -> DL_FDDI */
720         0x08,           /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
721         -1,             /* WTAP_ENCAP_RAW_IP -> unsupported */
722         -1,             /* WTAP_ENCAP_ARCNET -> unsupported */
723         -1,             /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
724         -1,             /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
725         -1,             /* WTAP_ENCAP_LAPB -> unsupported*/
726         0x12,           /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
727         -1              /* WTAP_ENCAP_NULL -> unsupported */
728 };
729 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
730
731 /* Returns 0 if we could write the specified encapsulation type,
732    an error indication otherwise. */
733 int snoop_dump_can_write_encap(int encap)
734 {
735         /* Per-packet encapsulations aren't supported. */
736         if (encap == WTAP_ENCAP_PER_PACKET)
737                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
738
739         if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
740                 return WTAP_ERR_UNSUPPORTED_ENCAP;
741
742         return 0;
743 }
744
745 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
746    failure */
747 gboolean snoop_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
748 {
749         struct snoop_hdr file_hdr;
750         size_t nwritten;
751
752         /* This is a snoop file */
753         wdh->subtype_write = snoop_dump;
754         wdh->subtype_close = NULL;
755
756         /* Write the file header. */
757         nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
758         if (nwritten != sizeof snoop_magic) {
759                 if (nwritten == 0 && ferror(wdh->fh))
760                         *err = errno;
761                 else
762                         *err = WTAP_ERR_SHORT_WRITE;
763                 return FALSE;
764         }
765
766         /* current "snoop" format is 2 */
767         file_hdr.version = g_htonl(2);
768         file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
769         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
770         if (nwritten != sizeof file_hdr) {
771                 if (nwritten == 0 && ferror(wdh->fh))
772                         *err = errno;
773                 else
774                         *err = WTAP_ERR_SHORT_WRITE;
775                 return FALSE;
776         }
777
778         return TRUE;
779 }
780
781 /* Write a record for a packet to a dump file.
782    Returns TRUE on success, FALSE on failure. */
783 static gboolean snoop_dump(wtap_dumper *wdh,
784         const struct wtap_pkthdr *phdr,
785         const union wtap_pseudo_header *pseudo_header _U_,
786         const guchar *pd, int *err)
787 {
788         struct snooprec_hdr rec_hdr;
789         size_t nwritten;
790         int reclen;
791         guint padlen;
792         static char zeroes[4];
793         struct snoop_atm_hdr atm_hdr;
794         int atm_hdrsize;
795
796         if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
797                 atm_hdrsize = sizeof (struct snoop_atm_hdr);
798         else
799                 atm_hdrsize = 0;
800
801         /* Record length = header length plus data length... */
802         reclen = sizeof rec_hdr + phdr->caplen + atm_hdrsize;
803
804         /* ... plus enough bytes to pad it to a 4-byte boundary. */
805         padlen = ((reclen + 3) & ~3) - reclen;
806         reclen += padlen;
807
808         rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
809         rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
810         rec_hdr.rec_len = g_htonl(reclen);
811         rec_hdr.cum_drops = 0;
812         rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
813         rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
814         nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
815         if (nwritten != sizeof rec_hdr) {
816                 if (nwritten == 0 && ferror(wdh->fh))
817                         *err = errno;
818                 else
819                         *err = WTAP_ERR_SHORT_WRITE;
820                 return FALSE;
821         }
822
823         if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
824                 /*
825                  * Write the ATM header.
826                  */
827                 atm_hdr.flags =
828                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
829                 switch (pseudo_header->atm.aal) {
830
831                 case AAL_SIGNALLING:
832                         /* Signalling AAL */
833                         atm_hdr.flags |= 0x06;
834                         break;
835
836                 case AAL_5:
837                         switch (pseudo_header->atm.type) {
838
839                         case TRAF_LANE:
840                                 /* LANE */
841                                 atm_hdr.flags |= 0x01;
842                                 break;
843
844                         case TRAF_LLCMX:
845                                 /* RFC 1483 LLC multiplexed traffic */
846                                 atm_hdr.flags |= 0x02;
847                                 break;
848
849                         case TRAF_ILMI:
850                                 /* ILMI */
851                                 atm_hdr.flags |= 0x05;
852                                 break;
853                         }
854                         break;
855                 }
856                 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
857                 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
858                 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
859                 if (nwritten != sizeof atm_hdr) {
860                         if (nwritten == 0 && ferror(wdh->fh))
861                                 *err = errno;
862                         else
863                                 *err = WTAP_ERR_SHORT_WRITE;
864                         return FALSE;
865                 }
866         }
867
868         nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
869         if (nwritten != phdr->caplen) {
870                 if (nwritten == 0 && ferror(wdh->fh))
871                         *err = errno;
872                 else
873                         *err = WTAP_ERR_SHORT_WRITE;
874                 return FALSE;
875         }
876
877         /* Now write the padding. */
878         nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
879         if (nwritten != padlen) {
880                 if (nwritten == 0 && ferror(wdh->fh))
881                         *err = errno;
882                 else
883                         *err = WTAP_ERR_SHORT_WRITE;
884                 return FALSE;
885         }
886         return TRUE;
887 }