A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-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 
21 #include "ns3/log.h"
22 #include "ns3/ptr.h"
23 #include "ns3/names.h"
24 #include "ns3/node.h"
25 #include "ns3/ipv6.h"
26 #include "ns3/ipv6-route.h"
27 #include "ns3/ipv6-list-routing.h"
28 #include "ns3/assert.h"
29 #include "ns3/ipv6-address.h"
30 #include "ns3/ipv6-routing-protocol.h"
31 
33 
34 NS_LOG_COMPONENT_DEFINE ("Ipv6StaticRoutingHelper");
35 
36 namespace ns3 {
37 
39 {
40 }
41 
43 {
44 }
45 
48 {
49  return new Ipv6StaticRoutingHelper (*this);
50 }
51 
54 {
55  return CreateObject<Ipv6StaticRouting> ();
56 }
57 
60 {
61  NS_LOG_FUNCTION (this);
62  Ptr<Ipv6RoutingProtocol> ipv6rp = ipv6->GetRoutingProtocol ();
63  NS_ASSERT_MSG (ipv6rp, "No routing protocol associated with Ipv6");
64  if (DynamicCast<Ipv6StaticRouting> (ipv6rp))
65  {
66  NS_LOG_LOGIC ("Static routing found as the main IPv4 routing protocol.");
67  return DynamicCast<Ipv6StaticRouting> (ipv6rp);
68  }
69  if (DynamicCast<Ipv6ListRouting> (ipv6rp))
70  {
71  Ptr<Ipv6ListRouting> lrp = DynamicCast<Ipv6ListRouting> (ipv6rp);
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<Ipv6RoutingProtocol> temp = lrp->GetRoutingProtocol (i, priority);
77  if (DynamicCast<Ipv6StaticRouting> (temp))
78  {
79  NS_LOG_LOGIC ("Found static routing in list");
80  return DynamicCast<Ipv6StaticRouting> (temp);
81  }
82  }
83  }
84  NS_LOG_LOGIC ("Static routing not found");
85  return 0;
86 }
87 
88 void
90  Ptr<Node> n,
91  Ipv6Address source,
93  Ptr<NetDevice> input,
95 {
96  Ptr<Ipv6> ipv6 = n->GetObject<Ipv6> ();
97 
98  // We need to convert the NetDeviceContainer to an array of interface
99  // numbers
100  std::vector<uint32_t> outputInterfaces;
101  for (NetDeviceContainer::Iterator i = output.Begin (); i != output.End (); ++i)
102  {
103  Ptr<NetDevice> nd = *i;
104  int32_t interface = ipv6->GetInterfaceForDevice (nd);
105  NS_ASSERT_MSG (interface >= 0,
106  "Ipv6StaticRoutingHelper::AddMulticastRoute (): "
107  "Expected an interface associated with the device nd");
108  outputInterfaces.push_back (interface);
109  }
110 
111  int32_t inputInterface = ipv6->GetInterfaceForDevice (input);
112  NS_ASSERT_MSG (inputInterface >= 0,
113  "Ipv6StaticRoutingHelper::AddMulticastRoute (): "
114  "Expected an interface associated with the device input");
116  Ptr<Ipv6StaticRouting> ipv6StaticRouting = helper.GetStaticRouting (ipv6);
117  if (!ipv6StaticRouting)
118  {
119  NS_ASSERT_MSG (ipv6StaticRouting,
120  "Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (): "
121  "Expected an Ipv6StaticRouting associated with this node");
122  }
123  ipv6StaticRouting->AddMulticastRoute (source, group, inputInterface, outputInterfaces);
124 }
125 
126 void
128  Ptr<Node> n,
129  Ipv6Address source,
131  std::string inputName,
133 {
134  Ptr<NetDevice> input = Names::Find<NetDevice> (inputName);
135  AddMulticastRoute (n, source, group, input, output);
136 }
137 
138 void
140  std::string nName,
141  Ipv6Address source,
143  Ptr<NetDevice> input,
145 {
146  Ptr<Node> n = Names::Find<Node> (nName);
147  AddMulticastRoute (n, source, group, input, output);
148 }
149 
150 void
152  std::string nName,
153  Ipv6Address source,
155  std::string inputName,
157 {
158  Ptr<NetDevice> input = Names::Find<NetDevice> (inputName);
159  Ptr<Node> n = Names::Find<Node> (nName);
160  AddMulticastRoute (n, source, group, input, output);
161 }
162 
163 #if 0
164 void
165 Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (
166  Ptr<Node> n,
167  Ptr<NetDevice> nd)
168 {
169  Ptr<Ipv6> ipv6 = n->GetObject<Ipv6> ();
170  int32_t interfaceSrc = ipv6->GetInterfaceForDevice (nd);
171  NS_ASSERT_MSG (interfaceSrc >= 0,
172  "Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (): "
173  "Expected an interface associated with the device");
175  Ptr<Ipv6StaticRouting> ipv6StaticRouting = helper.GetStaticRouting (ipv6);
176  if (!ipv6StaticRouting)
177  {
178  NS_ASSERT_MSG (ipv6StaticRouting,
179  "Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (): "
180  "Expected an Ipv6StaticRouting associated with this node");
181  }
182  ipv6StaticRouting->SetDefaultMulticastRoute (interfaceSrc);
183 }
184 
185 void
186 Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (
187  Ptr<Node> n,
188  std::string ndName)
189 {
190  Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
191  SetDefaultMulticastRoute (n, nd);
192 }
193 
194 void
195 Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (
196  std::string nName,
197  Ptr<NetDevice> nd)
198 {
199  Ptr<Node> n = Names::Find<Node> (nName);
200  SetDefaultMulticastRoute (n, nd);
201 }
202 
203 void
204 Ipv6StaticRoutingHelper::SetDefaultMulticastRoute (
205  std::string nName,
206  std::string ndName)
207 {
208  Ptr<Node> n = Names::Find<Node> (nName);
209  Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
210  SetDefaultMulticastRoute (n, nd);
211 }
212 #endif
213 
214 } // 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:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:79
void AddMulticastRoute(Ptr< Node > n, Ipv6Address source, Ipv6Address group, Ptr< NetDevice > input, NetDeviceContainer output)
Add a multicast route to a node and net device using explicit Ptr and Ptr ...
Ptr< Ipv6StaticRouting > GetStaticRouting(Ptr< Ipv6 > ipv6) const
Get Ipv6StaticRouting pointer from IPv6 stack.
virtual Ptr< Ipv6RoutingProtocol > Create(Ptr< Node > node) const
holds a vector of ns3::NetDevice pointers
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Helper class that adds ns3::Ipv6StaticRouting objects.
Ipv6StaticRoutingHelper * Copy(void) const
NS_LOG_COMPONENT_DEFINE("Ipv6StaticRoutingHelper")
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Describes an IPv6 address.
Definition: ipv6-address.h:46
std::vector< Ptr< NetDevice > >::const_iterator Iterator
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:361