Add to Wiretap the ability to write capture files; for now, it can only
[metze/wireshark/wip.git] / wiretap / netxray.c
1 /* netxray.c
2  *
3  * $Id: netxray.c,v 1.9 1999/08/18 04:17:37 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.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
27 #include <stdlib.h>
28 #include <time.h>
29 #include "wtap.h"
30 #include "netxray.h"
31 #include "buffer.h"
32
33 /* Capture file header, *including* magic number, is padded to 128 bytes. */
34 #define CAPTUREFILE_HEADER_SIZE 128
35
36 /* Magic number in NetXRay files. */
37 static const char netxray_magic[] = {   /* magic header */
38         'X', 'C', 'P', '\0'
39 };
40
41 /* NetXRay file header (minus magic number). */
42 struct netxray_hdr {
43         char    version[8];     /* version number */
44         guint32 start_time;     /* UNIX time when capture started */
45         guint32 xxx[2];         /* unknown */
46         guint32 start_offset;   /* offset of first packet in capture */
47         guint32 end_offset;     /* offset after last packet in capture */
48         guint32 xxy[3];         /* unknown */
49         guint16 network;        /* datalink type */
50         guint8  xxz[6];
51         guint32 timelo;         /* lower 32 bits of time stamp of capture start */
52         guint32 timehi;         /* upper 32 bits of time stamp of capture start */
53         /*
54          * XXX - other stuff.
55          */
56 };
57
58 /* Version number strings. */
59 static const char vers_1_0[] = {
60         '0', '0', '1', '.', '0', '0', '0', '\0'
61 };
62
63 static const char vers_1_1[] = {
64         '0', '0', '1', '.', '1', '0', '0', '\0'
65 };
66
67 static const char vers_2_001[] = {
68         '0', '0', '2', '.', '0', '0', '1', '\0'
69 };
70
71 /* NetXRay 1.x data record format - followed by frame data. */
72 struct netxrayrec_1_x_hdr {
73         guint32 timelo;         /* lower 32 bits of time stamp */
74         guint32 timehi;         /* upper 32 bits of time stamp */
75         guint16 orig_len;       /* packet length */
76         guint16 incl_len;       /* capture length */
77         guint32 xxx[4];         /* unknown */
78 };
79
80 /* NetXRay 2.x data record format - followed by frame data. */
81 struct netxrayrec_2_x_hdr {
82         guint32 timelo;         /* lower 32 bits of time stamp */
83         guint32 timehi;         /* upper 32 bits of time stamp */
84         guint16 orig_len;       /* packet length */
85         guint16 incl_len;       /* capture length */
86         guint32 xxx[7];         /* unknown */
87 };
88
89 /* Returns WTAP_FILE_NETXRAY on success, WTAP_FILE_UNKNOWN on failure */
90 int netxray_open(wtap *wth)
91 {
92         int bytes_read;
93         char magic[sizeof netxray_magic];
94         struct netxray_hdr hdr;
95         double timeunit;
96         int version_major;
97         int file_type;
98         double t;
99         static const int netxray_encap[] = {
100                 WTAP_ENCAP_ETHERNET,
101                 WTAP_ENCAP_TR,
102                 WTAP_ENCAP_FDDI,
103                 WTAP_ENCAP_NONE,        /* WAN */
104                 WTAP_ENCAP_NONE,        /* LocalTalk */
105                 WTAP_ENCAP_NONE,        /* "DIX" - should not occur */
106                 WTAP_ENCAP_NONE,        /* ARCNET raw */
107                 WTAP_ENCAP_NONE,        /* ARCNET 878.2 */
108                 WTAP_ENCAP_NONE,        /* ATM */
109                 WTAP_ENCAP_NONE,        /* Wireless WAN */
110                 WTAP_ENCAP_NONE         /* IrDA */
111         };
112         #define NUM_NETXRAY_ENCAPS (sizeof netxray_encap / sizeof netxray_encap[0])
113
114         /* Read in the string that should be at the start of a NetXRay
115          * file */
116         fseek(wth->fh, 0, SEEK_SET);
117         bytes_read = fread(magic, 1, sizeof magic, wth->fh);
118
119         if (bytes_read != sizeof magic) {
120                 return WTAP_FILE_UNKNOWN;
121         }
122
123         if (memcmp(magic, netxray_magic, sizeof netxray_magic) != 0) {
124                 return WTAP_FILE_UNKNOWN;
125         }
126
127         /* Read the rest of the header. */
128         bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh);
129         if (bytes_read != sizeof hdr) {
130                 return WTAP_FILE_UNKNOWN;
131         }
132
133         /* It appears that version 1.1 files (as produced by Windows
134          * Sniffer Pro 2.0.01) have the time stamp in microseconds,
135          * rather than the milliseconds version 1.0 files appear to have.
136          *
137          * It also appears that version 2.001 files (as produced by
138          * Windows(?) Sniffer Pro 2.50.05) have per-packet headers with
139          * some extra fields. */
140         if (memcmp(hdr.version, vers_1_0, sizeof vers_1_0) == 0) {
141                 timeunit = 1000.0;
142                 version_major = 1;
143                 file_type = WTAP_FILE_NETXRAY_1_0;
144         } else if (memcmp(hdr.version, vers_1_1, sizeof vers_1_1) == 0) {
145                 timeunit = 1000000.0;
146                 version_major = 1;
147                 file_type = WTAP_FILE_NETXRAY_1_1;
148         } else if (memcmp(hdr.version, vers_2_001, sizeof vers_2_001) == 0) {
149                 timeunit = 1000000.0;
150                 version_major = 2;
151                 file_type = WTAP_FILE_NETXRAY_2_001;
152         } else {
153                 return WTAP_FILE_UNKNOWN;
154         }
155
156         hdr.network = pletohs(&hdr.network);
157         if (hdr.network >= NUM_NETXRAY_ENCAPS) {
158                 g_error("netxray: network type %d unknown", hdr.network);
159                 return WTAP_FILE_UNKNOWN;
160         }
161
162         /* This is a netxray file */
163         wth->capture.netxray = g_malloc(sizeof(netxray_t));
164         wth->subtype_read = netxray_read;
165         wth->file_encap = netxray_encap[hdr.network];
166         wth->snapshot_length = 16384;   /* XXX - not available in header */
167         wth->capture.netxray->start_time = pletohl(&hdr.start_time);
168         wth->capture.netxray->timeunit = timeunit;
169         t = (double)pletohl(&hdr.timelo)
170             + (double)pletohl(&hdr.timehi)*4294967296.0;
171         t = t/timeunit;
172         wth->capture.netxray->start_timestamp = t;
173         wth->capture.netxray->version_major = version_major;
174         /*wth->frame_number = 0;*/
175         /*wth->file_byte_offset = 0x10b;*/
176
177         /* Remember the offset after the last packet in the capture (which
178          * isn't necessarily the last packet in the file), as it appears
179          * there's sometimes crud after it. */
180         wth->capture.netxray->wrapped = 0;
181         wth->capture.netxray->end_offset = pletohl(&hdr.end_offset);
182
183         /* Seek to the beginning of the data records. */
184         fseek(wth->fh, pletohl(&hdr.start_offset), SEEK_SET);
185
186         return file_type;
187 }
188
189 /* Read the next packet */
190 int netxray_read(wtap *wth)
191 {
192         int     packet_size;
193         int     bytes_read;
194         union {
195                 struct netxrayrec_1_x_hdr hdr_1_x;
196                 struct netxrayrec_2_x_hdr hdr_2_x;
197         }       hdr;
198         int     hdr_size = 0;
199         int     data_offset;
200         double  t;
201
202 reread:
203         /* Have we reached the end of the packet data? */
204         data_offset = ftell(wth->fh);
205         if (data_offset == wth->capture.netxray->end_offset) {
206                 /* Yes. */
207                 return 0;
208         }
209         /* Read record header. */
210         switch (wth->capture.netxray->version_major) {
211
212         case 1:
213                 hdr_size = sizeof (struct netxrayrec_1_x_hdr);
214                 break;
215
216         case 2:
217                 hdr_size = sizeof (struct netxrayrec_2_x_hdr);
218                 break;
219         }
220         bytes_read = fread(&hdr, 1, hdr_size, wth->fh);
221         if (bytes_read != hdr_size) {
222                 if (bytes_read != 0) {
223                         g_error("netxray_read: not enough packet header data (%d bytes)",
224                                         bytes_read);
225                         return -1;
226                 }
227
228                 /* We're at EOF.  Wrap? */
229                 if (!wth->capture.netxray->wrapped) {
230                         /* Yes.  Remember that we did. */
231                         wth->capture.netxray->wrapped = 1;
232                         fseek(wth->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET);
233                         goto reread;
234                 }
235
236                 /* We've already wrapped - don't wrap again. */
237                 return 0;
238         }
239         data_offset += hdr_size;
240
241         packet_size = pletohs(&hdr.hdr_1_x.incl_len);
242         buffer_assure_space(wth->frame_buffer, packet_size);
243         bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
244                         packet_size, wth->fh);
245
246         if (bytes_read != packet_size) {
247                 if (ferror(wth->fh)) {
248                         g_error("netxray_read: fread for data: read error\n");
249                 } else {
250                         g_error("netxray_read: fread for data: %d bytes out of %d",
251                                 bytes_read, packet_size);
252                 }
253                 return -1;
254         }
255
256         t = (double)pletohl(&hdr.hdr_1_x.timelo)
257             + (double)pletohl(&hdr.hdr_1_x.timehi)*4294967296.0;
258         t /= wth->capture.netxray->timeunit;
259         t -= wth->capture.netxray->start_timestamp;
260         wth->phdr.ts.tv_sec = wth->capture.netxray->start_time + (long)t;
261         wth->phdr.ts.tv_usec = (unsigned long)((t-(double)(unsigned long)(t))
262                 *1.0e6);
263         wth->phdr.caplen = packet_size;
264         wth->phdr.len = pletohs(&hdr.hdr_1_x.orig_len);
265         wth->phdr.pkt_encap = wth->file_encap;
266
267         return data_offset;
268 }