A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv4-static-routing-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <vector>
20 #include "ns3/log.h"
21 #include "ns3/ptr.h"
22 #include "ns3/names.h"
23 #include "ns3/node.h"
24 #include "ns3/ipv4.h"
25 #include "ns3/ipv4-route.h"
26 #include "ns3/ipv4-list-routing.h"
27 #include "ns3/assert.h"
28 #include "ns3/ipv4-address.h"
29 #include "ns3/ipv4-routing-protocol.h"
31 
32 NS_LOG_COMPONENT_DEFINE ("Ipv4StaticRoutingHelper");
33 
34 namespace ns3 {
35 
37 {
38 }
39 
41 {
42 }
43 
46 {
47  return new Ipv4StaticRoutingHelper (*this);
48 }
49 
52 {
53  return CreateObject<Ipv4StaticRouting> ();
54 }
55 
56 
59 {
60  NS_LOG_FUNCTION (this);
61  Ptr<Ipv4RoutingProtocol> ipv4rp = ipv4->GetRoutingProtocol ();
62  NS_ASSERT_MSG (ipv4rp, "No routing protocol associated with Ipv4");
63  if (DynamicCast<Ipv4StaticRouting> (ipv4rp))
64  {
65  NS_LOG_LOGIC ("Static routing found as the main IPv4 routing protocol.");
66  return DynamicCast<Ipv4StaticRouting> (ipv4rp);
67  }
68  if (DynamicCast<Ipv4ListRouting> (ipv4rp))
69  {
70  Ptr<Ipv4ListRouting> lrp = DynamicCast<Ipv4ListRouting> (ipv4rp);
71  int16_t priority;
72  for (uint32_t i = 0; i < lrp->GetNRoutingProtocols (); i++)
73  {
74  NS_LOG_LOGIC ("Searching for static routing in list");
75  Ptr<Ipv4RoutingProtocol> temp = lrp->GetRoutingProtocol (i, priority);
76  if (DynamicCast<Ipv4StaticRouting> (temp))
77  {
78  NS_LOG_LOGIC ("Found static routing in list");
79  return DynamicCast<Ipv4StaticRouting> (temp);
80  }
81  }
82  }
83  NS_LOG_LOGIC ("Static routing not found");
84  return 0;
85 }
86 
87 void
89  Ptr<Node> n,
90  Ipv4Address source,
92  Ptr<NetDevice> input,
94 {
95  Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
96 
97  // We need to convert the NetDeviceContainer to an array of interface
98  // numbers
99  std::vector<uint32_t> outputInterfaces;
100  for (NetDeviceContainer::Iterator i = output.Begin (); i != output.End (); ++i)
101  {
102  Ptr<NetDevice> nd = *i;
103  int32_t interface = ipv4->GetInterfaceForDevice (nd);
104  NS_ASSERT_MSG (interface >= 0,
105  "Ipv4StaticRoutingHelper::AddMulticastRoute(): "
106  "Expected an interface associated with the device nd");
107  outputInterfaces.push_back (interface);
108  }
109 
110  int32_t inputInterface = ipv4->GetInterfaceForDevice (input);
111  NS_ASSERT_MSG (inputInterface >= 0,
112  "Ipv4StaticRoutingHelper::AddMulticastRoute(): "
113  "Expected an interface associated with the device input");
115  Ptr<Ipv4StaticRouting> ipv4StaticRouting = helper.GetStaticRouting (ipv4);
116  if (!ipv4StaticRouting)
117  {
118  NS_ASSERT_MSG (ipv4StaticRouting,
119  "Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
120  "Expected an Ipv4StaticRouting associated with this node");
121  }
122  ipv4StaticRouting->AddMulticastRoute (source, group, inputInterface, outputInterfaces);
123 }
124 
125 void
127  Ptr<Node> n,
128  Ipv4Address source,
130  std::string inputName,
132 {
133  Ptr<NetDevice> input = Names::Find<NetDevice> (inputName);
134  AddMulticastRoute (n, source, group, input, output);
135 }
136 
137 void
139  std::string nName,
140  Ipv4Address source,
142  Ptr<NetDevice> input,
144 {
145  Ptr<Node> n = Names::Find<Node> (nName);
146  AddMulticastRoute (n, source, group, input, output);
147 }
148 
149 void
151  std::string nName,
152  Ipv4Address source,
154  std::string inputName,
156 {
157  Ptr<NetDevice> input = Names::Find<NetDevice> (inputName);
158  Ptr<Node> n = Names::Find<Node> (nName);
159  AddMulticastRoute (n, source, group, input, output);
160 }
161 
162 void
164  Ptr<Node> n,
165  Ptr<NetDevice> nd)
166 {
167  Ptr<Ipv4> ipv4 = n->GetObject<Ipv4> ();
168  int32_t interfaceSrc = ipv4->GetInterfaceForDevice (nd);
169  NS_ASSERT_MSG (interfaceSrc >= 0,
170  "Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
171  "Expected an interface associated with the device");
173  Ptr<Ipv4StaticRouting> ipv4StaticRouting = helper.GetStaticRouting (ipv4);
174  if (!ipv4StaticRouting)
175  {
176  NS_ASSERT_MSG (ipv4StaticRouting,
177  "Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
178  "Expected an Ipv4StaticRouting associated with this node");
179  }
180  ipv4StaticRouting->SetDefaultMulticastRoute (interfaceSrc);
181 }
182 
183 void
185  Ptr<Node> n,
186  std::string ndName)
187 {
188  Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
189  SetDefaultMulticastRoute (n, nd);
190 }
191 
192 void
194  std::string nName,
195  Ptr<NetDevice> nd)
196 {
197  Ptr<Node> n = Names::Find<Node> (nName);
198  SetDefaultMulticastRoute (n, nd);
199 }
200 
201 void
203  std::string nName,
204  std::string ndName)
205 {
206  Ptr<Node> n = Names::Find<Node> (nName);
207  Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
208  SetDefaultMulticastRoute (n, nd);
209 }
210 
211 } // namespace ns3
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
void SetDefaultMulticastRoute(Ptr< Node > n, Ptr< NetDevice > nd)
Add a default route to the static routing protocol to forward packets out a particular interface...
void AddMulticastRoute(Ptr< Node > n, Ipv4Address source, Ipv4Address group, Ptr< NetDevice > input, NetDeviceContainer output)
Add a multicast route to a node and net device using explicit Ptr and Ptr ...
holds a vector of ns3::NetDevice pointers
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:84
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
Helper class that adds ns3::Ipv4StaticRouting objects.
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Ipv4StaticRoutingHelper * Copy(void) const
virtual Ptr< Ipv4RoutingProtocol > Create(Ptr< Node > node) const
EventId output
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
Ptr< T > GetObject(void) const
Definition: object.h:362