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"
30 #include "ipv4-static-routing-helper.h"
31
32 namespace ns3 {
33
34 NS_LOG_COMPONENT_DEFINE ("Ipv4StaticRoutingHelper");
35
Ipv4StaticRoutingHelper()36 Ipv4StaticRoutingHelper::Ipv4StaticRoutingHelper()
37 {
38 }
39
Ipv4StaticRoutingHelper(const Ipv4StaticRoutingHelper & o)40 Ipv4StaticRoutingHelper::Ipv4StaticRoutingHelper (const Ipv4StaticRoutingHelper &o)
41 {
42 }
43
44 Ipv4StaticRoutingHelper*
Copy(void) const45 Ipv4StaticRoutingHelper::Copy (void) const
46 {
47 return new Ipv4StaticRoutingHelper (*this);
48 }
49
50 Ptr<Ipv4RoutingProtocol>
Create(Ptr<Node> node) const51 Ipv4StaticRoutingHelper::Create (Ptr<Node> node) const
52 {
53 return CreateObject<Ipv4StaticRouting> ();
54 }
55
56
57 Ptr<Ipv4StaticRouting>
GetStaticRouting(Ptr<Ipv4> ipv4) const58 Ipv4StaticRoutingHelper::GetStaticRouting (Ptr<Ipv4> ipv4) const
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
AddMulticastRoute(Ptr<Node> n,Ipv4Address source,Ipv4Address group,Ptr<NetDevice> input,NetDeviceContainer output)88 Ipv4StaticRoutingHelper::AddMulticastRoute (
89 Ptr<Node> n,
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 (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");
114 Ipv4StaticRoutingHelper helper;
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
AddMulticastRoute(Ptr<Node> n,Ipv4Address source,Ipv4Address group,std::string inputName,NetDeviceContainer output)126 Ipv4StaticRoutingHelper::AddMulticastRoute (
127 Ptr<Node> n,
128 Ipv4Address source,
129 Ipv4Address group,
130 std::string inputName,
131 NetDeviceContainer output)
132 {
133 Ptr<NetDevice> input = Names::Find<NetDevice> (inputName);
134 AddMulticastRoute (n, source, group, input, output);
135 }
136
137 void
AddMulticastRoute(std::string nName,Ipv4Address source,Ipv4Address group,Ptr<NetDevice> input,NetDeviceContainer output)138 Ipv4StaticRoutingHelper::AddMulticastRoute (
139 std::string nName,
140 Ipv4Address source,
141 Ipv4Address group,
142 Ptr<NetDevice> input,
143 NetDeviceContainer output)
144 {
145 Ptr<Node> n = Names::Find<Node> (nName);
146 AddMulticastRoute (n, source, group, input, output);
147 }
148
149 void
AddMulticastRoute(std::string nName,Ipv4Address source,Ipv4Address group,std::string inputName,NetDeviceContainer output)150 Ipv4StaticRoutingHelper::AddMulticastRoute (
151 std::string nName,
152 Ipv4Address source,
153 Ipv4Address group,
154 std::string inputName,
155 NetDeviceContainer output)
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
SetDefaultMulticastRoute(Ptr<Node> n,Ptr<NetDevice> nd)163 Ipv4StaticRoutingHelper::SetDefaultMulticastRoute (
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");
172 Ipv4StaticRoutingHelper helper;
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
SetDefaultMulticastRoute(Ptr<Node> n,std::string ndName)184 Ipv4StaticRoutingHelper::SetDefaultMulticastRoute (
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
SetDefaultMulticastRoute(std::string nName,Ptr<NetDevice> nd)193 Ipv4StaticRoutingHelper::SetDefaultMulticastRoute (
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
SetDefaultMulticastRoute(std::string nName,std::string ndName)202 Ipv4StaticRoutingHelper::SetDefaultMulticastRoute (
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
212