Remember the last allocated port and try to allocate the next one on the
following call (prevents 1024 from being allocated each time)
Modified: trunk/reactos/drivers/lib/ip/network/ports.c
Modified: trunk/reactos/drivers/net/tcpip/include/ports.h

Modified: trunk/reactos/drivers/lib/ip/network/ports.c
--- trunk/reactos/drivers/lib/ip/network/ports.c	2005-12-21 21:29:07 UTC (rev 20296)
+++ trunk/reactos/drivers/lib/ip/network/ports.c	2005-12-22 11:44:16 UTC (rev 20297)
@@ -15,6 +15,8 @@
 		   UINT PortsToManage ) {
     PortSet->StartingPort = StartingPort;
     PortSet->PortsToOversee = PortsToManage;
+    PortSet->LastAllocatedPort = PortSet->StartingPort +
+                                 PortSet->PortsToOversee - 1;
     PortSet->ProtoBitBuffer =
 	PoolAllocateBuffer( (PortSet->PortsToOversee + 7) / 8 );
     RtlInitializeBitMap( &PortSet->ProtoBitmap,
@@ -52,12 +54,23 @@
 
 ULONG AllocateAnyPort( PPORT_SET PortSet ) {
     ULONG AllocatedPort;
+    ULONG Next;
 
+__asm__("int $3\n");
+    if (PortSet->StartingPort + PortSet->PortsToOversee <=
+        PortSet->LastAllocatedPort + 1) {
+	Next = PortSet->StartingPort;
+    } else {
+	Next = PortSet->LastAllocatedPort + 1;
+    }
+    Next -= PortSet->StartingPort;
+
     ExAcquireFastMutex( &PortSet->Mutex );
     AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, 0 );
     if( AllocatedPort != (ULONG)-1 ) {
 	RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
 	AllocatedPort += PortSet->StartingPort;
+	PortSet->LastAllocatedPort = AllocatedPort;
     }
     ExReleaseFastMutex( &PortSet->Mutex );
 
@@ -68,16 +81,28 @@
 
 ULONG AllocatePortFromRange( PPORT_SET PortSet, ULONG Lowest, ULONG Highest ) {
     ULONG AllocatedPort;
+    ULONG Next;
 
+    if (PortSet->StartingPort + PortSet->PortsToOversee <=
+        PortSet->LastAllocatedPort + 1) {
+	Next = PortSet->StartingPort;
+    } else {
+	Next = PortSet->LastAllocatedPort + 1;
+    }
+    if (Next < Lowest || Highest <= Next) {
+	Next = Lowest;
+    }
+    Next -= PortSet->StartingPort;
     Lowest -= PortSet->StartingPort;
     Highest -= PortSet->StartingPort;
 
     ExAcquireFastMutex( &PortSet->Mutex );
-    AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Lowest );
+    AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Next );
     if( AllocatedPort != (ULONG)-1 && AllocatedPort >= Lowest &&
         AllocatedPort <= Highest) {
 	RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
 	AllocatedPort += PortSet->StartingPort;
+	PortSet->LastAllocatedPort = AllocatedPort;
     }
     ExReleaseFastMutex( &PortSet->Mutex );
 

Modified: trunk/reactos/drivers/net/tcpip/include/ports.h
--- trunk/reactos/drivers/net/tcpip/include/ports.h	2005-12-21 21:29:07 UTC (rev 20296)
+++ trunk/reactos/drivers/net/tcpip/include/ports.h	2005-12-22 11:44:16 UTC (rev 20297)
@@ -16,6 +16,7 @@
     PVOID ProtoBitBuffer;
     UINT StartingPort;
     UINT PortsToOversee;
+    UINT LastAllocatedPort;
     FAST_MUTEX Mutex;
 } PORT_SET, *PPORT_SET;