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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
8 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
9 */
10
12
14
15#include "ns3/names.h"
16#include "ns3/node-list.h"
17
18namespace ns3
19{
20
24
27{
28 return m_interfaces.begin();
29}
30
33{
34 return m_interfaces.end();
35}
36
39{
40 return m_interfaces.size();
41}
42
48
51{
52 Ptr<Ipv6> ipv6 = m_interfaces[i].first;
53 uint32_t interface = m_interfaces[i].second;
54 return ipv6->GetAddress(interface, j).GetAddress();
55}
56
57void
59{
60 m_interfaces.emplace_back(ipv6, interface);
61}
62
63void
64Ipv6InterfaceContainer::Add(std::string ipv6Name, uint32_t interface)
65{
66 Ptr<Ipv6> ipv6 = Names::Find<Ipv6>(ipv6Name);
67 m_interfaces.emplace_back(ipv6, interface);
68}
69
70void
72{
73 for (auto it = c.m_interfaces.begin(); it != c.m_interfaces.end(); it++)
74 {
75 m_interfaces.push_back(*it);
76 }
77}
78
79std::pair<Ptr<Ipv6>, uint32_t>
81{
82 return m_interfaces[i];
83}
84
85void
87{
88 Ptr<Ipv6> ipv6 = m_interfaces[i].first;
89 ipv6->SetForwarding(m_interfaces[i].second, router);
90}
91
92void
94{
95 Ptr<Ipv6> ipv6 = m_interfaces[router].first;
96 uint32_t other;
97
98 Ipv6Address routerAddress = GetLinkLocalAddress(router);
99 NS_ASSERT_MSG(routerAddress != Ipv6Address::GetAny(),
100 "No link-local address found on router, aborting");
101
102 for (other = 0; other < m_interfaces.size(); other++)
103 {
104 if (other != router)
105 {
106 Ptr<Ipv6StaticRouting> routing = nullptr;
107 Ipv6StaticRoutingHelper routingHelper;
108
109 ipv6 = m_interfaces[other].first;
110 routing = routingHelper.GetStaticRouting(ipv6);
112 routing,
113 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
114 routing->SetDefaultRoute(routerAddress, m_interfaces[other].second);
115 }
116 }
117}
118
119void
121{
122 uint32_t routerIndex = 0;
123 bool found = false;
124 for (uint32_t index = 0; index < m_interfaces.size(); index++)
125 {
126 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
127 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
128 {
129 Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
130 if (addr == routerAddress)
131 {
132 routerIndex = index;
133 found = true;
134 break;
135 }
136 }
137 if (found)
138 {
139 break;
140 }
141 }
142 NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
143
144 for (uint32_t other = 0; other < m_interfaces.size(); other++)
145 {
146 if (other != routerIndex)
147 {
148 Ptr<Ipv6StaticRouting> routing = nullptr;
149 Ipv6StaticRoutingHelper routingHelper;
150
151 Ptr<Ipv6> ipv6 = m_interfaces[other].first;
152 routing = routingHelper.GetStaticRouting(ipv6);
154 routing,
155 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
156 routing->SetDefaultRoute(routerAddress, m_interfaces[other].second);
157 }
158 }
159}
160
161void
163{
164 NS_ASSERT_MSG(i != router,
165 "A node shouldn't set itself as the default router, isn't it? Aborting.");
166
167 Ptr<Ipv6> ipv6 = m_interfaces[i].first;
168
169 Ipv6Address routerAddress = GetLinkLocalAddress(router);
170 NS_ASSERT_MSG(routerAddress != Ipv6Address::GetAny(),
171 "No link-local address found on router, aborting");
172
173 Ptr<Ipv6StaticRouting> routing = nullptr;
174 Ipv6StaticRoutingHelper routingHelper;
175
176 routing = routingHelper.GetStaticRouting(ipv6);
178 routing,
179 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
180 routing->SetDefaultRoute(routerAddress, m_interfaces[i].second);
181}
182
183void
185{
186 uint32_t routerIndex = 0;
187 bool found = false;
188 for (uint32_t index = 0; index < m_interfaces.size(); index++)
189 {
190 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
191 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
192 {
193 Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
194 if (addr == routerAddr)
195 {
196 routerIndex = index;
197 found = true;
198 break;
199 }
200 }
201 if (found)
202 {
203 break;
204 }
205 }
206 NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
207
208 NS_ASSERT_MSG(i != routerIndex,
209 "A node shouldn't set itself as the default router, isn't it? Aborting.");
210
211 Ptr<Ipv6> ipv6 = m_interfaces[i].first;
212 Ipv6Address routerLinkLocalAddress = GetLinkLocalAddress(routerIndex);
213 Ptr<Ipv6StaticRouting> routing = nullptr;
214 Ipv6StaticRoutingHelper routingHelper;
215
216 routing = routingHelper.GetStaticRouting(ipv6);
218 routing,
219 "Default router setup failed because no Ipv6StaticRouting was found on the node.");
220 routing->SetDefaultRoute(routerLinkLocalAddress, m_interfaces[i].second);
221}
222
225{
226 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
227 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
228 {
229 Ipv6InterfaceAddress iAddress;
230 iAddress = ipv6->GetAddress(m_interfaces[index].second, i);
232 {
233 return iAddress.GetAddress();
234 }
235 }
236 return Ipv6Address::GetAny();
237}
238
241{
242 if (address.IsLinkLocal())
243 {
244 return address;
245 }
246
247 uint32_t nodeIndex = 0;
248 bool found = false;
249 for (uint32_t index = 0; index < m_interfaces.size(); index++)
250 {
251 Ptr<Ipv6> ipv6 = m_interfaces[index].first;
252 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[index].second); i++)
253 {
254 Ipv6Address addr = ipv6->GetAddress(m_interfaces[index].second, i).GetAddress();
255 if (addr == address)
256 {
257 nodeIndex = index;
258 found = true;
259 break;
260 }
261 }
262 if (found)
263 {
264 break;
265 }
266 }
267 NS_ASSERT_MSG(found != true, "No such address in the interfaces. Aborting.");
268
269 Ptr<Ipv6> ipv6 = m_interfaces[nodeIndex].first;
270 for (uint32_t i = 0; i < ipv6->GetNAddresses(m_interfaces[nodeIndex].second); i++)
271 {
272 Ipv6InterfaceAddress iAddress;
273 iAddress = ipv6->GetAddress(m_interfaces[nodeIndex].second, i);
275 {
276 return iAddress.GetAddress();
277 }
278 }
279 return Ipv6Address::GetAny();
280}
281
282} /* namespace ns3 */
Describes an IPv6 address.
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.
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition names.h:443
Smart pointer class similar to boost::intrusive_ptr.
#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:75
Every class exported by the ns3 library is enclosed in the ns3 namespace.