1111
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
4 |
* it under the terms of the GNU General Public License version 2 as
|
|
5 |
* published by the Free Software Foundation;
|
|
6 |
*
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
* GNU General Public License for more details.
|
|
11 |
*
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
13 |
* along with this program; if not, write to the Free Software
|
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
15 |
*/
|
|
16 |
|
|
17 |
#include "ns3/debug.h"
|
|
18 |
#include "ns3/assert.h"
|
|
19 |
#include "ns3/channel.h"
|
|
20 |
#include "ns3/net-device.h"
|
|
21 |
#include "ns3/internet-node.h"
|
|
22 |
#include "ns3/ipv4.h"
|
|
23 |
#include "global-router-interface.h"
|
|
24 |
|
|
25 |
NS_DEBUG_COMPONENT_DEFINE ("GlobalRouter");
|
|
26 |
|
|
27 |
namespace ns3 {
|
|
28 |
|
|
29 |
// ---------------------------------------------------------------------------
|
|
30 |
//
|
|
31 |
// GlobalRouterLinkRecord Implementation
|
|
32 |
//
|
|
33 |
// ---------------------------------------------------------------------------
|
|
34 |
|
|
35 |
GlobalRouterLinkRecord::GlobalRouterLinkRecord ()
|
|
36 |
:
|
|
37 |
m_linkId ("0.0.0.0"),
|
|
38 |
m_linkData ("0.0.0.0"),
|
|
39 |
m_linkType (Unknown),
|
|
40 |
m_metric (0)
|
|
41 |
{
|
|
42 |
NS_DEBUG("GlobalRouterLinkRecord::GlobalRouterLinkRecord ()");
|
|
43 |
}
|
|
44 |
|
|
45 |
GlobalRouterLinkRecord::GlobalRouterLinkRecord (
|
|
46 |
LinkType linkType,
|
|
47 |
Ipv4Address linkId,
|
|
48 |
Ipv4Address linkData,
|
|
49 |
uint32_t metric)
|
|
50 |
:
|
|
51 |
m_linkId (linkId),
|
|
52 |
m_linkData (linkData),
|
|
53 |
m_linkType (linkType),
|
|
54 |
m_metric (metric)
|
|
55 |
{
|
|
56 |
NS_DEBUG("GlobalRouterLinkRecord::GlobalRouterLinkRecord (" <<
|
|
57 |
linkType << ", " << linkId << ", " << linkData << ", " << metric << ")");
|
|
58 |
}
|
|
59 |
|
|
60 |
GlobalRouterLinkRecord::~GlobalRouterLinkRecord ()
|
|
61 |
{
|
|
62 |
NS_DEBUG("GlobalRouterLinkRecord::~GlobalRouterLinkRecord ()");
|
|
63 |
}
|
|
64 |
|
|
65 |
Ipv4Address
|
|
66 |
GlobalRouterLinkRecord::GetLinkId (void) const
|
|
67 |
{
|
|
68 |
NS_DEBUG("GlobalRouterLinkRecord::GetLinkId ()");
|
|
69 |
return m_linkId;
|
|
70 |
}
|
|
71 |
|
|
72 |
void
|
|
73 |
GlobalRouterLinkRecord::SetLinkId (Ipv4Address addr)
|
|
74 |
{
|
|
75 |
NS_DEBUG("GlobalRouterLinkRecord::SetLinkId ()");
|
|
76 |
m_linkId = addr;
|
|
77 |
}
|
|
78 |
|
|
79 |
Ipv4Address
|
|
80 |
GlobalRouterLinkRecord::GetLinkData (void) const
|
|
81 |
{
|
|
82 |
NS_DEBUG("GlobalRouterLinkRecord::GetLinkData ()");
|
|
83 |
return m_linkData;
|
|
84 |
}
|
|
85 |
|
|
86 |
void
|
|
87 |
GlobalRouterLinkRecord::SetLinkData (Ipv4Address addr)
|
|
88 |
{
|
|
89 |
NS_DEBUG("GlobalRouterLinkRecord::SetLinkData ()");
|
|
90 |
m_linkData = addr;
|
|
91 |
}
|
|
92 |
|
|
93 |
GlobalRouterLinkRecord::LinkType
|
|
94 |
GlobalRouterLinkRecord::GetLinkType (void) const
|
|
95 |
{
|
|
96 |
NS_DEBUG("GlobalRouterLinkRecord::GetLinkType ()");
|
|
97 |
return m_linkType;
|
|
98 |
}
|
|
99 |
|
|
100 |
void
|
|
101 |
GlobalRouterLinkRecord::SetLinkType (
|
|
102 |
GlobalRouterLinkRecord::LinkType linkType)
|
|
103 |
{
|
|
104 |
NS_DEBUG("GlobalRouterLinkRecord::SetLinkType ()");
|
|
105 |
m_linkType = linkType;
|
|
106 |
}
|
|
107 |
|
|
108 |
uint32_t
|
|
109 |
GlobalRouterLinkRecord::GetMetric (void) const
|
|
110 |
{
|
|
111 |
NS_DEBUG("GlobalRouterLinkRecord::GetMetric ()");
|
|
112 |
return m_metric;
|
|
113 |
}
|
|
114 |
|
|
115 |
void
|
|
116 |
GlobalRouterLinkRecord::SetMetric (uint32_t metric)
|
|
117 |
{
|
|
118 |
NS_DEBUG("GlobalRouterLinkRecord::SetMetric ()");
|
|
119 |
m_metric = metric;
|
|
120 |
}
|
|
121 |
|
|
122 |
// ---------------------------------------------------------------------------
|
|
123 |
//
|
|
124 |
// GlobalRouterLSA Implementation
|
|
125 |
//
|
|
126 |
// ---------------------------------------------------------------------------
|
|
127 |
|
|
128 |
GlobalRouterLSA::GlobalRouterLSA()
|
|
129 |
:
|
|
130 |
m_linkStateId("0.0.0.0"),
|
|
131 |
m_advertisingRtr("0.0.0.0"),
|
|
132 |
m_linkRecords(),
|
|
133 |
m_status(GlobalRouterLSA::LSA_SPF_NOT_EXPLORED)
|
|
134 |
{
|
|
135 |
NS_DEBUG("GlobalRouterLSA::GlobalRouterLSA ()");
|
|
136 |
}
|
|
137 |
|
|
138 |
GlobalRouterLSA::GlobalRouterLSA (
|
|
139 |
GlobalRouterLSA::SPFStatus status,
|
|
140 |
Ipv4Address linkStateId,
|
|
141 |
Ipv4Address advertisingRtr)
|
|
142 |
:
|
|
143 |
m_linkStateId(linkStateId),
|
|
144 |
m_advertisingRtr(advertisingRtr),
|
|
145 |
m_linkRecords(),
|
|
146 |
m_status(status)
|
|
147 |
{
|
|
148 |
NS_DEBUG("GlobalRouterLSA::GlobalRouterLSA (" << status << ", " <<
|
|
149 |
linkStateId << ", " << advertisingRtr << ")");
|
|
150 |
}
|
|
151 |
|
|
152 |
GlobalRouterLSA::GlobalRouterLSA (GlobalRouterLSA& lsa)
|
|
153 |
: m_linkStateId(lsa.m_linkStateId), m_advertisingRtr(lsa.m_advertisingRtr),
|
|
154 |
m_status(lsa.m_status)
|
|
155 |
{
|
|
156 |
NS_ASSERT_MSG(IsEmpty(),
|
|
157 |
"GlobalRouterLSA::GlobalRouterLSA (): Non-empty LSA in constructor");
|
|
158 |
CopyLinkRecords (lsa);
|
|
159 |
}
|
|
160 |
|
|
161 |
GlobalRouterLSA&
|
|
162 |
GlobalRouterLSA::operator= (const GlobalRouterLSA& lsa)
|
|
163 |
{
|
|
164 |
m_linkStateId = lsa.m_linkStateId;
|
|
165 |
m_advertisingRtr = lsa.m_advertisingRtr;
|
|
166 |
m_status = lsa.m_status;
|
|
167 |
|
|
168 |
ClearLinkRecords ();
|
|
169 |
CopyLinkRecords (lsa);
|
|
170 |
return *this;
|
|
171 |
}
|
|
172 |
|
|
173 |
void
|
|
174 |
GlobalRouterLSA::CopyLinkRecords (const GlobalRouterLSA& lsa)
|
|
175 |
{
|
|
176 |
for (ListOfLinkRecords_t::const_iterator i = lsa.m_linkRecords.begin ();
|
|
177 |
i != lsa.m_linkRecords.end ();
|
|
178 |
i++)
|
|
179 |
{
|
|
180 |
GlobalRouterLinkRecord *pSrc = *i;
|
|
181 |
GlobalRouterLinkRecord *pDst = new GlobalRouterLinkRecord;
|
|
182 |
|
|
183 |
pDst->SetLinkType (pSrc->GetLinkType ());
|
|
184 |
pDst->SetLinkId (pSrc->GetLinkId ());
|
|
185 |
pDst->SetLinkData (pSrc->GetLinkData ());
|
|
186 |
|
|
187 |
m_linkRecords.push_back(pDst);
|
|
188 |
pDst = 0;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
GlobalRouterLSA::~GlobalRouterLSA()
|
|
193 |
{
|
|
194 |
NS_DEBUG("GlobalRouterLSA::~GlobalRouterLSA ()");
|
|
195 |
ClearLinkRecords ();
|
|
196 |
}
|
|
197 |
|
|
198 |
void
|
|
199 |
GlobalRouterLSA::ClearLinkRecords(void)
|
|
200 |
{
|
|
201 |
for ( ListOfLinkRecords_t::iterator i = m_linkRecords.begin ();
|
|
202 |
i != m_linkRecords.end ();
|
|
203 |
i++)
|
|
204 |
{
|
|
205 |
NS_DEBUG("GlobalRouterLSA::ClearLinkRecords (): free link record");
|
|
206 |
|
|
207 |
GlobalRouterLinkRecord *p = *i;
|
|
208 |
delete p;
|
|
209 |
p = 0;
|
|
210 |
|
|
211 |
*i = 0;
|
|
212 |
}
|
|
213 |
NS_DEBUG("GlobalRouterLSA::ClearLinkRecords(): clear list");
|
|
214 |
m_linkRecords.clear();
|
|
215 |
}
|
|
216 |
|
|
217 |
uint32_t
|
|
218 |
GlobalRouterLSA::AddLinkRecord (GlobalRouterLinkRecord* lr)
|
|
219 |
{
|
|
220 |
m_linkRecords.push_back (lr);
|
|
221 |
return m_linkRecords.size ();
|
|
222 |
}
|
|
223 |
|
|
224 |
uint32_t
|
|
225 |
GlobalRouterLSA::GetNLinkRecords (void) const
|
|
226 |
{
|
|
227 |
return m_linkRecords.size ();
|
|
228 |
}
|
|
229 |
|
|
230 |
GlobalRouterLinkRecord *
|
|
231 |
GlobalRouterLSA::GetLinkRecord (uint32_t n) const
|
|
232 |
{
|
|
233 |
uint32_t j = 0;
|
|
234 |
for ( ListOfLinkRecords_t::const_iterator i = m_linkRecords.begin ();
|
|
235 |
i != m_linkRecords.end ();
|
|
236 |
i++, j++)
|
|
237 |
{
|
|
238 |
if (j == n)
|
|
239 |
{
|
|
240 |
return *i;
|
|
241 |
}
|
|
242 |
}
|
|
243 |
NS_ASSERT_MSG(false, "GlobalRouterLSA::GetLinkRecord (): invalid index");
|
|
244 |
return 0;
|
|
245 |
}
|
|
246 |
|
|
247 |
bool
|
|
248 |
GlobalRouterLSA::IsEmpty (void) const
|
|
249 |
{
|
|
250 |
return m_linkRecords.size () == 0;
|
|
251 |
}
|
|
252 |
|
|
253 |
Ipv4Address
|
|
254 |
GlobalRouterLSA::GetLinkStateId (void) const
|
|
255 |
{
|
|
256 |
return m_linkStateId;
|
|
257 |
}
|
|
258 |
|
|
259 |
void
|
|
260 |
GlobalRouterLSA::SetLinkStateId (Ipv4Address addr)
|
|
261 |
{
|
|
262 |
m_linkStateId = addr;
|
|
263 |
}
|
|
264 |
|
|
265 |
Ipv4Address
|
|
266 |
GlobalRouterLSA::GetAdvertisingRouter (void) const
|
|
267 |
{
|
|
268 |
return m_advertisingRtr;
|
|
269 |
}
|
|
270 |
|
|
271 |
void
|
|
272 |
GlobalRouterLSA::SetAdvertisingRouter (Ipv4Address addr)
|
|
273 |
{
|
|
274 |
m_advertisingRtr = addr;
|
|
275 |
}
|
|
276 |
|
|
277 |
GlobalRouterLSA::SPFStatus
|
|
278 |
GlobalRouterLSA::GetStatus (void) const
|
|
279 |
{
|
|
280 |
return m_status;
|
|
281 |
}
|
|
282 |
|
|
283 |
void
|
|
284 |
GlobalRouterLSA::SetStatus (GlobalRouterLSA::SPFStatus status)
|
|
285 |
{
|
|
286 |
m_status = status;
|
|
287 |
}
|
|
288 |
|
|
289 |
void
|
|
290 |
GlobalRouterLSA::Print (std::ostream &os) const
|
|
291 |
{
|
|
292 |
os << "m_linkStateId = " << m_linkStateId << std::endl <<
|
|
293 |
"m_advertisingRtr = " << m_advertisingRtr << std::endl;
|
|
294 |
|
|
295 |
for ( ListOfLinkRecords_t::const_iterator i = m_linkRecords.begin ();
|
|
296 |
i != m_linkRecords.end ();
|
|
297 |
i++)
|
|
298 |
{
|
|
299 |
GlobalRouterLinkRecord *p = *i;
|
|
300 |
os << "----------" << std::endl;
|
|
301 |
os << "m_linkId = " << p->GetLinkId () << std::endl;
|
|
302 |
os << "m_linkData = " << p->GetLinkData () << std::endl;
|
|
303 |
}
|
|
304 |
}
|
|
305 |
|
|
306 |
std::ostream& operator<< (std::ostream& os, GlobalRouterLSA& lsa)
|
|
307 |
{
|
|
308 |
lsa.Print (os);
|
|
309 |
return os;
|
|
310 |
}
|
|
311 |
|
|
312 |
// ---------------------------------------------------------------------------
|
|
313 |
//
|
|
314 |
// GlobalRouter Implementation
|
|
315 |
//
|
|
316 |
// ---------------------------------------------------------------------------
|
|
317 |
|
|
318 |
const InterfaceId GlobalRouter::iid =
|
|
319 |
MakeInterfaceId ("GlobalRouter", Object::iid);
|
|
320 |
|
|
321 |
GlobalRouter::GlobalRouter (Ptr<Node> node)
|
|
322 |
: m_node(node), m_LSAs()
|
|
323 |
{
|
|
324 |
NS_DEBUG("GlobalRouter::GlobalRouter ()");
|
|
325 |
SetInterfaceId (GlobalRouter::iid);
|
|
326 |
m_routerId.Set(RoutingEnvironment::AllocateRouterId());
|
|
327 |
}
|
|
328 |
|
|
329 |
GlobalRouter::~GlobalRouter ()
|
|
330 |
{
|
|
331 |
NS_DEBUG("GlobalRouter::~GlobalRouter ()");
|
|
332 |
ClearLSAs();
|
|
333 |
}
|
|
334 |
|
|
335 |
void
|
|
336 |
GlobalRouter::ClearLSAs ()
|
|
337 |
{
|
|
338 |
NS_DEBUG("GlobalRouter::ClearLSAs ()");
|
|
339 |
|
|
340 |
for ( ListOfLSAs_t::iterator i = m_LSAs.begin ();
|
|
341 |
i != m_LSAs.end ();
|
|
342 |
i++)
|
|
343 |
{
|
|
344 |
NS_DEBUG("GlobalRouter::ClearLSAs (): free LSA");
|
|
345 |
|
|
346 |
GlobalRouterLSA *p = *i;
|
|
347 |
delete p;
|
|
348 |
p = 0;
|
|
349 |
|
|
350 |
*i = 0;
|
|
351 |
}
|
|
352 |
NS_DEBUG("GlobalRouter::ClearLSAs (): clear list");
|
|
353 |
m_LSAs.clear();
|
|
354 |
}
|
|
355 |
|
|
356 |
Ipv4Address
|
|
357 |
GlobalRouter::GetRouterId (void) const
|
|
358 |
{
|
|
359 |
return m_routerId;
|
|
360 |
}
|
|
361 |
|
|
362 |
//
|
|
363 |
// Go out and discover any adjacent routers and build the Link State
|
|
364 |
// Advertisements that reflect them and their associated networks.
|
|
365 |
//
|
|
366 |
uint32_t
|
|
367 |
GlobalRouter::DiscoverLSAs (void)
|
|
368 |
{
|
|
369 |
NS_DEBUG("GlobalRouter::DiscoverLSAs ()");
|
|
370 |
NS_ASSERT_MSG(m_node,
|
|
371 |
"GlobalRouter::DiscoverLSAs (): <Node> interface not set");
|
|
372 |
|
|
373 |
ClearLSAs ();
|
|
374 |
//
|
|
375 |
// We're aggregated to a node. We need to ask the node for a pointer to its
|
|
376 |
// Ipv4 interface. This is where the information regarding the attached
|
|
377 |
// interfaces lives.
|
|
378 |
//
|
|
379 |
Ptr<Ipv4> ipv4Local = m_node->QueryInterface<Ipv4> (Ipv4::iid);
|
|
380 |
NS_ASSERT_MSG(ipv4Local,
|
|
381 |
"GlobalRouter::DiscoverLSAs (): QI for <Ipv4> interface failed");
|
|
382 |
//
|
|
383 |
// We are, for now at least, only going to report RouterLSAs in this method.
|
|
384 |
// What this means is that there is going to be one advertisement with some
|
|
385 |
// number of link records. This means that GetNumLSAs will actually always
|
|
386 |
// return exactly one.
|
|
387 |
//
|
|
388 |
GlobalRouterLSA *pLSA = new GlobalRouterLSA;
|
|
389 |
pLSA->SetLinkStateId (m_routerId);
|
|
390 |
pLSA->SetAdvertisingRouter (m_routerId);
|
|
391 |
pLSA->SetStatus (GlobalRouterLSA::LSA_SPF_NOT_EXPLORED);
|
|
392 |
//
|
|
393 |
// We need to ask the node for the number of net devices attached. This isn't
|
|
394 |
// necessarily equal to the number of links to adjacent nodes (other routers)
|
|
395 |
// as the number of devices may include those for stub networks (e.g.,
|
|
396 |
// ethernets, etc.). So we have to walk through the list of net devices and
|
|
397 |
// pay attention to those that are directly connected to another router through
|
|
398 |
// a point-to-point channel.
|
|
399 |
//
|
|
400 |
uint32_t numDevices = m_node->GetNDevices();
|
|
401 |
NS_DEBUG("GlobalRouter::DiscoverLSAs (): numDevices = " << numDevices);
|
|
402 |
//
|
|
403 |
// Loop through the devices looking for those connected to a point-to-point
|
|
404 |
// channel.
|
|
405 |
//
|
|
406 |
for (uint32_t i = 0; i < numDevices; ++i)
|
|
407 |
{
|
|
408 |
Ptr<NetDevice> ndLocal = m_node->GetDevice(i);
|
|
409 |
|
|
410 |
if (!ndLocal->IsPointToPoint ())
|
|
411 |
{
|
|
412 |
NS_DEBUG("GlobalRouter::DiscoverLSAs (): non-point-to-point device");
|
|
413 |
continue;
|
|
414 |
}
|
|
415 |
|
|
416 |
NS_DEBUG("GlobalRouter::DiscoverLSAs (): Point-to-point device");
|
|
417 |
//
|
|
418 |
// Now, we have to find the Ipv4 interface whose netdevice is the one we
|
|
419 |
// just found. This is still the IP on the local side of the channel. There
|
|
420 |
// is a function to do this used down in the guts of the stack, but it's not
|
|
421 |
// exported so we had to whip up an equivalent.
|
|
422 |
//
|
|
423 |
uint32_t ifIndexLocal = FindIfIndexForDevice(m_node, ndLocal);
|
|
424 |
//
|
|
425 |
// Now that we have the Ipv4 interface index, we can get the address and mask
|
|
426 |
// we need.
|
|
427 |
//
|
|
428 |
Ipv4Address addrLocal = ipv4Local->GetAddress(ifIndexLocal);
|
|
429 |
Ipv4Mask maskLocal = ipv4Local->GetNetworkMask(ifIndexLocal);
|
|
430 |
NS_DEBUG("Working with local address " << addrLocal);
|
|
431 |
//
|
|
432 |
// Now, we're going to walk over to the remote net device on the other end of
|
|
433 |
// the point-to-point channel we now know we have. This is where our adjacent
|
|
434 |
// router (to use OSPF lingo) is running.
|
|
435 |
//
|
|
436 |
Ptr<Channel> ch = ndLocal->GetChannel();
|
|
437 |
Ptr<NetDevice> ndRemote = GetAdjacent(ndLocal, ch);
|
|
438 |
//
|
|
439 |
// The adjacent net device is aggregated to a node. We need to ask that net
|
|
440 |
// device for its node, then ask that node for its Ipv4 interface.
|
|
441 |
//
|
|
442 |
Ptr<Node> nodeRemote = ndRemote->GetNode();
|
|
443 |
Ptr<Ipv4> ipv4Remote = nodeRemote->QueryInterface<Ipv4> (Ipv4::iid);
|
|
444 |
NS_ASSERT_MSG(ipv4Remote,
|
|
445 |
"GlobalRouter::DiscoverLSAs (): QI for remote <Ipv4> failed");
|
|
446 |
//
|
|
447 |
// Per the OSPF spec, we're going to need the remote router ID, so we might as
|
|
448 |
// well get it now.
|
|
449 |
//
|
|
450 |
Ptr<GlobalRouter> srRemote =
|
|
451 |
nodeRemote->QueryInterface<GlobalRouter> (GlobalRouter::iid);
|
|
452 |
NS_ASSERT_MSG(srRemote,
|
|
453 |
"GlobalRouter::DiscoverLSAs (): QI for remote <GlobalRouter> failed");
|
|
454 |
Ipv4Address rtrIdRemote = srRemote->GetRouterId();
|
|
455 |
NS_DEBUG("Working with remote router " << rtrIdRemote);
|
|
456 |
//
|
|
457 |
// Now, just like we did above, we need to get the IP interface index for the
|
|
458 |
// net device on the other end of the point-to-point channel.
|
|
459 |
//
|
|
460 |
uint32_t ifIndexRemote = FindIfIndexForDevice(nodeRemote, ndRemote);
|
|
461 |
//
|
|
462 |
// Now that we have the Ipv4 interface, we can get the (remote) address and
|
|
463 |
// mask we need.
|
|
464 |
//
|
|
465 |
Ipv4Address addrRemote = ipv4Remote->GetAddress(ifIndexRemote);
|
|
466 |
Ipv4Mask maskRemote = ipv4Remote->GetNetworkMask(ifIndexRemote);
|
|
467 |
NS_DEBUG("Working with remote address " << addrRemote);
|
|
468 |
//
|
|
469 |
// Now we can fill out the link records for this link. There are always two
|
|
470 |
// link records; the first is a point-to-point record describing the link and
|
|
471 |
// the second is a stub network record with the network number.
|
|
472 |
//
|
|
473 |
GlobalRouterLinkRecord *plr = new GlobalRouterLinkRecord;
|
|
474 |
plr->SetLinkType (GlobalRouterLinkRecord::PointToPoint);
|
|
475 |
plr->SetLinkId (rtrIdRemote);
|
|
476 |
plr->SetLinkData (addrLocal);
|
|
477 |
pLSA->AddLinkRecord(plr);
|
|
478 |
plr = 0;
|
|
479 |
|
|
480 |
plr = new GlobalRouterLinkRecord;
|
|
481 |
plr->SetLinkType (GlobalRouterLinkRecord::StubNetwork);
|
|
482 |
plr->SetLinkId (addrRemote);
|
|
483 |
plr->SetLinkData (Ipv4Address(maskRemote.GetHostOrder())); // Frown
|
|
484 |
pLSA->AddLinkRecord(plr);
|
|
485 |
plr = 0;
|
|
486 |
}
|
|
487 |
//
|
|
488 |
// The LSA goes on a list of LSAs in case we want to begin exporting other
|
|
489 |
// kinds of advertisements (than Router LSAs).
|
|
490 |
m_LSAs.push_back (pLSA);
|
|
491 |
NS_DEBUG(*pLSA);
|
|
492 |
return m_LSAs.size ();
|
|
493 |
}
|
|
494 |
|
|
495 |
uint32_t
|
|
496 |
GlobalRouter::GetNumLSAs (void) const
|
|
497 |
{
|
|
498 |
NS_DEBUG("GlobalRouter::GetNumLSAs ()");
|
|
499 |
return m_LSAs.size ();
|
|
500 |
}
|
|
501 |
|
|
502 |
//
|
|
503 |
// Get the nth link state advertisement from this router.
|
|
504 |
//
|
|
505 |
bool
|
|
506 |
GlobalRouter::GetLSA (uint32_t n, GlobalRouterLSA &lsa) const
|
|
507 |
{
|
|
508 |
NS_ASSERT_MSG(lsa.IsEmpty(), "GlobalRouter::GetLSA (): Must pass empty LSA");
|
|
509 |
//
|
|
510 |
// All of the work was done in GetNumLSAs. All we have to do here is to
|
|
511 |
// walk the list of link state advertisements created there and return the
|
|
512 |
// one the client is interested in.
|
|
513 |
//
|
|
514 |
ListOfLSAs_t::const_iterator i = m_LSAs.begin ();
|
|
515 |
uint32_t j = 0;
|
|
516 |
|
|
517 |
for (; i != m_LSAs.end (); i++, j++)
|
|
518 |
{
|
|
519 |
if (j == n)
|
|
520 |
{
|
|
521 |
GlobalRouterLSA *p = *i;
|
|
522 |
lsa = *p;
|
|
523 |
return true;
|
|
524 |
}
|
|
525 |
}
|
|
526 |
|
|
527 |
return false;
|
|
528 |
}
|
|
529 |
|
|
530 |
//
|
|
531 |
// Link through the given channel and find the net device that's on the
|
|
532 |
// other end. This only makes sense with a point-to-point channel.
|
|
533 |
//
|
|
534 |
Ptr<NetDevice>
|
|
535 |
GlobalRouter::GetAdjacent(Ptr<NetDevice> nd, Ptr<Channel> ch) const
|
|
536 |
{
|
|
537 |
//
|
|
538 |
// Double-check that channel agrees with device that it's a point-to-point
|
|
539 |
//
|
|
540 |
NS_ASSERT(ch->GetType () == Channel::PointToPoint);
|
|
541 |
|
|
542 |
uint32_t nDevices = ch->GetNDevices();
|
|
543 |
NS_ASSERT_MSG(nDevices == 2,
|
|
544 |
"GlobalRouter::GetAdjacent (): Channel with other than two devices");
|
|
545 |
//
|
|
546 |
// This is a point to point channel with two endpoints. Get both of them.
|
|
547 |
//
|
|
548 |
Ptr<NetDevice> nd1 = ch->GetDevice(0);
|
|
549 |
Ptr<NetDevice> nd2 = ch->GetDevice(1);
|
|
550 |
//
|
|
551 |
// One of the endpoints is going to be "us" -- that is the net device attached
|
|
552 |
// to the node on which we're running -- i.e., "nd". The other endpoint (the
|
|
553 |
// one to which we are connected via the channel) is the adjacent router.
|
|
554 |
//
|
|
555 |
if (nd1 == nd)
|
|
556 |
{
|
|
557 |
return nd2;
|
|
558 |
}
|
|
559 |
else if (nd2 == nd)
|
|
560 |
{
|
|
561 |
return nd1;
|
|
562 |
}
|
|
563 |
else
|
|
564 |
{
|
|
565 |
NS_ASSERT_MSG(false,
|
|
566 |
"GlobalRouter::GetAdjacent (): Wrong or confused channel?");
|
|
567 |
return 0;
|
|
568 |
}
|
|
569 |
}
|
|
570 |
|
|
571 |
//
|
|
572 |
// Given a node and a net device, find the IPV4 interface index that
|
|
573 |
// corresponds to that net device.
|
|
574 |
//
|
|
575 |
uint32_t
|
|
576 |
GlobalRouter::FindIfIndexForDevice(Ptr<Node> node, Ptr<NetDevice> nd) const
|
|
577 |
{
|
|
578 |
Ptr<Ipv4> ipv4 = node->QueryInterface<Ipv4> (Ipv4::iid);
|
|
579 |
NS_ASSERT_MSG(ipv4, "QI for <Ipv4> interface failed");
|
|
580 |
for (uint32_t i = 0; i < ipv4->GetNInterfaces(); ++i )
|
|
581 |
{
|
|
582 |
if (ipv4->GetNetDevice(i) == nd)
|
|
583 |
{
|
|
584 |
return i;
|
|
585 |
}
|
|
586 |
}
|
|
587 |
|
|
588 |
NS_ASSERT_MSG(0, "Cannot find interface for device");
|
|
589 |
return 0;
|
|
590 |
}
|
|
591 |
|
|
592 |
} // namespace ns3
|