A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-interface-container.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008-2009 Strasbourg University
4  * 2013 Universita' di Firenze
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
20  * Tommaso Pecorella <tommaso.pecorella@unifi.it>
21  */
22 
23 #include "ns3/node-list.h"
24 #include "ns3/names.h"
25 
27 #include "ns3/ipv6-static-routing-helper.h"
28 
29 namespace ns3 {
30 
32 {
33 }
34 
37 {
38  return m_interfaces.begin ();
39 }
40 
43 {
44  return m_interfaces.end ();
45 }
46 
48 {
49  return m_interfaces.size ();
50 }
51 
52 uint32_t Ipv6InterfaceContainer::GetInterfaceIndex (uint32_t i) const
53 {
54  return m_interfaces[i].second;
55 }
56 
57 Ipv6Address Ipv6InterfaceContainer::GetAddress (uint32_t i, uint32_t j) const
58 {
59  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
60  uint32_t interface = m_interfaces[i].second;
61  return ipv6->GetAddress (interface, j).GetAddress ();
62 }
63 
64 void Ipv6InterfaceContainer::Add (Ptr<Ipv6> ipv6, uint32_t interface)
65 {
66  m_interfaces.push_back (std::make_pair (ipv6, interface));
67 }
68 
69 void Ipv6InterfaceContainer::Add (std::string ipv6Name, uint32_t interface)
70 {
71  Ptr<Ipv6> ipv6 = Names::Find<Ipv6> (ipv6Name);
72  m_interfaces.push_back (std::make_pair (ipv6, interface));
73 }
74 
76 {
77  for (InterfaceVector::const_iterator it = c.m_interfaces.begin (); it != c.m_interfaces.end (); it++)
78  {
79  m_interfaces.push_back (*it);
80  }
81 }
82 
83 void Ipv6InterfaceContainer::SetRouter (uint32_t i, bool router)
84 {
85  // This function is deprecated and should be substituted by:
86  // SetForwarding (RouterInterfaceIndex, true);
87  // SetDefaultRouteInAllNodes (RouterInterfaceIndex);
88 
89  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
90  ipv6->SetForwarding (m_interfaces[i].second, router);
91 
92  if (router)
93  {
94  uint32_t other;
95  /* assume first global address is index 1 (0 is link-local) */
96  Ipv6Address routerAddress = ipv6->GetAddress (m_interfaces[i].second, 0).GetAddress ();
97 
98  for (other = 0; other < m_interfaces.size (); other++)
99  {
100  if (other != i)
101  {
102  Ptr<Ipv6StaticRouting> routing = 0;
103  Ipv6StaticRoutingHelper routingHelper;
104 
105  ipv6 = m_interfaces[other].first;
106  routing = routingHelper.GetStaticRouting (ipv6);
107  routing->SetDefaultRoute (routerAddress, m_interfaces[other].second);
108  }
109  }
110  }
111 }
112 
113 void Ipv6InterfaceContainer::SetForwarding (uint32_t i, bool router)
114 {
115  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
116  ipv6->SetForwarding (m_interfaces[i].second, router);
117 }
118 
120 {
121  Ptr<Ipv6> ipv6 = m_interfaces[router].first;
122  uint32_t other;
123 
124  Ipv6Address routerAddress = GetLinkLocalAddress (router);
125  NS_ASSERT_MSG (routerAddress != Ipv6Address::GetAny (), "No link-local address found on router, aborting");
126 
127  for (other = 0; other < m_interfaces.size (); other++)
128  {
129  if (other != router)
130  {
131  Ptr<Ipv6StaticRouting> routing = 0;
132  Ipv6StaticRoutingHelper routingHelper;
133 
134  ipv6 = m_interfaces[other].first;
135  routing = routingHelper.GetStaticRouting (ipv6);
136  routing->SetDefaultRoute (routerAddress, m_interfaces[other].second);
137  }
138  }
139 }
140 
142 {
143  uint32_t routerIndex = 0;
144  bool found = false;
145  for (uint32_t index = 0; index < m_interfaces.size (); index++)
146  {
147  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
148  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
149  {
150  Ipv6Address addr = ipv6->GetAddress (m_interfaces[index].second, i).GetAddress ();
151  if (addr == routerAddress)
152  {
153  routerIndex = index;
154  found = true;
155  break;
156  }
157  }
158  if (found)
159  {
160  break;
161  }
162  }
163  NS_ASSERT_MSG (found != true, "No such address in the interfaces. Aborting.");
164 
165  for (uint32_t other = 0; other < m_interfaces.size (); other++)
166  {
167  if (other != routerIndex)
168  {
169  Ptr<Ipv6StaticRouting> routing = 0;
170  Ipv6StaticRoutingHelper routingHelper;
171 
172  Ptr<Ipv6> ipv6 = m_interfaces[other].first;
173  routing = routingHelper.GetStaticRouting (ipv6);
174  routing->SetDefaultRoute (routerAddress, m_interfaces[other].second);
175  }
176  }
177 }
178 
179 
180 void Ipv6InterfaceContainer::SetDefaultRoute (uint32_t i, uint32_t router)
181 {
182  NS_ASSERT_MSG (i != router, "A node shouldn't set itself as the default router, isn't it? Aborting.");
183 
184  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
185 
186  Ipv6Address routerAddress = GetLinkLocalAddress (router);
187  NS_ASSERT_MSG (routerAddress != Ipv6Address::GetAny (), "No link-local address found on router, aborting");
188 
189  Ptr<Ipv6StaticRouting> routing = 0;
190  Ipv6StaticRoutingHelper routingHelper;
191 
192  routing = routingHelper.GetStaticRouting (ipv6);
193  routing->SetDefaultRoute (routerAddress, m_interfaces[i].second);
194 }
195 
196 
198 {
199  uint32_t routerIndex = 0;
200  bool found = false;
201  for (uint32_t index = 0; index < m_interfaces.size (); index++)
202  {
203  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
204  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
205  {
206  Ipv6Address addr = ipv6->GetAddress (m_interfaces[index].second, i).GetAddress ();
207  if (addr == routerAddr)
208  {
209  routerIndex = index;
210  found = true;
211  break;
212  }
213  }
214  if (found)
215  {
216  break;
217  }
218  }
219  NS_ASSERT_MSG (found != true, "No such address in the interfaces. Aborting.");
220 
221  NS_ASSERT_MSG (i != routerIndex, "A node shouldn't set itself as the default router, isn't it? Aborting.");
222 
223  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
224  Ipv6Address routerLinkLocalAddress = GetLinkLocalAddress (routerIndex);
225  Ptr<Ipv6StaticRouting> routing = 0;
226  Ipv6StaticRoutingHelper routingHelper;
227 
228  routing = routingHelper.GetStaticRouting (ipv6);
229  routing->SetDefaultRoute (routerLinkLocalAddress, m_interfaces[i].second);
230 }
231 
232 
234 {
235  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
236  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
237  {
238  Ipv6InterfaceAddress iAddress;
239  iAddress = ipv6->GetAddress (m_interfaces[index].second, i);
240  if (iAddress.GetScope () == Ipv6InterfaceAddress::LINKLOCAL)
241  {
242  return iAddress.GetAddress ();
243  }
244  }
245  return Ipv6Address::GetAny ();
246 }
247 
249 {
250  if (address.IsLinkLocal ())
251  {
252  return address;
253  }
254 
255  uint32_t nodeIndex = 0;
256  bool found = false;
257  for (uint32_t index = 0; index < m_interfaces.size (); index++)
258  {
259  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
260  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
261  {
262  Ipv6Address addr = ipv6->GetAddress (m_interfaces[index].second, i).GetAddress ();
263  if (addr == address)
264  {
265  nodeIndex = index;
266  found = true;
267  break;
268  }
269  }
270  if (found)
271  {
272  break;
273  }
274  }
275  NS_ASSERT_MSG (found != true, "No such address in the interfaces. Aborting.");
276 
277  Ptr<Ipv6> ipv6 = m_interfaces[nodeIndex].first;
278  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[nodeIndex].second); i++)
279  {
280  Ipv6InterfaceAddress iAddress;
281  iAddress = ipv6->GetAddress (m_interfaces[nodeIndex].second, i);
282  if (iAddress.GetScope () == Ipv6InterfaceAddress::LINKLOCAL)
283  {
284  return iAddress.GetAddress ();
285  }
286  }
287  return Ipv6Address::GetAny ();
288 }
289 
290 
291 } /* namespace ns3 */
292 
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Keep track of a set of IPv6 interfaces.
void SetDefaultRouteInAllNodes(uint32_t router)
Set the default route for all the devices (except the router itself).
void Add(Ptr< Ipv6 > ipv6, uint32_t interface)
Add a couple IPv6/interface.
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Ptr< Ipv6StaticRouting > GetStaticRouting(Ptr< Ipv6 > ipv6) const
Get Ipv6StaticRouting pointer from IPv6 stack.
IPv6 address associated with an interface.
Link-local address (fe80::/64)
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.
std::vector< std::pair< Ptr< Ipv6 >, uint32_t > >::const_iterator Iterator
Container Const Iterator for pairs of Ipv6 smart pointer / Interface Index.
InterfaceVector m_interfaces
List of IPv6 stack and interfaces index.
Ipv6Address GetAddress() const
Get the IPv6 address.
uint32_t GetInterfaceIndex(uint32_t i) const
Get the interface index for the specified node index.
void SetDefaultRoute(uint32_t i, uint32_t router)
Set the default route for the specified index.
Ipv6Address GetLinkLocalAddress(uint32_t i)
Get the link-local address for the specified index.
Helper class that adds ns3::Ipv6StaticRouting objects.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv6InterfaceAddress::Scope_e GetScope() const
Get address scope.
Iterator Begin(void) const
Get an iterator which refers to the first pair in the container.
tuple address
Definition: first.py:37
void SetRouter(uint32_t i, bool router) NS_DEPRECATED
Set the state of the stack (act as a router or not) for the specified index.
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
Ipv6Address GetAddress(uint32_t i, uint32_t j) const
Get the address for the specified index.