mirror of
https://github.com/reactos/reactos.git
synced 2025-04-21 04:37:15 +00:00
- Manage TCP ports using port set and dynamically assign TCP port number if sin_port == 0.
svn path=/trunk/; revision=12018
This commit is contained in:
parent
84d1a39be9
commit
21fc8bafa5
5 changed files with 52 additions and 3 deletions
|
@ -51,7 +51,25 @@ ULONG AllocateAnyPort( PPORT_SET PortSet ) {
|
|||
ExAcquireFastMutex( &PortSet->Mutex );
|
||||
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, 0 );
|
||||
if( AllocatedPort != (ULONG)-1 ) {
|
||||
RtlSetBits( &PortSet->ProtoBitmap, AllocatedPort, 1 );
|
||||
RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
|
||||
AllocatedPort += PortSet->StartingPort;
|
||||
}
|
||||
ExReleaseFastMutex( &PortSet->Mutex );
|
||||
|
||||
return AllocatedPort;
|
||||
}
|
||||
|
||||
ULONG AllocatePortFromRange( PPORT_SET PortSet, ULONG Lowest, ULONG Highest ) {
|
||||
ULONG AllocatedPort;
|
||||
|
||||
Lowest -= PortSet->StartingPort;
|
||||
Highest -= PortSet->StartingPort;
|
||||
|
||||
ExAcquireFastMutex( &PortSet->Mutex );
|
||||
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Lowest );
|
||||
if( AllocatedPort != (ULONG)-1 && AllocatedPort >= Lowest &&
|
||||
AllocatedPort <= Highest) {
|
||||
RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
|
||||
AllocatedPort += PortSet->StartingPort;
|
||||
}
|
||||
ExReleaseFastMutex( &PortSet->Mutex );
|
||||
|
|
|
@ -17,6 +17,7 @@ LIST_ENTRY SignalledConnections;
|
|||
LIST_ENTRY SleepingThreadsList;
|
||||
FAST_MUTEX SleepingThreadsLock;
|
||||
RECURSIVE_MUTEX TCPLock;
|
||||
PORT_SET TCPPorts;
|
||||
|
||||
static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
|
||||
ULONG NewState ) {
|
||||
|
@ -268,6 +269,8 @@ NTSTATUS TCPStartup(VOID)
|
|||
InitializeListHead( &SleepingThreadsList );
|
||||
InitializeListHead( &SignalledConnections );
|
||||
|
||||
PortsStartup( &TCPPorts, 1, 0xfffe );
|
||||
|
||||
RegisterOskitTCPEventHandlers( &EventHandlers );
|
||||
InitOskitTCP();
|
||||
|
||||
|
@ -308,6 +311,8 @@ NTSTATUS TCPShutdown(VOID)
|
|||
|
||||
DeinitOskitTCP();
|
||||
|
||||
PortsShutdown( &TCPPorts );
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -620,4 +625,15 @@ VOID TCPTimeout(VOID) {
|
|||
TcpipRecursiveMutexLeave( &TCPLock );
|
||||
}
|
||||
|
||||
UINT TCPAllocatePort( UINT HintPort ) {
|
||||
if( HintPort ) {
|
||||
if( AllocatePort( &TCPPorts, HintPort ) ) return HintPort;
|
||||
else return (UINT)-1;
|
||||
} else return AllocatePortFromRange( &TCPPorts, 1024, 5000 );
|
||||
}
|
||||
|
||||
VOID TCPFreePort( UINT Port ) {
|
||||
DeallocatePort( &TCPPorts, Port );
|
||||
}
|
||||
|
||||
/* EOF */
|
||||
|
|
|
@ -26,5 +26,6 @@ VOID PortsShutdown( PPORT_SET PortSet );
|
|||
VOID DeallocatePort( PPORT_SET PortSet, ULONG Port );
|
||||
BOOLEAN AllocatePort( PPORT_SET PortSet, ULONG Port );
|
||||
ULONG AllocateAnyPort( PPORT_SET PortSet );
|
||||
ULONG AllocatePortFromRange( PPORT_SET PortSet, ULONG Lowest, ULONG Highest );
|
||||
|
||||
#endif/*_TCPIP_PORTS_H*/
|
||||
|
|
|
@ -151,6 +151,10 @@ NTSTATUS TCPTranslateError( int OskitError );
|
|||
|
||||
VOID TCPTimeout();
|
||||
|
||||
UINT TCPAllocatePort( UINT HintPort );
|
||||
|
||||
VOID TCPFreePort( UINT Port );
|
||||
|
||||
NTSTATUS TCPStartup(
|
||||
VOID);
|
||||
|
||||
|
|
|
@ -268,7 +268,7 @@ NTSTATUS FileOpenAddress(
|
|||
switch (Protocol) {
|
||||
case IPPROTO_TCP:
|
||||
/* FIXME: If specified port is 0, a port is chosen dynamically */
|
||||
AddrFile->Port = Address->Address[0].Address[0].sin_port;
|
||||
AddrFile->Port = TCPAllocatePort(Address->Address[0].Address[0].sin_port);
|
||||
AddrFile->Send = NULL; /* TCPSendData */
|
||||
break;
|
||||
|
||||
|
@ -346,7 +346,17 @@ NTSTATUS FileCloseAddress(
|
|||
AF_CLR_VALID(AddrFile);
|
||||
|
||||
TcpipReleaseSpinLock(&AddrFile->Lock, OldIrql);
|
||||
|
||||
/* Protocol specific handling */
|
||||
switch (AddrFile->Protocol) {
|
||||
case IPPROTO_TCP:
|
||||
TCPFreePort( AddrFile->Port );
|
||||
break;
|
||||
|
||||
case IPPROTO_UDP:
|
||||
UDPFreePort( AddrFile->Port );
|
||||
break;
|
||||
}
|
||||
|
||||
DeleteAddress(AddrFile);
|
||||
|
||||
|
|
Loading…
Reference in a new issue