Bug 1163: Part 2
authorJohn Abraham <john.abraham@gatech.edu>
Wed, 17 Aug 2011 12:31:34 -0400
changeset 7442 7fbcc8eb8679
parent 7441 bf446d9feecc
child 7443 32635b529440
Bug 1163: Part 2
src/internet/model/ipv4-end-point-demux.cc
--- a/src/internet/model/ipv4-end-point-demux.cc	Wed Aug 17 12:23:54 2011 -0400
+++ b/src/internet/model/ipv4-end-point-demux.cc	Wed Aug 17 12:31:34 2011 -0400
@@ -347,25 +347,27 @@
     }
   return generic;
 }
-
 uint16_t
 Ipv4EndPointDemux::AllocateEphemeralPort (void)
 {
+  // Similar to counting up logic in netinet/in_pcb.c
   NS_LOG_FUNCTION_NOARGS ();
   uint16_t port = m_ephemeral;
+  int count = m_portLast - m_portFirst;
   do 
     {
-      port++;
-      if (port == 65535) 
+      if (count-- < 0)
         {
-          port = 49152;
+          return 0;
         }
-      if (!LookupPortLocal (port)) 
+      ++port;
+      if (port < m_portFirst || port > m_portLast)
         {
-          return port;
+          port = m_portFirst;
         }
-    } while (port != m_ephemeral);
-  return 0;
+    } while (LookupPortLocal (port));
+  m_ephemeral = port;
+  return port;
 }
 
 } // namespace ns3