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
38 #include "file_wrappers.h"
41 #include <wsutil/frequency-utils.h>
43 typedef struct commview_header {
45 guint16 source_data_len;
54 guint8 flags; /* Bit-field positions defined below */
55 guint8 signal_level_percent;
59 guint8 direction; /* Or for WiFi, high order byte of
61 gint8 signal_level_dbm;
62 gint8 noise_level; /* In dBm (WiFi only) */
65 #define COMMVIEW_HEADER_SIZE 24
67 /* Bit-field positions for various fields in the flags variable of the header */
68 #define FLAGS_MEDIUM 0x0F
69 #define FLAGS_DECRYPTED 0x10
70 #define FLAGS_BROKEN 0x20
71 #define FLAGS_COMPRESSED 0x40
72 #define FLAGS_RESERVED 0x80
74 /* Values for the band variable of the header */
78 #define BAND_11A_TURBO 0x08
79 #define BAND_SUPERG 0x10
80 #define BAND_PUBLIC_SAFETY 0x20 /* 4.99 GHz public safety */
81 #define BAND_11N_5GHZ 0x40
82 #define BAND_11N_2_4GHZ 0x80
84 /* Capture mediums as defined by the commview file format */
85 #define MEDIUM_ETHERNET 0
87 #define MEDIUM_TOKEN_RING 2
89 static gboolean commview_read(wtap *wth, int *err, gchar **err_info,
91 static gboolean commview_seek_read(wtap *wth, gint64 seek_off,
92 struct wtap_pkthdr *phdr,
93 Buffer *buf, int *err, gchar **err_info);
94 static gboolean commview_read_header(commview_header_t *cv_hdr, FILE_T fh,
95 int *err, gchar **err_info);
96 static gboolean commview_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
97 const guint8 *pd, int *err, gchar **err_info);
99 wtap_open_return_val commview_open(wtap *wth, int *err, gchar **err_info)
101 commview_header_t cv_hdr;
103 if(!commview_read_header(&cv_hdr, wth->fh, err, err_info)) {
104 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
105 return WTAP_OPEN_ERROR;
106 return WTAP_OPEN_NOT_MINE;
109 /* If any of these fields do not match what we expect, bail out. */
110 if(cv_hdr.version != 0 ||
111 cv_hdr.year < 1970 || cv_hdr.year >= 2038 ||
112 cv_hdr.month < 1 || cv_hdr.month > 12 ||
113 cv_hdr.day < 1 || cv_hdr.day > 31 ||
115 cv_hdr.minutes > 59 ||
116 cv_hdr.seconds > 60 ||
117 cv_hdr.signal_level_percent > 100 ||
118 (cv_hdr.flags & FLAGS_RESERVED) != 0 ||
119 ((cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_ETHERNET &&
120 (cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_WIFI &&
121 (cv_hdr.flags & FLAGS_MEDIUM) != MEDIUM_TOKEN_RING))
122 return WTAP_OPEN_NOT_MINE; /* Not our kind of file */
124 /* No file header. Reset the fh to 0 so we can read the first packet */
125 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
126 return WTAP_OPEN_ERROR;
128 /* Set up the pointers to the handlers for this file type */
129 wth->subtype_read = commview_read;
130 wth->subtype_seek_read = commview_seek_read;
132 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COMMVIEW;
133 wth->file_encap = WTAP_ENCAP_PER_PACKET;
134 wth->file_tsprec = WTAP_TSPREC_USEC;
136 return WTAP_OPEN_MINE; /* Our kind of file */
140 commview_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
141 int *err, gchar **err_info)
143 commview_header_t cv_hdr;
147 if(!commview_read_header(&cv_hdr, fh, err, err_info))
150 switch(cv_hdr.flags & FLAGS_MEDIUM) {
152 case MEDIUM_ETHERNET :
153 phdr->pkt_encap = WTAP_ENCAP_ETHERNET;
154 phdr->pseudo_header.eth.fcs_len = -1; /* Unknown */
158 phdr->pkt_encap = WTAP_ENCAP_IEEE_802_11_WITH_RADIO;
159 memset(&phdr->pseudo_header.ieee_802_11, 0, sizeof(phdr->pseudo_header.ieee_802_11));
160 phdr->pseudo_header.ieee_802_11.fcs_len = -1; /* Unknown */
161 phdr->pseudo_header.ieee_802_11.decrypted = FALSE;
162 phdr->pseudo_header.ieee_802_11.datapad = FALSE;
163 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_UNKNOWN;
164 switch (cv_hdr.band) {
167 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11A;
168 phdr->pseudo_header.ieee_802_11.phy_info.info_11a.has_turbo_type = TRUE;
169 phdr->pseudo_header.ieee_802_11.phy_info.info_11a.turbo_type =
170 PHDR_802_11A_TURBO_TYPE_NORMAL;
171 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, FALSE);
175 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11B;
176 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, TRUE);
180 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11G;
181 phdr->pseudo_header.ieee_802_11.phy_info.info_11g.has_mode = TRUE;
182 phdr->pseudo_header.ieee_802_11.phy_info.info_11g.mode =
183 PHDR_802_11G_MODE_NORMAL;
184 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, TRUE);
188 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11A;
189 phdr->pseudo_header.ieee_802_11.phy_info.info_11a.has_turbo_type = TRUE;
190 phdr->pseudo_header.ieee_802_11.phy_info.info_11a.turbo_type =
191 PHDR_802_11A_TURBO_TYPE_TURBO;
192 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, FALSE);
196 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11G;
197 phdr->pseudo_header.ieee_802_11.phy_info.info_11g.has_mode = TRUE;
198 phdr->pseudo_header.ieee_802_11.phy_info.info_11g.mode =
199 PHDR_802_11G_MODE_SUPER_G;
200 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, TRUE);
204 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11N;
205 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, FALSE);
208 case BAND_11N_2_4GHZ:
209 phdr->pseudo_header.ieee_802_11.phy = PHDR_802_11_PHY_11N;
210 frequency = ieee80211_chan_to_mhz(cv_hdr.channel, TRUE);
213 case BAND_PUBLIC_SAFETY:
215 * XXX - what do we do here? What are the channel
216 * numbers? How do we distinguish the several
217 * different flavors of 4.9 GHz frequencies?
226 if (frequency != 0) {
227 phdr->pseudo_header.ieee_802_11.has_frequency = TRUE;
228 phdr->pseudo_header.ieee_802_11.frequency = frequency;
230 phdr->pseudo_header.ieee_802_11.has_channel = TRUE;
231 phdr->pseudo_header.ieee_802_11.channel = cv_hdr.channel;
233 phdr->pseudo_header.ieee_802_11.has_data_rate = TRUE;
234 phdr->pseudo_header.ieee_802_11.data_rate =
235 cv_hdr.rate | (cv_hdr.direction << 8);
237 phdr->pseudo_header.ieee_802_11.has_signal_percent = TRUE;
238 phdr->pseudo_header.ieee_802_11.signal_percent = cv_hdr.signal_level_percent;
241 * XXX - these are positive in captures I've seen; does
242 * that mean that they are the negative of the actual
243 * dBm value? (80 dBm is a bit more power than most
244 * countries' regulatory agencies are likely to allow
245 * any individual to have in their home. :-))
247 * XXX - sometimes these are 0; assume that means that no
250 if (cv_hdr.signal_level_dbm != 0) {
251 phdr->pseudo_header.ieee_802_11.signal_dbm = -cv_hdr.signal_level_dbm;
252 phdr->pseudo_header.ieee_802_11.has_signal_dbm = TRUE;
254 if (cv_hdr.noise_level != 0) {
255 phdr->pseudo_header.ieee_802_11.noise_dbm = -cv_hdr.noise_level;
256 phdr->pseudo_header.ieee_802_11.has_noise_dbm = TRUE;
260 case MEDIUM_TOKEN_RING :
261 phdr->pkt_encap = WTAP_ENCAP_TOKEN_RING;
265 *err = WTAP_ERR_BAD_FILE;
266 *err_info = g_strdup_printf("commview: unsupported encap: %u",
267 cv_hdr.flags & FLAGS_MEDIUM);
271 tm.tm_year = cv_hdr.year - 1900;
272 tm.tm_mon = cv_hdr.month - 1;
273 tm.tm_mday = cv_hdr.day;
274 tm.tm_hour = cv_hdr.hours;
275 tm.tm_min = cv_hdr.minutes;
276 tm.tm_sec = cv_hdr.seconds;
279 phdr->rec_type = REC_TYPE_PACKET;
280 phdr->presence_flags = WTAP_HAS_TS;
282 phdr->len = cv_hdr.data_len;
283 phdr->caplen = cv_hdr.data_len;
285 phdr->ts.secs = mktime(&tm);
286 phdr->ts.nsecs = cv_hdr.usecs * 1000;
288 return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);
292 commview_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
294 *data_offset = file_tell(wth->fh);
296 return commview_read_packet(wth->fh, &wth->phdr, wth->frame_buffer, err,
301 commview_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
302 Buffer *buf, int *err, gchar **err_info)
304 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
307 return commview_read_packet(wth->random_fh, phdr, buf, err, err_info);
311 commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
314 if (!wtap_read_bytes_or_eof(fh, &cv_hdr->data_len, 2, err, err_info))
316 if (!wtap_read_bytes(fh, &cv_hdr->source_data_len, 2, err, err_info))
318 if (!wtap_read_bytes(fh, &cv_hdr->version, 1, err, err_info))
320 if (!wtap_read_bytes(fh, &cv_hdr->year, 2, err, err_info))
322 if (!wtap_read_bytes(fh, &cv_hdr->month, 1, err, err_info))
324 if (!wtap_read_bytes(fh, &cv_hdr->day, 1, err, err_info))
326 if (!wtap_read_bytes(fh, &cv_hdr->hours, 1, err, err_info))
328 if (!wtap_read_bytes(fh, &cv_hdr->minutes, 1, err, err_info))
330 if (!wtap_read_bytes(fh, &cv_hdr->seconds, 1, err, err_info))
332 if (!wtap_read_bytes(fh, &cv_hdr->usecs, 4, err, err_info))
334 if (!wtap_read_bytes(fh, &cv_hdr->flags, 1, err, err_info))
336 if (!wtap_read_bytes(fh, &cv_hdr->signal_level_percent, 1, err, err_info))
338 if (!wtap_read_bytes(fh, &cv_hdr->rate, 1, err, err_info))
340 if (!wtap_read_bytes(fh, &cv_hdr->band, 1, err, err_info))
342 if (!wtap_read_bytes(fh, &cv_hdr->channel, 1, err, err_info))
344 if (!wtap_read_bytes(fh, &cv_hdr->direction, 1, err, err_info))
346 if (!wtap_read_bytes(fh, &cv_hdr->signal_level_dbm, 1, err, err_info))
348 if (!wtap_read_bytes(fh, &cv_hdr->noise_level, 1, err, err_info))
351 /* Convert multi-byte values from little endian to host endian format */
352 cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
353 cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
354 cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
355 cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);
360 /* Returns 0 if we can write out the specified encapsulation type
361 * into a CommView format file. */
362 int commview_dump_can_write_encap(int encap)
366 case WTAP_ENCAP_ETHERNET :
367 case WTAP_ENCAP_IEEE_802_11 :
368 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
369 case WTAP_ENCAP_TOKEN_RING :
370 case WTAP_ENCAP_PER_PACKET :
374 return WTAP_ERR_UNWRITABLE_ENCAP;
378 /* Returns TRUE on success, FALSE on failure;
379 sets "*err" to an error code on failure */
380 gboolean commview_dump_open(wtap_dumper *wdh, int *err _U_)
382 wdh->subtype_write = commview_dump;
384 /* There is no file header to write out */
385 wdh->bytes_dumped = 0;
390 /* Write a record for a packet to a dump file.
391 * Returns TRUE on success, FALSE on failure. */
392 static gboolean commview_dump(wtap_dumper *wdh,
393 const struct wtap_pkthdr *phdr,
394 const guint8 *pd, int *err, gchar **err_info _U_)
396 commview_header_t cv_hdr;
399 /* We can only write packet records. */
400 if (phdr->rec_type != REC_TYPE_PACKET) {
401 *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
405 /* Don't write out anything bigger than we can read.
406 * (The length field in packet headers is 16 bits, which
407 * imposes a hard limit.) */
408 if (phdr->caplen > 65535) {
409 *err = WTAP_ERR_PACKET_TOO_LARGE;
413 memset(&cv_hdr, 0, sizeof(cv_hdr));
415 cv_hdr.data_len = GUINT16_TO_LE((guint16)phdr->caplen);
416 cv_hdr.source_data_len = GUINT16_TO_LE((guint16)phdr->caplen);
419 tm = localtime(&phdr->ts.secs);
420 cv_hdr.year = tm->tm_year + 1900;
421 cv_hdr.month = tm->tm_mon + 1;
422 cv_hdr.day = tm->tm_mday;
423 cv_hdr.hours = tm->tm_hour;
424 cv_hdr.minutes = tm->tm_min;
425 cv_hdr.seconds = tm->tm_sec;
426 cv_hdr.usecs = GUINT32_TO_LE(phdr->ts.nsecs / 1000);
428 switch(phdr->pkt_encap) {
430 case WTAP_ENCAP_ETHERNET :
431 cv_hdr.flags |= MEDIUM_ETHERNET;
434 case WTAP_ENCAP_IEEE_802_11 :
435 cv_hdr.flags |= MEDIUM_WIFI;
438 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO :
439 cv_hdr.flags |= MEDIUM_WIFI;
441 switch (phdr->pseudo_header.ieee_802_11.phy) {
443 case PHDR_802_11_PHY_11A:
445 * If we don't know whether it's turbo, say it's
448 if (!phdr->pseudo_header.ieee_802_11.phy_info.info_11a.has_turbo_type ||
449 phdr->pseudo_header.ieee_802_11.phy_info.info_11a.turbo_type == PHDR_802_11A_TURBO_TYPE_NORMAL)
450 cv_hdr.band = BAND_11A;
452 cv_hdr.band = BAND_11A_TURBO;
455 case PHDR_802_11_PHY_11B:
456 cv_hdr.band = BAND_11B;
459 case PHDR_802_11_PHY_11G:
461 * If we don't know whether it's Super G, say it's
464 if (!phdr->pseudo_header.ieee_802_11.phy_info.info_11g.has_mode)
465 cv_hdr.band = BAND_11G;
467 switch (phdr->pseudo_header.ieee_802_11.phy_info.info_11g.mode) {
469 case PHDR_802_11G_MODE_NORMAL:
470 cv_hdr.band = BAND_11G;
473 case PHDR_802_11G_MODE_SUPER_G:
474 cv_hdr.band = BAND_SUPERG;
478 cv_hdr.band = BAND_11G;
484 case PHDR_802_11_PHY_11N:
486 * Pick the band based on the frequency.
488 if (phdr->pseudo_header.ieee_802_11.has_frequency) {
489 if (phdr->pseudo_header.ieee_802_11.frequency > 2484) {
491 cv_hdr.band = BAND_11N_5GHZ;
494 cv_hdr.band = BAND_11N_2_4GHZ;
497 /* Band is unknown. */
504 * It's not documented how they handle 11ac,
505 * and they don't support the older PHYs.
511 phdr->pseudo_header.ieee_802_11.has_channel ?
512 phdr->pseudo_header.ieee_802_11.channel :
515 phdr->pseudo_header.ieee_802_11.has_data_rate ?
516 (guint8)(phdr->pseudo_header.ieee_802_11.data_rate & 0xFF) :
519 phdr->pseudo_header.ieee_802_11.has_data_rate ?
520 (guint8)((phdr->pseudo_header.ieee_802_11.data_rate >> 8) & 0xFF) :
522 cv_hdr.signal_level_percent =
523 phdr->pseudo_header.ieee_802_11.has_signal_percent ?
524 phdr->pseudo_header.ieee_802_11.signal_percent :
526 cv_hdr.signal_level_dbm =
527 phdr->pseudo_header.ieee_802_11.has_signal_dbm ?
528 -phdr->pseudo_header.ieee_802_11.signal_dbm :
531 phdr->pseudo_header.ieee_802_11.has_noise_dbm ?
532 -phdr->pseudo_header.ieee_802_11.noise_dbm :
536 case WTAP_ENCAP_TOKEN_RING :
537 cv_hdr.flags |= MEDIUM_TOKEN_RING;
541 *err = WTAP_ERR_UNWRITABLE_ENCAP;
545 if (!wtap_dump_file_write(wdh, &cv_hdr.data_len, 2, err))
547 if (!wtap_dump_file_write(wdh, &cv_hdr.source_data_len, 2, err))
549 if (!wtap_dump_file_write(wdh, &cv_hdr.version, 1, err))
551 if (!wtap_dump_file_write(wdh, &cv_hdr.year, 2, err))
553 if (!wtap_dump_file_write(wdh, &cv_hdr.month, 1, err))
555 if (!wtap_dump_file_write(wdh, &cv_hdr.day, 1, err))
557 if (!wtap_dump_file_write(wdh, &cv_hdr.hours, 1, err))
559 if (!wtap_dump_file_write(wdh, &cv_hdr.minutes, 1, err))
561 if (!wtap_dump_file_write(wdh, &cv_hdr.seconds, 1, err))
563 if (!wtap_dump_file_write(wdh, &cv_hdr.usecs, 4, err))
565 if (!wtap_dump_file_write(wdh, &cv_hdr.flags, 1, err))
567 if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_percent, 1, err))
569 if (!wtap_dump_file_write(wdh, &cv_hdr.rate, 1, err))
571 if (!wtap_dump_file_write(wdh, &cv_hdr.band, 1, err))
573 if (!wtap_dump_file_write(wdh, &cv_hdr.channel, 1, err))
575 if (!wtap_dump_file_write(wdh, &cv_hdr.direction, 1, err))
577 if (!wtap_dump_file_write(wdh, &cv_hdr.signal_level_dbm, 2, err))
579 if (!wtap_dump_file_write(wdh, &cv_hdr.noise_level, 2, err))
581 wdh->bytes_dumped += COMMVIEW_HEADER_SIZE;
583 if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
585 wdh->bytes_dumped += phdr->caplen;
591 * Editor modelines - http://www.wireshark.org/tools/modelines.html
596 * indent-tabs-mode: t
599 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
600 * :indentSize=8:tabSize=8:noTabs=false: