[TCPIP]
[reactos.git] / reactos / lib / drivers / ip / network / routines.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/routines.c
5 * PURPOSE: Common routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * REVISIONS:
8 * CSH 01/08-2000 Created
9 */
10
11 #include "precomp.h"
12
13 static UINT RandomNumber = 0x12345678;
14
15
16 UINT Random(
17 VOID)
18 /*
19 * FUNCTION: Returns a pseudo random number
20 * RETURNS:
21 * Pseudo random number
22 */
23 {
24 RandomNumber ^= 0x78563412;
25
26 return RandomNumber;
27 }
28
29 #if DBG
30 static VOID DisplayIPHeader(
31 PCHAR Header,
32 UINT Length)
33 {
34 /* FIXME: IPv4 only */
35 PIPv4_HEADER IPHeader = (PIPv4_HEADER)Header;
36
37 DbgPrint("IPv4 header:\n");
38 DbgPrint("VerIHL: 0x%x (version 0x%x, length %d 32-bit words)\n",
39 IPHeader->VerIHL, (IPHeader->VerIHL & 0xF0) >> 4, IPHeader->VerIHL & 0x0F);
40 DbgPrint(" Tos: %d\n", IPHeader->Tos);
41 DbgPrint(" TotalLength: %d\n", WN2H(IPHeader->TotalLength));
42 DbgPrint(" Id: %d\n", WN2H(IPHeader->Id));
43 DbgPrint(" FlagsFragOfs: 0x%x (offset 0x%x)\n", WN2H(IPHeader->FlagsFragOfs), WN2H(IPHeader->FlagsFragOfs) & IPv4_FRAGOFS_MASK);
44 if ((WN2H(IPHeader->FlagsFragOfs) & IPv4_DF_MASK) > 0) DbgPrint(" IPv4_DF - Don't fragment\n");
45 if ((WN2H(IPHeader->FlagsFragOfs) & IPv4_MF_MASK) > 0) DbgPrint(" IPv4_MF - More fragments\n");
46 DbgPrint(" Ttl: %d\n", IPHeader->Ttl);
47 DbgPrint(" Protocol: %d\n", IPHeader->Protocol);
48 DbgPrint(" Checksum: 0x%x\n", WN2H(IPHeader->Checksum));
49 DbgPrint(" SrcAddr: %d.%d.%d.%d\n",
50 ((IPHeader->SrcAddr >> 0) & 0xFF), ((IPHeader->SrcAddr >> 8) & 0xFF),
51 ((IPHeader->SrcAddr >> 16) & 0xFF), ((IPHeader->SrcAddr >> 24) & 0xFF));
52 DbgPrint(" DstAddr: %d.%d.%d.%d\n",
53 ((IPHeader->DstAddr >> 0) & 0xFF), ((IPHeader->DstAddr >> 8) & 0xFF),
54 ((IPHeader->DstAddr >> 16) & 0xFF), ((IPHeader->DstAddr >> 24) & 0xFF));
55 }
56
57 #endif
58
59 VOID DisplayIPPacket(
60 PIP_PACKET IPPacket)
61 {
62 #if DBG
63 PCHAR p;
64 UINT Length;
65 PNDIS_BUFFER Buffer;
66 PNDIS_BUFFER NextBuffer;
67 PCHAR CharBuffer;
68
69 if ((DbgQueryDebugFilterState(DPFLTR_TCPIP_ID, DEBUG_PBUFFER | DPFLTR_MASK) != TRUE) ||
70 (DbgQueryDebugFilterState(DPFLTR_TCPIP_ID, DEBUG_IP | DPFLTR_MASK) != TRUE)) {
71 return;
72 }
73
74 if (!IPPacket) {
75 TI_DbgPrint(MIN_TRACE, ("Cannot display null packet.\n"));
76 return;
77 }
78
79 TI_DbgPrint(MIN_TRACE, ("IPPacket is at (0x%X).\n", IPPacket));
80 TI_DbgPrint(MIN_TRACE, ("Header buffer is at (0x%X).\n", IPPacket->Header));
81 TI_DbgPrint(MIN_TRACE, ("Header size is (%d).\n", IPPacket->HeaderSize));
82 TI_DbgPrint(MIN_TRACE, ("TotalSize (%d).\n", IPPacket->TotalSize));
83 TI_DbgPrint(MIN_TRACE, ("ContigSize (%d).\n", IPPacket->ContigSize));
84 TI_DbgPrint(MIN_TRACE, ("NdisPacket (0x%X).\n", IPPacket->NdisPacket));
85
86 if (IPPacket->NdisPacket) {
87 NdisQueryPacket(IPPacket->NdisPacket, NULL, NULL, &Buffer, NULL);
88 for (; Buffer != NULL; Buffer = NextBuffer) {
89 NdisGetNextBuffer(Buffer, &NextBuffer);
90 NdisQueryBuffer(Buffer, (PVOID)&p, &Length);
91 //OskitDumpBuffer( p, Length );
92 }
93 } else {
94 p = IPPacket->Header;
95 Length = IPPacket->ContigSize;
96 //OskitDumpBuffer( p, Length );
97 }
98
99 CharBuffer = IPPacket->Header;
100 Length = IPPacket->ContigSize;
101 DisplayIPHeader(CharBuffer, Length);
102 #endif
103 }
104