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