Fix to allow compressed file handling of Ascend captures
[obnox/wireshark/wip.git] / wiretap / ascend.c
1 /* ascend.c
2  *
3  * $Id: ascend.c,v 1.6 1999/09/22 07:37:46 ashokn Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
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.h"
27 #include "buffer.h"
28 #include "ascend.h"
29 #include "file.h"
30
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <ctype.h>
34 #include <string.h>
35
36 /* This module reads the output of the 'wandsession', 'wannext',
37    'wandisplay', and similar commands available on Lucent/Ascend access
38    equipment.  The output is text, with a header line followed by the
39    packet data.  Usage instructions for the commands can be found by
40    searching http://aos.ascend.com .  Ascend likes to move their pages
41    around quite a bit, otherwise I'd put a more specific URL here.
42
43    Example 'wandsess' output data:
44    
45 RECV-iguana:241:(task: B02614C0, time: 1975432.85) 49 octets @ 8003BD94
46   [0000]: FF 03 00 3D C0 06 CA 22 2F 45 00 00 28 6A 3B 40 
47   [0010]: 00 3F 03 D7 37 CE 41 62 12 CF 00 FB 08 20 27 00 
48   [0020]: 50 E4 08 DD D7 7C 4C 71 92 50 10 7D 78 67 C8 00 
49   [0030]: 00 
50 XMIT-iguana:241:(task: B04E12C0, time: 1975432.85) 53 octets @ 8009EB16
51   [0000]: FF 03 00 3D C0 09 1E 31 21 45 00 00 2C 2D BD 40 
52   [0010]: 00 7A 06 D8 B1 CF 00 FB 08 CE 41 62 12 00 50 20 
53   [0020]: 29 7C 4C 71 9C 9A 6A 93 A4 60 12 22 38 3F 10 00 
54   [0030]: 00 02 04 05 B4 
55
56   Note that a maximum of eight rows will be displayed (for a maximum of
57   128 bytes), no matter what the octet count is.
58   
59   When reading a packet, the module prepends an ascend_pkt_hdr to the 
60   data.
61
62  */
63
64 /* How far into the file we should look for packet headers */
65 #define ASCEND_MAX_SEEK 1000000
66
67 /* XXX  Should we replace this with a more generalized array? */
68 /* Magic numbers for Ascend wandsession/wanopening/ether-display data */
69 static const char ascend_xmagic[]  = { 'X', 'M', 'I', 'T', '-' };
70 static const char ascend_rmagic[]  = { 'R', 'E', 'C', 'V', '-' };
71 static const char ascend_w1magic[] = { 'D', 'a', 't', 'e', ':',  };
72 static const char ascend_w2magic[] = { 'W', 'D', '_', 'D', 'I', 'A', 'L', 'O', 'U', 'T', '_', 'D', 'I', 'S', 'P', ':' };
73
74 #define ASCEND_X_SIZE  (sizeof ascend_xmagic  / sizeof ascend_xmagic[0])
75 #define ASCEND_R_SIZE  (sizeof ascend_rmagic  / sizeof ascend_rmagic[0])
76 #define ASCEND_W1_SIZE (sizeof ascend_w1magic / sizeof ascend_w1magic[0])
77 #define ASCEND_W2_SIZE (sizeof ascend_w2magic / sizeof ascend_w2magic[0])
78
79 static int ascend_read(wtap *wth, int *err);
80
81 /* Seeks to the beginning of the next packet, and returns the
82    byte offset.  Returns -1 on failure.  A valid offset is 0; since
83    that causes problems with wtap_loop, offsets are incremented by one. */
84 /* XXX - Handle I/O errors. */
85 static int ascend_seek(wtap *wth, int max_seek)
86 {
87   int byte, bytes_read = 0, date_off = 0;
88   int x_level = 0, r_level = 0, w1_level = 0, w2_level = 0;
89
90   while (((byte = file_getc(wth->fh)) != EOF) && bytes_read < max_seek) {
91     if (byte == ascend_xmagic[x_level]) {
92       x_level++;
93       if (x_level >= ASCEND_X_SIZE) {
94         file_seek(wth->fh, -(ASCEND_X_SIZE), SEEK_CUR);
95         return file_tell(wth->fh) + 1;
96       }
97     } else {
98       x_level = 0;
99     }
100     if (byte == ascend_rmagic[r_level]) {
101       r_level++;
102       if (r_level >= ASCEND_R_SIZE) {
103         file_seek(wth->fh, -(ASCEND_R_SIZE), SEEK_CUR);
104         return file_tell(wth->fh) + 1;
105       }
106     } else {
107       r_level = 0;
108     }
109     if (byte == ascend_w1magic[w1_level]) {
110       w1_level++;
111       if (w1_level >= ASCEND_W1_SIZE) {
112         date_off = file_tell(wth->fh) - ASCEND_W1_SIZE + 1;
113       }
114     } else {
115       w1_level = 0;
116     }
117     if (byte == ascend_w2magic[w2_level]) {
118       w2_level++;
119       if (w2_level >= ASCEND_W2_SIZE && date_off) {
120         file_seek(wth->fh, date_off - 1, SEEK_SET);
121         return date_off;
122       }
123     } else {
124       w2_level = 0;
125     }
126     bytes_read++;
127   }
128   return -1;
129 }
130
131 /* XXX - return -1 on I/O error and actually do something with 'err'. */
132 int ascend_open(wtap *wth, int *err)
133 {
134   int offset;
135   struct stat statbuf;
136
137   file_seek(wth->fh, 0, SEEK_SET);
138   offset = ascend_seek(wth, ASCEND_MAX_SEEK);
139   if (offset < 1) {
140     return 0;
141   }
142
143   wth->data_offset = offset;
144   wth->file_encap = WTAP_ENCAP_ASCEND;
145   wth->file_type = WTAP_FILE_ASCEND;
146   wth->snapshot_length = ASCEND_MAX_PKT_LEN;
147   wth->subtype_read = ascend_read;
148   wth->capture.ascend = g_malloc(sizeof(ascend_t));
149
150   /* MAXen and Pipelines report the time since reboot.  In order to keep 
151      from reporting packet times near the epoch, we subtract the first
152      packet's timestamp from the capture file's ctime, which gives us an
153      offset that we can apply to each packet.
154
155      NOTE: Since we can't fstat a compressed file, assume that the first
156      packet time is 0 and other packets are relative to this.
157    */
158   wth->capture.ascend->inittime = 0;
159   wth->capture.ascend->adjusted = 0;
160   wth->capture.ascend->seek_add = -1;
161
162   init_parse_ascend();
163
164   return 1;
165 }
166
167 /* Read the next packet; called from wtap_loop(). */
168 static int ascend_read(wtap *wth, int *err)
169 {
170   int offset;
171   guint8 *buf = buffer_start_ptr(wth->frame_buffer);
172   ascend_pkthdr header;
173
174   /* (f)lex reads large chunks of the file into memory, so file_tell() doesn't
175      give us the correct location of the packet.  Instead, we seek to the 
176      location of the last packet and try to find the next packet.  In
177      addition, we fool around with the seek offset in case a valid packet
178      starts at the beginning of the file.  */  
179   file_seek(wth->fh, wth->data_offset + wth->capture.ascend->seek_add, SEEK_SET);
180   wth->capture.ascend->seek_add = 0;
181   offset = ascend_seek(wth, ASCEND_MAX_SEEK);
182   if (offset < 1) {
183     return 0;
184   }
185   if (! parse_ascend(wth->fh, buf, &wth->phdr.pseudo_header.ascend, &header, 0)) {
186     *err = WTAP_ERR_BAD_RECORD;
187     return -1;
188   }
189
190   buffer_assure_space(wth->frame_buffer, wth->snapshot_length);
191
192   if (! wth->capture.ascend->adjusted) {
193     wth->capture.ascend->adjusted = 1;
194     wth->capture.ascend->inittime = -header.secs;
195   }
196   wth->phdr.ts.tv_sec = header.secs + wth->capture.ascend->inittime;
197   wth->phdr.ts.tv_usec = header.usecs;
198   wth->phdr.caplen = header.caplen;
199   wth->phdr.len = header.len;
200   wth->phdr.pkt_encap = wth->file_encap;
201   wth->data_offset = offset;
202
203   return offset;
204 }
205
206 int ascend_seek_read (FILE *fh, int seek_off, guint8 *pd, int len)
207 {
208   file_seek(fh, seek_off - 1, SEEK_SET);
209   return parse_ascend(fh, pd, NULL, NULL, len);
210 }