Coverage Report

Created: 2025-08-04 07:15

/src/wireshark/wiretap/ngsniffer.c
Line
Count
Source (jump to first uncovered line)
1
/* ngsniffer.c
2
 *
3
 * Wiretap Library
4
 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5
 *
6
 * SPDX-License-Identifier: GPL-2.0-or-later
7
 */
8
9
/* The code in ngsniffer.c that decodes the time fields for each packet in the
10
 * Sniffer trace originally came from code from TCPVIEW:
11
 *
12
 * TCPVIEW
13
 *
14
 * Author:  Martin Hunt
15
 *    Networks and Distributed Computing
16
 *    Computing & Communications
17
 *    University of Washington
18
 *    Administration Building, AG-44
19
 *    Seattle, WA  98195
20
 *    Internet: martinh@cac.washington.edu
21
 *
22
 *
23
 * Copyright 1992 by the University of Washington
24
 *
25
 * Permission to use, copy, modify, and distribute this software and its
26
 * documentation for any purpose and without fee is hereby granted, provided
27
 * that the above copyright notice appears in all copies and that both the
28
 * above copyright notice and this permission notice appear in supporting
29
 * documentation, and that the name of the University of Washington not be
30
 * used in advertising or publicity pertaining to distribution of the software
31
 * without specific, written prior permission.  This software is made
32
 * available "as is", and
33
 * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
34
 * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
35
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
36
 * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
37
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
38
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
39
 * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
40
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41
 *
42
 */
43
#include "config.h"
44
#include "ngsniffer.h"
45
46
#include <string.h>
47
#include "wtap-int.h"
48
#include "file_wrappers.h"
49
#include <wsutil/ws_assert.h>
50
51
/* Magic number in Sniffer files. */
52
static const char ngsniffer_magic[] = {
53
  'T', 'R', 'S', 'N', 'I', 'F', 'F', ' ', 'd', 'a', 't', 'a',
54
  ' ', ' ', ' ', ' ', 0x1a
55
};
56
57
/*
58
 * Sniffer record types.
59
 */
60
0
#define REC_VERS  1  /* Version record (f_vers) */
61
0
#define REC_FRAME2  4  /* Frame data (f_frame2) */
62
0
#define REC_FRAME4  8  /* Frame data (f_frame4) */
63
0
#define REC_FRAME6  12  /* Frame data (f_frame6) (see below) */
64
0
#define REC_EOF   3  /* End-of-file record (no data follows) */
65
/*
66
 * and now for some unknown header types
67
 */
68
0
#define REC_HEADER1 6  /* Header containing various information,
69
         * not yet reverse engineered - some binary,
70
         * some strings (Serial numbers?  Names
71
         * under which the software is registered?
72
         * Software version numbers?  Mysterious
73
         * strings such as "PA-55X" and "PA-30X"
74
         * and "PA-57X" and "PA-11X"?), some strings
75
         * that are partially overwritten
