4 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "file_wrappers.h"
26 #include <wsutil/buffer.h>
29 /* See RFC 1761 for a description of the "snoop" file format. */
31 /* Magic number in "snoop" files. */
32 static const char snoop_magic[] = {
33 's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
36 /* "snoop" file header (minus magic number). */
38 guint32 version; /* version number (should be 2) */
39 guint32 network; /* network type */
42 /* "snoop" record header. */
44 guint32 orig_len; /* actual length of packet */
45 guint32 incl_len; /* number of octets captured in file */
46 guint32 rec_len; /* length of record */
47 guint32 cum_drops; /* cumulative number of dropped packets */
48 guint32 ts_sec; /* timestamp seconds */
49 guint32 ts_usec; /* timestamp microseconds */
53 * The link-layer header on ATM packets.
55 struct snoop_atm_hdr {
56 guint8 flags; /* destination and traffic type */
58 guint16 vci; /* VCI */
62 * Extra information stuffed into the padding in Shomiti/Finisar Surveyor
65 struct shomiti_trailer {
66 guint16 phy_rx_length; /* length on the wire, including FCS? */
67 guint16 phy_rx_status; /* status flags */
68 guint32 ts_40_ns_lsb; /* 40 ns time stamp, low-order bytes? */
69 guint32 ts_40_ns_msb; /* 40 ns time stamp, low-order bytes? */
70 gint32 frame_id; /* "FrameID"? */
74 * phy_rx_status flags.
76 #define RX_STATUS_OVERFLOW 0x8000 /* overflow error */
77 #define RX_STATUS_BAD_CRC 0x4000 /* CRC error */
78 #define RX_STATUS_DRIBBLE_NIBBLE 0x2000 /* dribble/nibble bits? */
79 #define RX_STATUS_SHORT_FRAME 0x1000 /* frame < 64 bytes */
80 #define RX_STATUS_OVERSIZE_FRAME 0x0800 /* frame > 1518 bytes */
81 #define RX_STATUS_GOOD_FRAME 0x0400 /* frame OK */
82 #define RX_STATUS_N12_BYTES_RECEIVED 0x0200 /* first 12 bytes of frame received? */
83 #define RX_STATUS_RXABORT 0x0100 /* RXABORT during reception */
84 #define RX_STATUS_FIFO_ERROR 0x0080 /* receive FIFO error */
85 #define RX_STATUS_TRIGGERED 0x0001 /* frame did trigger */
87 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
89 static gboolean snoop_seek_read(wtap *wth, gint64 seek_off,
90 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
91 static int snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
92 Buffer *buf, int *err, gchar **err_info);
93 static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
94 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
95 static gboolean snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
96 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
98 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
99 const guint8 *pd, int *err);
104 * http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
106 * for the "dlpi.h" header file specified by The Open Group, which lists
107 * the DL_ values for various protocols; Solaris 7 uses the same values.
111 * http://www.iana.org/assignments/snoop-datalink-types/snoop-datalink-types.xml
113 * for the IETF list of snoop datalink types.
117 * http://mrpink.lerc.nasa.gov/118x/support.html
119 * had links to modified versions of "tcpdump" and "libpcap" for SUNatm
120 * DLPI support; they suggested that the 3.0 verson of SUNatm uses those
121 * values. The Wayback Machine archived that page, but not the stuff
122 * to which it linked, unfortunately.
124 * It also has a link to "convert.c", which is a program to convert files
125 * from the format written by the "atmsnoop" program that comes with the
126 * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
127 * to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
130 * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
131 * that in an "atmsnoop" file, the header contains 2 bytes (direction and
132 * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
133 * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
134 * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
135 * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
136 * 1 byte of VPI, and 2 bytes of VCI.
138 * The aforementioned page also has a link to some capture files from
139 * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
141 * Source to an "atmdump" package, which includes a modified version of
142 * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
143 * also includes "atmdump", which is a modified "tcpdump", was available
146 * ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
148 * (the host name is no longer valid) and that code also indicated that
149 * DL_IPATM is used, and that an ATM packet handed up from the Sun driver
150 * for the Sun SBus ATM card on Solaris 2.5.1 has 1 byte of direction,
151 * 1 byte of VPI, 2 bytes of VCI, and then the ATM PDU, and suggests that
152 * the direction flag is 0x80 for "transmitted" (presumably meaning
153 * DTE->DCE) and presumably not 0x80 for "received" (presumably meaning
154 * DCE->DTE). That code was used as the basis for the SunATM support in
155 * later versions of libpcap and tcpdump, and it worked at the time the
156 * development was done with the SunATM code on the system on which the
157 * development was done.
159 * In fact, the "direction" byte appears to have some other stuff, perhaps
160 * a traffic type, in the lower 7 bits, with the 8th bit indicating the
161 * direction. That appears to be the case.
163 * I don't know what the encapsulation of any of the other types is, so I
164 * leave them all as WTAP_ENCAP_UNKNOWN, except for those for which Brian
165 * Ginsbach has supplied information about the way UNICOS/mp uses them.
166 * I also don't know whether "snoop" can handle any of them (it presumably
167 * can't handle ATM, otherwise Sun wouldn't have supplied "atmsnoop"; even
168 * if it can't, this may be useful reference information for anybody doing
169 * code to use DLPI to do raw packet captures on those network types.
171 * http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
173 * gives information on Shomiti's mutant flavor of snoop. For some unknown
174 * reason, they decided not to just Go With The DLPI Flow, and instead used
175 * the types unspecified in RFC 1461 for their own nefarious purposes, such
176 * as distinguishing 10MB from 100MB from 1000MB Ethernet and distinguishing
177 * 4MB from 16MB Token Ring, and distinguishing both of them from the
178 * "Shomiti" versions of same.
180 int snoop_open(wtap *wth, int *err, gchar **err_info)
183 char magic[sizeof snoop_magic];
184 struct snoop_hdr hdr;
185 struct snooprec_hdr rec_hdr;
188 static const int snoop_encap[] = {
189 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
190 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
191 WTAP_ENCAP_TOKEN_RING,
192 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
194 WTAP_ENCAP_UNKNOWN, /* HDLC */
195 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
196 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
197 WTAP_ENCAP_FDDI_BITSWAPPED,
198 WTAP_ENCAP_NULL, /* Other */
199 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
200 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
201 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
202 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
203 WTAP_ENCAP_NULL, /* software loopback */
204 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
205 WTAP_ENCAP_IP_OVER_FC, /* Fibre Channel */
206 WTAP_ENCAP_UNKNOWN, /* ATM */
207 WTAP_ENCAP_ATM_PDUS, /* ATM Classical IP */
208 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
209 WTAP_ENCAP_UNKNOWN, /* ISDN */
210 WTAP_ENCAP_UNKNOWN, /* HIPPI */
211 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
212 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
213 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
214 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
215 WTAP_ENCAP_IP_OVER_IB, /* Infiniband */
217 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
218 #define SNOOP_PRIVATE_BIT 0x80000000
219 static const int snoop_private_encap[] = {
220 WTAP_ENCAP_UNKNOWN, /* Not Used */
221 WTAP_ENCAP_UNKNOWN, /* IPv4 Tunnel Link */
222 WTAP_ENCAP_UNKNOWN, /* IPv6 Tunnel Link */
223 WTAP_ENCAP_UNKNOWN, /* Virtual network interface */
224 WTAP_ENCAP_UNKNOWN, /* IEEE 802.11 */
225 WTAP_ENCAP_IPNET, /* ipnet(7D) link */
226 WTAP_ENCAP_UNKNOWN, /* IPMP stub interface */
227 WTAP_ENCAP_UNKNOWN, /* 6to4 Tunnel Link */
229 #define NUM_SNOOP_PRIVATE_ENCAPS (sizeof snoop_private_encap / sizeof snoop_private_encap[0])
230 static const int shomiti_encap[] = {
231 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
232 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
233 WTAP_ENCAP_TOKEN_RING,
234 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
236 WTAP_ENCAP_UNKNOWN, /* HDLC */
237 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
238 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
239 WTAP_ENCAP_FDDI_BITSWAPPED,
240 WTAP_ENCAP_UNKNOWN, /* Other */
241 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
242 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
243 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
244 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
245 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
246 WTAP_ENCAP_UNKNOWN, /* Other */
247 WTAP_ENCAP_UNKNOWN, /* Other */
248 WTAP_ENCAP_UNKNOWN, /* Other */
249 WTAP_ENCAP_IEEE_802_11_WITH_RADIO, /* IEEE 802.11 with Radio Header */
250 WTAP_ENCAP_ETHERNET, /* 10 Gigabit Ethernet */
252 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
256 /* Read in the string that should be at the start of a "snoop" file */
257 errno = WTAP_ERR_CANT_READ;
258 bytes_read = file_read(magic, sizeof magic, wth->fh);
259 if (bytes_read != sizeof magic) {
260 *err = file_error(wth->fh, err_info);
261 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
266 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
270 /* Read the rest of the header. */
271 errno = WTAP_ERR_CANT_READ;
272 bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
273 if (bytes_read != sizeof hdr) {
274 *err = file_error(wth->fh, err_info);
276 *err = WTAP_ERR_SHORT_READ;
281 * Make sure it's a version we support.
283 hdr.version = g_ntohl(hdr.version);
284 switch (hdr.version) {
286 case 2: /* Solaris 2.x and later snoop, and Shomiti
287 Surveyor prior to 3.0, or 3.0 and later
289 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
290 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
291 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
295 *err = WTAP_ERR_UNSUPPORTED;
296 *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
301 * Oh, this is lovely.
303 * I suppose Shomiti could give a bunch of lawyerly noise about
304 * how "well, RFC 1761 said they were unassigned, and that's
305 * the standard, not the DLPI header file, so it's perfectly OK
306 * for us to use them, blah blah blah", but it's still irritating
307 * as hell that they used the unassigned-in-RFC-1761 values for
308 * their own purposes - especially given that Sun also used
309 * one of them in atmsnoop.
311 * We can't determine whether it's a Shomiti capture based on
312 * the version number, as, according to their documentation on
313 * their capture file format, Shomiti uses a version number of 2
314 * if the data "was captured using an NDIS card", which presumably
315 * means "captured with an ordinary boring network card via NDIS"
316 * as opposed to "captured with our whizzo special capture
319 * The only way I can see to determine that is to check how much
320 * padding there is in the first packet - if there's enough
321 * padding for a Shomiti trailer, it's probably a Shomiti
322 * capture, and otherwise, it's probably from Snoop.
326 * Start out assuming it's not a Shomiti capture.
330 /* Read first record header. */
331 saved_offset = file_tell(wth->fh);
332 errno = WTAP_ERR_CANT_READ;
333 bytes_read = file_read(&rec_hdr, sizeof rec_hdr, wth->fh);
334 if (bytes_read != sizeof rec_hdr) {
335 *err = file_error(wth->fh, err_info);
337 *err = WTAP_ERR_SHORT_READ;
341 * The file ends after the record header, which means this
342 * is a capture with no packets.
344 * We assume it's a snoop file; the actual type of file is
345 * irrelevant, as there are no records in it, and thus no
346 * extra information if it's a Shomiti capture, and no
347 * link-layer headers whose type we have to know, and no
348 * Ethernet frames that might have an FCS.
352 * Compute the number of bytes of padding in the
353 * record. If it's at least the size of a Shomiti
354 * trailer record, we assume this is a Shomiti
355 * capture. (Some atmsnoop captures appear
356 * to have 4 bytes of padding, and at least one
357 * snoop capture appears to have 6 bytes of padding;
358 * the Shomiti header is larger than either of those.)
360 if (g_ntohl(rec_hdr.rec_len) >
361 (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
363 * Well, we have padding; how much?
365 padbytes = g_ntohl(rec_hdr.rec_len) -
366 ((guint)sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
369 * Is it at least the size of a Shomiti trailer?
372 (padbytes >= sizeof (struct shomiti_trailer));
377 * Seek back to the beginning of the first record.
379 if (file_seek(wth->fh, saved_offset, SEEK_SET, err) == -1)
382 hdr.network = g_ntohl(hdr.network);
384 if (hdr.network >= NUM_SHOMITI_ENCAPS
385 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
386 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
387 *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
391 file_encap = shomiti_encap[hdr.network];
393 /* This is a Shomiti file */
394 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SHOMITI;
395 } else if (hdr.network & SNOOP_PRIVATE_BIT) {
396 if ((hdr.network^SNOOP_PRIVATE_BIT) >= NUM_SNOOP_PRIVATE_ENCAPS
397 || snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT] == WTAP_ENCAP_UNKNOWN) {
398 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
399 *err_info = g_strdup_printf("snoop: private network type %u unknown or unsupported",
403 file_encap = snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT];
405 /* This is a snoop file */
406 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
408 if (hdr.network >= NUM_SNOOP_ENCAPS
409 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
410 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
411 *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
415 file_encap = snoop_encap[hdr.network];
417 /* This is a snoop file */
418 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
422 * We don't currently use the extra information in Shomiti
423 * records, so we use the same routines to read snoop and
426 wth->subtype_read = snoop_read;
427 wth->subtype_seek_read = snoop_seek_read;
428 wth->file_encap = file_encap;
429 wth->snapshot_length = 0; /* not available in header */
430 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
443 } shomiti_wireless_header;
446 /* Read the next packet */
447 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
455 *data_offset = file_tell(wth->fh);
457 padbytes = snoop_read_packet(wth, wth->fh, &wth->phdr,
458 wth->frame_buffer, err, err_info);
463 * Skip over the padding (don't "fseek()", as the standard
464 * I/O library on some platforms discards buffered data if
465 * you do that, which means it does a lot more reads).
467 * XXX - is that still true?
469 * There's probably not much padding (it's probably padded only
470 * to a 4-byte boundary), so we probably need only do one read.
472 while (padbytes != 0) {
473 bytes_to_read = padbytes;
474 if ((unsigned)bytes_to_read > sizeof padbuf)
475 bytes_to_read = sizeof padbuf;
476 errno = WTAP_ERR_CANT_READ;
477 bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
478 if (bytes_read != bytes_to_read) {
479 *err = file_error(wth->fh, err_info);
481 *err = WTAP_ERR_SHORT_READ;
484 padbytes -= bytes_read;
491 snoop_seek_read(wtap *wth, gint64 seek_off,
492 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
494 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
497 if (snoop_read_packet(wth, wth->random_fh, phdr, buf, err, err_info) == -1) {
499 *err = WTAP_ERR_SHORT_READ;
506 snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
507 Buffer *buf, int *err, gchar **err_info)
509 struct snooprec_hdr hdr;
516 /* Read record header. */
517 errno = WTAP_ERR_CANT_READ;
518 bytes_read = file_read(&hdr, sizeof hdr, fh);
519 if (bytes_read != sizeof hdr) {
520 *err = file_error(fh, err_info);
521 if (*err == 0 && bytes_read != 0)
522 *err = WTAP_ERR_SHORT_READ;
526 rec_size = g_ntohl(hdr.rec_len);
527 orig_size = g_ntohl(hdr.orig_len);
528 packet_size = g_ntohl(hdr.incl_len);
529 if (orig_size > WTAP_MAX_PACKET_SIZE) {
531 * Probably a corrupt capture file; don't blow up trying
532 * to allocate space for an immensely-large packet.
534 *err = WTAP_ERR_BAD_FILE;
535 *err_info = g_strdup_printf("snoop: File has %u-byte original length, bigger than maximum of %u",
536 orig_size, WTAP_MAX_PACKET_SIZE);
539 if (packet_size > WTAP_MAX_PACKET_SIZE) {
541 * Probably a corrupt capture file; don't blow up trying
542 * to allocate space for an immensely-large packet.
544 *err = WTAP_ERR_BAD_FILE;
545 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
546 packet_size, WTAP_MAX_PACKET_SIZE);
549 if (packet_size > rec_size) {
551 * Probably a corrupt capture file.
553 *err = WTAP_ERR_BAD_FILE;
554 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
555 packet_size, rec_size);
559 switch (wth->file_encap) {
561 case WTAP_ENCAP_ATM_PDUS:
563 * This is an ATM packet, so the first four bytes are
564 * the direction of the packet (transmit/receive), the
565 * VPI, and the VCI; read them and generate the
566 * pseudo-header from them.
568 if (packet_size < sizeof (struct snoop_atm_hdr)) {
570 * Uh-oh, the packet isn't big enough to even
571 * have a pseudo-header.
573 *err = WTAP_ERR_BAD_FILE;
574 *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header",
578 if (!snoop_read_atm_pseudoheader(fh, &phdr->pseudo_header,
580 return -1; /* Read error */
583 * Don't count the pseudo-header as part of the packet.
585 rec_size -= (guint32)sizeof (struct snoop_atm_hdr);
586 orig_size -= (guint32)sizeof (struct snoop_atm_hdr);
587 packet_size -= (guint32)sizeof (struct snoop_atm_hdr);
590 case WTAP_ENCAP_ETHERNET:
592 * If this is a snoop file, we assume there's no FCS in
593 * this frame; if this is a Shomit file, we assume there
594 * is. (XXX - or should we treat it a "maybe"?)
596 if (wth->file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_SHOMITI)
597 phdr->pseudo_header.eth.fcs_len = 4;
599 phdr->pseudo_header.eth.fcs_len = 0;
602 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
603 if (packet_size < sizeof (shomiti_wireless_header)) {
605 * Uh-oh, the packet isn't big enough to even
606 * have a pseudo-header.
608 *err = WTAP_ERR_BAD_FILE;
609 *err_info = g_strdup_printf("snoop: Shomiti wireless file has a %u-byte packet, too small to have even a wireless pseudo-header",
613 if (!snoop_read_shomiti_wireless_pseudoheader(fh,
614 &phdr->pseudo_header, err, err_info, &header_size))
615 return -1; /* Read error */
618 * Don't count the pseudo-header as part of the packet.
620 rec_size -= header_size;
621 orig_size -= header_size;
622 packet_size -= header_size;
626 phdr->rec_type = REC_TYPE_PACKET;
627 phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
628 phdr->ts.secs = g_ntohl(hdr.ts_sec);
629 phdr->ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
630 phdr->caplen = packet_size;
631 phdr->len = orig_size;
633 if (rec_size < (sizeof hdr + packet_size)) {
635 * What, *negative* padding? Bogus.
637 *err = WTAP_ERR_BAD_FILE;
638 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
639 rec_size, packet_size);
644 * Read the packet data.
646 if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
647 return -1; /* failed */
650 * If this is ATM LANE traffic, try to guess what type of LANE
651 * traffic it is based on the packet contents.
653 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
654 phdr->pseudo_header.atm.type == TRAF_LANE) {
655 atm_guess_lane_type(phdr, ws_buffer_start_ptr(buf));
658 return rec_size - ((guint)sizeof hdr + packet_size);
662 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
663 int *err, gchar **err_info)
665 struct snoop_atm_hdr atm_phdr;
670 errno = WTAP_ERR_CANT_READ;
671 bytes_read = file_read(&atm_phdr, sizeof (struct snoop_atm_hdr), fh);
672 if (bytes_read != sizeof (struct snoop_atm_hdr)) {
673 *err = file_error(fh, err_info);
675 *err = WTAP_ERR_SHORT_READ;
680 vci = pntoh16(&atm_phdr.vci);
683 * The lower 4 bits of the first byte of the header indicate
684 * the type of traffic, as per the "atmioctl.h" header in
687 switch (atm_phdr.flags & 0x0F) {
689 case 0x01: /* LANE */
690 pseudo_header->atm.aal = AAL_5;
691 pseudo_header->atm.type = TRAF_LANE;
694 case 0x02: /* RFC 1483 LLC multiplexed traffic */
695 pseudo_header->atm.aal = AAL_5;
696 pseudo_header->atm.type = TRAF_LLCMX;
699 case 0x05: /* ILMI */
700 pseudo_header->atm.aal = AAL_5;
701 pseudo_header->atm.type = TRAF_ILMI;
704 case 0x06: /* Signalling AAL */
705 pseudo_header->atm.aal = AAL_SIGNALLING;
706 pseudo_header->atm.type = TRAF_UNKNOWN;
709 case 0x03: /* MARS (RFC 2022) */
710 pseudo_header->atm.aal = AAL_5;
711 pseudo_header->atm.type = TRAF_UNKNOWN;
714 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
715 pseudo_header->atm.aal = AAL_5;
716 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
721 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
722 * case assume it's AAL_SIGNALLING; we know nothing more
725 * XXX - is this necessary? Or are we guaranteed that
726 * all signalling traffic has a type of 0x06?
728 * XXX - is this guaranteed to be AAL5? Or, if the type is
729 * 0x00 ("raw"), might it be non-AAL5 traffic?
731 if (vpi == 0 && vci == 5)
732 pseudo_header->atm.aal = AAL_SIGNALLING;
734 pseudo_header->atm.aal = AAL_5;
735 pseudo_header->atm.type = TRAF_UNKNOWN;
738 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
740 pseudo_header->atm.vpi = vpi;
741 pseudo_header->atm.vci = vci;
742 pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
744 /* We don't have this information */
745 pseudo_header->atm.flags = 0;
746 pseudo_header->atm.cells = 0;
747 pseudo_header->atm.aal5t_u2u = 0;
748 pseudo_header->atm.aal5t_len = 0;
749 pseudo_header->atm.aal5t_chksum = 0;
755 snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
756 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
759 shomiti_wireless_header whdr;
763 errno = WTAP_ERR_CANT_READ;
764 bytes_read = file_read(&whdr, sizeof (shomiti_wireless_header), fh);
765 if (bytes_read != sizeof (shomiti_wireless_header)) {
766 *err = file_error(fh, err_info);
768 *err = WTAP_ERR_SHORT_READ;
772 /* the 4th byte of the pad is actually a header length,
773 * we've already read 8 bytes of it, and it must never
776 * XXX - presumably that means that the header length
777 * doesn't include the length field, as we've read
780 * XXX - what's in the other 3 bytes of the padding? Is it a
781 * 4-byte length field?
782 * XXX - is there anything in the rest of the header of interest?
783 * XXX - are there any files where the header is shorter than
784 * 4 bytes of length plus 8 bytes of information?
786 if (whdr.pad[3] < 8) {
787 *err = WTAP_ERR_BAD_FILE;
788 *err_info = g_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
792 /* Skip the header. */
793 rsize = ((int) whdr.pad[3]) - 8;
794 if (file_seek(fh, rsize, SEEK_CUR, err) == -1)
797 pseudo_header->ieee_802_11.fcs_len = 4;
798 pseudo_header->ieee_802_11.channel = whdr.channel;
799 pseudo_header->ieee_802_11.data_rate = whdr.rate;
800 pseudo_header->ieee_802_11.signal_level = whdr.signal;
802 /* add back the header and don't forget the pad as well */
803 *header_size = rsize + 8 + 4;
808 static const int wtap_encap[] = {
809 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
810 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
811 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
812 -1, /* WTAP_ENCAP_SLIP -> unsupported */
813 -1, /* WTAP_ENCAP_PPP -> unsupported */
814 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
815 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
816 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
817 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
818 -1, /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
819 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
820 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
821 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
822 0x12, /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
824 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
826 /* Returns 0 if we could write the specified encapsulation type,
827 an error indication otherwise. */
828 int snoop_dump_can_write_encap(int encap)
830 /* Per-packet encapsulations aren't supported. */
831 if (encap == WTAP_ENCAP_PER_PACKET)
832 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
834 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
835 return WTAP_ERR_UNSUPPORTED_ENCAP;
840 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
842 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
844 struct snoop_hdr file_hdr;
846 /* This is a snoop file */
847 wdh->subtype_write = snoop_dump;
848 wdh->subtype_close = NULL;
850 /* Write the file header. */
851 if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
854 /* current "snoop" format is 2 */
855 file_hdr.version = g_htonl(2);
856 file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
857 if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
863 /* Write a record for a packet to a dump file.
864 Returns TRUE on success, FALSE on failure. */
865 static gboolean snoop_dump(wtap_dumper *wdh,
866 const struct wtap_pkthdr *phdr,
867 const guint8 *pd, int *err)
869 const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
870 struct snooprec_hdr rec_hdr;
873 static const char zeroes[4] = {0};
874 struct snoop_atm_hdr atm_hdr;
877 /* We can only write packet records. */
878 if (phdr->rec_type != REC_TYPE_PACKET) {
879 *err = WTAP_ERR_REC_TYPE_UNSUPPORTED;
883 if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
884 atm_hdrsize = sizeof (struct snoop_atm_hdr);
888 /* Record length = header length plus data length... */
889 reclen = (int)sizeof rec_hdr + phdr->caplen + atm_hdrsize;
892 /* ... plus enough bytes to pad it to a 4-byte boundary. */
893 padlen = ((reclen + 3) & ~3) - reclen;
896 /* Don't write anything we're not willing to read. */
897 if (phdr->caplen + atm_hdrsize > WTAP_MAX_PACKET_SIZE) {
898 *err = WTAP_ERR_PACKET_TOO_LARGE;
902 rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
903 rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
904 rec_hdr.rec_len = g_htonl(reclen);
905 rec_hdr.cum_drops = 0;
906 rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
907 rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
908 if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
911 if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
913 * Write the ATM header.
916 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
917 switch (pseudo_header->atm.aal) {
921 atm_hdr.flags |= 0x06;
925 switch (pseudo_header->atm.type) {
929 atm_hdr.flags |= 0x01;
933 /* RFC 1483 LLC multiplexed traffic */
934 atm_hdr.flags |= 0x02;
939 atm_hdr.flags |= 0x05;
944 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
945 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
946 if (!wtap_dump_file_write(wdh, &atm_hdr, sizeof atm_hdr, err))
950 if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
953 /* Now write the padding. */
954 if (!wtap_dump_file_write(wdh, zeroes, padlen, err))