Do not give a warning for not implemented OID if value is a "NULL tag".
[obnox/wireshark/wip.git] / wiretap / daintree-sna.c
1 /* daintree_sna.c
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>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
15  * 
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.
20  *
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.
25  *
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,
29  * USA.
30  */
31
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.
37  */
38
39 /* Example capture file:
40  
41 #Format=4
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
46
47 */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include <glib.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <errno.h>
57 #include <string.h>
58 #include <ctype.h>
59
60 #include "wtap.h"
61 #include "wtap-int.h"
62 #include "buffer.h"
63 #include "file_wrappers.h"
64 #include "daintree-sna.h"
65
66 typedef struct daintree_sna_header {
67         guint32 len;
68         guint64 ts;
69 } daintree_sna_header_t;
70
71 #define DAINTREE_SNA_HEADER_SIZE 2
72 #define FCS_LENGTH 2
73
74 static const char daintree_magic_text[] =
75 { '#', 'F', 'o', 'r', 'm', 'a', 't', '=' };
76
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 */
83
84 #define COMMENT_LINE daintree_magic_text[0]
85
86 static char readLine[DAINTREE_MAX_LINE_SIZE];
87 static char seekLine[DAINTREE_MAX_LINE_SIZE];
88
89 static char readData[READDATA_BUF_SIZE];
90 static char seekData[SEEKDATA_BUF_SIZE];
91
92 static gboolean daintree_sna_read(wtap *wth, int *err, gchar **err_info _U_,
93         gint64 *data_offset);
94
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_);
99
100 static guint daintree_sna_hex_char(guchar *str, int *err);
101
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_)
104 {
105         guint i; 
106
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);
110
111         /* check magic text */
112         i = 0;
113         while (i < DAINTREE_MAGIC_TEXT_SIZE) {
114                 if (readLine[i] != daintree_magic_text[i]) return 0; /* not daintree format */
115                 i++;
116         } 
117
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 */
122
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;
126
127         /* set up for file type */
128         wth->file_type = WTAP_FILE_DAINTREE_SNA;
129         wth->file_encap = WTAP_ENCAP_IEEE802_15_4;
130         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
131
132         return 1; /* it's a Daintree file */
133 }
134
135 /* Read the capture file sequentially
136  * Wireshark scans the file with sequential reads during preview and initial display. */
137 static gboolean
138 daintree_sna_read(wtap *wth, int *err, gchar **err_info _U_, gint64 *data_offset)
139 {
140         guint64 seconds;
141
142         *data_offset = wth->data_offset;
143
144         /* we've only seen file header lines starting with '#', but
145          * if others appear in the file, they are tossed */
146         do {
147                 if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh) == NULL) {
148                         *err = file_error(wth->fh);
149                         return FALSE; /* all done */
150                 }
151                 wth->data_offset += strlen(readLine);
152         } while (readLine[0] == COMMENT_LINE);
153
154         /* parse one line of capture data */
155         if (sscanf(readLine, "%*s %" G_GINT64_MODIFIER "u.%d %u %" READDATA_MAX_FIELD_SIZE "s",
156                 &seconds, &wth->phdr.ts.nsecs,
157                 &wth->phdr.len, readData) != 4) {
158                         *err = WTAP_ERR_BAD_RECORD;
159                         *err_info = g_strdup("daintree_sna: invalid read record");
160                         return FALSE;
161         }
162
163         wth->phdr.ts.secs = (time_t) seconds;
164         wth->phdr.ts.nsecs *= 1000; /* convert mS to nS */
165
166         /* convert packet data from ASCII string to hex, sanity-check its length against what we assume is the
167          * packet length field, write data to frame buffer */
168         if ((wth->phdr.caplen = daintree_sna_hex_char(readData, err)) > FCS_LENGTH) {
169                 if (wth->phdr.caplen <= wth->phdr.len) {
170                         /* Daintree doesn't store the FCS, but pads end of packet with 0xffff, which we toss */
171                         wth->phdr.caplen -= FCS_LENGTH;
172                         buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
173                         memcpy(buffer_start_ptr(wth->frame_buffer), readData, wth->phdr.caplen);
174                 } else {
175                         *err = WTAP_ERR_BAD_RECORD;
176                         *err_info = g_strdup_printf("daintree_sna: capture length (%d) > packet length (%d)",
177                                 wth->phdr.caplen, wth->phdr.len);
178                         return FALSE;
179                 }
180         } else {
181                 *err = WTAP_ERR_BAD_RECORD;
182                 *err_info = g_strdup("daintree_sna: invalid packet data");
183                 return FALSE;
184         }
185
186         return TRUE;
187 }
188
189 /* Read the capture file randomly 
190  * Wireshark opens the capture file for random access when displaying user-selected packets */
191 static gboolean
192 daintree_sna_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
193         *pseudo_header _U_, guchar *pd, int len, int *err,
194         gchar **err_info _U_)
195 {
196         guint pkt_len;
197
198         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
199                 return FALSE;
200
201         /* It appears only file header lines start with '#', but
202          * if we find any others, we toss them */
203         do {
204                 if (file_gets(seekLine, DAINTREE_MAX_LINE_SIZE, wth->random_fh) == NULL) {
205                         *err = file_error(wth->random_fh);
206                         return FALSE; /* all done */
207                 }
208         } while (seekLine[0] == COMMENT_LINE);
209
210         /* ignore all but packet data, since the sequential read pass stored everything else */
211         if (sscanf(seekLine, "%*s %*u.%*u %*u %" SEEKDATA_MAX_FIELD_SIZE "s", seekData) != 1) {
212                 *err = WTAP_ERR_BAD_RECORD;
213                 *err_info = g_strdup("daintree_sna: corrupted seek record");
214                 return FALSE;
215         }
216
217         /* convert packet data from ASCII hex string to guchar */
218         if ((pkt_len = daintree_sna_hex_char(seekData, err)) <= FCS_LENGTH) {
219                 *err = WTAP_ERR_BAD_RECORD;
220                 *err_info = g_strdup("daintree_sna: corrupted packet data");
221                 return FALSE;
222         }
223
224         pkt_len -= FCS_LENGTH; /* remove padded bytes that Daintree stores instead of FCS */
225
226         if (pkt_len == (guint) len) {
227                 /* move to frame buffer for dissection */
228                 memcpy(pd, seekData, pkt_len);
229         } else {
230                 *err = WTAP_ERR_BAD_RECORD;
231                 *err_info = g_strdup("daintree-sna: corrupted frame");
232                 return FALSE;
233         } 
234
235         return TRUE;
236 }
237
238 /* Convert an ASCII hex string to guchar */
239 static guint
240 daintree_sna_hex_char(guchar *str, int *err _U_) {
241         guint bytes;
242         guchar *p;
243
244         p = str; /* overlay source buffer */
245         bytes = 0;
246         /* convert hex string to guchar */
247         while(*str) {
248                 if (!isxdigit((guchar)*str)) return 0;
249                 /* most significant nibble */
250                 if(isdigit((guchar)*str)) {
251                         *p = (*str - '0') << 4;
252                 } else {
253                         *p = ((tolower(*str) - 'a') + 10) << 4;
254                 }
255                 str++;
256
257                 if (!isxdigit((guchar)*str)) return 0;
258                 /* least significant nibble */
259                 if(isdigit((guchar)*str)) {
260                         *p += *str - '0';
261                 } else {
262                         *p += (tolower(*str) - 'a') + 10;
263                 }
264                 str++;
265
266                 /* next byte in buffer */
267                 p++;
268                 bytes++;
269         }
270
271         return bytes;
272 }