76
         * ("UNSERIALIZED", "Network General
77
         * Corporation"), differing from major
78
         * version to major version */
79
0
#define REC_HEADER2 7  /* Header containing ??? */
80
0
#define REC_V2DESC  8  /* In version 2 sniffer traces contains
81
         * info about this capturing session,
82
         * in the form of a multi-line string
83
         * with NL as the line separator.
84
         * Collides with REC_FRAME4 */
85
0
#define REC_HEADER3 13  /* Retransmission counts? */
86
0
#define REC_HEADER4 14  /* ? */
87
0
#define REC_HEADER5 15  /* ? */
88
0
#define REC_HEADER6 16  /* More broadcast/retransmission counts? */
89
0
#define REC_HEADER7 17  /* ? */
90
91
/*
92
 * Sniffer record header structure.
93
 */
94
struct rec_header {
95
  uint16_t  type;   /* record type */
96
  uint16_t  length;   /* record length */
97
};
98
99
/*
100
 * Sniffer version record format.
101
 */
102
struct vers_rec {
103
  int16_t maj_vers; /* major version number */
104
  int16_t min_vers; /* minor version number */
105
  int16_t time_dos; /* DOS-format time */
106
  int16_t date;   /* DOS-format date */
107
  int8_t  type;   /* what type of records follow */
108
  uint8_t network;  /* network type */
109
  int8_t  format;   /* format version */
110
  uint8_t timeunit; /* timestamp units */
111
  int8_t  cmprs_vers; /* compression version */
112
  int8_t  cmprs_level;  /* compression level */
113
  int16_t rsvd[2];  /* reserved */
114
};
115
116
/*
117
 * Network types.
118
 */
119
#define NETWORK_TRING   0 /* Token ring */
120
0
#define NETWORK_ENET    1  /* Ethernet */
121
#define NETWORK_ARCNET    2 /* ARCNET */
122
#define NETWORK_STARLAN   3 /* StarLAN */
123
#define NETWORK_PCNW    4 /* PC Network broadband (Sytek?) */
124
#define NETWORK_LOCALTALK 5 /* LocalTalk */
125
0
#define NETWORK_SYNCHRO   7  /* Internetwork analyzer (synchronous) */
126
0
#define NETWORK_ASYNC   8  /* Internetwork analyzer (asynchronous) */
127
0
#define NETWORK_FDDI    9  /* FDDI */
128
0
#define NETWORK_ATM   10  /* ATM */
129
130
/*
131
 * Sniffer type 2 data record format - followed by frame data.
132
 *
133
 * The Expert Sniffer Network Analyzer Operations manual, Release 5.50,
134
 * documents some of the values used in "fs" and "flags".  "flags" don't
135
 * look as if they'd be of much interest to us, as those are internal
136
 * flags for state used by the Sniffer, but "fs" gives various status
137
 * bits including error indications *and*:
138
 *
139
 *  ISDN channel information for ISDN;
140
 *
141
 *  PPP vs. SLIP information for Async.
142
 *
143
 * In that section it also refers to "FDDI analyzers using the NPI PCI
144
 * FDDI adapter" and "FDDI analyzers using the NPI ISA FDDI adapter",
145
 * referring to the first as "F1SNIFF" and the second as "FDSNIFF";
146
 * those sound as if they *could* be replacements for "TRSNIFF" in
147
 * the file header, but that manual says, earlier, that the header
148
 * starts with "TRSNIFF data, no matter where the frames were
149
 * collected".
150
 *
151
 * It also says that a type 2 record has an 8-bit "time_high"
152
 * and an 8-bit "time_day" field; the code here used to have a
153
 * 16-bit "time_high" value, but that gave wrong time stamps on at
154
 * least some captures.  Did some older manual have it as a 16-bit
155
 * "tstamp_high", so that perhaps it depends on the version number
156
 * in the file, or is it "tstamp_high" plus "tstamp_day" in all
157
 * versions?  (I forget whether this came purely from tcpview, or if
158
 * I saw any of it in an NAI document.)
159
 *
160
 * We interpret them as unsigned, as interpreting them as signed
161
 * would appear to allow time stamps that precede the start of the
162
 * capture.  The description of the record format shows them as
163
 * "char", but the section "How the Analyzer Stores Time" shows a
164
 * time stamp structure with those fields being "unsigned char".
165
 *
166
 * In addition, the description of the record format has the comment
167
 * for the "time_day" field saying it's the time in days since the
168
 * start of the capture, but the "How the Analyzer Stores Time"
169
 * section says it's increased by 1 if the capture continues past
170
 * midnight - and also says that the time stamp structure has a time
171
 * relative to midnight when the capture started, not since the
172
 * actual capture start, so that might be a difference between
173
 * the internal time stamp in the Sniffer software and the time
174
 * stamp in capture files (i.e., the latter might be relative to
175
 * the time when the capture starts).
176
 */
177
struct frame2_rec {
178
  uint16_t time_low;  /* low part of time stamp */
179
  uint16_t time_med;  /* middle part of time stamp */
180
  uint8_t  time_high; /* high part of the time stamp */
181
  uint8_t  time_day;  /* time in days since start of capture */
182
  int16_t  size;    /* number of bytes of data */
183
  uint8_t  fs;    /* frame error status bits */
184
  uint8_t  flags;   /* buffer flags */
185
  int16_t  true_size; /* size of original frame, in bytes */
186
  int16_t  rsvd;    /* reserved */
187
};
188
189
/*
190
 * Bits in "fs".
191
 *
192
 * The bits differ for different link-layer types.
193
 */
194
195
/*
196
 * Ethernet.
197
 */
198
0
#define FS_ETH_CRC    0x80  /* CRC error */
199
0
#define FS_ETH_ALIGN    0x40  /* bad alignment */
200
#define FS_ETH_RU   0x20  /* "RU out of resources" */
201
#define FS_ETH_OVERRUN    0x10  /* DMA overrun */
202
0
#define FS_ETH_RUNT   0x08  /* frame too small */
203
#define FS_ETH_COLLISION  0x02  /* collision fragment */
204
205
/*
206
 * FDDI.
207
 */
208
0
#define FS_FDDI_INVALID   0x10  /* frame indicators are invalid */
209
#define FS_FDDI_ERROR   0x20  /* "frame error bit 1" */
210
#define FS_FDDI_PCI_VDL   0x01  /* VDL (Valid Data Length?) error on frame on PCI adapter */
211
0
#define FS_FDDI_PCI_CRC   0x02  /* CRC error on frame on PCI adapter */
212
0
#define FS_FDDI_ISA_CRC   0x20  /* CRC error on frame on ISA adapter */
213
214
/*
215
 * Internetwork analyzer (synchronous and asynchronous).
216
 */
217
0
#define FS_WAN_DTE    0x80  /* DTE->DCE frame */
218
219
/*
220
 * Internetwork analyzer (synchronous).
221
 */
222
#define FS_SYNC_LOST    0x01  /* some frames were lost */
223
0
#define FS_SYNC_CRC   0x02  /* CRC error */
224
#define FS_SYNC_ABORT   0x04  /* aborted frame */
225
0
#define FS_ISDN_CHAN_MASK 0x18  /* ISDN channel */
226
0
#define FS_ISDN_CHAN_D    0x18  /* ISDN channel D */
227
0
#define FS_ISDN_CHAN_B1   0x08  /* ISDN channel B1 */
228
0
#define FS_ISDN_CHAN_B2   0x10  /* ISDN channel B2 */
229
230
/*
231
 * Internetwork analyzer (asynchronous).
232
 * XXX - are some of these synchronous flags?  They're listed with the
233
 * asynchronous flags in the Sniffer 5.50 Network Analyzer Operations
234
 * manual.  Is one of the "overrun" errors a synchronous overrun error?
235
 */
236
#define FS_ASYNC_LOST   0x01  /* some frames were lost */
237
#define FS_ASYNC_OVERRUN  0x02  /* UART overrun, lost bytes */
238
#define FS_ASYNC_FRAMING  0x04  /* bad character (framing error?) */
239
#define FS_ASYNC_PPP    0x08  /* PPP frame */
240
#define FS_ASYNC_SLIP   0x10  /* SLIP frame */
241
#define FS_ASYNC_ALIGN    0x20  /* alignment or DLPP(?) error */
242
#define FS_ASYNC_OVERRUN2 0x40  /* overrun or bad frame length */
243
244
/*
245
 * Sniffer type 4 data record format - followed by frame data.
246
 *
247
 * The ATM Sniffer manual says that the "flags" field holds "buffer flags;
248
 * BF_xxxx", but doesn't say what the BF_xxxx flags are.  They may
249
 * be the same as they are in a type 2 record, in which case they're
250
 * probably not of much interest to us.
251
 *
252
 * XXX - the manual also says there's an 8-byte "ATMTimeStamp" driver
253
 * time stamp at the end of "ATMSaveInfo", but, from an ATM Sniffer capture
254
 * file I've looked at, that appears not to be the case.
255
 */
256
257
/*
258
 * Fields from the AAL5 trailer for the frame, if it's an AAL5 frame
259
 * rather than a cell.
260
 */
261
typedef struct _ATM_AAL5Trailer {
262
  uint16_t aal5t_u2u; /* user-to-user indicator */
263
  uint16_t aal5t_len; /* length of the packet */
264
  uint32_t aal5t_chksum;  /* checksum for AAL5 packet */
265
} ATM_AAL5Trailer;
266
267
typedef struct _ATMTimeStamp {
268
  uint32_t msw; /* most significant word */
269
  uint32_t lsw; /* least significant word */
270
} ATMTimeStamp;
271
272
typedef struct _ATMSaveInfo {
273
  uint32_t StatusWord;  /* status word from driver */
274
  ATM_AAL5Trailer Trailer; /* AAL5 trailer */
275
  uint8_t  AppTrafType; /* traffic type */
276
  uint8_t  AppHLType; /* protocol type */
277
  uint16_t AppReserved; /* reserved */
278
  uint16_t Vpi;   /* virtual path identifier */
279
  uint16_t Vci;   /* virtual circuit identifier */
280
  uint16_t channel; /* link: 0 for DCE, 1 for DTE */
281
  uint16_t cells;   /* number of cells */
282
  uint32_t AppVal1; /* type-dependent */
283
  uint32_t AppVal2; /* type-dependent */
284
} ATMSaveInfo;
285
286
/*
287
 * Bits in StatusWord.
288
 */
289
#define SW_ERRMASK    0x0F  /* Error mask: */
290
#define SW_RX_FIFO_UNDERRUN 0x01  /* Receive FIFO underrun */
291
#define SW_RX_FIFO_OVERRUN  0x02  /* Receive FIFO overrun */
292
#define SW_RX_PKT_TOO_LONG  0x03  /* Received packet > max size */
293
#define SW_CRC_ERROR    0x04  /* CRC error */
294
#define SW_USER_ABORTED_RX  0x05  /* User aborted receive */
295
#define SW_BUF_LEN_TOO_LONG 0x06  /* buffer len > max buf */
296
#define SW_INTERNAL_T1_ERROR  0x07  /* Internal T1 error */
297
#define SW_RX_CHANNEL_DEACTIV8  0x08  /* Rx channel deactivate */
298
299
#define SW_ERROR    0x80  /* Error indicator */
300
#define SW_CONGESTION   0x40  /* Congestion indicator */
301
#define SW_CLP      0x20  /* Cell loss priority indicator */
302
0
#define SW_RAW_CELL   0x100  /* RAW cell indicator */
303
#define SW_OAM_CELL   0x200 /* OAM cell indicator */
304
305
/*
306
 * Bits in AppTrafType.
307
 *
308
 * For AAL types other than AAL5, the packet data is presumably for a
309
 * single cell, not a reassembled frame, as the ATM Sniffer manual says
310
 * it doesn't reassemble cells other than AAL5 cells.
311
 */
312
0
#define ATT_AALTYPE   0x0F  /* AAL type: */
313
0
#define ATT_AAL_UNKNOWN   0x00  /* Unknown AAL */
314
0
#define ATT_AAL1    0x01  /* AAL1 */
315
0
#define ATT_AAL3_4    0x02  /* AAL3/4 */
316
0
#define ATT_AAL5    0x03  /* AAL5 */
317
0
#define ATT_AAL_USER    0x04  /* User AAL */
318
0
#define ATT_AAL_SIGNALLING  0x05  /* Signaling AAL */
319
0
#define ATT_OAMCELL   0x06  /* OAM cell */
320
321
0
#define ATT_HLTYPE    0xF0  /* Higher-layer type: */
322
0
#define ATT_HL_UNKNOWN    0x00  /* unknown */
323
0
#define ATT_HL_LLCMX    0x10  /* LLC multiplexed (probably RFC 1483) */
324
0
#define ATT_HL_VCMX   0x20  /* VC multiplexed (probably RFC 1483) */
325
0
#define ATT_HL_LANE   0x30  /* LAN Emulation */
326
0
#define ATT_HL_ILMI   0x40  /* ILMI */
327
0
#define ATT_HL_FRMR   0x50  /* Frame Relay */
328
0
#define ATT_HL_SPANS    0x60  /* FORE SPANS */
329
0
#define ATT_HL_IPSILON    0x70  /* Ipsilon */
330
331
/*
332
 * Values for AppHLType; the interpretation depends on the ATT_HLTYPE
333
 * bits in AppTrafType.
334
 */
335
0
#define AHLT_UNKNOWN    0x0
336
0
#define AHLT_VCMX_802_3_FCS 0x1  /* VCMX: 802.3 FCS */
337
0
#define AHLT_LANE_LE_CTRL 0x1  /* LANE: LE Ctrl */
338
0
#define AHLT_IPSILON_FT0  0x1  /* Ipsilon: Flow Type 0 */
339
0
#define AHLT_VCMX_802_4_FCS 0x2  /* VCMX: 802.4 FCS */
340
0
#define AHLT_LANE_802_3   0x2  /* LANE: 802.3 */
341
0
#define AHLT_IPSILON_FT1  0x2  /* Ipsilon: Flow Type 1 */
342
0
#define AHLT_VCMX_802_5_FCS 0x3  /* VCMX: 802.5 FCS */
343
0
#define AHLT_LANE_802_5   0x3  /* LANE: 802.5 */
344
0
#define AHLT_IPSILON_FT2  0x3  /* Ipsilon: Flow Type 2 */
345
0
#define AHLT_VCMX_FDDI_FCS  0x4  /* VCMX: FDDI FCS */
346
0
#define AHLT_LANE_802_3_MC  0x4  /* LANE: 802.3 multicast */
347
0
#define AHLT_VCMX_802_6_FCS 0x5  /* VCMX: 802.6 FCS */
348
0
#define AHLT_LANE_802_5_MC  0x5  /* LANE: 802.5 multicast */
349
0
#define AHLT_VCMX_802_3   0x7  /* VCMX: 802.3 */
350
0
#define AHLT_VCMX_802_4   0x8  /* VCMX: 802.4 */
351
0
#define AHLT_VCMX_802_5   0x9  /* VCMX: 802.5 */
352
0
#define AHLT_VCMX_FDDI    0xa  /* VCMX: FDDI */
353
0
#define AHLT_VCMX_802_6   0xb  /* VCMX: 802.6 */
354
0
#define AHLT_VCMX_FRAGMENTS 0xc  /* VCMX: Fragments */
355
0
#define AHLT_VCMX_BPDU    0xe  /* VCMX: BPDU */
356
357
struct frame4_rec {
358
  uint16_t time_low;  /* low part of time stamp */
359
  uint16_t time_med;  /* middle part of time stamp */
360
  uint8_t  time_high; /* high part of time stamp */
361
  uint8_t  time_day;  /* time in days since start of capture */
362
  int16_t  size;    /* number of bytes of data */
363
  int8_t   fs;    /* frame error status bits */
364
  int8_t   flags;   /* buffer flags */
365
  int16_t  true_size; /* size of original frame, in bytes */
366
  int16_t  rsvd3;   /* reserved */
367
  int16_t  atm_pad; /* pad to 4-byte boundary */
368
  ATMSaveInfo atm_info; /* ATM-specific stuff */
369
};
370
371
/*
372
 * XXX - I have a version 5.50 file with a bunch of token ring
373
 * records listed as type "12".  The record format below was
374
 * derived from frame4_rec and a bit of experimentation.
375
 * - Gerald
376
 */
377
struct frame6_rec {
378
  uint16_t time_low;  /* low part of time stamp */
379
  uint16_t time_med;  /* middle part of time stamp */
380
  uint8_t  time_high; /* high part of time stamp */
381
  uint8_t  time_day;  /* time in days since start of capture */
382
  int16_t  size;    /* number of bytes of data */
383
  uint8_t  fs;    /* frame error status bits */
384
  uint8_t  flags;   /* buffer flags */
385
  int16_t  true_size; /* size of original frame, in bytes */
386
  uint8_t  chemical_x[22];/* ? */
387
};
388
389
/*
390
 * Network type values in some type 7 records.
391
 *
392
 * Captures with a major version number of 2 appear to have type 7
393
 * records with text in them (at least one I have does).
394
 *
395
 * Captures with a major version of 4, and at least some captures with
396
 * a major version of 5, have type 7 records with those values in the
397
 * 5th byte.
398
 *
399
 * However, some captures with a major version number of 5 appear not to
400
 * have type 7 records at all (at least one I have doesn't), but do appear
401
 * to put non-zero values in the "rsvd" field of the version header (at
402
 * least one I have does) - at least some other captures with smaller version
403
 * numbers appear to put 0 there, so *maybe* that's where the network
404
 * (sub)type is hidden in those captures.  The version 5 captures I've seen
405
 * that *do* have type 7 records put 0 there, so it's not as if *all* V5
406
 * captures have something in the "rsvd" field, however.
407
 *
408
 * The semantics of these network types is inferred from the Sniffer
409
 * documentation, as they correspond to types described in the UI;
410
 * in particular, see
411
 *
412
 *  http://www.mcafee.com/common/media/sniffer/support/sdos/operation.pdf
413
 *
414
 * starting at page 3-10 (56 of 496).
415
 *
416
 * XXX - I've seen X.25 captures with NET_ROUTER, and I've seen bridge/
417
 * router captures with NET_HDLC.  Sigh....  Are those just captures for
418
 * which the user set the wrong network type when capturing?
419
 */
420
0
#define NET_SDLC  0  /* Probably "SDLC then SNA" */
421
0
#define NET_HDLC  1  /* Used for X.25; is it used for other
422
           things as well, or is it "HDLC then
423
           X.25", as referred to by the document
424
           cited above, and only used for X.25? */
425
0
#define NET_FRAME_RELAY 2
426
0
#define NET_ROUTER  3  /* Probably "Router/Bridge", for various
427
           point-to-point protocols for use between
428
           bridges and routers, including PPP as well
429
           as various proprietary protocols; also
430
           used for ISDN, for reasons not obvious
431
           to me, given that a Sniffer knows
432
           whether it's using a WAN or an ISDN pod */
433
0
#define NET_PPP   4  /* "Asynchronous", which includes SLIP too */
434
#define NET_SMDS  5 /* Not mentioned in the document, but
435
           that's a document for version 5.50 of
436
           the Sniffer, and that version might use
437
           version 5 in the file format and thus
438
           might not be using type 7 records */
439
440
/*
441
 * Values for V.timeunit, in picoseconds, so that they can be represented
442
 * as integers.  These values must be < 2^(64-40); see below.
443
 *
444
 * XXX - at least some captures with a V.timeunit value of 2 show
445
 * packets with time stamps in 2011 if the time stamp is interpreted
446
 * to be in units of 15 microseconds.  The capture predates 2008,
447
 * so that interpretation is probably wrong.  Perhaps the interpretation
448
 * of V.timeunit depends on the version number of the file?
449
 */
450
static const uint32_t Psec[] = {
451
  15000000,   /* 15.0 usecs = 15000000 psecs */
452
    838096,   /* .838096 usecs = 838096 psecs */
453
  15000000,   /* 15.0 usecs = 15000000 psecs */
454
    500000,   /* 0.5 usecs = 500000 psecs */
455
   2000000,   /* 2.0 usecs = 2000000 psecs */
456
   1000000,   /* 1.0 usecs = 1000000 psecs */
457
        /* XXX - Sniffer doc says 0.08 usecs = 80000 psecs */
458
    100000    /* 0.1 usecs = 100000 psecs */
459
};
460
0
#define NUM_NGSNIFF_TIMEUNITS array_length(Psec)
461
462
/* Information for a compressed Sniffer data stream. */
463
typedef struct {
464
  unsigned char *buf; /* buffer into which we uncompress data */
465
  unsigned int nbytes;  /* number of bytes of data in that buffer */
466
  int nextout;  /* offset in that buffer of stream's current position */
467
  int64_t comp_offset;  /* current offset in compressed data stream */
468
  int64_t uncomp_offset;  /* current offset in uncompressed data stream */
469
} ngsniffer_comp_stream_t;
470
471
typedef struct {
472
  unsigned  maj_vers;
473
  unsigned  min_vers;
474
  bool is_compressed;
475
  uint32_t  timeunit;
476
  time_t  start;
477
  unsigned  network;    /* network type */
478
  ngsniffer_comp_stream_t seq;  /* sequential access */
479
  ngsniffer_comp_stream_t rand; /* random access */
480
  GList *first_blob;    /* list element for first blob */
481
  GList *last_blob;   /* list element for last blob */
482
  GList *current_blob;    /* list element for current blob */
483
} ngsniffer_t;
484
485
/*
486
 * DOS date to "struct tm" conversion values.
487
 */
488
/* DOS year = upper 7 bits */
489
0
#define DOS_YEAR_OFFSET (1980-1900)  /* tm_year = year+1900, DOS date year year+1980 */
490
0
#define DOS_YEAR_SHIFT  9
491
0
#define DOS_YEAR_MASK (0x7F<<DOS_YEAR_SHIFT)
492
/* DOS month = next 4 bits */
493
0
#define DOS_MONTH_OFFSET  (-1)  /* tm_mon = month #-1, DOS date month = month # */
494
0
#define DOS_MONTH_SHIFT 5
495
0
#define DOS_MONTH_MASK  (0x0F<<DOS_MONTH_SHIFT)
496
/* DOS day = next 5 bits */
497
0
#define DOS_DAY_SHIFT 0
498
0
#define DOS_DAY_MASK  (0x1F<<DOS_DAY_SHIFT)
499
500
static int process_header_records(wtap *wth, int *err, char **err_info,
501
    int16_t maj_vers, uint8_t network);
502
static int process_rec_header2_v2(wtap *wth, unsigned char *buffer,
503
    uint16_t length, int *err, char **err_info);
504
static int process_rec_header2_v145(wtap *wth, unsigned char *buffer,
505
    uint16_t length, int16_t maj_vers, int *err, char **err_info);
506
static bool ngsniffer_read(wtap *wth, wtap_rec *rec,
507
    int *err, char **err_info, int64_t *data_offset);
508
static bool ngsniffer_seek_read(wtap *wth, int64_t seek_off,
509
    wtap_rec *rec, int *err, char **err_info);
510
static bool read_rec_header(wtap *wth, bool is_random,
511
    struct rec_header *hdr, int *err, char **err_info);
512
static bool process_frame_record(wtap *wth, bool is_random,
513
    unsigned *padding, struct rec_header *hdr, wtap_rec *rec,
514
    int *err, char **err_info);
515
static void set_metadata_frame2(wtap *wth, wtap_rec *rec,
516
    struct frame2_rec *frame2);
517
static void set_pseudo_header_frame4(union wtap_pseudo_header *pseudo_header,
518
    struct frame4_rec *frame4);
519
static void set_pseudo_header_frame6(wtap *wth,
520
    union wtap_pseudo_header *pseudo_header, struct frame6_rec *frame6);
521
static int infer_pkt_encap(const uint8_t *pd, int len);
522
static int fix_pseudo_header(int encap, wtap_rec *rec, int len);
523
static void ngsniffer_sequential_close(wtap *wth);
524
static void ngsniffer_close(wtap *wth);
525
static bool ngsniffer_dump(wtap_dumper *wdh, const wtap_rec *rec,
526
    int *err, char **err_info);
527
static bool ngsniffer_dump_finish(wtap_dumper *wdh, int *err,
528
    char **err_info);
529
static int SnifferDecompress( unsigned char * inbuf, size_t inlen,
530
    unsigned char * outbuf, size_t outlen, int *err, char **err_info );
531
static bool ng_read_bytes_or_eof(wtap *wth, void *buffer,
532
    unsigned int nbytes, bool is_random, int *err, char **err_info);
533
static bool ng_read_bytes(wtap *wth, void *buffer, unsigned int nbytes,
534
    bool is_random, int *err, char **err_info);
535
static bool read_blob(FILE_T infile, ngsniffer_comp_stream_t *comp_stream,
536
    int *err, char **err_info);
537
static bool ng_skip_bytes_seq(wtap *wth, unsigned int count, int *err,
538
    char **err_info);
539
static bool ng_file_seek_rand(wtap *wth, int64_t offset, int *err,
540
    char **err_info);
541
542
static int ngsniffer_uncompressed_file_type_subtype = -1;
543
static int ngsniffer_compressed_file_type_subtype = -1;
544
545
void register_ngsniffer(void);
546
547
wtap_open_return_val
548
ngsniffer_open(wtap *wth, int *err, char **err_info)
549
0
{
550
0
  char magic[sizeof ngsniffer_magic];
551
0
  char record_type[2];
552
0
  char record_length[4]; /* only the first 2 bytes are length,
553
          the last 2 are "reserved" and are thrown away */
554
0
  uint16_t type;
555
0
  struct vers_rec version;
556
0
  uint16_t maj_vers;
557
0
  uint16_t  start_date;
558
#if 0
559
  uint16_t  start_time;
560
#endif
561
0
  static const int sniffer_encap[] = {
562
0
    WTAP_ENCAP_TOKEN_RING,
563
0
    WTAP_ENCAP_ETHERNET,
564
0
    WTAP_ENCAP_ARCNET,
565
0
    WTAP_ENCAP_UNKNOWN, /* StarLAN */
566
0
    WTAP_ENCAP_UNKNOWN, /* PC Network broadband */
567
0
    WTAP_ENCAP_UNKNOWN, /* LocalTalk */
568
0
    WTAP_ENCAP_UNKNOWN, /* Znet */
569
0
    WTAP_ENCAP_PER_PACKET, /* Internetwork analyzer (synchronous) */
570
0
    WTAP_ENCAP_PER_PACKET, /* Internetwork analyzer (asynchronous) */
571
0
    WTAP_ENCAP_FDDI_BITSWAPPED,
572
0
    WTAP_ENCAP_ATM_PDUS
573
0
  };
574
0
  #define NUM_NGSNIFF_ENCAPS array_length(sniffer_encap)
575
0
  struct tm tm;
576
0
  int64_t current_offset;
577
0
  ngsniffer_t *ngsniffer;
578
579
  /* Read in the string that should be at the start of a Sniffer file */
580
0
  if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
581
0
    if (*err != WTAP_ERR_SHORT_READ)
582
0
      return WTAP_OPEN_ERROR;
583
0
    return WTAP_OPEN_NOT_MINE;
584
0
  }
585
586
0
  if (memcmp(magic, ngsniffer_magic, sizeof ngsniffer_magic)) {
587
0
    return WTAP_OPEN_NOT_MINE;
588
0
  }
589
590
  /*
591
   * Read the first record, which the manual says is a version
592
   * record.
593
   */
594
0
  if (!wtap_read_bytes(wth->fh, record_type, 2, err, err_info))
595
0
    return WTAP_OPEN_ERROR;
596
0
  if (!wtap_read_bytes(wth->fh, record_length, 4, err, err_info))
597
0
    return WTAP_OPEN_ERROR;
598
599
0
  type = pletoh16(record_type);
600
601
0
  if (type != REC_VERS) {
602
0
    *err = WTAP_ERR_BAD_FILE;
603
0
    *err_info = g_strdup("ngsniffer: Sniffer file doesn't start with a version record");
604
0
    return WTAP_OPEN_ERROR;
605
0
  }
606
607
0
  if (!wtap_read_bytes(wth->fh, &version, sizeof version, err, err_info))
608
0
    return WTAP_OPEN_ERROR;
609
610
  /* Check the data link type. */
611
0
  if (version.network >= NUM_NGSNIFF_ENCAPS
612
0
      || sniffer_encap[version.network] == WTAP_ENCAP_UNKNOWN) {
613
0
    *err = WTAP_ERR_UNSUPPORTED;
614
0
    *err_info = ws_strdup_printf("ngsniffer: network type %u unknown or unsupported",
615
0
        version.network);
616
0
    return WTAP_OPEN_ERROR;
617
0
  }
618
619
  /* Check the time unit */
620
0
  if (version.timeunit >= NUM_NGSNIFF_TIMEUNITS) {
621
0
    *err = WTAP_ERR_UNSUPPORTED;
622
0
    *err_info = ws_strdup_printf("ngsniffer: Unknown timeunit %u", version.timeunit);
623
0
    return WTAP_OPEN_ERROR;
624
0
  }
625
626
  /* Set encap type before reading header records because the
627
   * header record may change encap type.
628
   */
629
0
  wth->file_encap = sniffer_encap[version.network];
630
631
  /*
632
   * We don't know how to handle the remaining header record types,
633
   * so we just skip them - except for REC_HEADER2 records, which
634
   * we look at, for "Internetwork analyzer" captures, to attempt to
635
   * determine what the link-layer encapsulation is.
636
   *
637
   * XXX - in some version 1.16 internetwork analyzer files
638
   * generated by the Windows Sniffer when saving Windows
639
   * Sniffer files as DOS Sniffer files, there's no REC_HEADER2
640
   * record, but the first "rsvd" word is 1 for PRI ISDN files, 2
641
   * for BRI ISDN files, and 0 for non-ISDN files; is that something
642
   * the DOS Sniffer understands?
643
   */
644
0
  maj_vers = pletoh16(&version.maj_vers);
645
0
  if (process_header_records(wth, err, err_info, maj_vers,
646
0
      version.network) < 0)
647
0
    return WTAP_OPEN_ERROR;
648
0
  if ((version.network == NETWORK_SYNCHRO ||
649
0
      version.network == NETWORK_ASYNC) &&
650
0
      wth->file_encap == WTAP_ENCAP_PER_PACKET) {
651
    /*
652
     * Well, we haven't determined the internetwork analyzer
653
     * subtype yet...
654
     */
655
0
    switch (maj_vers) {
656
657
0
    case 1:
658
      /*
659
       * ... and this is a version 1 capture; look
660
       * at the first "rsvd" word.
661
       */
662
0
      switch (pletoh16(&version.rsvd[0])) {
663
664
0
      case 1:
665
0
      case 2:
666
0
        wth->file_encap = WTAP_ENCAP_ISDN;
667
0
        break;
668
0
      }
669
0
      break;
670
671
0
    case 3:
672
      /*
673
       * ...and this is a version 3 capture; we've
674
       * seen nothing in those that obviously
675
       * indicates the capture type, but the only
676
       * one we've seen is a Frame Relay capture,
677
       * so mark it as Frame Relay for now.
678
       */
679
0
      wth->file_encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
680
0
      break;
681
0
    }
682
0
  }
683
684
0
  current_offset = file_tell(wth->fh);
685
686
  /*
687
   * Now, if we have a random stream open, position it to the same
688
   * location, which should be the beginning of the real data, and
689
   * should be the beginning of the compressed data.
690
   *
691
   * XXX - will we see any records other than REC_FRAME2, REC_FRAME4,
692
   * or REC_EOF after this?  If not, we can get rid of the loop in
693
   * "ngsniffer_read()".
694
   */
695
0
  if (wth->random_fh != NULL) {
696
0
    if (file_seek(wth->random_fh, current_offset, SEEK_SET, err) == -1)
697
0
      return WTAP_OPEN_ERROR;
698
0
  }
699
700
  /* This is a ngsniffer file */
701
0
  ngsniffer = g_new(ngsniffer_t, 1);
702
0
  wth->priv = (void *)ngsniffer;
703
704
  /* compressed or uncompressed Sniffer file? */
705
0
  if (version.format != 1) {
706
0
    wth->file_type_subtype = ngsniffer_compressed_file_type_subtype;
707
0
    ngsniffer->is_compressed = true;
708
0
  } else {
709
0
    wth->file_type_subtype = ngsniffer_uncompressed_file_type_subtype;
710
0
    ngsniffer->is_compressed = false;
711
0
  }
712
713
0
  ngsniffer->maj_vers = maj_vers;
714
0
  ngsniffer->min_vers = pletoh16(&version.min_vers);
715
716
  /* We haven't allocated any uncompression buffers yet. */
717
0
  ngsniffer->seq.buf = NULL;
718
0
  ngsniffer->seq.nbytes = 0;
719
0
  ngsniffer->seq.nextout = 0;
720
0
  ngsniffer->rand.buf = NULL;
721
0
  ngsniffer->rand.nbytes = 0;
722
0
  ngsniffer->rand.nextout = 0;
723
724
  /* Set the current file offset; the offset in the compressed file
725
     and in the uncompressed data stream currently the same. */
726
0
  ngsniffer->seq.uncomp_offset = current_offset;
727
0
  ngsniffer->seq.comp_offset = current_offset;
728
0
  ngsniffer->rand.uncomp_offset = current_offset;
729
0
  ngsniffer->rand.comp_offset = current_offset;
730
731
  /* We don't yet have any list of compressed blobs. */
732
0
  ngsniffer->first_blob = NULL;
733
0
  ngsniffer->last_blob = NULL;
734
0
  ngsniffer->current_blob = NULL;
735
736
0
  wth->subtype_read = ngsniffer_read;
737
0
  wth->subtype_seek_read = ngsniffer_seek_read;
738
0
  wth->subtype_sequential_close = ngsniffer_sequential_close;
739
0
  wth->subtype_close = ngsniffer_close;
740
0
  wth->snapshot_length = 0; /* not available in header, only in frame */
741
0
  ngsniffer->timeunit = Psec[version.timeunit];
742
0
  ngsniffer->network = version.network;
743
744
  /* Get capture start time */
745
0
  start_date = pletoh16(&version.date);
746
0
  tm.tm_year = ((start_date&DOS_YEAR_MASK)>>DOS_YEAR_SHIFT) + DOS_YEAR_OFFSET;
747
0
  tm.tm_mon = ((start_date&DOS_MONTH_MASK)>>DOS_MONTH_SHIFT) + DOS_MONTH_OFFSET;
748
0
  tm.tm_mday = ((start_date&DOS_DAY_MASK)>>DOS_DAY_SHIFT);
749
  /*
750
   * The time does not appear to act as an offset; only the date.
751
   * XXX - sometimes it does appear to act as an offset; is this
752
   * version-dependent?
753
   */
754
#if 0
755
  start_time = pletoh16(&version.time_dos);
756
  tm.tm_hour = (start_time&0xf800)>>11;
757
  tm.tm_min = (start_time&0x7e0)>>5;
758
  tm.tm_sec = (start_time&0x1f)<<1;
759
#else
760
0
  tm.tm_hour = 0;
761
0
  tm.tm_min = 0;
762
0
  tm.tm_sec = 0;
763
0
#endif
764
0
  tm.tm_isdst = -1;
765
0
  ngsniffer->start = mktime(&tm);
766
  /*
767
   * XXX - what if "secs" is -1?  Unlikely,
768
   * but if the capture was done in a time
769
   * zone that switches between standard and
770
   * summer time sometime other than when we
771
   * do, and thus the time was one that doesn't
772
   * exist here because a switch from standard
773
   * to summer time zips over it, it could
774
   * happen.
775
   *
776
   * On the other hand, if the capture was done
777
   * in a different time zone, this won't work
778
   * right anyway; unfortunately, the time zone
779
   * isn't stored in the capture file.
780
   */
781
782
0
  wth->file_tsprec = WTAP_TSPREC_NSEC; /* XXX */
783
784
0
  return WTAP_OPEN_MINE;
785
0
}
786
787
static int
788
process_header_records(wtap *wth, int *err, char **err_info, int16_t maj_vers,
789
    uint8_t network)
