From Marc Milgram: e-mail address update.
[obnox/wireshark/wip.git] / wiretap / dbs-etherwatch.c
1 /* dbs-etherwatch.c
2  *
3  * $Id: dbs-etherwatch.c,v 1.11 2003/01/17 23:54:19 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "wtap-int.h"
27 #include "buffer.h"
28 #include "dbs-etherwatch.h"
29 #include "file_wrappers.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 /* This module reads the text output of the 'DBS-ETHERTRACE' command in VMS
37  * It was initially based on vms.c.
38  */
39
40 /*
41    Example 'TCPIPTRACE' output data:
42 ETHERWATCH  X5-008
43 42 names and addresses were loaded
44 Reading recorded data from PERSISTENCE
45 ------------------------------------------------------------------------------
46 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
47 Protocol 08-00 00 00-00-00-00-00,   60 byte buffer at 10-OCT-2001 10:20:45.16
48   [E..<8.....Ò.....]-    0-[45 00 00 3C 38 93 00 00 1D 06 D2 12 80 93 11 1A]
49   [...Ö.Ò...(¤.....]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 90 00 00 00 00]
50   [.....½.....´....]-   32-[A0 02 FF FF 95 BD 00 00 02 04 05 B4 03 03 04 01]
51   [......å.....    ]-   48-[01 01 08 0A 90 90 E5 14 00 00 00 00]
52 ------------------------------------------------------------------------------
53 From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB]
54 Protocol 08-00 00 00-00-00-00-00,   50 byte buffer at 10-OCT-2001 10:20:45.17
55   [E..(8.....Ò%....]-    0-[45 00 00 28 38 94 00 00 1D 06 D2 25 80 93 11 1A]
56   [...Ö.Ò...(¤.Z.4w]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 91 5A 1C 34 77]
57   [P.#(Ás.....´....]-   32-[50 10 23 28 C1 73 00 00 02 04 05 B4 03 03 00 00]
58   [..              ]-   48-[02 04]
59  */
60
61 /* Magic text to check for DBS-ETHERWATCH-ness of file */
62 static const char dbs_etherwatch_hdr_magic[]  =
63 { 'E', 'T', 'H', 'E', 'R', 'W', 'A', 'T', 'C', 'H', ' ', ' '};
64 #define DBS_ETHERWATCH_HDR_MAGIC_SIZE  \
65         (sizeof dbs_etherwatch_hdr_magic  / sizeof dbs_etherwatch_hdr_magic[0])
66
67 /* Magic text for start of packet */
68 static const char dbs_etherwatch_rec_magic[]  =
69 {'F', 'r', 'o', 'm', ' '};
70 #define DBS_ETHERWATCH_REC_MAGIC_SIZE \
71         (sizeof dbs_etherwatch_rec_magic  / sizeof dbs_etherwatch_rec_magic[0])
72
73 /*
74  * XXX - is this the biggest packet we can get?
75  */
76 #define DBS_ETHERWATCH_MAX_PACKET_LEN   16384
77
78 static gboolean dbs_etherwatch_read(wtap *wth, int *err, long *data_offset);
79 static gboolean dbs_etherwatch_seek_read(wtap *wth, long seek_off,
80         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len, int *err);
81 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
82         long byte_offset);
83 static gboolean parse_dbs_etherwatch_hex_dump(FILE_T fh, int pkt_len,
84         guint8* buf, int *err);
85 static int parse_dbs_etherwatch_rec_hdr(wtap *wth, FILE_T fh, int *err);
86
87 /* Seeks to the beginning of the next packet, and returns the
88    byte offset.  Returns -1 on failure, and sets "*err" to the error. */
89 static long dbs_etherwatch_seek_next_packet(wtap *wth, int *err)
90 {
91   int byte;
92   unsigned int level = 0;
93   long cur_off;
94
95   while ((byte = file_getc(wth->fh)) != EOF) {
96     if (byte == dbs_etherwatch_rec_magic[level]) {
97       level++;
98       if (level >= DBS_ETHERWATCH_REC_MAGIC_SIZE) {
99         /* note: we're leaving file pointer right after the magic characters */
100         cur_off = file_tell(wth->fh);
101         if (cur_off == -1) {
102           /* Error. */
103           *err = file_error(wth->fh);
104           return -1;
105         }
106         return cur_off + 1;
107       }
108     } else {
109       level = 0;
110     }
111   }
112   if (file_eof(wth->fh)) {
113     /* We got an EOF. */
114     *err = 0;
115   } else {
116     /* We (presumably) got an error (there's no equivalent to "ferror()"
117        in zlib, alas, so we don't have a wrapper to check for an error). */
118     *err = file_error(wth->fh);
119   }
120   return -1;
121 }
122
123 #define DBS_ETHERWATCH_HEADER_LINES_TO_CHECK    200
124 #define DBS_ETHERWATCH_LINE_LENGTH              240
125
126 /* Look through the first part of a file to see if this is
127  * a DBS Ethertrace text trace file.
128  *
129  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
130  * if we get an I/O error, "*err" will be set to a non-zero value.
131  */
132 static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err)
133 {
134         char    buf[DBS_ETHERWATCH_LINE_LENGTH];
135         int     line, byte;
136         unsigned int reclen, i, level;
137
138         buf[DBS_ETHERWATCH_LINE_LENGTH-1] = 0;
139
140         for (line = 0; line < DBS_ETHERWATCH_HEADER_LINES_TO_CHECK; line++) {
141                 if (file_gets(buf, DBS_ETHERWATCH_LINE_LENGTH, wth->fh)!=NULL){
142
143                         reclen = strlen(buf);
144                         if (reclen < DBS_ETHERWATCH_HDR_MAGIC_SIZE)
145                                 continue;
146
147                         level = 0;
148                         for (i = 0; i < reclen; i++) {
149                                 byte = buf[i];
150                                 if (byte == dbs_etherwatch_hdr_magic[level]) {
151                                         level++;
152                                         if (level >=
153                                               DBS_ETHERWATCH_HDR_MAGIC_SIZE) {
154                                                 return TRUE;
155                                         }
156                                 }
157                                 else
158                                         level = 0;
159                         }
160                 }
161                 else {
162                         /* EOF or error. */
163                         if (file_eof(wth->fh))
164                                 *err = 0;
165                         else
166                                 *err = file_error(wth->fh);
167                         return FALSE;
168                 }
169         }
170         *err = 0;
171         return FALSE;
172 }
173
174
175 int dbs_etherwatch_open(wtap *wth, int *err)
176 {
177         /* Look for DBS ETHERWATCH header */
178         if (!dbs_etherwatch_check_file_type(wth, err)) {
179                 if (*err == 0)
180                         return 0;
181                 else
182                         return -1;
183         }
184
185         wth->data_offset = 0;
186         wth->file_encap = WTAP_ENCAP_RAW_IP;
187         wth->file_type = WTAP_FILE_DBS_ETHERWATCH;
188         wth->snapshot_length = 0;       /* not known */
189         wth->subtype_read = dbs_etherwatch_read;
190         wth->subtype_seek_read = dbs_etherwatch_seek_read;
191
192         return 1;
193 }
194
195 /* Find the next packet and parse it; called from wtap_loop(). */
196 static gboolean dbs_etherwatch_read(wtap *wth, int *err, long *data_offset)
197 {
198         long    offset;
199         guint8  *buf;
200         int     pkt_len;
201
202         /* Find the next packet */
203         offset = dbs_etherwatch_seek_next_packet(wth, err);
204         if (offset < 1)
205                 return FALSE;
206
207         /* Parse the header */
208         pkt_len = parse_dbs_etherwatch_rec_hdr(wth, wth->fh, err);
209         if (pkt_len == -1)
210                 return FALSE;
211
212         /* Make sure we have enough room for the packet */
213         buffer_assure_space(wth->frame_buffer, DBS_ETHERWATCH_MAX_PACKET_LEN);
214         buf = buffer_start_ptr(wth->frame_buffer);
215
216         /* Convert the ASCII hex dump to binary data */
217         if (!parse_dbs_etherwatch_hex_dump(wth->fh, pkt_len, buf, err))
218                 return FALSE;
219
220         wth->data_offset = offset;
221         *data_offset = offset;
222         return TRUE;
223 }
224
225 /* Used to read packets in random-access fashion */
226 static gboolean
227 dbs_etherwatch_seek_read (wtap *wth, long seek_off,
228         union wtap_pseudo_header *pseudo_header _U_,
229         guint8 *pd, int len, int *err)
230 {
231         int     pkt_len;
232
233         if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
234                 return FALSE;
235
236         pkt_len = parse_dbs_etherwatch_rec_hdr(NULL, wth->random_fh, err);
237
238         if (pkt_len != len) {
239                 if (pkt_len != -1)
240                         *err = WTAP_ERR_BAD_RECORD;
241                 return FALSE;
242         }
243
244         return parse_dbs_etherwatch_hex_dump(wth->random_fh, pkt_len, pd, err);
245 }
246
247 /* Parses a packet record header. */
248 static int
249 parse_dbs_etherwatch_rec_hdr(wtap *wth, FILE_T fh, int *err)
250 {
251         char    line[DBS_ETHERWATCH_LINE_LENGTH];
252         int     num_items_scanned;
253         int     pkt_len, csec;
254         struct tm time;
255         char mon[4];
256         guchar *p;
257         static guchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
258
259         pkt_len = 0;
260
261         /* Our file pointer should be on the first line containing the
262          * summary information for a packet. Read in that line and
263          * extract the useful information
264          */
265         if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
266                 *err = file_error(fh);
267                 if (*err == 0) {
268                         *err = WTAP_ERR_SHORT_READ;
269                 }
270                 return -1;
271         }
272
273         /* But that line only contains the mac addresses, so we will ignore
274            that line for now.  Read the next line */
275         if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
276                 *err = file_error(fh);
277                 if (*err == 0) {
278                         *err = WTAP_ERR_SHORT_READ;
279                 }
280                 return -1;
281         }
282
283         num_items_scanned = sscanf(line+33, "%d byte buffer at %d-%3s-%d %d:%d:%d.%d",
284                                    &pkt_len,
285                                    &time.tm_mday, mon,
286                                    &time.tm_year, &time.tm_hour, &time.tm_min,
287                                    &time.tm_sec, &csec);
288
289         if (num_items_scanned != 8) {
290                 *err = WTAP_ERR_BAD_RECORD;
291                 return -1;
292         }
293
294         if (wth) {
295                 p = strstr(months, mon);
296                 if (p)
297                         time.tm_mon = (p - months) / 3;
298                 time.tm_year -= 1900;
299
300                 time.tm_isdst = -1;
301                 wth->phdr.ts.tv_sec = mktime(&time);
302
303                 wth->phdr.ts.tv_usec = csec * 10000;
304                 wth->phdr.caplen = pkt_len;
305                 wth->phdr.len = pkt_len;
306                 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
307         }
308
309         return pkt_len;
310 }
311
312 /* Converts ASCII hex dump to binary data */
313 static gboolean
314 parse_dbs_etherwatch_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err)
315 {
316         guchar  line[DBS_ETHERWATCH_LINE_LENGTH];
317         int     i, hex_lines;
318
319         /* Calculate the number of hex dump lines, each
320          * containing 16 bytes of data */
321         hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);
322
323         for (i = 0; i < hex_lines; i++) {
324                 if (file_gets(line, DBS_ETHERWATCH_LINE_LENGTH, fh) == NULL) {
325                         *err = file_error(fh);
326                         if (*err == 0) {
327                                 *err = WTAP_ERR_SHORT_READ;
328                         }
329                         return FALSE;
330                 }
331                 if (!parse_single_hex_dump_line(line, buf, i * 16)) {
332                         *err = WTAP_ERR_BAD_RECORD;
333                         return FALSE;
334                 }
335         }
336         return TRUE;
337 }
338
339 /*
340           1         2         3         4
341 0123456789012345678901234567890123456789012345
342   [E..(8.....Ò.....]-    0-[45 00 00 28 38 9B 00 00 1D 06 D2 1E 80 93 11 1A]
343   [...Ö.Ò...(¤¿Z.4y]-   16-[80 93 80 D6 02 D2 02 03 00 28 A4 BF 5A 1C 34 79]
344   [P.#(ÁC...00000..]-   32-[50 10 23 28 C1 43 00 00 03 30 30 30 30 30 00 00]
345   [.0              ]-   48-[03 30]
346 */
347
348 #define START_POS       28
349 #define HEX_LENGTH      ((16 * 2) + 15) /* sixteen clumps of 2 bytes with 15 inner spaces */
350 /* Take a string representing one line from a hex dump and converts the
351  * text to binary data. We check the printed offset with the offset
352  * we are passed to validate the record. We place the bytes in the buffer
353  * at the specified offset.
354  *
355  * In the process, we're going to write all over the string.
356  *
357  * Returns TRUE if good hex dump, FALSE if bad.
358  */
359 static gboolean
360 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset) {
361
362         int             pos, i;
363         char            *s;
364         long            value;
365
366
367         /* Get the byte_offset directly from the record */
368         rec[26] = '\0';
369         s = rec + 21;
370         value = strtol(s, NULL, 10);
371
372         if (value != byte_offset) {
373                 return FALSE;
374         }
375
376         /* Go through the substring representing the values and:
377          *      1. Replace any spaces with '0's
378          *      2. Place \0's every 3 bytes (to terminate the string)
379          *
380          * Then read the eight sets of hex bytes
381          */
382
383         for (pos = START_POS; pos < START_POS + HEX_LENGTH; pos++) {
384                 if (rec[pos] == ' ') {
385                         rec[pos] = '0';
386                 }
387         }
388
389         pos = START_POS;
390         for (i = 0; i < 16; i++) {
391                 rec[pos+2] = '\0';
392
393                 buf[byte_offset + i] = (guint8) strtoul(&rec[pos], NULL, 16);
394                 pos += 3;
395         }
396
397         return TRUE;
398 }