|
|
| 1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
2 |
/* |
| 3 |
* Copyright (c) 2008-2009 Strasbourg University |
3 |
* Copyright (c) 2008-2009 Strasbourg University |
|
|
4 |
* 2013 Universita' di Firenze |
| 4 |
* |
5 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* 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 |
* it under the terms of the GNU General Public License version 2 as |
|
|
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
* |
18 |
* |
| 18 |
* Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr> |
19 |
* Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr> |
|
|
20 |
* Tommaso Pecorella <tommaso.pecorella@unifi.it> |
| 19 |
*/ |
21 |
*/ |
| 20 |
|
22 |
|
| 21 |
#include "ns3/node-list.h" |
23 |
#include "ns3/node-list.h" |
|
|
| 24 |
#include "ipv6-interface-container.h" |
26 |
#include "ipv6-interface-container.h" |
| 25 |
#include "ns3/ipv6-static-routing-helper.h" |
27 |
#include "ns3/ipv6-static-routing-helper.h" |
| 26 |
|
28 |
|
| 27 |
namespace ns3 |
29 |
namespace ns3 { |
| 28 |
{ |
|
|
| 29 |
|
30 |
|
| 30 |
Ipv6InterfaceContainer::Ipv6InterfaceContainer () |
31 |
Ipv6InterfaceContainer::Ipv6InterfaceContainer () |
| 31 |
{ |
32 |
{ |
| 32 |
} |
33 |
} |
| 33 |
|
34 |
|
| 34 |
Ipv6InterfaceContainer::Iterator |
35 |
Ipv6InterfaceContainer::Iterator |
| 35 |
Ipv6InterfaceContainer::Begin (void) const |
36 |
Ipv6InterfaceContainer::Begin (void) const |
| 36 |
{ |
37 |
{ |
| 37 |
return m_interfaces.begin (); |
38 |
return m_interfaces.begin (); |
| 38 |
} |
39 |
} |
| 39 |
|
40 |
|
| 40 |
Ipv6InterfaceContainer::Iterator |
41 |
Ipv6InterfaceContainer::Iterator |
| 41 |
Ipv6InterfaceContainer::End (void) const |
42 |
Ipv6InterfaceContainer::End (void) const |
| 42 |
{ |
43 |
{ |
| 43 |
return m_interfaces.end (); |
44 |
return m_interfaces.end (); |
|
|
| 81 |
|
82 |
|
| 82 |
void Ipv6InterfaceContainer::SetRouter (uint32_t i, bool router) |
83 |
void Ipv6InterfaceContainer::SetRouter (uint32_t i, bool router) |
| 83 |
{ |
84 |
{ |
|
|
85 |
// This function is deprecated and should be substituted by: |
| 86 |
// SetForwarding (RouterInterfaceIndex, true); |
| 87 |
// SetDefaultRouteInAllNodes (RouterInterfaceIndex); |
| 88 |
|
| 84 |
Ptr<Ipv6> ipv6 = m_interfaces[i].first; |
89 |
Ptr<Ipv6> ipv6 = m_interfaces[i].first; |
| 85 |
ipv6->SetForwarding (m_interfaces[i].second, router); |
90 |
ipv6->SetForwarding (m_interfaces[i].second, router); |
| 86 |
|
91 |
|
|
|
| 105 |
} |
110 |
} |
| 106 |
} |
111 |
} |
| 107 |
|
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 |
|
| 119 |
void Ipv6InterfaceContainer::SetDefaultRouteInAllNodes (uint32_t router) |
| 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 |
|
| 141 |
void Ipv6InterfaceContainer::SetDefaultRouteInAllNodes (Ipv6Address routerAddress) |
| 142 |
{ |
| 143 |
uint32_t routerIndex; |
| 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 |
|
| 108 |
void Ipv6InterfaceContainer::SetDefaultRoute (uint32_t i, uint32_t router) |
180 |
void Ipv6InterfaceContainer::SetDefaultRoute (uint32_t i, uint32_t router) |
| 109 |
{ |
181 |
{ |
|
|
182 |
NS_ASSERT_MSG (i != router, "A node shouldn't set itself as the default router, isn't it? Aborting."); |
| 183 |
|
| 110 |
Ptr<Ipv6> ipv6 = m_interfaces[i].first; |
184 |
Ptr<Ipv6> ipv6 = m_interfaces[i].first; |
| 111 |
Ptr<Ipv6> ipv6Router = m_interfaces[router].first; |
185 |
|
| 112 |
Ipv6Address routerAddress = ipv6Router->GetAddress (m_interfaces[router].second, 0).GetAddress (); |
186 |
Ipv6Address routerAddress = GetLinkLocalAddress (router); |
|
|
187 |
NS_ASSERT_MSG (routerAddress != Ipv6Address::GetAny (), "No link-local address found on router, aborting"); |
| 188 |
|
| 113 |
Ptr<Ipv6StaticRouting> routing = 0; |
189 |
Ptr<Ipv6StaticRouting> routing = 0; |
| 114 |
Ipv6StaticRoutingHelper routingHelper; |
190 |
Ipv6StaticRoutingHelper routingHelper; |
| 115 |
|
191 |
|
|
|
| 117 |
routing->SetDefaultRoute (routerAddress, m_interfaces[i].second); |
193 |
routing->SetDefaultRoute (routerAddress, m_interfaces[i].second); |
| 118 |
} |
194 |
} |
| 119 |
|
195 |
|
|
|
196 |
|
| 197 |
void Ipv6InterfaceContainer::SetDefaultRoute (uint32_t i, Ipv6Address routerAddr) |
| 198 |
{ |
| 199 |
uint32_t routerIndex; |
| 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 |
|
| 233 |
Ipv6Address Ipv6InterfaceContainer::GetLinkLocalAddress (uint32_t index) |
| 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 |
|
| 248 |
Ipv6Address Ipv6InterfaceContainer::GetLinkLocalAddress (Ipv6Address address) |
| 249 |
{ |
| 250 |
if (address.IsLinkLocal ()) |
| 251 |
{ |
| 252 |
return address; |
| 253 |
} |
| 254 |
|
| 255 |
uint32_t nodeIndex; |
| 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 |
|
| 120 |
} /* namespace ns3 */ |
291 |
} /* namespace ns3 */ |
| 121 |
|
292 |
|