A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
ipv4-static-routing-helper.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2009 University of Washington
3
*
4
* SPDX-License-Identifier: GPL-2.0-only
5
*/
6
7
#include "
ipv4-static-routing-helper.h
"
8
9
#include "ns3/assert.h"
10
#include "ns3/ipv4-address.h"
11
#include "ns3/ipv4-list-routing.h"
12
#include "ns3/ipv4-route.h"
13
#include "ns3/ipv4-routing-protocol.h"
14
#include "ns3/ipv4.h"
15
#include "ns3/log.h"
16
#include "ns3/names.h"
17
#include "ns3/node.h"
18
#include "ns3/ptr.h"
19
20
#include <vector>
21
22
namespace
ns3
23
{
24
25
NS_LOG_COMPONENT_DEFINE
(
"Ipv4StaticRoutingHelper"
);
26
27
Ipv4StaticRoutingHelper::Ipv4StaticRoutingHelper
()
28
{
29
}
30
31
Ipv4StaticRoutingHelper::Ipv4StaticRoutingHelper
(
const
Ipv4StaticRoutingHelper
&
o
)
32
{
33
}
34
35
Ipv4StaticRoutingHelper
*
36
Ipv4StaticRoutingHelper::Copy
()
const
37
{
38
return
new
Ipv4StaticRoutingHelper
(*
this
);
39
}
40
41
Ptr<Ipv4RoutingProtocol>
42
Ipv4StaticRoutingHelper::Create
(
Ptr<Node>
node)
const
43
{
44
return
CreateObject<Ipv4StaticRouting>
();
45
}
46
47
Ptr<Ipv4StaticRouting>
48
Ipv4StaticRoutingHelper::GetStaticRouting
(
Ptr<Ipv4>
ipv4)
const
49
{
50
NS_LOG_FUNCTION
(
this
);
51
Ptr<Ipv4RoutingProtocol>
ipv4rp
= ipv4->GetRoutingProtocol();
52
NS_ASSERT_MSG
(
ipv4rp
,
"No routing protocol associated with Ipv4"
);
53
if
(
DynamicCast<Ipv4StaticRouting>
(
ipv4rp
))
54
{
55
NS_LOG_LOGIC
(
"Static routing found as the main IPv4 routing protocol."
);
56
return
DynamicCast<Ipv4StaticRouting>
(
ipv4rp
);
57
}
58
if
(
DynamicCast<Ipv4ListRouting>
(
ipv4rp
))
59
{
60
Ptr<Ipv4ListRouting>
lrp
=
DynamicCast<Ipv4ListRouting>
(
ipv4rp
);
61
int16_t priority;
62
for
(
uint32_t
i
= 0;
i
<
lrp
->GetNRoutingProtocols();
i
++)
63
{
64
NS_LOG_LOGIC
(
"Searching for static routing in list"
);
65
Ptr<Ipv4RoutingProtocol>
temp
=
lrp
->GetRoutingProtocol(
i
, priority);
66
if
(
DynamicCast<Ipv4StaticRouting>
(
temp
))
67
{
68
NS_LOG_LOGIC
(
"Found static routing in list"
);
69
return
DynamicCast<Ipv4StaticRouting>
(
temp
);
70
}
71
}
72
}
73
NS_LOG_LOGIC
(
"Static routing not found"
);
74
return
nullptr
;
75
}
76
77
void
78
Ipv4StaticRoutingHelper::AddMulticastRoute
(
Ptr<Node>
n,
79
Ipv4Address
source
,
80
Ipv4Address
group
,
81
Ptr<NetDevice>
input
,
82
NetDeviceContainer
output
)
83
{
84
Ptr<Ipv4>
ipv4 = n->GetObject<
Ipv4
>();
85
86
// We need to convert the NetDeviceContainer to an array of interface
87
// numbers
88
std::vector<uint32_t>
outputInterfaces
;
89
for
(
auto
i
=
output
.Begin();
i
!=
output
.End(); ++
i
)
90
{
91
Ptr<NetDevice>
nd
= *
i
;
92
int32_t
interface
= ipv4->GetInterfaceForDevice(
nd
);
93
NS_ASSERT_MSG
(interface >= 0,
94
"Ipv4StaticRoutingHelper::AddMulticastRoute(): "
95
"Expected an interface associated with the device nd"
);
96
outputInterfaces
.push_back(interface);
97
}
98
99
int32_t
inputInterface
= ipv4->GetInterfaceForDevice(
input
);
100
NS_ASSERT_MSG
(
inputInterface
>= 0,
101
"Ipv4StaticRoutingHelper::AddMulticastRoute(): "
102
"Expected an interface associated with the device input"
);
103
Ipv4StaticRoutingHelper
helper
;
104
Ptr<Ipv4StaticRouting>
ipv4StaticRouting
=
helper
.GetStaticRouting(ipv4);
105
if
(!
ipv4StaticRouting
)
106
{
107
NS_ASSERT_MSG
(
ipv4StaticRouting
,
108
"Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
109
"Expected an Ipv4StaticRouting associated with this node"
);
110
}
111
ipv4StaticRouting
->AddMulticastRoute(
source
,
group
,
inputInterface
,
outputInterfaces
);
112
}
113
114
void
115
Ipv4StaticRoutingHelper::AddMulticastRoute
(
Ptr<Node>
n,
116
Ipv4Address
source
,
117
Ipv4Address
group
,
118
std::string
inputName
,
119
NetDeviceContainer
output
)
120
{
121
Ptr<NetDevice>
input
= Names::Find<NetDevice>(
inputName
);
122
AddMulticastRoute
(n,
source
,
group
,
input
,
output
);
123
}
124
125
void
126
Ipv4StaticRoutingHelper::AddMulticastRoute
(std::string
nName
,
127
Ipv4Address
source
,
128
Ipv4Address
group
,
129
Ptr<NetDevice>
input
,
130
NetDeviceContainer
output
)
131
{
132
Ptr<Node>
n = Names::Find<Node>(
nName
);
133
AddMulticastRoute
(n,
source
,
group
,
input
,
output
);
134
}
135
136
void
137
Ipv4StaticRoutingHelper::AddMulticastRoute
(std::string
nName
,
138
Ipv4Address
source
,
139
Ipv4Address
group
,
140
std::string
inputName
,
141
NetDeviceContainer
output
)
142
{
143
Ptr<NetDevice>
input
= Names::Find<NetDevice>(
inputName
);
144
Ptr<Node>
n = Names::Find<Node>(
nName
);
145
AddMulticastRoute
(n,
source
,
group
,
input
,
output
);
146
}
147
148
void
149
Ipv4StaticRoutingHelper::SetDefaultMulticastRoute
(
Ptr<Node>
n,
Ptr<NetDevice>
nd
)
150
{
151
Ptr<Ipv4>
ipv4 = n->GetObject<
Ipv4
>();
152
int32_t
interfaceSrc
= ipv4->GetInterfaceForDevice(
nd
);
153
NS_ASSERT_MSG
(
interfaceSrc
>= 0,
154
"Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
155
"Expected an interface associated with the device"
);
156
Ipv4StaticRoutingHelper
helper
;
157
Ptr<Ipv4StaticRouting>
ipv4StaticRouting
=
helper
.GetStaticRouting(ipv4);
158
if
(!
ipv4StaticRouting
)
159
{
160
NS_ASSERT_MSG
(
ipv4StaticRouting
,
161
"Ipv4StaticRoutingHelper::SetDefaultMulticastRoute(): "
162
"Expected an Ipv4StaticRouting associated with this node"
);
163
}
164
ipv4StaticRouting
->SetDefaultMulticastRoute(
interfaceSrc
);
165
}
166
167
void
168
Ipv4StaticRoutingHelper::SetDefaultMulticastRoute
(
Ptr<Node>
n, std::string
ndName
)
169
{
170
Ptr<NetDevice>
nd
= Names::Find<NetDevice>(
ndName
);
171
SetDefaultMulticastRoute
(n,
nd
);
172
}
173
174
void
175
Ipv4StaticRoutingHelper::SetDefaultMulticastRoute
(std::string
nName
,
Ptr<NetDevice>
nd
)
176
{
177
Ptr<Node>
n = Names::Find<Node>(
nName
);
178
SetDefaultMulticastRoute
(n,
nd
);
179
}
180
181
void
182
Ipv4StaticRoutingHelper::SetDefaultMulticastRoute
(std::string
nName
, std::string
ndName
)
183
{
184
Ptr<Node>
n = Names::Find<Node>(
nName
);
185
Ptr<NetDevice>
nd
= Names::Find<NetDevice>(
ndName
);
186
SetDefaultMulticastRoute
(n,
nd
);
187
}
188
189
}
// namespace ns3
int32_t
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv4
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition
ipv4.h:69
ns3::Ipv4StaticRoutingHelper
Helper class that adds ns3::Ipv4StaticRouting objects.
Definition
ipv4-static-routing-helper.h:33
ns3::Ipv4StaticRoutingHelper::Copy
Ipv4StaticRoutingHelper * Copy() const override
Definition
ipv4-static-routing-helper.cc:36
ns3::Ipv4StaticRoutingHelper::GetStaticRouting
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...
Definition
ipv4-static-routing-helper.cc:48
ns3::Ipv4StaticRoutingHelper::Ipv4StaticRoutingHelper
Ipv4StaticRoutingHelper()
Definition
ipv4-static-routing-helper.cc:27
ns3::Ipv4StaticRoutingHelper::AddMulticastRoute
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>
Definition
ipv4-static-routing-helper.cc:78
ns3::Ipv4StaticRoutingHelper::SetDefaultMulticastRoute
void SetDefaultMulticastRoute(Ptr< Node > n, Ptr< NetDevice > nd)
Add a default route to the static routing protocol to forward packets out a particular interface.
Definition
ipv4-static-routing-helper.cc:149
ns3::Ipv4StaticRoutingHelper::Create
Ptr< Ipv4RoutingProtocol > Create(Ptr< Node > node) const override
Definition
ipv4-static-routing-helper.cc:42
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition
net-device-container.h:32
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
ptr.h:66
uint32_t
NS_ASSERT_MSG
#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:75
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition
log.h:271
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition
log-macros-enabled.h:229
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ipv4-static-routing-helper.h
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
internet
helper
ipv4-static-routing-helper.cc
Generated on Mon Dec 15 2025 15:21:53 for ns-3 by
1.9.8