3 * $Id: snoop.c,v 1.55 2002/08/28 20:30:45 jmayer 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 * has links to modified versions of "tcpdump" and "libpcap" for SUNatm
88 * DLPI support; they suggest the 3.0 verson of SUNatm uses those
91 * It also has a link to "convert.c", which is a program to convert files
92 * from the format written by the "atmsnoop" program that comes with the
93 * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
94 * to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
97 * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
98 * that in an "atmsnoop" file, the header contains 2 bytes (direction and
99 * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
100 * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
101 * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
102 * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
103 * 1 byte of VPI, and 2 bytes of VCI.
105 * The aforementioned page also has a link to some capture files from
106 * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
108 * Source to an "atmdump" package, which includes a modified version of
109 * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
110 * also includes "atmdump", which is a modified "tcpdump", says that an
111 * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
112 * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
113 * and then the ATM PDU, and suggests that the direction byte is 0x80 for
114 * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
115 * "received" (presumably meaning DCE->DTE).
117 * In fact, the "direction" byte appears to have some other stuff, perhaps
118 * a traffic type, in the lower 7 bits, with the 8th bit indicating the
121 * I don't know what the encapsulation of any of the other types is, so I
122 * leave them all as WTAP_ENCAP_UNKNOWN. I also don't know whether "snoop"
123 * can handle any of them (it presumably can't handle ATM, otherwise Sun
124 * wouldn't have supplied "atmsnoop"; even if it can't, this may be useful
125 * reference information for anybody doing code to use DLPI to do raw packet
126 * captures on those network types.
130 * http://www.shomiti.com/support/TNCapFileFormat.htm
132 * for information on Shomiti's mutant flavor of snoop. For some unknown
133 * unknown reason, they decided not to just Go With The DLPI Flow, and
134 * instead used the types unspecified in RFC 1461 for their own nefarious
135 * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
136 * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
137 * of them from the "Shomiti" versions of same.
139 int snoop_open(wtap *wth, int *err)
142 char magic[sizeof snoop_magic];
143 struct snoop_hdr hdr;
144 static const int snoop_encap[] = {
145 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
146 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
147 WTAP_ENCAP_TOKEN_RING,
148 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
150 WTAP_ENCAP_UNKNOWN, /* HDLC */
151 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
152 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
153 WTAP_ENCAP_FDDI_BITSWAPPED,
154 WTAP_ENCAP_UNKNOWN, /* Other */
155 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
156 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
157 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
158 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
159 WTAP_ENCAP_UNKNOWN, /* software loopback */
160 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
161 WTAP_ENCAP_UNKNOWN, /* Fibre Channel */
162 WTAP_ENCAP_UNKNOWN, /* ATM */
163 WTAP_ENCAP_ATM_SNIFFER, /* ATM Classical IP */
164 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
165 WTAP_ENCAP_UNKNOWN, /* ISDN */
166 WTAP_ENCAP_UNKNOWN, /* HIPPI */
167 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
168 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
169 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
170 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
172 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
173 static const int shomiti_encap[] = {
174 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
175 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
176 WTAP_ENCAP_TOKEN_RING,
177 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
179 WTAP_ENCAP_UNKNOWN, /* HDLC */
180 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
181 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
182 WTAP_ENCAP_FDDI_BITSWAPPED,
183 WTAP_ENCAP_UNKNOWN, /* Other */
184 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
185 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
186 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
187 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
188 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
190 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
193 /* Read in the string that should be at the start of a "snoop" file */
194 errno = WTAP_ERR_CANT_READ;
195 bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
196 if (bytes_read != sizeof magic) {
197 *err = file_error(wth->fh);
202 wth->data_offset += sizeof magic;
204 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
208 /* Read the rest of the header. */
209 errno = WTAP_ERR_CANT_READ;
210 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
211 if (bytes_read != sizeof hdr) {
212 *err = file_error(wth->fh);
217 wth->data_offset += sizeof hdr;
220 * Oh, this is lovely.
222 * I suppose Shomiti could give a bunch of lawyerly noise about
223 * how "well, RFC 1761 said they were unassigned, and that's
224 * the standard, not the DLPI header file, so it's perfectly OK
225 * for us to use them, blah blah blah", but it's still irritating
226 * as hell that they used the unassigned-in-RFC-1761 values for
227 * their own purposes - especially given that Sun also used
228 * one of them in atmsnoop.
230 * For now, we treat a version number of 2 as indicating that
231 * this is a Sun snoop file, and version numbers of 3, 4, and 5
232 * as indicating that this is a Shomiti file, even though
233 * their capture file format documentation claims that they
234 * use 2 if the data "was captured using an NDIS card", which
235 * presumably means "captured with an ordinary boring network
236 * card via NDIS" as opposed to "captured with our whizzo
237 * special capture hardware".
239 * This runs the risk that we may misinterpret the network
240 * type of Shomiti captures not done using their hardware.
241 * Currently, the only not-in-RFC-1761 type we interpret in
242 * Sun snoop files is 18, for atmsnoop, and that's not used
243 * by Shomiti, but if any of the types used by Shomiti are
244 * also used by Snoop or a variant thereof - e.g.:
246 * value snoop Shomiti
247 * 10 Frame Relay 100MB Ethernet
248 * 11 MP over Frame Relay 4MB 802.5
249 * 12 "Character Async" 1000MB Ethernet
250 * 13 X.25 Classical IP "IEEE 802.5 Shomiti"
251 * 14 "software loopback" "4MB IEEE 802.5 Shomiti"
253 * then we have a problem that may be resolvable only by checking
254 * how much padding there is in the first packet - if there're 3
255 * bytes or less, it's probably Sun snoop, which uses the padding
256 * only for padding, but if there's more, it's probably a Shomiti
257 * tool, which uses the padding for additional information.
259 hdr.version = g_ntohl(hdr.version);
260 hdr.network = g_ntohl(hdr.network);
261 switch (hdr.version) {
263 case 2: /* Solaris 2.x and later snoop, and Shomiti
264 Surveyor prior to 3.0 (or 3.x with NDIS card?) */
265 if (hdr.network >= NUM_SNOOP_ENCAPS
266 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
267 g_message("snoop: network type %u unknown or unsupported",
269 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
272 file_encap = snoop_encap[hdr.network];
275 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
276 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
277 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
278 if (hdr.network >= NUM_SHOMITI_ENCAPS
279 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
280 g_message("snoop: Shomiti network type %u unknown or unsupported",
282 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
285 file_encap = shomiti_encap[hdr.network];
289 g_message("snoop: version %u unsupported", hdr.version);
290 *err = WTAP_ERR_UNSUPPORTED;
294 /* This is a snoop file */
295 wth->file_type = WTAP_FILE_SNOOP;
296 wth->subtype_read = snoop_read;
297 wth->subtype_seek_read = snoop_seek_read;
298 wth->file_encap = file_encap;
299 wth->snapshot_length = 0; /* not available in header */
303 /* Read the next packet */
304 static gboolean snoop_read(wtap *wth, int *err, long *data_offset)
310 struct snooprec_hdr hdr;
315 /* Read record header. */
316 errno = WTAP_ERR_CANT_READ;
317 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
318 if (bytes_read != sizeof hdr) {
319 *err = file_error(wth->fh);
320 if (*err == 0 && bytes_read != 0) {
321 *err = WTAP_ERR_SHORT_READ;
325 wth->data_offset += sizeof hdr;
327 rec_size = g_ntohl(hdr.rec_len);
328 orig_size = g_ntohl(hdr.orig_len);
329 packet_size = g_ntohl(hdr.incl_len);
330 if (packet_size > WTAP_MAX_PACKET_SIZE) {
332 * Probably a corrupt capture file; don't blow up trying
333 * to allocate space for an immensely-large packet.
335 g_message("snoop: File has %u-byte packet, bigger than maximum of %u",
336 packet_size, WTAP_MAX_PACKET_SIZE);
337 *err = WTAP_ERR_BAD_RECORD;
341 *data_offset = wth->data_offset;
344 * If this is an ATM packet, the first four bytes are the
345 * direction of the packet (transmit/receive), the VPI, and
346 * the VCI; read them and generate the pseudo-header from
349 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
350 if (packet_size < sizeof (struct snoop_atm_hdr)) {
352 * Uh-oh, the packet isn't big enough to even
353 * have a pseudo-header.
355 g_message("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
357 *err = WTAP_ERR_BAD_RECORD;
360 if (!snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
362 return FALSE; /* Read error */
365 * Don't count the pseudo-header as part of the packet.
367 rec_size -= sizeof (struct snoop_atm_hdr);
368 orig_size -= sizeof (struct snoop_atm_hdr);
369 packet_size -= sizeof (struct snoop_atm_hdr);
370 wth->data_offset += sizeof (struct snoop_atm_hdr);
373 buffer_assure_space(wth->frame_buffer, packet_size);
374 if (!snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
376 return FALSE; /* Read error */
377 wth->data_offset += packet_size;
379 wth->phdr.ts.tv_sec = g_ntohl(hdr.ts_sec);
380 wth->phdr.ts.tv_usec = g_ntohl(hdr.ts_usec);
381 wth->phdr.caplen = packet_size;
382 wth->phdr.len = orig_size;
383 wth->phdr.pkt_encap = wth->file_encap;
386 * If this is ATM LANE traffic, try to guess what type of LANE
387 * traffic it is based on the packet contents.
389 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER &&
390 wth->pseudo_header.atm.type == TRAF_LANE) {
391 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
392 wth->phdr.caplen, &wth->pseudo_header);
396 * Skip over the padding (don't "fseek()", as the standard
397 * I/O library on some platforms discards buffered data if
398 * you do that, which means it does a lot more reads).
399 * There's probably not much padding (it's probably padded only
400 * to a 4-byte boundary), so we probably need only do one read.
402 padbytes = rec_size - (sizeof hdr + packet_size);
403 while (padbytes != 0) {
404 bytes_to_read = padbytes;
405 if ((unsigned)bytes_to_read > sizeof padbuf)
406 bytes_to_read = sizeof padbuf;
407 errno = WTAP_ERR_CANT_READ;
408 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
409 if (bytes_read != bytes_to_read) {
410 *err = file_error(wth->fh);
412 *err = WTAP_ERR_SHORT_READ;
415 wth->data_offset += bytes_read;
416 padbytes -= bytes_read;
423 snoop_seek_read(wtap *wth, long seek_off,
424 union wtap_pseudo_header *pseudo_header, guchar *pd, int length, int *err)
426 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
429 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
430 if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
438 * Read the packet data.
440 if (!snoop_read_rec_data(wth->random_fh, pd, length, err))
441 return FALSE; /* failed */
444 * If this is ATM LANE traffic, try to guess what type of LANE
445 * traffic it is based on the packet contents.
447 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER &&
448 pseudo_header->atm.type == TRAF_LANE)
449 atm_guess_lane_type(pd, length, pseudo_header);
454 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
457 struct snoop_atm_hdr atm_phdr;
462 errno = WTAP_ERR_CANT_READ;
463 bytes_read = file_read(&atm_phdr, 1, sizeof (struct snoop_atm_hdr), fh);
464 if (bytes_read != sizeof (struct snoop_atm_hdr)) {
465 *err = file_error(fh);
467 *err = WTAP_ERR_SHORT_READ;
472 vci = pntohs(&atm_phdr.vci);
475 * The lower 4 bits of the first byte of the header indicate
476 * the type of traffic, as per the "atmioctl.h" header in
479 switch (atm_phdr.flags & 0x0F) {
481 case 0x01: /* LANE */
482 pseudo_header->atm.aal = AAL_5;
483 pseudo_header->atm.type = TRAF_LANE;
486 case 0x02: /* RFC 1483 LLC multiplexed traffic */
487 pseudo_header->atm.aal = AAL_5;
488 pseudo_header->atm.type = TRAF_LLCMX;
491 case 0x05: /* ILMI */
492 pseudo_header->atm.aal = AAL_5;
493 pseudo_header->atm.type = TRAF_ILMI;
496 case 0x06: /* Signalling AAL */
497 pseudo_header->atm.aal = AAL_SIGNALLING;
498 pseudo_header->atm.type = TRAF_UNKNOWN;
501 case 0x03: /* MARS (RFC 2022) */
502 pseudo_header->atm.aal = AAL_5;
503 pseudo_header->atm.type = TRAF_UNKNOWN;
506 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
507 pseudo_header->atm.aal = AAL_5;
508 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
513 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
514 * case assume it's AAL_SIGNALLING; we know nothing more
517 * XXX - is this necessary? Or are we guaranteed that
518 * all signalling traffic has a type of 0x06?
520 * XXX - is this guaranteed to be AAL5? Or, if the type is
521 * 0x00 ("raw"), might it be non-AAL5 traffic?
523 if (vpi == 0 && vci == 5)
524 pseudo_header->atm.aal = AAL_SIGNALLING;
526 pseudo_header->atm.aal = AAL_5;
527 pseudo_header->atm.type = TRAF_UNKNOWN;
530 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
532 pseudo_header->atm.vpi = vpi;
533 pseudo_header->atm.vci = vci;
534 pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 1 : 0;
536 /* We don't have this information */
537 pseudo_header->atm.cells = 0;
538 pseudo_header->atm.aal5t_u2u = 0;
539 pseudo_header->atm.aal5t_len = 0;
540 pseudo_header->atm.aal5t_chksum = 0;
546 snoop_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
550 errno = WTAP_ERR_CANT_READ;
551 bytes_read = file_read(pd, 1, length, fh);
553 if (bytes_read != length) {
554 *err = file_error(fh);
556 *err = WTAP_ERR_SHORT_READ;
562 static const int wtap_encap[] = {
563 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
564 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
565 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
566 -1, /* WTAP_ENCAP_SLIP -> unsupported */
567 -1, /* WTAP_ENCAP_PPP -> unsupported */
568 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
569 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
570 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
571 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
572 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
573 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
574 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
575 0x12, /* WTAP_ENCAP_ATM_SNIFFER -> DL_IPATM */
576 -1 /* WTAP_ENCAP_NULL -> unsupported */
578 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
580 /* Returns 0 if we could write the specified encapsulation type,
581 an error indication otherwise. */
582 int snoop_dump_can_write_encap(int encap)
584 /* Per-packet encapsulations aren't supported. */
585 if (encap == WTAP_ENCAP_PER_PACKET)
586 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
588 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
589 return WTAP_ERR_UNSUPPORTED_ENCAP;
594 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
596 gboolean snoop_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
598 struct snoop_hdr file_hdr;
601 /* This is a snoop file */
602 wdh->subtype_write = snoop_dump;
603 wdh->subtype_close = NULL;
605 /* Write the file header. */
606 nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
607 if (nwritten != sizeof snoop_magic) {
608 if (nwritten == 0 && ferror(wdh->fh))
611 *err = WTAP_ERR_SHORT_WRITE;
615 /* current "snoop" format is 2 */
616 file_hdr.version = g_htonl(2);
617 file_hdr.network = g_htonl(wtap_encap[wdh->encap]);
618 nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
619 if (nwritten != sizeof file_hdr) {
620 if (nwritten == 0 && ferror(wdh->fh))
623 *err = WTAP_ERR_SHORT_WRITE;
630 /* Write a record for a packet to a dump file.
631 Returns TRUE on success, FALSE on failure. */
632 static gboolean snoop_dump(wtap_dumper *wdh,
633 const struct wtap_pkthdr *phdr,
634 const union wtap_pseudo_header *pseudo_header _U_,
635 const guchar *pd, int *err)
637 struct snooprec_hdr rec_hdr;
641 static char zeroes[4];
642 struct snoop_atm_hdr atm_hdr;
645 if (wdh->encap == WTAP_ENCAP_ATM_SNIFFER)
646 atm_hdrsize = sizeof (struct snoop_atm_hdr);
650 /* Record length = header length plus data length... */
651 reclen = sizeof rec_hdr + phdr->caplen + atm_hdrsize;
653 /* ... plus enough bytes to pad it to a 4-byte boundary. */
654 padlen = ((reclen + 3) & ~3) - reclen;
657 rec_hdr.orig_len = g_htonl(phdr->len + atm_hdrsize);
658 rec_hdr.incl_len = g_htonl(phdr->caplen + atm_hdrsize);
659 rec_hdr.rec_len = g_htonl(reclen);
660 rec_hdr.cum_drops = 0;
661 rec_hdr.ts_sec = g_htonl(phdr->ts.tv_sec);
662 rec_hdr.ts_usec = g_htonl(phdr->ts.tv_usec);
663 nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
664 if (nwritten != sizeof rec_hdr) {
665 if (nwritten == 0 && ferror(wdh->fh))
668 *err = WTAP_ERR_SHORT_WRITE;
672 if (wdh->encap == WTAP_ENCAP_ATM_SNIFFER) {
674 * Write the ATM header.
677 (pseudo_header->atm.channel != 0) ? 0x80 : 0x00;
678 switch (pseudo_header->atm.aal) {
682 atm_hdr.flags |= 0x06;
686 switch (pseudo_header->atm.type) {
690 atm_hdr.flags |= 0x01;
694 /* RFC 1483 LLC multiplexed traffic */
695 atm_hdr.flags |= 0x02;
700 atm_hdr.flags |= 0x05;
705 atm_hdr.vpi = pseudo_header->atm.vpi;
706 atm_hdr.vci = g_htons(pseudo_header->atm.vci);
707 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
708 if (nwritten != sizeof atm_hdr) {
709 if (nwritten == 0 && ferror(wdh->fh))
712 *err = WTAP_ERR_SHORT_WRITE;
717 nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
718 if (nwritten != phdr->caplen) {
719 if (nwritten == 0 && ferror(wdh->fh))
722 *err = WTAP_ERR_SHORT_WRITE;
726 /* Now write the padding. */
727 nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
728 if (nwritten != padlen) {
729 if (nwritten == 0 && ferror(wdh->fh))
732 *err = WTAP_ERR_SHORT_WRITE;