Bug 1163 - Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
Status: RESOLVED FIXED
Product: ns-3
Classification: Unclassified
Component: internet
ns-3.10
All All
: P5 trivial
Assigned To: George Riley
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-05-18 04:09 EDT by Frederic Urbani
Modified: 2011-08-18 01:43 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Frederic Urbani 2011-05-18 04:09:49 EDT
Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port in consequence you may obtain everytime the same port if you do something like this:

 fd = socket(AF_INET...)

 connect(fd, ...)

 getsockaddr(fd,...);

 close();

 fd2 = socket(AF_INET...)

 connect(fd2, ...)

 getsockaddr(fd2, ...); --> same port ! 

----

I am using DCE for my tests, so as you see I use a standard API to test the TCP sockets.

So I propose this patch: 


diff -r 1050aa2c5ee0 src/internet/model/ipv4-end-point-demux.cc
--- a/src/internet/model/ipv4-end-point-demux.cc	Tue May 17 17:10:58 2011 +0200
+++ b/src/internet/model/ipv4-end-point-demux.cc	Wed May 18 09:53:38 2011 +0200
@@ -362,6 +362,7 @@
         }
       if (!LookupPortLocal (port)) 
         {
+          m_ephemeral = port;
           return port;
         }
   } while (port != m_ephemeral);
Comment 1 John Abraham 2011-08-17 08:51:42 EDT
(In reply to comment #0)
> Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port in
> consequence you may obtain everytime the same port if you do something like
> this:
>  fd = socket(AF_INET...)
>  connect(fd, ...)
>  getsockaddr(fd,...);
>  close();
>  fd2 = socket(AF_INET...)
>  connect(fd2, ...)
>  getsockaddr(fd2, ...); --> same port ! 
> ----
> I am using DCE for my tests, so as you see I use a standard API to test the TCP
> sockets.
> So I propose this patch: 
> diff -r 1050aa2c5ee0 src/internet/model/ipv4-end-point-demux.cc
> --- a/src/internet/model/ipv4-end-point-demux.cc    Tue May 17 17:10:58 2011
> +0200
> +++ b/src/internet/model/ipv4-end-point-demux.cc    Wed May 18 09:53:38 2011
> +0200
> @@ -362,6 +362,7 @@
>          }
>        if (!LookupPortLocal (port)) 
>          {
> +          m_ephemeral = port;
>            return port;
>          }
>    } while (port != m_ephemeral);




Although the diff addresses one problem, I think even in this case, we will skip port 49152 and 65535
To be more inline with the linux implementation at netinet/in_pcb.c
we could try something like this

diff -r 2e26b9e61270 src/internet/model/ipv4-end-point-demux.cc
--- a/src/internet/model/ipv4-end-point-demux.cc	Fri Jul 08 09:25:12 2011 -0400
+++ b/src/internet/model/ipv4-end-point-demux.cc	Wed Aug 17 08:45:37 2011 -0400
@@ -27,7 +27,7 @@
 NS_LOG_COMPONENT_DEFINE ("Ipv4EndPointDemux");
 
 Ipv4EndPointDemux::Ipv4EndPointDemux ()
-  : m_ephemeral (49152)
+  : m_ephemeral (49152), m_portLast (65535), m_portFirst (49152)
 {
   NS_LOG_FUNCTION_NOARGS ();
 }
@@ -348,6 +348,7 @@
   return generic;
 }
 

+uint16_t
+Ipv4EndPointDemux::AllocateEphemeralPort (void)
+{
+  NS_LOG_FUNCTION_NOARGS ();
+  uint16_t port = m_ephemeral;
+  int count = m_portLast - m_portFirst;
+  do 
+    {
+      if (count-- < 0)
+        {
+	   return 0;
+	 }
+      ++port;
+      if (port < m_portFirst || port > m_portLast)
+        {
+          port = m_portFirst;
+	 }
+    } while (LookupPortLocal (port));
+  m_ephemeral = port;
+  return port;
+}
 } //namespace ns3
 
diff -r 2e26b9e61270 src/internet/model/ipv4-end-point-demux.h
--- a/src/internet/model/ipv4-end-point-demux.h	Fri Jul 08 09:25:12 2011 -0400
+++ b/src/internet/model/ipv4-end-point-demux.h	Wed Aug 17 08:45:37 2011 -0400
@@ -77,6 +77,8 @@
   uint16_t AllocateEphemeralPort (void);
 
   uint16_t m_ephemeral;
+  uint16_t m_portLast;
+  uint16_t m_portFirst;
   EndPoints m_endPoints;
 };
Comment 2 Tom Henderson 2011-08-18 01:43:01 EDT
changeset: bf446d9feecc