From Owen Kirby :
[obnox/wireshark/wip.git] / wiretap / commview.c
1 /* commview.c
2  * Routines for opening CommView file format packet captures
3  * Copyright 2007, Stephen Fisher <stephentfisher@yahoo.com>
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;
62         guint8          rate;
63         guint8          band;
64         guint8          channel;
65         guint8          direction;      /* Not applicable to WiFi packets */
66         guint16         reserved;       /* Unused bytes */
67 } commview_header_t;
68
69 #define COMMVIEW_HEADER_SIZE 24
70
71 /* Bit-field positions for various fields in the flags variable of the header */
72 #define FLAGS_MEDIUM            0x0F
73 #define FLAGS_DECRYPTED         0x10
74 #define FLAGS_BROKEN            0x20
75 #define FLAGS_COMPRESSED        0x40
76 #define FLAGS_RESERVED          0x80
77
78 /* Capture mediums as defined by the commview file format */
79 #define MEDIUM_ETHERNET         0
80 #define MEDIUM_WIFI             1
81 #define MEDIUM_TOKEN_RING       2
82
83 static gboolean commview_read(wtap *wth, int *err, gchar **err_info _U_,
84                               gint64 *data_offset);
85 static gboolean commview_seek_read(wtap *wth, gint64 seek_off,
86                                    union wtap_pseudo_header *pseudo_header,
87                                    guchar *pd, int length, int *err,
88                                    gchar **err_info _U_);
89 static gboolean  commview_read_header(commview_header_t *cv_hdr, FILE_T fh,
90                                       int *err);
91 static gboolean commview_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
92                               const union wtap_pseudo_header *pseudo_header _U_,
93                               const guchar *pd, int *err);
94
95 int commview_open(wtap *wth, int *err, gchar **err_info _U_)
96 {
97         commview_header_t cv_hdr;
98
99         if(!commview_read_header(&cv_hdr, wth->fh, err))
100                 return -1;
101
102         /* If any of these fields do not match what we expect, bail out. */
103         if(cv_hdr.version != 0 ||
104            cv_hdr.year < 1970 || cv_hdr.year >= 2038 ||
105            cv_hdr.month < 1 || cv_hdr.month > 12 ||
106            cv_hdr.day < 1 || cv_hdr.day > 31 ||
107            cv_hdr.hours > 23 ||
108            cv_hdr.minutes > 59 ||
109            cv_hdr.seconds > 60 ||
110            cv_hdr.signal_level > 100 ||
111            (cv_hdr.direction != 0x00 && cv_hdr.direction != 0x01 &&
112             cv_hdr.direction != 0x02) ||
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            cv_hdr.reserved != 0)
118                 return 0; /* Not our kind of file */
119
120         /* No file header. Reset the fh to 0 so we can read the first packet */
121         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
122                 return -1;
123
124         /* Set up the pointers to the handlers for this file type */
125         wth->subtype_read = commview_read;
126         wth->subtype_seek_read = commview_seek_read;
127
128         wth->data_offset = 0;
129         wth->file_type = WTAP_FILE_COMMVIEW;
130         wth->file_encap = WTAP_ENCAP_PER_PACKET;
131         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
132         
133         return 1; /* Our kind of file */
134 }
135
136 static gboolean
137 commview_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset)
138 {
139         commview_header_t cv_hdr;
140         struct tm tm;
141         int bytes_read;
142         
143         *data_offset = wth->data_offset;
144         
145         if(!commview_read_header(&cv_hdr, wth->fh, err))
146                 return FALSE;
147
148         wth->data_offset += COMMVIEW_HEADER_SIZE;
149
150         switch(cv_hdr.flags & FLAGS_MEDIUM) {
151
152         case MEDIUM_ETHERNET :
153                 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
154                 break;
155
156         case MEDIUM_WIFI :
157                 wth->phdr.pkt_encap = WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
158                 break;
159
160         case MEDIUM_TOKEN_RING :
161                 wth->phdr.pkt_encap = WTAP_ENCAP_TOKEN_RING;
162                 break;
163         }
164
165         buffer_assure_space(wth->frame_buffer, cv_hdr.data_len);
166         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
167                                cv_hdr.data_len, wth->fh);
168         if(bytes_read != cv_hdr.data_len) {
169                 *err = file_error(wth->fh);
170                 if(*err == 0)
171                         *err = WTAP_ERR_SHORT_READ;
172                 return FALSE;
173         }
174
175         tm.tm_year = cv_hdr.year - 1900;
176         tm.tm_mon = cv_hdr.month - 1;
177         tm.tm_mday = cv_hdr.day;
178         tm.tm_hour = cv_hdr.hours;
179         tm.tm_min = cv_hdr.minutes;
180         tm.tm_sec = cv_hdr.seconds;
181         tm.tm_isdst = -1;
182
183         wth->data_offset += cv_hdr.data_len;
184
185         wth->phdr.len = cv_hdr.data_len;
186         wth->phdr.caplen = cv_hdr.data_len;
187
188         wth->phdr.ts.secs = mktime(&tm);
189         wth->phdr.ts.nsecs = cv_hdr.usecs * 1000;
190
191         return TRUE;
192 }
193
194 static gboolean
195 commview_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
196                    *pseudo_header, guchar *pd, int length, int *err,
197                    gchar **err_info _U_)
198 {
199         commview_header_t cv_hdr;
200         int bytes_read;
201
202         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
203                 return FALSE;
204         
205         if(!commview_read_header(&cv_hdr, wth->random_fh, err)) {
206                 if(*err == 0)
207                         *err = WTAP_ERR_SHORT_READ;
208
209                 return FALSE;
210         }
211         
212         if(length != cv_hdr.data_len) {
213                 *err = WTAP_ERR_BAD_RECORD;
214                 *err_info = g_strdup_printf("commview: record length %u doesn't match requested length %d", cv_hdr.data_len, length);
215                 return FALSE;
216         }
217
218         /* Pass some data to the 802.11 dissector if this is a WiFi packet */
219         if((cv_hdr.flags & FLAGS_MEDIUM) == MEDIUM_WIFI) {
220                 pseudo_header->ieee_802_11.fcs_len = -1; /* Unknown */
221                 pseudo_header->ieee_802_11.channel = cv_hdr.channel;
222                 pseudo_header->ieee_802_11.data_rate = cv_hdr.rate;
223                 pseudo_header->ieee_802_11.signal_level = cv_hdr.signal_level;
224         }
225
226         bytes_read = file_read(pd, 1, cv_hdr.data_len, wth->random_fh);
227         if(bytes_read != cv_hdr.data_len) {
228                 *err = file_error(wth->random_fh);
229                 if(*err == 0)
230                         *err = WTAP_ERR_SHORT_READ;
231
232                 return FALSE;
233         }
234
235         return TRUE;
236 }
237
238 static gboolean
239 commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err)
240 {
241         int bytes_read = 0;
242
243         bytes_read += file_read(&cv_hdr->data_len, 2, 1, fh);
244         bytes_read += file_read(&cv_hdr->source_data_len, 2, 1, fh);
245         bytes_read += file_read(&cv_hdr->version, 1, 1, fh);
246         bytes_read += file_read(&cv_hdr->year, 2, 1, fh);
247         bytes_read += file_read(&cv_hdr->month, 1, 1, fh);
248         bytes_read += file_read(&cv_hdr->day, 1, 1, fh);
249         bytes_read += file_read(&cv_hdr->hours, 1, 1, fh);
250         bytes_read += file_read(&cv_hdr->minutes, 1, 1, fh);
251         bytes_read += file_read(&cv_hdr->seconds, 1, 1, fh);
252         bytes_read += file_read(&cv_hdr->usecs, 4, 1, fh);
253         bytes_read += file_read(&cv_hdr->flags, 1, 1, fh);
254         bytes_read += file_read(&cv_hdr->signal_level, 1, 1, fh);
255         bytes_read += file_read(&cv_hdr->rate, 1, 1, fh);
256         bytes_read += file_read(&cv_hdr->band, 1, 1, fh);
257         bytes_read += file_read(&cv_hdr->channel, 1, 1, fh);
258         bytes_read += file_read(&cv_hdr->direction, 1, 1, fh);
259         bytes_read += file_read(&cv_hdr->reserved, 2, 1, fh);
260
261         /* Convert multi-byte values from little endian to host endian format */
262         cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
263         cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
264         cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
265         cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);
266
267         if(bytes_read < COMMVIEW_HEADER_SIZE) {
268                 *err = file_error(fh);
269                 if(*err == 0 && bytes_read > 0)
270                         *err = WTAP_ERR_SHORT_READ;
271
272                 return FALSE;
273         }
274
275         return TRUE;
276 }
277
278 /* Returns 0 if we can write out the specified encapsulatio type
279  * into a CommView format file. */
280 int commview_dump_can_write_encap(int encap)
281 {
282         switch (encap) {
283
284         case WTAP_ENCAP_ETHERNET :
285         case WTAP_ENCAP_IEEE_802_11 :
286         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
287         case WTAP_ENCAP_TOKEN_RING :
288         case WTAP_ENCAP_PER_PACKET :
289                 return 0;
290
291         default:
292                 return WTAP_ERR_UNSUPPORTED_ENCAP;
293         }
294 }
295
296 /* Returns TRUE on success, FALSE on failure;
297    sets "*err" to an error code on failure */
298 gboolean commview_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_,
299                             int *err _U_)
300 {
301         wdh->subtype_write = commview_dump;
302         wdh->subtype_close = NULL;
303         
304         /* There is no file header to write out */
305         wdh->bytes_dumped = 0;
306         
307         return TRUE;
308 }
309
310 /* Write a record for a packet to a dump file.
311  * Returns TRUE on success, FALSE on failure. */
312 static gboolean commview_dump(wtap_dumper *wdh,
313                               const struct wtap_pkthdr *phdr,
314                               const union wtap_pseudo_header *pseudo_header,
315                               const guchar *pd, int *err)
316 {
317         commview_header_t cv_hdr;
318         size_t bytes_written = 0;
319         char date_time[5];
320
321         memset(&cv_hdr, 0, sizeof(cv_hdr));
322
323         cv_hdr.data_len = GUINT16_TO_LE((guint16)phdr->caplen);
324         cv_hdr.source_data_len = GUINT16_TO_LE((guint16)phdr->caplen);
325         cv_hdr.version = 0;
326
327         strftime(date_time, 5, "%Y", localtime(&phdr->ts.secs));
328         cv_hdr.year = GUINT16_TO_LE((guint16)strtol(date_time, NULL, 10));
329
330         strftime(date_time, 5, "%m", localtime(&phdr->ts.secs));
331         cv_hdr.month = (guint8)strtol(date_time, NULL, 10);
332
333         strftime(date_time, 5, "%d", localtime(&phdr->ts.secs));
334         cv_hdr.day = (guint8)strtol(date_time, NULL, 10);
335
336         strftime(date_time, 5, "%H", localtime(&phdr->ts.secs));
337         cv_hdr.hours = (guint8)strtol(date_time, NULL, 10);
338
339         strftime(date_time, 5, "%M", localtime(&phdr->ts.secs));
340         cv_hdr.minutes = (guint8)strtol(date_time, NULL, 10);
341
342         strftime(date_time, 5, "%S", localtime(&phdr->ts.secs));
343         cv_hdr.seconds = (guint8)strtol(date_time, NULL, 10);
344
345         cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000);
346
347         switch(phdr->pkt_encap) {
348
349         case WTAP_ENCAP_ETHERNET :
350                 cv_hdr.flags |= MEDIUM_ETHERNET;
351                 break;
352
353         case WTAP_ENCAP_IEEE_802_11 :
354                 cv_hdr.flags |=  MEDIUM_WIFI;
355                 break;
356
357         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
358                 cv_hdr.flags |=  MEDIUM_WIFI;
359
360                 cv_hdr.channel = pseudo_header->ieee_802_11.channel;
361                 cv_hdr.rate = pseudo_header->ieee_802_11.data_rate;
362                 cv_hdr.signal_level = pseudo_header->ieee_802_11.signal_level;
363
364                 break;
365
366         case WTAP_ENCAP_TOKEN_RING :
367                 cv_hdr.flags |= MEDIUM_TOKEN_RING;
368
369         default :
370                 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
371                 return FALSE;
372         }
373
374         bytes_written += fwrite(&cv_hdr.data_len, 2, 1, wdh->fh);
375         bytes_written += fwrite(&cv_hdr.source_data_len, 2, 1, wdh->fh);
376         bytes_written += fwrite(&cv_hdr.version, 1, 1, wdh->fh);
377         bytes_written += fwrite(&cv_hdr.year, 2, 1, wdh->fh);
378         bytes_written += fwrite(&cv_hdr.month, 1, 1, wdh->fh);
379         bytes_written += fwrite(&cv_hdr.day, 1, 1, wdh->fh);
380         bytes_written += fwrite(&cv_hdr.hours, 1, 1, wdh->fh);
381         bytes_written += fwrite(&cv_hdr.minutes, 1, 1, wdh->fh);
382         bytes_written += fwrite(&cv_hdr.seconds, 1, 1, wdh->fh);
383         bytes_written += fwrite(&cv_hdr.usecs, 4, 1, wdh->fh);
384         bytes_written += fwrite(&cv_hdr.flags, 1, 1, wdh->fh);
385         bytes_written += fwrite(&cv_hdr.signal_level, 1, 1, wdh->fh);
386         bytes_written += fwrite(&cv_hdr.rate, 1, 1, wdh->fh);
387         bytes_written += fwrite(&cv_hdr.band, 1, 1, wdh->fh);
388         bytes_written += fwrite(&cv_hdr.channel, 1, 1, wdh->fh);
389         bytes_written += fwrite(&cv_hdr.direction, 1, 1, wdh->fh);
390         bytes_written += fwrite(&cv_hdr.reserved, 2, 1, wdh->fh);
391
392         if(bytes_written != 17) { /* 17 units of data should have been written
393                                    * above. */
394                 if(bytes_written == 0 && ferror(wdh->fh))
395                         *err = errno;
396                 else
397                         *err = WTAP_ERR_SHORT_WRITE;
398                
399                 return FALSE;
400         }
401
402         wdh->bytes_dumped += COMMVIEW_HEADER_SIZE;
403
404         bytes_written = fwrite(pd, 1, phdr->caplen, wdh->fh);
405
406         if(bytes_written != phdr->caplen) {
407                 if(bytes_written == 0 && ferror(wdh->fh))
408                         *err = errno;
409                 else
410                         *err = WTAP_ERR_SHORT_WRITE;
411
412                 return FALSE;
413         }
414
415         wdh->bytes_dumped += phdr->caplen;
416
417         return TRUE;
418 }