Commit in reactos/drivers on MAIN
lib/ip/network/ports.c+19-11.1 -> 1.2
lib/ip/transport/tcp/tcp.c+161.13 -> 1.14
net/tcpip/include/ports.h+11.1 -> 1.2
                 /tcp.h+41.14 -> 1.15
net/tcpip/tcpip/fileobjs.c+12-21.27 -> 1.28
+52-3
5 modified files
- Manage TCP ports using port set and dynamically assign TCP port number if sin_port == 0.

reactos/drivers/lib/ip/network
ports.c 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- ports.c	14 Nov 2004 21:28:21 -0000	1.1
+++ ports.c	11 Dec 2004 16:36:05 -0000	1.2
@@ -51,7 +51,25 @@
     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 );

reactos/drivers/lib/ip/transport/tcp
tcp.c 1.13 -> 1.14
diff -u -r1.13 -r1.14
--- tcp.c	4 Dec 2004 23:29:56 -0000	1.13
+++ tcp.c	11 Dec 2004 16:36:06 -0000	1.14
@@ -17,6 +17,7 @@
 LIST_ENTRY SleepingThreadsList;
 FAST_MUTEX SleepingThreadsLock;
 RECURSIVE_MUTEX TCPLock;
+PORT_SET TCPPorts;
 
 static VOID HandleSignalledConnection( PCONNECTION_ENDPOINT Connection,
 				       ULONG NewState ) {
@@ -268,6 +269,8 @@
     InitializeListHead( &SleepingThreadsList );    
     InitializeListHead( &SignalledConnections );
 
+    PortsStartup( &TCPPorts, 1, 0xfffe );
+
     RegisterOskitTCPEventHandlers( &EventHandlers );
     InitOskitTCP();
     
@@ -308,6 +311,8 @@
 
     DeinitOskitTCP();
 
+    PortsShutdown( &TCPPorts );
+
     return STATUS_SUCCESS;
 }
 
@@ -620,4 +625,15 @@
     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 */

reactos/drivers/net/tcpip/include
ports.h 1.1 -> 1.2
diff -u -r1.1 -r1.2
--- ports.h	14 Nov 2004 19:45:16 -0000	1.1
+++ ports.h	11 Dec 2004 16:36:06 -0000	1.2
@@ -26,5 +26,6 @@
 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*/

reactos/drivers/net/tcpip/include
tcp.h 1.14 -> 1.15
diff -u -r1.14 -r1.15
--- tcp.h	4 Dec 2004 23:29:55 -0000	1.14
+++ tcp.h	11 Dec 2004 16:36:06 -0000	1.15
@@ -151,6 +151,10 @@
 
 VOID TCPTimeout();
 
+UINT TCPAllocatePort( UINT HintPort );
+
+VOID TCPFreePort( UINT Port );
+
 NTSTATUS TCPStartup(
   VOID);
 

reactos/drivers/net/tcpip/tcpip
fileobjs.c 1.27 -> 1.28
diff -u -r1.27 -r1.28
--- fileobjs.c	1 Dec 2004 08:14:15 -0000	1.27
+++ fileobjs.c	11 Dec 2004 16:36:06 -0000	1.28
@@ -268,7 +268,7 @@
   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 @@
   AF_CLR_VALID(AddrFile);
   
   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);
   
CVSspam 0.2.8