A Discrete-Event Network Simulator
API
ipv6-interface-container.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2009 Strasbourg University
3  * 2013 Universita' di Firenze
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  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  * Tommaso Pecorella <tommaso.pecorella@unifi.it>
20  */
21 
23 
25 
26 #include "ns3/names.h"
27 #include "ns3/node-list.h"
28 
29 namespace ns3
30 {
31 
33 {
34 }
35 
38 {
39  return m_interfaces.begin();
40 }
41 
44 {
45  return m_interfaces.end();
46 }
47 
48 uint32_t
50 {
51  return m_interfaces.size();
52 }
53 
54 uint32_t
56 {
57  return m_interfaces[i].second;
58 }
59 
61 Ipv6InterfaceContainer::GetAddress(uint32_t i, uint32_t j) const
62 {
63  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
64  uint32_t interface = m_interfaces[i].second;
65  return ipv6->GetAddress(interface, j).GetAddress();
66 }
67 
68 void
69 Ipv6InterfaceContainer::Add(Ptr<Ipv6> ipv6, uint32_t interface)
70 {
71  m_interfaces.emplace_back(ipv6, interface);
72 }
73 
74 void
75 Ipv6InterfaceContainer::Add(std::string ipv6Name, uint32_t interface)
76 {
77  Ptr<Ipv6> ipv6 = Names::Find<Ipv6>(ipv6Name);
78  m_interfaces.emplace_back(ipv6, interface);
79 }
80 
81 void
83 {
84  for (auto it = c.m_interfaces.begin(); it != c.m_interfaces.end(); it++)
85  {
86  m_interfaces.push_back(*it);
87  }
88 }
89 
90 std::pair<Ptr<Ipv6>, uint32_t>
91 Ipv6InterfaceContainer::Get(uint32_t i) const
92 {
93  return m_interfaces[i];
94 }
95 
96 void
97 Ipv6InterfaceContainer::SetForwarding(uint32_t i, bool router)
98 {
99  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
100  ipv6->SetForwarding(m_interfaces[i].second, router);
101 }
102 
103 void
105 {
106  Ptr<Ipv6> ipv6 = m_interfaces[router].first;
107  uint32_t other;
108 
109  Ipv6Address routerAddress = GetLinkLocalAddress(router);
110  NS_ASSERT_MSG(routerAddress != Ipv6Address::GetAny(),
111  "No link-local address found on router, aborting");
112 
113  for (other = 0; other < m_interfaces.size(); other++)
114  {
115  if (other != router)
116  {
117  Ptr<Ipv6StaticRouting> routing = nullptr;
118  Ipv6StaticRoutingHelper routingHelper;
119 
120  ipv6 = m_interfaces[other].first;
121  routing = routingHelper.GetStaticRouting(ipv6);
123  routing,
124  "Default router setup failed because no Ipv6StaticRouting was found on the node.");
125  routing->SetDefaultRoute(routerAddress, m_interfaces[other].second);
126  }
127  }
128 }
129 
130 void
132 {
133  uint32_t routerIndex = 0;
134  bool found = false;
135  for (uint32_t index = 0; index < m_interfaces.size(); index++)
136  {
137  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
138  for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
139  {
140  Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
141  if (addr == routerAddress)
142  {
143  routerIndex = index;
144  found = true;
145  break;
146  }
147  }
148  if (found)
149  {
150  break;
151  }
152  }
153  NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
154 
155  for (uint32_t other = 0; other < m_interfaces.size(); other++)
156  {
157  if (other != routerIndex)
158  {
159  Ptr<Ipv6StaticRouting> routing = nullptr;
160  Ipv6StaticRoutingHelper routingHelper;
161 
162  Ptr<Ipv6> ipv6 = m_interfaces[other].first;
163  routing = routingHelper.GetStaticRouting(ipv6);
165  routing,
166  "Default router setup failed because no Ipv6StaticRouting was found on the node.");
167  routing->SetDefaultRoute(routerAddress, m_interfaces[other].second);
168  }
169  }
170 }
171 
172 void
173 Ipv6InterfaceContainer::SetDefaultRoute(uint32_t i, uint32_t router)
174 {
175  NS_ASSERT_MSG(i != router,
176  "A node shouldn't set itself as the default router, isn't it? Aborting.");
177 
178  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
179 
180  Ipv6Address routerAddress = GetLinkLocalAddress(router);
181  NS_ASSERT_MSG(routerAddress != Ipv6Address::GetAny(),
182  "No link-local address found on router, aborting");
183 
184  Ptr<Ipv6StaticRouting> routing = nullptr;
185  Ipv6StaticRoutingHelper routingHelper;
186 
187  routing = routingHelper.GetStaticRouting(ipv6);
189  routing,
190  "Default router setup failed because no Ipv6StaticRouting was found on the node.");
191  routing->SetDefaultRoute(routerAddress, m_interfaces[i].second);
192 }
193 
194 void
196 {
197  uint32_t routerIndex = 0;
198  bool found = false;
199  for (uint32_t index = 0; index < m_interfaces.size(); index++)
200  {
201  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
202  for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
203  {
204  Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
205  if (addr == routerAddr)
206  {
207  routerIndex = index;
208  found = true;
209  break;
210  }
211  }
212  if (found)
213  {
214  break;
215  }
216  }
217  NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
218 
219  NS_ASSERT_MSG(i != routerIndex,
220  "A node shouldn't set itself as the default router, isn't it? Aborting.");
221 
222  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
223  Ipv6Address routerLinkLocalAddress = GetLinkLocalAddress(routerIndex);
224  Ptr<Ipv6StaticRouting> routing = nullptr;
225  Ipv6StaticRoutingHelper routingHelper;
226 
227  routing = routingHelper.GetStaticRouting(ipv6);
229  routing,
230  "Default router setup failed because no Ipv6StaticRouting was found on the node.");
231  routing->SetDefaultRoute(routerLinkLocalAddress, m_interfaces[i].second);
232 }
233 
236 {
237  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
238  for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
239  {
240  Ipv6InterfaceAddress iAddress;
241  iAddress = ipv6->GetAddress(m_interfaces[index].second, i);
242  if (iAddress.GetScope() == Ipv6InterfaceAddress::LINKLOCAL)
243  {
244  return iAddress.GetAddress();
245  }
246  }
247  return Ipv6Address::GetAny();
248 }
249 
252 {
253  if (address.IsLinkLocal())
254  {
255  return address;
256  }
257 
258  uint32_t nodeIndex = 0;
259  bool found = false;
260  for (uint32_t index = 0; index < m_interfaces.size(); index++)
261  {
262  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
263  for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
264  {
265  Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
266  if (addr == address)
267  {
268  nodeIndex = index;
269  found = true;
270  break;
271  }
272  }
273  if (found)
274  {
275  break;
276  }
277  }
278  NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
279 
280  Ptr<Ipv6> ipv6 = m_interfaces[nodeIndex].first;
281  for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[nodeIndex].second); i++)
282  {
283  Ipv6InterfaceAddress iAddress;
284  iAddress = ipv6->GetAddress(m_interfaces[nodeIndex].second, i);
285  if (iAddress.GetScope() == Ipv6InterfaceAddress::LINKLOCAL)
286  {
287  return iAddress.GetAddress();
288  }
289  }
290  return Ipv6Address::GetAny();
291 }
292 
293 } /* namespace ns3 */
Describes an IPv6 address.
Definition: ipv6-address.h:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
Ipv6InterfaceAddress::Scope_e GetScope() const
Get address scope.
@ LINKLOCAL
Link-local address (fe80::/64)
Keep track of a set of IPv6 interfaces.
void SetForwarding(uint32_t i, bool state)
Set the state of the stack (act as a router or as an host) for the specified index.
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
void SetDefaultRoute(uint32_t i, uint32_t router)
Set the default route for the specified index.
InterfaceVector m_interfaces
List of IPv6 stack and interfaces index.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.
Ipv6Address GetLinkLocalAddress(uint32_t i)
Get the link-local address for the specified index.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
std::vector< std::pair< Ptr< Ipv6 >, uint32_t > >::const_iterator Iterator
Container Const Iterator for pairs of Ipv6 smart pointer / Interface Index.
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
std::pair< Ptr< Ipv6 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv6> and interface stored at the location specified by the index.
Iterator Begin() const
Get an iterator which refers to the first pair in the container.
Helper class that adds ns3::Ipv6StaticRouting objects.
Ptr< Ipv6StaticRouting > GetStaticRouting(Ptr< Ipv6 > ipv6) const
Get Ipv6StaticRouting pointer from IPv6 stack.
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
address
Definition: first.py:47
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Definition: second.py:1