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