790
0
{
791
0
  char record_type[2];
792
0
  char record_length[4]; /* only the first 2 bytes are length,
793
          the last 2 are "reserved" and are thrown away */
794
0
  uint16_t rec_type, rec_length_remaining;
795
0
  int bytes_to_read;
796
0
  unsigned char buffer[256];
797
798
0
  for (;;) {
799
0
    if (!wtap_read_bytes_or_eof(wth->fh, record_type, 2, err, err_info)) {
800
0
      if (*err != 0)
801
0
        return -1;
802
0
      return 0; /* EOF */
803
0
    }
804
805
0
    rec_type = pletoh16(record_type);
806
0
    if ((rec_type != REC_HEADER1) && (rec_type != REC_HEADER2)
807
0
      && (rec_type != REC_HEADER3) && (rec_type != REC_HEADER4)
808
0
      && (rec_type != REC_HEADER5) && (rec_type != REC_HEADER6)
809
0
      && (rec_type != REC_HEADER7)
810
0
      && ((rec_type != REC_V2DESC) || (maj_vers > 2)) ) {
811
      /*
812
       * Well, this is either some unknown header type
813
       * (we ignore this case), an uncompressed data
814
       * frame or the length of a compressed blob
815
       * which implies data. Seek backwards over the
816
       * two bytes we read, and return.
817
       */
818
0
      if (file_seek(wth->fh, -2, SEEK_CUR, err) == -1)
819
0
        return -1;
820
0
      return 0;
821
0
    }
822
823
0
    if (!wtap_read_bytes(wth->fh, record_length, 4,
824
0
        err, err_info))
825
0
      return -1;
826
827
0
    rec_length_remaining = pletoh16(record_length);
828
829
    /*
830
     * Is this is an "Internetwork analyzer" capture, and
831
     * is this a REC_HEADER2 record?
832
     *
833
     * If so, it appears to specify the particular type
834
     * of network we're on.
835
     *
836
     * XXX - handle sync and async differently?  (E.g.,
837
     * does this apply only to sync?)
838
     */
839
0
    if ((network == NETWORK_SYNCHRO || network == NETWORK_ASYNC) &&
840
0
        rec_type == REC_HEADER2) {
841
      /*
842
       * Yes, get the first up-to-256 bytes of the
843
       * record data.
844
       */
845
0
      bytes_to_read = MIN(rec_length_remaining, (int)sizeof buffer);
846
0
      if (!wtap_read_bytes(wth->fh, buffer,
847
0
          bytes_to_read, err, err_info))
848
0
        return -1;
849
850
0
      switch (maj_vers) {
851
852
0
      case 2:
853
0
        if (process_rec_header2_v2(wth, buffer,
854
0
            rec_length_remaining, err, err_info) < 0)
855
0
          return -1;
856
0
        break;
857
858
0
      case 1:
859
0
      case 4:
860
0
      case 5:
861
0
        if (process_rec_header2_v145(wth, buffer,
862
0
            rec_length_remaining, maj_vers, err, err_info) < 0)
863
0
          return -1;
864
0
        break;
865
0
      }
866
867
      /*
868
       * Skip the rest of the record.
869
       */
870
0
      if (rec_length_remaining > sizeof buffer) {
871
0
        if (file_seek(wth->fh, rec_length_remaining - sizeof buffer,
872
0
            SEEK_CUR, err) == -1)
873
0
          return -1;
874
0
      }
875
0
    } else {
876
      /* Nope, just skip over the data. */
877
0
      if (file_seek(wth->fh, rec_length_remaining, SEEK_CUR, err) == -1)
878
0
        return -1;
879
0
    }
880
0
  }
881
0
}
882
883
static int
884
process_rec_header2_v2(wtap *wth, unsigned char *buffer, uint16_t length,
885
    int *err, char **err_info)
886
0
{
887
0
  static const char x_25_str[] = "HDLC\nX.25\n";
888
889
  /*
890
   * There appears to be a string in a REC_HEADER2 record, with
891
   * a list of protocols.  In one X.25 capture I've seen, the
892
   * string was "HDLC\nX.25\nCLNP\nISO_TP\nSESS\nPRES\nVTP\nACSE".
893
   * Presumably CLNP and everything else is per-packet, but
894
   * we assume "HDLC\nX.25\n" indicates that it's an X.25 capture.
895
   */
896
0
  if (length < sizeof x_25_str - 1) {
897
    /*
898
     * There's not enough data to compare.
899
     */
900
0
    *err = WTAP_ERR_UNSUPPORTED;
901
0
    *err_info = g_strdup("ngsniffer: WAN capture has too-short protocol list");
902
0
    return -1;
903
0
  }
904
905
0
  if (strncmp((char *)buffer, x_25_str, sizeof x_25_str - 1) == 0) {
906
    /*
907
     * X.25.
908
     */
909
0
    wth->file_encap = WTAP_ENCAP_LAPB;
910
0
  } else {
911
0
    *err = WTAP_ERR_UNSUPPORTED;
912
0
    *err_info = ws_strdup_printf("ngsniffer: WAN capture protocol string %.*s unknown",
913
0
        length, buffer);
914
0
    return -1;
915
0
  }
916
0
  return 0;
917
0
}
918
919
static int
920
process_rec_header2_v145(wtap *wth, unsigned char *buffer, uint16_t length,
921
    int16_t maj_vers, int *err, char **err_info)
922
0
{
923
  /*
924
   * The 5th byte of the REC_HEADER2 record appears to be a
925
   * network type.
926
   */
927
0
  if (length < 5) {
928
    /*
929
     * There is no 5th byte; give up.
930
     */
931
0
    *err = WTAP_ERR_UNSUPPORTED;
932
0
    *err_info = g_strdup("ngsniffer: WAN capture has no network subtype");
933
0
    return -1;
934
0
  }
935
936
  /*
937
   * The X.25 captures I've seen have a type of NET_HDLC, and the
938
   * Sniffer documentation seems to imply that it's used for
939
   * X.25, although it could be used for other purposes as well.
940
   *
941
   * NET_ROUTER is used for all sorts of point-to-point protocols,
942
   * including ISDN.  It appears, from the documentation, that the
943
   * Sniffer attempts to infer the particular protocol by looking
944
   * at the traffic; it's not clear whether it stores in the file
945
   * an indication of the protocol it inferred was being used.
946
   *
947
   * Unfortunately, it also appears that NET_HDLC is used for
948
   * stuff other than X.25 as well, so we can't just interpret
949
   * it unconditionally as X.25.
950
   *
951
   * For now, we interpret both NET_HDLC and NET_ROUTER as "per-packet
952
   * encapsulation".  We remember that we saw NET_ROUTER, though,
953
   * as it appears that we can infer whether a packet is PPP or
954
   * ISDN based on the channel number subfield of the frame error
955
   * status bits - if it's 0, it's PPP, otherwise it's ISDN and
956
   * the channel number indicates which channel it is.  We assume
957
   * NET_HDLC isn't used for ISDN.
958
   */
959
0
  switch (buffer[4]) {
960
961
0
  case NET_SDLC:
962
0
    wth->file_encap = WTAP_ENCAP_SDLC;
963
0
    break;
964
965
0
  case NET_HDLC:
966
0
    wth->file_encap = WTAP_ENCAP_PER_PACKET;
967
0
    break;
968
969
0
  case NET_FRAME_RELAY:
970
0
    wth->file_encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
971
0
    break;
972
973
0
  case NET_ROUTER:
974
    /*
975
     * For most of the version 4 capture files I've seen,
976
     * 0xfa in buffer[1] means the file is an ISDN capture,
977
     * but there's one PPP file with 0xfa there; does that
978
     * mean that the 0xfa has nothing to do with ISDN,
979
     * or is that just an ISDN file with no D channel
980
     * packets?  (The channel number is not 0 in any
981
     * of the packets, so perhaps it is.)
982
     *
983
     * For one version 5 ISDN capture I've seen, there's
984
     * a 0x01 in buffer[6]; none of the non-ISDN version
985
     * 5 captures have it.
986
     */
987
0
    wth->file_encap = WTAP_ENCAP_PER_PACKET;
988
0
    switch (maj_vers) {
989
990
0
    case 4:
991
0
      if (buffer[1] == 0xfa)
992
0
        wth->file_encap = WTAP_ENCAP_ISDN;
993
0
      break;
994
995
0
    case 5:
996
0
      if (length < 7) {
997
        /*
998
         * There is no 5th byte; give up.
999
         */
1000
0
        *err = WTAP_ERR_UNSUPPORTED;
1001
0
        *err_info = g_strdup("ngsniffer: WAN bridge/router capture has no ISDN flag");
1002
0
        return -1;
1003
0
      }
1004
0
      if (buffer[6] == 0x01)
1005
0
        wth->file_encap = WTAP_ENCAP_ISDN;
1006
0
      break;
1007
0
    }
1008
0
    break;
1009
1010
0
  case NET_PPP:
1011
0
    wth->file_encap = WTAP_ENCAP_PPP_WITH_PHDR;
1012
0
    break;
1013
1014
0
  default:
1015
    /*
1016
     * Reject these until we can figure them out.
1017
     */
1018
0
    *err = WTAP_ERR_UNSUPPORTED;
1019
0
    *err_info = ws_strdup_printf("ngsniffer: WAN network subtype %u unknown or unsupported",
1020
0
        buffer[4]);
1021
0
    return -1;
1022
0
  }
1023
0
  return 0;
1024
0
}
1025
1026
/* Read the next packet */
1027
static bool
1028
ngsniffer_read(wtap *wth, wtap_rec *rec, int *err, char **err_info,
1029
    int64_t *data_offset)
