forked from hauke/wireshark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngsniffer.c
2784 lines (2486 loc) · 83.9 KB
/
ngsniffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ngsniffer.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* The code in ngsniffer.c that decodes the time fields for each packet in the
* Sniffer trace originally came from code from TCPVIEW:
*
* TCPVIEW
*
* Author: Martin Hunt
* Networks and Distributed Computing
* Computing & Communications
* University of Washington
* Administration Building, AG-44
* Seattle, WA 98195
* Internet: [email protected]
*
*
* Copyright 1992 by the University of Washington
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appears in all copies and that both the
* above copyright notice and this permission notice appear in supporting
* documentation, and that the name of the University of Washington not be
* used in advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. This software is made
* available "as is", and
* THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
* WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
* NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "ngsniffer.h"
/* Magic number in Sniffer files. */
static const char ngsniffer_magic[] = {
'T', 'R', 'S', 'N', 'I', 'F', 'F', ' ', 'd', 'a', 't', 'a',
' ', ' ', ' ', ' ', 0x1a
};
/*
* Sniffer record types.
*/
#define REC_VERS 1 /* Version record (f_vers) */
#define REC_FRAME2 4 /* Frame data (f_frame2) */
#define REC_FRAME4 8 /* Frame data (f_frame4) */
#define REC_FRAME6 12 /* Frame data (f_frame6) (see below) */
#define REC_EOF 3 /* End-of-file record (no data follows) */
/*
* and now for some unknown header types
*/
#define REC_HEADER1 6 /* Header containing various information,
* not yet reverse engineered - some binary,
* some strings (Serial numbers? Names
* under which the software is registered?
* Software version numbers? Mysterious
* strings such as "PA-55X" and "PA-30X"
* and "PA-57X" and "PA-11X"?), some strings
* that are partially overwritten
* ("UNSERIALIZED", "Network General
* Corporation"), differing from major
* version to major version */
#define REC_HEADER2 7 /* Header containing ??? */
#define REC_V2DESC 8 /* In version 2 sniffer traces contains
* info about this capturing session,
* in the form of a multi-line string
* with NL as the line separator.
* Collides with REC_FRAME4 */
#define REC_HEADER3 13 /* Retransmission counts? */
#define REC_HEADER4 14 /* ? */
#define REC_HEADER5 15 /* ? */
#define REC_HEADER6 16 /* More broadcast/retransmission counts? */
#define REC_HEADER7 17 /* ? */
/*
* Sniffer version record format.
*/
struct vers_rec {
gint16 maj_vers; /* major version number */
gint16 min_vers; /* minor version number */
gint16 time; /* DOS-format time */
gint16 date; /* DOS-format date */
gint8 type; /* what type of records follow */
guint8 network; /* network type */
gint8 format; /* format version */
guint8 timeunit; /* timestamp units */
gint8 cmprs_vers; /* compression version */
gint8 cmprs_level; /* compression level */
gint16 rsvd[2]; /* reserved */
};
/*
* Network types.
*/
#define NETWORK_TRING 0 /* Token ring */
#define NETWORK_ENET 1 /* Ethernet */
#define NETWORK_ARCNET 2 /* ARCNET */
#define NETWORK_STARLAN 3 /* StarLAN */
#define NETWORK_PCNW 4 /* PC Network broadband (Sytek?) */
#define NETWORK_LOCALTALK 5 /* LocalTalk */
#define NETWORK_SYNCHRO 7 /* Internetwork analyzer (synchronous) */
#define NETWORK_ASYNC 8 /* Internetwork analyzer (asynchronous) */
#define NETWORK_FDDI 9 /* FDDI */
#define NETWORK_ATM 10 /* ATM */
/*
* Sniffer type 2 data record format - followed by frame data.
*
* The Expert Sniffer Network Analyzer Operations manual, Release 5.50,
* documents some of the values used in "fs" and "flags". "flags" don't
* look as if they'd be of much interest to us, as those are internal
* flags for state used by the Sniffer, but "fs" gives various status
* bits including error indications *and*:
*
* ISDN channel information for ISDN;
*
* PPP vs. SLIP information for Async.
*
* In that section it also refers to "FDDI analyzers using the NPI PCI
* FDDI adapter" and "FDDI analyzers using the NPI ISA FDDI adapter",
* referring to the first as "F1SNIFF" and the second as "FDSNIFF";
* those sound as if they *could* be replacements for "TRSNIFF" in
* the file header, but that manual says, earlier, that the header
* starts with "TRSNIFF data, no matter where the frames were
* collected".
*
* It also says that a type 2 record has an 8-bit "time_high"
* and an 8-bit "time_day" field; the code here used to have a
* 16-bit "time_high" value, but that gave wrong time stamps on at
* least some captures. Did some older manual have it as a 16-bit
* "tstamp_high", so that perhaps it depends on the version number
* in the file, or is it "tstamp_high" plus "tstamp_day" in all
* versions? (I forget whether this came purely from tcpview, or if
* I saw any of it in an NAI document.)
*
* We interpret them as unsigned, as interpreting them as signed
* would appear to allow time stamps that precede the start of the
* capture. The description of the record format shows them as
* "char", but the section "How the Analyzer Stores Time" shows a
* time stamp structure with those fields being "unsigned char".
*
* In addition, the description of the record format has the comment
* for the "time_day" field saying it's the time in days since the
* start of the capture, but the "How the Analyzer Stores Time"
* section says it's increased by 1 if the capture continues past
* midnight - and also says that the time stamp structure has a time
* relative to midnight when the capture started, not since the
* actual capture start, so that might be a difference between
* the internal time stamp in the Sniffer software and the time
* stamp in capture files (i.e., the latter might be relative to
* the time when the capture starts).
*/
struct frame2_rec {
guint16 time_low; /* low part of time stamp */
guint16 time_med; /* middle part of time stamp */
guint8 time_high; /* high part of the time stamp */
guint8 time_day; /* time in days since start of capture */
gint16 size; /* number of bytes of data */
guint8 fs; /* frame error status bits */
guint8 flags; /* buffer flags */
gint16 true_size; /* size of original frame, in bytes */
gint16 rsvd; /* reserved */
};
/*
* Bits in "fs".
*
* The bits differ for different link-layer types.
*/
/*
* Ethernet.
*/
#define FS_ETH_CRC 0x80 /* CRC error */
#define FS_ETH_ALIGN 0x40 /* bad alignment */
#define FS_ETH_RU 0x20 /* "RU out of resources" */
#define FS_ETH_OVERRUN 0x10 /* DMA overrun */
#define FS_ETH_RUNT 0x08 /* frame too small */
#define FS_ETH_COLLISION 0x02 /* collision fragment */
/*
* FDDI.
*/
#define FS_FDDI_INVALID 0x10 /* frame indicators are invalid */
#define FS_FDDI_ERROR 0x20 /* "frame error bit 1" */
#define FS_FDDI_PCI_VDL 0x01 /* VDL error on frame on PCI adapter */
#define FS_FDDI_PCI_CRC 0x02 /* CRC error on frame on PCI adapter */
#define FS_FDDI_ISA_CRC 0x20 /* CRC error on frame on ISA adapter */
/*
* Internetwork analyzer (synchronous and asynchronous).
*/
#define FS_WAN_DTE 0x80 /* DTE->DCE frame */
/*
* Internetwork analyzer (synchronous).
*/
#define FS_SYNC_LOST 0x01 /* some frames were lost */
#define FS_SYNC_CRC 0x02 /* CRC error */
#define FS_SYNC_ABORT 0x04 /* aborted frame */
#define FS_ISDN_CHAN_MASK 0x18 /* ISDN channel */
#define FS_ISDN_CHAN_D 0x18 /* ISDN channel D */
#define FS_ISDN_CHAN_B1 0x08 /* ISDN channel B1 */
#define FS_ISDN_CHAN_B2 0x10 /* ISDN channel B2 */
/*
* Internetwork analyzer (asynchronous).
* XXX - are some of these synchronous flags? They're listed with the
* asynchronous flags in the Sniffer 5.50 Network Analyzer Operations
* manual. Is one of the "overrun" errors a synchronous overrun error?
*/
#define FS_ASYNC_LOST 0x01 /* some frames were lost */
#define FS_ASYNC_OVERRUN 0x02 /* UART overrun, lost bytes */
#define FS_ASYNC_FRAMING 0x04 /* bad character (framing error?) */
#define FS_ASYNC_PPP 0x08 /* PPP frame */
#define FS_ASYNC_SLIP 0x10 /* SLIP frame */
#define FS_ASYNC_ALIGN 0x20 /* alignment or DLPP(?) error */
#define FS_ASYNC_OVERRUN2 0x40 /* overrun or bad frame length */
/*
* Sniffer type 4 data record format - followed by frame data.
*
* The ATM Sniffer manual says that the "flags" field holds "buffer flags;
* BF_xxxx", but doesn't say what the BF_xxxx flags are. They may
* be the same as they are in a type 2 record, in which case they're
* probably not of much interest to us.
*
* XXX - the manual also says there's an 8-byte "ATMTimeStamp" driver
* time stamp at the end of "ATMSaveInfo", but, from an ATM Sniffer capture
* file I've looked at, that appears not to be the case.
*/
/*
* Fields from the AAL5 trailer for the frame, if it's an AAL5 frame
* rather than a cell.
*/
typedef struct _ATM_AAL5Trailer {
guint16 aal5t_u2u; /* user-to-user indicator */
guint16 aal5t_len; /* length of the packet */
guint32 aal5t_chksum; /* checksum for AAL5 packet */
} ATM_AAL5Trailer;
typedef struct _ATMTimeStamp {
guint32 msw; /* most significant word */
guint32 lsw; /* least significant word */
} ATMTimeStamp;
typedef struct _ATMSaveInfo {
guint32 StatusWord; /* status word from driver */
ATM_AAL5Trailer Trailer; /* AAL5 trailer */
guint8 AppTrafType; /* traffic type */
guint8 AppHLType; /* protocol type */
guint16 AppReserved; /* reserved */
guint16 Vpi; /* virtual path identifier */
guint16 Vci; /* virtual circuit identifier */
guint16 channel; /* link: 0 for DCE, 1 for DTE */
guint16 cells; /* number of cells */
guint32 AppVal1; /* type-dependent */
guint32 AppVal2; /* type-dependent */
} ATMSaveInfo;
/*
* Bits in StatusWord.
*/
#define SW_ERRMASK 0x0F /* Error mask: */
#define SW_RX_FIFO_UNDERRUN 0x01 /* Receive FIFO underrun */
#define SW_RX_FIFO_OVERRUN 0x02 /* Receive FIFO overrun */
#define SW_RX_PKT_TOO_LONG 0x03 /* Received packet > max size */
#define SW_CRC_ERROR 0x04 /* CRC error */
#define SW_USER_ABORTED_RX 0x05 /* User aborted receive */
#define SW_BUF_LEN_TOO_LONG 0x06 /* buffer len > max buf */
#define SW_INTERNAL_T1_ERROR 0x07 /* Internal T1 error */
#define SW_RX_CHANNEL_DEACTIV8 0x08 /* Rx channel deactivate */
#define SW_ERROR 0x80 /* Error indicator */
#define SW_CONGESTION 0x40 /* Congestion indicator */
#define SW_CLP 0x20 /* Cell loss priority indicator */
#define SW_RAW_CELL 0x100 /* RAW cell indicator */
#define SW_OAM_CELL 0x200 /* OAM cell indicator */
/*
* Bits in AppTrafType.
*
* For AAL types other than AAL5, the packet data is presumably for a
* single cell, not a reassembled frame, as the ATM Sniffer manual says
* it dosn't reassemble cells other than AAL5 cells.
*/
#define ATT_AALTYPE 0x0F /* AAL type: */
#define ATT_AAL_UNKNOWN 0x00 /* Unknown AAL */
#define ATT_AAL1 0x01 /* AAL1 */
#define ATT_AAL3_4 0x02 /* AAL3/4 */
#define ATT_AAL5 0x03 /* AAL5 */
#define ATT_AAL_USER 0x04 /* User AAL */
#define ATT_AAL_SIGNALLING 0x05 /* Signaling AAL */
#define ATT_OAMCELL 0x06 /* OAM cell */
#define ATT_HLTYPE 0xF0 /* Higher-layer type: */
#define ATT_HL_UNKNOWN 0x00 /* unknown */
#define ATT_HL_LLCMX 0x10 /* LLC multiplexed (probably RFC 1483) */
#define ATT_HL_VCMX 0x20 /* VC multiplexed (probably RFC 1483) */
#define ATT_HL_LANE 0x30 /* LAN Emulation */
#define ATT_HL_ILMI 0x40 /* ILMI */
#define ATT_HL_FRMR 0x50 /* Frame Relay */
#define ATT_HL_SPANS 0x60 /* FORE SPANS */
#define ATT_HL_IPSILON 0x70 /* Ipsilon */
/*
* Values for AppHLType; the interpretation depends on the ATT_HLTYPE
* bits in AppTrafType.
*/
#define AHLT_UNKNOWN 0x0
#define AHLT_VCMX_802_3_FCS 0x1 /* VCMX: 802.3 FCS */
#define AHLT_LANE_LE_CTRL 0x1 /* LANE: LE Ctrl */
#define AHLT_IPSILON_FT0 0x1 /* Ipsilon: Flow Type 0 */
#define AHLT_VCMX_802_4_FCS 0x2 /* VCMX: 802.4 FCS */
#define AHLT_LANE_802_3 0x2 /* LANE: 802.3 */
#define AHLT_IPSILON_FT1 0x2 /* Ipsilon: Flow Type 1 */
#define AHLT_VCMX_802_5_FCS 0x3 /* VCMX: 802.5 FCS */
#define AHLT_LANE_802_5 0x3 /* LANE: 802.5 */
#define AHLT_IPSILON_FT2 0x3 /* Ipsilon: Flow Type 2 */
#define AHLT_VCMX_FDDI_FCS 0x4 /* VCMX: FDDI FCS */
#define AHLT_LANE_802_3_MC 0x4 /* LANE: 802.3 multicast */
#define AHLT_VCMX_802_6_FCS 0x5 /* VCMX: 802.6 FCS */
#define AHLT_LANE_802_5_MC 0x5 /* LANE: 802.5 multicast */
#define AHLT_VCMX_802_3 0x7 /* VCMX: 802.3 */
#define AHLT_VCMX_802_4 0x8 /* VCMX: 802.4 */
#define AHLT_VCMX_802_5 0x9 /* VCMX: 802.5 */
#define AHLT_VCMX_FDDI 0xa /* VCMX: FDDI */
#define AHLT_VCMX_802_6 0xb /* VCMX: 802.6 */
#define AHLT_VCMX_FRAGMENTS 0xc /* VCMX: Fragments */
#define AHLT_VCMX_BPDU 0xe /* VCMX: BPDU */
struct frame4_rec {
guint16 time_low; /* low part of time stamp */
guint16 time_med; /* middle part of time stamp */
guint8 time_high; /* high part of time stamp */
guint8 time_day; /* time in days since start of capture */
gint16 size; /* number of bytes of data */
gint8 fs; /* frame error status bits */
gint8 flags; /* buffer flags */
gint16 true_size; /* size of original frame, in bytes */
gint16 rsvd3; /* reserved */
gint16 atm_pad; /* pad to 4-byte boundary */
ATMSaveInfo atm_info; /* ATM-specific stuff */
};
/*
* XXX - I have a version 5.50 file with a bunch of token ring
* records listed as type "12". The record format below was
* derived from frame4_rec and a bit of experimentation.
* - Gerald
*/
struct frame6_rec {
guint16 time_low; /* low part of time stamp */
guint16 time_med; /* middle part of time stamp */
guint8 time_high; /* high part of time stamp */
guint8 time_day; /* time in days since start of capture */
gint16 size; /* number of bytes of data */
guint8 fs; /* frame error status bits */
guint8 flags; /* buffer flags */
gint16 true_size; /* size of original frame, in bytes */
guint8 chemical_x[22]; /* ? */
};
/*
* Network type values in some type 7 records.
*
* Captures with a major version number of 2 appear to have type 7
* records with text in them (at least one I have does).
*
* Captures with a major version of 4, and at least some captures with
* a major version of 5, have type 7 records with those values in the
* 5th byte.
*
* However, some captures with a major version number of 5 appear not to
* have type 7 records at all (at least one I have doesn't), but do appear
* to put non-zero values in the "rsvd" field of the version header (at
* least one I have does) - at least some other captures with smaller version
* numbers appear to put 0 there, so *maybe* that's where the network
* (sub)type is hidden in those captures. The version 5 captures I've seen
* that *do* have type 7 records put 0 there, so it's not as if *all* V5
* captures have something in the "rsvd" field, however.
*
* The semantics of these network types is inferred from the Sniffer
* documentation, as they correspond to types described in the UI;
* in particular, see
*
* http://www.mcafee.com/common/media/sniffer/support/sdos/operation.pdf
*
* starting at page 3-10 (56 of 496).
*
* XXX - I've seen X.25 captures with NET_ROUTER, and I've seen bridge/
* router captures with NET_HDLC. Sigh.... Are those just captures for
* which the user set the wrong network type when capturing?
*/
#define NET_SDLC 0 /* Probably "SDLC then SNA" */
#define NET_HDLC 1 /* Used for X.25; is it used for other
things as well, or is it "HDLC then
X.25", as referred to by the document
cited above, and only used for X.25? */
#define NET_FRAME_RELAY 2
#define NET_ROUTER 3 /* Probably "Router/Bridge", for various
point-to-point protocols for use between
bridges and routers, including PPP as well
as various proprietary protocols; also
used for ISDN, for reasons not obvious
to me, given that a Sniffer knows
whether it's using a WAN or an ISDN pod */
#define NET_PPP 4 /* "Asynchronous", which includes SLIP too */
#define NET_SMDS 5 /* Not mentioned in the document, but
that's a document for version 5.50 of
the Sniffer, and that version might use
version 5 in the file format and thus
might not be using type 7 records */
/*
* Values for V.timeunit, in picoseconds, so that they can be represented
* as integers. These values must be < 2^(64-40); see below.
*
* XXX - at least some captures with a V.timeunit value of 2 show
* packets with time stamps in 2011 if the time stamp is interpreted
* to be in units of 15 microseconds. The capture predates 2008,
* so that interpretation is probably wrong. Perhaps the interpretation
* of V.timeunit depends on the version number of the file?
*/
static const guint32 Psec[] = {
15000000, /* 15.0 usecs = 15000000 psecs */
838096, /* .838096 usecs = 838096 psecs */
15000000, /* 15.0 usecs = 15000000 psecs */
500000, /* 0.5 usecs = 500000 psecs */
2000000, /* 2.0 usecs = 2000000 psecs */
1000000, /* 1.0 usecs = 1000000 psecs */
/* XXX - Sniffer doc says 0.08 usecs = 80000 psecs */
100000 /* 0.1 usecs = 100000 psecs */
};
#define NUM_NGSNIFF_TIMEUNITS (sizeof Psec / sizeof Psec[0])
/* Information for a compressed Sniffer data stream. */
typedef struct {
unsigned char *buf; /* buffer into which we uncompress data */
unsigned int nbytes; /* number of bytes of data in that buffer */
int nextout; /* offset in that buffer of stream's current position */
gint64 comp_offset; /* current offset in compressed data stream */
gint64 uncomp_offset; /* current offset in uncompressed data stream */
} ngsniffer_comp_stream_t;
typedef struct {
guint maj_vers;
guint min_vers;
guint32 timeunit;
time_t start;
guint network; /* network type */
ngsniffer_comp_stream_t seq; /* sequential access */
ngsniffer_comp_stream_t rand; /* random access */
GList *first_blob; /* list element for first blob */
GList *last_blob; /* list element for last blob */
GList *current_blob; /* list element for current blob */
} ngsniffer_t;
/*
* DOS date to "struct tm" conversion values.
*/
/* DOS year = upper 7 bits */
#define DOS_YEAR_OFFSET (1980-1900) /* tm_year = year+1900, DOS date year year+1980 */
#define DOS_YEAR_SHIFT 9
#define DOS_YEAR_MASK (0x7F<<DOS_YEAR_SHIFT)
/* DOS month = next 4 bits */
#define DOS_MONTH_OFFSET (-1) /* tm_mon = month #-1, DOS date month = month # */
#define DOS_MONTH_SHIFT 5
#define DOS_MONTH_MASK (0x0F<<DOS_MONTH_SHIFT)
/* DOS day = next 5 bits */
#define DOS_DAY_SHIFT 0
#define DOS_DAY_MASK (0x1F<<DOS_DAY_SHIFT)
static int process_header_records(wtap *wth, int *err, gchar **err_info,
gint16 maj_vers, guint8 network);
static int process_rec_header2_v2(wtap *wth, unsigned char *buffer,
guint16 length, int *err, gchar **err_info);
static int process_rec_header2_v145(wtap *wth, unsigned char *buffer,
guint16 length, gint16 maj_vers, int *err, gchar **err_info);
static gboolean ngsniffer_read(wtap *wth, int *err, gchar **err_info,
gint64 *data_offset);
static gboolean ngsniffer_seek_read(wtap *wth, gint64 seek_off,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
static int ngsniffer_process_record(wtap *wth, gboolean is_random,
guint *padding, struct wtap_pkthdr *phdr, Buffer *buf, int *err,
gchar **err_info);
static void set_pseudo_header_frame2(wtap *wth,
union wtap_pseudo_header *pseudo_header, struct frame2_rec *frame2);
static void set_pseudo_header_frame4(union wtap_pseudo_header *pseudo_header,
struct frame4_rec *frame4);
static void set_pseudo_header_frame6(wtap *wth,
union wtap_pseudo_header *pseudo_header, struct frame6_rec *frame6);
static int infer_pkt_encap(const guint8 *pd, int len);
static int fix_pseudo_header(int encap, Buffer *buf, int len,
union wtap_pseudo_header *pseudo_header);
static void ngsniffer_sequential_close(wtap *wth);
static void ngsniffer_close(wtap *wth);
static gboolean ngsniffer_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
const guint8 *pd, int *err, gchar **err_info);
static gboolean ngsniffer_dump_finish(wtap_dumper *wdh, int *err);
static int SnifferDecompress( unsigned char * inbuf, size_t inlen,
unsigned char * outbuf, size_t outlen, int *err, gchar **err_info );
static gboolean ng_read_bytes_or_eof(wtap *wth, void *buffer,
unsigned int nbytes, gboolean is_random, int *err, gchar **err_info);
static gboolean ng_read_bytes(wtap *wth, void *buffer, unsigned int nbytes,
gboolean is_random, int *err, gchar **err_info);
static gboolean read_blob(FILE_T infile, ngsniffer_comp_stream_t *comp_stream,
int *err, gchar **err_info);
static gboolean ng_file_skip_seq(wtap *wth, gint64 delta, int *err,
gchar **err_info);
static gboolean ng_file_seek_rand(wtap *wth, gint64 offset, int *err,
gchar **err_info);
wtap_open_return_val
ngsniffer_open(wtap *wth, int *err, gchar **err_info)
{
char magic[sizeof ngsniffer_magic];
char record_type[2];
char record_length[4]; /* only the first 2 bytes are length,
the last 2 are "reserved" and are thrown away */
guint16 type;
struct vers_rec version;
guint16 maj_vers;
guint16 start_date;
#if 0
guint16 start_time;
#endif
static const int sniffer_encap[] = {
WTAP_ENCAP_TOKEN_RING,
WTAP_ENCAP_ETHERNET,
WTAP_ENCAP_ARCNET,
WTAP_ENCAP_UNKNOWN, /* StarLAN */
WTAP_ENCAP_UNKNOWN, /* PC Network broadband */
WTAP_ENCAP_UNKNOWN, /* LocalTalk */
WTAP_ENCAP_UNKNOWN, /* Znet */
WTAP_ENCAP_PER_PACKET, /* Internetwork analyzer (synchronous) */
WTAP_ENCAP_PER_PACKET, /* Internetwork analyzer (asynchronous) */
WTAP_ENCAP_FDDI_BITSWAPPED,
WTAP_ENCAP_ATM_PDUS
};
#define NUM_NGSNIFF_ENCAPS (sizeof sniffer_encap / sizeof sniffer_encap[0])
struct tm tm;
gint64 current_offset;
ngsniffer_t *ngsniffer;
/* Read in the string that should be at the start of a Sniffer file */
if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
if (memcmp(magic, ngsniffer_magic, sizeof ngsniffer_magic)) {
return WTAP_OPEN_NOT_MINE;
}
/*
* Read the first record, which the manual says is a version
* record.
*/
if (!wtap_read_bytes(wth->fh, record_type, 2, err, err_info))
return WTAP_OPEN_ERROR;
if (!wtap_read_bytes(wth->fh, record_length, 4, err, err_info))
return WTAP_OPEN_ERROR;
type = pletoh16(record_type);
if (type != REC_VERS) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup_printf("ngsniffer: Sniffer file doesn't start with a version record");
return WTAP_OPEN_ERROR;
}
if (!wtap_read_bytes(wth->fh, &version, sizeof version, err, err_info))
return WTAP_OPEN_ERROR;
/* Check the data link type. */
if (version.network >= NUM_NGSNIFF_ENCAPS
|| sniffer_encap[version.network] == WTAP_ENCAP_UNKNOWN) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = g_strdup_printf("ngsniffer: network type %u unknown or unsupported",
version.network);
return WTAP_OPEN_ERROR;
}
/* Check the time unit */
if (version.timeunit >= NUM_NGSNIFF_TIMEUNITS) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = g_strdup_printf("ngsniffer: Unknown timeunit %u", version.timeunit);
return WTAP_OPEN_ERROR;
}
/* compressed or uncompressed Sniffer file? */
if (version.format != 1) {
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_NGSNIFFER_COMPRESSED;
} else {
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_NGSNIFFER_UNCOMPRESSED;
}
/* Set encap type before reading header records because the
* header record may change encap type.
*/
wth->file_encap = sniffer_encap[version.network];
/*
* We don't know how to handle the remaining header record types,
* so we just skip them - except for REC_HEADER2 records, which
* we look at, for "Internetwork analyzer" captures, to attempt to
* determine what the link-layer encapsulation is.
*
* XXX - in some version 1.16 internetwork analyzer files
* generated by the Windows Sniffer when saving Windows
* Sniffer files as DOS Sniffer files, there's no REC_HEADER2
* record, but the first "rsvd" word is 1 for PRI ISDN files, 2
* for BRI ISDN files, and 0 for non-ISDN files; is that something
* the DOS Sniffer understands?
*/
maj_vers = pletoh16(&version.maj_vers);
if (process_header_records(wth, err, err_info, maj_vers,
version.network) < 0)
return WTAP_OPEN_ERROR;
if ((version.network == NETWORK_SYNCHRO ||
version.network == NETWORK_ASYNC) &&
wth->file_encap == WTAP_ENCAP_PER_PACKET) {
/*
* Well, we haven't determined the internetwork analyzer
* subtype yet...
*/
switch (maj_vers) {
case 1:
/*
* ... and this is a version 1 capture; look
* at the first "rsvd" word.
*/
switch (pletoh16(&version.rsvd[0])) {
case 1:
case 2:
wth->file_encap = WTAP_ENCAP_ISDN;
break;
}
break;
case 3:
/*
* ...and this is a version 3 capture; we've
* seen nothing in those that obviously
* indicates the capture type, but the only
* one we've seen is a Frame Relay capture,
* so mark it as Frame Relay for now.
*/
wth->file_encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
break;
}
}
current_offset = file_tell(wth->fh);
/*
* Now, if we have a random stream open, position it to the same
* location, which should be the beginning of the real data, and
* should be the beginning of the compressed data.
*
* XXX - will we see any records other than REC_FRAME2, REC_FRAME4,
* or REC_EOF after this? If not, we can get rid of the loop in
* "ngsniffer_read()".
*/
if (wth->random_fh != NULL) {
if (file_seek(wth->random_fh, current_offset, SEEK_SET, err) == -1)
return WTAP_OPEN_ERROR;
}
/* This is a ngsniffer file */
ngsniffer = (ngsniffer_t *)g_malloc(sizeof(ngsniffer_t));
wth->priv = (void *)ngsniffer;
ngsniffer->maj_vers = maj_vers;
ngsniffer->min_vers = pletoh16(&version.min_vers);
/* We haven't allocated any uncompression buffers yet. */
ngsniffer->seq.buf = NULL;
ngsniffer->seq.nbytes = 0;
ngsniffer->seq.nextout = 0;
ngsniffer->rand.buf = NULL;
ngsniffer->rand.nbytes = 0;
ngsniffer->rand.nextout = 0;
/* Set the current file offset; the offset in the compressed file
and in the uncompressed data stream currently the same. */
ngsniffer->seq.uncomp_offset = current_offset;
ngsniffer->seq.comp_offset = current_offset;
ngsniffer->rand.uncomp_offset = current_offset;
ngsniffer->rand.comp_offset = current_offset;
/* We don't yet have any list of compressed blobs. */
ngsniffer->first_blob = NULL;
ngsniffer->last_blob = NULL;
ngsniffer->current_blob = NULL;
wth->subtype_read = ngsniffer_read;
wth->subtype_seek_read = ngsniffer_seek_read;
wth->subtype_sequential_close = ngsniffer_sequential_close;
wth->subtype_close = ngsniffer_close;
wth->snapshot_length = 0; /* not available in header, only in frame */
ngsniffer->timeunit = Psec[version.timeunit];
ngsniffer->network = version.network;
/* Get capture start time */
start_date = pletoh16(&version.date);
tm.tm_year = ((start_date&DOS_YEAR_MASK)>>DOS_YEAR_SHIFT) + DOS_YEAR_OFFSET;
tm.tm_mon = ((start_date&DOS_MONTH_MASK)>>DOS_MONTH_SHIFT) + DOS_MONTH_OFFSET;
tm.tm_mday = ((start_date&DOS_DAY_MASK)>>DOS_DAY_SHIFT);
#if 0
/* The time does not appear to act as an offset; only the date */
start_time = pletoh16(&version.time);
tm.tm_hour = (start_time&0xf800)>>11;
tm.tm_min = (start_time&0x7e0)>>5;
tm.tm_sec = (start_time&0x1f)<<1;
#endif
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
ngsniffer->start = mktime(&tm);
/*
* XXX - what if "secs" is -1? Unlikely,
* but if the capture was done in a time
* zone that switches between standard and
* summer time sometime other than when we
* do, and thus the time was one that doesn't
* exist here because a switch from standard
* to summer time zips over it, it could
* happen.
*
* On the other hand, if the capture was done
* in a different time zone, this won't work
* right anyway; unfortunately, the time zone
* isn't stored in the capture file.
*/
wth->file_tsprec = WTAP_TSPREC_NSEC; /* XXX */
return WTAP_OPEN_MINE;
}
static int
process_header_records(wtap *wth, int *err, gchar **err_info, gint16 maj_vers,
guint8 network)
{
char record_type[2];
char record_length[4]; /* only the first 2 bytes are length,
the last 2 are "reserved" and are thrown away */
guint16 rec_type, rec_length_remaining;
int bytes_to_read;
unsigned char buffer[256];
for (;;) {
if (!wtap_read_bytes_or_eof(wth->fh, record_type, 2, err, err_info)) {
if (*err != 0)
return -1;
return 0; /* EOF */
}
rec_type = pletoh16(record_type);
if ((rec_type != REC_HEADER1) && (rec_type != REC_HEADER2)
&& (rec_type != REC_HEADER3) && (rec_type != REC_HEADER4)
&& (rec_type != REC_HEADER5) && (rec_type != REC_HEADER6)
&& (rec_type != REC_HEADER7)
&& ((rec_type != REC_V2DESC) || (maj_vers > 2)) ) {
/*
* Well, this is either some unknown header type
* (we ignore this case), an uncompressed data
* frame or the length of a compressed blob
* which implies data. Seek backwards over the
* two bytes we read, and return.
*/
if (file_seek(wth->fh, -2, SEEK_CUR, err) == -1)
return -1;
return 0;
}
if (!wtap_read_bytes(wth->fh, record_length, 4,
err, err_info))
return -1;
rec_length_remaining = pletoh16(record_length);
/*
* Is this is an "Internetwork analyzer" capture, and
* is this a REC_HEADER2 record?
*
* If so, it appears to specify the particular type
* of network we're on.
*
* XXX - handle sync and async differently? (E.g.,
* does this apply only to sync?)
*/
if ((network == NETWORK_SYNCHRO || network == NETWORK_ASYNC) &&
rec_type == REC_HEADER2) {
/*
* Yes, get the first up-to-256 bytes of the
* record data.
*/
bytes_to_read = MIN(rec_length_remaining, (int)sizeof buffer);
if (!wtap_read_bytes(wth->fh, buffer,
bytes_to_read, err, err_info))
return -1;
switch (maj_vers) {
case 2:
if (process_rec_header2_v2(wth, buffer,
rec_length_remaining, err, err_info) < 0)
return -1;
break;
case 1:
case 4:
case 5:
if (process_rec_header2_v145(wth, buffer,
rec_length_remaining, maj_vers, err, err_info) < 0)
return -1;
break;
}
/*
* Skip the rest of the record.
*/
if (rec_length_remaining > sizeof buffer) {
if (file_seek(wth->fh, rec_length_remaining - sizeof buffer,
SEEK_CUR, err) == -1)
return -1;
}
} else {
/* Nope, just skip over the data. */
if (file_seek(wth->fh, rec_length_remaining, SEEK_CUR, err) == -1)
return -1;
}
}
}
static int
process_rec_header2_v2(wtap *wth, unsigned char *buffer, guint16 length,
int *err, gchar **err_info)
{
static const char x_25_str[] = "HDLC\nX.25\n";
/*
* There appears to be a string in a REC_HEADER2 record, with
* a list of protocols. In one X.25 capture I've seen, the
* string was "HDLC\nX.25\nCLNP\nISO_TP\nSESS\nPRES\nVTP\nACSE".
* Presumably CLNP and everything else is per-packet, but
* we assume "HDLC\nX.25\n" indicates that it's an X.25 capture.
*/
if (length < sizeof x_25_str - 1) {
/*
* There's not enough data to compare.
*/
*err = WTAP_ERR_UNSUPPORTED;
*err_info = g_strdup_printf("ngsniffer: WAN capture has too-short protocol list");
return -1;
}
if (strncmp((char *)buffer, x_25_str, sizeof x_25_str - 1) == 0) {
/*
* X.25.
*/
wth->file_encap = WTAP_ENCAP_LAPB;
} else {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = g_strdup_printf("ngsniffer: WAN capture protocol string %.*s unknown",
length, buffer);
return -1;
}
return 0;
}
static int
process_rec_header2_v145(wtap *wth, unsigned char *buffer, guint16 length,
gint16 maj_vers, int *err, gchar **err_info)
{
/*
* The 5th byte of the REC_HEADER2 record appears to be a
* network type.
*/
if (length < 5) {
/*
* There is no 5th byte; give up.
*/
*err = WTAP_ERR_UNSUPPORTED;
*err_info = g_strdup("ngsniffer: WAN capture has no network subtype");
return -1;
}
/*
* The X.25 captures I've seen have a type of NET_HDLC, and the
* Sniffer documentation seems to imply that it's used for
* X.25, although it could be used for other purposes as well.
*
* NET_ROUTER is used for all sorts of point-to-point protocols,
* including ISDN. It appears, from the documentation, that the
* Sniffer attempts to infer the particular protocol by looking
* at the traffic; it's not clear whether it stores in the file
* an indication of the protocol it inferred was being used.
*
* Unfortunately, it also appears that NET_HDLC is used for
* stuff other than X.25 as well, so we can't just interpret
* it unconditionally as X.25.
*
* For now, we interpret both NET_HDLC and NET_ROUTER as "per-packet
* encapsulation". We remember that we saw NET_ROUTER, though,
* as it appears that we can infer whether a packet is PPP or
* ISDN based on the channel number subfield of the frame error
* status bits - if it's 0, it's PPP, otherwise it's ISDN and
* the channel number indicates which channel it is. We assume
* NET_HDLC isn't used for ISDN.
*/
switch (buffer[4]) {
case NET_SDLC:
wth->file_encap = WTAP_ENCAP_SDLC;
break;
case NET_HDLC:
wth->file_encap = WTAP_ENCAP_PER_PACKET;
break;
case NET_FRAME_RELAY:
wth->file_encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
break;
case NET_ROUTER:
/*
* For most of the version 4 capture files I've seen,
* 0xfa in buffer[1] means the file is an ISDN capture,
* but there's one PPP file with 0xfa there; does that
* mean that the 0xfa has nothing to do with ISDN,
* or is that just an ISDN file with no D channel
* packets? (The channel number is not 0 in any
* of the packets, so perhaps it is.)
*
* For one version 5 ISDN capture I've seen, there's
* a 0x01 in buffer[6]; none of the non-ISDN version
* 5 captures have it.
*/
wth->file_encap = WTAP_ENCAP_PER_PACKET;
switch (maj_vers) {
case 4:
if (buffer[1] == 0xfa)
wth->file_encap = WTAP_ENCAP_ISDN;
break;
case 5:
if (length < 7) {
/*
* There is no 5th byte; give up.
*/
*err = WTAP_ERR_UNSUPPORTED;
*err_info = g_strdup("ngsniffer: WAN bridge/router capture has no ISDN flag");
return -1;
}
if (buffer[6] == 0x01)
wth->file_encap = WTAP_ENCAP_ISDN;
break;
}
break;
case NET_PPP:
wth->file_encap = WTAP_ENCAP_PPP_WITH_PHDR;