Bugzilla – Bug 1163
Ipv4EndPointDemux::AllocateEphemeralPort forget to increment the port
Last modified: 2011-08-18 01:43:01 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);
(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; };
changeset: bf446d9feecc