3 * $Id: snoop.c,v 1.51 2002/05/26 21:32:39 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 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
37 /* See RFC 1761 for a description of the "snoop" file format. */
39 /* Magic number in "snoop" files. */
40 static const char snoop_magic[] = {
41 's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
44 /* "snoop" file header (minus magic number). */
46 guint32 version; /* version number (should be 2) */
47 guint32 network; /* network type */
50 /* "snoop" record header. */
52 guint32 orig_len; /* actual length of packet */
53 guint32 incl_len; /* number of octets captured in file */
54 guint32 rec_len; /* length of record */
55 guint32 cum_drops; /* cumulative number of dropped packets */
56 guint32 ts_sec; /* timestamp seconds */
57 guint32 ts_usec; /* timestamp microseconds */
61 * The link-layer header on ATM packets.
63 struct snoop_atm_hdr {
64 guint8 flags; /* destination and traffic type */
66 guint16 vci; /* VCI */
69 static gboolean snoop_read(wtap *wth, int *err, long *data_offset);
70 static gboolean snoop_seek_read(wtap *wth, long seek_off,
71 union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err);
72 static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
73 union wtap_pseudo_header *pseudo_header, int *err);
74 static gboolean snoop_read_rec_data(FILE_T fh, u_char *pd, int length,
76 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
77 const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err);
82 * http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
84 * for the "dlpi.h" header file specified by The Open Group, which lists
85 * the DL_ values for various protocols; Solaris 7 uses the same values.
89 * http://mrpink.lerc.nasa.gov/118x/support.html
91 * has links to modified versions of "tcpdump" and "libpcap" for SUNatm
92 * DLPI support; they suggest the 3.0 verson of SUNatm uses those
95 * It also has a link to "convert.c", which is a program to convert files
96 * from the format written by the "atmsnoop" program that comes with the
97 * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
98 * to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
101 * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
102 * that in an "atmsnoop" file, the header contains 2 bytes (direction and
103 * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
104 * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
105 * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
106 * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
107 * 1 byte of VPI, and 2 bytes of VCI.
109 * The aforementioned page also has a link to some capture files from
110 * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
112 * Source to an "atmdump" package, which includes a modified version of
113 * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
114 * also includes "atmdump", which is a modified "tcpdump", says that an
115 * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
116 * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
117 * and then the ATM PDU, and suggests that the direction byte is 0x80 for
118 * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
119 * "received" (presumably meaning DCE->DTE).
121 * In fact, the "direction" byte appears to have some other stuff, perhaps
122 * a traffic type, in the lower 7 bits, with the 8th bit indicating the
125 * I don't know what the encapsulation of any of the other types is, so I
126 * leave them all as WTAP_ENCAP_UNKNOWN. I also don't know whether "snoop"
127 * can handle any of them (it presumably can't handle ATM, otherwise Sun
128 * wouldn't have supplied "atmsnoop"; even if it can't, this may be useful
129 * reference information for anybody doing code to use DLPI to do raw packet
130 * captures on those network types.
134 * http://www.shomiti.com/support/TNCapFileFormat.htm
136 * for information on Shomiti's mutant flavor of snoop. For some unknown
137 * unknown reason, they decided not to just Go With The DLPI Flow, and
138 * instead used the types unspecified in RFC 1461 for their own nefarious
139 * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
140 * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
141 * of them from the "Shomiti" versions of same.
143 int snoop_open(wtap *wth, int *err)
146 char magic[sizeof snoop_magic];
147 struct snoop_hdr hdr;
148 static const int snoop_encap[] = {
149 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
150 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
151 WTAP_ENCAP_TOKEN_RING,
152 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
154 WTAP_ENCAP_UNKNOWN, /* HDLC */
155 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
156 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
157 WTAP_ENCAP_FDDI_BITSWAPPED,
158 WTAP_ENCAP_UNKNOWN, /* Other */
159 WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
160 WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
161 WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
162 WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
163 WTAP_ENCAP_UNKNOWN, /* software loopback */
164 WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
165 WTAP_ENCAP_UNKNOWN, /* Fibre Channel */
166 WTAP_ENCAP_UNKNOWN, /* ATM */
167 WTAP_ENCAP_ATM_SNIFFER, /* ATM Classical IP */
168 WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
169 WTAP_ENCAP_UNKNOWN, /* ISDN */
170 WTAP_ENCAP_UNKNOWN, /* HIPPI */
171 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
172 WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
173 WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
174 WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
176 #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
177 static const int shomiti_encap[] = {
178 WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
179 WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
180 WTAP_ENCAP_TOKEN_RING,
181 WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
183 WTAP_ENCAP_UNKNOWN, /* HDLC */
184 WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
185 WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
186 WTAP_ENCAP_FDDI_BITSWAPPED,
187 WTAP_ENCAP_UNKNOWN, /* Other */
188 WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
189 WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
190 WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
191 WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
192 WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
194 #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
197 /* Read in the string that should be at the start of a "snoop" file */
198 errno = WTAP_ERR_CANT_READ;
199 bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
200 if (bytes_read != sizeof magic) {
201 *err = file_error(wth->fh);
206 wth->data_offset += sizeof magic;
208 if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
212 /* Read the rest of the header. */
213 errno = WTAP_ERR_CANT_READ;
214 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
215 if (bytes_read != sizeof hdr) {
216 *err = file_error(wth->fh);
221 wth->data_offset += sizeof hdr;
224 * Oh, this is lovely.
226 * I suppose Shomiti could give a bunch of lawyerly noise about
227 * how "well, RFC 1761 said they were unassigned, and that's
228 * the standard, not the DLPI header file, so it's perfectly OK
229 * for us to use them, blah blah blah", but it's still irritating
230 * as hell that they used the unassigned-in-RFC-1761 values for
231 * their own purposes - especially given that Sun also used
232 * one of them in atmsnoop.
234 * For now, we treat a version number of 2 as indicating that
235 * this is a Sun snoop file, and version numbers of 3, 4, and 5
236 * as indicating that this is a Shomiti file, even though
237 * their capture file format documentation claims that they
238 * use 2 if the data "was captured using an NDIS card", which
239 * presumably means "captured with an ordinary boring network
240 * card via NDIS" as opposed to "captured with our whizzo
241 * special capture hardware".
243 * This runs the risk that we may misinterpret the network
244 * type of Shomiti captures not done using their hardware.
245 * Currently, the only not-in-RFC-1761 type we interpret in
246 * Sun snoop files is 18, for atmsnoop, and that's not used
247 * by Shomiti, but if any of the types used by Shomiti are
248 * also used by Snoop or a variant thereof - e.g.:
250 * value snoop Shomiti
251 * 10 Frame Relay 100MB Ethernet
252 * 11 MP over Frame Relay 4MB 802.5
253 * 12 "Character Async" 1000MB Ethernet
254 * 13 X.25 Classical IP "IEEE 802.5 Shomiti"
255 * 14 "software loopback" "4MB IEEE 802.5 Shomiti"
257 * then we have a problem that may be resolvable only by checking
258 * how much padding there is in the first packet - if there're 3
259 * bytes or less, it's probably Sun snoop, which uses the padding
260 * only for padding, but if there's more, it's probably a Shomiti
261 * tool, which uses the padding for additional information.
263 hdr.version = ntohl(hdr.version);
264 hdr.network = ntohl(hdr.network);
265 switch (hdr.version) {
267 case 2: /* Solaris 2.x and later snoop, and Shomiti
268 Surveyor prior to 3.0 (or 3.x with NDIS card?) */
269 if (hdr.network >= NUM_SNOOP_ENCAPS
270 || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
271 g_message("snoop: network type %u unknown or unsupported",
273 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
276 file_encap = snoop_encap[hdr.network];
279 case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
280 case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
281 case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
282 if (hdr.network >= NUM_SHOMITI_ENCAPS
283 || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
284 g_message("snoop: Shomiti network type %u unknown or unsupported",
286 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
289 file_encap = shomiti_encap[hdr.network];
293 g_message("snoop: version %u unsupported", hdr.version);
294 *err = WTAP_ERR_UNSUPPORTED;
298 /* This is a snoop file */
299 wth->file_type = WTAP_FILE_SNOOP;
300 wth->subtype_read = snoop_read;
301 wth->subtype_seek_read = snoop_seek_read;
302 wth->file_encap = file_encap;
303 wth->snapshot_length = 0; /* not available in header */
307 /* Read the next packet */
308 static gboolean snoop_read(wtap *wth, int *err, long *data_offset)
314 struct snooprec_hdr hdr;
319 /* Read record header. */
320 errno = WTAP_ERR_CANT_READ;
321 bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
322 if (bytes_read != sizeof hdr) {
323 *err = file_error(wth->fh);
324 if (*err == 0 && bytes_read != 0) {
325 *err = WTAP_ERR_SHORT_READ;
329 wth->data_offset += sizeof hdr;
331 rec_size = ntohl(hdr.rec_len);
332 orig_size = ntohl(hdr.orig_len);
333 packet_size = ntohl(hdr.incl_len);
334 if (packet_size > WTAP_MAX_PACKET_SIZE) {
336 * Probably a corrupt capture file; don't blow up trying
337 * to allocate space for an immensely-large packet.
339 g_message("snoop: File has %u-byte packet, bigger than maximum of %u",
340 packet_size, WTAP_MAX_PACKET_SIZE);
341 *err = WTAP_ERR_BAD_RECORD;
345 *data_offset = wth->data_offset;
348 * If this is an ATM packet, the first four bytes are the
349 * direction of the packet (transmit/receive), the VPI, and
350 * the VCI; read them and generate the pseudo-header from
353 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
354 if (packet_size < sizeof (struct snoop_atm_hdr)) {
356 * Uh-oh, the packet isn't big enough to even
357 * have a pseudo-header.
359 g_message("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
361 *err = WTAP_ERR_BAD_RECORD;
364 if (!snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
366 return FALSE; /* Read error */
369 * Don't count the pseudo-header as part of the packet.
371 rec_size -= sizeof (struct snoop_atm_hdr);
372 orig_size -= sizeof (struct snoop_atm_hdr);
373 packet_size -= sizeof (struct snoop_atm_hdr);
374 wth->data_offset += sizeof (struct snoop_atm_hdr);
377 buffer_assure_space(wth->frame_buffer, packet_size);
378 if (!snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
380 return FALSE; /* Read error */
381 wth->data_offset += packet_size;
383 wth->phdr.ts.tv_sec = ntohl(hdr.ts_sec);
384 wth->phdr.ts.tv_usec = ntohl(hdr.ts_usec);
385 wth->phdr.caplen = packet_size;
386 wth->phdr.len = orig_size;
387 wth->phdr.pkt_encap = wth->file_encap;
390 * If this is ATM LANE traffic, try to guess what type of LANE
391 * traffic it is based on the packet contents.
393 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER &&
394 wth->pseudo_header.atm.type == TRAF_LANE) {
395 atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
396 wth->phdr.caplen, &wth->pseudo_header);
400 * Skip over the padding (don't "fseek()", as the standard
401 * I/O library on some platforms discards buffered data if
402 * you do that, which means it does a lot more reads).
403 * There's probably not much padding (it's probably padded only
404 * to a 4-byte boundary), so we probably need only do one read.
406 padbytes = rec_size - (sizeof hdr + packet_size);
407 while (padbytes != 0) {
408 bytes_to_read = padbytes;
409 if ((unsigned)bytes_to_read > sizeof padbuf)
410 bytes_to_read = sizeof padbuf;
411 errno = WTAP_ERR_CANT_READ;
412 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
413 if (bytes_read != bytes_to_read) {
414 *err = file_error(wth->fh);
416 *err = WTAP_ERR_SHORT_READ;
419 wth->data_offset += bytes_read;
420 padbytes -= bytes_read;
427 snoop_seek_read(wtap *wth, long seek_off,
428 union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err)
430 if (file_seek(wth->random_fh, seek_off, SEEK_SET) == -1) {
431 *err = file_error(wth->random_fh);
435 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
436 if (!snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
444 * Read the packet data.
446 if (!snoop_read_rec_data(wth->random_fh, pd, length, err))
447 return FALSE; /* failed */
450 * If this is ATM LANE traffic, try to guess what type of LANE
451 * traffic it is based on the packet contents.
453 if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER &&
454 pseudo_header->atm.type == TRAF_LANE)
455 atm_guess_lane_type(pd, length, pseudo_header);
460 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
463 struct snoop_atm_hdr atm_phdr;
468 errno = WTAP_ERR_CANT_READ;
469 bytes_read = file_read(&atm_phdr, 1, sizeof (struct snoop_atm_hdr), fh);
470 if (bytes_read != sizeof (struct snoop_atm_hdr)) {
471 *err = file_error(fh);
473 *err = WTAP_ERR_SHORT_READ;
478 vci = pntohs(&atm_phdr.vci);
481 * The lower 4 bits of the first byte of the header indicate
482 * the type of traffic, as per the "atmioctl.h" header in
485 switch (atm_phdr.flags & 0x0F) {
487 case 0x01: /* LANE */
488 pseudo_header->atm.aal = AAL_5;
489 pseudo_header->atm.type = TRAF_LANE;
492 case 0x02: /* RFC 1483 LLC multiplexed traffic */
493 pseudo_header->atm.aal = AAL_5;
494 pseudo_header->atm.type = TRAF_LLCMX;
497 case 0x05: /* ILMI */
498 pseudo_header->atm.aal = AAL_5;
499 pseudo_header->atm.type = TRAF_ILMI;
502 case 0x06: /* Signalling AAL */
503 pseudo_header->atm.aal = AAL_SIGNALLING;
504 pseudo_header->atm.type = TRAF_UNKNOWN;
507 case 0x03: /* MARS (RFC 2022) */
508 pseudo_header->atm.aal = AAL_5;
509 pseudo_header->atm.type = TRAF_UNKNOWN;
512 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
513 pseudo_header->atm.aal = AAL_5;
514 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
519 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
520 * case assume it's AAL_SIGNALLING; we know nothing more
523 * XXX - is this necessary? Or are we guaranteed that
524 * all signalling traffic has a type of 0x06?
526 * XXX - is this guaranteed to be AAL5? Or, if the type is
527 * 0x00 ("raw"), might it be non-AAL5 traffic?
529 if (vpi == 0 && vci == 5)
530 pseudo_header->atm.aal = AAL_SIGNALLING;
532 pseudo_header->atm.aal = AAL_5;
533 pseudo_header->atm.type = TRAF_UNKNOWN;
536 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
538 pseudo_header->atm.vpi = vpi;
539 pseudo_header->atm.vci = vci;
540 pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 1 : 0;
542 /* We don't have this information */
543 pseudo_header->atm.cells = 0;
544 pseudo_header->atm.aal5t_u2u = 0;
545 pseudo_header->atm.aal5t_len = 0;
546 pseudo_header->atm.aal5t_chksum = 0;
552 snoop_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
556 errno = WTAP_ERR_CANT_READ;
557 bytes_read = file_read(pd, 1, length, fh);
559 if (bytes_read != length) {
560 *err = file_error(fh);
562 *err = WTAP_ERR_SHORT_READ;
568 static const int wtap_encap[] = {
569 -1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
570 0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
571 0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
572 -1, /* WTAP_ENCAP_SLIP -> unsupported */
573 -1, /* WTAP_ENCAP_PPP -> unsupported */
574 0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
575 0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
576 -1, /* WTAP_ENCAP_RAW_IP -> unsupported */
577 -1, /* WTAP_ENCAP_ARCNET -> unsupported */
578 -1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
579 -1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
580 -1, /* WTAP_ENCAP_LAPB -> unsupported*/
581 0x12, /* WTAP_ENCAP_ATM_SNIFFER -> DL_IPATM */
582 -1 /* WTAP_ENCAP_NULL -> unsupported */
584 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
586 /* Returns 0 if we could write the specified encapsulation type,
587 an error indication otherwise. */
588 int snoop_dump_can_write_encap(int encap)
590 /* Per-packet encapsulations aren't supported. */
591 if (encap == WTAP_ENCAP_PER_PACKET)
592 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
594 if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
595 return WTAP_ERR_UNSUPPORTED_ENCAP;
600 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
602 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
604 struct snoop_hdr file_hdr;
607 /* This is a snoop file */
608 wdh->subtype_write = snoop_dump;
609 wdh->subtype_close = NULL;
611 /* Write the file header. */
612 nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
613 if (nwritten != sizeof snoop_magic) {
614 if (nwritten == 0 && ferror(wdh->fh))
617 *err = WTAP_ERR_SHORT_WRITE;
621 /* current "snoop" format is 2 */
622 file_hdr.version = htonl(2);
623 file_hdr.network = htonl(wtap_encap[wdh->encap]);
624 nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
625 if (nwritten != sizeof file_hdr) {
626 if (nwritten == 0 && ferror(wdh->fh))
629 *err = WTAP_ERR_SHORT_WRITE;
636 /* Write a record for a packet to a dump file.
637 Returns TRUE on success, FALSE on failure. */
638 static gboolean snoop_dump(wtap_dumper *wdh,
639 const struct wtap_pkthdr *phdr,
640 const union wtap_pseudo_header *pseudo_header _U_,
641 const u_char *pd, int *err)
643 struct snooprec_hdr rec_hdr;
647 static char zeroes[4];
648 struct snoop_atm_hdr atm_hdr;
651 if (wdh->encap == WTAP_ENCAP_ATM_SNIFFER)
652 atm_hdrsize = sizeof (struct snoop_atm_hdr);
656 /* Record length = header length plus data length... */
657 reclen = sizeof rec_hdr + phdr->caplen + atm_hdrsize;
659 /* ... plus enough bytes to pad it to a 4-byte boundary. */
660 padlen = ((reclen + 3) & ~3) - reclen;
663 rec_hdr.orig_len = htonl(phdr->len + atm_hdrsize);
664 rec_hdr.incl_len = htonl(phdr->caplen + atm_hdrsize);
665 rec_hdr.rec_len = htonl(reclen);
666 rec_hdr.cum_drops = 0;
667 rec_hdr.ts_sec = htonl(phdr->ts.tv_sec);
668 rec_hdr.ts_usec = htonl(phdr->ts.tv_usec);
669 nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
670 if (nwritten != sizeof rec_hdr) {
671 if (nwritten == 0 && ferror(wdh->fh))
674 *err = WTAP_ERR_SHORT_WRITE;
678 if (wdh->encap == WTAP_ENCAP_ATM_SNIFFER) {
680 * Write the ATM header.
683 (pseudo_header->atm.channel != 0) ? 0x80 : 0x00;
684 switch (pseudo_header->atm.aal) {
688 atm_hdr.flags |= 0x06;
692 switch (pseudo_header->atm.type) {
696 atm_hdr.flags |= 0x01;
700 /* RFC 1483 LLC multiplexed traffic */
701 atm_hdr.flags |= 0x02;
706 atm_hdr.flags |= 0x05;
711 atm_hdr.vpi = pseudo_header->atm.vpi;
712 atm_hdr.vci = htons(pseudo_header->atm.vci);
713 nwritten = fwrite(&atm_hdr, 1, sizeof atm_hdr, wdh->fh);
714 if (nwritten != sizeof atm_hdr) {
715 if (nwritten == 0 && ferror(wdh->fh))
718 *err = WTAP_ERR_SHORT_WRITE;
723 nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
724 if (nwritten != phdr->caplen) {
725 if (nwritten == 0 && ferror(wdh->fh))
728 *err = WTAP_ERR_SHORT_WRITE;
732 /* Now write the padding. */
733 nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
734 if (nwritten != padlen) {
735 if (nwritten == 0 && ferror(wdh->fh))
738 *err = WTAP_ERR_SHORT_WRITE;