A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ipv4-static-routing-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
19 
20 #include "ns3/assert.h"
21 #include "ns3/ipv4-address.h"
22 #include "ns3/ipv4-list-routing.h"
23 #include "ns3/ipv4-route.h"
24 #include "ns3/ipv4-routing-protocol.h"
25 #include "ns3/ipv4.h"
26 #include "ns3/log.h"
27 #include "ns3/names.h"
28 #include "ns3/node.h"
29 #include "ns3/ptr.h"
30 
31 #include <vector>
32 
33 namespace ns3
34 {
35 
36 NS_LOG_COMPONENT_DEFINE("Ipv4StaticRoutingHelper");
37 
39 {
40 }
41 
43 {
44 }
45 
48 {
49  return new Ipv4StaticRoutingHelper(*this);
50 }
51 
54 {
55  return CreateObject<Ipv4StaticRouting>();
56 }
57 
60 {
61  NS_LOG_FUNCTION(this);
62  Ptr<Ipv4RoutingProtocol> ipv4rp = ipv4->GetRoutingProtocol();
63  NS_ASSERT_MSG(ipv4rp, "No routing protocol associated with Ipv4");
64  if (DynamicCast<Ipv4StaticRouting>(ipv4rp))
65  {
66  NS_LOG_LOGIC("Static routing found as the main IPv4 routing protocol.");
67  return DynamicCast<Ipv4StaticRouting>(ipv4rp);
68  }
69  if (DynamicCast<Ipv4ListRouting>(ipv4rp))
70  {
71  Ptr<Ipv4ListRouting> lrp = DynamicCast<Ipv4ListRouting>(ipv4rp);
72  int16_t priority;
73  for (uint32_t i = 0; i < lrp->GetNRoutingProtocols(); i++)
74  {
75  NS_LOG_LOGIC("Searching for static routing in list");
76  Ptr<Ipv4RoutingProtocol> temp = lrp->GetRoutingProtocol(i, priority);
77  if (DynamicCast<Ipv4StaticRouting>(temp))
78  {
79  NS_LOG_LOGIC("Found static routing in list");
80  return DynamicCast<Ipv4StaticRouting>(temp);
81  }
82  }
83  }
84  NS_LOG_LOGIC("Static routing not found");
85  return nullptr;
86 }
87 
88 void
90  Ipv4Address source,
91  Ipv4Address group,
92  Ptr<NetDevice> input,
93  NetDeviceContainer output)
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 (auto 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  Ipv4Address source,
128  Ipv4Address group,
129  std::string inputName,
130  NetDeviceContainer output)
131 {
132  Ptr<NetDevice> input = Names::Find<NetDevice>(inputName);
133  AddMulticastRoute(n, source, group, input, output);
134 }
135 
136 void
138  Ipv4Address source,
139  Ipv4Address group,
140  Ptr<NetDevice> input,
141  NetDeviceContainer output)
142 {
143  Ptr<Node> n = Names::Find<Node>(nName);
144  AddMulticastRoute(n, source, group, input, output);
145 }
146 
147 void
149  Ipv4Address source,
150  Ipv4Address group,
151  std::string inputName,
152  NetDeviceContainer output)
153 {
154  Ptr<NetDevice> input = Names::Find<NetDevice>(inputName);
155  Ptr<Node> n = Names::Find<Node>(nName);
156  AddMulticastRoute(n, source, group, input, output);
157 }
158 
159 void
161 {
162  Ptr<Ipv4> ipv4 = n->GetObject<Ipv4>();
163  int32_t interfaceSrc = ipv4->GetInterfaceForDevice(nd);
164  NS_ASSERT_MSG(interfaceSrc >= 0,
165  "Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
166  "Expected an interface associated with the device");
168  Ptr<Ipv4StaticRouting> ipv4StaticRouting = helper.GetStaticRouting(ipv4);
169  if (!ipv4StaticRouting)
170  {
171  NS_ASSERT_MSG(ipv4StaticRouting,
172  "Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
173  "Expected an Ipv4StaticRouting associated with this node");
174  }
175  ipv4StaticRouting->SetDefaultMulticastRoute(interfaceSrc);
176 }
177 
178 void
180 {
181  Ptr<NetDevice> nd = Names::Find<NetDevice>(ndName);
183 }
184 
185 void
187 {
188  Ptr<Node> n = Names::Find<Node>(nName);
190 }
191 
192 void
193 Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(std::string nName, std::string ndName)
194 {
195  Ptr<Node> n = Names::Find<Node>(nName);
196  Ptr<NetDevice> nd = Names::Find<NetDevice>(ndName);
198 }
199 
200 } // namespace ns3
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
Helper class that adds ns3::Ipv4StaticRouting objects.
Ipv4StaticRoutingHelper * Copy() const override
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...
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<Node> and Ptr<NetDevice>
void SetDefaultMulticastRoute(Ptr< Node > n, Ptr< NetDevice > nd)
Add a default route to the static routing protocol to forward packets out a particular interface.
Ptr< Ipv4RoutingProtocol > Create(Ptr< Node > node) const override
holds a vector of ns3::NetDevice pointers
Iterator Begin() const
Get an iterator which refers to the first NetDevice in the container.
Iterator End() const
Get an iterator which indicates past-the-last NetDevice in the container.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
#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:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.