Fix UDP source address, make it honor Bind().
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Fri Feb 01 19:29:56 2008 +0000 (2008-02-01)
changeset 2300055ff23aa034
parent 2299 0b9193f38f18
child 2301 78185ba7b17e
child 2305 518187ed457c
Fix UDP source address, make it honor Bind().
src/internet-node/udp-socket.cc
     1.1 --- a/src/internet-node/udp-socket.cc	Fri Feb 01 18:40:14 2008 +0000
     1.2 +++ b/src/internet-node/udp-socket.cc	Fri Feb 01 19:29:56 2008 +0000
     1.3 @@ -282,9 +282,22 @@
     1.4            Ipv4Address addri = ipv4->GetAddress (i);
     1.5            Ipv4Mask maski = ipv4->GetNetworkMask (i);
     1.6            Ipv4Address bcast = addri.GetSubnetDirectedBroadcast (maski);
     1.7 -          NS_LOG_LOGIC ("Sending one copy from " << addri << " to " << bcast
     1.8 +          Ipv4Address saddr;
     1.9 +
    1.10 +          if (m_endPoint->GetLocalAddress () == Ipv4Address::GetAny ())
    1.11 +            {
    1.12 +              // source address = interface address
    1.13 +              saddr = addri;
    1.14 +            }
    1.15 +          else
    1.16 +            {
    1.17 +              // Bind was called on the socket to set an address; use it
    1.18 +              saddr = m_endPoint->GetLocalAddress ();
    1.19 +            }
    1.20 +          
    1.21 +          NS_LOG_LOGIC ("Sending one copy from " << saddr << " to " << bcast
    1.22                          << " (mask is " << maski << ")");
    1.23 -          m_udp->Send (p->Copy (), addri, bcast,
    1.24 +          m_udp->Send (p->Copy (), saddr, bcast,
    1.25                         m_endPoint->GetLocalPort (), port);
    1.26            NotifyDataSent (p->GetSize ());
    1.27          }
    1.28 @@ -292,8 +305,21 @@
    1.29      }
    1.30    else if (ipv4->GetIfIndexForDestination(dest, localIfIndex))
    1.31      {
    1.32 +      Ipv4Address saddr;
    1.33 +
    1.34 +      if (m_endPoint->GetLocalAddress () == Ipv4Address::GetAny ())
    1.35 +        {
    1.36 +          // source address = interface address
    1.37 +          saddr = ipv4->GetAddress (localIfIndex);
    1.38 +        }
    1.39 +      else
    1.40 +        {
    1.41 +          // Bind was called on the socket to set an address; use it
    1.42 +          saddr = m_endPoint->GetLocalAddress ();
    1.43 +        }
    1.44 +
    1.45        NS_LOG_LOGIC ("Route exists");
    1.46 -      m_udp->Send (p, ipv4->GetAddress (localIfIndex), dest,
    1.47 +      m_udp->Send (p, saddr, dest,
    1.48  		   m_endPoint->GetLocalPort (), port);
    1.49        NotifyDataSent (p->GetSize ());
    1.50        return 0;