aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/ascendtext.c
blob: 2795a23ca28dfcfd403c2e38e3e1a463634b7275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* ascendtext.c
 *
 * $Id$
 *
 * Wiretap Library
 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "wtap-int.h"
#include "buffer.h"
#include "ascendtext.h"
#include "ascend-int.h"
#include "file_wrappers.h"
#include <wsutil/file_util.h>

#include <errno.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <ctype.h>
#include <string.h>

/* Last updated: Feb 03 2005: Josh Bailey (joshbailey@lucent.com).

   This module reads the text hex dump output of various TAOS
   (Lucent/Ascend Max, Max TNT, APX, etc) debug commands, including:

   * pridisplay		traces primary rate ISDN
   * ether-display	traces Ethernet packets (dangerous! CPU intensive)
   * wanopening, wandisplay, wannext, wandsess
			traces PPP or other WAN connections

   Please see ascend.y for examples.

   Detailed documentation on TAOS products is at http://support.lucent.com.

   Support for other commands will be added on an ongoing basis. */

typedef struct _ascend_magic_string {
  guint        type;
  const gchar   *strptr;
} ascend_magic_string;

#define ASCEND_MAGIC_STRINGS	11
#define ASCEND_DATE		"Date:"

/* these magic strings signify the headers of a supported debug commands */
static const ascend_magic_string ascend_magic[] = {
  { ASCEND_PFX_ISDN_X,	"PRI-XMIT-" },
  { ASCEND_PFX_ISDN_R,	"PRI-RCV-" },
  { ASCEND_PFX_WDS_X,	"XMIT-" },
  { ASCEND_PFX_WDS_R,	"RECV-" },
  { ASCEND_PFX_WDS_X,	"XMIT:" },
  { ASCEND_PFX_WDS_R,	"RECV:" },
  { ASCEND_PFX_WDS_X,   "PPP-OUT" },
  { ASCEND_PFX_WDS_R,   "PPP-IN" },
  { ASCEND_PFX_WDD,	ASCEND_DATE },
  { ASCEND_PFX_WDD,	"WD_DIALOUT_DISP:" },
  { ASCEND_PFX_ETHER,	"ETHER" },
};

typedef struct {
	time_t inittime;
	int adjusted;
	gint64 next_packet_seek_start;
} ascend_t;

static gboolean ascend_read(wtap *wth, int *err, gchar **err_info,
	gint64 *data_offset);
static gboolean ascend_seek_read(wtap *wth, gint64 seek_off,
	union wtap_pseudo_header *pseudo_head, guint8 *pd, int len,
	int *err, gchar **err_info);

/* Seeks to the beginning of the next packet, and returns the
   byte offset at which the header for that packet begins.
   Returns -1 on failure. */
static gint64 ascend_seek(wtap *wth, int *err, gchar **err_info)
{
  int byte;
  gint64 date_off = -1, cur_off, packet_off;
  size_t string_level[ASCEND_MAGIC_STRINGS];
  guint string_i = 0, type = 0;
  guint excessive_read_count = 262144;

  memset(&string_level, 0, sizeof(string_level));

  while (((byte = file_getc(wth->fh)) != EOF)) {
    excessive_read_count--;

    if (!excessive_read_count) {
      *err = 0;
      return -1;
    }

    for (string_i = 0; string_i < ASCEND_MAGIC_STRINGS; string_i++) {
      const gchar *strptr = ascend_magic[string_i].strptr;
      size_t len          = strlen(strptr);

      if (byte == *(strptr + string_level[string_i])) {
        string_level[string_i]++;
        if (string_level[string_i] >= len) {
          cur_off = file_tell(wth->fh);
          if (cur_off == -1) {
            /* Error. */
            *err = file_error(wth->fh, err_info);
            return -1;
          }

          /* Date: header is a special case. Remember the offset,
             but keep looking for other headers. */
	  if (strcmp(strptr, ASCEND_DATE) == 0) {
            date_off = cur_off - len;
          } else {
            if (date_off == -1) {
              /* Back up over the header we just read; that's where a read
                 of this packet should start. */
              packet_off = cur_off - len;
            } else {
              /* This packet has a date/time header; a read of it should
                 start at the beginning of *that* header. */
              packet_off = date_off;
            }

            type = ascend_magic[string_i].type;
            goto found;
          }
        }
      } else {
        string_level[string_i] = 0;
      }
    }
  }

  if (byte != EOF || file_eof(wth->fh)) {
    /* Either we didn't find the offset, or we got an EOF. */
    *err = 0;
  } else {
    /* We (presumably) got an error (there's no equivalent to "ferror()"
       in zlib, alas, so we don't have a wrapper to check for an error). */
    *err = file_error(wth->fh, err_info);
  }
  return -1;

found:
  /*
   * Move to where the read for this packet should start, and return
   * that seek offset.
   */
  if (file_seek(wth->fh, packet_off, SEEK_SET, err) == -1)
    return -1;

  wth->pseudo_header.ascend.type = type;

  return packet_off;
}