1030
0
{
1031
0
  ngsniffer_t *ngsniffer;
1032
0
  struct rec_header hdr;
1033
0
  unsigned  padding;
1034
1035
0
  ngsniffer = (ngsniffer_t *)wth->priv;
1036
0
  for (;;) {
1037
    /*
1038
     * We use the uncompressed offset, as that's what
1039
     * we need to use for compressed files.
1040
     */
1041
0
    *data_offset = ngsniffer->seq.uncomp_offset;
1042
1043
    /*
1044
     * Read the record header.
1045
     */
1046
0
    if (!read_rec_header(wth, false, &hdr, err, err_info)) {
1047
      /* Read error or short read */
1048
0
      return false;
1049
0
    }
1050
1051
    /*
1052
     * Process the record.
1053
     */
1054
0
    switch (hdr.type) {
1055
1056
0
    case REC_FRAME2:
1057
0
    case REC_FRAME4:
1058
0
    case REC_FRAME6:
1059
      /* Frame record */
1060
0
      if (!process_frame_record(wth, false, &padding,
1061
0
          &hdr, rec, err, err_info)) {
1062
        /* Read error, short read, or other error */
1063
0
        return false;
1064
0
      }
1065
1066
      /*
1067
       * Skip any extra data in the record.
1068
       */
1069
0
      if (padding != 0) {
1070
0
        if (!ng_skip_bytes_seq(wth, padding, err,
1071
0
            err_info))
1072
0
          return false;
1073
0
      }
1074
0
      return true;
1075
1076
0
    case REC_EOF:
1077
      /*
1078
       * End of file.  Skip past any data (if any),
1079
       * the length of which is in hdr.length, and
1080
       * return an EOF indication.
1081
       */
1082
0
      if (hdr.length != 0) {
1083
0
        if (!ng_skip_bytes_seq(wth, hdr.length, err,
1084
0
            err_info))
1085
0
          return false;
1086
0
      }
1087
0
      *err = 0; /* EOF, not error */
1088
0
      return false;
1089
1090
0
    default:
1091
      /*
1092
       * Well, we don't know what it is, or we know what
1093
       * it is but can't handle it.  Skip past the data
1094
       * portion (if any), the length of which is in
1095
       * hdr.length, and keep looping.
1096
       */
1097
0
      if (hdr.length != 0) {
1098
0
        if (!ng_skip_bytes_seq(wth, hdr.length, err,
1099
0
            err_info))
1100
0
          return false;
1101
0
      }
1102
0
      break;
1103
0
    }
1104
0
  }
1105
0
}
1106
1107
static bool
1108
ngsniffer_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec,
1109
    int *err, char **err_info)
1110
0
{
1111
0
  struct rec_header hdr;
1112
1113
0
  if (!ng_file_seek_rand(wth, seek_off, err, err_info))
1114
0
    return false;
1115
1116
0
  if (!read_rec_header(wth, true, &hdr, err, err_info)) {
1117
    /* Read error or short read */
1118
0
    return false;
1119
0
  }
1120
1121
  /*
1122
   * hdr.type is the record type.
1123
   */
1124
0
  switch (hdr.type) {
1125
1126
0
  case REC_FRAME2:
1127
0
  case REC_FRAME4:
1128
0
  case REC_FRAME6:
1129
    /* Frame record */
1130
0
    if (!process_frame_record(wth, true, NULL, &hdr, rec,
1131
0
        err, err_info)) {
1132
      /* Read error, short read, or other error */
1133
0
      return false;
1134
0
    }
1135
0
    break;
1136
1137
0
  default:
1138
    /*
1139
     * Other record type, or EOF.
1140
     * This "can't happen".
1141
     */
1142
0
    ws_assert_not_reached();
1143
0
    return false;
1144
0
  }
1145
1146
0
  return true;
1147
0
}
1148
1149
/*
1150
 * Read the record header.
1151
 *
1152
 * Returns true on success, false on error.
1153
 */
1154
static bool
1155
read_rec_header(wtap *wth, bool is_random, struct rec_header *hdr,
1156
    int *err, char **err_info)
1157
0
{
1158
0
  char  record_type[2];
1159
0
  char  record_length[4]; /* only 1st 2 bytes are length */
1160
1161
  /*
1162
   * Read the record type.
1163
   */
1164
0
  if (!ng_read_bytes_or_eof(wth, record_type, 2, is_random, err, err_info)) {
1165
0
    if (*err != 0)
1166
0
      return false;
1167
    /*
1168
     * End-of-file; construct a fake EOF record.
1169
     * (A file might have an EOF record at the end, or
1170
     * it might just come to an end.)
1171
     * (XXX - is that true of all Sniffer files?)
1172
     */
1173
0
    hdr->type = REC_EOF;
1174
0
    hdr->length = 0;
1175
0
    return true;
1176
0
  }
1177
1178
  /*
1179
   * Read the record length.
1180
   */
1181
0
  if (!ng_read_bytes(wth, record_length, 4, is_random, err, err_info))
1182
0
    return false;
1183
1184
0
  hdr->type = pletoh16(record_type);
1185
0
  hdr->length = pletoh16(record_length);
1186
0
  return true;
1187
0
}
1188
1189
/*
1190
 * Returns true on success, false on error.
1191
 * If padding is non-null, sets *padding to the amount of padding at
1192
 * the end of the record.
1193
 */
1194
static bool
1195
process_frame_record(wtap *wth, bool is_random, unsigned *padding,
1196
    struct rec_header *hdr, wtap_rec *rec, int *err,
1197
    char **err_info)
1198
0
{
1199
0
  ngsniffer_t *ngsniffer;
1200
0
  unsigned  rec_length_remaining;
1201
0
  struct frame2_rec frame2;
1202
0
  struct frame4_rec frame4;
1203
0
  struct frame6_rec frame6;
1204
0
  uint16_t  time_low, time_med, true_size, size;
1205
0
  uint8_t time_high, time_day;
1206
0
  uint64_t t, tsecs, tpsecs;
1207
1208
0
  rec_length_remaining = hdr->length;
1209
1210
  /* Initialize - we'll be setting some presence flags below. */
1211
0
  wtap_setup_packet_rec(rec, wth->file_encap);
1212
0
  rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
1213
0
  rec->presence_flags = 0;
1214
1215
0
  ngsniffer = (ngsniffer_t *)wth->priv;
1216
0
  switch (hdr->type) {
1217
1218
0
  case REC_FRAME2:
1219
0
    if (ngsniffer->network == NETWORK_ATM) {
1220
      /*
1221
       * We shouldn't get a frame2 record in
1222
       * an ATM capture.
1223
       */
1224
0
      *err = WTAP_ERR_BAD_FILE;
1225
0
      *err_info = g_strdup("ngsniffer: REC_FRAME2 record in an ATM Sniffer file");
1226
0
      return false;
1227
0
    }
1228
1229
    /* Do we have an f_frame2_struct worth of data? */
1230
0
    if (rec_length_remaining < sizeof frame2) {
1231
0
      *err = WTAP_ERR_BAD_FILE;
1232
0
      *err_info = g_strdup("ngsniffer: REC_FRAME2 record length is less than record header length");
1233
0
      return false;
1234
0
    }
1235
1236
    /* Read the f_frame2_struct */
1237
0
    if (!ng_read_bytes(wth, &frame2, (unsigned int)sizeof frame2,
1238
0
       is_random, err, err_info))
1239
0
      return false;
1240
0
    time_low = pletoh16(&frame2.time_low);
1241
0
    time_med = pletoh16(&frame2.time_med);
1242
0
    time_high = frame2.time_high;
1243
0
    time_day = frame2.time_day;
1244
0
    size = pletoh16(&frame2.size);
1245
0
    true_size = pletoh16(&frame2.true_size);
1246
1247
0
    rec_length_remaining -= (unsigned)sizeof frame2;  /* we already read that much */
1248
1249
0
    set_metadata_frame2(wth, rec, &frame2);
1250
0
    break;
1251
1252
0
  case REC_FRAME4:
1253
0
    if (ngsniffer->network != NETWORK_ATM) {
1254
      /*
1255
       * We shouldn't get a frame2 record in
1256
       * a non-ATM capture.
1257
       */
1258
0
      *err = WTAP_ERR_BAD_FILE;
1259
0
      *err_info = g_strdup("ngsniffer: REC_FRAME4 record in a non-ATM Sniffer file");
1260
0
      return false;
1261
0
    }
1262
1263
    /*
1264
     * XXX - it looks as if some version 4 captures have
1265
     * a bogus record length, based on the assumption
1266
     * that the record is a frame2 record, i.e. the length
1267
     * was calculated based on the record being a frame2
1268
     * record, so it's too short by (sizeof frame4 - sizeof frame2).
1269
     */
1270
0
    if (ngsniffer->maj_vers < 5 && ngsniffer->min_vers >= 95)
1271
0
      rec_length_remaining += (unsigned)(sizeof frame4 - sizeof frame2);
1272
1273
    /* Do we have an f_frame4_struct worth of data? */
1274
0
    if (rec_length_remaining < sizeof frame4) {
1275
0
      *err = WTAP_ERR_BAD_FILE;
1276
0
      *err_info = g_strdup("ngsniffer: REC_FRAME4 record length is less than record header length");
1277
0
      return false;
1278
0
    }
1279
1280
    /* Read the f_frame4_struct */
1281
0
    if (!ng_read_bytes(wth, &frame4, (unsigned int)sizeof frame4,
1282
0
        is_random, err, err_info))
1283
0
      return false;
1284
0
    time_low = pletoh16(&frame4.time_low);
1285
0
    time_med = pletoh16(&frame4.time_med);
1286
0
    time_high = frame4.time_high;
1287
0
    time_day = frame4.time_day;
1288
0
    size = pletoh16(&frame4.size);
1289
0
    true_size = pletoh16(&frame4.true_size);
1290
1291
0
    rec_length_remaining -= (unsigned)sizeof frame4;  /* we already read that much */
1292
1293
0
    set_pseudo_header_frame4(&rec->rec_header.packet_header.pseudo_header, &frame4);
1294
0
    break;
1295
1296
0
  case REC_FRAME6:
1297
    /* Do we have an f_frame6_struct worth of data? */
1298
0
    if (rec_length_remaining < sizeof frame6) {
1299
0
      *err = WTAP_ERR_BAD_FILE;
1300
0
      *err_info = g_strdup("ngsniffer: REC_FRAME6 record length is less than record header length");
1301
0
      return false;
1302
0
    }
1303
1304
    /* Read the f_frame6_struct */
1305
0
    if (!ng_read_bytes(wth, &frame6, (unsigned int)sizeof frame6,
1306
0
        is_random, err, err_info))
1307
0
      return false;
1308
0
    time_low = pletoh16(&frame6.time_low);
1309
0
    time_med = pletoh16(&frame6.time_med);
1310
0
    time_high = frame6.time_high;
1311
0
    time_day = frame6.time_day;
1312
0
    size = pletoh16(&frame6.size);
1313
0
    true_size = pletoh16(&frame6.true_size);
1314
1315
0
    rec_length_remaining -= (unsigned)sizeof frame6;  /* we already read that much */
1316
1317
0
    set_pseudo_header_frame6(wth, &rec->rec_header.packet_header.pseudo_header, &frame6);
1318
0
    break;
1319
1320
0
  default:
1321
    /*
1322
     * This should never happen.
1323
     */
1324
0
    ws_assert_not_reached();
1325
0
    return false;
1326
0
  }
1327
1328
  /*
1329
   * Is the frame data size greater than what's left of the
1330
   * record?
1331
   */
1332
0
  if (size > rec_length_remaining) {
1333
    /*
1334
     * Yes - treat this as an error.
1335
     */
1336
0
    *err = WTAP_ERR_BAD_FILE;
1337
0
    *err_info = g_strdup("ngsniffer: Record length is less than packet size");
1338
0
    return false;
1339
0
  }
1340
1341
  /*
1342
   * The maximum value of length is 65535, which is less than
1343
   * WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check
1344
   * it.
1345
   */
1346
0
  if (padding != NULL) {
1347
    /*
1348
     * Padding, if the frame data size is less than what's
1349
     * left of the record.
1350
     */
1351
0
    *padding = rec_length_remaining - size;
1352
0
  }
1353
1354
0
  rec->presence_flags |= true_size ? WTAP_HAS_TS|WTAP_HAS_CAP_LEN : WTAP_HAS_TS;
1355
0
  rec->rec_header.packet_header.len = true_size ? true_size : size;
1356
0
  rec->rec_header.packet_header.caplen = size;
1357
1358
  /*
1359
   * Read the packet data.
1360
   */
1361
0
  ws_buffer_assure_space(&rec->data, size);
1362
0
  if (!ng_read_bytes(wth, ws_buffer_start_ptr(&rec->data), size, is_random,
1363
0
      err, err_info))
1364
0
    return false;
1365
1366
0
  rec->rec_header.packet_header.pkt_encap = fix_pseudo_header(wth->file_encap,
1367
0
      rec, size);
1368
1369
  /*
1370
   * 40-bit time stamp, in units of timeunit picoseconds.
1371
   */
1372
0
  t = (((uint64_t)time_high)<<32) | (((uint64_t)time_med) << 16) | time_low;
1373
1374
  /*
1375
   * timeunit is always < 2^(64-40), so t * timeunit fits in 64
1376
   * bits.  That gives a 64-bit time stamp, in units of
1377
   * picoseconds.
1378
   */
1379
0
  t *= ngsniffer->timeunit;
1380
1381
  /*
1382
   * Convert to seconds and picoseconds.
1383
   */
1384
0
  tsecs = t/UINT64_C(1000000000000);
1385
0
  tpsecs = t - tsecs*UINT64_C(1000000000000);
1386
1387
  /*
1388
   * Add in the time_day value (86400 seconds/day).
1389
   */
1390
0
  tsecs += time_day*86400;
1391
1392
  /*
1393
   * Add in the capture start time.
1394
   */
1395
0
  tsecs += ngsniffer->start;
1396
1397
0
  rec->ts.secs = (time_t)tsecs;
1398
0
  rec->ts.nsecs = (int)(tpsecs/1000); /* psecs to nsecs */
1399
1400
0
  return true;  /* success */
1401
0
}
1402
1403
static void
1404
set_metadata_frame2(wtap *wth, wtap_rec *rec, struct frame2_rec *frame2)
1405
0
{
1406
0
  ngsniffer_t *ngsniffer;
1407
0
  uint32_t pack_flags;
1408
0
  union wtap_pseudo_header *pseudo_header;
1409
1410
0
  ngsniffer = (ngsniffer_t *)wth->priv;
1411
1412
  /*
1413
   * In one PPP "Internetwork analyzer" capture:
1414
   *
1415
   *  The only bit seen in "frame2.fs" is the 0x80 bit, which
1416
   *  probably indicates the packet's direction; all other
1417
   *  bits were zero.  The Expert Sniffer Network Analyzer
1418
   *  5.50 Operations manual says that bit is the FS_DTE bit
1419
   *  for async/PPP data.  The other bits are error bits
1420
   *  plus bits indicating whether the frame is PPP or SLIP,
1421
   *  but the PPP bit isn't set.
1422
   *
1423
   *  All bits in "frame2.flags" were zero.
1424
   *
1425
   * In one X.25 "Internetwork analyzer" capture:
1426
   *
1427
   *  The only bit seen in "frame2.fs" is the 0x80 bit, which
1428
   *  probably indicates the packet's direction; all other
1429
   *  bits were zero.
1430
   *
1431
   *  "frame2.flags" was always 0x18; however, the Sniffer
1432
   *  manual says that just means that a display filter was
1433
   *  calculated for the frame, and it should be displayed,
1434
   *  so perhaps that's just a quirk of that particular capture.
1435
   *
1436
   * In one Ethernet capture:
1437
   *
1438
   *  "frame2.fs" was always 0; the Sniffer manual says they're
1439
   *  error bits of various sorts.
1440
   *
1441
   *  "frame2.flags" was either 0 or 0x18, with no obvious
1442
   *  correlation with anything.  See previous comment
1443
   *  about display filters.
1444
   *
1445
   * In one Token Ring capture:
1446
   *
1447
   *  "frame2.fs" was either 0 or 0xcc; the Sniffer manual says
1448
   *  nothing about those bits for Token Ring captures.
1449
   *
1450
   *  "frame2.flags" was either 0 or 0x18, with no obvious
1451
   *  correlation with anything.  See previous comment
1452
   *  about display filters.
1453
   */
1454
0
  switch (ngsniffer->network) {
1455
1456
0
  case NETWORK_ENET:
1457
0
    pack_flags = 0;
1458
0
    if (frame2->fs & FS_ETH_CRC)
1459
0
      pack_flags |= PACK_FLAGS_CRC_ERROR;
1460
0
    if (frame2->fs & FS_ETH_ALIGN)
1461
0
      pack_flags |= PACK_FLAGS_UNALIGNED_FRAME;
1462
0
    if (frame2->fs & FS_ETH_RUNT)
1463
0
      pack_flags |= PACK_FLAGS_PACKET_TOO_SHORT;
1464
0
    wtap_block_add_uint32_option(rec->block, OPT_PKT_FLAGS, pack_flags);
1465
0
    break;
1466
1467
0
  case NETWORK_FDDI:
1468
0
    pack_flags = 0;
1469
0
    if (!(frame2->fs & FS_FDDI_INVALID) &&
1470
0
        (frame2->fs & (FS_FDDI_PCI_CRC|FS_FDDI_ISA_CRC)))
1471
0
      pack_flags |= PACK_FLAGS_CRC_ERROR;
1472
0
    wtap_block_add_uint32_option(rec->block, OPT_PKT_FLAGS, pack_flags);
1473
0
    break;
1474
1475
0
  case NETWORK_SYNCHRO:
1476
0
    pack_flags = 0;
1477
0
    if (frame2->fs & FS_SYNC_CRC)
1478
0
      pack_flags |= PACK_FLAGS_CRC_ERROR;
1479
0
    wtap_block_add_uint32_option(rec->block, OPT_PKT_FLAGS, pack_flags);
1480
0
    break;
1481
0
  }
1482
1483
0
  pseudo_header = &rec->rec_header.packet_header.pseudo_header;
1484
0
  switch (wth->file_encap) {
1485
1486
0
  case WTAP_ENCAP_ETHERNET:
1487
    /*
1488
     * XXX - do we ever have an FCS?  If not, why do we often
1489
     * have 4 extra bytes of stuff at the end?  Do some
1490
     * PC Ethernet interfaces report the length including the
1491
     * FCS but not store the FCS in the packet, or do some
1492
     * Ethernet drivers work that way?
1493
     */
1494
0
    pseudo_header->eth.fcs_len = 0;
1495
0
    break;
1496
1497
0
  case WTAP_ENCAP_PPP_WITH_PHDR:
1498
0
  case WTAP_ENCAP_SDLC:
1499
0
    pseudo_header->p2p.sent = (frame2->fs & FS_WAN_DTE) ? true : false;
1500
0
    break;
1501
1502
0
  case WTAP_ENCAP_LAPB:
1503
0
  case WTAP_ENCAP_FRELAY_WITH_PHDR:
1504
0
  case WTAP_ENCAP_PER_PACKET:
1505
0
    pseudo_header->dte_dce.flags = (frame2->fs & FS_WAN_DTE) ? 0x00 : FROM_DCE;
1506
0
    break;
1507
1508
0
  case WTAP_ENCAP_ISDN:
1509
0
    pseudo_header->isdn.uton = (frame2->fs & FS_WAN_DTE) ? false : true;
1510
0
    switch (frame2->fs & FS_ISDN_CHAN_MASK) {
1511
1512
0
    case FS_ISDN_CHAN_D:
1513
0
      pseudo_header->isdn.channel = 0;  /* D-channel */
1514
0
      break;
1515
1516
0
    case FS_ISDN_CHAN_B1:
1517
0
      pseudo_header->isdn.channel = 1;  /* B1-channel */
1518
0
      break;
1519
1520
0
    case FS_ISDN_CHAN_B2:
1521
0
      pseudo_header->isdn.channel = 2;  /* B2-channel */
1522
0
      break;
1523
1524
0
    default:
1525
0
      pseudo_header->isdn.channel = 30; /* XXX */
1526
0
      break;
1527
0
    }
1528
0
  }
1529
0
}
1530
1531
static void
1532
set_pseudo_header_frame4(union wtap_pseudo_header *pseudo_header,
1533
    struct frame4_rec *frame4)
