2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: transport/tcp/event.c
5 * PURPOSE: Transmission Control Protocol -- Events from oskittcp
6 * PROGRAMMERS: Art Yerkes
8 * CSH 01/08-2000 Created
13 int TCPSocketState(void *ClientData
,
15 void *WhichConnection
,
17 PCONNECTION_ENDPOINT Connection
= WhichConnection
;
21 ASSERT_LOCKED(&TCPLock
);
23 TI_DbgPrint(MID_TRACE
,("Flags: %c%c%c%c\n",
24 NewState
& SEL_CONNECT
? 'C' : 'c',
25 NewState
& SEL_READ
? 'R' : 'r',
26 NewState
& SEL_FIN
? 'F' : 'f',
27 NewState
& SEL_ACCEPT
? 'A' : 'a'));
29 TI_DbgPrint(DEBUG_TCP
,("Called: NewState %x (Conn %x) (Change %x)\n",
31 Connection
? Connection
->SignalState
^ NewState
:
35 TI_DbgPrint(DEBUG_TCP
,("Socket closing.\n"));
36 Connection
= FileFindConnectionByContext( WhichSocket
);
40 TI_DbgPrint(DEBUG_TCP
,("Found socket %x\n", Connection
));
43 OldState
= Connection
->SignalState
;
45 Connection
->SignalState
|= NewState
;
47 NewState
= HandleSignalledConnection(Connection
);
49 KeAcquireSpinLock(&SignalledConnectionsLock
, &OldIrql
);
50 if ((NewState
== 0 || NewState
== SEL_FIN
) &&
51 (OldState
!= 0 && OldState
!= SEL_FIN
))
53 RemoveEntryList(&Connection
->SignalList
);
55 else if (NewState
!= 0 && NewState
!= SEL_FIN
)
57 InsertTailList(&SignalledConnectionsList
, &Connection
->SignalList
);
59 KeReleaseSpinLock(&SignalledConnectionsLock
, OldIrql
);
64 void TCPPacketSendComplete( PVOID Context
,
65 PNDIS_PACKET NdisPacket
,
66 NDIS_STATUS NdisStatus
) {
67 TI_DbgPrint(DEBUG_TCP
,("called %x\n", NdisPacket
));
68 FreeNdisPacket(NdisPacket
);
69 TI_DbgPrint(DEBUG_TCP
,("done\n"));
72 #define STRINGIFY(x) #x
74 int TCPPacketSend(void *ClientData
, OSK_PCHAR data
, OSK_UINT len
) {
75 NDIS_STATUS NdisStatus
;
76 PNEIGHBOR_CACHE_ENTRY NCE
;
77 IP_PACKET Packet
= { 0 };
78 IP_ADDRESS RemoteAddress
, LocalAddress
;
81 ASSERT_LOCKED(&TCPLock
);
83 if( *data
== 0x45 ) { /* IPv4 */
84 Header
= (PIPv4_HEADER
)data
;
85 LocalAddress
.Type
= IP_ADDRESS_V4
;
86 LocalAddress
.Address
.IPv4Address
= Header
->SrcAddr
;
87 RemoteAddress
.Type
= IP_ADDRESS_V4
;
88 RemoteAddress
.Address
.IPv4Address
= Header
->DstAddr
;
90 TI_DbgPrint(MIN_TRACE
,("Outgoing packet is not IPv4\n"));
91 OskitDumpBuffer( data
, len
);
95 if(!(NCE
= RouteGetRouteToDestination( &RemoteAddress
))) {
96 TI_DbgPrint(MIN_TRACE
,("No route to %s\n", A2S(&RemoteAddress
)));
97 return OSK_EADDRNOTAVAIL
;
100 NdisStatus
= AllocatePacketWithBuffer( &Packet
.NdisPacket
, NULL
, len
);
102 if (NdisStatus
!= NDIS_STATUS_SUCCESS
) {
103 TI_DbgPrint(DEBUG_TCP
, ("Error from NDIS: %08x\n", NdisStatus
));
107 GetDataPtr( Packet
.NdisPacket
, 0,
108 (PCHAR
*)&Packet
.Header
, &Packet
.ContigSize
);
110 RtlCopyMemory( Packet
.Header
, data
, len
);
112 Packet
.HeaderSize
= sizeof(IPv4_HEADER
);
113 Packet
.TotalSize
= len
;
114 Packet
.SrcAddr
= LocalAddress
;
115 Packet
.DstAddr
= RemoteAddress
;
117 if (!NT_SUCCESS(IPSendDatagram( &Packet
, NCE
, TCPPacketSendComplete
, NULL
)))
119 FreeNdisPacket(Packet
.NdisPacket
);
126 /* Memory management routines
128 * By far the most requests for memory are either for 128 or 2048 byte blocks,
129 * so we want to satisfy those from lookaside lists. Unfortunately, the
130 * TCPFree() function doesn't pass the size of the block to be freed, so we
131 * need to keep track of it ourselves. We do it by prepending each block with
132 * 4 bytes, indicating if this is a 'L'arge (2048), 'S'mall (128) or 'O'ther
136 /* Set to some non-zero value to get a profile of memory allocation sizes */
137 #define MEM_PROFILE 0
139 #define SMALL_SIZE 128
140 #define LARGE_SIZE 2048
142 #define SIGNATURE_LARGE 'LLLL'
143 #define SIGNATURE_SMALL 'SSSS'
144 #define SIGNATURE_OTHER 'OOOO'
145 #define TCP_TAG ' PCT'
147 static NPAGED_LOOKASIDE_LIST LargeLookasideList
;
148 static NPAGED_LOOKASIDE_LIST SmallLookasideList
;
151 TCPMemStartup( void )
153 ExInitializeNPagedLookasideList( &LargeLookasideList
,
157 LARGE_SIZE
+ sizeof( ULONG
),
160 ExInitializeNPagedLookasideList( &SmallLookasideList
,
164 SMALL_SIZE
+ sizeof( ULONG
),
168 return STATUS_SUCCESS
;
171 void *TCPMalloc( void *ClientData
,
172 OSK_UINT Bytes
, OSK_PCHAR File
, OSK_UINT Line
) {
176 ASSERT_LOCKED(&TCPLock
);
179 static OSK_UINT
*Sizes
= NULL
, *Counts
= NULL
, ArrayAllocated
= 0;
180 static OSK_UINT ArrayUsed
= 0, AllocationCount
= 0;
181 OSK_UINT i
, NewSize
, *NewArray
;
185 for ( i
= 0; i
< ArrayUsed
&& ! Found
; i
++ ) {
186 Found
= ( Sizes
[i
] == Bytes
);
192 if ( ArrayAllocated
<= ArrayUsed
) {
193 NewSize
= ( 0 == ArrayAllocated
? 16 : 2 * ArrayAllocated
);
194 NewArray
= exAllocatePool( NonPagedPool
, 2 * NewSize
* sizeof( OSK_UINT
) );
195 if ( NULL
!= NewArray
) {
196 if ( 0 != ArrayAllocated
) {
197 memcpy( NewArray
, Sizes
,
198 ArrayAllocated
* sizeof( OSK_UINT
) );
200 memcpy( NewArray
+ NewSize
, Counts
,
201 ArrayAllocated
* sizeof( OSK_UINT
) );
202 exFreePool( Counts
);
205 Counts
= NewArray
+ NewSize
;
206 ArrayAllocated
= NewSize
;
207 } else if ( 0 != ArrayAllocated
) {
209 exFreePool( Counts
);
213 if ( ArrayUsed
< ArrayAllocated
) {
214 Sizes
[ArrayUsed
] = Bytes
;
215 Counts
[ArrayUsed
] = 1;
220 if ( 0 == (++AllocationCount
% MEM_PROFILE
) ) {
221 TI_DbgPrint(DEBUG_TCP
, ("Memory allocation size profile:\n"));
222 for ( i
= 0; i
< ArrayUsed
; i
++ ) {
223 TI_DbgPrint(DEBUG_TCP
,
224 ("Size %4u Count %5u\n", Sizes
[i
], Counts
[i
]));
226 TI_DbgPrint(DEBUG_TCP
, ("End of memory allocation size profile\n"));
228 #endif /* MEM_PROFILE */
230 if ( SMALL_SIZE
== Bytes
) {
231 v
= ExAllocateFromNPagedLookasideList( &SmallLookasideList
);
232 Signature
= SIGNATURE_SMALL
;
233 } else if ( LARGE_SIZE
== Bytes
) {
234 v
= ExAllocateFromNPagedLookasideList( &LargeLookasideList
);
235 Signature
= SIGNATURE_LARGE
;
237 v
= ExAllocatePool( NonPagedPool
, Bytes
+ sizeof(ULONG
) );
238 Signature
= SIGNATURE_OTHER
;
241 *((ULONG
*) v
) = Signature
;
242 v
= (void *)((char *) v
+ sizeof(ULONG
));
243 TrackWithTag( FOURCC('f','b','s','d'), v
, (PCHAR
)File
, Line
);
249 void TCPFree( void *ClientData
,
250 void *data
, OSK_PCHAR File
, OSK_UINT Line
) {
253 ASSERT_LOCKED(&TCPLock
);
255 UntrackFL( (PCHAR
)File
, Line
, data
, FOURCC('f','b','s','d') );
256 data
= (void *)((char *) data
- sizeof(ULONG
));
257 Signature
= *((ULONG
*) data
);
258 if ( SIGNATURE_SMALL
== Signature
) {
259 ExFreeToNPagedLookasideList( &SmallLookasideList
, data
);
260 } else if ( SIGNATURE_LARGE
== Signature
) {
261 ExFreeToNPagedLookasideList( &LargeLookasideList
, data
);
262 } else if ( SIGNATURE_OTHER
== Signature
) {
270 TCPMemShutdown( void )
272 ExDeleteNPagedLookasideList( &SmallLookasideList
);
273 ExDeleteNPagedLookasideList( &LargeLookasideList
);