Get the Windows build going again. The Netxray "fix" cast to guint8 may not be OK.
[metze/wireshark/wip.git] / wiretap / commview.c
1 /* commview.c
2  * Routines for opening CommView file format packet captures
3  * Copyright 2007, Stephen Fisher (see AUTHORS file)
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Based on csids.c and nettl.c
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
26  * USA.
27  */
28
29 /* A brief description of this file format is available at:
30  *    http://www.tamos.com/htmlhelp/commview/logformat.htm
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <glib.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <string.h>
42
43 #include "wtap.h"
44 #include "wtap-int.h"
45 #include "buffer.h"
46 #include "file_wrappers.h"
47 #include "commview.h"
48
49 typedef struct commview_header {
50         guint16         data_len;
51         guint16         source_data_len;
52         guint8          version;
53         guint16         year;
54         guint8          month;
55         guint8          day;
56         guint8          hours;
57         guint8          minutes;
58         guint8          seconds;
59         guint32         usecs;
60         guint8          flags;          /* Bit-field positions defined below */
61         guint8          signal_level_percent;
62         guint16         rate;
63         guint8          band;
64         guint8          channel;
65         guint8          direction;      /* Or for WiFi, high order byte of
66                                          * packet rate. */
67         guint8          signal_level_dbm;
68         guint8          noise_level;    /* In dBm (WiFi only) */
69 } commview_header_t;
70
71 #define COMMVIEW_HEADER_SIZE 24
72
73 /* Bit-field positions for various fields in the flags variable of the header */
74 #define FLAGS_MEDIUM            0x0F
75 #define FLAGS_DECRYPTED         0x10
76 #define FLAGS_BROKEN            0x20
77 #define FLAGS_COMPRESSED        0x40
78 #define FLAGS_RESERVED          0x80
79
80 /* Capture mediums as defined by the commview file format */
81 #define MEDIUM_ETHERNET         0
82 #define MEDIUM_WIFI             1
83 #define MEDIUM_TOKEN_RING       2
84
85 static gboolean commview_read(wtap *wth, int *err, gchar **err_info,
86                               gint64 *data_offset);
87 static gboolean commview_seek_read(wtap *wth, gint64 seek_off,
88                                    union wtap_pseudo_header *pseudo_header,
89                                    guint8 *pd, int length, int *err,
90                                    gchar **err_info);
91 static gboolean commview_read_header(commview_header_t *cv_hdr, FILE_T fh,
92                                      int *err, gchar **err_info);
93 static gboolean commview_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
94                               const union wtap_pseudo_header *pseudo_header,
95                               const guint8 *pd, int *err);
96
97 int commview_open(wtap *wth, int *err, gchar **err_info)
98 {
99         commview_header_t cv_hdr;
100
101         if(!commview_read_header(&cv_hdr, wth->fh, err, err_info))
102                 return -1;
103
104         /* If any of these fields do not match what we expect, bail out. */
105         if(cv_hdr.version != 0 ||
106            cv_hdr.year < 1970 || cv_hdr.year >= 2038 ||
107            cv_hdr.month < 1 || cv_hdr.month > 12 ||
108            cv_hdr.day < 1 || cv_hdr.day > 31 ||
109            cv_hdr.hours > 23 ||
110            cv_hdr.minutes > 59 ||
111            cv_hdr.seconds > 60 ||
112            cv_hdr.signal_level_percent > 100 ||
113            (cv_hdr.flags & FLAGS_RESERVED) != 0 ||
114            ((cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_ETHERNET &&
115             (cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_WIFI &&
116             (cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_TOKEN_RING))
117                 return 0; /* Not our kind of file */
118
119         /* No file header. Reset the fh to 0 so we can read the first packet */
120         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
121                 return -1;
122
123         /* Set up the pointers to the handlers for this file type */
124         wth->subtype_read = commview_read;
125         wth->subtype_seek_read = commview_seek_read;
126
127         wth->data_offset = 0;
128         wth->file_type = WTAP_FILE_COMMVIEW;
129         wth->file_encap = WTAP_ENCAP_PER_PACKET;
130         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
131
132         return 1; /* Our kind of file */
133 }
134
135 static void
136 commview_set_pseudo_header(commview_header_t *cv_hdrp, union wtap_pseudo_header
137                            *pseudo_header)
138 {
139         switch(cv_hdrp->flags & FLAGS_MEDIUM) {
140
141         case MEDIUM_ETHERNET :
142                 pseudo_header->eth.fcs_len = -1; /* Unknown */
143                 break;
144
145         case MEDIUM_WIFI :
146                 pseudo_header->ieee_802_11.fcs_len = -1; /* Unknown */
147                 pseudo_header->ieee_802_11.channel = cv_hdrp->channel;
148                 pseudo_header->ieee_802_11.data_rate = cv_hdrp->rate;
149                 pseudo_header->ieee_802_11.signal_level = cv_hdrp->signal_level_percent;
150                 break;
151
152         default :
153                 /* Token Ring or unknown - no pseudo-header for that */
154                 break;
155         }
156 }
157
158 static gboolean
159 commview_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
160 {
161         commview_header_t cv_hdr;
162         struct tm tm;
163         int bytes_read;
164
165         *data_offset = wth->data_offset;
166
167         if(!commview_read_header(&cv_hdr, wth->fh, err, err_info))
168                 return FALSE;
169
170         wth->data_offset += COMMVIEW_HEADER_SIZE;
171
172         switch(cv_hdr.flags & FLAGS_MEDIUM) {
173
174         case MEDIUM_ETHERNET :
175                 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
176                 break;
177
178         case MEDIUM_WIFI :
179                 wth->phdr.pkt_encap = WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
180                 break;
181
182         case MEDIUM_TOKEN_RING :
183                 wth->phdr.pkt_encap = WTAP_ENCAP_TOKEN_RING;
184                 break;
185
186         default :
187                 *err = WTAP_ERR_BAD_FILE;
188                 *err_info = g_strdup_printf("commview: unsupported encap: %u",
189                                             cv_hdr.flags & FLAGS_MEDIUM);
190                 return FALSE;
191         }
192
193         commview_set_pseudo_header(&cv_hdr, &wth->pseudo_header);
194
195         buffer_assure_space(wth->frame_buffer, cv_hdr.data_len);
196         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer),
197                                cv_hdr.data_len, wth->fh);
198         if(bytes_read != cv_hdr.data_len) {
199                 *err = file_error(wth->fh, err_info);
200                 if(*err == 0)
201                         *err = WTAP_ERR_SHORT_READ;
202                 return FALSE;
203         }
204
205         tm.tm_year = cv_hdr.year - 1900;
206         tm.tm_mon = cv_hdr.month - 1;
207         tm.tm_mday = cv_hdr.day;
208         tm.tm_hour = cv_hdr.hours;
209         tm.tm_min = cv_hdr.minutes;
210         tm.tm_sec = cv_hdr.seconds;
211         tm.tm_isdst = -1;
212
213         wth->data_offset += cv_hdr.data_len;
214
215         wth->phdr.presence_flags = WTAP_HAS_TS;
216
217         wth->phdr.len = cv_hdr.data_len;
218         wth->phdr.caplen = cv_hdr.data_len;
219
220         wth->phdr.ts.secs = mktime(&tm);
221         wth->phdr.ts.nsecs = cv_hdr.usecs * 1000;
222
223         return TRUE;
224 }
225
226 static gboolean
227 commview_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
228                    *pseudo_header, guint8 *pd, int length, int *err,
229                    gchar **err_info)
230 {
231         commview_header_t cv_hdr;
232         int bytes_read;
233
234         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
235                 return FALSE;
236
237         if(!commview_read_header(&cv_hdr, wth->random_fh, err, err_info)) {
238                 if(*err == 0)
239                         *err = WTAP_ERR_SHORT_READ;
240
241                 return FALSE;
242         }
243
244         if(length != cv_hdr.data_len) {
245                 *err = WTAP_ERR_BAD_FILE;
246                 *err_info = g_strdup_printf("commview: record length %u doesn't match requested length %d", cv_hdr.data_len, length);
247                 return FALSE;
248         }
249
250         commview_set_pseudo_header(&cv_hdr, pseudo_header);
251
252         bytes_read = file_read(pd, cv_hdr.data_len, wth->random_fh);
253         if(bytes_read != cv_hdr.data_len) {
254                 *err = file_error(wth->random_fh, err_info);
255                 if(*err == 0)
256                         *err = WTAP_ERR_SHORT_READ;
257
258                 return FALSE;
259         }
260
261         return TRUE;
262 }
263
264 static gboolean
265 commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
266     gchar **err_info)
267 {
268         wtap_file_read_expected_bytes(&cv_hdr->data_len, 2, fh, err, err_info);
269         wtap_file_read_expected_bytes(&cv_hdr->source_data_len, 2, fh, err, err_info);
270         wtap_file_read_expected_bytes(&cv_hdr->version, 1, fh, err, err_info);
271         wtap_file_read_expected_bytes(&cv_hdr->year, 2, fh, err, err_info);
272         wtap_file_read_expected_bytes(&cv_hdr->month, 1, fh, err, err_info);
273         wtap_file_read_expected_bytes(&cv_hdr->day, 1, fh, err, err_info);
274         wtap_file_read_expected_bytes(&cv_hdr->hours, 1, fh, err, err_info);
275         wtap_file_read_expected_bytes(&cv_hdr->minutes, 1, fh, err, err_info);
276         wtap_file_read_expected_bytes(&cv_hdr->seconds, 1, fh, err, err_info);
277         wtap_file_read_expected_bytes(&cv_hdr->usecs, 4, fh, err, err_info);
278         wtap_file_read_expected_bytes(&cv_hdr->flags, 1, fh, err, err_info);
279         wtap_file_read_expected_bytes(&cv_hdr->signal_level_percent, 1, fh, err, err_info);
280         wtap_file_read_expected_bytes(&cv_hdr->rate, 1, fh, err, err_info);
281         wtap_file_read_expected_bytes(&cv_hdr->band, 1, fh, err, err_info);
282         wtap_file_read_expected_bytes(&cv_hdr->channel, 1, fh, err, err_info);
283         wtap_file_read_expected_bytes(&cv_hdr->direction, 1, fh, err, err_info);
284         wtap_file_read_expected_bytes(&cv_hdr->signal_level_dbm, 1, fh, err, err_info);
285         wtap_file_read_expected_bytes(&cv_hdr->noise_level, 1, fh, err, err_info);
286
287         /* Convert multi-byte values from little endian to host endian format */
288         cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
289         cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
290         cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
291         cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);
292
293         return TRUE;
294 }
295
296 /* Returns 0 if we can write out the specified encapsulation type
297  * into a CommView format file. */
298 int commview_dump_can_write_encap(int encap)
299 {
300         switch (encap) {
301
302         case WTAP_ENCAP_ETHERNET :
303         case WTAP_ENCAP_IEEE_802_11 :
304         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
305         case WTAP_ENCAP_TOKEN_RING :
306         case WTAP_ENCAP_PER_PACKET :
307                 return 0;
308
309         default:
310                 return WTAP_ERR_UNSUPPORTED_ENCAP;
311         }
312 }
313
314 /* Returns TRUE on success, FALSE on failure;
315    sets "*err" to an error code on failure */
316 gboolean commview_dump_open(wtap_dumper *wdh, int *err _U_)
317 {
318         wdh->subtype_write = commview_dump;
319         wdh->subtype_close = NULL;
320
321         /* There is no file header to write out */
322         wdh->bytes_dumped = 0;
323
324         return TRUE;
325 }
326
327 /* Write a record for a packet to a dump file.
328  * Returns TRUE on success, FALSE on failure. */
329 static gboolean commview_dump(wtap_dumper *wdh,
330                               const struct wtap_pkthdr *phdr,
331                               const union wtap_pseudo_header *pseudo_header,
332                               const guint8 *pd, int *err)
333 {
334         commview_header_t cv_hdr;
335         struct tm *tm;
336
337         memset(&cv_hdr, 0, sizeof(cv_hdr));
338
339         cv_hdr.data_len = GUINT16_TO_LE((guint16)phdr->caplen);
340         cv_hdr.source_data_len = GUINT16_TO_LE((guint16)phdr->caplen);
341         cv_hdr.version = 0;
342
343         tm = localtime(&phdr->ts.secs);
344         cv_hdr.year = tm->tm_year + 1900;
345         cv_hdr.month = tm->tm_mon + 1;
346         cv_hdr.day = tm->tm_mday;
347         cv_hdr.hours = tm->tm_hour;
348         cv_hdr.minutes = tm->tm_min;
349         cv_hdr.seconds = tm->tm_sec;
350         cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000);
351
352         switch(phdr->pkt_encap) {
353
354         case WTAP_ENCAP_ETHERNET :
355                 cv_hdr.flags |= MEDIUM_ETHERNET;
356                 break;
357
358         case WTAP_ENCAP_IEEE_802_11 :
359                 cv_hdr.flags |=  MEDIUM_WIFI;
360                 break;
361
362         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
363                 cv_hdr.flags |=  MEDIUM_WIFI;
364
365                 cv_hdr.channel = pseudo_header->ieee_802_11.channel;
366                 cv_hdr.rate = pseudo_header->ieee_802_11.data_rate;
367                 cv_hdr.signal_level_percent = pseudo_header->ieee_802_11.signal_level;
368                 break;
369
370         case WTAP_ENCAP_TOKEN_RING :
371                 cv_hdr.flags |= MEDIUM_TOKEN_RING;
372                 break;
373
374         default :
375                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
376                 return FALSE;
377         }
378
379         if (!wtap_dump_file_write(wdh, &cv_hdr.data_len, 2, err))
380                 return FALSE;
381         if (!wtap_dump_file_write(wdh, &cv_hdr.source_data_len, 2, err))
382                 return FALSE;
383         if (!wtap_dump_file_write(wdh, &cv_hdr.version, 1, err))
384                 return FALSE;
385         if (!wtap_dump_file_write(wdh, &cv_hdr.year, 2, err))
386                 return FALSE;
387         if (!wtap_dump_file_write(wdh, &cv_hdr.month, 1, err))
388                 return FALSE;
389         if (!wtap_dump_file_write(wdh, &cv_hdr.day, 1, err))
390                 return FALSE;
391         if (!wtap_dump_file_write(wdh, &cv_hdr.hours, 1, err))
392                 return FALSE;
393         if (!wtap_dump_file_write(wdh, &cv_hdr.minutes, 1, err))
394                 return FALSE;
395         if (!wtap_dump_file_write(wdh, &cv_hdr.seconds, 1, err))
396                 return FALSE;
397         if (!wtap_dump_file_write(wdh, &cv_hdr.usecs, 4, err))
398                 return FALSE;
399         if (!wtap_dump_file_write(wdh, &cv_hdr.flags, 1, err))
400                 return FALSE;
401         if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_percent, 1, err))
402                 return FALSE;
403         if (!wtap_dump_file_write(wdh, &cv_hdr.rate, 1, err))
404                 return FALSE;
405         if (!wtap_dump_file_write(wdh, &cv_hdr.band, 1, err))
406                 return FALSE;
407         if (!wtap_dump_file_write(wdh, &cv_hdr.channel, 1, err))
408                 return FALSE;
409         if (!wtap_dump_file_write(wdh, &cv_hdr.direction, 1, err))
410                 return FALSE;
411         if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_dbm, 2, err))
412                 return FALSE;
413         if (!wtap_dump_file_write(wdh, &cv_hdr.noise_level, 2, err))
414                 return FALSE;
415         wdh->bytes_dumped += COMMVIEW_HEADER_SIZE;
416
417         if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
418                 return FALSE;
419         wdh->bytes_dumped += phdr->caplen;
420
421         return TRUE;
422 }