A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
29namespace ns3
30{
31
33{
34}
35
38{
39 return m_interfaces.begin();
40}
41
44{
45 return m_interfaces.end();
46}
47
50{
51 return m_interfaces.size();
52}
53
56{
57 return m_interfaces[i].second;
58}
59
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
68void
70{
71 m_interfaces.emplace_back(ipv6, interface);
72}
73
74void
75Ipv6InterfaceContainer::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
81void
83{
84 for (InterfaceVector::const_iterator it = c.m_interfaces.begin(); it != c.m_interfaces.end();
85 it++)
86 {
87 m_interfaces.push_back(*it);
88 }
89}
90
91std::pair<Ptr<Ipv6>, uint32_t>
93{
94 return m_interfaces[i];
95}
96
97void
99{
100 Ptr<Ipv6> ipv6 = m_interfaces[i].first;
101 ipv6->SetForwarding(m_interfaces[i].second, router);
102}
103
104void
106{
107 Ptr<Ipv6> ipv6 = m_interfaces[router].first;
108 uint32_t other;
109
110 Ipv6Address routerAddress = GetLinkLocalAddress(router);
111 NS_ASSERT_MSG(routerAddress != Ipv6Address::GetAny(),
112 "No link-local address found on router, aborting");
113
114 for (other = 0; other < m_interfaces.size(); other++)
115 {
116 if (other != router)
117 {
118 Ptr<Ipv6StaticRouting> routing = nullptr;
119 Ipv6StaticRoutingHelper routingHelper;
120
121 ipv6 = m_interfaces[other].first;
122 routing = routingHelper.GetStaticRouting(ipv6);
124 routing,
125 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
126 routing->SetDefaultRoute(routerAddress, m_interfaces[other].second);
127 }
128 }
129}
130
131void
133{
134 uint32_t routerIndex = 0;
135 bool found = false;
136 for (uint32_t index = 0; index < m_interfaces.size(); index++)
137 {
138 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
139 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
140 {
141 Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
142 if (addr == routerAddress)
143 {
144 routerIndex = index;
145 found = true;
146 break;
147 }
148 }
149 if (found)
150 {
151 break;
152 }
153 }
154 NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
155
156 for (uint32_t other = 0; other < m_interfaces.size(); other++)
157 {
158 if (other != routerIndex)
159 {
160 Ptr<Ipv6StaticRouting> routing = nullptr;
161 Ipv6StaticRoutingHelper routingHelper;
162
163 Ptr<Ipv6> ipv6 = m_interfaces[other].first;
164 routing = routingHelper.GetStaticRouting(ipv6);
166 routing,
167 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
168 routing->SetDefaultRoute(routerAddress, m_interfaces[other].second);
169 }
170 }
171}
172
173void
175{
176 NS_ASSERT_MSG(i != router,
177 "A node shouldn't set itself as the default router, isn't it? Aborting.");
178
179 Ptr<Ipv6> ipv6 = m_interfaces[i].first;
180
181 Ipv6Address routerAddress = GetLinkLocalAddress(router);
182 NS_ASSERT_MSG(routerAddress != Ipv6Address::GetAny(),
183 "No link-local address found on router, aborting");
184
185 Ptr<Ipv6StaticRouting> routing = nullptr;
186 Ipv6StaticRoutingHelper routingHelper;
187
188 routing = routingHelper.GetStaticRouting(ipv6);
190 routing,
191 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
192 routing->SetDefaultRoute(routerAddress, m_interfaces[i].second);
193}
194
195void
197{
198 uint32_t routerIndex = 0;
199 bool found = false;
200 for (uint32_t index = 0; index < m_interfaces.size(); index++)
201 {
202 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
203 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
204 {
205 Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
206 if (addr == routerAddr)
207 {
208 routerIndex = index;
209 found = true;
210 break;
211 }
212 }
213 if (found)
214 {
215 break;
216 }
217 }
218 NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
219
220 NS_ASSERT_MSG(i != routerIndex,
221 "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 = nullptr;
226 Ipv6StaticRoutingHelper routingHelper;
227
228 routing = routingHelper.GetStaticRouting(ipv6);
230 routing,
231 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
232 routing->SetDefaultRoute(routerLinkLocalAddress, m_interfaces[i].second);
233}
234
237{
238 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
239 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
240 {
241 Ipv6InterfaceAddress iAddress;
242 iAddress = ipv6->GetAddress(m_interfaces[index].second, i);
244 {
245 return iAddress.GetAddress();
246 }
247 }
248 return Ipv6Address::GetAny();
249}
250
253{
254 if (address.IsLinkLocal())
255 {
256 return address;
257 }
258
259 uint32_t nodeIndex = 0;
260 bool found = false;
261 for (uint32_t index = 0; index < m_interfaces.size(); index++)
262 {
263 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
264 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
265 {
266 Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
267 if (addr == address)
268 {
269 nodeIndex = index;
270 found = true;
271 break;
272 }
273 }
274 if (found)
275 {
276 break;
277 }
278 }
279 NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
280
281 Ptr<Ipv6> ipv6 = m_interfaces[nodeIndex].first;
282 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[nodeIndex].second); i++)
283 {
284 Ipv6InterfaceAddress iAddress;
285 iAddress = ipv6->GetAddress(m_interfaces[nodeIndex].second, i);
287 {
288 return iAddress.GetAddress();
289 }
290 }
291 return Ipv6Address::GetAny();
292}
293
294} /* 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.
std::vector< std::pair< Ptr< Ipv6 >, uint32_t > >::const_iterator Iterator
Container Const Iterator for pairs of Ipv6 smart pointer / Interface 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.
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:78
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Definition: second.py:1