Author: cgutman
Date: Sun Oct 26 16:33:06 2008
New Revision: 37003
URL:
http://svn.reactos.org/svn/reactos?rev=37003&view=rev
Log:
- Simplify port allocation
Modified:
branches/aicom-network-fixes/drivers/network/tcpip/include/ports.h
branches/aicom-network-fixes/lib/drivers/ip/network/ports.c
Modified: branches/aicom-network-fixes/drivers/network/tcpip/include/ports.h
URL:
http://svn.reactos.org/svn/reactos/branches/aicom-network-fixes/drivers/net…
==============================================================================
--- branches/aicom-network-fixes/drivers/network/tcpip/include/ports.h [iso-8859-1]
(original)
+++ branches/aicom-network-fixes/drivers/network/tcpip/include/ports.h [iso-8859-1] Sun
Oct 26 16:33:06 2008
@@ -16,7 +16,6 @@
PVOID ProtoBitBuffer;
UINT StartingPort;
UINT PortsToOversee;
- UINT LastAllocatedPort;
FAST_MUTEX Mutex;
} PORT_SET, *PPORT_SET;
Modified: branches/aicom-network-fixes/lib/drivers/ip/network/ports.c
URL:
http://svn.reactos.org/svn/reactos/branches/aicom-network-fixes/lib/drivers…
==============================================================================
--- branches/aicom-network-fixes/lib/drivers/ip/network/ports.c [iso-8859-1] (original)
+++ branches/aicom-network-fixes/lib/drivers/ip/network/ports.c [iso-8859-1] Sun Oct 26
16:33:06 2008
@@ -15,8 +15,7 @@
UINT PortsToManage ) {
PortSet->StartingPort = StartingPort;
PortSet->PortsToOversee = PortsToManage;
- PortSet->LastAllocatedPort = PortSet->StartingPort +
- PortSet->PortsToOversee - 1;
+
PortSet->ProtoBitBuffer =
PoolAllocateBuffer( (PortSet->PortsToOversee + 7) / 8 );
if(!PortSet->ProtoBitBuffer) return STATUS_INSUFFICIENT_RESOURCES;
@@ -43,8 +42,13 @@
BOOLEAN Clear;
Port = htons(Port);
- ASSERT(Port >= PortSet->StartingPort);
- ASSERT(Port < PortSet->StartingPort + PortSet->PortsToOversee);
+
+ if ((Port < PortSet->StartingPort) ||
+ (Port > PortSet->StartingPort + PortSet->PortsToOversee))
+ {
+ return FALSE;
+ }
+
Port -= PortSet->StartingPort;
ExAcquireFastMutex( &PortSet->Mutex );
@@ -57,64 +61,42 @@
ULONG AllocateAnyPort( PPORT_SET PortSet ) {
ULONG AllocatedPort;
- ULONG Next;
-
- 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, Next );
+ AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, 0 );
if( AllocatedPort != (ULONG)-1 ) {
RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
AllocatedPort += PortSet->StartingPort;
- PortSet->LastAllocatedPort = AllocatedPort;
}
ExReleaseFastMutex( &PortSet->Mutex );
AllocatedPort = htons(AllocatedPort);
-
- ASSERT(AllocatedPort >= PortSet->StartingPort);
- ASSERT(AllocatedPort < PortSet->StartingPort + PortSet->PortsToOversee);
return AllocatedPort;
}
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;
+
+ if (Lowest < PortSet->StartingPort)
+ Lowest = PortSet->StartingPort;
+
+ if (Highest > PortSet->StartingPort + PortSet->PortsToOversee)
+ Highest = PortSet->StartingPort + PortSet->PortsToOversee;
+
Lowest -= PortSet->StartingPort;
Highest -= PortSet->StartingPort;
ExAcquireFastMutex( &PortSet->Mutex );
- AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Next );
- if( AllocatedPort != (ULONG)-1 && AllocatedPort >= Lowest &&
- AllocatedPort <= Highest) {
+ AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Lowest );
+ if( AllocatedPort != (ULONG)-1 && AllocatedPort <= Highest) {
RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
AllocatedPort += PortSet->StartingPort;
- PortSet->LastAllocatedPort = AllocatedPort;
}
ExReleaseFastMutex( &PortSet->Mutex );
AllocatedPort = htons(AllocatedPort);
- ASSERT(AllocatedPort >= PortSet->StartingPort);
- ASSERT(AllocatedPort < PortSet->StartingPort + PortSet->PortsToOversee);
-
return AllocatedPort;
}