2 * Routines for opening CommView file format packet captures
3 * Copyright 2007, Stephen Fisher (see AUTHORS file)
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * Based on csids.c and nettl.c
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 /* A brief description of this file format is available at:
28 * http://www.tamos.com/htmlhelp/commview/logformat.htm
42 #include "file_wrappers.h"
45 typedef struct commview_header {
47 guint16 source_data_len;
56 guint8 flags; /* Bit-field positions defined below */
57 guint8 signal_level_percent;
61 guint8 direction; /* Or for WiFi, high order byte of
63 guint8 signal_level_dbm;
64 guint8 noise_level; /* In dBm (WiFi only) */
67 #define COMMVIEW_HEADER_SIZE 24
69 /* Bit-field positions for various fields in the flags variable of the header */
70 #define FLAGS_MEDIUM 0x0F
71 #define FLAGS_DECRYPTED 0x10
72 #define FLAGS_BROKEN 0x20
73 #define FLAGS_COMPRESSED 0x40
74 #define FLAGS_RESERVED 0x80
76 /* Capture mediums as defined by the commview file format */
77 #define MEDIUM_ETHERNET 0
79 #define MEDIUM_TOKEN_RING 2
81 static gboolean commview_read(wtap *wth, int *err, gchar **err_info,
83 static gboolean commview_seek_read(wtap *wth, gint64 seek_off,
84 struct wtap_pkthdr *phdr,
85 Buffer *buf, int *err, gchar **err_info);
86 static gboolean commview_read_header(commview_header_t *cv_hdr, FILE_T fh,
87 int *err, gchar **err_info);
88 static gboolean commview_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
89 const guint8 *pd, int *err);
91 int commview_open(wtap *wth, int *err, gchar **err_info)
93 commview_header_t cv_hdr;
95 if(!commview_read_header(&cv_hdr, wth->fh, err, err_info)) {
96 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
101 /* If any of these fields do not match what we expect, bail out. */
102 if(cv_hdr.version != 0 ||
103 cv_hdr.year < 1970 || cv_hdr.year >= 2038 ||
104 cv_hdr.month < 1 || cv_hdr.month > 12 ||
105 cv_hdr.day < 1 || cv_hdr.day > 31 ||
107 cv_hdr.minutes > 59 ||
108 cv_hdr.seconds > 60 ||
109 cv_hdr.signal_level_percent > 100 ||
110 (cv_hdr.flags & FLAGS_RESERVED) != 0 ||
111 ((cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_ETHERNET &&
112 (cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_WIFI &&
113 (cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_TOKEN_RING))
114 return 0; /* Not our kind of file */
116 /* No file header. Reset the fh to 0 so we can read the first packet */
117 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
120 /* Set up the pointers to the handlers for this file type */
121 wth->subtype_read = commview_read;
122 wth->subtype_seek_read = commview_seek_read;
124 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COMMVIEW;
125 wth->file_encap = WTAP_ENCAP_PER_PACKET;
126 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
128 return 1; /* Our kind of file */
132 commview_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
133 int *err, gchar **err_info)
135 commview_header_t cv_hdr;
138 if(!commview_read_header(&cv_hdr, fh, err, err_info))
141 switch(cv_hdr.flags & FLAGS_MEDIUM) {
143 case MEDIUM_ETHERNET :
144 phdr->pkt_encap = WTAP_ENCAP_ETHERNET;
145 phdr->pseudo_header.eth.fcs_len = -1; /* Unknown */
149 phdr->pkt_encap = WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
150 phdr->pseudo_header.ieee_802_11.fcs_len = -1; /* Unknown */
151 phdr->pseudo_header.ieee_802_11.channel = cv_hdr.channel;
152 phdr->pseudo_header.ieee_802_11.data_rate =
153 cv_hdr.rate | (cv_hdr.direction << 8);
154 phdr->pseudo_header.ieee_802_11.signal_level = cv_hdr.signal_level_percent;
157 case MEDIUM_TOKEN_RING :
158 phdr->pkt_encap = WTAP_ENCAP_TOKEN_RING;
162 *err = WTAP_ERR_BAD_FILE;
163 *err_info = g_strdup_printf("commview: unsupported encap: %u",
164 cv_hdr.flags & FLAGS_MEDIUM);
168 tm.tm_year = cv_hdr.year - 1900;
169 tm.tm_mon = cv_hdr.month - 1;
170 tm.tm_mday = cv_hdr.day;
171 tm.tm_hour = cv_hdr.hours;
172 tm.tm_min = cv_hdr.minutes;
173 tm.tm_sec = cv_hdr.seconds;
176 phdr->presence_flags = WTAP_HAS_TS;
178 phdr->len = cv_hdr.data_len;
179 phdr->caplen = cv_hdr.data_len;
181 phdr->ts.secs = mktime(&tm);
182 phdr->ts.nsecs = cv_hdr.usecs * 1000;
184 return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);
188 commview_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
190 *data_offset = file_tell(wth->fh);
192 return commview_read_packet(wth->fh, &wth->phdr, wth->frame_buffer, err,
197 commview_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
198 Buffer *buf, int *err, gchar **err_info)
200 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
203 return commview_read_packet(wth->random_fh, phdr, buf, err, err_info);
207 commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
210 wtap_file_read_expected_bytes(&cv_hdr->data_len, 2, fh, err, err_info);
211 wtap_file_read_expected_bytes(&cv_hdr->source_data_len, 2, fh, err, err_info);
212 wtap_file_read_expected_bytes(&cv_hdr->version, 1, fh, err, err_info);
213 wtap_file_read_expected_bytes(&cv_hdr->year, 2, fh, err, err_info);
214 wtap_file_read_expected_bytes(&cv_hdr->month, 1, fh, err, err_info);
215 wtap_file_read_expected_bytes(&cv_hdr->day, 1, fh, err, err_info);
216 wtap_file_read_expected_bytes(&cv_hdr->hours, 1, fh, err, err_info);
217 wtap_file_read_expected_bytes(&cv_hdr->minutes, 1, fh, err, err_info);
218 wtap_file_read_expected_bytes(&cv_hdr->seconds, 1, fh, err, err_info);
219 wtap_file_read_expected_bytes(&cv_hdr->usecs, 4, fh, err, err_info);
220 wtap_file_read_expected_bytes(&cv_hdr->flags, 1, fh, err, err_info);
221 wtap_file_read_expected_bytes(&cv_hdr->signal_level_percent, 1, fh, err, err_info);
222 wtap_file_read_expected_bytes(&cv_hdr->rate, 1, fh, err, err_info);
223 wtap_file_read_expected_bytes(&cv_hdr->band, 1, fh, err, err_info);
224 wtap_file_read_expected_bytes(&cv_hdr->channel, 1, fh, err, err_info);
225 wtap_file_read_expected_bytes(&cv_hdr->direction, 1, fh, err, err_info);
226 wtap_file_read_expected_bytes(&cv_hdr->signal_level_dbm, 1, fh, err, err_info);
227 wtap_file_read_expected_bytes(&cv_hdr->noise_level, 1, fh, err, err_info);
229 /* Convert multi-byte values from little endian to host endian format */
230 cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
231 cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
232 cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
233 cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);
238 /* Returns 0 if we can write out the specified encapsulation type
239 * into a CommView format file. */
240 int commview_dump_can_write_encap(int encap)
244 case WTAP_ENCAP_ETHERNET :
245 case WTAP_ENCAP_IEEE_802_11 :
246 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
247 case WTAP_ENCAP_TOKEN_RING :
248 case WTAP_ENCAP_PER_PACKET :
252 return WTAP_ERR_UNSUPPORTED_ENCAP;
256 /* Returns TRUE on success, FALSE on failure;
257 sets "*err" to an error code on failure */
258 gboolean commview_dump_open(wtap_dumper *wdh, int *err _U_)
260 wdh->subtype_write = commview_dump;
261 wdh->subtype_close = NULL;
263 /* There is no file header to write out */
264 wdh->bytes_dumped = 0;
269 /* Write a record for a packet to a dump file.
270 * Returns TRUE on success, FALSE on failure. */
271 static gboolean commview_dump(wtap_dumper *wdh,
272 const struct wtap_pkthdr *phdr,
273 const guint8 *pd, int *err)
275 commview_header_t cv_hdr;
278 /* Don't write out anything bigger than we can read.
279 * (The length field in packet headers is 16 bits, which
280 * imposes a hard limit.) */
281 if (phdr->caplen > 65535) {
282 *err = WTAP_ERR_PACKET_TOO_LARGE;
286 memset(&cv_hdr, 0, sizeof(cv_hdr));
288 cv_hdr.data_len = GUINT16_TO_LE((guint16)phdr->caplen);
289 cv_hdr.source_data_len = GUINT16_TO_LE((guint16)phdr->caplen);
292 tm = localtime(&phdr->ts.secs);
293 cv_hdr.year = tm->tm_year + 1900;
294 cv_hdr.month = tm->tm_mon + 1;
295 cv_hdr.day = tm->tm_mday;
296 cv_hdr.hours = tm->tm_hour;
297 cv_hdr.minutes = tm->tm_min;
298 cv_hdr.seconds = tm->tm_sec;
299 cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000);
301 switch(phdr->pkt_encap) {
303 case WTAP_ENCAP_ETHERNET :
304 cv_hdr.flags |= MEDIUM_ETHERNET;
307 case WTAP_ENCAP_IEEE_802_11 :
308 cv_hdr.flags |= MEDIUM_WIFI;
311 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
312 cv_hdr.flags |= MEDIUM_WIFI;
314 cv_hdr.channel = phdr->pseudo_header.ieee_802_11.channel;
315 cv_hdr.rate = (guint8)(phdr->pseudo_header.ieee_802_11.data_rate & 0xFF);
316 cv_hdr.direction = (guint8)((phdr->pseudo_header.ieee_802_11.data_rate >> 8) & 0xFF);
317 cv_hdr.signal_level_percent = phdr->pseudo_header.ieee_802_11.signal_level;
320 case WTAP_ENCAP_TOKEN_RING :
321 cv_hdr.flags |= MEDIUM_TOKEN_RING;
325 *err = WTAP_ERR_UNSUPPORTED_ENCAP;
329 if (!wtap_dump_file_write(wdh, &cv_hdr.data_len, 2, err))
331 if (!wtap_dump_file_write(wdh, &cv_hdr.source_data_len, 2, err))
333 if (!wtap_dump_file_write(wdh, &cv_hdr.version, 1, err))
335 if (!wtap_dump_file_write(wdh, &cv_hdr.year, 2, err))
337 if (!wtap_dump_file_write(wdh, &cv_hdr.month, 1, err))
339 if (!wtap_dump_file_write(wdh, &cv_hdr.day, 1, err))
341 if (!wtap_dump_file_write(wdh, &cv_hdr.hours, 1, err))
343 if (!wtap_dump_file_write(wdh, &cv_hdr.minutes, 1, err))
345 if (!wtap_dump_file_write(wdh, &cv_hdr.seconds, 1, err))
347 if (!wtap_dump_file_write(wdh, &cv_hdr.usecs, 4, err))
349 if (!wtap_dump_file_write(wdh, &cv_hdr.flags, 1, err))
351 if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_percent, 1, err))
353 if (!wtap_dump_file_write(wdh, &cv_hdr.rate, 1, err))
355 if (!wtap_dump_file_write(wdh, &cv_hdr.band, 1, err))
357 if (!wtap_dump_file_write(wdh, &cv_hdr.channel, 1, err))
359 if (!wtap_dump_file_write(wdh, &cv_hdr.direction, 1, err))
361 if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_dbm, 2, err))
363 if (!wtap_dump_file_write(wdh, &cv_hdr.noise_level, 2, err))
365 wdh->bytes_dumped += COMMVIEW_HEADER_SIZE;
367 if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
369 wdh->bytes_dumped += phdr->caplen;