Do case insensitive search for lua scripts to load.
[obnox/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         guint8          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                                    guchar *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 _U_,
95                               const guchar *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 gboolean
136 commview_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
137 {
138         commview_header_t cv_hdr;
139         struct tm tm;
140         int bytes_read;
141
142         *data_offset = wth->data_offset;
143
144         if(!commview_read_header(&cv_hdr, wth->fh, err, err_info))
145                 return FALSE;
146
147         wth->data_offset += COMMVIEW_HEADER_SIZE;
148
149         switch(cv_hdr.flags & FLAGS_MEDIUM) {
150
151         case MEDIUM_ETHERNET :
152                 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
153                 break;
154
155         case MEDIUM_WIFI :
156                 wth->phdr.pkt_encap = WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
157                 break;
158
159         case MEDIUM_TOKEN_RING :
160                 wth->phdr.pkt_encap = WTAP_ENCAP_TOKEN_RING;
161                 break;
162         default:
163                 *err = WTAP_ERR_BAD_RECORD;
164                 *err_info = g_strdup_printf("commview: unsupported encap: %u",
165                                             cv_hdr.flags & FLAGS_MEDIUM);
166                 return FALSE;
167         }
168
169         buffer_assure_space(wth->frame_buffer, cv_hdr.data_len);
170         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer),
171                                cv_hdr.data_len, wth->fh);
172         if(bytes_read != cv_hdr.data_len) {
173                 *err = file_error(wth->fh, err_info);
174                 if(*err == 0)
175                         *err = WTAP_ERR_SHORT_READ;
176                 return FALSE;
177         }
178
179         tm.tm_year = cv_hdr.year - 1900;
180         tm.tm_mon = cv_hdr.month - 1;
181         tm.tm_mday = cv_hdr.day;
182         tm.tm_hour = cv_hdr.hours;
183         tm.tm_min = cv_hdr.minutes;
184         tm.tm_sec = cv_hdr.seconds;
185         tm.tm_isdst = -1;
186
187         wth->data_offset += cv_hdr.data_len;
188
189         wth->phdr.len = cv_hdr.data_len;
190         wth->phdr.caplen = cv_hdr.data_len;
191
192         wth->phdr.ts.secs = mktime(&tm);
193         wth->phdr.ts.nsecs = cv_hdr.usecs * 1000;
194
195         return TRUE;
196 }
197
198 static gboolean
199 commview_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
200                    *pseudo_header, guchar *pd, int length, int *err,
201                    gchar **err_info)
202 {
203         commview_header_t cv_hdr;
204         int bytes_read;
205
206         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
207                 return FALSE;
208
209         if(!commview_read_header(&cv_hdr, wth->random_fh, err, err_info)) {
210                 if(*err == 0)
211                         *err = WTAP_ERR_SHORT_READ;
212
213                 return FALSE;
214         }
215
216         if(length != cv_hdr.data_len) {
217                 *err = WTAP_ERR_BAD_RECORD;
218                 *err_info = g_strdup_printf("commview: record length %u doesn't match requested length %d", cv_hdr.data_len, length);
219                 return FALSE;
220         }
221
222         /* Pass some data to the 802.11 dissector if this is a WiFi packet */
223         if((cv_hdr.flags & FLAGS_MEDIUM) == MEDIUM_WIFI) {
224                 pseudo_header->ieee_802_11.fcs_len = -1; /* Unknown */
225                 pseudo_header->ieee_802_11.channel = cv_hdr.channel;
226                 pseudo_header->ieee_802_11.data_rate = cv_hdr.rate;
227                 pseudo_header->ieee_802_11.signal_level = cv_hdr.signal_level_percent;
228         }
229
230         bytes_read = file_read(pd, cv_hdr.data_len, wth->random_fh);
231         if(bytes_read != cv_hdr.data_len) {
232                 *err = file_error(wth->random_fh, err_info);
233                 if(*err == 0)
234                         *err = WTAP_ERR_SHORT_READ;
235
236                 return FALSE;
237         }
238
239         return TRUE;
240 }
241
242 static gboolean
243 commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
244     gchar **err_info)
245 {
246         wtap_file_read_expected_bytes(&cv_hdr->data_len, 2, fh, err, err_info);
247         wtap_file_read_expected_bytes(&cv_hdr->source_data_len, 2, fh, err, err_info);
248         wtap_file_read_expected_bytes(&cv_hdr->version, 1, fh, err, err_info);
249         wtap_file_read_expected_bytes(&cv_hdr->year, 2, fh, err, err_info);
250         wtap_file_read_expected_bytes(&cv_hdr->month, 1, fh, err, err_info);
251         wtap_file_read_expected_bytes(&cv_hdr->day, 1, fh, err, err_info);
252         wtap_file_read_expected_bytes(&cv_hdr->hours, 1, fh, err, err_info);
253         wtap_file_read_expected_bytes(&cv_hdr->minutes, 1, fh, err, err_info);
254         wtap_file_read_expected_bytes(&cv_hdr->seconds, 1, fh, err, err_info);
255         wtap_file_read_expected_bytes(&cv_hdr->usecs, 4, fh, err, err_info);
256         wtap_file_read_expected_bytes(&cv_hdr->flags, 1, fh, err, err_info);
257         wtap_file_read_expected_bytes(&cv_hdr->signal_level_percent, 1, fh, err, err_info);
258         wtap_file_read_expected_bytes(&cv_hdr->rate, 1, fh, err, err_info);
259         wtap_file_read_expected_bytes(&cv_hdr->band, 1, fh, err, err_info);
260         wtap_file_read_expected_bytes(&cv_hdr->channel, 1, fh, err, err_info);
261         wtap_file_read_expected_bytes(&cv_hdr->direction, 1, fh, err, err_info);
262         wtap_file_read_expected_bytes(&cv_hdr->signal_level_dbm, 1, fh, err, err_info);
263         wtap_file_read_expected_bytes(&cv_hdr->noise_level, 1, fh, err, err_info);
264
265         /* Convert multi-byte values from little endian to host endian format */
266         cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
267         cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
268         cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
269         cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);
270
271         return TRUE;
272 }
273
274 /* Returns 0 if we can write out the specified encapsulation type
275  * into a CommView format file. */
276 int commview_dump_can_write_encap(int encap)
277 {
278         switch (encap) {
279
280         case WTAP_ENCAP_ETHERNET :
281         case WTAP_ENCAP_IEEE_802_11 :
282         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
283         case WTAP_ENCAP_TOKEN_RING :
284         case WTAP_ENCAP_PER_PACKET :
285                 return 0;
286
287         default:
288                 return WTAP_ERR_UNSUPPORTED_ENCAP;
289         }
290 }
291
292 /* Returns TRUE on success, FALSE on failure;
293    sets "*err" to an error code on failure */
294 gboolean commview_dump_open(wtap_dumper *wdh, int *err _U_)
295 {
296         wdh->subtype_write = commview_dump;
297         wdh->subtype_close = NULL;
298
299         /* There is no file header to write out */
300         wdh->bytes_dumped = 0;
301
302         return TRUE;
303 }
304
305 /* Write a record for a packet to a dump file.
306  * Returns TRUE on success, FALSE on failure. */
307 static gboolean commview_dump(wtap_dumper *wdh,
308                               const struct wtap_pkthdr *phdr,
309                               const union wtap_pseudo_header *pseudo_header,
310                               const guchar *pd, int *err)
311 {
312         commview_header_t cv_hdr;
313         char date_time[5];
314
315         memset(&cv_hdr, 0, sizeof(cv_hdr));
316
317         cv_hdr.data_len = GUINT16_TO_LE((guint16)phdr->caplen);
318         cv_hdr.source_data_len = GUINT16_TO_LE((guint16)phdr->caplen);
319         cv_hdr.version = 0;
320
321         strftime(date_time, 5, "%Y", localtime(&phdr->ts.secs));
322         cv_hdr.year = GUINT16_TO_LE((guint16)strtol(date_time, NULL, 10));
323
324         strftime(date_time, 5, "%m", localtime(&phdr->ts.secs));
325         cv_hdr.month = (guint8)strtol(date_time, NULL, 10);
326
327         strftime(date_time, 5, "%d", localtime(&phdr->ts.secs));
328         cv_hdr.day = (guint8)strtol(date_time, NULL, 10);
329
330         strftime(date_time, 5, "%H", localtime(&phdr->ts.secs));
331         cv_hdr.hours = (guint8)strtol(date_time, NULL, 10);
332
333         strftime(date_time, 5, "%M", localtime(&phdr->ts.secs));
334         cv_hdr.minutes = (guint8)strtol(date_time, NULL, 10);
335
336         strftime(date_time, 5, "%S", localtime(&phdr->ts.secs));
337         cv_hdr.seconds = (guint8)strtol(date_time, NULL, 10);
338
339         cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000);
340
341         switch(phdr->pkt_encap) {
342
343         case WTAP_ENCAP_ETHERNET :
344                 cv_hdr.flags |= MEDIUM_ETHERNET;
345                 break;
346
347         case WTAP_ENCAP_IEEE_802_11 :
348                 cv_hdr.flags |=  MEDIUM_WIFI;
349                 break;
350
351         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
352                 cv_hdr.flags |=  MEDIUM_WIFI;
353
354                 cv_hdr.channel = pseudo_header->ieee_802_11.channel;
355                 cv_hdr.rate = pseudo_header->ieee_802_11.data_rate;
356                 cv_hdr.signal_level_percent = pseudo_header->ieee_802_11.signal_level;
357                 break;
358
359         case WTAP_ENCAP_TOKEN_RING :
360                 cv_hdr.flags |= MEDIUM_TOKEN_RING;
361                 break;
362
363         default :
364                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
365                 return FALSE;
366         }
367
368         if (!wtap_dump_file_write(wdh, &cv_hdr.data_len, 2, err))
369                 return FALSE;
370         if (!wtap_dump_file_write(wdh, &cv_hdr.source_data_len, 2, err))
371                 return FALSE;
372         if (!wtap_dump_file_write(wdh, &cv_hdr.version, 1, err))
373                 return FALSE;
374         if (!wtap_dump_file_write(wdh, &cv_hdr.year, 2, err))
375                 return FALSE;
376         if (!wtap_dump_file_write(wdh, &cv_hdr.month, 1, err))
377                 return FALSE;
378         if (!wtap_dump_file_write(wdh, &cv_hdr.day, 1, err))
379                 return FALSE;
380         if (!wtap_dump_file_write(wdh, &cv_hdr.hours, 1, err))
381                 return FALSE;
382         if (!wtap_dump_file_write(wdh, &cv_hdr.minutes, 1, err))
383                 return FALSE;
384         if (!wtap_dump_file_write(wdh, &cv_hdr.seconds, 1, err))
385                 return FALSE;
386         if (!wtap_dump_file_write(wdh, &cv_hdr.usecs, 4, err))
387                 return FALSE;
388         if (!wtap_dump_file_write(wdh, &cv_hdr.flags, 1, err))
389                 return FALSE;
390         if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_percent, 1, err))
391                 return FALSE;
392         if (!wtap_dump_file_write(wdh, &cv_hdr.rate, 1, err))
393                 return FALSE;
394         if (!wtap_dump_file_write(wdh, &cv_hdr.band, 1, err))
395                 return FALSE;
396         if (!wtap_dump_file_write(wdh, &cv_hdr.channel, 1, err))
397                 return FALSE;
398         if (!wtap_dump_file_write(wdh, &cv_hdr.direction, 1, err))
399                 return FALSE;
400         if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_dbm, 2, err))
401                 return FALSE;
402         if (!wtap_dump_file_write(wdh, &cv_hdr.noise_level, 2, err))
403                 return FALSE;
404         wdh->bytes_dumped += COMMVIEW_HEADER_SIZE;
405
406         if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
407                 return FALSE;
408         wdh->bytes_dumped += phdr->caplen;
409
410         return TRUE;
411 }