1534
0
{
1535
0
  uint32_t StatusWord;
1536
0
  uint8_t aal_type, hl_type;
1537
0
  uint16_t vpi, vci;
1538
1539
  /*
1540
   * Map flags from frame4.atm_info.StatusWord.
1541
   */
1542
0
  pseudo_header->atm.flags = 0;
1543
0
  StatusWord = pletoh32(&frame4->atm_info.StatusWord);
1544
0
  if (StatusWord & SW_RAW_CELL)
1545
0
    pseudo_header->atm.flags |= ATM_RAW_CELL;
1546
1547
0
  aal_type = frame4->atm_info.AppTrafType & ATT_AALTYPE;
1548
0
  hl_type = frame4->atm_info.AppTrafType & ATT_HLTYPE;
1549
0
  vpi = pletoh16(&frame4->atm_info.Vpi);
1550
0
  vci = pletoh16(&frame4->atm_info.Vci);
1551
1552
0
  switch (aal_type) {
1553
1554
0
  case ATT_AAL_UNKNOWN:
1555
    /*
1556
     * Map ATT_AAL_UNKNOWN on VPI 0, VCI 5 to ATT_AAL_SIGNALLING,
1557
     * as that's the VPCI used for signalling.
1558
     *
1559
     * XXX - is this necessary, or will frames to 0/5 always
1560
     * have ATT_AAL_SIGNALLING?
1561
     */
1562
0
    if (vpi == 0 && vci == 5)
1563
0
      pseudo_header->atm.aal = AAL_SIGNALLING;
1564
0
    else
1565
0
      pseudo_header->atm.aal = AAL_UNKNOWN;
1566
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1567
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1568
0
    break;
1569
1570
0
  case ATT_AAL1:
1571
0
    pseudo_header->atm.aal = AAL_1;
1572
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1573
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1574
0
    break;
1575
1576
0
  case ATT_AAL3_4:
1577
0
    pseudo_header->atm.aal = AAL_3_4;
1578
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1579
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1580
0
    break;
1581
1582
0
  case ATT_AAL5:
1583
0
    pseudo_header->atm.aal = AAL_5;
1584
0
    switch (hl_type) {
1585
1586
0
    case ATT_HL_UNKNOWN:
1587
0
      pseudo_header->atm.type = TRAF_UNKNOWN;
1588
0
      pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1589
0
      break;
1590
1591
0
    case ATT_HL_LLCMX:
1592
0
      pseudo_header->atm.type = TRAF_LLCMX;
1593
0
      pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1594
0
      break;
1595
1596
0
    case ATT_HL_VCMX:
1597
0
      pseudo_header->atm.type = TRAF_VCMX;
1598
0
      switch (frame4->atm_info.AppHLType) {
1599
1600
0
      case AHLT_UNKNOWN:
1601
0
        pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1602
0
        break;
1603
1604
0
      case AHLT_VCMX_802_3_FCS:
1605
0
        pseudo_header->atm.subtype =
1606
0
            TRAF_ST_VCMX_802_3_FCS;
1607
0
        break;
1608
1609
0
      case AHLT_VCMX_802_4_FCS:
1610
0
        pseudo_header->atm.subtype =
1611
0
            TRAF_ST_VCMX_802_4_FCS;
1612
0
        break;
1613
1614
0
      case AHLT_VCMX_802_5_FCS:
1615
0
        pseudo_header->atm.subtype =
1616
0
            TRAF_ST_VCMX_802_5_FCS;
1617
0
        break;
1618
1619
0
      case AHLT_VCMX_FDDI_FCS:
1620
0
        pseudo_header->atm.subtype =
1621
0
            TRAF_ST_VCMX_FDDI_FCS;
1622
0
        break;
1623
1624
0
      case AHLT_VCMX_802_6_FCS:
1625
0
        pseudo_header->atm.subtype =
1626
0
            TRAF_ST_VCMX_802_6_FCS;
1627
0
        break;
1628
1629
0
      case AHLT_VCMX_802_3:
1630
0
        pseudo_header->atm.subtype = TRAF_ST_VCMX_802_3;
1631
0
        break;
1632
1633
0
      case AHLT_VCMX_802_4:
1634
0
        pseudo_header->atm.subtype = TRAF_ST_VCMX_802_4;
1635
0
        break;
1636
1637
0
      case AHLT_VCMX_802_5:
1638
0
        pseudo_header->atm.subtype = TRAF_ST_VCMX_802_5;
1639
0
        break;
1640
1641
0
      case AHLT_VCMX_FDDI:
1642
0
        pseudo_header->atm.subtype = TRAF_ST_VCMX_FDDI;
1643
0
        break;
1644
1645
0
      case AHLT_VCMX_802_6:
1646
0
        pseudo_header->atm.subtype = TRAF_ST_VCMX_802_6;
1647
0
        break;
1648
1649
0
      case AHLT_VCMX_FRAGMENTS:
1650
0
        pseudo_header->atm.subtype =
1651
0
            TRAF_ST_VCMX_FRAGMENTS;
1652
0
        break;
1653
1654
0
      case AHLT_VCMX_BPDU:
1655
0
        pseudo_header->atm.subtype = TRAF_ST_VCMX_BPDU;
1656
0
        break;
1657
1658
0
      default:
1659
0
        pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1660
0
        break;
1661
0
      }
1662
0
      break;
1663
1664
0
    case ATT_HL_LANE:
1665
0
      pseudo_header->atm.type = TRAF_LANE;
1666
0
      switch (frame4->atm_info.AppHLType) {
1667
1668
0
      case AHLT_UNKNOWN:
1669
0
        pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1670
0
        break;
1671
1672
0
      case AHLT_LANE_LE_CTRL:
1673
0
        pseudo_header->atm.subtype =
1674
0
            TRAF_ST_LANE_LE_CTRL;
1675
0
        break;
1676
1677
0
      case AHLT_LANE_802_3:
1678
0
        pseudo_header->atm.subtype = TRAF_ST_LANE_802_3;
1679
0
        break;
1680
1681
0
      case AHLT_LANE_802_5:
1682
0
        pseudo_header->atm.subtype = TRAF_ST_LANE_802_5;
1683
0
        break;
1684
1685
0
      case AHLT_LANE_802_3_MC:
1686
0
        pseudo_header->atm.subtype =
1687
0
            TRAF_ST_LANE_802_3_MC;
1688
0
        break;
1689
1690
0
      case AHLT_LANE_802_5_MC:
1691
0
        pseudo_header->atm.subtype =
1692
0
            TRAF_ST_LANE_802_5_MC;
1693
0
        break;
1694
1695
0
      default:
1696
0
        pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1697
0
        break;
1698
0
      }
1699
0
      break;
1700
1701
0
    case ATT_HL_ILMI:
1702
0
      pseudo_header->atm.type = TRAF_ILMI;
1703
0
      pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1704
0
      break;
1705
1706
0
    case ATT_HL_FRMR:
1707
0
      pseudo_header->atm.type = TRAF_FR;
1708
0
      pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1709
0
      break;
1710
1711
0
    case ATT_HL_SPANS:
1712
0
      pseudo_header->atm.type = TRAF_SPANS;
1713
0
      pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1714
0
      break;
1715
1716
0
    case ATT_HL_IPSILON:
1717
0
      pseudo_header->atm.type = TRAF_IPSILON;
1718
0
      switch (frame4->atm_info.AppHLType) {
1719
1720
0
      case AHLT_UNKNOWN:
1721
0
        pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1722
0
        break;
1723
1724
0
      case AHLT_IPSILON_FT0:
1725
0
        pseudo_header->atm.subtype =
1726
0
            TRAF_ST_IPSILON_FT0;
1727
0
        break;
1728
1729
0
      case AHLT_IPSILON_FT1:
1730
0
        pseudo_header->atm.subtype =
1731
0
            TRAF_ST_IPSILON_FT1;
1732
0
        break;
1733
1734
0
      case AHLT_IPSILON_FT2:
1735
0
        pseudo_header->atm.subtype =
1736
0
            TRAF_ST_IPSILON_FT2;
1737
0
        break;
1738
1739
0
      default:
1740
0
        pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1741
0
        break;
1742
0
      }
1743
0
      break;
1744
1745
0
    default:
1746
0
      pseudo_header->atm.type = TRAF_UNKNOWN;
1747
0
      pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1748
0
      break;
1749
0
    }
1750
0
    break;
1751
1752
0
  case ATT_AAL_USER:
1753
0
    pseudo_header->atm.aal = AAL_USER;
1754
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1755
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1756
0
    break;
1757
1758
0
  case ATT_AAL_SIGNALLING:
1759
0
    pseudo_header->atm.aal = AAL_SIGNALLING;
1760
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1761
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1762
0
    break;
1763
1764
0
  case ATT_OAMCELL:
1765
0
    pseudo_header->atm.aal = AAL_OAMCELL;
1766
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1767
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1768
0
    break;
1769
1770
0
  default:
1771
0
    pseudo_header->atm.aal = AAL_UNKNOWN;
1772
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
1773
0
    pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
1774
0
    break;
1775
0
  }
1776
0
  pseudo_header->atm.vpi = vpi;
1777
0
  pseudo_header->atm.vci = vci;
1778
0
  pseudo_header->atm.channel = pletoh16(&frame4->atm_info.channel);
1779
0
  pseudo_header->atm.cells = pletoh16(&frame4->atm_info.cells);
1780
0
  pseudo_header->atm.aal5t_u2u = pletoh16(&frame4->atm_info.Trailer.aal5t_u2u);
1781
0
  pseudo_header->atm.aal5t_len = pletoh16(&frame4->atm_info.Trailer.aal5t_len);
1782
0
  pseudo_header->atm.aal5t_chksum = pntoh32(&frame4->atm_info.Trailer.aal5t_chksum);
1783
0
}
1784
1785
static void
1786
set_pseudo_header_frame6(wtap *wth, union wtap_pseudo_header *pseudo_header,
1787
    struct frame6_rec *frame6 _U_)
1788
0
{
1789
  /* XXX - Once the frame format is divined, something will most likely go here */
1790
1791
0
  switch (wth->file_encap) {
1792
1793
0
  case WTAP_ENCAP_ETHERNET:
1794
    /* XXX - is there an FCS? */
1795
0
    pseudo_header->eth.fcs_len = -1;
1796
0
    break;
1797
0
  }
1798
0
}
1799
1800
/*
1801
 * OK, this capture is from an "Internetwork analyzer", and we either
1802
 * didn't see a type 7 record or it had a network type such as NET_HDLC
1803
 * that doesn't tell us which *particular* HDLC derivative this is;
1804
 * let's look at the first few bytes of the packet, a pointer to which
1805
 * was passed to us as an argument, and see whether it looks like PPP,
1806
 * Frame Relay, Wellfleet HDLC, Cisco HDLC, or LAPB - or, if it's none
1807
 * of those, assume it's LAPD.
1808
 *
1809
 * (XXX - are there any "Internetwork analyzer" captures that don't
1810
 * have type 7 records?  If so, is there some other field that will
1811
 * tell us what type of capture it is?)
1812
 */
