2 * Routines for opening .dcf capture files created by Daintree's
3 * Sensor Network Analyzer for 802.15.4 radios
4 * Copyright 2009, Exegin Technologies Limited <fff@exegin.com>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * Started with packetlogger.c as a template, but little packetlogger code
13 * remains. Borrowed many snippets from dbs-etherwatch.c, the
14 * daintree_sna_hex_char function having the largest chunk.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
32 /* This module reads capture files saved by Daintree's Sensor Network Analyzer.
33 * Daintree captures are plain text files with a two line header,
34 * followed by packet records, one per line, with whitespace separated fields
35 * consisting of: packet number, time, bytes of capture data, capture data,
36 * unknown, unknown, signal strength?, unknown, etc, and terminated with CRLF.
39 /* Example capture file:
42 # SNA v2.2.0.4 SUS:20090709 ACT:819705
43 1 1233783799.326400 10 030809ffffffff07ffff 42 1 -69 25 2 0 1 32767
44 2 1233783799.477440 5 02000bffff 110 1 -44 25 6 0 1 32767
45 3 1233783799.809920 5 020013ffff 107 1 -45 25 43 0 1 3276
63 #include "file_wrappers.h"
64 #include "daintree-sna.h"
66 typedef struct daintree_sna_header {
69 } daintree_sna_header_t;
71 #define DAINTREE_SNA_HEADER_SIZE 2
74 static const char daintree_magic_text[] =
75 { '#', 'F', 'o', 'r', 'm', 'a', 't', '=' };
77 #define DAINTREE_MAGIC_TEXT_SIZE (sizeof daintree_magic_text)
78 #define DAINTREE_MAX_LINE_SIZE 512
79 #define READDATA_BUF_SIZE (DAINTREE_MAX_LINE_SIZE/2)
80 #define SEEKDATA_BUF_SIZE (DAINTREE_MAX_LINE_SIZE/2)
81 #define READDATA_MAX_FIELD_SIZE "255" /* DAINTREE_MAX_LINE_SIZE/2 -1 */
82 #define SEEKDATA_MAX_FIELD_SIZE "255" /* DAINTREE_MAX_LINE_SIZE/2 -1 */
84 #define COMMENT_LINE daintree_magic_text[0]
86 static char readLine[DAINTREE_MAX_LINE_SIZE];
87 static char seekLine[DAINTREE_MAX_LINE_SIZE];
89 static char readData[READDATA_BUF_SIZE];
90 static char seekData[SEEKDATA_BUF_SIZE];
92 static gboolean daintree_sna_read(wtap *wth, int *err, gchar **err_info _U_,
95 static gboolean daintree_sna_seek_read(wtap *wth, gint64 seek_off,
96 union wtap_pseudo_header *pseudo_header _U_,
97 guchar *pd, int len, int *err,
98 gchar **err_info _U_);
100 static guint daintree_sna_hex_char(guchar *str, int *err);
102 /* Open a file and determine if it's a Daintree file */
103 int daintree_sna_open(wtap *wth, int *err _U_, gchar **err_info _U_)
107 /* get first line of file header */
108 if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh)==NULL) return 0;
109 wth->data_offset += strlen(readLine);
111 /* check magic text */
113 while (i < DAINTREE_MAGIC_TEXT_SIZE) {
114 if (readLine[i] != daintree_magic_text[i]) return 0; /* not daintree format */
118 /* read second header line */
119 if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh)==NULL) return 0;
120 wth->data_offset += strlen(readLine);
121 if (readLine[0] != COMMENT_LINE) return 0; /* daintree files have a two line header */
123 /* set up the pointers to the handlers for this file type */
124 wth->subtype_read = daintree_sna_read;
125 wth->subtype_seek_read = daintree_sna_seek_read;
127 /* set up for file type */
128 wth->file_type = WTAP_FILE_DAINTREE_SNA;
129 wth->file_encap = WTAP_ENCAP_IEEE802_15_4_NOFCS;
130 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
131 wth->snapshot_length = 0; /* not available in header */
133 return 1; /* it's a Daintree file */
136 /* Read the capture file sequentially
137 * Wireshark scans the file with sequential reads during preview and initial display. */
139 daintree_sna_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset)
143 *data_offset = wth->data_offset;
145 /* we've only seen file header lines starting with '#', but
146 * if others appear in the file, they are tossed */
148 if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh) == NULL) {
149 *err = file_error(wth->fh);
150 return FALSE; /* all done */
152 wth->data_offset += strlen(readLine);
153 } while (readLine[0] == COMMENT_LINE);
155 /* parse one line of capture data */
156 if (sscanf(readLine, "%*s %" G_GINT64_MODIFIER "u.%d %u %" READDATA_MAX_FIELD_SIZE "s",
157 &seconds, &wth->phdr.ts.nsecs, &wth->phdr.len, readData) != 4) {
158 *err = WTAP_ERR_BAD_RECORD;
159 *err_info = g_strdup("daintree_sna: invalid read record");
163 /* Daintree doesn't store the FCS, but pads end of packet with 0xffff, which we toss */
164 if (wth->phdr.len <= FCS_LENGTH) {
165 *err = WTAP_ERR_BAD_RECORD;
166 *err_info = g_strdup_printf("daintree_sna: packet length <= %u bytes, no frame data present",
170 wth->phdr.len -= FCS_LENGTH;
172 wth->phdr.ts.secs = (time_t) seconds;
173 wth->phdr.ts.nsecs *= 1000; /* convert mS to nS */
175 /* convert packet data from ASCII string to hex, sanity-check its length against what we assume is the
176 * packet length field, write data to frame buffer */
177 if ((wth->phdr.caplen = daintree_sna_hex_char(readData, err)) > FCS_LENGTH) {
178 /* Daintree doesn't store the FCS, but pads end of packet with 0xffff, which we toss */
179 wth->phdr.caplen -= FCS_LENGTH;
180 if (wth->phdr.caplen <= wth->phdr.len) {
181 buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
182 memcpy(buffer_start_ptr(wth->frame_buffer), readData, wth->phdr.caplen);
184 *err = WTAP_ERR_BAD_RECORD;
185 *err_info = g_strdup_printf("daintree_sna: capture length (%u) > packet length (%u)",
186 wth->phdr.caplen, wth->phdr.len);
190 *err = WTAP_ERR_BAD_RECORD;
191 *err_info = g_strdup("daintree_sna: invalid packet data");
198 /* Read the capture file randomly
199 * Wireshark opens the capture file for random access when displaying user-selected packets */
201 daintree_sna_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
202 *pseudo_header _U_, guchar *pd, int len, int *err,
203 gchar **err_info _U_)
207 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
210 /* It appears only file header lines start with '#', but
211 * if we find any others, we toss them */
213 if (file_gets(seekLine, DAINTREE_MAX_LINE_SIZE, wth->random_fh) == NULL) {
214 *err = file_error(wth->random_fh);
215 return FALSE; /* all done */
217 } while (seekLine[0] == COMMENT_LINE);
219 /* ignore all but packet data, since the sequential read pass stored everything else */
220 if (sscanf(seekLine, "%*s %*u.%*u %*u %" SEEKDATA_MAX_FIELD_SIZE "s", seekData) != 1) {
221 *err = WTAP_ERR_BAD_RECORD;
222 *err_info = g_strdup("daintree_sna: corrupted seek record");
226 /* convert packet data from ASCII hex string to guchar */
227 if ((pkt_len = daintree_sna_hex_char(seekData, err)) <= FCS_LENGTH) {
228 *err = WTAP_ERR_BAD_RECORD;
229 *err_info = g_strdup("daintree_sna: corrupted packet data");
233 pkt_len -= FCS_LENGTH; /* remove padded bytes that Daintree stores instead of FCS */
235 if (pkt_len == (guint) len) {
236 /* move to frame buffer for dissection */
237 memcpy(pd, seekData, pkt_len);
239 *err = WTAP_ERR_BAD_RECORD;
240 *err_info = g_strdup("daintree-sna: corrupted frame");
247 /* Convert an ASCII hex string to guchar */
249 daintree_sna_hex_char(guchar *str, int *err _U_) {
253 p = str; /* overlay source buffer */
255 /* convert hex string to guchar */
257 if (!isxdigit((guchar)*str)) return 0;
258 /* most significant nibble */
259 if(isdigit((guchar)*str)) {
260 *p = (*str - '0') << 4;
262 *p = ((tolower(*str) - 'a') + 10) << 4;
266 if (!isxdigit((guchar)*str)) return 0;
267 /* least significant nibble */
268 if(isdigit((guchar)*str)) {
271 *p += (tolower(*str) - 'a') + 10;
275 /* next byte in buffer */