int ascend_open(wtap *wth, int *err, gchar **err_info)
{
  gint64 offset;
  ws_statb64 statbuf;
  guint8 buf[ASCEND_MAX_PKT_LEN];
  ascend_pkthdr header;
  gint64 dummy_seek_start;
  ascend_t *ascend;

  /* We haven't yet allocated a data structure for our private stuff;
     set the pointer to null, so that "ascend_seek()" knows not to
     fill it in. */
  wth->priv = NULL;

  offset = ascend_seek(wth, err, err_info);
  if (offset == -1) {
    if (*err == 0)
      return 0;
    else
      return -1;
  }

  /* Do a trial parse of the first packet just found to see if we might really have an Ascend file */
  init_parse_ascend();
  if (parse_ascend(wth->fh, buf, &wth->pseudo_header.ascend, &header,
      &dummy_seek_start) != PARSED_RECORD) {
    return 0;
  }

  wth->file_type = WTAP_FILE_ASCEND;

  switch(wth->pseudo_header.ascend.type) {
    case ASCEND_PFX_ISDN_X:
    case ASCEND_PFX_ISDN_R:
      wth->file_encap = WTAP_ENCAP_ISDN;
      break;

    case ASCEND_PFX_ETHER:
      wth->file_encap = WTAP_ENCAP_ETHERNET;
      break;

    default:
      wth->file_encap = WTAP_ENCAP_ASCEND;
  }

  wth->snapshot_length = ASCEND_MAX_PKT_LEN;
  wth->subtype_read = ascend_read;
  wth->subtype_seek_read = ascend_seek_read;
  ascend = (ascend_t *)g_malloc(sizeof(ascend_t));
  wth->priv = (void *)ascend;

  /* The first packet we want to read is the one that "ascend_seek()"
     just found; start searching for it at the offset at which it
     found it. */
  ascend->next_packet_seek_start = offset;

  /* MAXen and Pipelines report the time since reboot.  In order to keep
     from reporting packet times near the epoch, we subtract the first
     packet's timestamp from the capture file's ctime, which gives us an
     offset that we can apply to each packet.
   */
  if (wtap_fstat(wth, &statbuf, err) == -1) {
    g_free(ascend);
    return -1;
  }
  ascend->inittime = statbuf.st_ctime;
  ascend->adjusted = 0;
  wth->tsprecision = WTAP_FILE_TSPREC_USEC;

  init_parse_ascend();

  return 1;
}

static void config_pseudo_header(union wtap_pseudo_header *pseudo_head)
{
  switch(pseudo_head->ascend.type) {
    case ASCEND_PFX_ISDN_X:
      pseudo_head->isdn.uton = TRUE;
      pseudo_head->isdn.channel = 0;
      break;

    case ASCEND_PFX_ISDN_R:
      pseudo_head->isdn.uton = FALSE;
      pseudo_head->isdn.channel = 0;
      break;

    case ASCEND_PFX_ETHER:
      pseudo_head->eth.fcs_len = 0;
      break;
  }
}

/* Read the next packet; called from wtap_read(). */
static gboolean ascend_read(wtap *wth, int *err, gchar **err_info,
	gint64 *data_offset)
{
  ascend_t *ascend = (ascend_t *)wth->priv;
  gint64 offset;
  guint8 *buf = buffer_start_ptr(wth->frame_buffer);
  ascend_pkthdr header;

  /* parse_ascend() will advance the point at which to look for the next
     packet's header, to just after the last packet's header (ie. at the
     start of the last packet's data). We have to get past the last
     packet's header because we might mistake part of it for a new header. */
  if (file_seek(wth->fh, ascend->next_packet_seek_start,
                SEEK_SET, err) == -1)
    return FALSE;

    offset = ascend_seek(wth, err, err_info);
    if (offset == -1)
      return FALSE;
  if (parse_ascend(wth->fh, buf, &wth->pseudo_header.ascend, &header,
      &(ascend->next_packet_seek_start)) != PARSED_RECORD) {
    *err = WTAP_ERR_BAD_FILE;
    *err_info = g_strdup((ascend_parse_error != NULL) ? ascend_parse_error : "parse error");
    return FALSE;
  }

  buffer_assure_space(wth->frame_buffer, wth->snapshot_length);

  config_pseudo_header(&wth->pseudo_header);

  if (! ascend->adjusted) {
    ascend->adjusted = 1;
    if (header.start_time != 0) {
      /*
       * Capture file contained a date and time.
       * We do this only if this is the very first packet we've seen -
       * i.e., if "ascend->adjusted" is false - because
       * if we get a date and time after the first packet, we can't
       * go back and adjust the time stamps of the packets we've already
       * processed, and basing the time stamps of this and following
       * packets on the time stamp from the file text rather than the
       * ctime of the capture file means times before this and after
       * this can't be compared.
       */
      ascend->inittime = header.start_time;
    }
    if (ascend->inittime > header.secs)
      ascend->inittime -= header.secs;
  }
  wth->phdr.presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
  wth->phdr.ts.secs = header.secs + ascend->inittime;
  wth->phdr.ts.nsecs = header.usecs * 1000;
  wth->phdr.caplen = header.caplen;
  wth->phdr.len = header.len;

  *data_offset = offset;
  return TRUE;
}

static gboolean ascend_seek_read(wtap *wth, gint64 seek_off,
	union wtap_pseudo_header *pseudo_head, guint8 *pd, int len _U_,
	int *err, gchar **err_info)
{
  ascend_t *ascend = (ascend_t *)wth->priv;

  if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
    return FALSE;
  if (parse_ascend(wth->random_fh, pd, &pseudo_head->ascend, NULL,
      &(ascend->next_packet_seek_start)) != PARSED_RECORD) {
    *err = WTAP_ERR_BAD_FILE;
    *err_info = g_strdup((ascend_parse_error != NULL) ? ascend_parse_error : "parse error");
    return FALSE;
  }

  config_pseudo_header(pseudo_head);
  return TRUE;
}