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