6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
29 #include "file_wrappers.h"
33 /* See RFC 1761 for a description of the "snoop" file format. */
35 /* Magic number in "snoop" files. */
36 static const char snoop_magic[] = {
37 's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
40 /* "snoop" file header (minus magic number). */
42 guint32 version; /* version number (should be 2) */
43 guint32 network; /* network type */
46 /* "snoop" record header. */
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 */
57 * The link-layer header on ATM packets.
59 struct snoop_atm_hdr {
60 guint8 flags; /* destination and traffic type */
62 guint16 vci; /* VCI */
66 * Extra information stuffed into the padding in Shomiti/Finisar Surveyor
69 struct shomiti_trailer {
70 guint16 phy_rx_length; /* length on the wire, including FCS? */
71 guint16 phy_rx_status; /* status flags */
72 guint32 ts_40_ns_lsb; /* 40 ns time stamp, low-order bytes? */
73 guint32 ts_40_ns_msb; /* 40 ns time stamp, low-order bytes? */
74 gint32 frame_id; /* "FrameID"? */
78 * phy_rx_status flags.
80 #define RX_STATUS_OVERFLOW 0x8000 /* overflow error */
81 #define RX_STATUS_BAD_CRC 0x4000 /* CRC error */
82 #define RX_STATUS_DRIBBLE_NIBBLE 0x2000 /* dribble/nibble bits? */
83 #define RX_STATUS_SHORT_FRAME 0x1000 /* frame < 64 bytes */
84 #define RX_STATUS_OVERSIZE_FRAME 0x0800 /* frame > 1518 bytes */
85 #define RX_STATUS_GOOD_FRAME 0x0400 /* frame OK */
86 #define RX_STATUS_N12_BYTES_RECEIVED 0x0200 /* first 12 bytes of frame received? */
87 #define RX_STATUS_RXABORT 0x0100 /* RXABORT during reception */
88 #define RX_STATUS_FIFO_ERROR 0x0080 /* receive FIFO error */
89 #define RX_STATUS_TRIGGERED 0x0001 /* frame did trigger */
91 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
93 static gboolean snoop_seek_read(wtap *wth, long seek_off,
94 union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
95 int *err, gchar **err_info);
96 static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
97 union wtap_pseudo_header *pseudo_header, int *err);
98 static gboolean snoop_read_rec_data(FILE_T fh, guchar *pd, int length,
100 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
101 const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
106 * http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
108 * for the "dlpi.h" header file specified by The Open Group, which lists
109 * the DL_ values for various protocols; Solaris 7 uses the same values.
113 * http://mrpink.lerc.nasa.gov/118x/support.html
115 * had links to modified versions of "tcpdump" and "libpcap" for SUNatm
116 * DLPI support; they suggested that the 3.0 verson of SUNatm uses those
117 * values. The Wayback Machine archived that page, but not the stuff
118 * to which it linked, unfortunately.
120 * It also has a link to "convert.c", which is a program to convert files
121 * from the format written by the "atmsnoop" program that comes with the
122 * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
123 * to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
126 * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
127 * that in an "atmsnoop" file, the header contains 2 bytes (direction and
128 * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
129 * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
130 * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
131 * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
132 * 1 byte of VPI, and 2 bytes of VCI.
134 * The aforementioned page also has a link to some capture files from
135 * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
137 * Source to an "atmdump" package, which includes a modified version of
138 * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
139 * also includes "atmdump", which is a modified "tcpdump", is available
142 * ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
144 * and that code also indicates that DL_IPATM is used, and that an
145 * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
146 * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
147 * and then the ATM PDU, and suggests that the direction flag is 0x80 for
148 * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
149 * "received" (presumably meaning DCE->DTE). That code was used as the
150 * basis for the SunATM support in current CVS versions of libpcap and
151 * tcpdump, and it works.
153 * In fact, the "direction" byte appears to have some other stuff, perhaps
154 * a traffic type, in the lower 7 bits, with the 8th bit indicating the
155 * direction. That appears to be the case.
157 * I don't know what the encapsulation of any of the other types is, so I
158 * leave them all as WTAP_ENCAP_UNKNOWN, except for those for which Brian
159 * Ginsbach has supplied information about the way UNICOS/mp uses them.
160 * I also don't know whether "snoop" can handle any of them (it presumably
161 * can't handle ATM, otherwise Sun wouldn't have supplied "atmsnoop"; even
162 * if it can't, this may be useful reference information for anybody doing
163 * code to use DLPI to do raw packet captures on those network types.
167 * http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
169 * for information on Shomiti's mutant flavor of snoop. For some unknown
170 * unknown reason, they decided not to just Go With The DLPI Flow, and
171 * instead used the types unspecified in RFC 1461 for their own nefarious
172 * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
173 * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
174 * of them from the "Shomiti" versions of same.
176 int snoop_open(wtap *wth, int *err, gchar **err_info)
179 char magic[sizeof snoop_magic];
180 struct snoop_hdr hdr;
181 struct snooprec_hdr rec_hdr;
184 static const int snoop_encap[] = {
185 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
186 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
187 WTAP_ENCAP_TOKEN_RING,
188 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
190 WTAP_ENCAP_UNKNOWN, /* HDLC */
191 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
192 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
193 WTAP_ENCAP_FDDI_BITSWAPPED,
194 WTAP_ENCAP_NULL, /* Other */
195 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
196 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
197 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
198 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
199 WTAP_ENCAP_NULL, /* software loopback */
200 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
201 WTAP_ENCAP_IP_OVER_FC, /* Fibre Channel */
202 WTAP_ENCAP_UNKNOWN, /* ATM */
203 WTAP_ENCAP_ATM_PDUS, /* ATM Classical IP */
204 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
205 WTAP_ENCAP_UNKNOWN, /* ISDN */
206 WTAP_ENCAP_UNKNOWN, /* HIPPI */
207 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
208 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
209 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
210 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
212 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
213 static const int shomiti_encap[] = {
214 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
215 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
216 WTAP_ENCAP_TOKEN_RING,
217 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
219 WTAP_ENCAP_UNKNOWN, /* HDLC */
220 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
221 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
222 WTAP_ENCAP_FDDI_BITSWAPPED,
223 WTAP_ENCAP_UNKNOWN, /* Other */
224 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
225 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
226 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
227 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
228 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
230 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
233 /* Read in the string that should be at the start of a "snoop" file */
234 errno = WTAP_ERR_CANT_READ;
235 bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
236 if (bytes_read != sizeof magic) {
237 *err = file_error(wth->fh);
242 wth->data_offset += sizeof magic;
244 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
248 /* Read the rest of the header. */
249 errno = WTAP_ERR_CANT_READ;
250 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
251 if (bytes_read != sizeof hdr) {
252 *err = file_error(wth->fh);
257 wth->data_offset += sizeof hdr;
260 * Make sure it's a version we support.
262 hdr.version = g_ntohl(hdr.version);
263 switch (hdr.version) {
265 case 2: /* Solaris 2.x and later snoop, and Shomiti
266 Surveyor prior to 3.0, or 3.0 and later
268 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
269 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
270 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
274 *err = WTAP_ERR_UNSUPPORTED;
275 *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
280 * Oh, this is lovely.
282 * I suppose Shomiti could give a bunch of lawyerly noise about
283 * how "well, RFC 1761 said they were unassigned, and that's
284 * the standard, not the DLPI header file, so it's perfectly OK
285 * for us to use them, blah blah blah", but it's still irritating
286 * as hell that they used the unassigned-in-RFC-1761 values for
287 * their own purposes - especially given that Sun also used
288 * one of them in atmsnoop.
290 * We can't determine whether it's a Shomiti capture based on
291 * the version number, as, according to their documentation on
292 * their capture file format, Shomiti uses a version number of 2
293 * if the data "was captured using an NDIS card", which presumably
294 * means "captured with an ordinary boring network card via NDIS"
295 * as opposed to "captured with our whizzo special capture
298 * The only way I can see to determine that is to check how much
299 * padding there is in the first packet - if there's enough
300 * padding for a Shomiti trailer, it's probably a Shomiti
301 * capture, and otherwise, it's probably from Snoop.
305 * Start out assuming it's not a Shomiti capture.
309 /* Read first record header. */
310 errno = WTAP_ERR_CANT_READ;
311 bytes_read = file_read(&rec_hdr, 1, sizeof rec_hdr, wth->fh);
312 if (bytes_read != sizeof rec_hdr) {
313 *err = file_error(wth->fh);
314 if (*err == 0 && bytes_read != 0)
315 *err = WTAP_ERR_SHORT_READ;
324 * The file ends after the record header, which means this
325 * is a capture with no packets.
327 * We assume it's a snoop file; the actual type of file is
328 * irrelevant, as there are no records in it, and thus no
329 * extra information if it's a Shomiti capture, and no
330 * link-layer headers whose type we have to know, and no
331 * Ethernet frames that might have an FCS.
335 * Compute the number of bytes of padding in the
336 * record. If it's at least the size of a Shomiti
337 * trailer record, we assume this is a Shomiti
338 * capture. (Some atmsnoop captures appear
339 * to have 4 bytes of padding, and at least one
340 * snoop capture appears to have 6 bytes of padding;
341 * the Shomiti header is larger than either of those.)
343 if (g_ntohl(rec_hdr.rec_len) >
344 (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
346 * Well, we have padding; how much?
348 padbytes = g_ntohl(rec_hdr.rec_len) -
349 (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
352 * Is it at least the size of a Shomiti trailer?
355 (padbytes >= sizeof (struct shomiti_trailer));
360 * Seek back to the beginning of the first record.
362 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1)
365 hdr.network = g_ntohl(hdr.network);
367 if (hdr.network >= NUM_SHOMITI_ENCAPS
368 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
369 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
370 *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
374 file_encap = shomiti_encap[hdr.network];
376 /* This is a Shomiti file */
377 wth->file_type = WTAP_FILE_SHOMITI;
379 if (hdr.network >= NUM_SNOOP_ENCAPS
380 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
381 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
382 *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
386 file_encap = snoop_encap[hdr.network];
388 /* This is a snoop file */
389 wth->file_type = WTAP_FILE_SNOOP;
393 * We don't currently use the extra information in Shomiti
394 * records, so we use the same routines to read snoop and
397 wth->subtype_read = snoop_read;
398 wth->subtype_seek_read = snoop_seek_read;
399 wth->file_encap = file_encap;
400 wth->snapshot_length = 0; /* not available in header */
401 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
405 /* Read the next packet */
406 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
413 struct snooprec_hdr hdr;
418 /* Read record header. */
419 errno = WTAP_ERR_CANT_READ;
420 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
421 if (bytes_read != sizeof hdr) {
422 *err = file_error(wth->fh);
423 if (*err == 0 && bytes_read != 0)
424 *err = WTAP_ERR_SHORT_READ;
427 wth->data_offset += sizeof hdr;
429 rec_size = g_ntohl(hdr.rec_len);
430 orig_size = g_ntohl(hdr.orig_len);
431 packet_size = g_ntohl(hdr.incl_len);
432 if (packet_size > WTAP_MAX_PACKET_SIZE) {
434 * Probably a corrupt capture file; don't blow up trying
435 * to allocate space for an immensely-large packet.
437 *err = WTAP_ERR_BAD_RECORD;
438 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
439 packet_size, WTAP_MAX_PACKET_SIZE);
442 if (packet_size > rec_size) {
444 * Probably a corrupt capture file.
446 *err = WTAP_ERR_BAD_RECORD;
447 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
448 packet_size, rec_size);
452 *data_offset = wth->data_offset;
455 * If this is an ATM packet, the first four bytes are the
456 * direction of the packet (transmit/receive), the VPI, and
457 * the VCI; read them and generate the pseudo-header from
460 switch (wth->file_encap) {
462 case WTAP_ENCAP_ATM_PDUS:
463 if (packet_size < sizeof (struct snoop_atm_hdr)) {
465 * Uh-oh, the packet isn't big enough to even
466 * have a pseudo-header.
468 *err = WTAP_ERR_BAD_RECORD;
469 *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
473 if (!snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
475 return FALSE; /* Read error */
478 * Don't count the pseudo-header as part of the packet.
480 rec_size -= sizeof (struct snoop_atm_hdr);
481 orig_size -= sizeof (struct snoop_atm_hdr);
482 packet_size -= sizeof (struct snoop_atm_hdr);
483 wth->data_offset += sizeof (struct snoop_atm_hdr);
486 case WTAP_ENCAP_ETHERNET:
488 * If this is a snoop file, we assume there's no FCS in
489 * this frame; if this is a Shomit file, we assume there
490 * is. (XXX - or should we treat it a "maybe"?)
492 if (wth->file_type == WTAP_FILE_SHOMITI)
493 wth->pseudo_header.eth.fcs_len = 4;
495 wth->pseudo_header.eth.fcs_len = 0;
499 buffer_assure_space(wth->frame_buffer, packet_size);
500 if (!snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
502 return FALSE; /* Read error */
503 wth->data_offset += packet_size;
505 wth->phdr.ts.secs = g_ntohl(hdr.ts_sec);
506 wth->phdr.ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
507 wth->phdr.caplen = packet_size;
508 wth->phdr.len = orig_size;
511 * If this is ATM LANE traffic, try to guess what type of LANE
512 * traffic it is based on the packet contents.
514 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
515 wth->pseudo_header.atm.type == TRAF_LANE) {
516 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
517 wth->phdr.caplen, &wth->pseudo_header);
521 * Skip over the padding (don't "fseek()", as the standard
522 * I/O library on some platforms discards buffered data if
523 * you do that, which means it does a lot more reads).
524 * There's probably not much padding (it's probably padded only
525 * to a 4-byte boundary), so we probably need only do one read.
527 if (rec_size < (sizeof hdr + packet_size)) {
529 * What, *negative* padding? Bogus.
531 *err = WTAP_ERR_BAD_RECORD;
532 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
533 rec_size, packet_size);
536 padbytes = rec_size - (sizeof hdr + packet_size);
537 while (padbytes != 0) {
538 bytes_to_read = padbytes;
539 if ((unsigned)bytes_to_read > sizeof padbuf)
540 bytes_to_read = sizeof padbuf;
541 errno = WTAP_ERR_CANT_READ;
542 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
543 if (bytes_read != bytes_to_read) {
544 *err = file_error(wth->fh);
546 *err = WTAP_ERR_SHORT_READ;
549 wth->data_offset += bytes_read;
550 padbytes -= bytes_read;
557 snoop_seek_read(wtap *wth, long seek_off,
558 union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
559 int *err, gchar **err_info _U_)
561 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
564 switch (wth->file_encap) {
566 case WTAP_ENCAP_ATM_PDUS:
567 if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
574 case WTAP_ENCAP_ETHERNET:
576 * If this is a snoop file, we assume there's no FCS in
577 * this frame; if this is a Shomit file, we assume there
578 * is. (XXX - or should we treat it a "maybe"?)
580 if (wth->file_type == WTAP_FILE_SHOMITI)
581 pseudo_header->eth.fcs_len = 4;
583 pseudo_header->eth.fcs_len = 0;
588 * Read the packet data.
590 if (!snoop_read_rec_data(wth->random_fh, pd, length, err))
591 return FALSE; /* failed */
594 * If this is ATM LANE traffic, try to guess what type of LANE
595 * traffic it is based on the packet contents.
597 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
598 pseudo_header->atm.type == TRAF_LANE)
599 atm_guess_lane_type(pd, length, pseudo_header);
604 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
607 struct snoop_atm_hdr atm_phdr;
612 errno = WTAP_ERR_CANT_READ;
613 bytes_read = file_read(&atm_phdr, 1, sizeof (struct snoop_atm_hdr), fh);
614 if (bytes_read != sizeof (struct snoop_atm_hdr)) {
615 *err = file_error(fh);
617 *err = WTAP_ERR_SHORT_READ;
622 vci = pntohs(&atm_phdr.vci);
625 * The lower 4 bits of the first byte of the header indicate
626 * the type of traffic, as per the "atmioctl.h" header in
629 switch (atm_phdr.flags & 0x0F) {
631 case 0x01: /* LANE */
632 pseudo_header->atm.aal = AAL_5;
633 pseudo_header->atm.type = TRAF_LANE;
636 case 0x02: /* RFC 1483 LLC multiplexed traffic */
637 pseudo_header->atm.aal = AAL_5;
638 pseudo_header->atm.type = TRAF_LLCMX;
641 case 0x05: /* ILMI */
642 pseudo_header->atm.aal = AAL_5;
643 pseudo_header->atm.type = TRAF_ILMI;
646 case 0x06: /* Signalling AAL */
647 pseudo_header->atm.aal = AAL_SIGNALLING;
648 pseudo_header->atm.type = TRAF_UNKNOWN;
651 case 0x03: /* MARS (RFC 2022) */
652 pseudo_header->atm.aal = AAL_5;
653 pseudo_header->atm.type = TRAF_UNKNOWN;
656 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
657 pseudo_header->atm.aal = AAL_5;
658 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
663 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
664 * case assume it's AAL_SIGNALLING; we know nothing more
667 * XXX - is this necessary? Or are we guaranteed that
668 * all signalling traffic has a type of 0x06?
670 * XXX - is this guaranteed to be AAL5? Or, if the type is
671 * 0x00 ("raw"), might it be non-AAL5 traffic?
673 if (vpi == 0 && vci == 5)
674 pseudo_header->atm.aal = AAL_SIGNALLING;
676 pseudo_header->atm.aal = AAL_5;
677 pseudo_header->atm.type = TRAF_UNKNOWN;
680 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
682 pseudo_header->atm.vpi = vpi;
683 pseudo_header->atm.vci = vci;
684 pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
686 /* We don't have this information */
687 pseudo_header->atm.flags = 0;
688 pseudo_header->atm.cells = 0;
689 pseudo_header->atm.aal5t_u2u = 0;
690 pseudo_header->atm.aal5t_len = 0;
691 pseudo_header->atm.aal5t_chksum = 0;
697 snoop_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
701 errno = WTAP_ERR_CANT_READ;
702 bytes_read = file_read(pd, 1, length, fh);
704 if (bytes_read != length) {
705 *err = file_error(fh);
707 *err = WTAP_ERR_SHORT_READ;
713 static const int wtap_encap[] = {
714 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
715 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
716 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
717 -1, /* WTAP_ENCAP_SLIP -> unsupported */
718 -1, /* WTAP_ENCAP_PPP -> unsupported */
719 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
720 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
721 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
722 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
723 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
724 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
725 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
726 0x12, /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
727 -1 /* WTAP_ENCAP_NULL -> unsupported */
729 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
731 /* Returns 0 if we could write the specified encapsulation type,
732 an error indication otherwise. */
733 int snoop_dump_can_write_encap(int encap)
735 /* Per-packet encapsulations aren't supported. */
736 if (encap == WTAP_ENCAP_PER_PACKET)
737 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
739 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
740 return WTAP_ERR_UNSUPPORTED_ENCAP;
745 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
747 gboolean snoop_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
749 struct snoop_hdr file_hdr;
752 /* This is a snoop file */
753 wdh->subtype_write = snoop_dump;
754 wdh->subtype_close = NULL;
756 /* Write the file header. */
757 nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
758 if (nwritten != sizeof snoop_magic) {
759 if (nwritten == 0 && ferror(wdh->fh))
762 *err = WTAP_ERR_SHORT_WRITE;
766 /* current "snoop" format is 2 */
767 file_hdr.version = g_htonl(2);
768 file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
769 nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
770 if (nwritten != sizeof file_hdr) {
771 if (nwritten == 0 && ferror(wdh->fh))
774 *err = WTAP_ERR_SHORT_WRITE;
781 /* Write a record for a packet to a dump file.
782 Returns TRUE on success, FALSE on failure. */
783 static gboolean snoop_dump(wtap_dumper *wdh,
784 const struct wtap_pkthdr *phdr,
785 const union wtap_pseudo_header *pseudo_header _U_,
786 const guchar *pd, int *err)
788 struct snooprec_hdr rec_hdr;
792 static char zeroes[4];
793 struct snoop_atm_hdr atm_hdr;
796 if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
797 atm_hdrsize = sizeof (struct snoop_atm_hdr);
801 /* Record length = header length plus data length... */
802 reclen = sizeof rec_hdr + phdr->caplen + atm_hdrsize;
804 /* ... plus enough bytes to pad it to a 4-byte boundary. */
805 padlen = ((reclen + 3) & ~3) - reclen;
808 rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
809 rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
810 rec_hdr.rec_len = g_htonl(reclen);
811 rec_hdr.cum_drops = 0;
812 rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
813 rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
814 nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
815 if (nwritten != sizeof rec_hdr) {
816 if (nwritten == 0 && ferror(wdh->fh))
819 *err = WTAP_ERR_SHORT_WRITE;
823 if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
825 * Write the ATM header.
828 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
829 switch (pseudo_header->atm.aal) {
833 atm_hdr.flags |= 0x06;
837 switch (pseudo_header->atm.type) {
841 atm_hdr.flags |= 0x01;
845 /* RFC 1483 LLC multiplexed traffic */
846 atm_hdr.flags |= 0x02;
851 atm_hdr.flags |= 0x05;
856 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
857 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
858 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
859 if (nwritten != sizeof atm_hdr) {
860 if (nwritten == 0 && ferror(wdh->fh))
863 *err = WTAP_ERR_SHORT_WRITE;
868 nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
869 if (nwritten != phdr->caplen) {
870 if (nwritten == 0 && ferror(wdh->fh))
873 *err = WTAP_ERR_SHORT_WRITE;
877 /* Now write the padding. */
878 nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
879 if (nwritten != padlen) {
880 if (nwritten == 0 && ferror(wdh->fh))
883 *err = WTAP_ERR_SHORT_WRITE;