-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpshow.c
1514 lines (1273 loc) · 53.4 KB
/
tcpshow.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
#if !defined(MAY_NOT_MODIFY)
/****==========------------------------------------------------==========****/
/* */
/* tcpshow, v1.0 */
/* */
/* Quickie to decode a "tcpdump" savefile. */
/* */
/* The application data is displayed as ASCII -- application protocols are */
/* not decoded. */
/* */
/* The data captured by "tcpdump" might be less than in the original */
/* packet. We kludge a solution to this with setjmp()/longjmp(). */
/* */
/* Although written to read tcpdump savefiles, with tcpdump itself as a */
/* front-end, it'll decode any hex dump that adheres to the format */
/* expected. Some programs which capture network data offer an option to */
/* save the trace to a file in hex format -- this can often be massaged */
/* easily with Perl/awk/sh scripts to turn it into the format expected. */
/* As a special case, "tcpdump -s 1500 -lenx | tcpshow -cooked" works */
/* rather well, and "tcpdump -s 1500 -lenx | tcpshow -cooked -data" is nice */
/* for watching the data traffic in real time. */
/* */
/* ------------------------------------------------------------------------ */
/* */
/* Copyright (c) 1996 I.T. NetworX Ltd. All rights reserved. */
/* */
/* This source code is owned and copyrighted by I.T. NetworX Ltd. This */
/* file and all files derived from it, directly or indirectly (such files */
/* collectively and separately being referred to henceforth as "this file") */
/* may be used, modified and redistributed subject to the following five */
/* Conditions. */
/* */
/* Condition 1 of 5: */
/* That all text (code/comments, etc.) in this file surrounded by the macro */
/* block "#if !defined(MAY_NOT_MODIFY) ... #endif", including the macro */
/* statements themselves, may not be modified in any way, or deleted. In */
/* particular, this comment block and the printf() statements identifying */
/* I.T. NetworX as being the copyright owner, in the function usage(), may */
/* not be modified or deleted. The single, only, exception to this is that */
/* the non-inclusion of C comments by a C compiler/linker, in the object */
/* and executable images it produces, is permitted. */
/* */
/* Condition 2 of 5: */
/* That no financial gain be made from using this file, redistributing this */
/* file or modifying this file. */
/* */
/* Condition 3 of 5: */
/* That no conditions other than these five Conditions be applied to the */
/* use, modification or redistribution of this file. */
/* */
/* Condition 4 of 5: */
/* That I.T. NetworX, its employees, agents and everybody else in the world */
/* dead, living and yet to be born, are hereby free from liability of all */
/* and every kind arising from the use of this file by anybody for any */
/* purpose. This file comes "as is" and all warranties, express or */
/* implied, are disclaimed. As the manual page for chat(1) says, "if it */
/* breaks, then you get to keep both pieces". */
/* */
/* Condition 5 of 5: */
/* That I.T. NetworX reserves the right to alter these Conditions at any */
/* time without giving prior notice, such alterations to apply only to the */
/* version current at the time of issue of the alterations and all later */
/* versions, and such alterations to apply only to versions produced */
/* exclusively by I.T. NetworX or its agents. */
/* */
/* Addendum to Copyright: */
/* I'm not a legal eagle and I worded the above notice off the top of my */
/* head, so it may be full of holes, but the spirit of my intentions are */
/* clear from reading it. Please respect these intentions. */
/* */
/* Me too: */
/* If anybody makes significant improvements to this file, such as adding */
/* decode support for DNS traffic, I would appreciate it if they send me a */
/* copy of their work ([email protected]). Thanks. */
/* */
/* ------------------------------------------------------------------------ */
/* */
/* File layout is as follows: */
/* system includes */
/* local includes */
/* #define macros */
/* typedefs */
/* declarations of extern variables */
/* declarations of extern functions */
/* declarations of global variables */
/* declarations of global functions */
/* declarations of static variables */
/* declarations of static functions */
/* definitions of functions */
/* (all functions and variables are declared/defined in alphabetical order) */
/* */
/* ------------------------------------------------------------------------ */
/* */
/* Compiles as follows: */
/* cc -s -O -o tcpshow tcpshow.c */
/* */
/* ------------------------------------------------------------------------ */
/* */
/* Who and when: */
/* MikeRyan, 11apr96. */
/* */
/* I.T. NetworX, */
/* 67 Merrion Square, */
/* Dublin 2, */
/* Ireland. */
/* Phone: +353-1-676-8866 */
/* Fax: +353-1-676-8868 */
/* Email: [email protected] */
/* */
/* ------------------------------------------------------------------------ */
/* */
/* Modification History */
/* MikeRyan, 14may96: Allow "tcpdump" expressions to be passed in. */
/* MikeRyan, 15may96: Added UDP decode logic. */
/* MikeRyan, 16may96: Added ICMP decode logic. */
/* MikeRyan, 16may96: Added -b switch to break long lines */
/* -w option to specify the width of the page */
/* -h flag to give help on usage. */
/* MikeRyan, 28may96: Added -nolink */
/* -nodata */
/* -noip */
/* -track */
/* -terse */
/* -sb */
/* -s */
/* MikeRyan, 25jun96: Incorporated my "general.h" typedef's, so that the */
/* present source file doesn't depend on any non-standard header files. */
/* This allows it to be distributed as a single file. Also included the */
/* copyright notice, as I'm making the program freely available. */
/* */
/* MikeRyan, 26jun96: Added -cooked */
/* -pp */
/* */
/****==========------------------------------------------------==========****/
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <netdb.h>
#include <setjmp.h>
/* Some general defines. */
#if defined(FALSE)
#undef FALSE
#endif
#if defined(TRUE)
#undef TRUE
#endif
#define FALSE (boolean)0
#define TRUE (boolean)1
#define elif else if
#if !defined(reg)
#define reg register /* For debugging purposes */
#endif
#define VERSION 1.0 /* Please change when appropriate */
#define COOKER "tcpdump"
#define MAXCOOKARGS 100 /* Max tcpdump expression words */
#define MAXPKT 10240 /* Should be 1518 for Ethernet */
#define NCOLS 60
/* IP header elements. */
#define IPHDRLEN 20
#define FRAGOFF 0x1FFF
#define MF 0x2000
#define DF 0x4000
/* TCP header elements. */
#define TCPHDRLEN 20
#define URG 0x0020
#define ACK 0x0010
#define PSH 0x0008
#define RST 0x0004
#define SYN 0x0002
#define FIN 0x0001
/* UDP header elements. */
#define UDPHDRLEN 8
/* ICMP header elements. */
#define ICMPHDRLEN 4
/* IP protocol types. */
#define IP 0
#define ICMP 1
#define IGMP 2
#define GGP 3
#define IPENCAP 4
#define ST 5
#define TCP 6
#define EGP 8
#define PUP 12
#define UDP 17
#define HMP 20
#define XNSIDP 22
#define RDP 27
#define ISOTP4 29
#define XTP 36
#define IDPRCMTP 39
#define RSVP 46
#define VMTP 81
#define OSPF 89
#define IPIP 94
#define ENCAP 98
/* ICMP types. */
#define ECHO_REPLY 0
#define DST_UNREACH 3
#define SRC_QUENCH 4
#define REDIRECT 5
#define ECHO_REQ 8
#define ROUTER_AD 9
#define ROUTER_SOL 10
#define TIME_EXCEED 11
#define PARAM_PROB 12
#define TIME_REQ 13
#define TIME_REPLY 14
#define INFO_REQ 15
#define INFO_REPLY 16
#define MASK_REQ 17
#define MASK_REPLY 18
/* ICMP codes for type == Destination Unreachable. */
#define NET_UNREACH 0
#define HOST_UNREACH 1
#define PROTO_UNREACH 2
#define PORT_UNREACH 3
#define DF_SET 4
#define SRCROUTE_FAILED 5
#define DSTNET_UNKNOWN 6
#define DSTHOST_UNKNOWN 7
#define SRCHOST_ISOLATED 8
#define DSTNET_PROHIB 9
#define DSTHOST_PROHIB 10
#define NET_UNREACH_TOS 11
#define HOST_UNREACH_TOS 12
#define COMM_PROHIB 13
#define HOST_PREC_VIOL 14
#define PREC_CUTOFF 15
/* ICMP codes for type == Redirect. */
#define REDIR_FOR_NET 0
#define REDIR_FOR_HOST 1
#define REDIR_FOR_TOSNET 2
#define REDIR_FOR_TOSHOST 3
/* ICMP codes for type == Time Exceeded. */
#define TTL_ZERO 0
#define REASS_TIMEOUT 1
/* ICMP codes for type == Parameter Problem. */
#define IP_HDR_BAD 0
#define MISSING_OPT 1
/* Skip remaining lines of current packet. Note that this causes a */
/* longjmp(), so a succeeding "return" from a function isn't needed. */
#define nextpkt() for ( ; ; ) (void)getpkt()
/* Display a separator line between packet decodes. */
#define prsep() \
printf( \
"-----------------------------------------------------------------\n" \
)
/* My own preferred basic data types -- amend per target machine. */
typedef char boolean;
typedef float float4;
typedef double float8;
typedef char int1;
typedef short int2;
typedef int int4;
typedef unsigned char uint1;
typedef unsigned short uint2;
typedef unsigned int uint4;
typedef unsigned char uchar;
void main(int, char **);
static boolean bflag = FALSE;
static char *cookargs[MAXCOOKARGS+1];
static boolean cookedflag = FALSE;
static boolean dataflag = FALSE;
static uint2 datalen = 0;
static char *dflt_cookargs[] = {
COOKER, "-enx", "-s10240", "-r-", (char *)NULL
};
static char dip[16];
static boolean isip;
static jmp_buf jmpbuf;
static boolean nodataflag = FALSE;
static boolean noipflag = FALSE;
static boolean nolinkflag = FALSE;
static int npkts_shown = 0;
static char *off = "off,"; /* "off" in middle of list */
static char *off_e = "off"; /* "off" at end of list */
static char *on = "on, "; /* "on" in middle of list */
static char *on_e = "on"; /* "on" at end of list */
static int pagewidth = NCOLS;
static char *pkt;
static boolean ppflag = FALSE;
static uint1 proto;
static boolean sflag = FALSE;
static boolean sbflag = FALSE;
static char sip[16];
static boolean terseflag = FALSE;
static boolean trackflag = FALSE;
static char *unknown = "<unknown>";
static void error(char *);
static char *etheraddr(char *);
static char *ether_proto(char *);
static void fork_tcpdump(int, char **);
static uint1 getbyte(char **);
static uint4 getlongword(char **);
static char *getpkt(void);
static uint2 getword(char **);
static char *icmpcode(uint1, uint1);
static char *icmptype(uint1);
static char *ipaddr(char **);
static char *ip_proto(uint1);
static char nextchar(char **);
static char *rmwspace(char *);
static char *showdata(char *);
static char *showhdr(char *);
static char *showicmp(char *);
static char *showip(char *);
static void showpkt(char *);
static char *showtcp(char *);
static char *showudp(char *);
static char *skip(char *, uint2);
static char *svcname(uint2, char *, boolean);
static void usage(void);
/****==========------------------------------------------------==========****/
/* */
/* Print an error message and exit. */
/* */
/****==========------------------------------------------------==========****/
static void error (
char *msg
) {
fprintf(stderr, "***Error: %s\n", msg);
exit(1);
}
/****==========------------------------------------------------==========****/
/* */
/* Print a formatted Ethernet address. */
/* */
/****==========------------------------------------------------==========****/
static char *etheraddr (
char *eaddr
) {
static char formatted[18];
int i;
int j;
for (i = j = 0; i < 6; i++)
if (eaddr[1] == ':') {
formatted[j++] = '0';
formatted[j++] = toupper(eaddr[0]);
formatted[j++] = ':';
eaddr += 2;
}
else {
formatted[j++] = toupper(eaddr[0]);
formatted[j++] = toupper(eaddr[1]);
formatted[j++] = ':';
eaddr += 3;
}
formatted[j-1] = '\0';
return formatted;
}
/****==========------------------------------------------------==========****/
/* */
/* Print the type of protocol encapsulated in the Ethernet frame. */
/* */
/****==========------------------------------------------------==========****/
static char *ether_proto (
char *type
) {
if (strcmp(type, "0800") == 0)
return "IP";
elif (strcmp(type, "0806") == 0)
return "ARP";
elif (strcmp(type, "8035") == 0)
return "RARP";
else
return type;
}
/****==========------------------------------------------------==========****/
/* */
/* Run tcpdump to pre-process the trace file. */
/* */
/****==========------------------------------------------------==========****/
static void fork_tcpdump (
int argc,
char **argv
) {
int fd[2];
int i;
pid_t pid;
/* Required "tcpdump" flags. */
i = 0;
while (dflt_cookargs[i]) {
cookargs[i] = dflt_cookargs[i];
i++;
}
while (argc-- > 0) {
if (i >= MAXCOOKARGS) error("Too many expressions");
cookargs[i++] = *argv++;
}
cookargs[i] = (char *)NULL;
/* Fork tcpdump to cook our input. */
if (pipe(fd)) error("pipe() failed");
if ((pid=fork()) < 0) error("fork() failed");
if (pid == 0) {
(void)close(1);
if (dup(fd[1]) != 1) error("dup() failed");
(void)close(fd[0]);
(void)close(fd[1]);
execvp(COOKER, cookargs);
error("execvp() failed");
}
(void)close(0);
if (dup(fd[0]) != 0) error("dup() failed");
(void)close(fd[0]);
(void)close(fd[1]);
}
/****==========------------------------------------------------==========****/
/* */
/* Return the byte value and increment the pointer by sizeof(byte). */
/* */
/****==========------------------------------------------------==========****/
static uint1 getbyte (
char **pkt
) {
char byte[1*2+1]; /* ASCII representation of a byte */
unsigned int val;
byte[0] = nextchar(pkt);
byte[1] = nextchar(pkt);
byte[2] = '\0';
(void)sscanf(byte, "%x", &val);
return (uint1)val;
}
/****==========------------------------------------------------==========****/
/* */
/* Return the longword value and increment the pointer by sizeof(longword). */
/* */
/****==========------------------------------------------------==========****/
static uint4 getlongword (
char **pkt
) {
char longword[4*2+1]; /* ASCII representation of a longword */
unsigned long val;
longword[0] = nextchar(pkt);
longword[1] = nextchar(pkt);
longword[2] = nextchar(pkt);
longword[3] = nextchar(pkt);
longword[4] = nextchar(pkt);
longword[5] = nextchar(pkt);
longword[6] = nextchar(pkt);
longword[7] = nextchar(pkt);
longword[8] = '\0';
(void)sscanf(longword, "%lx", &val);
return (uint4)val;
}
/****==========------------------------------------------------==========****/
/* */
/* Read in the next line of packet data. */
/* */
/****==========------------------------------------------------==========****/
static char *getpkt (
) {
static boolean been_here_already = FALSE;
static char pktbuf[MAXPKT+1];
if (fgets(pktbuf, MAXPKT+1, stdin) == (char *)NULL) exit(0);
/* Line without leading <tab> means start of new packet. */
if (*pktbuf == '\t')
return rmwspace(pktbuf);
elif (! been_here_already) { /* setjmp() won't have been called */
been_here_already = TRUE; /* before reading 1st packet */
return pkt = pktbuf;
}
else {
if (datalen > 0)
printf("\n\t<*** Rest of data missing from packet dump ***>\n");
pkt = pktbuf;
longjmp(jmpbuf, 1);
}
}
/****==========------------------------------------------------==========****/
/* */
/* Return the word value and increment the pointer by sizeof(word). */
/* */
/****==========------------------------------------------------==========****/
static uint2 getword (
char **pkt
) {
char word[2*2+1]; /* ASCII representation of a word */
unsigned int val;
word[0] = nextchar(pkt);
word[1] = nextchar(pkt);
word[2] = nextchar(pkt);
word[3] = nextchar(pkt);
word[4] = '\0';
(void)sscanf(word, "%x", &val);
return (uint2)val;
}
/****==========------------------------------------------------==========****/
/* */
/* Print the code relating to the ICMP type. */
/* */
/****==========------------------------------------------------==========****/
static char *icmpcode (
uint1 type,
uint1 code
) {
char *bad;
char *descr;
bad = "<*** CORRUPT ***>";
descr = (char *)NULL;
switch (type) {
case ECHO_REPLY:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case DST_UNREACH:
switch (code) {
case NET_UNREACH: descr = "network-unreachable"; break;
case HOST_UNREACH: descr = "host-unreachable"; break;
case PROTO_UNREACH: descr = "protocol-unreachable"; break;
case PORT_UNREACH: descr = "port-unreachable"; break;
case DF_SET: descr = "frag-needed-but-DF-set"; break;
case SRCROUTE_FAILED: descr = "source-route-failed"; break;
case DSTNET_UNKNOWN: descr = "destination-network-unknown"; break;
case DSTHOST_UNKNOWN: descr = "destination-host-unknown"; break;
case SRCHOST_ISOLATED: descr = "source-host-isolated"; break;
case DSTNET_PROHIB: descr = "dest-net-admin-prohibited"; break;
case DSTHOST_PROHIB: descr = "dest-host-admin-prohibited"; break;
case NET_UNREACH_TOS: descr = "network-unreachable-for-TOS"; break;
case HOST_UNREACH_TOS: descr = "host-unreachable-for-TOS"; break;
case COMM_PROHIB: descr = "trafffic-prohibited-by-filter"; break;
case HOST_PREC_VIOL: descr = "host-precedence-violation"; break;
case PREC_CUTOFF: descr = "precedence-cutoff-in-effect"; break;
default: descr = bad; break;
}
break;
case SRC_QUENCH:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case REDIRECT:
switch (code) {
case REDIR_FOR_NET: descr = "route-wrong-for-network"; break;
case REDIR_FOR_HOST: descr = "route-wrong-for-host"; break;
case REDIR_FOR_TOSNET: descr = "route-wrong-for-TOS-and-net"; break;
case REDIR_FOR_TOSHOST: descr = "route-wrong-for-TOS-and-host"; break;
default: descr = bad; break;
}
break;
case ECHO_REQ:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case ROUTER_AD:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case ROUTER_SOL:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case TIME_EXCEED:
switch (code) {
case TTL_ZERO: descr = "TTL-reached-zero"; break;
case REASS_TIMEOUT: descr = "reassembly-timer-expired"; break;
default: descr = bad; break;
}
break;
case PARAM_PROB:
switch (code) {
case IP_HDR_BAD: descr = "IP-header-bad"; break;
case MISSING_OPT: descr = "required-option-is-missing"; break;
default: descr = bad; break;
}
break;
case TIME_REQ:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case TIME_REPLY:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case INFO_REQ:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case INFO_REPLY:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case MASK_REQ:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
case MASK_REPLY:
switch (code) {
case 0: break;
default: descr = bad; break;
}
break;
default:
break;
}
return descr;
}
/****==========------------------------------------------------==========****/
/* */
/* Print the type of ICMP packet. */
/* */
/****==========------------------------------------------------==========****/
static char *icmptype (
uint1 type
) {
char *descr;
switch (type) {
case ECHO_REPLY: descr = "echo-reply"; break;
case DST_UNREACH: descr = "destination-unreachable"; break;
case SRC_QUENCH: descr = "source-quench"; break;
case REDIRECT: descr = "redirect"; break;
case ECHO_REQ: descr = "echo-request"; break;
case ROUTER_AD: descr = "router-advertisement"; break;
case ROUTER_SOL: descr = "router-solicitation"; break;
case TIME_EXCEED: descr = "time-exceeded"; break;
case PARAM_PROB: descr = "parameter-problem"; break;
case TIME_REQ: descr = "timestamp-request"; break;
case TIME_REPLY: descr = "timestamp-reply"; break;
case INFO_REQ: descr = "information-request"; break;
case INFO_REPLY: descr = "information-reply"; break;
case MASK_REQ: descr = "address-mask-request"; break;
case MASK_REPLY: descr = "address-mask-reply"; break;
default: descr = unknown; break;
}
return descr;
}
/****==========------------------------------------------------==========****/
/* */
/* Print the IP address in dotted-quad. */
/* */
/****==========------------------------------------------------==========****/
static char *ipaddr (
char **pkt
) {
static char addr[16];
uint2 byte1;
uint2 byte2;
uint2 byte3;
uint2 byte4;
/* We don't use inet_ntoa() because it wants a socket structure. */
byte1 = (uint2)getbyte(pkt);
byte2 = (uint2)getbyte(pkt);
byte3 = (uint2)getbyte(pkt);
byte4 = (uint2)getbyte(pkt);
(void)sprintf(addr, "%d.%d.%d.%d", byte1, byte2, byte3, byte4);
return addr;
}
/****==========------------------------------------------------==========****/
/* */
/* Print the type of protocol encapsulated in the IP datagram. */
/* */
/****==========------------------------------------------------==========****/
static char *ip_proto (
uint1 code
) {
char *name;
/* A simple table won't do, as the codes aren't contiguous. */
switch (code) {
case IP:
name = "IP"; break;
case ICMP:
name = "ICMP"; break;
case IGMP:
name = "IGMP"; break;
case GGP:
name = "GGP"; break;
case IPENCAP:
name = "IPENCAP"; break;
case ST:
name = "ST"; break;
case TCP:
name = "TCP"; break;
case EGP:
name = "EGP"; break;
case PUP:
name = "PUP"; break;
case UDP:
name = "UDP"; break;
case HMP:
name = "HMP"; break;
case XNSIDP:
name = "XNSIDP"; break;
case RDP:
name = "RDP"; break;
case ISOTP4:
name = "ISOTP4"; break;
case XTP:
name = "XTP"; break;
case IDPRCMTP:
name = "IDPRCMTP"; break;
case RSVP:
name = "RSVP"; break;
case VMTP:
name = "VMTP"; break;
case OSPF:
name = "OSPF"; break;
case IPIP:
name = "IPIP"; break;
case ENCAP:
name = "ENCAP"; break;
default:
name = unknown; break;
}
return name;
}
/****==========------------------------------------------------==========****/
/* */
/* Decode a "tcpdump" savefile. */
/* */
/****==========------------------------------------------------==========****/
void main (
int argc,
char **argv
) {
/* Command line options. */
while (--argc > 0 && **++argv == '-')
if (strcmp(*argv, "-data") == 0)
dataflag = nolinkflag = noipflag = TRUE;
elif (strcmp(*argv, "-s") == 0) sflag = TRUE;
elif (strcmp(*argv, "-b") == 0) bflag = TRUE;
elif (strcmp(*argv, "-sb") == 0) sbflag = TRUE;
elif (strcmp(*argv, "-terse") == 0) terseflag = TRUE;
elif (strcmp(*argv, "-track") == 0) trackflag = TRUE;
elif (strcmp(*argv, "-nodata") == 0) nodataflag = TRUE;
elif (strcmp(*argv, "-nolink") == 0) nolinkflag = TRUE;
elif (strcmp(*argv, "-noip") == 0) noipflag = TRUE;
elif (strcmp(*argv, "-cooked") == 0) cookedflag = TRUE;
elif (strcmp(*argv, "-pp") == 0) ppflag = TRUE;
elif (strcmp(*argv, "-h") == 0) usage();
elif (strcmp(*argv, "-w") == 0) {
if (--argc <= 0) error("-w needs a numeric argument");
if ((pagewidth=atoi(*++argv)) < 1) error("-w value too small");
}
else error("Unknown command line flag");
if (! cookedflag)
fork_tcpdump(argc, argv);
elif (argc != 0)
fprintf(stderr, "input is cooked -- ignoring tcpdump expressions\n");
pkt = getpkt();
for ( ; ; ) if (! setjmp(jmpbuf)) showpkt(pkt);
exit(0);
}
/****==========------------------------------------------------==========****/
/* */
/* Return the next character in the packet buffer. */
/* */
/****==========------------------------------------------------==========****/
static char nextchar (
char **pkt
) {
if (! **pkt) *pkt = getpkt();
return *(*pkt)++;
}
/****==========------------------------------------------------==========****/
/* */
/* Remove whitespace from the buffer. */
/* */
/****==========------------------------------------------------==========****/
static char *rmwspace (
reg char *pktbuf
) {
static char cleanpkt[MAXPKT+1];
reg char *pkt;
pkt = cleanpkt;
while (*pktbuf) {
if (! isspace(*pktbuf)) *pkt++ = *pktbuf;
pktbuf++;
}
*pkt = '\0';
return cleanpkt;
}
/****==========------------------------------------------------==========****/
/* */
/* Decode the TCP/UDP data. */
/* */
/****==========------------------------------------------------==========****/
static char *showdata (
char *pkt
) {
uint1 byte;
int col;
char *descr;
if (dataflag)
putchar('\t');
elif (terseflag)
printf("DATA:\t");
else {
switch (proto) {
case TCP: descr = "TCP"; break;
case UDP: descr = "UDP"; break;
case ICMP: descr = "ICMP"; break;
default: descr = unknown; break;
}
printf("%s Data\n\t", descr);
}
if (nodataflag) {
uint2 ndatabytes = datalen;
datalen = 0;
printf("%d bytes\n", ndatabytes);
return skip(pkt, ndatabytes);
}
if (datalen == 0) {
printf("<No data>\n");
return pkt;
}
switch (bflag) {
case TRUE:
for (col = 1; datalen > 0; datalen--, col++) {
byte = getbyte(&pkt);
if (byte == '\n') {
putchar('\n');
byte = '\t';
col = 0;
}
elif (col > pagewidth) {
printf("%s\n\t", sbflag? "<break>": "");
col = 1;
}
if (byte != '\t' && byte != '\n' && !isprint(byte)) byte = '.';
putchar(byte);
}
break;
case FALSE:
for ( ; datalen > 0; datalen--) {
byte = getbyte(&pkt);
if (byte == '\n') {