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