1813
static int
1814
infer_pkt_encap(const uint8_t *pd, int len)
1815
0
{
1816
0
  int i;
1817
1818
0
  if (len <= 0) {
1819
    /*
1820
     * Nothing to infer, but it doesn't matter how you
1821
     * dissect an empty packet.  Let's just say PPP.
1822
     */
1823
0
    return WTAP_ENCAP_PPP_WITH_PHDR;
1824
0
  }
1825
1826
0
  if (pd[0] == 0xFF) {
1827
    /*
1828
     * PPP.  (XXX - check for 0xFF 0x03?)
1829
     */
1830
0
    return WTAP_ENCAP_PPP_WITH_PHDR;
1831
0
  }
1832
1833
0
  if (len >= 2) {
1834
0
    if (pd[0] == 0x07 && pd[1] == 0x03) {
1835
      /*
1836
       * Wellfleet HDLC.
1837
       */
1838
0
      return WTAP_ENCAP_WFLEET_HDLC;
1839
0
    } else if ((pd[0] == 0x0F && pd[1] == 0x00) ||
1840
0
         (pd[0] == 0x8F && pd[1] == 0x00)) {
1841
      /*
1842
       * Cisco HDLC.
1843
       */
1844
0
      return WTAP_ENCAP_CHDLC_WITH_PHDR;
1845
0
    }
1846
1847
    /*
1848
     * Check for Frame Relay.  Look for packets with at least
1849
     * 3 bytes of header - 2 bytes of DLCI followed by 1 byte
1850
     * of control, which, for now, we require to be 0x03 (UI),
1851
     * although there might be other frame types as well.
1852
     * Scan forward until we see the last DLCI byte, with
1853
     * the low-order bit being 1, and then check the next
1854
     * byte, if it exists, to see if it's a control byte.
1855
     *
1856
     * XXX - in version 4 and 5 captures, wouldn't this just
1857
     * have a capture subtype of NET_FRAME_RELAY?  Or is this
1858
     * here only to handle other versions of the capture
1859
     * file, where we might just not yet have found where
1860
     * the subtype is specified in the capture?
1861
     *
1862
     * Bay Networks/Nortel Networks had a mechanism in the Optivity
1863
     * software for some of their routers to save captures
1864
     * in Sniffer format; they use a version number of 4.9, but
1865
     * don't put out any header records before the first FRAME2
1866
     * record. That means we have to use heuristics to guess
1867
     * what type of packet we have.
1868
     */
1869
0
    for (i = 0; i < len && (pd[i] & 0x01) == 0; i++)
1870
0
      ;
1871
0
    if (i >= len - 1) {
1872
      /*
1873
       * Either all the bytes have the low-order bit
1874
       * clear, so we didn't even find the last DLCI
1875
       * byte, or the very last byte had the low-order
1876
       * bit set, so, if that's a DLCI, it fills the
1877
       * buffer, so there is no control byte after
1878
       * the last DLCI byte.
1879
       */
1880
0
      return WTAP_ENCAP_LAPB;
1881
0
    }
1882
0
    i++;  /* advance to the byte after the last DLCI byte */
1883
0
    if (pd[i] == 0x03)
1884
0
      return WTAP_ENCAP_FRELAY_WITH_PHDR;
1885
0
  }
1886
1887
  /*
1888
   * Assume LAPB, for now.  If we support other HDLC encapsulations,
1889
   * we can check whether the low-order bit of the first byte is
1890
   * set (as it should be for LAPB) if no other checks pass.
1891
   *
1892
   * Or, if it's truly impossible to distinguish ISDN from non-ISDN
1893
   * captures, we could assume it's ISDN if it's not anything
1894
   * else.
1895
   */
1896
0
  return WTAP_ENCAP_LAPB;
1897
0
}
1898
1899
static int
1900
fix_pseudo_header(int encap, wtap_rec *rec, int len)
1901
0
{
1902
0
  const uint8_t *pd;
1903
0
  union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
1904
1905
0
  pd = ws_buffer_start_ptr(&rec->data);
1906
0
  switch (encap) {
1907
1908
0
  case WTAP_ENCAP_PER_PACKET:
1909
    /*
1910
     * Infer the packet type from the first two bytes.
1911
     */
1912
0
    encap = infer_pkt_encap(pd, len);
1913
1914
    /*
1915
     * Fix up the pseudo-header to match the new
1916
     * encapsulation type.
1917
     */
1918
0
    switch (encap) {
1919
1920
0
    case WTAP_ENCAP_WFLEET_HDLC:
1921
0
    case WTAP_ENCAP_CHDLC_WITH_PHDR:
1922
0
    case WTAP_ENCAP_PPP_WITH_PHDR:
1923
0
      if (pseudo_header->dte_dce.flags == 0)
1924
0
        pseudo_header->p2p.sent = true;
1925
0
      else
1926
0
        pseudo_header->p2p.sent = false;
1927
0
      break;
1928
1929
0
    case WTAP_ENCAP_ISDN:
1930
0
      if (pseudo_header->dte_dce.flags == 0x00)
1931
0
        pseudo_header->isdn.uton = false;
1932
0
      else
1933
0
        pseudo_header->isdn.uton = true;
1934
1935
      /*
1936
       * XXX - this is currently a per-packet
1937
       * encapsulation type, and we can't determine
1938
       * whether a capture is an ISDN capture before
1939
       * seeing any packets, and B-channel PPP packets
1940
       * look like PPP packets and are given
1941
       * WTAP_ENCAP_PPP_WITH_PHDR, not WTAP_ENCAP_ISDN,
1942
       * so we assume this is a D-channel packet and
1943
       * thus give it a channel number of 0.
1944
       */
1945
0
      pseudo_header->isdn.channel = 0;
1946
0
      break;
1947
0
    }
1948
0
    break;
1949
1950
0
  case WTAP_ENCAP_ATM_PDUS:
1951
    /*
1952
     * If the Windows Sniffer writes out one of its ATM
1953
     * capture files in DOS Sniffer format, it doesn't
1954
     * distinguish between LE Control and LANE encapsulated
1955
     * LAN frames, it just marks them as LAN frames,
1956
     * so we fix that up here.
1957
     *
1958
     * I've also seen DOS Sniffer captures claiming that
1959
     * LANE packets that *don't* start with FF 00 are
1960
     * marked as LE Control frames, so we fix that up
1961
     * as well.
1962
     */
1963
0
    if (pseudo_header->atm.type == TRAF_LANE && len >= 2) {
1964
0
      if (pd[0] == 0xff && pd[1] == 0x00) {
1965
        /*
1966
         * This must be LE Control.
1967
         */
1968
0
        pseudo_header->atm.subtype =
1969
0
            TRAF_ST_LANE_LE_CTRL;
1970
0
      } else {
1971
        /*
1972
         * This can't be LE Control.
1973
         */
1974
0
        if (pseudo_header->atm.subtype ==
1975
0
            TRAF_ST_LANE_LE_CTRL) {
1976
          /*
1977
           * XXX - Ethernet or Token Ring?
1978
           */
1979
0
          pseudo_header->atm.subtype =
1980
0
              TRAF_ST_LANE_802_3;
1981
0
        }
1982
0
      }
1983
0
    }
1984
0
    break;
1985
0
  }
1986
0
  return encap;
1987
0
}
1988
1989
/* Throw away the buffers used by the sequential I/O stream, but not
1990
   those used by the random I/O stream. */
1991
static void
1992
ngsniffer_sequential_close(wtap *wth)
1993
0
{
1994
0
  ngsniffer_t *ngsniffer;
1995
1996
0
  ngsniffer = (ngsniffer_t *)wth->priv;
1997
0
  if (ngsniffer->seq.buf != NULL) {
1998
0
    g_free(ngsniffer->seq.buf);
1999
0
    ngsniffer->seq.buf = NULL;
2000
0
  }
2001
0
}
2002
2003
static void
2004
free_blob(void *data, void *user_data _U_)
2005
0
{
2006
0
  g_free(data);
2007
0
}
2008
2009
/* Close stuff used by the random I/O stream, if any, and free up any
2010
   private data structures.  (If there's a "sequential_close" routine
2011
   for a capture file type, it'll be called before the "close" routine
2012
   is called, so we don't have to free the sequential buffer here.) */
2013
static void
2014
ngsniffer_close(wtap *wth)
2015
0
{
2016
0
  ngsniffer_t *ngsniffer;
2017
2018
0
  ngsniffer = (ngsniffer_t *)wth->priv;
2019
0
  g_free(ngsniffer->rand.buf);
2020
0
  g_list_foreach(ngsniffer->first_blob, free_blob, NULL);
2021
0
  g_list_free(ngsniffer->first_blob);
2022
0
}
2023
2024
typedef struct {
2025
  bool first_frame;
2026
  time_t start;
2027
} ngsniffer_dump_t;
2028
2029
static const int wtap_encap[] = {
2030
  -1,   /* WTAP_ENCAP_UNKNOWN -> unsupported */
2031
  1,    /* WTAP_ENCAP_ETHERNET */
2032
  0,    /* WTAP_ENCAP_TOKEN_RING */
2033
  -1,   /* WTAP_ENCAP_SLIP -> unsupported */
2034
  7,    /* WTAP_ENCAP_PPP -> Internetwork analyzer (synchronous) FIXME ! */
2035
  9,    /* WTAP_ENCAP_FDDI */
2036
  9,    /* WTAP_ENCAP_FDDI_BITSWAPPED */
2037
  -1,   /* WTAP_ENCAP_RAW_IP -> unsupported */
2038
  2,    /* WTAP_ENCAP_ARCNET */
2039
  -1,   /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
2040
  -1,   /* WTAP_ENCAP_ATM_RFC1483 */
2041
  -1,   /* WTAP_ENCAP_LINUX_ATM_CLIP */
2042
  7,    /* WTAP_ENCAP_LAPB -> Internetwork analyzer (synchronous) */
2043
  -1,   /* WTAP_ENCAP_ATM_PDUS */
2044
  -1,   /* WTAP_ENCAP_NULL -> unsupported */
2045
  -1,   /* WTAP_ENCAP_ASCEND -> unsupported */
2046
  -1,   /* WTAP_ENCAP_ISDN -> unsupported */
2047
  -1,   /* WTAP_ENCAP_IP_OVER_FC -> unsupported */
2048
  7,    /* WTAP_ENCAP_PPP_WITH_PHDR -> Internetwork analyzer (synchronous) FIXME ! */
2049
};
2050
0
#define NUM_WTAP_ENCAPS array_length(wtap_encap)
2051
2052
/* Returns 0 if we could write the specified encapsulation type,
2053
   an error indication otherwise. */
2054
static int
2055
ngsniffer_dump_can_write_encap(int encap)
2056
0
{
2057
  /* Per-packet encapsulations aren't supported. */
2058
0
  if (encap == WTAP_ENCAP_PER_PACKET)
2059
0
    return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
2060
2061
0
  if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
2062
0
    return WTAP_ERR_UNWRITABLE_ENCAP;
2063
2064
0
  return 0;
2065
0
}
2066
2067
/* Returns true on success, false on failure; sets "*err" to an error code on
2068
   failure */
2069
static bool
2070
ngsniffer_dump_open(wtap_dumper *wdh, int *err, char **err_info _U_)
2071
0
{
2072
0
  ngsniffer_dump_t *ngsniffer;
2073
0
  char buf[6] = {REC_VERS, 0x00, 0x12, 0x00, 0x00, 0x00}; /* version record */
2074
2075
  /* This is a sniffer file */
2076
0
  wdh->subtype_write = ngsniffer_dump;
2077
0
  wdh->subtype_finish = ngsniffer_dump_finish;
2078
2079
0
  ngsniffer = g_new(ngsniffer_dump_t, 1);
2080
0
  wdh->priv = (void *)ngsniffer;
2081
0
  ngsniffer->first_frame = true;
2082
0
  ngsniffer->start = 0;
2083
2084
  /* Write the file header. */
2085
0
  if (!wtap_dump_file_write(wdh, ngsniffer_magic, sizeof ngsniffer_magic,
2086
0
          err))
2087
0
    return false;
2088
0
  if (!wtap_dump_file_write(wdh, buf, 6, err))
2089
0
    return false;
2090
2091
0
  return true;
2092
0
}
2093
2094
/* Write a record for a packet to a dump file.
2095
   Returns true on success, false on failure. */
2096
static bool
2097
ngsniffer_dump(wtap_dumper *wdh, const wtap_rec *rec,
2098
         int *err, char **err_info _U_)
2099
0
{
2100
0
  const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
2101
0
  ngsniffer_dump_t *ngsniffer = (ngsniffer_dump_t *)wdh->priv;
2102
0
  struct frame2_rec rec_hdr;
2103
0
  char buf[6];
2104
0
  time_t tsecs;
2105
0
  uint64_t t;
2106
0
  uint16_t t_low, t_med;
2107
0
  uint8_t t_high;
2108
0
  struct vers_rec version;
2109
0
  int16_t maj_vers, min_vers;
2110
0
  uint16_t start_date;
2111
0
  struct tm *tm;
2112
2113
  /* We can only write packet records. */
2114
0
  if (rec->rec_type != REC_TYPE_PACKET) {
2115
0
    *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
2116
0
    *err_info = wtap_unwritable_rec_type_err_string(rec);
2117
0
    return false;
2118
0
  }
2119
2120
  /*
2121
   * Make sure this packet doesn't have a link-layer type that
2122
   * differs from the one for the file.
2123
   */
2124
0
  if (wdh->file_encap != rec->rec_header.packet_header.pkt_encap) {
2125
0
    *err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
2126
0
    return false;
2127
0
  }
2128
2129
  /* The captured length field is 16 bits, so there's a hard
2130
     limit of 65535. */
2131
0
  if (rec->rec_header.packet_header.caplen > 65535) {
2132
0
    *err = WTAP_ERR_PACKET_TOO_LARGE;
2133
0
    return false;
2134
0
  }
2135
2136
  /* Sniffer files have a capture start date in the file header, and
2137
     have times relative to the beginning of that day in the packet
2138
     headers; pick the date of the first packet as the capture start
2139
     date. */
2140
0
  if (ngsniffer->first_frame) {
2141
0
    ngsniffer->first_frame=false;
2142
0
    tm = localtime(&rec->ts.secs);
2143
0
    if (tm != NULL && tm->tm_year >= DOS_YEAR_OFFSET) {
2144
0
      start_date = (tm->tm_year - DOS_YEAR_OFFSET) << DOS_YEAR_SHIFT;
2145
0
      start_date |= (tm->tm_mon - DOS_MONTH_OFFSET) << DOS_MONTH_SHIFT;
2146
0
      start_date |= tm->tm_mday << DOS_DAY_SHIFT;
2147
      /* record the start date, not the start time */
2148
0
      ngsniffer->start = rec->ts.secs - (3600*tm->tm_hour + 60*tm->tm_min + tm->tm_sec);
2149
0
    } else {
2150
0
      start_date = 0;
2151
0
      ngsniffer->start = 0;
2152
0
    }
2153
2154
    /* "sniffer" version ? */
2155
0
    maj_vers = 4;
2156
0
    min_vers = 0;
2157
0
    version.maj_vers = GUINT16_TO_LE(maj_vers);
2158
0
    version.min_vers = GUINT16_TO_LE(min_vers);
2159
0
    version.time_dos = 0;
2160
0
    version.date = GUINT16_TO_LE(start_date);
2161
0
    version.type = 4;
2162
0
    version.network = wtap_encap[wdh->file_encap];
2163
0
    version.format = 1;
2164
0
    version.timeunit = 1; /* 0.838096 */
2165
0
    version.cmprs_vers = 0;
2166
0
    version.cmprs_level = 0;
2167
0
    version.rsvd[0] = 0;
2168
0
    version.rsvd[1] = 0;
2169
0
    if (!wtap_dump_file_write(wdh, &version, sizeof version, err))
2170
0
      return false;
2171
0
  }
2172
2173
0
  buf[0] = REC_FRAME2;
2174
0
  buf[1] = 0x00;
2175
0
  buf[2] = (char)((rec->rec_header.packet_header.caplen + sizeof(struct frame2_rec))%256);
2176
0
  buf[3] = (char)((rec->rec_header.packet_header.caplen + sizeof(struct frame2_rec))/256);
2177
0
  buf[4] = 0x00;
2178
0
  buf[5] = 0x00;
2179
0
  if (!wtap_dump_file_write(wdh, buf, 6, err))
2180
0
    return false;
2181
  /* Seconds since the start of the capture */
2182
0
  tsecs = rec->ts.secs - ngsniffer->start;
2183
  /* Extract the number of days since the start of the capture */
2184
0
  rec_hdr.time_day = (uint8_t)(tsecs / 86400);  /* # days of capture - 86400 secs/day */
2185
0
  tsecs -= rec_hdr.time_day * 86400;  /* time within day */
2186
  /* Convert to picoseconds */
2187
0
  t = tsecs*UINT64_C(1000000000000) +
2188
0
    rec->ts.nsecs*UINT64_C(1000);
2189
  /* Convert to units of timeunit = 1 */
2190
0
  t /= Psec[1];
2191
0
  t_low = (uint16_t)((t >> 0) & 0xFFFF);
2192
0
  t_med = (uint16_t)((t >> 16) & 0xFFFF);
2193
0
  t_high = (uint8_t)((t >> 32) & 0xFF);
2194
0
  rec_hdr.time_low = GUINT16_TO_LE(t_low);
2195
0
  rec_hdr.time_med = GUINT16_TO_LE(t_med);
2196
0
  rec_hdr.time_high = t_high;
2197
0
  rec_hdr.size = GUINT16_TO_LE(rec->rec_header.packet_header.caplen);
2198
0
  switch (wdh->file_encap) {
2199
2200
0
  case WTAP_ENCAP_LAPB:
2201
0
  case WTAP_ENCAP_FRELAY_WITH_PHDR:
2202
0
    rec_hdr.fs = (pseudo_header->dte_dce.flags & FROM_DCE) ? 0x00 : FS_WAN_DTE;
2203
0
    break;
2204
2205
0
  case WTAP_ENCAP_PPP_WITH_PHDR:
2206
0
  case WTAP_ENCAP_SDLC:
2207
0
    rec_hdr.fs = pseudo_header->p2p.sent ? 0x00 : FS_WAN_DTE;
2208
0
    break;
2209
2210
0
  case WTAP_ENCAP_ISDN:
2211
0
    rec_hdr.fs = pseudo_header->isdn.uton ? FS_WAN_DTE : 0x00;
2212
0
    switch (pseudo_header->isdn.channel) {
2213
2214
0
    case 0:   /* D-channel */
2215
0
      rec_hdr.fs |= FS_ISDN_CHAN_D;
2216
0
      break;
2217
2218
0
    case 1:   /* B1-channel */
2219
0
      rec_hdr.fs |= FS_ISDN_CHAN_B1;
2220
0
      break;
2221
2222
0
    case 2:   /* B2-channel */
2223
0
      rec_hdr.fs |= FS_ISDN_CHAN_B2;
2224
0
      break;
2225
0
    }
2226
0
    break;
2227
2228
0
  default:
2229
0
    rec_hdr.fs = 0;
2230
0
    break;
2231
0
  }
2232
0
  rec_hdr.flags = 0;
2233
0
  rec_hdr.true_size = rec->rec_header.packet_header.len != rec->rec_header.packet_header.caplen ? GUINT16_TO_LE(rec->rec_header.packet_header.len) : 0;
2234
0
  rec_hdr.rsvd = 0;
2235
0
  if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
2236
0
    return false;
2237
0
  if (!wtap_dump_file_write(wdh, ws_buffer_start_ptr(&rec->data),
2238
0
      rec->rec_header.packet_header.caplen, err))
2239
0
    return false;
2240
0
  return true;
2241
0
}
2242
2243
/* Finish writing to a dump file.
2244
   Returns true on success, false on failure. */
