merge ROS Shell without integrated explorer part into trunk
[reactos.git] / reactos / drivers / lib / 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 #ifdef 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 static VOID DisplayTCPHeader(
58 PCHAR Header,
59 UINT Length)
60 {
61 /* FIXME: IPv4 only */
62 PIPv4_HEADER IPHeader = (PIPv4_HEADER)Header;
63 PTCPv4_HEADER TCPHeader;
64
65 if (IPHeader->Protocol != IPPROTO_TCP) {
66 DbgPrint("This is not a TCP datagram. Protocol is %d\n", IPHeader->Protocol);
67 return;
68 }
69
70 TCPHeader = (PTCPv4_HEADER)((PCHAR)IPHeader + (IPHeader->VerIHL & 0x0F) * 4);
71
72 DbgPrint("TCP header:\n");
73 DbgPrint(" SourcePort: %d\n", WN2H(TCPHeader->SourcePort));
74 DbgPrint(" DestinationPort: %d\n", WN2H(TCPHeader->DestinationPort));
75 DbgPrint(" SequenceNumber: 0x%x\n", DN2H(TCPHeader->SequenceNumber));
76 DbgPrint(" AckNumber: 0x%x\n", DN2H(TCPHeader->AckNumber));
77 DbgPrint(" DataOffset: 0x%x (0x%x) 32-bit words\n", TCPHeader->DataOffset, TCPHeader->DataOffset >> 4);
78 DbgPrint(" Flags: 0x%x (0x%x)\n", TCPHeader->Flags, TCPHeader->Flags & 0x3F);
79 if ((TCPHeader->Flags & TCP_URG) > 0) DbgPrint(" TCP_URG - Urgent Pointer field significant\n");
80 if ((TCPHeader->Flags & TCP_ACK) > 0) DbgPrint(" TCP_ACK - Acknowledgement field significant\n");
81 if ((TCPHeader->Flags & TCP_PSH) > 0) DbgPrint(" TCP_PSH - Push Function\n");
82 if ((TCPHeader->Flags & TCP_RST) > 0) DbgPrint(" TCP_RST - Reset the connection\n");
83 if ((TCPHeader->Flags & TCP_SYN) > 0) DbgPrint(" TCP_SYN - Synchronize sequence numbers\n");
84 if ((TCPHeader->Flags & TCP_FIN) > 0) DbgPrint(" TCP_FIN - No more data from sender\n");
85 DbgPrint(" Window: 0x%x\n", WN2H(TCPHeader->Window));
86 DbgPrint(" Checksum: 0x%x\n", WN2H(TCPHeader->Checksum));
87 DbgPrint(" Urgent: 0x%x\n", WN2H(TCPHeader->Urgent));
88 }
89
90
91 VOID DisplayTCPPacket(
92 PIP_PACKET IPPacket)
93 {
94 UINT Length;
95 PCHAR Buffer;
96
97 if ((DebugTraceLevel & (DEBUG_PBUFFER | DEBUG_TCP)) != (DEBUG_PBUFFER | DEBUG_TCP)) {
98 return;
99 }
100
101 if (!IPPacket) {
102 TI_DbgPrint(MIN_TRACE, ("Cannot display null packet.\n"));
103 return;
104 }
105
106 DisplayIPPacket(IPPacket);
107
108 TI_DbgPrint(MIN_TRACE, ("IPPacket is at (0x%X).\n", IPPacket));
109 TI_DbgPrint(MIN_TRACE, ("Header buffer is at (0x%X).\n", IPPacket->Header));
110 TI_DbgPrint(MIN_TRACE, ("Header size is (%d).\n", IPPacket->HeaderSize));
111 TI_DbgPrint(MIN_TRACE, ("TotalSize (%d).\n", IPPacket->TotalSize));
112 TI_DbgPrint(MIN_TRACE, ("ContigSize (%d).\n", IPPacket->ContigSize));
113 TI_DbgPrint(MIN_TRACE, ("NdisPacket (0x%X).\n", IPPacket->NdisPacket));
114
115 if (IPPacket->NdisPacket) {
116 NdisQueryPacket(IPPacket->NdisPacket, NULL, NULL, NULL, &Length);
117 Length -= MaxLLHeaderSize;
118 Buffer = exAllocatePool(NonPagedPool, Length);
119 Length = CopyPacketToBuffer(Buffer, IPPacket->NdisPacket, MaxLLHeaderSize, Length);
120 DisplayTCPHeader(Buffer, Length);
121 exFreePool(Buffer);
122 } else {
123 Buffer = IPPacket->Header;
124 Length = IPPacket->ContigSize;
125 DisplayTCPHeader(Buffer, Length);
126 }
127 }
128 #endif
129
130 VOID DisplayIPPacket(
131 PIP_PACKET IPPacket)
132 {
133 #ifdef DBG
134 PCHAR p;
135 UINT Length;
136 PNDIS_BUFFER Buffer;
137 PNDIS_BUFFER NextBuffer;
138 PCHAR CharBuffer;
139
140 if ((DebugTraceLevel & (DEBUG_PBUFFER | DEBUG_IP)) != (DEBUG_PBUFFER | DEBUG_IP)) {
141 return;
142 }
143
144 if (!IPPacket) {
145 TI_DbgPrint(MIN_TRACE, ("Cannot display null packet.\n"));
146 return;
147 }
148
149 TI_DbgPrint(MIN_TRACE, ("IPPacket is at (0x%X).\n", IPPacket));
150 TI_DbgPrint(MIN_TRACE, ("Header buffer is at (0x%X).\n", IPPacket->Header));
151 TI_DbgPrint(MIN_TRACE, ("Header size is (%d).\n", IPPacket->HeaderSize));
152 TI_DbgPrint(MIN_TRACE, ("TotalSize (%d).\n", IPPacket->TotalSize));
153 TI_DbgPrint(MIN_TRACE, ("ContigSize (%d).\n", IPPacket->ContigSize));
154 TI_DbgPrint(MIN_TRACE, ("NdisPacket (0x%X).\n", IPPacket->NdisPacket));
155
156 if (IPPacket->NdisPacket) {
157 NdisQueryPacket(IPPacket->NdisPacket, NULL, NULL, &Buffer, NULL);
158 for (; Buffer != NULL; Buffer = NextBuffer) {
159 NdisGetNextBuffer(Buffer, &NextBuffer);
160 NdisQueryBuffer(Buffer, (PVOID)&p, &Length);
161 //OskitDumpBuffer( p, Length );
162 }
163 } else {
164 p = IPPacket->Header;
165 Length = IPPacket->ContigSize;
166 //OskitDumpBuffer( p, Length );
167 }
168
169 CharBuffer = IPPacket->Header;
170 Length = IPPacket->ContigSize;
171 DisplayIPHeader(CharBuffer, Length);
172 #endif
173 }
174