From Joerg Mayer: remove unused variables and declarations of
[obnox/wireshark/wip.git] / wiretap / snoop.c
1 /* snoop.c
2  *
3  * $Id: snoop.c,v 1.41 2002/02/27 08:57:25 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  * 
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.
12  * 
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.
17  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <errno.h>
27 #include <string.h>
28 #include "wtap-int.h"
29 #include "file_wrappers.h"
30 #include "buffer.h"
31 #include "snoop.h"
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
34 #endif
35
36 /* See RFC 1761 for a description of the "snoop" file format. */
37
38 /* Magic number in "snoop" files. */
39 static const char snoop_magic[] = {
40         's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
41 };
42
43 /* "snoop" file header (minus magic number). */
44 struct snoop_hdr {
45         guint32 version;        /* version number (should be 2) */
46         guint32 network;        /* network type */
47 };
48
49 /* "snoop" record header. */
50 struct snooprec_hdr {
51         guint32 orig_len;       /* actual length of packet */
52         guint32 incl_len;       /* number of octets captured in file */
53         guint32 rec_len;        /* length of record */
54         guint32 cum_drops;      /* cumulative number of dropped packets */
55         guint32 ts_sec;         /* timestamp seconds */
56         guint32 ts_usec;        /* timestamp microseconds */
57 };
58
59 static gboolean snoop_read(wtap *wth, int *err, long *data_offset);
60 static int snoop_seek_read(wtap *wth, long seek_off,
61     union wtap_pseudo_header *pseudo_header, u_char *pd, int length);
62 static int snoop_read_atm_pseudoheader(FILE_T fh,
63     union wtap_pseudo_header *pseudo_header, int *err);
64 static int snoop_read_rec_data(FILE_T fh, u_char *pd, int length, int *err);
65 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
66     const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err);
67
68 /*
69  * See
70  * 
71  *      http://www.opengroup.org/onlinepubs/9638599/apdxf.htm
72  *
73  * for the "dlpi.h" header file specified by The Open Group, which lists
74  * the DL_ values for various protocols; Solaris 7 uses the same values.
75  *
76  * The page at
77  *
78  *      http://mrpink.lerc.nasa.gov/118x/support.html
79  *
80  * has links to modified versions of "tcpdump" and "libpcap" for SUNatm
81  * DLPI support; they suggest the 3.0 verson of SUNatm uses those
82  * values.
83  *
84  * It also has a link to "convert.c", which is a program to convert files
85  * from the format written by the "atmsnoop" program that comes with the
86  * SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
87  * to be DL_FDDI (don't ask why).  SunATM 3.0 claims to be DL_IPATM, which
88  * is 0x12".
89  *
90  * It also says that "ATM Mac header is 12 bytes long.", and seems to imply
91  * that in an "atmsnoop" file, the header contains 2 bytes (direction and
92  * VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
93  * type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
94  * of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
95  * pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
96  * 1 byte of VPI, and 2 bytes of VCI.
97  *
98  * The aforementioned page also has a link to some capture files from
99  * "atmsnoop"; this version of "snoop.c" appears to be able to read them.
100  *
101  * Source to an "atmdump" package, which includes a modified version of
102  * "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
103  * also includes "atmdump", which is a modified "tcpdump", says that an
104  * ATM packet handed up from the Sun driver for the Sun SBus ATM card on
105  * Solaris 2.5.1 has 1 byte of direction, 1 byte of VPI, 2 bytes of VCI,
106  * and then the ATM PDU, and suggests that the direction byte is 0x80 for
107  * "transmitted" (presumably meaning DTE->DCE) and presumably not 0x80 for
108  * "received" (presumably meaning DCE->DTE).
109  *
110  * In fact, the "direction" byte appears to have some other stuff, perhaps
111  * a traffic type, in the lower 7 bits, with the 8th bit indicating the
112  * direction.
113  *
114  * I don't know what the encapsulation of any of the other types is, so I
115  * leave them all as WTAP_ENCAP_UNKNOWN.  I also don't know whether "snoop"
116  * can handle any of them (it presumably can't handle ATM, otherwise Sun
117  * wouldn't have supplied "atmsnoop"; even if it can't, this may be useful
118  * reference information for anybody doing code to use DLPI to do raw packet
119  * captures on those network types.
120  *
121  * See
122  *
123  *      http://www.shomiti.com/support/TNCapFileFormat.htm
124  *
125  * for information on Shomiti's mutant flavor of snoop.  For some unknown
126  * unknown reason, they decided not to just Go With The DLPI Flow, and
127  * instead used the types unspecified in RFC 1461 for their own nefarious
128  * purposes, such as distinguishing 10MB from 100MB from 1000MB Ethernet
129  * and distinguishing 4MB from 16MB Token Ring, and distinguishing both
130  * of them from the "Shomiti" versions of same.
131  */
132 int snoop_open(wtap *wth, int *err)
133 {
134         int bytes_read;
135         char magic[sizeof snoop_magic];
136         struct snoop_hdr hdr;
137         static const int snoop_encap[] = {
138                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
139                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
140                 WTAP_ENCAP_TOKEN_RING,
141                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
142                 WTAP_ENCAP_ETHERNET,
143                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
144                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
145                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
146                 WTAP_ENCAP_FDDI_BITSWAPPED,
147                 WTAP_ENCAP_UNKNOWN,     /* Other */
148                 WTAP_ENCAP_UNKNOWN,     /* Frame Relay LAPF */
149                 WTAP_ENCAP_UNKNOWN,     /* Multi-protocol over Frame Relay */
150                 WTAP_ENCAP_UNKNOWN,     /* Character Async (e.g., SLIP and PPP?) */
151                 WTAP_ENCAP_UNKNOWN,     /* X.25 Classical IP */
152                 WTAP_ENCAP_UNKNOWN,     /* software loopback */
153                 WTAP_ENCAP_UNKNOWN,     /* not defined in "dlpi.h" */
154                 WTAP_ENCAP_UNKNOWN,     /* Fibre Channel */
155                 WTAP_ENCAP_UNKNOWN,     /* ATM */
156                 WTAP_ENCAP_ATM_SNIFFER, /* ATM Classical IP */
157                 WTAP_ENCAP_UNKNOWN,     /* X.25 LAPB */
158                 WTAP_ENCAP_UNKNOWN,     /* ISDN */
159                 WTAP_ENCAP_UNKNOWN,     /* HIPPI */
160                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Ethernet */
161                 WTAP_ENCAP_UNKNOWN,     /* 100VG-AnyLAN Token Ring */
162                 WTAP_ENCAP_UNKNOWN,     /* "ISO 8802/3 and Ethernet" */
163                 WTAP_ENCAP_UNKNOWN,     /* 100BaseT (but that's just Ethernet) */
164         };
165         #define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
166         static const int shomiti_encap[] = {
167                 WTAP_ENCAP_ETHERNET,    /* IEEE 802.3 */
168                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.4 Token Bus */
169                 WTAP_ENCAP_TOKEN_RING,
170                 WTAP_ENCAP_UNKNOWN,     /* IEEE 802.6 Metro Net */
171                 WTAP_ENCAP_ETHERNET,
172                 WTAP_ENCAP_UNKNOWN,     /* HDLC */
173                 WTAP_ENCAP_UNKNOWN,     /* Character Synchronous, e.g. bisync */
174                 WTAP_ENCAP_UNKNOWN,     /* IBM Channel-to-Channel */
175                 WTAP_ENCAP_FDDI_BITSWAPPED,
176                 WTAP_ENCAP_UNKNOWN,     /* Other */
177                 WTAP_ENCAP_ETHERNET,    /* Fast Ethernet */
178                 WTAP_ENCAP_TOKEN_RING,  /* 4MB 802.5 token ring */
179                 WTAP_ENCAP_ETHERNET,    /* Gigabit Ethernet */
180                 WTAP_ENCAP_TOKEN_RING,  /* "IEEE 802.5 Shomiti" */
181                 WTAP_ENCAP_TOKEN_RING,  /* "4MB IEEE 802.5 Shomiti" */
182         };
183         #define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
184         int file_encap;
185
186         /* Read in the string that should be at the start of a "snoop" file */
187         errno = WTAP_ERR_CANT_READ;
188         bytes_read = file_read(magic, 1, sizeof magic, wth->fh);
189         if (bytes_read != sizeof magic) {
190                 *err = file_error(wth->fh);
191                 if (*err != 0)
192                         return -1;
193                 return 0;
194         }
195         wth->data_offset += sizeof magic;
196
197         if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
198                 return 0;
199         }
200
201         /* Read the rest of the header. */
202         errno = WTAP_ERR_CANT_READ;
203         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
204         if (bytes_read != sizeof hdr) {
205                 *err = file_error(wth->fh);
206                 if (*err != 0)
207                         return -1;
208                 return 0;
209         }
210         wth->data_offset += sizeof hdr;
211
212         /*
213          * Oh, this is lovely.
214          *
215          * I suppose Shomiti could give a bunch of lawyerly noise about
216          * how "well, RFC 1761 said they were unassigned, and that's
217          * the standard, not the DLPI header file, so it's perfectly OK
218          * for us to use them, blah blah blah", but it's still irritating
219          * as hell that they used the unassigned-in-RFC-1761 values for
220          * their own purposes - especially given that Sun also used
221          * one of them in atmsnoop.
222          *
223          * For now, we treat a version number of 2 as indicating that
224          * this is a Sun snoop file, and version numbers of 3, 4, and 5
225          * as indicating that this is a Shomiti file, even though
226          * their capture file format documentation claims that they
227          * use 2 if the data "was captured using an NDIS card", which
228          * presumably means "captured with an ordinary boring network
229          * card via NDIS" as opposed to "captured with our whizzo
230          * special capture hardware".
231          *
232          * This runs the risk that we may misinterpret the network
233          * type of Shomiti captures not done using their hardware.
234          * Currently, the only not-in-RFC-1761 type we interpret in
235          * Sun snoop files is 18, for atmsnoop, and that's not used
236          * by Shomiti, but if any of the types used by Shomiti are
237          * also used by Snoop or a variant thereof - e.g.:
238          *
239          *      value   snoop                   Shomiti
240          *      10      Frame Relay             100MB Ethernet
241          *      11      MP over Frame Relay     4MB 802.5
242          *      12      "Character Async"       1000MB Ethernet
243          *      13      X.25 Classical IP       "IEEE 802.5 Shomiti"
244          *      14      "software loopback"     "4MB IEEE 802.5 Shomiti"
245          *
246          * then we have a problem that may be resolvable only by checking
247          * how much padding there is in the first packet - if there're 3
248          * bytes or less, it's probably Sun snoop, which uses the padding
249          * only for padding, but if there's more, it's probably a Shomiti
250          * tool, which uses the padding for additional information.
251          */
252         hdr.version = ntohl(hdr.version);
253         hdr.network = ntohl(hdr.network);
254         switch (hdr.version) {
255
256         case 2:         /* Solaris 2.x and later snoop, and Shomiti
257                            Surveyor prior to 3.0 (or 3.x with NDIS card?) */
258                 if (hdr.network >= NUM_SNOOP_ENCAPS
259                     || snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
260                         g_message("snoop: network type %u unknown or unsupported",
261                             hdr.network);
262                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
263                         return -1;
264                 }
265                 file_encap = snoop_encap[hdr.network];
266                 break;
267
268         case 3:         /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
269         case 4:         /* Surveyor 3.0 and later, with Shomiti GAM hardware */
270         case 5:         /* Surveyor 3.0 and later, with Shomiti THG hardware */
271                 if (hdr.network >= NUM_SHOMITI_ENCAPS
272                     || shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
273                         g_message("snoop: Shomiti network type %u unknown or unsupported",
274                             hdr.network);
275                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
276                         return -1;
277                 }
278                 file_encap = shomiti_encap[hdr.network];
279                 break;
280
281         default:
282                 g_message("snoop: version %u unsupported", hdr.version);
283                 *err = WTAP_ERR_UNSUPPORTED;
284                 return -1;
285         }
286
287         /* This is a snoop file */
288         wth->file_type = WTAP_FILE_SNOOP;
289         wth->subtype_read = snoop_read;
290         wth->subtype_seek_read = snoop_seek_read;
291         wth->file_encap = file_encap;
292         wth->snapshot_length = 0;       /* not available in header */
293         return 1;
294 }
295
296 /* Read the next packet */
297 static gboolean snoop_read(wtap *wth, int *err, long *data_offset)
298 {
299         guint32 rec_size;
300         guint32 packet_size;
301         guint32 orig_size;
302         int     bytes_read;
303         struct snooprec_hdr hdr;
304         char    padbuf[4];
305         int     padbytes;
306         int     bytes_to_read;
307
308         /* Read record header. */
309         errno = WTAP_ERR_CANT_READ;
310         bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
311         if (bytes_read != sizeof hdr) {
312                 *err = file_error(wth->fh);
313                 if (*err == 0 && bytes_read != 0) {
314                         *err = WTAP_ERR_SHORT_READ;
315                 }
316                 return FALSE;
317         }
318         wth->data_offset += sizeof hdr;
319
320         rec_size = ntohl(hdr.rec_len);
321         orig_size = ntohl(hdr.orig_len);
322         packet_size = ntohl(hdr.incl_len);
323         if (packet_size > WTAP_MAX_PACKET_SIZE) {
324                 /*
325                  * Probably a corrupt capture file; don't blow up trying
326                  * to allocate space for an immensely-large packet.
327                  */
328                 g_message("snoop: File has %u-byte packet, bigger than maximum of %u",
329                     packet_size, WTAP_MAX_PACKET_SIZE);
330                 *err = WTAP_ERR_BAD_RECORD;
331                 return FALSE;
332         }
333
334         *data_offset = wth->data_offset;
335
336         /*
337          * If this is an ATM packet, the first four bytes are the
338          * direction of the packet (transmit/receive), the VPI, and
339          * the VCI; read them and generate the pseudo-header from
340          * them.
341          */
342         if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
343                 if (packet_size < 4) {
344                         /*
345                          * Uh-oh, the packet isn't big enough to even
346                          * have a pseudo-header.
347                          */
348                         g_message("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header\n",
349                             packet_size);
350                         *err = WTAP_ERR_BAD_RECORD;
351                         return FALSE;
352                 }
353                 if (snoop_read_atm_pseudoheader(wth->fh, &wth->pseudo_header,
354                     err) < 0)
355                         return FALSE;   /* Read error */
356
357                 /*
358                  * Don't count the pseudo-header as part of the packet.
359                  */
360                 rec_size -= 4;
361                 orig_size -= 4;
362                 packet_size -= 4;
363                 wth->data_offset += 4;
364         }
365
366         buffer_assure_space(wth->frame_buffer, packet_size);
367         if (snoop_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
368             packet_size, err) < 0)
369                 return FALSE;   /* Read error */
370         wth->data_offset += packet_size;
371
372         wth->phdr.ts.tv_sec = ntohl(hdr.ts_sec);
373         wth->phdr.ts.tv_usec = ntohl(hdr.ts_usec);
374         wth->phdr.caplen = packet_size;
375         wth->phdr.len = orig_size;
376         wth->phdr.pkt_encap = wth->file_encap;
377
378         /*
379          * Skip over the padding (don't "fseek()", as the standard
380          * I/O library on some platforms discards buffered data if
381          * you do that, which means it does a lot more reads).
382          * There's probably not much padding (it's probably padded only
383          * to a 4-byte boundary), so we probably need only do one read.
384          */
385         padbytes = rec_size - (sizeof hdr + packet_size);
386         while (padbytes != 0) {
387                 bytes_to_read = padbytes;
388                 if ((unsigned)bytes_to_read > sizeof padbuf)
389                         bytes_to_read = sizeof padbuf;
390                 errno = WTAP_ERR_CANT_READ;
391                 bytes_read = file_read(padbuf, 1, bytes_to_read, wth->fh);
392                 if (bytes_read != bytes_to_read) {
393                         *err = file_error(wth->fh);
394                         if (*err == 0)
395                                 *err = WTAP_ERR_SHORT_READ;
396                         return FALSE;
397                 }
398                 wth->data_offset += bytes_read;
399                 padbytes -= bytes_read;
400         }
401
402         return TRUE;
403 }
404
405 static int
406 snoop_seek_read(wtap *wth, long seek_off,
407     union wtap_pseudo_header *pseudo_header, u_char *pd, int length)
408 {
409         int     ret;
410         int     err;            /* XXX - return this */
411
412         file_seek(wth->random_fh, seek_off, SEEK_SET);
413
414         if (wth->file_encap == WTAP_ENCAP_ATM_SNIFFER) {
415                 ret = snoop_read_atm_pseudoheader(wth->random_fh, pseudo_header,
416                     &err);
417                 if (ret < 0) {
418                         /* Read error */
419                         return ret;
420                 }
421         }
422
423         /*
424          * Read the packet data.
425          */
426         return snoop_read_rec_data(wth->random_fh, pd, length, &err);
427 }
428
429 static int
430 snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
431     int *err)
432 {
433         char    atm_phdr[4];
434         int     bytes_read;
435
436         errno = WTAP_ERR_CANT_READ;
437         bytes_read = file_read(atm_phdr, 1, 4, fh);
438         if (bytes_read != 4) {
439                 *err = file_error(fh);
440                 if (*err == 0)
441                         *err = WTAP_ERR_SHORT_READ;
442                 return -1;
443         }
444
445         pseudo_header->ngsniffer_atm.channel = (atm_phdr[0] & 0x80) ? 1 : 0;
446         pseudo_header->ngsniffer_atm.Vpi = atm_phdr[1];
447         pseudo_header->ngsniffer_atm.Vci = pntohs(&atm_phdr[2]);
448
449         /* We don't have this information */
450         pseudo_header->ngsniffer_atm.cells = 0;
451         pseudo_header->ngsniffer_atm.aal5t_u2u = 0;
452         pseudo_header->ngsniffer_atm.aal5t_len = 0;
453         pseudo_header->ngsniffer_atm.aal5t_chksum = 0;
454
455         /*
456          * Assume it's AAL5; we know nothing more about it.
457          *
458          * For what it's worth, in one "atmsnoop" capture,
459          * the lower 7 bits of the first byte of the header
460          * were 0x05 for ILMI traffic, 0x06 for Signalling
461          * AAL traffic, and 0x02 for at least some RFC 1483-style
462          * LLC multiplexed traffic.
463          */
464         pseudo_header->ngsniffer_atm.AppTrafType = ATT_AAL5|ATT_HL_UNKNOWN;
465         pseudo_header->ngsniffer_atm.AppHLType = AHLT_UNKNOWN;
466
467         return 0;
468 }
469
470 static int
471 snoop_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
472 {
473         int     bytes_read;
474
475         errno = WTAP_ERR_CANT_READ;
476         bytes_read = file_read(pd, 1, length, fh);
477
478         if (bytes_read != length) {
479                 *err = file_error(fh);
480                 if (*err == 0)
481                         *err = WTAP_ERR_SHORT_READ;
482                 return -1;
483         }
484         return 0;
485 }
486
487 static const int wtap_encap[] = {
488         -1,             /* WTAP_ENCAP_UNKNOWN -> unsupported */
489         0x04,           /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
490         0x02,           /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
491         -1,             /* WTAP_ENCAP_SLIP -> unsupported */
492         -1,             /* WTAP_ENCAP_PPP -> unsupported */
493         0x08,           /* WTAP_ENCAP_FDDI -> DL_FDDI */
494         0x08,           /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
495         -1,             /* WTAP_ENCAP_RAW_IP -> unsupported */
496         -1,             /* WTAP_ENCAP_ARCNET -> unsupported */
497         -1,             /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
498         -1,             /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
499         -1,             /* WTAP_ENCAP_LAPB -> unsupported*/
500         -1,             /* WTAP_ENCAP_ATM_SNIFFER -> unsupported */
501         0               /* WTAP_ENCAP_NULL -> DLT_NULL */
502 };
503 #define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
504
505 /* Returns 0 if we could write the specified encapsulation type,
506    an error indication otherwise. */
507 int snoop_dump_can_write_encap(int encap)
508 {
509         /* Per-packet encapsulations aren't supported. */
510         if (encap == WTAP_ENCAP_PER_PACKET)
511                 return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
512
513         if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
514                 return WTAP_ERR_UNSUPPORTED_ENCAP;
515
516         return 0;
517 }
518
519 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
520    failure */
521 gboolean snoop_dump_open(wtap_dumper *wdh, int *err)
522 {
523         struct snoop_hdr file_hdr;
524         size_t nwritten;
525
526         /* This is a snoop file */
527         wdh->subtype_write = snoop_dump;
528         wdh->subtype_close = NULL;
529
530         /* Write the file header. */
531         nwritten = fwrite(&snoop_magic, 1, sizeof snoop_magic, wdh->fh);
532         if (nwritten != sizeof snoop_magic) {
533                 if (nwritten == 0 && ferror(wdh->fh))
534                         *err = errno;
535                 else
536                         *err = WTAP_ERR_SHORT_WRITE;
537                 return FALSE;
538         }
539
540         /* current "snoop" format is 2 */
541         file_hdr.version = htonl(2);
542         file_hdr.network = htonl(wtap_encap[wdh->encap]);
543         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
544         if (nwritten != sizeof file_hdr) {
545                 if (nwritten == 0 && ferror(wdh->fh))
546                         *err = errno;
547                 else
548                         *err = WTAP_ERR_SHORT_WRITE;
549                 return FALSE;
550         }
551
552         return TRUE;
553 }
554
555 /* Write a record for a packet to a dump file.
556    Returns TRUE on success, FALSE on failure. */
557 static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
558     const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err)
559 {
560         struct snooprec_hdr rec_hdr;
561         size_t nwritten;
562         int reclen;
563         guint padlen;
564         static char zeroes[4];
565
566         /* Record length = header length plus data length... */
567         reclen = sizeof rec_hdr + phdr->caplen;
568
569         /* ... plus enough bytes to pad it to a 4-byte boundary. */
570         padlen = ((reclen + 3) & ~3) - reclen;
571         reclen += padlen;
572
573         rec_hdr.orig_len = htonl(phdr->len);
574         rec_hdr.incl_len = htonl(phdr->caplen);
575         rec_hdr.rec_len = htonl(reclen);
576         rec_hdr.cum_drops = 0;
577         rec_hdr.ts_sec = htonl(phdr->ts.tv_sec);
578         rec_hdr.ts_usec = htonl(phdr->ts.tv_usec);
579         nwritten = fwrite(&rec_hdr, 1, sizeof rec_hdr, wdh->fh);
580         if (nwritten != sizeof rec_hdr) {
581                 if (nwritten == 0 && ferror(wdh->fh))
582                         *err = errno;
583                 else
584                         *err = WTAP_ERR_SHORT_WRITE;
585                 return FALSE;
586         }
587         nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
588         if (nwritten != phdr->caplen) {
589                 if (nwritten == 0 && ferror(wdh->fh))
590                         *err = errno;
591                 else
592                         *err = WTAP_ERR_SHORT_WRITE;
593                 return FALSE;
594         }
595
596         /* Now write the padding. */
597         nwritten = fwrite(zeroes, 1, padlen, wdh->fh);
598         if (nwritten != padlen) {
599                 if (nwritten == 0 && ferror(wdh->fh))
600                         *err = errno;
601                 else
602                         *err = WTAP_ERR_SHORT_WRITE;
603                 return FALSE;
604         }
605         return TRUE;
606 }