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"
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.
173 * http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
175 * gave information on Shomiti's mutant flavor of snoop; Shomiti's Web site
176 * is no longer available on the Wayback Machine. For some unknown reason,
177 * they decided not to just Go With The DLPI Flow, and instead used the types
178 * unspecified in RFC 1461 for their own nefarious purposes, such as
179 * distinguishing 10MB from 100MB from 1000MB Ethernet and distinguishing
180 * 4MB from 16MB Token Ring, and distinguishing both of them from the
181 * "Shomiti" versions of same.
183 int snoop_open(wtap *wth, int *err, gchar **err_info)
186 char magic[sizeof snoop_magic];
187 struct snoop_hdr hdr;
188 struct snooprec_hdr rec_hdr;
191 static const int snoop_encap[] = {
192 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
193 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
194 WTAP_ENCAP_TOKEN_RING,
195 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
197 WTAP_ENCAP_UNKNOWN, /* HDLC */
198 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
199 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
200 WTAP_ENCAP_FDDI_BITSWAPPED,
201 WTAP_ENCAP_NULL, /* Other */
202 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
203 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
204 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
205 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
206 WTAP_ENCAP_NULL, /* software loopback */
207 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
208 WTAP_ENCAP_IP_OVER_FC, /* Fibre Channel */
209 WTAP_ENCAP_UNKNOWN, /* ATM */
210 WTAP_ENCAP_ATM_PDUS, /* ATM Classical IP */
211 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
212 WTAP_ENCAP_UNKNOWN, /* ISDN */
213 WTAP_ENCAP_UNKNOWN, /* HIPPI */
214 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
215 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
216 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
217 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
218 WTAP_ENCAP_IP_OVER_IB, /* Infiniband */
220 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
221 #define SNOOP_PRIVATE_BIT 0x80000000
222 static const int snoop_private_encap[] = {
223 WTAP_ENCAP_UNKNOWN, /* Not Used */
224 WTAP_ENCAP_UNKNOWN, /* IPv4 Tunnel Link */
225 WTAP_ENCAP_UNKNOWN, /* IPv6 Tunnel Link */
226 WTAP_ENCAP_UNKNOWN, /* Virtual network interface */
227 WTAP_ENCAP_UNKNOWN, /* IEEE 802.11 */
228 WTAP_ENCAP_IPNET, /* ipnet(7D) link */
229 WTAP_ENCAP_UNKNOWN, /* IPMP stub interface */
230 WTAP_ENCAP_UNKNOWN, /* 6to4 Tunnel Link */
232 #define NUM_SNOOP_PRIVATE_ENCAPS (sizeof snoop_private_encap / sizeof snoop_private_encap[0])
233 static const int shomiti_encap[] = {
234 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
235 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
236 WTAP_ENCAP_TOKEN_RING,
237 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
239 WTAP_ENCAP_UNKNOWN, /* HDLC */
240 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
241 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
242 WTAP_ENCAP_FDDI_BITSWAPPED,
243 WTAP_ENCAP_UNKNOWN, /* Other */
244 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
245 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
246 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
247 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
248 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
249 WTAP_ENCAP_UNKNOWN, /* Other */
250 WTAP_ENCAP_UNKNOWN, /* Other */
251 WTAP_ENCAP_UNKNOWN, /* Other */
252 WTAP_ENCAP_IEEE_802_11_WITH_RADIO, /* IEEE 802.11 with Radio Header */
253 WTAP_ENCAP_ETHERNET, /* 10 Gigabit Ethernet */
255 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
259 /* Read in the string that should be at the start of a "snoop" file */
260 errno = WTAP_ERR_CANT_READ;
261 bytes_read = file_read(magic, sizeof magic, wth->fh);
262 if (bytes_read != sizeof magic) {
263 *err = file_error(wth->fh, err_info);
264 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
269 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
273 /* Read the rest of the header. */
274 errno = WTAP_ERR_CANT_READ;
275 bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
276 if (bytes_read != sizeof hdr) {
277 *err = file_error(wth->fh, err_info);
279 *err = WTAP_ERR_SHORT_READ;
284 * Make sure it's a version we support.
286 hdr.version = g_ntohl(hdr.version);
287 switch (hdr.version) {
289 case 2: /* Solaris 2.x and later snoop, and Shomiti
290 Surveyor prior to 3.0, or 3.0 and later
292 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
293 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
294 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
298 *err = WTAP_ERR_UNSUPPORTED;
299 *err_info = g_strdup_printf("snoop: version %u unsupported", hdr.version);
304 * Oh, this is lovely.
306 * I suppose Shomiti could give a bunch of lawyerly noise about
307 * how "well, RFC 1761 said they were unassigned, and that's
308 * the standard, not the DLPI header file, so it's perfectly OK
309 * for us to use them, blah blah blah", but it's still irritating
310 * as hell that they used the unassigned-in-RFC-1761 values for
311 * their own purposes - especially given that Sun also used
312 * one of them in atmsnoop.
314 * We can't determine whether it's a Shomiti capture based on
315 * the version number, as, according to their documentation on
316 * their capture file format, Shomiti uses a version number of 2
317 * if the data "was captured using an NDIS card", which presumably
318 * means "captured with an ordinary boring network card via NDIS"
319 * as opposed to "captured with our whizzo special capture
322 * The only way I can see to determine that is to check how much
323 * padding there is in the first packet - if there's enough
324 * padding for a Shomiti trailer, it's probably a Shomiti
325 * capture, and otherwise, it's probably from Snoop.
329 * Start out assuming it's not a Shomiti capture.
333 /* Read first record header. */
334 saved_offset = file_tell(wth->fh);
335 errno = WTAP_ERR_CANT_READ;
336 bytes_read = file_read(&rec_hdr, sizeof rec_hdr, wth->fh);
337 if (bytes_read != sizeof rec_hdr) {
338 *err = file_error(wth->fh, err_info);
340 *err = WTAP_ERR_SHORT_READ;
344 * The file ends after the record header, which means this
345 * is a capture with no packets.
347 * We assume it's a snoop file; the actual type of file is
348 * irrelevant, as there are no records in it, and thus no
349 * extra information if it's a Shomiti capture, and no
350 * link-layer headers whose type we have to know, and no
351 * Ethernet frames that might have an FCS.
355 * Compute the number of bytes of padding in the
356 * record. If it's at least the size of a Shomiti
357 * trailer record, we assume this is a Shomiti
358 * capture. (Some atmsnoop captures appear
359 * to have 4 bytes of padding, and at least one
360 * snoop capture appears to have 6 bytes of padding;
361 * the Shomiti header is larger than either of those.)
363 if (g_ntohl(rec_hdr.rec_len) >
364 (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
366 * Well, we have padding; how much?
368 padbytes = g_ntohl(rec_hdr.rec_len) -
369 ((guint)sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
372 * Is it at least the size of a Shomiti trailer?
375 (padbytes >= sizeof (struct shomiti_trailer));
380 * Seek back to the beginning of the first record.
382 if (file_seek(wth->fh, saved_offset, SEEK_SET, err) == -1)
385 hdr.network = g_ntohl(hdr.network);
387 if (hdr.network >= NUM_SHOMITI_ENCAPS
388 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
389 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
390 *err_info = g_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
394 file_encap = shomiti_encap[hdr.network];
396 /* This is a Shomiti file */
397 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SHOMITI;
398 } else if (hdr.network & SNOOP_PRIVATE_BIT) {
399 if ((hdr.network^SNOOP_PRIVATE_BIT) >= NUM_SNOOP_PRIVATE_ENCAPS
400 || snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT] == WTAP_ENCAP_UNKNOWN) {
401 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
402 *err_info = g_strdup_printf("snoop: private network type %u unknown or unsupported",
406 file_encap = snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT];
408 /* This is a snoop file */
409 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
411 if (hdr.network >= NUM_SNOOP_ENCAPS
412 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
413 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
414 *err_info = g_strdup_printf("snoop: network type %u unknown or unsupported",
418 file_encap = snoop_encap[hdr.network];
420 /* This is a snoop file */
421 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_SNOOP;
425 * We don't currently use the extra information in Shomiti
426 * records, so we use the same routines to read snoop and
429 wth->subtype_read = snoop_read;
430 wth->subtype_seek_read = snoop_seek_read;
431 wth->file_encap = file_encap;
432 wth->snapshot_length = 0; /* not available in header */
433 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
446 } shomiti_wireless_header;
449 /* Read the next packet */
450 static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
458 *data_offset = file_tell(wth->fh);
460 padbytes = snoop_read_packet(wth, wth->fh, &wth->phdr,
461 wth->frame_buffer, err, err_info);
466 * Skip over the padding (don't "fseek()", as the standard
467 * I/O library on some platforms discards buffered data if
468 * you do that, which means it does a lot more reads).
470 * XXX - is that still true?
472 * There's probably not much padding (it's probably padded only
473 * to a 4-byte boundary), so we probably need only do one read.
475 while (padbytes != 0) {
476 bytes_to_read = padbytes;
477 if ((unsigned)bytes_to_read > sizeof padbuf)
478 bytes_to_read = sizeof padbuf;
479 errno = WTAP_ERR_CANT_READ;
480 bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
481 if (bytes_read != bytes_to_read) {
482 *err = file_error(wth->fh, err_info);
484 *err = WTAP_ERR_SHORT_READ;
487 padbytes -= bytes_read;
494 snoop_seek_read(wtap *wth, gint64 seek_off,
495 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
497 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
500 if (snoop_read_packet(wth, wth->random_fh, phdr, buf, err, err_info) == -1) {
502 *err = WTAP_ERR_SHORT_READ;
509 snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
510 Buffer *buf, int *err, gchar **err_info)
512 struct snooprec_hdr hdr;
519 /* Read record header. */
520 errno = WTAP_ERR_CANT_READ;
521 bytes_read = file_read(&hdr, sizeof hdr, fh);
522 if (bytes_read != sizeof hdr) {
523 *err = file_error(fh, err_info);
524 if (*err == 0 && bytes_read != 0)
525 *err = WTAP_ERR_SHORT_READ;
529 rec_size = g_ntohl(hdr.rec_len);
530 orig_size = g_ntohl(hdr.orig_len);
531 packet_size = g_ntohl(hdr.incl_len);
532 if (orig_size > WTAP_MAX_PACKET_SIZE) {
534 * Probably a corrupt capture file; don't blow up trying
535 * to allocate space for an immensely-large packet.
537 *err = WTAP_ERR_BAD_FILE;
538 *err_info = g_strdup_printf("snoop: File has %u-byte original length, bigger than maximum of %u",
539 orig_size, WTAP_MAX_PACKET_SIZE);
542 if (packet_size > WTAP_MAX_PACKET_SIZE) {
544 * Probably a corrupt capture file; don't blow up trying
545 * to allocate space for an immensely-large packet.
547 *err = WTAP_ERR_BAD_FILE;
548 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
549 packet_size, WTAP_MAX_PACKET_SIZE);
552 if (packet_size > rec_size) {
554 * Probably a corrupt capture file.
556 *err = WTAP_ERR_BAD_FILE;
557 *err_info = g_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
558 packet_size, rec_size);
562 switch (wth->file_encap) {
564 case WTAP_ENCAP_ATM_PDUS:
566 * This is an ATM packet, so the first four bytes are
567 * the direction of the packet (transmit/receive), the
568 * VPI, and the VCI; read them and generate the
569 * pseudo-header from them.
571 if (packet_size < sizeof (struct snoop_atm_hdr)) {
573 * Uh-oh, the packet isn't big enough to even
574 * have a pseudo-header.
576 *err = WTAP_ERR_BAD_FILE;
577 *err_info = g_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header",
581 if (!snoop_read_atm_pseudoheader(fh, &phdr->pseudo_header,
583 return -1; /* Read error */
586 * Don't count the pseudo-header as part of the packet.
588 rec_size -= (guint32)sizeof (struct snoop_atm_hdr);
589 orig_size -= (guint32)sizeof (struct snoop_atm_hdr);
590 packet_size -= (guint32)sizeof (struct snoop_atm_hdr);
593 case WTAP_ENCAP_ETHERNET:
595 * If this is a snoop file, we assume there's no FCS in
596 * this frame; if this is a Shomit file, we assume there
597 * is. (XXX - or should we treat it a "maybe"?)
599 if (wth->file_type_subtype == WTAP_FILE_TYPE_SUBTYPE_SHOMITI)
600 phdr->pseudo_header.eth.fcs_len = 4;
602 phdr->pseudo_header.eth.fcs_len = 0;
605 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
606 if (packet_size < sizeof (shomiti_wireless_header)) {
608 * Uh-oh, the packet isn't big enough to even
609 * have a pseudo-header.
611 *err = WTAP_ERR_BAD_FILE;
612 *err_info = g_strdup_printf("snoop: Shomiti wireless file has a %u-byte packet, too small to have even a wireless pseudo-header",
616 if (!snoop_read_shomiti_wireless_pseudoheader(fh,
617 &phdr->pseudo_header, err, err_info, &header_size))
618 return -1; /* Read error */
621 * Don't count the pseudo-header as part of the packet.
623 rec_size -= header_size;
624 orig_size -= header_size;
625 packet_size -= header_size;
629 phdr->rec_type = REC_TYPE_PACKET;
630 phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
631 phdr->ts.secs = g_ntohl(hdr.ts_sec);
632 phdr->ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
633 phdr->caplen = packet_size;
634 phdr->len = orig_size;
636 if (rec_size < (sizeof hdr + packet_size)) {
638 * What, *negative* padding? Bogus.
640 *err = WTAP_ERR_BAD_FILE;
641 *err_info = g_strdup_printf("snoop: File has %u-byte record with packet size of %u",
642 rec_size, packet_size);
647 * Read the packet data.
649 if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
650 return -1; /* failed */
653 * If this is ATM LANE traffic, try to guess what type of LANE
654 * traffic it is based on the packet contents.
656 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
657 phdr->pseudo_header.atm.type == TRAF_LANE) {
658 atm_guess_lane_type(phdr, buffer_start_ptr(buf));
661 return rec_size - ((guint)sizeof hdr + packet_size);
665 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
666 int *err, gchar **err_info)
668 struct snoop_atm_hdr atm_phdr;
673 errno = WTAP_ERR_CANT_READ;
674 bytes_read = file_read(&atm_phdr, sizeof (struct snoop_atm_hdr), fh);
675 if (bytes_read != sizeof (struct snoop_atm_hdr)) {
676 *err = file_error(fh, err_info);
678 *err = WTAP_ERR_SHORT_READ;
683 vci = pntoh16(&atm_phdr.vci);
686 * The lower 4 bits of the first byte of the header indicate
687 * the type of traffic, as per the "atmioctl.h" header in
690 switch (atm_phdr.flags & 0x0F) {
692 case 0x01: /* LANE */
693 pseudo_header->atm.aal = AAL_5;
694 pseudo_header->atm.type = TRAF_LANE;
697 case 0x02: /* RFC 1483 LLC multiplexed traffic */
698 pseudo_header->atm.aal = AAL_5;
699 pseudo_header->atm.type = TRAF_LLCMX;
702 case 0x05: /* ILMI */
703 pseudo_header->atm.aal = AAL_5;
704 pseudo_header->atm.type = TRAF_ILMI;
707 case 0x06: /* Signalling AAL */
708 pseudo_header->atm.aal = AAL_SIGNALLING;
709 pseudo_header->atm.type = TRAF_UNKNOWN;
712 case 0x03: /* MARS (RFC 2022) */
713 pseudo_header->atm.aal = AAL_5;
714 pseudo_header->atm.type = TRAF_UNKNOWN;
717 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
718 pseudo_header->atm.aal = AAL_5;
719 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
724 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
725 * case assume it's AAL_SIGNALLING; we know nothing more
728 * XXX - is this necessary? Or are we guaranteed that
729 * all signalling traffic has a type of 0x06?
731 * XXX - is this guaranteed to be AAL5? Or, if the type is
732 * 0x00 ("raw"), might it be non-AAL5 traffic?
734 if (vpi == 0 && vci == 5)
735 pseudo_header->atm.aal = AAL_SIGNALLING;
737 pseudo_header->atm.aal = AAL_5;
738 pseudo_header->atm.type = TRAF_UNKNOWN;
741 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
743 pseudo_header->atm.vpi = vpi;
744 pseudo_header->atm.vci = vci;
745 pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
747 /* We don't have this information */
748 pseudo_header->atm.flags = 0;
749 pseudo_header->atm.cells = 0;
750 pseudo_header->atm.aal5t_u2u = 0;
751 pseudo_header->atm.aal5t_len = 0;
752 pseudo_header->atm.aal5t_chksum = 0;
758 snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
759 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
762 shomiti_wireless_header whdr;
766 errno = WTAP_ERR_CANT_READ;
767 bytes_read = file_read(&whdr, sizeof (shomiti_wireless_header), fh);
768 if (bytes_read != sizeof (shomiti_wireless_header)) {
769 *err = file_error(fh, err_info);
771 *err = WTAP_ERR_SHORT_READ;
775 /* the 4th byte of the pad is actually a header length,
776 * we've already read 8 bytes of it, and it must never
779 * XXX - presumably that means that the header length
780 * doesn't include the length field, as we've read
783 * XXX - what's in the other 3 bytes of the padding? Is it a
784 * 4-byte length field?
785 * XXX - is there anything in the rest of the header of interest?
786 * XXX - are there any files where the header is shorter than
787 * 4 bytes of length plus 8 bytes of information?
789 if (whdr.pad[3] < 8) {
790 *err = WTAP_ERR_BAD_FILE;
791 *err_info = g_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
795 /* Skip the header. */
796 rsize = ((int) whdr.pad[3]) - 8;
797 if (file_seek(fh, rsize, SEEK_CUR, err) == -1)
800 pseudo_header->ieee_802_11.fcs_len = 4;
801 pseudo_header->ieee_802_11.channel = whdr.channel;
802 pseudo_header->ieee_802_11.data_rate = whdr.rate;
803 pseudo_header->ieee_802_11.signal_level = whdr.signal;
805 /* add back the header and don't forget the pad as well */
806 *header_size = rsize + 8 + 4;
811 static const int wtap_encap[] = {
812 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
813 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
814 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
815 -1, /* WTAP_ENCAP_SLIP -> unsupported */
816 -1, /* WTAP_ENCAP_PPP -> unsupported */
817 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
818 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
819 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
820 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
821 -1, /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
822 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
823 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
824 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
825 0x12, /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
827 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
829 /* Returns 0 if we could write the specified encapsulation type,
830 an error indication otherwise. */
831 int snoop_dump_can_write_encap(int encap)
833 /* Per-packet encapsulations aren't supported. */
834 if (encap == WTAP_ENCAP_PER_PACKET)
835 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
837 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
838 return WTAP_ERR_UNSUPPORTED_ENCAP;
843 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
845 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
847 struct snoop_hdr file_hdr;
849 /* This is a snoop file */
850 wdh->subtype_write = snoop_dump;
851 wdh->subtype_close = NULL;
853 /* Write the file header. */
854 if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
857 /* current "snoop" format is 2 */
858 file_hdr.version = g_htonl(2);
859 file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
860 if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
866 /* Write a record for a packet to a dump file.
867 Returns TRUE on success, FALSE on failure. */
868 static gboolean snoop_dump(wtap_dumper *wdh,
869 const struct wtap_pkthdr *phdr,
870 const guint8 *pd, int *err)
872 const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
873 struct snooprec_hdr rec_hdr;
876 static const char zeroes[4] = {0};
877 struct snoop_atm_hdr atm_hdr;
880 /* We can only write packet records. */
881 if (phdr->rec_type != REC_TYPE_PACKET) {
882 *err = WTAP_ERR_REC_TYPE_UNSUPPORTED;
886 if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
887 atm_hdrsize = sizeof (struct snoop_atm_hdr);
891 /* Record length = header length plus data length... */
892 reclen = (int)sizeof rec_hdr + phdr->caplen + atm_hdrsize;
895 /* ... plus enough bytes to pad it to a 4-byte boundary. */
896 padlen = ((reclen + 3) & ~3) - reclen;
899 /* Don't write anything we're not willing to read. */
900 if (phdr->caplen + atm_hdrsize > WTAP_MAX_PACKET_SIZE) {
901 *err = WTAP_ERR_PACKET_TOO_LARGE;
905 rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
906 rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
907 rec_hdr.rec_len = g_htonl(reclen);
908 rec_hdr.cum_drops = 0;
909 rec_hdr.ts_sec = g_htonl(phdr->ts.secs);
910 rec_hdr.ts_usec = g_htonl(phdr->ts.nsecs / 1000);
911 if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
914 if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
916 * Write the ATM header.
919 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
920 switch (pseudo_header->atm.aal) {
924 atm_hdr.flags |= 0x06;
928 switch (pseudo_header->atm.type) {
932 atm_hdr.flags |= 0x01;
936 /* RFC 1483 LLC multiplexed traffic */
937 atm_hdr.flags |= 0x02;
942 atm_hdr.flags |= 0x05;
947 atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
948 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
949 if (!wtap_dump_file_write(wdh, &atm_hdr, sizeof atm_hdr, err))
953 if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
956 /* Now write the padding. */
957 if (!wtap_dump_file_write(wdh, zeroes, padlen, err))