forked from sezero/watt32
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpcdbug.c
3776 lines (3206 loc) · 101 KB
/
pcdbug.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
/*!\file pcdbug.c
*
* Watt-32 protocol debugger.
* Writes to `debug.file' specified in config-file.
* File may be stdout/stderr/nul.
*
* Most functions are prefixed with `dbug_'. And variables
* with `dbg_'.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <setjmp.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <arpa/nameser.h>
#include <resolv.h>
#if defined(__DJGPP__) && (DJGPP_MINOR >= 4)
#include <sys/xdevices.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <fcntl.h>
#if !defined(__POCC__)
#include <share.h>
#endif
#endif
#include "wattcp.h"
#include "pcdns.h"
#include "misc.h"
#include "misc_str.h"
#include "run.h"
#include "timer.h"
#include "sock_ini.h"
#include "chksum.h"
#include "ip4_in.h"
#include "ip6_in.h"
#include "ip6_out.h"
#include "pctcp.h"
#include "pcsed.h"
#include "pcarp.h"
#include "pcqueue.h"
#include "pcpkt.h"
#include "pcstat.h"
#include "pcicmp.h"
#include "pcicmp6.h"
#include "pcconfig.h"
#include "netaddr.h"
#include "language.h"
#include "printk.h"
#include "gettod.h"
#include "get_xby.h"
#include "pppoe.h"
#include "ppp.h"
#include "split.h"
#include "bsddbug.h"
#include "pcdbug.h"
#if defined(USE_GZIP)
#include "zlib.h"
#endif
#if defined(USE_SCTP)
#include "sctp.h"
#endif
#define DEBUG_RTP 1 /**< \todo include detailed RTP debugging */
const char *tcpStateName (UINT state)
{
static char buf [20];
static const char *tcpStates[] = {
"LISTEN", "RESOLVE", "SYNSENT", "SYNREC", "ESTAB",
"ESTCLOSE", "FINWT1", "FINWT2", "CLOSWT", "CLOSING",
"LASTACK", "TIMEWT", "CLOSED"
};
if (state < DIM(tcpStates))
return tcpStates [(int)state];
return itoa (state, buf, 10);
}
DebugProc debug_xmit = NULL;
DebugProc debug_recv = NULL;
#if !defined(USE_DEBUG)
void dbug_init (void)
{
outsnl ("Debug-mode disabled");
}
#else /* rest of file */
static void W32_CALL dbug_exit (void);
static void dbug_close (void);
static void dump_ip_opt (const void *opt, int len);
static void dump_tcp_opt(const void *opt, int len, DWORD ack);
static DWORD dump_data (const void *data, UINT datalen);
static int udp_dump (const _udp_Socket *sock, const in_Header *ip);
static int tcp_dump (const _tcp_Socket *sock, const in_Header *ip);
static unsigned dns_dump (const BYTE *, unsigned, unsigned);
static const BYTE *dns_resource (const BYTE *, const BYTE *, const BYTE *);
static const BYTE *dns_labels (const BYTE *, const BYTE *, const BYTE *);
static int write_pcap_header (void);
static int write_pcap_packet (const void *pkt, BOOL out);
static void print_cpu_info (void);
#if defined(USE_PPPOE)
static unsigned ccp_dump (const BYTE *bp);
static unsigned lcp_dump (const BYTE *bp);
static unsigned ipcp_dump (const BYTE *bp);
#endif
#if (DOSX) && !(defined(__CYGWIN__) && defined(__x86_64__)) && \
!defined(__NO_INLINE__) /* Don't do this if 'gcc -O0' is used */
#define PRINT_CPU_INFO
#define COMPILING_PCDBUG_C
#include "cpuid.c"
#endif
static void (W32_CALL *prev_hook) (const char *, const char *) = NULL;
static jmp_buf dbg_jmp;
static char dbg_name [MAX_PATHLEN+1] = "WATTCP.DBG";
static BOOL dbg_linebuf = FALSE;
static BOOL pcap_mode = FALSE;
static DWORD now; /* ticks or milli-sec */
static BOOL outbound; /* transmitting packet? */
static BOOL in_dbug_dump;
static BOOL use_gzip; /* if using pcap-mode, use gzip-compression */
static BOOL use_ods = FALSE; /* Win32: use OutputDebugString() */
static char ip4_src [20];
static char ip4_dst [20];
static union {
FILE *stream;
void *gz_stream;
} dbg_file;
/* IP-fragment handling
*/
#define IS_FRAG 0x01 /* is this a fragment? */
#define IS_LAST 0x02 /* is this last fragment? */
#define IS_FIRST 0x04 /* is this first fragment? */
static int frag_status; /* IPv4 fragment bits */
#if defined(USE_IPV6)
static char ip6_src [50];
static char ip6_dst [50];
static int udp6_dump (const _udp_Socket *sock, const in6_Header *ip);
#endif
static struct {
char MAC;
char ARP;
char RARP;
char IP; /* both IPv4 and IPv6 */
char BCAST;
char MCAST;
char LOOP;
char NONE;
} filter = { 1,1,1,1,1,1,0,0 };
static struct {
char MAC;
char LLC;
char ARP;
char RARP;
char IP;
char UDP;
char TCP;
char ICMP;
char IGMP;
char SCTP;
} debug = { 0,0,1,1,1,1,1,1,1,1 };
/**
* These are public so they can be set by application if
* running without a config-file. \todo make them static.
*/
BOOL dbg_mode_all = 1;
BOOL dbg_print_stat = 1;
BOOL dbg_print_size = 0;
BOOL dbg_dns_details = 1;
#if defined(USE_GZIP)
static DWORD bytes_raw = 0UL; /* raw uncompressed bytes written */
#endif
/*----------------------------------------------------------------------*/
BOOL dbug_file (void)
{
if (pcap_mode) /* don't let anyone mess with this file */
return (FALSE);
return (dbg_file.stream != NULL || use_ods);
}
void dbug_open (void)
{
const char *fmode = pcap_mode ? "w+b" : "w+t";
const char *end;
if (dbg_file.stream || use_ods) /* Already opened; quit */
return;
#if defined(__DJGPP__) && (DJGPP_MINOR >= 4) /* Just for testing */
if (!stricmp(dbg_name, "/dev/zero"))
__install_dev_zero();
else if (!stricmp(dbg_name, "/dev/full"))
__install_dev_full();
#endif
end = strchr (dbg_name, '\0');
use_gzip = (end && !stricmp(end - 3, ".gz"));
if (!stricmp(dbg_name, "con") || !stricmp(dbg_name, "stdout"))
dbg_file.stream = stdout;
else if (!stricmp(dbg_name, "stderr"))
dbg_file.stream = stderr;
#if defined(_WIN32)
else if (!stricmp(dbg_name, "$ods"))
{
use_ods = TRUE;
goto quit;
}
#endif
#if defined(USE_GZIP)
else if (pcap_mode && use_gzip)
{
#if defined(_WIN32) && !defined(__WATCOMC__) && !defined(__CYGWIN__)
int fd = _sopen (dbg_name, _O_CREAT | _O_TRUNC | _O_WRONLY,
SH_DENYWR, S_IREAD | S_IWRITE);
if (fd > -1)
dbg_file.gz_stream = gzdopen (fd, "w+b2"); /* compression level 2 */
#else
errno = 0;
dbg_file.gz_stream = gzopen (dbg_name, "w+b2"); /* compression level 2 */
#endif
}
#endif /* USE_GZIP */
else
{
dbg_file.stream = fopen_excl (dbg_name, fmode);
use_gzip = FALSE;
}
if (!dbg_file.stream)
{
(*_printf) (_LANG("ERROR: unable to open debug file %s; %s\n"),
dbg_name, strerror(errno));
errno = 0; /* clear it. It coult confuse callers */
return;
}
if (pcap_mode)
{
if (dbg_file.stream == stdout || dbg_file.stream == stderr)
{
outsnl (_LANG("ERROR: cannot debug to screen with \"DEBUG.PCAP=1\""));
dbug_close();
return;
}
if (write_pcap_header() < 0)
{
(*_printf) (_LANG("ERROR: Failed to write pcap header; %s\n"),
strerror(errno));
dbug_close();
return;
}
}
#if !defined(__BORLANDC__) /* buggy output with this */
else if (dbg_linebuf)
{
static char buf [256];
setvbuf (dbg_file.stream, buf, _IOLBF, sizeof(buf));
}
#endif
#if defined(_WIN32)
quit:
#endif
RUNDOWN_ADD (dbug_exit, 5);
}
/*
* Return TRUE if MAC address is multicast (LSB bit 0 set).
*/
static __inline BOOL MAC_is_mcast (const void *addr)
{
return ((*(const BYTE*)addr & 1) == 1);
}
/*
* Return TRUE if MAC address is broadcast.
*/
static __inline BOOL MAC_is_bcast (const void *addr)
{
return (!memcmp(addr, _eth_brdcast, _eth_mac_len));
}
/*
* Using NDIS3PKT causes all non-broadcast packets sent to
* appear in receive buffer approx 100 msec later.
* WinPcap: Also if _pkt_rxmode == RXMODE_ALL_LOCAL.
*
* To avoid confusion, we print a warning in this case.
*/
static __inline BOOL is_looped (const in_Header *ip)
{
if (outbound || _pktserial)
return (FALSE);
if (memcmp(MAC_SRC(ip), _eth_addr, _eth_mac_len))
return (FALSE);
if (MAC_TYP(ip) != IP4_TYPE) /* assume looped if not IPv4 */
return (TRUE);
/* if MAC-src matches, but IP-src is different it could be
* Winsock sending. Thus it's not a looped packet.
*/
return (ntohl(ip->source) == my_ip_addr);
}
/*
* Return TRUE if MAC destination address of received/sent link-layer
* packet:
* - matches our link-layer address.
* - is broadcast and we don't filter broadcast.
* - is multicast and we don't filter multicast.
*/
static __inline BOOL match_link_destination (const void *addr)
{
if (!memcmp(addr, _eth_addr, _eth_mac_len) ||
(!filter.BCAST && MAC_is_bcast(addr)) ||
(!filter.MCAST && MAC_is_mcast(addr)))
return (TRUE);
return (FALSE);
}
/*
* Return TRUE if destination address of received/sent ARP packet:
* - matches our ether/IP-address.
* - is broadcast and we don't filter broadcast
* (ARP/RARP packets should never use multicast in it's header).
*/
static __inline BOOL match_arp_rarp (const arp_Header *arp)
{
if (!memcmp(&arp->dstEthAddr, _eth_addr, _eth_mac_len) ||
_ip4_is_local_addr(intel(arp->dstIPAddr)))
return (TRUE);
if (!filter.BCAST && MAC_is_bcast(&arp->dstEthAddr))
return (TRUE);
return (FALSE);
}
/*
* Return TRUE if destination address of received/sent IP packet:
* - matches our IP-address.
* - is broadcast and we don't filter (directed) IP-broadcast.
*/
static __inline BOOL match_ip4_dest (const in_Header *ip)
{
DWORD destin = intel (ip->destination);
if (_ip4_is_local_addr(destin) || (!filter.BCAST && _ip4_is_ip_brdcast(ip)))
return (TRUE);
return (FALSE);
}
#if defined(USE_IPV6)
static __inline BOOL match_ip6_dest (const in6_Header *ip)
{
const void *dest = &ip->destination;
if (_ip6_is_local_addr(dest) ||
IN6_IS_ADDR_MC_GLOBAL(dest)) /** \todo needs work */
return (TRUE);
return (FALSE);
}
#endif
/*
* Return checksum and print "ok" or "ERROR"
*/
static __inline const char *do_check_sum (WORD value, const void *p, int len)
{
static char buf [20];
WORD chk = CHECKSUM (p, len);
sprintf (buf, "%04X (%s)", value, (chk == 0xFFFF) ? "ok" : "ERROR");
return (buf);
}
/*
* Return name of some known link-layer protocols.
*/
static __inline const char *link_protocol (WORD type)
{
switch (type)
{
case IP4_TYPE:
return ("IP");
case IP6_TYPE:
return ("IP6");
case ARP_TYPE:
return ("ARP");
case RARP_TYPE:
return ("RARP");
case IEEE802_1Q_TYPE:
return ("VLAN");
case PPPOE_DISC_TYPE:
return ("PPPoE DISCOVERY");
case PPPOE_SESS_TYPE:
return ("PPPoE SESSION");
default:
return ("unknown");
}
}
/*
* Return name of known IP-protocols.
*/
static __inline const char *ip4_protocol (BYTE prot)
{
static char buf [4];
switch (prot)
{
case UDP_PROTO:
return ("UDP");
case TCP_PROTO:
return ("TCP");
case ICMP_PROTO:
return ("ICMP");
case IGMP_PROTO:
return ("IGMP");
case SCTP_PROTO:
return ("SCTP");
}
return itoa (prot, buf, 10);
}
/*
* Return name for IP's "Type Of Service".
*/
static const char *type_of_service (BYTE tos)
{
static char buf [30];
char *p = buf;
if (tos == 0)
return (" 0");
*p = '\0';
if (tos & IP_MINCOST)
{
strcat (buf, " Mcost");
tos &= ~IP_MINCOST;
}
if (tos & IP_RELIABILITY)
{
strcat (buf, " Rel");
tos &= ~IP_RELIABILITY;
}
if (tos & IP_THROUGHPUT)
{
strcat (buf, " ThPut");
tos &= ~IP_THROUGHPUT;
}
if (tos & IP_LOWDELAY)
{
strcat (buf, " LwDly");
tos &= ~IP_LOWDELAY;
}
p = strchr (buf, '\0');
if (tos)
sprintf (p, " %02X", tos);
return (buf);
}
/*
* Format time for "Round Trip Time" in msec.
*/
static __inline const char *RTT_str (DWORD rtt, DWORD now)
{
static char buf [40];
if (rtt == 0UL || rtt < now)
return ("--"); /* RTT timer stopped */
sprintf (buf, "%ld", get_timediff (rtt, now));
return (buf);
}
/*
* Return string for ARP/RARP opcodes.
*/
static __inline const char *arp_opcode (WORD code)
{
if (code == ARP_REQUEST || code == RARP_REQUEST)
return ("Request");
if (code == ARP_REPLY || code == RARP_REPLY)
return ("Reply");
return ("? op");
}
/*
* Print IP source/destination addresses and ports;
* "host1 (a) -> host2 (b)"
*
* Note: the udp_Header src/dst port field are not printed.
*/
static void dump_addr_port (const char *proto,
const void *sock,
const in_Header *ip)
{
const tcp_Header *tcp = (const tcp_Header*) ((BYTE*)ip + in_GetHdrLen(ip));
if (!sock)
dbug_printf ("%s: NO SOCKET: %s (%d) -> %s (%d)\n", proto,
ip4_src, intel16(tcp->srcPort),
ip4_dst, intel16(tcp->dstPort));
else
{
const _tcp_Socket *sk = (const _tcp_Socket*) sock;
if (outbound)
dbug_printf ("%s: %s (%d) -> %s (%d), sock %" ADDR_FMT "\n", proto,
_inet_ntoa(NULL, my_ip_addr), sk->myport,
_inet_ntoa(NULL, sk->hisaddr), sk->hisport,
ADDR_CAST(sock));
else dbug_printf ("%s: %s (%d) -> %s (%d), sock %" ADDR_FMT "\n", proto,
_inet_ntoa(NULL, sk->hisaddr), sk->hisport,
_inet_ntoa(NULL, my_ip_addr), sk->myport,
ADDR_CAST(sock));
}
}
#if defined(USE_IPV6)
static __inline void ip6_addr_type (char *ret, const void *addr)
{
if (IN6_IS_ADDR_UNSPECIFIED(addr))
strcpy (ret, "(anycast) ");
else if (IN6_IS_ADDR_MULTICAST(addr))
strcpy (ret, "(multicast) ");
else if (IN6_IS_ADDR_MC_NODELOCAL(addr))
strcpy (ret, "(MC node) ");
else if (IN6_IS_ADDR_MC_LINKLOCAL(addr))
strcpy (ret, "(MC link) ");
else if (IN6_IS_ADDR_MC_SITELOCAL(addr))
strcpy (ret, "(MC site) ");
else if (IN6_IS_ADDR_MC_GLOBAL(addr))
strcpy (ret, "(MC global) ");
else if (IN6_IS_ADDR_V4MAPPED(addr))
strcpy (ret, "(ip4-mapped) ");
else if (IN6_IS_ADDR_V4COMPAT(addr))
strcpy (ret, "(ip4-compat) ");
else if (IN6_IS_ADDR_LOOPBACK(addr))
strcpy (ret, "(loopback) ");
else *ret = '\0';
}
static void dump_addr6_port (const char *proto,
const void *sock,
const in6_Header *ip)
{
const tcp_Header *tcp = (const tcp_Header*) (ip + 1);
const _tcp_Socket *sk = (const _tcp_Socket*) sock;
if (!sock)
dbug_printf ("%s: NO SOCKET: %s (%d) -> %s (%d)\n", proto,
ip6_src, intel16(tcp->srcPort),
ip6_dst, intel16(tcp->dstPort));
else if (outbound)
dbug_printf ("%s: %s (%d) -> %s (%d)\n", proto,
_inet6_ntoa(&in6addr_my_ip), sk->myport,
_inet6_ntoa(sk->his6addr), sk->hisport);
else dbug_printf ("%s: %s (%d) -> %s (%d)\n", proto,
_inet6_ntoa(sk->his6addr), sk->hisport,
_inet6_ntoa(&in6addr_my_ip), sk->myport);
}
#endif
/*----------------------------------------------------------------------*/
static void link_head_dump (const union link_Packet *pkt)
{
const struct eth_Header *eth;
const char *src_name, *dst_name;
WORD type;
if (_pktdevclass == PDCLASS_TOKEN || _pktdevclass == PDCLASS_TOKEN_RIF)
{
const struct tok_Header *tok = &pkt->tok.head;
dbug_printf ("TR: destin %s, AC %02X, FC %02X\n"
" source %s, DSAP %02X, SSAP %02X, Ctrl %02X\n"
" type %s (%04X)\n",
MAC_address (tok->destination), tok->accessCtrl, tok->frameCtrl,
MAC_address (tok->source), tok->DSAP, tok->SSAP, tok->ctrl,
link_protocol(tok->type), intel16(tok->type));
return;
}
if (_pktdevclass == PDCLASS_FDDI)
{
const struct fddi_Header *fddi = &pkt->fddi.head;
dbug_printf ("FDDI: destin %s, FC %02X\n"
" source %s, DSAP %02X, SSAP %02X, Ctrl %02X\n"
" type %s (%04X)\n",
MAC_address (fddi->destination), fddi->frameCtrl,
MAC_address (fddi->source), fddi->DSAP, fddi->SSAP,
fddi->ctrl, link_protocol(fddi->type), intel16(fddi->type));
return;
}
if (_pktdevclass == PDCLASS_ARCNET)
{
static const struct search_list arcnet_protos[] = {
{ ARCNET_DP_BOOT, "DataPoint Boot" },
{ ARCNET_DP_MOUNT, "DataPoint Mount" },
{ ARCNET_PL_BEACON, "PowerLAN Beacon" },
{ ARCNET_PL_BEACON2, "PowerLAN Beacon2" },
{ ARCNET_DIAG, "Diagnose" },
{ ARCNET_IP6, "IPv6" },
{ ARCNET_BACNET, "BACNET" },
{ ARCNET_IP_1201, "IPv4 (RFC-1201)" },
{ ARCNET_ARP_1201, "ARP (RFC-1201)" },
{ ARCNET_RARP_1201, "RARP (RFC-1201)" },
{ ARCNET_ATALK, "AppleTalk" },
{ ARCNET_ETHER, "Raw Ether" }, /* MS LAN-Man/WfWg */
{ ARCNET_NOVELL, "Novell Encap" },
{ ARCNET_IP_1051, "IPv4 (RFC-1051)" },
{ ARCNET_ARP_1051, "ARP (RFC-1051)" },
{ ARCNET_BANYAN, "Banyan" },
{ ARCNET_IPX, "IPX" },
{ ARCNET_LANSOFT, "LanSoft" }
};
const struct arcnet_Header *arc = &pkt->arc.head;
const char *proto = list_lookup (arc->type, arcnet_protos,
DIM(arcnet_protos));
dbug_printf ("ARCNET:destin %02X, flags %02X, sequence %04X\n"
" source %02X %s\n"
" type %s\n",
arc->destination, arc->flags, arc->sequence,
arc->source, arc->flags == 0xFF ? "Exc-packet" : "",
proto);
return;
}
WATT_ASSERT (_pktdevclass == PDCLASS_ETHER);
eth = &pkt->eth.head;
/* src/dst_names comes from /etc/ethers file
*/
src_name = GetEtherName (ð->source);
dst_name = GetEtherName (ð->destination);
dbug_printf ("ETH: destin %s %s %s\n"
" source %s %s %s\n",
MAC_address (ð->destination),
MAC_is_bcast(ð->destination) ? "(broadcast)" :
MAC_is_mcast(ð->destination) ? "(multicast)" : "",
dst_name ? dst_name : "",
MAC_address (ð->source),
MAC_is_bcast(ð->source) ? "(broadcast)" :
MAC_is_mcast(ð->source) ? "(multicast)" : "",
src_name ? src_name : "");
type = intel16 (eth->type);
if (type == 0xFFFF)
{
dbug_printf (" Novell IPX\n");
}
else if (type < 0x600) /* type is LLC length field */
{
const llc_Header *llc = (const llc_Header*) (eth+1);
unsigned len = type;
dbug_printf (" IEEE 802.3 encap (LLC: DSAP %02X, SSAP %02X, len %u)\n",
llc->DSAP, llc->SSAP, len);
}
else
dbug_printf (" type %s (%04X)\n", link_protocol(eth->type), type);
}
/*----------------------------------------------------------------------*/
static int arp_dump (const arp_Header *arp)
{
_inet_ntoa (ip4_src, intel(arp->srcIPAddr));
_inet_ntoa (ip4_dst, intel(arp->dstIPAddr));
return dbug_printf ("ARP: %s (%d), hw %04X, type %04X\n"
" %s (%s) -> %s (%s)\n",
arp_opcode(arp->opcode), intel16(arp->opcode),
arp->hwType, intel16(arp->protType),
MAC_address (&arp->srcEthAddr), ip4_src,
MAC_address (&arp->dstEthAddr), ip4_dst);
}
/*----------------------------------------------------------------------*/
static int rarp_dump (const rarp_Header *rarp)
{
_inet_ntoa (ip4_src, intel(rarp->srcIPAddr));
_inet_ntoa (ip4_dst, intel(rarp->dstIPAddr));
return dbug_printf ("RARP: %s (%d), hw %04X, type %04X\n"
" %s (%s) -> %s (%s)\n",
arp_opcode(rarp->opcode), intel16(rarp->opcode),
rarp->hwType, intel16(rarp->protType),
MAC_address (&rarp->srcEthAddr), ip4_src,
MAC_address (&rarp->dstEthAddr), ip4_dst);
}
/*----------------------------------------------------------------------*/
static void ip4_dump (const in_Header *ip)
{
WORD ihl, flg;
DWORD ofs;
int opt_len;
const char *chk_ok;
ofs = intel16 (ip->frag_ofs);
flg = (WORD) (ofs & ~IP_OFFMASK);
ofs = (ofs & IP_OFFMASK) << 3;
frag_status = 0;
if (flg & IP_MF)
{
frag_status |= IS_FRAG;
/* This is 1st fragment. */
if (ofs == 0)
frag_status |= IS_FIRST;
}
else if (ofs)
frag_status = (IS_FRAG | IS_LAST); /* last fragment */
if (!debug.IP)
return;
dbug_printf ("IP4: %s -> %s\n", ip4_src, ip4_dst);
ihl = in_GetHdrLen (ip);
if (ihl < sizeof(*ip))
{
dbug_write (" Bad header\n");
return;
}
dbug_printf (" IHL %u, ver %u, tos%s, len %u,",
ihl, (BYTE)ip->ver, type_of_service(ip->tos),
intel16(ip->length));
chk_ok = do_check_sum (ip->checksum, ip, ihl);
dbug_printf (" ttl %u, prot %s, chksum %s\n"
" id %04X, ofs %lu",
(BYTE)ip->ttl, ip4_protocol(ip->proto), chk_ok,
intel16(ip->identification), (u_long)ofs);
dbug_flush();
if (flg & IP_CE)
dbug_write (", CE");
if (flg & IP_DF)
dbug_write (", DF");
if (frag_status)
{
if (frag_status & IS_FIRST)
dbug_write (", 1st frag");
if (frag_status & IS_LAST)
dbug_write (", last frag");
}
dbug_putc ('\n');
opt_len = ihl - sizeof(*ip);
if (opt_len > 0)
dump_ip_opt (ip + 1, opt_len);
}
/*----------------------------------------------------------------------*/
static int ip4_orig_dump (const in_Header *this_ip,
const in_Header *orig_ip, int icmp_len)
{
WORD ihl;
DWORD ofs;
if (icmp_len < SIZEOF(*orig_ip))
{
dbug_write (" Too little of original IP-header\n");
return dump_data (this_ip, intel16(this_ip->length));
}
ihl = in_GetHdrLen (orig_ip);
if (ihl < sizeof(*orig_ip))
{
dbug_write (" Bad orig. header\n");
return dump_data (this_ip, intel16(this_ip->length));
}
dbug_printf (" Orig IP: %s -> %s\n",
_inet_ntoa(NULL, intel(orig_ip->source)),
_inet_ntoa(NULL, intel(orig_ip->destination)));
ofs = (intel16(orig_ip->frag_ofs) & IP_OFFMASK) << 3;
dbug_printf (" IHL %u, ver %u, tos%s, len %u,"
" ttl %u, prot %s\n"
" chksum %s, id %04X, ofs %lu\n",
ihl, (BYTE)orig_ip->ver, type_of_service(orig_ip->tos),
intel16(orig_ip->length), (BYTE)orig_ip->ttl,
ip4_protocol (orig_ip->proto),
do_check_sum (orig_ip->checksum, orig_ip, ihl),
intel16 (orig_ip->identification), (u_long)ofs);
if (icmp_len <= ihl)
dbug_write (" No transport header present\n");
else if (orig_ip->proto == TCP_PROTO && icmp_len - ihl >= 4)
{
const tcp_Header *tcp = (const tcp_Header*) ((const BYTE*)orig_ip + ihl);
dbug_printf (" TCP: port %u -> %u\n",
intel16(tcp->srcPort), intel16(tcp->dstPort));
}
else if (orig_ip->proto == UDP_PROTO && icmp_len - ihl >= 4)
{
const udp_Header *udp = (const udp_Header*) ((const BYTE*)orig_ip + ihl);
dbug_printf (" UDP: port %u -> %u\n",
intel16(udp->srcPort), intel16(udp->dstPort));
}
if (icmp_len > 0) /* consider only original IP as payload */
return dump_data (orig_ip, icmp_len);
return (1);
}
/*----------------------------------------------------------------------*/
static int icmp4_dump (const in_Header *ip)
{
WORD len = in_GetHdrLen (ip);
const ICMP_PKT *icmp = (const ICMP_PKT*) ((const BYTE*)ip + len);
const char *type_str, *chk_ok;
char buf [200] = "";
char *p = buf;
int type, code;
len = intel16 (ip->length) - len; /* ICMP length */
type = icmp->unused.type;
code = icmp->unused.code;
if (len < sizeof(struct ICMP_info))
{
dbug_write ("ICMP: Short header\n");
return (1);
}
/* Simply dump the remaining data if this is a fragment.
*/
if (frag_status)
return dump_data (icmp, len);
switch (type)
{
case ICMP_UNREACH:
type_str = icmp_type_str [ICMP_UNREACH];
if (code < DIM(icmp_unreach_str))
sprintf (buf, "%s: %s", type_str, icmp_unreach_str[code]);
else sprintf (buf, "%s: code %d", type_str, code);
break;
case ICMP_TIMXCEED:
type_str = icmp_type_str [ICMP_TIMXCEED];
if (code < DIM(icmp_exceed_str))
p += sprintf (p, "%s: %s", type_str, icmp_exceed_str[code]);
else p += sprintf (buf, "%s: code %d", type_str, code);
break;
case ICMP_REDIRECT:
if (code < DIM(icmp_redirect_str))
strcpy (buf, icmp_redirect_str[code]);
else sprintf (buf, "Redirect; code %d", code);
break;
case ICMP_PARAMPROB:
if (code)
sprintf (buf, "Param prob code %d", code);
else sprintf (buf, "Param prob at %d", icmp->pointer.pointer);
break;
case ICMP_MASKREQ:
case ICMP_MASKREPLY:
sprintf (buf, "ICMP %s: %s", icmp_type_str[type],
_inet_ntoa(NULL, intel(icmp->mask.mask)));
break;
#if 0
/** \todo Handle debugging of these
*/
case ICMP_ROUTERADVERT:
case ICMP_ROUTERSOLICIT:
case ICMP_TSTAMP:
case ICMP_TSTAMPREPLY:
case ICMP_IREQ:
case ICMP_IREQREPLY:
#endif
default:
sprintf (buf, "%s (%d), code %d",
type < DIM(icmp_type_str) ?
icmp_type_str[type] : "Unknown", type, code);
}
chk_ok = do_check_sum (icmp->unused.checksum, icmp, len);
dbug_printf ("ICMP: %s -> %s\n"
" %s, chksum %s\n",
ip4_src, ip4_dst, buf, chk_ok);
if (type == ICMP_UNREACH || type == ICMP_PARAMPROB)
return ip4_orig_dump (ip, &icmp->ip.ip, len - 8);
if (type == ICMP_TIMXCEED)
return ip4_orig_dump (ip, &icmp->unused.ip, len - 8);
return dump_data (icmp, len);
}
/*----------------------------------------------------------------------*/
#if defined(USE_MULTICAST)
static int igmp0_dump (const IGMPv0_packet *igmp, WORD len)
{
ARGSUSED (len);
return dbug_printf ("IGMP: unfinished IGMP v0 parser. addr %s\n",
_inet_ntoa(NULL, intel(igmp->address)));
}
static int igmp1_dump (const IGMPv1_packet *igmp, WORD len)
{
static const struct search_list types[] = {
{ IGMPv1_QUERY, "Query" },
{ IGMPv1_REPORT, "Report" },
{ IGMPv1_DVMRP, "DVMRP" }
};
return dbug_printf ("IGMP: %s, ver %d, chksum %s, addr %s\n",
list_lookup(igmp->type, types, DIM(types)),
igmp->version, do_check_sum(igmp->checksum, igmp, len),
_inet_ntoa(NULL, intel(igmp->address)));