28b69da107e18efd526fc57df9776b342763ebc7
[obnox/wireshark/wip.git] / wiretap / ascend.c
1 /* ascend.c
2  *
3  * $Id: ascend.c,v 1.26 2001/11/13 23:55:43 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.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-int.h"
27 #include "buffer.h"
28 #include "ascend.h"
29 #include "ascend-int.h"
30 #include "file_wrappers.h"
31
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #include <ctype.h>
41 #include <string.h>
42
43 /* This module reads the output of the 'wandsession', 'wannext',
44    'wandisplay', and similar commands available on Lucent/Ascend access
45    equipment.  The output is text, with a header line followed by the
46    packet data.  Usage instructions for the commands can be found by
47    searching http://aos.ascend.com .  Ascend likes to move their pages
48    around quite a bit, otherwise I'd put a more specific URL here.
49
50    Example 'wandsess' output data:
51    
52 RECV-iguana:241:(task: B02614C0, time: 1975432.85) 49 octets @ 8003BD94
53   [0000]: FF 03 00 3D C0 06 CA 22 2F 45 00 00 28 6A 3B 40 
54   [0010]: 00 3F 03 D7 37 CE 41 62 12 CF 00 FB 08 20 27 00 
55   [0020]: 50 E4 08 DD D7 7C 4C 71 92 50 10 7D 78 67 C8 00 
56   [0030]: 00 
57 XMIT-iguana:241:(task: B04E12C0, time: 1975432.85) 53 octets @ 8009EB16
58   [0000]: FF 03 00 3D C0 09 1E 31 21 45 00 00 2C 2D BD 40 
59   [0010]: 00 7A 06 D8 B1 CF 00 FB 08 CE 41 62 12 00 50 20 
60   [0020]: 29 7C 4C 71 9C 9A 6A 93 A4 60 12 22 38 3F 10 00 
61   [0030]: 00 02 04 05 B4 
62
63     Example 'wdd' output data:
64
65 Date: 01/12/1990.  Time: 12:22:33
66 Cause an attempt to place call to 14082750382
67 WD_DIALOUT_DISP: chunk 2515EE type IP.
68 (task: 251790, time: 994953.28) 44 octets @ 2782B8
69   [0000]: 00 C0 7B 71 45 6C 00 60 08 16 AA 51 08 00 45 00
70   [0010]: 00 2C 66 1C 40 00 80 06 53 F6 AC 14 00 18 CC 47
71   [0020]: C8 45 0A 31 00 50 3B D9 5B 75 00 00
72
73     (note that the capture whence this came dates back to January
74     *1999*; I presume that either the person who sent it to me
75     hadn't bothered keeping its internal clock set, or that its
76     internal clock or the date it displays in those messages
77     is only loosely connected to reality)
78
79   Note that a maximum of eight rows will be displayed (for a maximum of
80   128 bytes), no matter what the octet count is.
81   
82   When reading a packet, the module prepends an ascend_pkt_hdr to the 
83   data.
84
85  */
86
87 /* How far into the file we should look for packet headers */
88 #define ASCEND_MAX_SEEK 100000
89
90 /* XXX  Should we replace this with a more generalized array? */
91 /* Magic numbers for Ascend wandsession/wanopening/ether-display data */
92 static const char ascend_xmagic[]  = { 'X', 'M', 'I', 'T', '-' };
93 static const char ascend_rmagic[]  = { 'R', 'E', 'C', 'V', '-' };
94 static const char ascend_w1magic[] = { 'D', 'a', 't', 'e', ':',  };
95 static const char ascend_w2magic[] = { 'W', 'D', '_', 'D', 'I', 'A', 'L', 'O', 'U', 'T', '_', 'D', 'I', 'S', 'P', ':' };
96
97 #define ASCEND_X_SIZE  (sizeof ascend_xmagic  / sizeof ascend_xmagic[0])
98 #define ASCEND_R_SIZE  (sizeof ascend_rmagic  / sizeof ascend_rmagic[0])
99 #define ASCEND_W1_SIZE (sizeof ascend_w1magic / sizeof ascend_w1magic[0])
100 #define ASCEND_W2_SIZE (sizeof ascend_w2magic / sizeof ascend_w2magic[0])
101
102 static gboolean ascend_read(wtap *wth, int *err, long *data_offset);
103 static int ascend_seek_read (wtap *wth, long seek_off,
104         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len);
105 static void ascend_close(wtap *wth);
106
107 /* Seeks to the beginning of the next packet, and returns the
108    byte offset at which the heade for that packet begins.
109    Returns -1 on failure.
110
111    If it finds a packet, then, if "wth->capture.ascend" is non-null,
112    it sets "wth->capture.ascend->next_packet_seek_start" to the point
113    at which the seek pointer should be set before this routine is called
114    to find the packet *after* the packet it finds. */
115 /* XXX - Handle I/O errors. */
116 static long ascend_seek(wtap *wth, int max_seek)
117 {
118   int byte, bytes_read = 0;
119   long date_off = -1, cur_off, packet_off;
120   unsigned int r_level = 0, x_level = 0, w1_level = 0, w2_level = 0;
121
122   while (((byte = file_getc(wth->fh)) != EOF) && bytes_read < max_seek) {
123     if (byte == ascend_xmagic[x_level]) {
124       x_level++;
125       if (x_level >= ASCEND_X_SIZE) {
126         /* At what offset are we now? */
127         cur_off = file_tell(wth->fh);
128
129         /* Back up over the header we just read; that's where a read
130            of this packet should start. */
131         packet_off = cur_off - ASCEND_X_SIZE;
132         goto found;
133       }
134     } else {
135       x_level = 0;
136     }
137     if (byte == ascend_rmagic[r_level]) {
138       r_level++;
139       if (r_level >= ASCEND_R_SIZE) {
140         /* At what offset are we now? */
141         cur_off = file_tell(wth->fh);
142
143         /* Back up over the header we just read; that's where a read
144            of this packet should start. */
145         packet_off = cur_off - ASCEND_R_SIZE;
146         goto found;
147       }
148     } else {
149       r_level = 0;
150     }
151     if (byte == ascend_w1magic[w1_level]) {
152       w1_level++;
153       if (w1_level >= ASCEND_W1_SIZE) {
154         /* Get the offset at which the "Date:" header started. */
155         date_off = file_tell(wth->fh) - ASCEND_W1_SIZE;
156       }
157     } else {
158       w1_level = 0;
159     }
160     if (byte == ascend_w2magic[w2_level]) {
161       w2_level++;
162       if (w2_level >= ASCEND_W2_SIZE) {
163         /* At what offset are we now? */
164         cur_off = file_tell(wth->fh);
165         if (date_off != -1) {
166           /* This packet has a date/time header; a read of it should
167              start at the beginning of *that* header. */
168           packet_off = date_off;
169         } else {
170           /* This packet has only a per-packet header.
171              Back up over that header, which we just read; that's where
172              a read of this packet should start. */
173           packet_off = cur_off - ASCEND_W2_SIZE;
174         }
175         goto found;
176       }
177     } else {
178       w2_level = 0;
179     }
180     bytes_read++;
181   }
182   return -1;
183
184 found:
185   /*
186    * The search for the packet after this one should start right
187    * after the header for this packet.  (Ideally, it should
188    * start after the *data* for this one, but we haven't
189    * read that yet.)
190    */
191   if (wth->capture.ascend != NULL)
192     wth->capture.ascend->next_packet_seek_start = cur_off + 1;
193
194   /*
195    * Move to where the read for this packet should start, and return
196    * that seek offset.
197    */
198   file_seek(wth->fh, packet_off, SEEK_SET);
199   return packet_off;
200 }
201
202 /* XXX - return -1 on I/O error and actually do something with 'err'. */
203 int ascend_open(wtap *wth, int *err)
204 {
205   long offset;
206   struct stat statbuf;
207
208   /* We haven't yet allocated a data structure for our private stuff;
209      set the pointer to null, so that "ascend_seek()" knows not to
210      fill it in. */
211   wth->capture.ascend = NULL;
212
213   offset = ascend_seek(wth, ASCEND_MAX_SEEK);
214   if (offset == -1) {
215     return 0;
216   }
217
218   wth->data_offset = offset;
219   wth->file_encap = WTAP_ENCAP_ASCEND;
220   wth->file_type = WTAP_FILE_ASCEND;
221   wth->snapshot_length = ASCEND_MAX_PKT_LEN;
222   wth->subtype_read = ascend_read;
223   wth->subtype_seek_read = ascend_seek_read;
224   wth->subtype_close = ascend_close;
225   wth->capture.ascend = g_malloc(sizeof(ascend_t));
226
227   /* The first packet we want to read is the one that "ascend_seek()"
228      just found; start searching for it at the offset at which it
229      found it. */
230   wth->capture.ascend->next_packet_seek_start = offset;
231
232   /* MAXen and Pipelines report the time since reboot.  In order to keep 
233      from reporting packet times near the epoch, we subtract the first
234      packet's timestamp from the capture file's ctime, which gives us an
235      offset that we can apply to each packet.
236    */
237   fstat(wtap_fd(wth), &statbuf);
238   wth->capture.ascend->inittime = statbuf.st_ctime;
239   wth->capture.ascend->adjusted = 0;
240
241   init_parse_ascend();
242
243   return 1;
244 }
245
246 /* Read the next packet; called from wtap_loop(). */
247 static gboolean ascend_read(wtap *wth, int *err, long *data_offset)
248 {
249   long offset;
250   guint8 *buf = buffer_start_ptr(wth->frame_buffer);
251   ascend_pkthdr header;
252
253   /* (f)lex reads large chunks of the file into memory, so file_tell() doesn't
254      give us the correct location of the packet.  Instead, we seek to the 
255      offset after the header of the previous packet and try to find the next
256      packet.  */
257   file_seek(wth->fh, wth->capture.ascend->next_packet_seek_start, SEEK_SET);
258   offset = ascend_seek(wth, ASCEND_MAX_SEEK);
259   if (offset == -1) {
260     *err = 0;           /* XXX - assume, for now, that it's an EOF */
261     return FALSE;
262   }
263   if (! parse_ascend(wth->fh, buf, &wth->pseudo_header.ascend, &header, 0)) {
264     *err = WTAP_ERR_BAD_RECORD;
265     return FALSE;
266   }
267
268   buffer_assure_space(wth->frame_buffer, wth->snapshot_length);
269
270   if (! wth->capture.ascend->adjusted) {
271     wth->capture.ascend->adjusted = 1;
272     if (header.start_time != 0) {
273       /*
274        * Capture file contained a date and time.
275        * We do this only if this is the very first packet we've seen -
276        * i.e., if "wth->capture.ascend->adjusted" is false - because
277        * if we get a date and time after the first packet, we can't
278        * go back and adjust the time stamps of the packets we've already
279        * processed, and basing the time stamps of this and following
280        * packets on the time stamp from the file text rather than the
281        * ctime of the capture file means times before this and after
282        * this can't be compared.
283        */
284       wth->capture.ascend->inittime = header.start_time;
285     }
286     if (wth->capture.ascend->inittime > header.secs)
287       wth->capture.ascend->inittime -= header.secs;
288   }
289   wth->phdr.ts.tv_sec = header.secs + wth->capture.ascend->inittime;
290   wth->phdr.ts.tv_usec = header.usecs;
291   wth->phdr.caplen = header.caplen;
292   wth->phdr.len = header.len;
293   wth->phdr.pkt_encap = wth->file_encap;
294   wth->data_offset = offset;
295
296   *data_offset = offset;
297   return TRUE;
298 }
299
300 static int ascend_seek_read (wtap *wth, long seek_off,
301         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
302 {
303   file_seek(wth->random_fh, seek_off, SEEK_SET);
304   return parse_ascend(wth->random_fh, pd, &pseudo_header->ascend, NULL, len);
305 }
306
307 static void ascend_close(wtap *wth)
308 {
309   g_free(wth->capture.ascend);
310 }