diff --git a/src/internet/model/ipv4-l3-protocol.cc b/src/internet/model/ipv4-l3-protocol.cc index b0cc51912..956fb7007 100644 --- a/src/internet/model/ipv4-l3-protocol.cc +++ b/src/internet/model/ipv4-l3-protocol.cc @@ -1124,6 +1124,33 @@ Ipv4L3Protocol::RemoveAddress (uint32_t i, Ipv4Address address) return false; } +Ipv4Address +Ipv4L3Protocol::SourceAddressSelection (uint32_t interfaceIdx, Ipv4Address dest) +{ + NS_LOG_FUNCTION (this << interfaceIdx << " " << dest); + if (GetNAddresses (interfaceIdx) == 1) // common case + { + return GetAddress (interfaceIdx, 0).GetLocal (); + } + // no way to determine the scope of the destination, so adopt the + // following rule: pick the first available address (index 0) unless + // a subsequent address is on link (in which case, pick the primary + // address if there are multiple) + Ipv4Address candidate = GetAddress (interfaceIdx, 0).GetLocal (); + for (uint32_t i = 0; i < GetNAddresses (interfaceIdx); i++) + { + Ipv4InterfaceAddress test = GetAddress (interfaceIdx, i); + if (test.GetLocal ().CombineMask (test.GetMask ()) == dest.CombineMask (test.GetMask ())) + { + if (test.IsSecondary () == false) + { + return test.GetLocal (); + } + } + } + return candidate; +} + Ipv4Address Ipv4L3Protocol::SelectSourceAddress (Ptr device, Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope) diff --git a/src/internet/model/ipv4-l3-protocol.h b/src/internet/model/ipv4-l3-protocol.h index c01075299..57bb22725 100644 --- a/src/internet/model/ipv4-l3-protocol.h +++ b/src/internet/model/ipv4-l3-protocol.h @@ -144,22 +144,8 @@ public: */ void Insert (Ptr protocol, uint32_t interfaceIndex); - /** - * \param protocolNumber number of protocol to lookup - * in this L4 Demux - * \returns a matching L4 Protocol - * - * This method is typically called by lower layers - * to forward packets up the stack to the right protocol. - */ + virtual Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest); virtual Ptr GetProtocol (int protocolNumber) const; - - /** - * \brief Get L4 protocol by protocol number for the specified interface. - * \param protocolNumber protocol number - * \param interfaceIndex interface index, -1 means "any" interface. - * \return corresponding IpL4Protocol or 0 if not found - */ virtual Ptr GetProtocol (int protocolNumber, int32_t interfaceIndex) const; /** @@ -194,7 +180,7 @@ public: * \param device network device * \param p the packet * \param protocol protocol value - * \param from address of the correspondant + * \param from address of the correspondent * \param to address of the destination * \param packetType type of the packet */ diff --git a/src/internet/model/ipv4-static-routing.cc b/src/internet/model/ipv4-static-routing.cc index 8563a241b..86088db4e 100644 --- a/src/internet/model/ipv4-static-routing.cc +++ b/src/internet/model/ipv4-static-routing.cc @@ -235,7 +235,7 @@ Ipv4StaticRouting::LookupStatic (Ipv4Address dest, Ptr oif) rtentry->SetDestination (dest); rtentry->SetGateway (Ipv4Address::GetZero ()); rtentry->SetOutputDevice (oif); - rtentry->SetSource (m_ipv4->GetAddress (oif->GetIfIndex (), 0).GetLocal ()); + rtentry->SetSource (m_ipv4->GetAddress (m_ipv4->GetInterfaceForDevice (oif), 0).GetLocal ()); return rtentry; } @@ -281,7 +281,7 @@ Ipv4StaticRouting::LookupStatic (Ipv4Address dest, Ptr oif) uint32_t interfaceIdx = route->GetInterface (); rtentry = Create (); rtentry->SetDestination (route->GetDest ()); - rtentry->SetSource (SourceAddressSelection (interfaceIdx, route->GetDest ())); + rtentry->SetSource (m_ipv4->SourceAddressSelection (interfaceIdx, route->GetDest ())); rtentry->SetGateway (route->GetGateway ()); rtentry->SetOutputDevice (m_ipv4->GetNetDevice (interfaceIdx)); if (masklen == 32) @@ -741,31 +741,5 @@ Ipv4StaticRouting::PrintRoutingTable (Ptr stream) const } *os << std::endl; } -Ipv4Address -Ipv4StaticRouting::SourceAddressSelection (uint32_t interfaceIdx, Ipv4Address dest) -{ - NS_LOG_FUNCTION (this << interfaceIdx << " " << dest); - if (m_ipv4->GetNAddresses (interfaceIdx) == 1) // common case - { - return m_ipv4->GetAddress (interfaceIdx, 0).GetLocal (); - } - // no way to determine the scope of the destination, so adopt the - // following rule: pick the first available address (index 0) unless - // a subsequent address is on link (in which case, pick the primary - // address if there are multiple) - Ipv4Address candidate = m_ipv4->GetAddress (interfaceIdx, 0).GetLocal (); - for (uint32_t i = 0; i < m_ipv4->GetNAddresses (interfaceIdx); i++) - { - Ipv4InterfaceAddress test = m_ipv4->GetAddress (interfaceIdx, i); - if (test.GetLocal ().CombineMask (test.GetMask ()) == dest.CombineMask (test.GetMask ())) - { - if (test.IsSecondary () == false) - { - return test.GetLocal (); - } - } - } - return candidate; -} } // namespace ns3 diff --git a/src/internet/model/ipv4-static-routing.h b/src/internet/model/ipv4-static-routing.h index 7f7554498..5d88e9529 100644 --- a/src/internet/model/ipv4-static-routing.h +++ b/src/internet/model/ipv4-static-routing.h @@ -417,14 +417,6 @@ private: Ptr LookupStatic (Ipv4Address origin, Ipv4Address group, uint32_t interface); - /** - * \brief Choose the source address to use with destination address. - * \param interface interface index - * \param dest IPv4 destination address - * \return IPv4 source address to use - */ - Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest); - /** * \brief the forwarding table for network. */ diff --git a/src/internet/model/ipv4.h b/src/internet/model/ipv4.h index e45ad1249..d8127eba8 100644 --- a/src/internet/model/ipv4.h +++ b/src/internet/model/ipv4.h @@ -359,6 +359,14 @@ public: */ virtual void SetForwarding (uint32_t interface, bool val) = 0; + /** + * \brief Choose the source address to use with destination address. + * \param interface interface index + * \param dest IPv4 destination address + * \return IPv4 source address to use + */ + virtual Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest) = 0; + /** * \param protocolNumber number of protocol to lookup * in this L4 Demux @@ -369,6 +377,14 @@ public: */ virtual Ptr GetProtocol (int protocolNumber) const = 0; + /** + * \brief Get L4 protocol by protocol number for the specified interface. + * \param protocolNumber protocol number + * \param interfaceIndex interface index, -1 means "any" interface. + * \return corresponding IpL4Protocol or 0 if not found + */ + virtual Ptr GetProtocol (int protocolNumber, int32_t interfaceIndex) const = 0; + /** * \brief Creates a raw socket *