A Discrete-Event Network Simulator
API
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::SetForwarding (uint32_t i, bool router)
84 {
85  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
86  ipv6->SetForwarding (m_interfaces[i].second, router);
87 }
88 
90 {
91  Ptr<Ipv6> ipv6 = m_interfaces[router].first;
92  uint32_t other;
93 
94  Ipv6Address routerAddress = GetLinkLocalAddress (router);
95  NS_ASSERT_MSG (routerAddress != Ipv6Address::GetAny (), "No link-local address found on router, aborting");
96 
97  for (other = 0; other < m_interfaces.size (); other++)
98  {
99  if (other != router)
100  {
101  Ptr<Ipv6StaticRouting> routing = 0;
102  Ipv6StaticRoutingHelper routingHelper;
103 
104  ipv6 = m_interfaces[other].first;
105  routing = routingHelper.GetStaticRouting (ipv6);
106  routing->SetDefaultRoute (routerAddress, m_interfaces[other].second);
107  }
108  }
109 }
110 
112 {
113  uint32_t routerIndex = 0;
114  bool found = false;
115  for (uint32_t index = 0; index < m_interfaces.size (); index++)
116  {
117  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
118  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
119  {
120  Ipv6Address addr = ipv6->GetAddress (m_interfaces[index].second, i).GetAddress ();
121  if (addr == routerAddress)
122  {
123  routerIndex = index;
124  found = true;
125  break;
126  }
127  }
128  if (found)
129  {
130  break;
131  }
132  }
133  NS_ASSERT_MSG (found != true, "No such address in the interfaces. Aborting.");
134 
135  for (uint32_t other = 0; other < m_interfaces.size (); other++)
136  {
137  if (other != routerIndex)
138  {
139  Ptr<Ipv6StaticRouting> routing = 0;
140  Ipv6StaticRoutingHelper routingHelper;
141 
142  Ptr<Ipv6> ipv6 = m_interfaces[other].first;
143  routing = routingHelper.GetStaticRouting (ipv6);
144  routing->SetDefaultRoute (routerAddress, m_interfaces[other].second);
145  }
146  }
147 }
148 
149 
150 void Ipv6InterfaceContainer::SetDefaultRoute (uint32_t i, uint32_t router)
151 {
152  NS_ASSERT_MSG (i != router, "A node shouldn't set itself as the default router, isn't it? Aborting.");
153 
154  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
155 
156  Ipv6Address routerAddress = GetLinkLocalAddress (router);
157  NS_ASSERT_MSG (routerAddress != Ipv6Address::GetAny (), "No link-local address found on router, aborting");
158 
159  Ptr<Ipv6StaticRouting> routing = 0;
160  Ipv6StaticRoutingHelper routingHelper;
161 
162  routing = routingHelper.GetStaticRouting (ipv6);
163  routing->SetDefaultRoute (routerAddress, m_interfaces[i].second);
164 }
165 
166 
168 {
169  uint32_t routerIndex = 0;
170  bool found = false;
171  for (uint32_t index = 0; index < m_interfaces.size (); index++)
172  {
173  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
174  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
175  {
176  Ipv6Address addr = ipv6->GetAddress (m_interfaces[index].second, i).GetAddress ();
177  if (addr == routerAddr)
178  {
179  routerIndex = index;
180  found = true;
181  break;
182  }
183  }
184  if (found)
185  {
186  break;
187  }
188  }
189  NS_ASSERT_MSG (found != true, "No such address in the interfaces. Aborting.");
190 
191  NS_ASSERT_MSG (i != routerIndex, "A node shouldn't set itself as the default router, isn't it? Aborting.");
192 
193  Ptr<Ipv6> ipv6 = m_interfaces[i].first;
194  Ipv6Address routerLinkLocalAddress = GetLinkLocalAddress (routerIndex);
195  Ptr<Ipv6StaticRouting> routing = 0;
196  Ipv6StaticRoutingHelper routingHelper;
197 
198  routing = routingHelper.GetStaticRouting (ipv6);
199  routing->SetDefaultRoute (routerLinkLocalAddress, m_interfaces[i].second);
200 }
201 
202 
204 {
205  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
206  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
207  {
208  Ipv6InterfaceAddress iAddress;
209  iAddress = ipv6->GetAddress (m_interfaces[index].second, i);
210  if (iAddress.GetScope () == Ipv6InterfaceAddress::LINKLOCAL)
211  {
212  return iAddress.GetAddress ();
213  }
214  }
215  return Ipv6Address::GetAny ();
216 }
217 
219 {
220  if (address.IsLinkLocal ())
221  {
222  return address;
223  }
224 
225  uint32_t nodeIndex = 0;
226  bool found = false;
227  for (uint32_t index = 0; index < m_interfaces.size (); index++)
228  {
229  Ptr<Ipv6> ipv6 = m_interfaces[index].first;
230  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[index].second); i++)
231  {
232  Ipv6Address addr = ipv6->GetAddress (m_interfaces[index].second, i).GetAddress ();
233  if (addr == address)
234  {
235  nodeIndex = index;
236  found = true;
237  break;
238  }
239  }
240  if (found)
241  {
242  break;
243  }
244  }
245  NS_ASSERT_MSG (found != true, "No such address in the interfaces. Aborting.");
246 
247  Ptr<Ipv6> ipv6 = m_interfaces[nodeIndex].first;
248  for (uint32_t i = 0; i < ipv6->GetNAddresses (m_interfaces[nodeIndex].second); i++)
249  {
250  Ipv6InterfaceAddress iAddress;
251  iAddress = ipv6->GetAddress (m_interfaces[nodeIndex].second, i);
252  if (iAddress.GetScope () == Ipv6InterfaceAddress::LINKLOCAL)
253  {
254  return iAddress.GetAddress ();
255  }
256  }
257  return Ipv6Address::GetAny ();
258 }
259 
260 
261 } /* namespace ns3 */
262 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
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.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Helper class that adds ns3::Ipv6StaticRouting objects.
#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:90
Describes an IPv6 address.
Definition: ipv6-address.h:47
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
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.