21 #define NS_LOG_APPEND_CONTEXT \
22 if (m_ipv4 && m_ipv4->GetObject<Node> ()) { \
23 std::clog << Simulator::Now ().GetSeconds () \
24 << " [node " << m_ipv4->GetObject<Node> ()->GetId () << "] "; }
28 #include "ns3/names.h"
29 #include "ns3/packet.h"
31 #include "ns3/simulator.h"
32 #include "ns3/ipv4-route.h"
33 #include "ns3/output-stream-wrapper.h"
34 #include "ipv4-static-routing.h"
35 #include "ipv4-routing-table-entry.h"
43 NS_OBJECT_ENSURE_REGISTERED (Ipv4StaticRouting);
46 Ipv4StaticRouting::GetTypeId (
void)
48 static TypeId tid = TypeId (
"ns3::Ipv4StaticRouting")
49 .SetParent<Ipv4RoutingProtocol> ()
50 .AddConstructor<Ipv4StaticRouting> ()
55 Ipv4StaticRouting::Ipv4StaticRouting ()
68 NS_LOG_FUNCTION (
this << network <<
" " << networkMask <<
" " << nextHop <<
" " << interface <<
" " << metric);
74 m_networkRoutes.push_back (make_pair (route,metric));
83 NS_LOG_FUNCTION (
this << network <<
" " << networkMask <<
" " << interface <<
" " << metric);
88 m_networkRoutes.push_back (make_pair (route,metric));
97 NS_LOG_FUNCTION (
this << dest <<
" " << nextHop <<
" " << interface <<
" " << metric);
115 NS_LOG_FUNCTION (
this << nextHop <<
" " << interface <<
" " << metric);
122 uint32_t inputInterface,
123 std::vector<uint32_t> outputInterfaces)
125 NS_LOG_FUNCTION (
this << origin <<
" " << group <<
" " << inputInterface <<
" " << &outputInterfaces);
128 inputInterface, outputInterfaces);
129 m_multicastRoutes.push_back (route);
145 m_networkRoutes.push_back (make_pair (route,0));
152 return m_multicastRoutes.size ();
160 "Ipv4StaticRouting::GetMulticastRoute (): Index out of range");
162 if (index < m_multicastRoutes.size ())
165 for (MulticastRoutesCI i = m_multicastRoutes.begin ();
166 i != m_multicastRoutes.end ();
182 uint32_t inputInterface)
184 NS_LOG_FUNCTION (
this << origin <<
" " << group <<
" " << inputInterface);
185 for (MulticastRoutesI i = m_multicastRoutes.begin ();
186 i != m_multicastRoutes.end ();
195 m_multicastRoutes.erase (i);
207 for (MulticastRoutesI i = m_multicastRoutes.begin ();
208 i != m_multicastRoutes.end ();
214 m_multicastRoutes.erase (i);
226 uint16_t longest_mask = 0;
227 uint32_t shortest_metric = 0xffffffff;
231 NS_ASSERT_MSG (oif,
"Try to send on link-local multicast address, and no interface index is given!");
233 rtentry = Create<Ipv4Route> ();
237 rtentry->
SetSource (m_ipv4->GetAddress (oif->GetIfIndex (), 0).GetLocal ());
242 for (NetworkRoutesI i = m_networkRoutes.begin ();
243 i != m_networkRoutes.end ();
246 Ipv4RoutingTableEntry *j=i->first;
247 uint32_t metric =i->second;
248 Ipv4Mask mask = (j)->GetDestNetworkMask ();
249 uint16_t masklen = mask.GetPrefixLength ();
250 Ipv4Address entry = (j)->GetDestNetwork ();
251 NS_LOG_LOGIC (
"Searching for route to " << dest <<
", checking against route to " << entry <<
"/" << masklen);
252 if (mask.IsMatch (dest, entry))
254 NS_LOG_LOGIC (
"Found global network route " << j <<
", mask length " << masklen <<
", metric " << metric);
257 if (oif != m_ipv4->GetNetDevice (j->GetInterface ()))
263 if (masklen < longest_mask)
268 if (masklen > longest_mask)
270 shortest_metric = 0xffffffff;
272 longest_mask = masklen;
273 if (metric > shortest_metric)
275 NS_LOG_LOGIC (
"Equal mask length, but previous metric shorter, skipping");
278 shortest_metric = metric;
279 Ipv4RoutingTableEntry* route = (j);
280 uint32_t interfaceIdx = route->GetInterface ();
281 rtentry = Create<Ipv4Route> ();
283 rtentry->
SetSource (SourceAddressSelection (interfaceIdx, route->GetDest ()));
294 NS_LOG_LOGIC (
"No matching route to " << dest <<
" found");
299 Ptr<Ipv4MulticastRoute>
300 Ipv4StaticRouting::LookupStatic (
306 Ptr<Ipv4MulticastRoute> mrtentry = 0;
308 for (MulticastRoutesI i = m_multicastRoutes.begin ();
309 i != m_multicastRoutes.end ();
312 Ipv4MulticastRoutingTableEntry *route = *i;
321 if (origin == route->GetOrigin () && group == route->GetGroup ())
324 NS_LOG_LOGIC (
"Found multicast source specific route" << *i);
326 if (group == route->GetGroup ())
328 if (interface == Ipv4::IF_ANY ||
329 interface == route->GetInputInterface ())
332 mrtentry = Create<Ipv4MulticastRoute> ();
333 mrtentry->SetGroup (route->GetGroup ());
334 mrtentry->SetOrigin (route->GetOrigin ());
335 mrtentry->SetParent (route->GetInputInterface ());
336 for (uint32_t j = 0; j < route->GetNOutputInterfaces (); j++)
338 if (route->GetOutputInterface (j))
340 NS_LOG_LOGIC (
"Setting output interface index " << route->GetOutputInterface (j));
341 mrtentry->SetOutputTtl (route->GetOutputInterface (j), Ipv4MulticastRoute::MAX_TTL - 1);
355 return m_networkRoutes.size ();;
364 uint32_t shortest_metric = 0xffffffff;
366 for (NetworkRoutesI i = m_networkRoutes.begin ();
367 i != m_networkRoutes.end ();
371 uint32_t metric = i->second;
372 Ipv4Mask mask = (j)->GetDestNetworkMask ();
378 if (metric > shortest_metric)
382 shortest_metric = metric;
400 for (NetworkRoutesCI j = m_networkRoutes.begin ();
401 j != m_networkRoutes.end ();
420 for (NetworkRoutesCI j = m_networkRoutes.begin ();
421 j != m_networkRoutes.end ();
439 for (NetworkRoutesI j = m_networkRoutes.begin ();
440 j != m_networkRoutes.end ();
446 m_networkRoutes.erase (j);
472 rtentry = LookupStatic (destination, oif);
475 sockerr = Socket::ERROR_NOTERROR;
479 sockerr = Socket::ERROR_NOROUTETOHOST;
493 NS_ASSERT (m_ipv4->GetInterfaceForDevice (idev) >= 0);
494 uint32_t iif = m_ipv4->GetInterfaceForDevice (idev);
502 ipHeader.
GetDestination (), m_ipv4->GetInterfaceForDevice (idev));
507 mcb (mrtentry, p, ipHeader);
530 for (uint32_t j = 0; j < m_ipv4->GetNInterfaces (); j++)
532 for (uint32_t i = 0; i < m_ipv4->GetNAddresses (j); i++)
540 NS_LOG_LOGIC (
"For me (destination " << addr <<
" match)");
546 lcb (p, ipHeader, iif);
552 lcb (p, ipHeader, iif);
559 if (m_ipv4->IsForwarding (iif) ==
false)
561 NS_LOG_LOGIC (
"Forwarding disabled for this interface");
562 ecb (p, ipHeader, Socket::ERROR_NOROUTETOHOST);
569 NS_LOG_LOGIC (
"Found unicast destination- calling unicast callback");
570 ucb (rtentry, p, ipHeader);
575 NS_LOG_LOGIC (
"Did not find unicast destination- returning false");
580 Ipv4StaticRouting::~Ipv4StaticRouting ()
589 for (NetworkRoutesI j = m_networkRoutes.begin ();
590 j != m_networkRoutes.end ();
591 j = m_networkRoutes.erase (j))
595 for (MulticastRoutesI i = m_multicastRoutes.begin ();
596 i != m_multicastRoutes.end ();
597 i = m_multicastRoutes.erase (i))
612 for (uint32_t j = 0; j < m_ipv4->GetNAddresses (i); j++)
614 if (m_ipv4->GetAddress (i,j).GetLocal () !=
Ipv4Address () &&
615 m_ipv4->GetAddress (i,j).GetMask () !=
Ipv4Mask () &&
618 AddNetworkRouteTo (m_ipv4->GetAddress (i,j).GetLocal ().CombineMask (m_ipv4->GetAddress (i,j).GetMask ()),
619 m_ipv4->GetAddress (i,j).GetMask (), i);
648 if (!m_ipv4->IsUp (interface))
654 Ipv4Mask networkMask = address.GetMask ();
659 networkMask, interface);
666 if (!m_ipv4->IsUp (interface))
671 Ipv4Mask networkMask = address.GetMask ();
693 for (uint32_t i = 0; i < m_ipv4->GetNInterfaces (); i++)
695 if (m_ipv4->IsUp (i))
713 *os <<
"Destination Gateway Genmask Flags Metric Ref Use Iface" << std::endl;
716 std::ostringstream dest, gw, mask, flags;
719 *os << std::setiosflags (std::ios::left) << std::setw (16) << dest.str ();
721 *os << std::setiosflags (std::ios::left) << std::setw (16) << gw.str ();
723 *os << std::setiosflags (std::ios::left) << std::setw (16) << mask.str ();
733 *os << std::setiosflags (std::ios::left) << std::setw (6) << flags.str ();
734 *os << std::setiosflags (std::ios::left) << std::setw (7) <<
GetMetric (j);
752 Ipv4StaticRouting::SourceAddressSelection (uint32_t interfaceIdx,
Ipv4Address dest)
755 if (m_ipv4->GetNAddresses (interfaceIdx) == 1)
757 return m_ipv4->GetAddress (interfaceIdx, 0).GetLocal ();
763 Ipv4Address candidate = m_ipv4->GetAddress (interfaceIdx, 0).GetLocal ();
764 for (uint32_t i = 0; i < m_ipv4->GetNAddresses (interfaceIdx); i++)
766 Ipv4InterfaceAddress test = m_ipv4->GetAddress (interfaceIdx, i);
767 if (test.GetLocal ().CombineMask (test.GetMask ()) == dest.
CombineMask (test.GetMask ()))
769 if (test.IsSecondary () ==
false)
771 return test.GetLocal ();
static Ipv4Mask GetOnes(void)
#define NS_LOG_FUNCTION(parameters)
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
a class to represent an Ipv4 address mask
void RemoveRoute(uint32_t i)
Remove a route from the static unicast routing table.
Ipv4Address GetOrigin(void) const
bool IsLocalMulticast(void) const
Ipv4RoutingTableEntry GetDefaultRoute(void)
Get the default route with lowest metric from the static routing table.
#define NS_ASSERT(condition)
Ipv4Address GetDestNetwork(void) const
Ipv4MulticastRoutingTableEntry GetMulticastRoute(uint32_t i) const
Get a route from the static multicast routing table.
#define NS_LOG_COMPONENT_DEFINE(name)
uint32_t GetInputInterface(void) const
bool IsMulticast(void) const
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
virtual void DoDispose(void)
void AddHostRouteTo(Ipv4Address dest, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a host route to the static routing table.
static Ipv4MulticastRoutingTableEntry CreateMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface, std::vector< uint32_t > outputInterfaces)
void SetSource(Ipv4Address src)
uint32_t GetInterface(void) const
virtual void NotifyInterfaceDown(uint32_t interface)
bool RemoveMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface)
Remove a route from the static multicast routing table.
bool IsGateway(void) const
void SetGateway(Ipv4Address gw)
static Ipv4RoutingTableEntry CreateNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface)
static Ipv4Mask GetZero(void)
bool IsBroadcast(void) const
Ipv4RoutingTableEntry GetRoute(uint32_t i) const
Get a route from the static unicast routing table.
virtual bool RouteInput(Ptr< const Packet > p, const Ipv4Header &header, Ptr< const NetDevice > idev, UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb, ErrorCallback ecb)
Route an input packet (to be forwarded or locally delivered)
bool IsNetwork(void) const
#define NS_LOG_LOGIC(msg)
Ipv4Address GetGateway(void) const
uint32_t GetMetric(uint32_t index) const
Get a metric for route from the static unicast routing table.
uint32_t GetNRoutes(void) const
Get the number of individual unicast routes that have been added to the routing table.
bool IsEqual(const Ipv4Address &other) const
Comparison operation between two Ipv4Addresses.
Ipv4Address GetDest(void) const
static Ipv4Address GetZero(void)
void SetOutputDevice(Ptr< NetDevice > outputDevice)
A record of an IPv4 multicast route for Ipv4GlobalRouting and Ipv4StaticRouting.
virtual void PrintRoutingTable(Ptr< OutputStreamWrapper > stream) const
Print the Routing Table entries.
#define NS_ASSERT_MSG(condition, message)
void AddNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a network route to the static routing table.
Ipv4 addresses are stored in host order in this class.
virtual void DoDispose(void)
a class to store IPv4 address information on an interface
virtual void NotifyInterfaceUp(uint32_t interface)
Ipv4Address GetGroup(void) const
Ipv4Address GetGateway(void) const
virtual void SetIpv4(Ptr< Ipv4 > ipv4)
virtual void NotifyAddAddress(uint32_t interface, Ipv4InterfaceAddress address)
static std::string FindName(Ptr< Object > object)
virtual void NotifyRemoveAddress(uint32_t interface, Ipv4InterfaceAddress address)
uint32_t GetNMulticastRoutes(void) const
Get the number of individual multicast routes that have been added to the routing table...
Ipv4Mask GetDestNetworkMask(void) const
std::ostream * GetStream(void)
uint16_t GetPrefixLength(void) const
void SetDestination(Ipv4Address dest)
void SetDefaultMulticastRoute(uint32_t outputInterface)
Add a default multicast route to the static routing table.
void AddMulticastRoute(Ipv4Address origin, Ipv4Address group, uint32_t inputInterface, std::vector< uint32_t > outputInterfaces)
Add a multicast route to the static routing table.
virtual Ptr< Ipv4Route > RouteOutput(Ptr< Packet > p, const Ipv4Header &header, Ptr< NetDevice > oif, Socket::SocketErrno &sockerr)
Query routing cache for an existing route, for an outbound packet.