3 * $Id: ascend.c,v 1.8 1999/10/08 07:45:31 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
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.
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.
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.
29 #include "ascend-int.h"
37 /* This module reads the output of the 'wandsession', 'wannext',
38 'wandisplay', and similar commands available on Lucent/Ascend access
39 equipment. The output is text, with a header line followed by the
40 packet data. Usage instructions for the commands can be found by
41 searching http://aos.ascend.com . Ascend likes to move their pages
42 around quite a bit, otherwise I'd put a more specific URL here.
44 Example 'wandsess' output data:
46 RECV-iguana:241:(task: B02614C0, time: 1975432.85) 49 octets @ 8003BD94
47 [0000]: FF 03 00 3D C0 06 CA 22 2F 45 00 00 28 6A 3B 40
48 [0010]: 00 3F 03 D7 37 CE 41 62 12 CF 00 FB 08 20 27 00
49 [0020]: 50 E4 08 DD D7 7C 4C 71 92 50 10 7D 78 67 C8 00
51 XMIT-iguana:241:(task: B04E12C0, time: 1975432.85) 53 octets @ 8009EB16
52 [0000]: FF 03 00 3D C0 09 1E 31 21 45 00 00 2C 2D BD 40
53 [0010]: 00 7A 06 D8 B1 CF 00 FB 08 CE 41 62 12 00 50 20
54 [0020]: 29 7C 4C 71 9C 9A 6A 93 A4 60 12 22 38 3F 10 00
55 [0030]: 00 02 04 05 B4
57 Note that a maximum of eight rows will be displayed (for a maximum of
58 128 bytes), no matter what the octet count is.
60 When reading a packet, the module prepends an ascend_pkt_hdr to the
65 /* How far into the file we should look for packet headers */
66 #define ASCEND_MAX_SEEK 1000000
68 /* XXX Should we replace this with a more generalized array? */
69 /* Magic numbers for Ascend wandsession/wanopening/ether-display data */
70 static const char ascend_xmagic[] = { 'X', 'M', 'I', 'T', '-' };
71 static const char ascend_rmagic[] = { 'R', 'E', 'C', 'V', '-' };
72 static const char ascend_w1magic[] = { 'D', 'a', 't', 'e', ':', };
73 static const char ascend_w2magic[] = { 'W', 'D', '_', 'D', 'I', 'A', 'L', 'O', 'U', 'T', '_', 'D', 'I', 'S', 'P', ':' };
75 #define ASCEND_X_SIZE (sizeof ascend_xmagic / sizeof ascend_xmagic[0])
76 #define ASCEND_R_SIZE (sizeof ascend_rmagic / sizeof ascend_rmagic[0])
77 #define ASCEND_W1_SIZE (sizeof ascend_w1magic / sizeof ascend_w1magic[0])
78 #define ASCEND_W2_SIZE (sizeof ascend_w2magic / sizeof ascend_w2magic[0])
80 static int ascend_read(wtap *wth, int *err);
82 /* Seeks to the beginning of the next packet, and returns the
83 byte offset. Returns -1 on failure. A valid offset is 0; since
84 that causes problems with wtap_loop, offsets are incremented by one. */
85 /* XXX - Handle I/O errors. */
86 static int ascend_seek(wtap *wth, int max_seek)
88 int byte, bytes_read = 0, date_off = 0;
89 int x_level = 0, r_level = 0, w1_level = 0, w2_level = 0;
91 while (((byte = file_getc(wth->fh)) != EOF) && bytes_read < max_seek) {
92 if (byte == ascend_xmagic[x_level]) {
94 if (x_level >= ASCEND_X_SIZE) {
95 file_seek(wth->fh, -(ASCEND_X_SIZE), SEEK_CUR);
96 return file_tell(wth->fh) + 1;
101 if (byte == ascend_rmagic[r_level]) {
103 if (r_level >= ASCEND_R_SIZE) {
104 file_seek(wth->fh, -(ASCEND_R_SIZE), SEEK_CUR);
105 return file_tell(wth->fh) + 1;
110 if (byte == ascend_w1magic[w1_level]) {
112 if (w1_level >= ASCEND_W1_SIZE) {
113 date_off = file_tell(wth->fh) - ASCEND_W1_SIZE + 1;
118 if (byte == ascend_w2magic[w2_level]) {
120 if (w2_level >= ASCEND_W2_SIZE && date_off) {
121 file_seek(wth->fh, date_off - 1, SEEK_SET);
132 /* XXX - return -1 on I/O error and actually do something with 'err'. */
133 int ascend_open(wtap *wth, int *err)
137 file_seek(wth->fh, 0, SEEK_SET);
138 offset = ascend_seek(wth, ASCEND_MAX_SEEK);
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));
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.
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.
158 wth->capture.ascend->inittime = 0;
159 wth->capture.ascend->adjusted = 0;
160 wth->capture.ascend->seek_add = -1;
167 /* Read the next packet; called from wtap_loop(). */
168 static int ascend_read(wtap *wth, int *err)
171 guint8 *buf = buffer_start_ptr(wth->frame_buffer);
172 ascend_pkthdr header;
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);
185 if (! parse_ascend(wth->fh, buf, &wth->phdr.pseudo_header.ascend, &header, 0)) {
186 *err = WTAP_ERR_BAD_RECORD;
190 buffer_assure_space(wth->frame_buffer, wth->snapshot_length);
192 if (! wth->capture.ascend->adjusted) {
193 wth->capture.ascend->adjusted = 1;
194 wth->capture.ascend->inittime = -header.secs;
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;
206 int ascend_seek_read (FILE *fh, int seek_off, guint8 *pd, int len)
208 file_seek(fh, seek_off - 1, SEEK_SET);
209 return parse_ascend(fh, pd, NULL, NULL, len);