mirror of
https://github.com/reactos/reactos.git
synced 2025-04-21 20:50:29 +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 );
|
ExAcquireFastMutex( &PortSet->Mutex );
|
||||||
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, 0 );
|
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, 0 );
|
||||||
if( AllocatedPort != (ULONG)-1 ) {
|
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;
|
AllocatedPort += PortSet->StartingPort;
|
||||||
}
|
}
|
||||||
ExReleaseFastMutex( &PortSet->Mutex );
|
ExReleaseFastMutex( &PortSet->Mutex );
|
||||||
|
|
|
@ -17,6 +17,7 @@ LIST_ENTRY SignalledConnections;
|
||||||
LIST_ENTRY SleepingThreadsList;
|
LIST_ENTRY SleepingThreadsList;
|
||||||
FAST_MUTEX SleepingThreadsLock;
|
FAST_MUTEX SleepingThreadsLock;
|
||||||
RECURSIVE_MUTEX TCPLock;
|
RECURSIVE_MUTEX TCPLock;
|
||||||
|
PORT_SET TCPPorts;
|
||||||
|
|
||||||
static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
|
static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
|
||||||
ULONG NewState ) {
|
ULONG NewState ) {
|
||||||
|
@ -268,6 +269,8 @@ NTSTATUS TCPStartup(VOID)
|
||||||
InitializeListHead( &SleepingThreadsList );
|
InitializeListHead( &SleepingThreadsList );
|
||||||
InitializeListHead( &SignalledConnections );
|
InitializeListHead( &SignalledConnections );
|
||||||
|
|
||||||
|
PortsStartup( &TCPPorts, 1, 0xfffe );
|
||||||
|
|
||||||
RegisterOskitTCPEventHandlers( &EventHandlers );
|
RegisterOskitTCPEventHandlers( &EventHandlers );
|
||||||
InitOskitTCP();
|
InitOskitTCP();
|
||||||
|
|
||||||
|
@ -308,6 +311,8 @@ NTSTATUS TCPShutdown(VOID)
|
||||||
|
|
||||||
DeinitOskitTCP();
|
DeinitOskitTCP();
|
||||||
|
|
||||||
|
PortsShutdown( &TCPPorts );
|
||||||
|
|
||||||
return STATUS_SUCCESS;
|
return STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -620,4 +625,15 @@ VOID TCPTimeout(VOID) {
|
||||||
TcpipRecursiveMutexLeave( &TCPLock );
|
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 */
|
/* EOF */
|
||||||
|
|
|
@ -26,5 +26,6 @@ VOID PortsShutdown( PPORT_SET PortSet );
|
||||||
VOID DeallocatePort( PPORT_SET PortSet, ULONG Port );
|
VOID DeallocatePort( PPORT_SET PortSet, ULONG Port );
|
||||||
BOOLEAN AllocatePort( PPORT_SET PortSet, ULONG Port );
|
BOOLEAN AllocatePort( PPORT_SET PortSet, ULONG Port );
|
||||||
ULONG AllocateAnyPort( PPORT_SET PortSet );
|
ULONG AllocateAnyPort( PPORT_SET PortSet );
|
||||||
|
ULONG AllocatePortFromRange( PPORT_SET PortSet, ULONG Lowest, ULONG Highest );
|
||||||
|
|
||||||
#endif/*_TCPIP_PORTS_H*/
|
#endif/*_TCPIP_PORTS_H*/
|
||||||
|
|
|
@ -151,6 +151,10 @@ NTSTATUS TCPTranslateError( int OskitError );
|
||||||
|
|
||||||
VOID TCPTimeout();
|
VOID TCPTimeout();
|
||||||
|
|
||||||
|
UINT TCPAllocatePort( UINT HintPort );
|
||||||
|
|
||||||
|
VOID TCPFreePort( UINT Port );
|
||||||
|
|
||||||
NTSTATUS TCPStartup(
|
NTSTATUS TCPStartup(
|
||||||
VOID);
|
VOID);
|
||||||
|
|
||||||
|
|
|
@ -268,7 +268,7 @@ NTSTATUS FileOpenAddress(
|
||||||
switch (Protocol) {
|
switch (Protocol) {
|
||||||
case IPPROTO_TCP:
|
case IPPROTO_TCP:
|
||||||
/* FIXME: If specified port is 0, a port is chosen dynamically */
|
/* 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 */
|
AddrFile->Send = NULL; /* TCPSendData */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -346,7 +346,17 @@ NTSTATUS FileCloseAddress(
|
||||||
AF_CLR_VALID(AddrFile);
|
AF_CLR_VALID(AddrFile);
|
||||||
|
|
||||||
TcpipReleaseSpinLock(&AddrFile->Lock, OldIrql);
|
TcpipReleaseSpinLock(&AddrFile->Lock, OldIrql);
|
||||||
UDPFreePort( AddrFile->Port );
|
|
||||||
|
/* Protocol specific handling */
|
||||||
|
switch (AddrFile->Protocol) {
|
||||||
|
case IPPROTO_TCP:
|
||||||
|
TCPFreePort( AddrFile->Port );
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPPROTO_UDP:
|
||||||
|
UDPFreePort( AddrFile->Port );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
DeleteAddress(AddrFile);
|
DeleteAddress(AddrFile);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue