3 * $Id: snoop.c,v 1.62 2003/02/18 19:59:00 guy Exp $
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 */
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,
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);
78 * http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
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.
85 * http://mrpink.lerc.nasa.gov/118x/support.html
87 * had links to modified versions of "tcpdump" and "libpcap" for SUNatm
88 * DLPI support; they suggested that the 3.0 verson of SUNatm uses those
89 * values. The Wayback Machine archived that page, but not the stuff
90 * to which it linked, unfortunately.
92 * It also has a link to "convert.c", which is a program to convert files
93 * from the format written by the "atmsnoop" program that comes with the
94 * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
95 * to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
98 * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
99 * that in an "atmsnoop" file, the header contains 2 bytes (direction and
100 * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
101 * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
102 * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
103 * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
104 * 1 byte of VPI, and 2 bytes of VCI.
106 * The aforementioned page also has a link to some capture files from
107 * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
109 * Source to an "atmdump" package, which includes a modified version of
110 * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
111 * also includes "atmdump", which is a modified "tcpdump", is available
114 * ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
116 * and that code also indicates that DL_IPATM is used, and that an
117 * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
118 * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
119 * and then the ATM PDU, and suggests that the direction flag is 0x80 for
120 * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
121 * "received" (presumably meaning DCE->DTE). That code was used as the
122 * basis for the SunATM support in current CVS versions of libpcap and
123 * tcpdump, and it works.
125 * In fact, the "direction" byte appears to have some other stuff, perhaps
126 * a traffic type, in the lower 7 bits, with the 8th bit indicating the
127 * direction. That appears to be the case.
129 * I don't know what the encapsulation of any of the other types is, so I
130 * leave them all as WTAP_ENCAP_UNKNOWN, except for those for which Brian
131 * Ginsbach has supplied information about the way UNICOS/mp uses them.
132 * I also don't know whether "snoop" can handle any of them (it presumably
133 * can't handle ATM, otherwise Sun wouldn't have supplied "atmsnoop"; even
134 * if it can't, this may be useful reference information for anybody doing
135 * code to use DLPI to do raw packet captures on those network types.
139 * http://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
141 * for information on Shomiti's mutant flavor of snoop. For some unknown
142 * unknown reason, they decided not to just Go With The DLPI Flow, and
143 * instead used the types unspecified in RFC 1461 for their own nefarious
144 * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
145 * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
146 * of them from the "Shomiti" versions of same.
148 int snoop_open(wtap *wth, int *err)
151 char magic[sizeof snoop_magic];
152 struct snoop_hdr hdr;
153 struct snooprec_hdr rec_hdr;
156 static const int snoop_encap[] = {
157 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
158 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
159 WTAP_ENCAP_TOKEN_RING,
160 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
162 WTAP_ENCAP_UNKNOWN, /* HDLC */
163 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
164 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
165 WTAP_ENCAP_FDDI_BITSWAPPED,
166 WTAP_ENCAP_RAW_IP, /* Other */
167 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
168 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
169 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
170 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
171 WTAP_ENCAP_RAW_IP, /* software loopback */
172 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
173 WTAP_ENCAP_IP_OVER_FC, /* Fibre Channel */
174 WTAP_ENCAP_UNKNOWN, /* ATM */
175 WTAP_ENCAP_ATM_PDUS, /* ATM Classical IP */
176 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
177 WTAP_ENCAP_UNKNOWN, /* ISDN */
178 WTAP_ENCAP_UNKNOWN, /* HIPPI */
179 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
180 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
181 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
182 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
184 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
185 static const int shomiti_encap[] = {
186 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
187 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
188 WTAP_ENCAP_TOKEN_RING,
189 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
191 WTAP_ENCAP_UNKNOWN, /* HDLC */
192 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
193 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
194 WTAP_ENCAP_FDDI_BITSWAPPED,
195 WTAP_ENCAP_UNKNOWN, /* Other */
196 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
197 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
198 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
199 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
200 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
202 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
205 /* Read in the string that should be at the start of a "snoop" file */
206 errno = WTAP_ERR_CANT_READ;
207 bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
208 if (bytes_read != sizeof magic) {
209 *err = file_error(wth->fh);
214 wth->data_offset += sizeof magic;
216 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
220 /* Read the rest of the header. */
221 errno = WTAP_ERR_CANT_READ;
222 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
223 if (bytes_read != sizeof hdr) {
224 *err = file_error(wth->fh);
229 wth->data_offset += sizeof hdr;
232 * Make sure it's a version we support.
234 hdr.version = g_ntohl(hdr.version);
235 switch (hdr.version) {
237 case 2: /* Solaris 2.x and later snoop, and Shomiti
238 Surveyor prior to 3.0, or 3.0 and later
240 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
241 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
242 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
246 g_message("snoop: version %u unsupported", hdr.version);
247 *err = WTAP_ERR_UNSUPPORTED;
252 * Oh, this is lovely.
254 * I suppose Shomiti could give a bunch of lawyerly noise about
255 * how "well, RFC 1761 said they were unassigned, and that's
256 * the standard, not the DLPI header file, so it's perfectly OK
257 * for us to use them, blah blah blah", but it's still irritating
258 * as hell that they used the unassigned-in-RFC-1761 values for
259 * their own purposes - especially given that Sun also used
260 * one of them in atmsnoop.
262 * We can't determine whether it's a Shomiti capture based on
263 * the version number, as, according to their documentation on
264 * their capture file format, Shomiti uses a version number of 2
265 * if the data "was captured using an NDIS card", which presumably
266 * means "captured with an ordinary boring network card via NDIS"
267 * as opposed to "captured with our whizzo special capture
270 * The only way I can see to determine that is to check how much
271 * padding there is in the first packet - if there're 4 bytes or
272 * fewer, it's probably Sun snoop, which uses the padding only
273 * for padding (except for atmsnoop, which sometimes appears to
274 * have 4 bytes of padding), but if there's more, it's probably
275 * a Shomiti tool, which uses the padding for additional
279 /* Read first record header. */
280 errno = WTAP_ERR_CANT_READ;
281 bytes_read = file_read(&rec_hdr, 1, sizeof rec_hdr, wth->fh);
282 if (bytes_read != sizeof rec_hdr) {
283 *err = file_error(wth->fh);
284 if (*err == 0 && bytes_read != 0)
285 *err = WTAP_ERR_SHORT_READ;
293 * The file ends after the record header,
294 * which means this is a capture with no
297 * Assume it's a snoop file; the actual type
298 * of file is irrelevant, as there are no
299 * records in it, and thus no extra information
300 * if it's a Shomiti capture, and no link-layer
301 * headers whose type we have to know.
307 * Compute the number of bytes of padding in the
308 * record. If it's greater than 4, this must be a
309 * Shomiti capture. (Some atmsnoop captures appear
310 * to have 4 bytes of padding; Shomiti captures stuff
311 * more than that into the padding.)
313 padbytes = g_ntohl(rec_hdr.rec_len) -
314 (sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
315 is_shomiti = (padbytes > 4);
319 * Seek back to the beginning of the first record.
321 if (file_seek(wth->fh, wth->data_offset, SEEK_SET, err) == -1)
324 hdr.network = g_ntohl(hdr.network);
326 if (hdr.network >= NUM_SHOMITI_ENCAPS
327 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
328 g_message("snoop: Shomiti network type %u unknown or unsupported",
330 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
333 file_encap = shomiti_encap[hdr.network];
335 /* This is a Shomiti file */
336 wth->file_type = WTAP_FILE_SHOMITI;
338 if (hdr.network >= NUM_SNOOP_ENCAPS
339 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
340 g_message("snoop: network type %u unknown or unsupported",
342 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
345 file_encap = snoop_encap[hdr.network];
347 /* This is a snoop file */
348 wth->file_type = WTAP_FILE_SNOOP;
352 * We don't currently use the extra information in Shomiti
353 * records, so we use the same routines to read snoop and
356 wth->subtype_read = snoop_read;
357 wth->subtype_seek_read = snoop_seek_read;
358 wth->file_encap = file_encap;
359 wth->snapshot_length = 0; /* not available in header */
363 /* Read the next packet */
364 static gboolean snoop_read(wtap *wth, int *err, long *data_offset)
370 struct snooprec_hdr hdr;
375 /* Read record header. */
376 errno = WTAP_ERR_CANT_READ;
377 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
378 if (bytes_read != sizeof hdr) {
379 *err = file_error(wth->fh);
380 if (*err == 0 && bytes_read != 0)
381 *err = WTAP_ERR_SHORT_READ;
384 wth->data_offset += sizeof hdr;
386 rec_size = g_ntohl(hdr.rec_len);
387 orig_size = g_ntohl(hdr.orig_len);
388 packet_size = g_ntohl(hdr.incl_len);
389 if (packet_size > WTAP_MAX_PACKET_SIZE) {
391 * Probably a corrupt capture file; don't blow up trying
392 * to allocate space for an immensely-large packet.
394 g_message("snoop: File has %u-byte packet, bigger than maximum of %u",
395 packet_size, WTAP_MAX_PACKET_SIZE);
396 *err = WTAP_ERR_BAD_RECORD;
400 *data_offset = wth->data_offset;
403 * If this is an ATM packet, the first four bytes are the
404 * direction of the packet (transmit/receive), the VPI, and
405 * the VCI; read them and generate the pseudo-header from
408 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
409 if (packet_size < sizeof (struct snoop_atm_hdr)) {
411 * Uh-oh, the packet isn't big enough to even
412 * have a pseudo-header.
414 g_message("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
416 *err = WTAP_ERR_BAD_RECORD;
419 if (!snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
421 return FALSE; /* Read error */
424 * Don't count the pseudo-header as part of the packet.
426 rec_size -= sizeof (struct snoop_atm_hdr);
427 orig_size -= sizeof (struct snoop_atm_hdr);
428 packet_size -= sizeof (struct snoop_atm_hdr);
429 wth->data_offset += sizeof (struct snoop_atm_hdr);
432 buffer_assure_space(wth->frame_buffer, packet_size);
433 if (!snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
435 return FALSE; /* Read error */
436 wth->data_offset += packet_size;
438 wth->phdr.ts.tv_sec = g_ntohl(hdr.ts_sec);
439 wth->phdr.ts.tv_usec = g_ntohl(hdr.ts_usec);
440 wth->phdr.caplen = packet_size;
441 wth->phdr.len = orig_size;
442 wth->phdr.pkt_encap = wth->file_encap;
445 * If this is ATM LANE traffic, try to guess what type of LANE
446 * traffic it is based on the packet contents.
448 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
449 wth->pseudo_header.atm.type == TRAF_LANE) {
450 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
451 wth->phdr.caplen, &wth->pseudo_header);
455 * Skip over the padding (don't "fseek()", as the standard
456 * I/O library on some platforms discards buffered data if
457 * you do that, which means it does a lot more reads).
458 * There's probably not much padding (it's probably padded only
459 * to a 4-byte boundary), so we probably need only do one read.
461 padbytes = rec_size - (sizeof hdr + packet_size);
462 while (padbytes != 0) {
463 bytes_to_read = padbytes;
464 if ((unsigned)bytes_to_read > sizeof padbuf)
465 bytes_to_read = sizeof padbuf;
466 errno = WTAP_ERR_CANT_READ;
467 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
468 if (bytes_read != bytes_to_read) {
469 *err = file_error(wth->fh);
471 *err = WTAP_ERR_SHORT_READ;
474 wth->data_offset += bytes_read;
475 padbytes -= bytes_read;
482 snoop_seek_read(wtap *wth, long seek_off,
483 union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err)
485 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
488 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
489 if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
497 * Read the packet data.
499 if (!snoop_read_rec_data(wth->random_fh, pd, length, err))
500 return FALSE; /* failed */
503 * If this is ATM LANE traffic, try to guess what type of LANE
504 * traffic it is based on the packet contents.
506 if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
507 pseudo_header->atm.type == TRAF_LANE)
508 atm_guess_lane_type(pd, length, pseudo_header);
513 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
516 struct snoop_atm_hdr atm_phdr;
521 errno = WTAP_ERR_CANT_READ;
522 bytes_read = file_read(&atm_phdr, 1, sizeof (struct snoop_atm_hdr), fh);
523 if (bytes_read != sizeof (struct snoop_atm_hdr)) {
524 *err = file_error(fh);
526 *err = WTAP_ERR_SHORT_READ;
531 vci = pntohs(&atm_phdr.vci);
534 * The lower 4 bits of the first byte of the header indicate
535 * the type of traffic, as per the "atmioctl.h" header in
538 switch (atm_phdr.flags & 0x0F) {
540 case 0x01: /* LANE */
541 pseudo_header->atm.aal = AAL_5;
542 pseudo_header->atm.type = TRAF_LANE;
545 case 0x02: /* RFC 1483 LLC multiplexed traffic */
546 pseudo_header->atm.aal = AAL_5;
547 pseudo_header->atm.type = TRAF_LLCMX;
550 case 0x05: /* ILMI */
551 pseudo_header->atm.aal = AAL_5;
552 pseudo_header->atm.type = TRAF_ILMI;
555 case 0x06: /* Signalling AAL */
556 pseudo_header->atm.aal = AAL_SIGNALLING;
557 pseudo_header->atm.type = TRAF_UNKNOWN;
560 case 0x03: /* MARS (RFC 2022) */
561 pseudo_header->atm.aal = AAL_5;
562 pseudo_header->atm.type = TRAF_UNKNOWN;
565 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
566 pseudo_header->atm.aal = AAL_5;
567 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
572 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
573 * case assume it's AAL_SIGNALLING; we know nothing more
576 * XXX - is this necessary? Or are we guaranteed that
577 * all signalling traffic has a type of 0x06?
579 * XXX - is this guaranteed to be AAL5? Or, if the type is
580 * 0x00 ("raw"), might it be non-AAL5 traffic?
582 if (vpi == 0 && vci == 5)
583 pseudo_header->atm.aal = AAL_SIGNALLING;
585 pseudo_header->atm.aal = AAL_5;
586 pseudo_header->atm.type = TRAF_UNKNOWN;
589 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
591 pseudo_header->atm.vpi = vpi;
592 pseudo_header->atm.vci = vci;
593 pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
595 /* We don't have this information */
596 pseudo_header->atm.flags = 0;
597 pseudo_header->atm.cells = 0;
598 pseudo_header->atm.aal5t_u2u = 0;
599 pseudo_header->atm.aal5t_len = 0;
600 pseudo_header->atm.aal5t_chksum = 0;
606 snoop_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
610 errno = WTAP_ERR_CANT_READ;
611 bytes_read = file_read(pd, 1, length, fh);
613 if (bytes_read != length) {
614 *err = file_error(fh);
616 *err = WTAP_ERR_SHORT_READ;
622 static const int wtap_encap[] = {
623 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
624 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
625 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
626 -1, /* WTAP_ENCAP_SLIP -> unsupported */
627 -1, /* WTAP_ENCAP_PPP -> unsupported */
628 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
629 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
630 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
631 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
632 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
633 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
634 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
635 0x12, /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
636 -1 /* WTAP_ENCAP_NULL -> unsupported */
638 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
640 /* Returns 0 if we could write the specified encapsulation type,
641 an error indication otherwise. */
642 int snoop_dump_can_write_encap(int encap)
644 /* Per-packet encapsulations aren't supported. */
645 if (encap == WTAP_ENCAP_PER_PACKET)
646 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
648 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
649 return WTAP_ERR_UNSUPPORTED_ENCAP;
654 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
656 gboolean snoop_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
658 struct snoop_hdr file_hdr;
661 /* This is a snoop file */
662 wdh->subtype_write = snoop_dump;
663 wdh->subtype_close = NULL;
665 /* Write the file header. */
666 nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
667 if (nwritten != sizeof snoop_magic) {
668 if (nwritten == 0 && ferror(wdh->fh))
671 *err = WTAP_ERR_SHORT_WRITE;
675 /* current "snoop" format is 2 */
676 file_hdr.version = g_htonl(2);
677 file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
678 nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
679 if (nwritten != sizeof file_hdr) {
680 if (nwritten == 0 && ferror(wdh->fh))
683 *err = WTAP_ERR_SHORT_WRITE;
690 /* Write a record for a packet to a dump file.
691 Returns TRUE on success, FALSE on failure. */
692 static gboolean snoop_dump(wtap_dumper *wdh,
693 const struct wtap_pkthdr *phdr,
694 const union wtap_pseudo_header *pseudo_header _U_,
695 const guchar *pd, int *err)
697 struct snooprec_hdr rec_hdr;
701 static char zeroes[4];
702 struct snoop_atm_hdr atm_hdr;
705 if (wdh->encap == WTAP_ENCAP_ATM_PDUS)
706 atm_hdrsize = sizeof (struct snoop_atm_hdr);
710 /* Record length = header length plus data length... */
711 reclen = sizeof rec_hdr + phdr->caplen + atm_hdrsize;
713 /* ... plus enough bytes to pad it to a 4-byte boundary. */
714 padlen = ((reclen + 3) & ~3) - reclen;
717 rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
718 rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
719 rec_hdr.rec_len = g_htonl(reclen);
720 rec_hdr.cum_drops = 0;
721 rec_hdr.ts_sec = g_htonl(phdr->ts.tv_sec);
722 rec_hdr.ts_usec = g_htonl(phdr->ts.tv_usec);
723 nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
724 if (nwritten != sizeof rec_hdr) {
725 if (nwritten == 0 && ferror(wdh->fh))
728 *err = WTAP_ERR_SHORT_WRITE;
732 if (wdh->encap == WTAP_ENCAP_ATM_PDUS) {
734 * Write the ATM header.
737 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
738 switch (pseudo_header->atm.aal) {
742 atm_hdr.flags |= 0x06;
746 switch (pseudo_header->atm.type) {
750 atm_hdr.flags |= 0x01;
754 /* RFC 1483 LLC multiplexed traffic */
755 atm_hdr.flags |= 0x02;
760 atm_hdr.flags |= 0x05;
765 atm_hdr.vpi = pseudo_header->atm.vpi;
766 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
767 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
768 if (nwritten != sizeof atm_hdr) {
769 if (nwritten == 0 && ferror(wdh->fh))
772 *err = WTAP_ERR_SHORT_WRITE;
777 nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
778 if (nwritten != phdr->caplen) {
779 if (nwritten == 0 && ferror(wdh->fh))
782 *err = WTAP_ERR_SHORT_WRITE;
786 /* Now write the padding. */
787 nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
788 if (nwritten != padlen) {
789 if (nwritten == 0 && ferror(wdh->fh))
792 *err = WTAP_ERR_SHORT_WRITE;