[SERVMAN]
[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 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 ((DbgQueryDebugFilterState(DPFLTR_TCPIP_ID, DEBUG_PBUFFER | DPFLTR_MASK) != TRUE) ||
98 (DbgQueryDebugFilterState(DPFLTR_TCPIP_ID, DEBUG_TCP | DPFLTR_MASK) != TRUE)) {
99 return;
100 }
101
102 if (!IPPacket) {
103 TI_DbgPrint(MIN_TRACE, ("Cannot display null packet.\n"));
104 return;
105 }
106
107 DisplayIPPacket(IPPacket);
108
109 TI_DbgPrint(MIN_TRACE, ("IPPacket is at (0x%X).\n", IPPacket));
110 TI_DbgPrint(MIN_TRACE, ("Header buffer is at (0x%X).\n", IPPacket->Header));
111 TI_DbgPrint(MIN_TRACE, ("Header size is (%d).\n", IPPacket->HeaderSize));
112 TI_DbgPrint(MIN_TRACE, ("TotalSize (%d).\n", IPPacket->TotalSize));
113 TI_DbgPrint(MIN_TRACE, ("ContigSize (%d).\n", IPPacket->ContigSize));
114 TI_DbgPrint(MIN_TRACE, ("NdisPacket (0x%X).\n", IPPacket->NdisPacket));
115
116 if (IPPacket->NdisPacket) {
117 NdisQueryPacket(IPPacket->NdisPacket, NULL, NULL, NULL, &Length);
118 Buffer = ExAllocatePool(NonPagedPool, Length);
119 if (Buffer) {
120 Length = CopyPacketToBuffer(Buffer, IPPacket->NdisPacket, 0, Length);
121 DisplayTCPHeader(Buffer, Length);
122 ExFreePool(Buffer);
123 }
124 } else {
125 Buffer = IPPacket->Header;
126 Length = IPPacket->ContigSize;
127 DisplayTCPHeader(Buffer, Length);
128 }
129 }
130 #endif
131
132 VOID DisplayIPPacket(
133 PIP_PACKET IPPacket)
134 {
135 #if DBG
136 PCHAR p;
137 UINT Length;
138 PNDIS_BUFFER Buffer;
139 PNDIS_BUFFER NextBuffer;
140 PCHAR CharBuffer;
141
142 if ((DbgQueryDebugFilterState(DPFLTR_TCPIP_ID, DEBUG_PBUFFER | DPFLTR_MASK) != TRUE) ||
143 (DbgQueryDebugFilterState(DPFLTR_TCPIP_ID, DEBUG_IP | DPFLTR_MASK) != TRUE)) {
144 return;
145 }
146
147 if (!IPPacket) {
148 TI_DbgPrint(MIN_TRACE, ("Cannot display null packet.\n"));
149 return;
150 }
151
152 TI_DbgPrint(MIN_TRACE, ("IPPacket is at (0x%X).\n", IPPacket));
153 TI_DbgPrint(MIN_TRACE, ("Header buffer is at (0x%X).\n", IPPacket->Header));
154 TI_DbgPrint(MIN_TRACE, ("Header size is (%d).\n", IPPacket->HeaderSize));
155 TI_DbgPrint(MIN_TRACE, ("TotalSize (%d).\n", IPPacket->TotalSize));
156 TI_DbgPrint(MIN_TRACE, ("ContigSize (%d).\n", IPPacket->ContigSize));
157 TI_DbgPrint(MIN_TRACE, ("NdisPacket (0x%X).\n", IPPacket->NdisPacket));
158
159 if (IPPacket->NdisPacket) {
160 NdisQueryPacket(IPPacket->NdisPacket, NULL, NULL, &Buffer, NULL);
161 for (; Buffer != NULL; Buffer = NextBuffer) {
162 NdisGetNextBuffer(Buffer, &NextBuffer);
163 NdisQueryBuffer(Buffer, (PVOID)&p, &Length);
164 //OskitDumpBuffer( p, Length );
165 }
166 } else {
167 p = IPPacket->Header;
168 Length = IPPacket->ContigSize;
169 //OskitDumpBuffer( p, Length );
170 }
171
172 CharBuffer = IPPacket->Header;
173 Length = IPPacket->ContigSize;
174 DisplayIPHeader(CharBuffer, Length);
175 #endif
176 }
177