2245
static bool
2246
ngsniffer_dump_finish(wtap_dumper *wdh, int *err, char **err_info _U_)
2247
0
{
2248
  /* EOF record */
2249
0
  char buf[6] = {REC_EOF, 0x00, 0x00, 0x00, 0x00, 0x00};
2250
2251
0
  if (!wtap_dump_file_write(wdh, buf, 6, err))
2252
0
    return false;
2253
0
  return true;
2254
0
}
2255
2256
/*
2257
   SnifferDecompress() decompresses a blob of compressed data from a
2258
   Sniffer(R) capture file.
2259
2260
   This function is Copyright (c) 1999-2999 Tim Farley
2261
2262
   Parameters
2263
      inbuf - buffer of compressed bytes from file, not including
2264
        the preceding length word
2265
      inlen - length of inbuf in bytes (max 64k)
2266
      outbuf - decompressed contents, could contain a partial Sniffer
2267
        record at the end.
2268
      outlen - length of outbuf.
2269
      err - return error code here
2270
      err_info - for WTAP_ERR_DECOMPRESS, return descriptive string here
2271
2272
   Return value is the number of bytes in outbuf on return.
2273
*/
2274
2275
/*
2276
 * Make sure we have at least "length" bytes remaining
2277
 * in the input buffer.
2278
 */
2279
#define CHECK_INPUT_POINTER( length ) \
2280
0
  if ( pin + (length - 1) >= pin_end ) \
2281
0
  { \
2282
0
    *err = WTAP_ERR_DECOMPRESS; \
2283
0
    *err_info = g_strdup("ngsniffer: Compressed data item goes past the end of the compressed block"); \
2284
0
    return ( -1 ); \
2285
0
  }
2286
2287
/*
2288
 * Make sure the byte containing the high order part of a buffer
2289
 * offset is present.
2290
 *
2291
 * If it is, then fetch it and combine it with the low-order part.
2292
 */
2293
#define FETCH_OFFSET_HIGH \
2294
0
  CHECK_INPUT_POINTER( 1 ); \
2295
0
  offset = code_low + ((unsigned int)(*pin++) << 4) + 3;
2296
2297
/*
2298
 * Make sure the output buffer is big enough to get "length"
2299
 * bytes added to it.
2300
 */
2301
#define CHECK_OUTPUT_LENGTH( length ) \
2302
0
  if ( pout + length > pout_end ) \
2303
0
  { \
2304
0
    *err = WTAP_ERR_UNC_OVERFLOW; \
2305
0
    return ( -1 ); \
2306
0
  }
2307
2308
/*
2309
 * Make sure we have another byte to fetch, and then fetch it and
2310
 * append it to the buffer "length" times.
2311
 */
2312
#define APPEND_RLE_BYTE( length ) \
2313
  /* If length would put us past end of output, avoid overflow */ \
2314
0
  CHECK_OUTPUT_LENGTH( length ); \
2315
0
  CHECK_INPUT_POINTER( 1 ); \
2316
0
  memset( pout, *pin++, length ); \
2317
0
  pout += length;
2318
2319
/*
2320
 * Make sure the specified offset and length refer, in the output
2321
 * buffer, to data that's entirely within the part of the output
2322
 * buffer that we've already filled in.
2323
 *
2324
 * Then append the string from the specified offset, with the
2325
 * specified length, to the output buffer.
2326
 */
2327
#define APPEND_LZW_STRING( offset, length ) \
2328
  /* If length would put us past end of output, avoid overflow */ \
2329
0
  CHECK_OUTPUT_LENGTH( length ); \
2330
0
  /* Check if offset would put us back past begin of buffer */ \
2331
0
  if ( pout - offset < outbuf ) \
2332
0
  { \
2333
0
    *err = WTAP_ERR_DECOMPRESS; \
2334
0
    *err_info = g_strdup("ngsniffer: LZ77 compressed data has bad offset to string"); \
2335
0
    return ( -1 ); \
2336
0
  } \
2337
0
  /* Check if offset would cause us to copy on top of ourselves */ \
2338
0
  if ( pout - offset + length > pout ) \
2339
0
  { \
2340
0
    *err = WTAP_ERR_DECOMPRESS; \
2341
0
    *err_info = g_strdup("ngsniffer: LZ77 compressed data has bad offset to string"); \
2342
0
    return ( -1 ); \
2343
0
  } \
2344
0
  /* Copy the string from previous text to output position, \
2345
0
     advance output pointer */ \
2346
0
  memcpy( pout, pout - offset, length ); \
2347
0
  pout += length;
2348
2349
static int
2350
SnifferDecompress(unsigned char *inbuf, size_t inlen, unsigned char *outbuf,
2351
      size_t outlen, int *err, char **err_info)
2352
0
{
2353
0
  unsigned char * pin  = inbuf;
2354
0
  unsigned char * pout = outbuf;
2355
0
  unsigned char * pin_end  = pin + inlen;
2356
0
  unsigned char * pout_end = pout + outlen;
2357
0
  unsigned int bit_mask;      /* one bit is set in this, to mask with bit_value */
2358
0
  unsigned int bit_value = 0; /* cache the last 16 coding bits we retrieved */
2359
0
  unsigned int code_type;     /* encoding type, from high 4 bits of byte */
2360
0
  unsigned int code_low;      /* other 4 bits from encoding byte */
2361
0
  int length;       /* length of RLE sequence or repeated string */
2362
0
  int offset;       /* offset of string to repeat */
2363
2364
0
  if (inlen > UINT16_MAX) {
2365
0
    return ( -1 );
2366
0
  }
2367
2368
0
  bit_mask  = 0;  /* don't have any bits yet */
2369
  /* Process until we've consumed all the input */
2370
0
  while (pin < pin_end)
2371
0
  {
2372
    /* Shift down the bit mask we use to see what's encoded */
2373
0
    bit_mask = bit_mask >> 1;
2374
2375
    /* If there are no bits left, time to get another 16 bits */
2376
0
    if ( 0 == bit_mask )
2377
0
    {
2378
      /* make sure there are at least *three* bytes
2379
         available - the two bytes of the bit value,
2380
         plus one byte after it */
2381
0
      CHECK_INPUT_POINTER( 3 );
2382
0
      bit_mask  = 0x8000;  /* start with the high bit */
2383
0
      bit_value = pletoh16(pin);   /* get the next 16 bits */
2384
0
      pin += 2;          /* skip over what we just grabbed */
2385
0
    }
2386
2387
    /* Use the bits in bit_value to see what's encoded and what is raw data */
2388
0
    if ( !(bit_mask & bit_value) )
2389
0
    {
2390
      /* bit not set - raw byte we just copy */
2391
2392
      /* If length would put us past end of output, avoid overflow */
2393
0
      CHECK_OUTPUT_LENGTH( 1 );
2394
0
      *(pout++) = *(pin++);
2395
0
    }
2396
0
    else
2397
0
    {
2398
      /* bit set - next item is encoded.  Peel off high nybble
2399
         of next byte to see the encoding type.  Set aside low
2400
         nybble while we are at it */
2401
0
      code_type = (unsigned int) ((*pin) >> 4 ) & 0xF;
2402
0
      code_low  = (unsigned int) ((*pin) & 0xF );
2403
0
      pin++;   /* increment over the code byte we just retrieved */
2404
2405
      /* Based on the code type, decode the compressed string */
2406
0
      switch ( code_type )
2407
0
      {
2408
0
      case 0  :   /* RLE short runs */
2409
        /*
2410
          Run length is the low nybble of the first code byte.
2411
          Byte to repeat immediately follows.
2412
          Total code size: 2 bytes.
2413
        */
2414
0
        length = code_low + 3;
2415
2416
        /* check the length and then, if it's OK,
2417
           generate the repeated series of bytes */
2418
0
        APPEND_RLE_BYTE( length );
2419
0
        break;
2420
0
      case 1  :   /* RLE long runs */
2421
        /*
2422
          Low 4 bits of run length is the low nybble of the
2423
          first code byte, upper 8 bits of run length is in
2424
          the next byte.
2425
          Byte to repeat immediately follows.
2426
          Total code size: 3 bytes.
2427
        */
2428
0
        CHECK_INPUT_POINTER( 1 );
2429
0
        length = code_low + ((unsigned int)(*pin++) << 4) + 19;
2430
2431
        /* check the length and then, if it's OK,
2432
           generate the repeated series of bytes */
2433
0
        APPEND_RLE_BYTE( length );
2434
0
        break;
2435
0
      case 2  :   /* LZ77 long strings */
2436
        /*
2437
          Low 4 bits of offset to string is the low nybble of the
2438
          first code byte, upper 8 bits of offset is in
2439
          the next byte.
2440
          Length of string immediately follows.
2441
          Total code size: 3 bytes.
2442
        */
2443
0
        FETCH_OFFSET_HIGH;
2444
2445
        /* get length from next byte, make sure it won't overrun buf */
2446
0
        CHECK_INPUT_POINTER( 1 );
2447
0
        length = (unsigned int)(*pin++) + 16;
2448
2449
        /* check the offset and length and then, if
2450
           they're OK, copy the data */
2451
0
        APPEND_LZW_STRING( offset, length );
2452
0
        break;
2453
0
      default :   /* (3 to 15): LZ77 short strings */
2454
        /*
2455
          Low 4 bits of offset to string is the low nybble of the
2456
          first code byte, upper 8 bits of offset is in
2457
          the next byte.
2458
          Length of string to repeat is overloaded into code_type.
2459
          Total code size: 2 bytes.
2460
        */
2461
0
        FETCH_OFFSET_HIGH;
2462
2463
        /* get length from code_type */
2464
0
        length = code_type;
2465
2466
        /* check the offset and length and then, if
2467
           they're OK, copy the data */
2468
0
        APPEND_LZW_STRING( offset, length );
2469
0
        break;
2470
0
      }
2471
0
    }
2472
0
  }
2473
2474
0
  return (int) ( pout - outbuf );  /* return length of expanded text */
2475
0
}
2476
2477
/*
2478
 * XXX - is there any guarantee that 65535 bytes is big enough to hold the
2479
 * uncompressed data from any blob?
2480
 */
2481
0
#define OUTBUF_SIZE 65536
2482
0
#define INBUF_SIZE  65536
2483
2484
/* Information about a compressed blob; we save the offset in the
2485
   underlying compressed file, and the offset in the uncompressed data
2486
   stream, of the blob. */
2487
typedef struct {
2488
  int64_t blob_comp_offset;
2489
  int64_t blob_uncomp_offset;
2490
} blob_info_t;
2491
2492
static bool
2493
ng_read_bytes_or_eof(wtap *wth, void *buffer, unsigned int nbytes, bool is_random,
2494
    int *err, char **err_info)
