2 * Routines for opening CommView file format packet captures
3 * Copyright 2007, Stephen Fisher (see AUTHORS file)
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * Based on csids.c and nettl.c
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.
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.
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,
29 /* A brief description of this file format is available at:
30 * http://www.tamos.com/htmlhelp/commview/logformat.htm
46 #include "file_wrappers.h"
49 typedef struct commview_header {
51 guint16 source_data_len;
60 guint8 flags; /* Bit-field positions defined below */
61 guint8 signal_level_percent;
65 guint8 direction; /* Or for WiFi, high order byte of
67 guint8 signal_level_dbm;
68 guint8 noise_level; /* In dBm (WiFi only) */
71 #define COMMVIEW_HEADER_SIZE 24
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
80 /* Capture mediums as defined by the commview file format */
81 #define MEDIUM_ETHERNET 0
83 #define MEDIUM_TOKEN_RING 2
85 static gboolean commview_read(wtap *wth, int *err, gchar **err_info,
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,
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);
97 int commview_open(wtap *wth, int *err, gchar **err_info)
99 commview_header_t cv_hdr;
101 if(!commview_read_header(&cv_hdr, wth->fh, err, err_info))
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 ||
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 */
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)
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;
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;
132 return 1; /* Our kind of file */
136 commview_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
138 commview_header_t cv_hdr;
142 *data_offset = wth->data_offset;
144 if(!commview_read_header(&cv_hdr, wth->fh, err, err_info))
147 wth->data_offset += COMMVIEW_HEADER_SIZE;
149 switch(cv_hdr.flags & FLAGS_MEDIUM) {
151 case MEDIUM_ETHERNET :
152 wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
156 wth->phdr.pkt_encap = WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
159 case MEDIUM_TOKEN_RING :
160 wth->phdr.pkt_encap = WTAP_ENCAP_TOKEN_RING;
163 *err = WTAP_ERR_BAD_RECORD;
164 *err_info = g_strdup_printf("commview: unsupported encap: %u",
165 cv_hdr.flags & FLAGS_MEDIUM);
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);
175 *err = WTAP_ERR_SHORT_READ;
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;
187 wth->data_offset += cv_hdr.data_len;
189 wth->phdr.len = cv_hdr.data_len;
190 wth->phdr.caplen = cv_hdr.data_len;
192 wth->phdr.ts.secs = mktime(&tm);
193 wth->phdr.ts.nsecs = cv_hdr.usecs * 1000;
199 commview_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
200 *pseudo_header, guchar *pd, int length, int *err,
203 commview_header_t cv_hdr;
206 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
209 if(!commview_read_header(&cv_hdr, wth->random_fh, err, err_info)) {
211 *err = WTAP_ERR_SHORT_READ;
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);
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;
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);
234 *err = WTAP_ERR_SHORT_READ;
243 commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
248 bytes_read += file_read(&cv_hdr->data_len, 2, fh);
249 bytes_read += file_read(&cv_hdr->source_data_len, 2, fh);
250 bytes_read += file_read(&cv_hdr->version, 1, fh);
251 bytes_read += file_read(&cv_hdr->year, 2, fh);
252 bytes_read += file_read(&cv_hdr->month, 1, fh);
253 bytes_read += file_read(&cv_hdr->day, 1, fh);
254 bytes_read += file_read(&cv_hdr->hours, 1, fh);
255 bytes_read += file_read(&cv_hdr->minutes, 1, fh);
256 bytes_read += file_read(&cv_hdr->seconds, 1, fh);
257 bytes_read += file_read(&cv_hdr->usecs, 4, fh);
258 bytes_read += file_read(&cv_hdr->flags, 1, fh);
259 bytes_read += file_read(&cv_hdr->signal_level_percent, 1, fh);
260 bytes_read += file_read(&cv_hdr->rate, 1, fh);
261 bytes_read += file_read(&cv_hdr->band, 1, fh);
262 bytes_read += file_read(&cv_hdr->channel, 1, fh);
263 bytes_read += file_read(&cv_hdr->direction, 1, fh);
264 bytes_read += file_read(&cv_hdr->signal_level_dbm, 1, fh);
265 bytes_read += file_read(&cv_hdr->noise_level, 1, fh);
267 /* Convert multi-byte values from little endian to host endian format */
268 cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
269 cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
270 cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
271 cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);
273 if(bytes_read < COMMVIEW_HEADER_SIZE) {
274 *err = file_error(fh, err_info);
275 if(*err == 0 && bytes_read > 0)
276 *err = WTAP_ERR_SHORT_READ;
284 /* Returns 0 if we can write out the specified encapsulation type
285 * into a CommView format file. */
286 int commview_dump_can_write_encap(int encap)
290 case WTAP_ENCAP_ETHERNET :
291 case WTAP_ENCAP_IEEE_802_11 :
292 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
293 case WTAP_ENCAP_TOKEN_RING :
294 case WTAP_ENCAP_PER_PACKET :
298 return WTAP_ERR_UNSUPPORTED_ENCAP;
302 /* Returns TRUE on success, FALSE on failure;
303 sets "*err" to an error code on failure */
304 gboolean commview_dump_open(wtap_dumper *wdh, int *err _U_)
306 wdh->subtype_write = commview_dump;
307 wdh->subtype_close = NULL;
309 /* There is no file header to write out */
310 wdh->bytes_dumped = 0;
315 /* Write a record for a packet to a dump file.
316 * Returns TRUE on success, FALSE on failure. */
317 static gboolean commview_dump(wtap_dumper *wdh,
318 const struct wtap_pkthdr *phdr,
319 const union wtap_pseudo_header *pseudo_header,
320 const guchar *pd, int *err)
322 commview_header_t cv_hdr;
325 memset(&cv_hdr, 0, sizeof(cv_hdr));
327 cv_hdr.data_len = GUINT16_TO_LE((guint16)phdr->caplen);
328 cv_hdr.source_data_len = GUINT16_TO_LE((guint16)phdr->caplen);
331 strftime(date_time, 5, "%Y", localtime(&phdr->ts.secs));
332 cv_hdr.year = GUINT16_TO_LE((guint16)strtol(date_time, NULL, 10));
334 strftime(date_time, 5, "%m", localtime(&phdr->ts.secs));
335 cv_hdr.month = (guint8)strtol(date_time, NULL, 10);
337 strftime(date_time, 5, "%d", localtime(&phdr->ts.secs));
338 cv_hdr.day = (guint8)strtol(date_time, NULL, 10);
340 strftime(date_time, 5, "%H", localtime(&phdr->ts.secs));
341 cv_hdr.hours = (guint8)strtol(date_time, NULL, 10);
343 strftime(date_time, 5, "%M", localtime(&phdr->ts.secs));
344 cv_hdr.minutes = (guint8)strtol(date_time, NULL, 10);
346 strftime(date_time, 5, "%S", localtime(&phdr->ts.secs));
347 cv_hdr.seconds = (guint8)strtol(date_time, NULL, 10);
349 cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000);
351 switch(phdr->pkt_encap) {
353 case WTAP_ENCAP_ETHERNET :
354 cv_hdr.flags |= MEDIUM_ETHERNET;
357 case WTAP_ENCAP_IEEE_802_11 :
358 cv_hdr.flags |= MEDIUM_WIFI;
361 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
362 cv_hdr.flags |= MEDIUM_WIFI;
364 cv_hdr.channel = pseudo_header->ieee_802_11.channel;
365 cv_hdr.rate = pseudo_header->ieee_802_11.data_rate;
366 cv_hdr.signal_level_percent = pseudo_header->ieee_802_11.signal_level;
369 case WTAP_ENCAP_TOKEN_RING :
370 cv_hdr.flags |= MEDIUM_TOKEN_RING;
374 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
378 if (!wtap_dump_file_write(wdh, &cv_hdr.data_len, 2, err))
380 if (!wtap_dump_file_write(wdh, &cv_hdr.source_data_len, 2, err))
382 if (!wtap_dump_file_write(wdh, &cv_hdr.version, 1, err))
384 if (!wtap_dump_file_write(wdh, &cv_hdr.year, 2, err))
386 if (!wtap_dump_file_write(wdh, &cv_hdr.month, 1, err))
388 if (!wtap_dump_file_write(wdh, &cv_hdr.day, 1, err))
390 if (!wtap_dump_file_write(wdh, &cv_hdr.hours, 1, err))
392 if (!wtap_dump_file_write(wdh, &cv_hdr.minutes, 1, err))
394 if (!wtap_dump_file_write(wdh, &cv_hdr.seconds, 1, err))
396 if (!wtap_dump_file_write(wdh, &cv_hdr.usecs, 4, err))
398 if (!wtap_dump_file_write(wdh, &cv_hdr.flags, 1, err))
400 if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_percent, 1, err))
402 if (!wtap_dump_file_write(wdh, &cv_hdr.rate, 1, err))
404 if (!wtap_dump_file_write(wdh, &cv_hdr.band, 1, err))
406 if (!wtap_dump_file_write(wdh, &cv_hdr.channel, 1, err))
408 if (!wtap_dump_file_write(wdh, &cv_hdr.direction, 1, err))
410 if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_dbm, 2, err))
412 if (!wtap_dump_file_write(wdh, &cv_hdr.noise_level, 2, err))
414 wdh->bytes_dumped += COMMVIEW_HEADER_SIZE;
416 if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
418 wdh->bytes_dumped += phdr->caplen;