2495
0
{
2496
0
  ngsniffer_t *ngsniffer;
2497
0
  FILE_T infile;
2498
0
  ngsniffer_comp_stream_t *comp_stream;
2499
0
  unsigned char *outbuffer = (unsigned char *)buffer; /* where to write next decompressed data */
2500
0
  blob_info_t *blob;
2501
0
  unsigned int bytes_to_copy;
2502
0
  unsigned int bytes_left;
2503
2504
0
  ngsniffer = (ngsniffer_t *)wth->priv;
2505
0
  if (is_random) {
2506
0
    infile = wth->random_fh;
2507
0
    comp_stream = &ngsniffer->rand;
2508
0
  } else {
2509
0
    infile = wth->fh;
2510
0
    comp_stream = &ngsniffer->seq;
2511
0
  }
2512
2513
0
  if (!ngsniffer->is_compressed) {
2514
    /* Uncompressed - just read bytes */
2515
0
    if (!wtap_read_bytes_or_eof(infile, buffer, nbytes, err, err_info))
2516
0
      return false;
2517
0
    comp_stream->uncomp_offset += nbytes;
2518
0
    comp_stream->comp_offset += nbytes;
2519
0
    return true;
2520
0
  }
2521
2522
  /*
2523
   * Compressed.
2524
   *
2525
   * Allocate the stream buffer if it hasn't already been allocated.
2526
   */
2527
0
  if (comp_stream->buf == NULL) {
2528
0
    comp_stream->buf = (unsigned char *)g_malloc(OUTBUF_SIZE);
2529
2530
0
    if (is_random) {
2531
      /* This is the first read of the random file, so we're at
2532
         the beginning of the sequence of blobs in the file
2533
         (as we've not done any random reads yet to move the
2534
         current position in the random stream); set the
2535
         current blob to be the first blob. */
2536
0
      ngsniffer->current_blob = ngsniffer->first_blob;
2537
0
    } else {
2538
      /* This is the first sequential read; if we also have a
2539
         random stream open, allocate the first element for the
2540
         list of blobs, and make it the last element as well. */
2541
0
      if (wth->random_fh != NULL) {
2542
0
        ws_assert(ngsniffer->first_blob == NULL);
2543
0
        blob = g_new(blob_info_t,1);
2544
0
        blob->blob_comp_offset = comp_stream->comp_offset;
2545
0
        blob->blob_uncomp_offset = comp_stream->uncomp_offset;
2546
0
        ngsniffer->first_blob = g_list_append(ngsniffer->first_blob,
2547
0
                      blob);
2548
0
        ngsniffer->last_blob = ngsniffer->first_blob;
2549
0
      }
2550
0
    }
2551
2552
    /* Now read the first blob into the buffer. */
2553
0
    if (!read_blob(infile, comp_stream, err, err_info))
2554
0
      return false;
2555
0
  }
2556
0
  while (nbytes > 0) {
2557
0
    bytes_left = comp_stream->nbytes - comp_stream->nextout;
2558
0
    if (bytes_left == 0) {
2559
      /* There's no decompressed stuff left to copy from the current
2560
         blob; get the next blob. */
2561
2562
0
      if (is_random) {
2563
        /* Move to the next blob in the list. */
2564
0
        ngsniffer->current_blob = g_list_next(ngsniffer->current_blob);
2565
0
        if (!ngsniffer->current_blob) {
2566
          /*
2567
           * XXX - this "can't happen"; we should have a
2568
           * blob for every byte in the file.
2569
           */
2570
0
          *err = WTAP_ERR_CANT_SEEK;
2571
0
          return false;
2572
0
        }
2573
0
      } else {
2574
        /* If we also have a random stream open, add a new element,
2575
           for this blob, to the list of blobs; we know the list is
2576
           non-empty, as we initialized it on the first sequential
2577
           read, so we just add the new element at the end, and
2578
           adjust the pointer to the last element to refer to it. */
2579
0
        if (wth->random_fh != NULL) {
2580
0
          blob = g_new(blob_info_t,1);
2581
0
          blob->blob_comp_offset = comp_stream->comp_offset;
2582
0
          blob->blob_uncomp_offset = comp_stream->uncomp_offset;
2583
0
          ngsniffer->last_blob = g_list_append(ngsniffer->last_blob,
2584
0
                       blob);
2585
0
        }
2586
0
      }
2587
2588
0
      if (!read_blob(infile, comp_stream, err, err_info))
2589
0
        return false;
2590
0
      bytes_left = comp_stream->nbytes - comp_stream->nextout;
2591
0
    }
2592
2593
0
    bytes_to_copy = nbytes;
2594
0
    if (bytes_to_copy > bytes_left)
2595
0
      bytes_to_copy = bytes_left;
2596
0
    memcpy(outbuffer, &comp_stream->buf[comp_stream->nextout],
2597
0
           bytes_to_copy);
2598
0
    nbytes -= bytes_to_copy;
2599
0
    outbuffer += bytes_to_copy;
2600
0
    comp_stream->nextout += bytes_to_copy;
2601
0
    comp_stream->uncomp_offset += bytes_to_copy;
2602
0
  }
2603
0
  return true;
2604
0
}
2605
2606
static bool
2607
ng_read_bytes(wtap *wth, void *buffer, unsigned int nbytes, bool is_random,
2608
    int *err, char **err_info)
2609
0
{
2610
0
  if (!ng_read_bytes_or_eof(wth, buffer, nbytes, is_random, err, err_info)) {
2611
    /*
2612
     * In this case, even reading zero bytes, because we're at
2613
     * the end of the file, is a short read.
2614
     */
2615
0
    if (*err == 0)
2616
0
      *err = WTAP_ERR_SHORT_READ;
2617
0
    return false;
2618
0
  }
2619
0
  return true;
2620
0
}
2621
2622
/* Read a blob from a compressed stream.
2623
   Return false and set "*err" and "*err_info" on error, otherwise return true. */
2624
static bool
2625
read_blob(FILE_T infile, ngsniffer_comp_stream_t *comp_stream, int *err,
2626
    char **err_info)
2627
0
{
2628
0
  int in_len;
2629
0
  unsigned short blob_len;
2630
0
  int16_t blob_len_host;
2631
0
  bool uncompressed;
2632
0
  unsigned char *file_inbuf;
2633
0
  int out_len;
2634
2635
  /* Read one 16-bit word which is length of next compressed blob */
2636
0
  if (!wtap_read_bytes_or_eof(infile, &blob_len, 2, err, err_info))
2637
0
    return false;
2638
0
  comp_stream->comp_offset += 2;
2639
0
  blob_len_host = pletoh16(&blob_len);
2640
2641
  /* Compressed or uncompressed? */
2642
0
  if (blob_len_host < 0) {
2643
    /* Uncompressed blob; blob length is absolute value of the number. */
2644
0
    in_len = -blob_len_host;
2645
0
    uncompressed = true;
2646
0
  } else {
2647
0
    in_len = blob_len_host;
2648
0
    uncompressed = false;
2649
0
  }
2650
2651
0
  file_inbuf = (unsigned char *)g_malloc(INBUF_SIZE);
2652
2653
  /* Read the blob */
2654
0
  if (!wtap_read_bytes(infile, file_inbuf, in_len, err, err_info)) {
2655
0
    g_free(file_inbuf);
2656
0
    return false;
2657
0
  }
2658
0
  comp_stream->comp_offset += in_len;
2659
2660
0
  if (uncompressed) {
2661
0
    memcpy(comp_stream->buf, file_inbuf, in_len);
2662
0
    out_len = in_len;
2663
0
  } else {
2664
    /* Decompress the blob */
2665
0
    out_len = SnifferDecompress(file_inbuf, in_len,
2666
0
              comp_stream->buf, OUTBUF_SIZE, err,
2667
0
              err_info);
2668
0
    if (out_len < 0) {
2669
0
      g_free(file_inbuf);
2670
0
      return false;
2671
0
    }
2672
0
  }
2673
2674
0
  g_free(file_inbuf);
2675
0
  comp_stream->nextout = 0;
2676
0
  comp_stream->nbytes = out_len;
2677
0
  return true;
2678
0
}
2679
2680
/* Skip some number of bytes forward in the sequential stream. */
2681
static bool
2682
ng_skip_bytes_seq(wtap *wth, unsigned int count, int *err, char **err_info)
2683
0
{
2684
0
  ngsniffer_t *ngsniffer;
2685
0
  char *buf;
2686
0
  unsigned int amount_to_read;
2687
2688
0
  ngsniffer = (ngsniffer_t *)wth->priv;
2689
2690
0
  if (!ngsniffer->is_compressed) {
2691
    /* Uncompressed - just read forward and discard data */
2692
0
    ngsniffer->seq.uncomp_offset += count;
2693
0
    return wtap_read_bytes(wth->fh, NULL, count, err, err_info);
2694
0
  }
2695
2696
  /*
2697
   * Compressed.
2698
   *
2699
   * Now read and discard "count" bytes.
2700
   */
2701
0
  buf = (char *)g_malloc(INBUF_SIZE);
2702
0
  while (count != 0) {
2703
0
    if (count > INBUF_SIZE)
2704
0
      amount_to_read = INBUF_SIZE;
2705
0
    else
2706
0
      amount_to_read = count;
2707
2708
0
    if (!ng_read_bytes(wth, buf, amount_to_read, false, err, err_info)) {
2709
0
      g_free(buf);
2710
0
      return false; /* error */
2711
0
    }
2712
2713
0
    count -= amount_to_read;
2714
0
  }
2715
2716
0
  g_free(buf);
2717
0
  return true;
2718
0
}
2719
2720
/* Seek to a given offset in the random data stream.
2721
2722
   On compressed files, we see whether we're seeking to a position within
2723
   the blob we currently have in memory and, if not, we find in the list
2724
   of blobs the last blob that starts at or before the position to which
2725
   we're seeking, and read that blob in.  We can then move to the appropriate
2726
   position within the blob we have in memory (whether it's the blob we
2727
   already had in memory or, if necessary, the one we read in). */
2728
static bool
2729
ng_file_seek_rand(wtap *wth, int64_t offset, int *err, char **err_info)
2730
0
{
2731
0
  ngsniffer_t *ngsniffer;
2732
0
  int64_t delta;
2733
0
  GList *new_list, *next_list;
2734
0
  blob_info_t *next_blob, *new_blob;
2735
2736
0
  ngsniffer = (ngsniffer_t *)wth->priv;
2737
2738
0
  if (!ngsniffer->is_compressed) {
2739
    /* Uncompressed - just seek. */
2740
0
    if (file_seek(wth->random_fh, offset, SEEK_SET, err) == -1)
2741
0
      return false;
2742
0
    return true;
2743
0
  }
2744
2745
  /*
2746
   * Compressed.
2747
   *
2748
   * How many *uncompressed* should we move forward or
2749
   * backward?
2750
   */
2751
0
  delta = offset - ngsniffer->rand.uncomp_offset;
2752
2753
  /* Is the place to which we're seeking within the current buffer, or
2754
     will we have to read a different blob into the buffer? */
2755
0
  new_list = NULL;
2756
0
  if (delta > 0) {
2757
    /* We're going forwards.
2758
       Is the place to which we're seeking within the current buffer? */
2759
0
    if ((size_t)(ngsniffer->rand.nextout + delta) >= ngsniffer->rand.nbytes) {
2760
      /* No.  Search for a blob that contains the target
2761
         offset in the uncompressed byte stream. */
2762
0
      if (ngsniffer->current_blob == NULL) {
2763
        /* We haven't read anything from the random
2764
           file yet, so we have no current blob;
2765
           search all the blobs, starting with
2766
           the first one. */
2767
0
        new_list = ngsniffer->first_blob;
2768
0
      } else {
2769
        /* We're seeking forward, so start searching
2770
           with the blob after the current one. */
2771
0
        new_list = g_list_next(ngsniffer->current_blob);
2772
0
      }
2773
0
      while (new_list) {
2774
0
        next_list = g_list_next(new_list);
2775
0
        if (next_list == NULL) {
2776
          /* No more blobs; the current one is it. */
2777
0
          break;
2778
0
        }
2779
2780
0
        next_blob = (blob_info_t *)next_list->data;
2781
        /* Does the next blob start after the target offset?
2782
           If so, the current blob is the one we want. */
2783
0
        if (next_blob->blob_uncomp_offset > offset)
2784
0
          break;
2785
2786
0
        new_list = next_list;
2787
0
      }
2788
0
      if (new_list == NULL) {
2789
        /*
2790
         * We're seeking past the end of what
2791
         * we've read so far.
2792
         */
2793
0
        *err = WTAP_ERR_CANT_SEEK;
2794
0
        return false;
2795
0
      }
2796
0
    }
2797
0
  } else if (delta < 0) {
2798
    /* We're going backwards.
2799
       Is the place to which we're seeking within the current buffer? */
2800
0
    if (ngsniffer->rand.nextout + delta < 0) {
2801
      /* No.  Search for a blob that contains the target
2802
         offset in the uncompressed byte stream. */
2803
0
      if (ngsniffer->current_blob == NULL) {
2804
        /* We haven't read anything from the random
2805
           file yet, so we have no current blob;
2806
           search all the blobs, starting with
2807
           the last one. */
2808
0
        new_list = ngsniffer->last_blob;
2809
0
      } else {
2810
        /* We're seeking backward, so start searching
2811
           with the blob before the current one. */
2812
0
        new_list = g_list_previous(ngsniffer->current_blob);
2813
0
      }
2814
0
      while (new_list) {
2815
        /* Does this blob start at or before the target offset?
2816
           If so, the current blob is the one we want. */
2817
0
        new_blob = (blob_info_t *)new_list->data;
2818
0
        if (new_blob->blob_uncomp_offset <= offset)
2819
0
          break;
2820
2821
        /* It doesn't - skip to the previous blob. */
2822
0
        new_list = g_list_previous(new_list);
2823
0
      }
2824
0
      if (new_list == NULL) {
2825
        /*
2826
         * XXX - shouldn't happen.
2827
         */
2828
0
        *err = WTAP_ERR_CANT_SEEK;
2829
0
        return false;
2830
0
      }
2831
0
    }
2832
0
  }
2833
2834
0
  if (new_list != NULL) {
2835
    /* The place to which we're seeking isn't in the current buffer;
2836
       move to a new blob. */
2837
0
    new_blob = (blob_info_t *)new_list->data;
2838
2839
    /* Seek in the compressed file to the offset in the compressed file
2840
       of the beginning of that blob. */
2841
0
    if (file_seek(wth->random_fh, new_blob->blob_comp_offset, SEEK_SET, err) == -1)
2842
0
      return false;
2843
2844
    /*
2845
     * Do we have a buffer for the random stream yet?
2846
     */
2847
0
    if (ngsniffer->rand.buf == NULL) {
2848
      /*
2849
       * No - allocate it, as we'll be reading into it.
2850
       */
2851
0
      ngsniffer->rand.buf = (unsigned char *)g_malloc(OUTBUF_SIZE);
2852
0
    }
2853
2854
    /* Make the blob we found the current one. */
2855
0
    ngsniffer->current_blob = new_list;
2856
2857
    /* Now set the current offsets to the offsets of the beginning
2858
       of the blob. */
2859
0
    ngsniffer->rand.uncomp_offset = new_blob->blob_uncomp_offset;
2860
0
    ngsniffer->rand.comp_offset = new_blob->blob_comp_offset;
2861
2862
    /* Now fill the buffer. */
2863
0
    if (!read_blob(wth->random_fh, &ngsniffer->rand, err, err_info))
2864
0
      return false;
2865
2866
    /* Set "delta" to the amount to move within this blob; it had
2867
       better be >= 0, and < the amount of uncompressed data in
2868
       the blob, as otherwise it'd mean we need to seek before
2869
       the beginning or after the end of this blob. */
2870
0
    delta = offset - ngsniffer->rand.uncomp_offset;
2871
0
    ws_assert(delta >= 0 && (unsigned long)delta < ngsniffer->rand.nbytes);
2872
0
  }
2873
2874
  /* OK, the place to which we're seeking is in the buffer; adjust
2875
     "ngsniffer->rand.nextout" to point to the place to which
2876
     we're seeking, and adjust "ngsniffer->rand.uncomp_offset" to be
2877
     the destination offset. */
2878
0
  ngsniffer->rand.nextout += (int) delta;
2879
0
  ngsniffer->rand.uncomp_offset += delta;
2880
2881
0
  return true;
2882
0
}
2883
2884
static const struct supported_block_type ngsniffer_uncompressed_blocks_supported[] = {
2885
  /*
2886
   * We support packet blocks, with no comments or other options.
2887
   */
2888
  { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
2889
};
2890
2891
static const struct file_type_subtype_info ngsniffer_uncompressed_info = {
2892
  "Sniffer (DOS)", "ngsniffer", "cap", "enc;trc;fdc;syc",
2893
  false, BLOCKS_SUPPORTED(ngsniffer_uncompressed_blocks_supported),
2894
  ngsniffer_dump_can_write_encap, ngsniffer_dump_open, NULL
2895
};
2896
2897
static const struct supported_block_type ngsniffer_compressed_blocks_supported[] = {
2898
  /*
2899
   * We support packet blocks, with no comments or other options.
2900
   */
2901
  { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
2902
};
2903
2904
static const struct file_type_subtype_info ngsniffer_compressed_info = {
2905
  "Sniffer (DOS), compressed", "ngsniffer_comp", "cap", "enc;trc;fdc;syc",
2906
  false, BLOCKS_SUPPORTED(ngsniffer_compressed_blocks_supported),
2907
  NULL, NULL, NULL
2908
};
2909
2910
void register_ngsniffer(void)
2911
14
{
2912
14
  ngsniffer_uncompressed_file_type_subtype = wtap_register_file_type_subtype(&ngsniffer_uncompressed_info);
2913
14
  ngsniffer_compressed_file_type_subtype = wtap_register_file_type_subtype(&ngsniffer_compressed_info);
2914
2915
  /*
2916
   * Register names for backwards compatibility with the
2917
   * wtap_filetypes table in Lua.
2918
   */
2919
14
  wtap_register_backwards_compatibility_lua_name("NGSNIFFER_UNCOMPRESSED",
2920
14
      ngsniffer_uncompressed_file_type_subtype);
2921
14
  wtap_register_backwards_compatibility_lua_name("NGSNIFFER_COMPRESSED",
2922
14
      ngsniffer_compressed_file_type_subtype);
2923
14
}
2924
2925
/*
2926
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
2927
 *
2928
 * Local variables:
2929
 * c-basic-offset: 8
2930
 * tab-width: 8
2931
 * indent-tabs-mode: t
2932
 * End:
2933
 *
2934
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2935
 * :indentSize=8:tabSize=8:noTabs=false:
2936
 */