|
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /*************************************************************************** |
|
3 * Copyright (C) 2004 by Francisco J. Ros * |
|
4 * fjrm@dif.um.es * |
|
5 * * |
|
6 * Modified for NS-3 by Gustavo J. A. M. Carneiro on 2007 * |
|
7 * gjc@inescporto.pt * |
|
8 * * |
|
9 * This program is free software; you can redistribute it and/or modify * |
|
10 * it under the terms of the GNU General Public License as published by * |
|
11 * the Free Software Foundation; either version 2 of the License, or * |
|
12 * (at your option) any later version. * |
|
13 * * |
|
14 * This program is distributed in the hope that it will be useful, * |
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|
17 * GNU General Public License for more details. * |
|
18 * * |
|
19 * You should have received a copy of the GNU General Public License * |
|
20 * along with this program; if not, write to the * |
|
21 * Free Software Foundation, Inc., * |
|
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|
23 ***************************************************************************/ |
|
24 |
|
25 /// |
|
26 /// \file OlsrState.cc |
|
27 /// \brief Implementation of all functions needed for manipulating the internal |
|
28 /// state of an OLSR node. |
|
29 /// |
|
30 |
|
31 #include "olsr-state.h" |
|
32 |
|
33 |
|
34 namespace ns3 { namespace olsr { |
|
35 |
|
36 |
|
37 /********** MPR Selector Set Manipulation **********/ |
|
38 |
|
39 MprSelectorTuple* |
|
40 OlsrState::FindMprSelectorTuple (Ipv4Address const &mainAddr) |
|
41 { |
|
42 for (MprSelectorSet::iterator it = m_mprSelectorSet.begin (); |
|
43 it != m_mprSelectorSet.end (); it++) |
|
44 { |
|
45 if (it->mainAddr == mainAddr) |
|
46 return &(*it); |
|
47 } |
|
48 return NULL; |
|
49 } |
|
50 |
|
51 void |
|
52 OlsrState::EraseMprSelectorTuple (const MprSelectorTuple &tuple) |
|
53 { |
|
54 for (MprSelectorSet::iterator it = m_mprSelectorSet.begin (); |
|
55 it != m_mprSelectorSet.end (); it++) |
|
56 { |
|
57 if (*it == tuple) |
|
58 { |
|
59 m_mprSelectorSet.erase (it); |
|
60 break; |
|
61 } |
|
62 } |
|
63 } |
|
64 |
|
65 void |
|
66 OlsrState::EraseMprSelectorTuples (const Ipv4Address &mainAddr) |
|
67 { |
|
68 for (MprSelectorSet::iterator it = m_mprSelectorSet.begin (); |
|
69 it != m_mprSelectorSet.end (); it++) |
|
70 { |
|
71 if (it->mainAddr == mainAddr) |
|
72 { |
|
73 it = m_mprSelectorSet.erase (it); |
|
74 it--; |
|
75 } |
|
76 } |
|
77 } |
|
78 |
|
79 void |
|
80 OlsrState::InsertMprSelectorTuple (MprSelectorTuple const &tuple) |
|
81 { |
|
82 m_mprSelectorSet.push_back (tuple); |
|
83 } |
|
84 |
|
85 /********** Neighbor Set Manipulation **********/ |
|
86 |
|
87 NeighborTuple* |
|
88 OlsrState::FindNeighborTuple (Ipv4Address const &mainAddr) |
|
89 { |
|
90 for (NeighborSet::iterator it = m_neighborSet.begin (); |
|
91 it != m_neighborSet.end (); it++) |
|
92 { |
|
93 if (it->neighborMainAddr == mainAddr) |
|
94 return &(*it); |
|
95 } |
|
96 return NULL; |
|
97 } |
|
98 |
|
99 NeighborTuple* |
|
100 OlsrState::FindSymNeighborTuple (Ipv4Address const &mainAddr) |
|
101 { |
|
102 for (NeighborSet::iterator it = m_neighborSet.begin (); |
|
103 it != m_neighborSet.end (); it++) |
|
104 { |
|
105 if (it->neighborMainAddr == mainAddr && it->status == NeighborTuple::STATUS_SYM) |
|
106 return &(*it); |
|
107 } |
|
108 return NULL; |
|
109 } |
|
110 |
|
111 NeighborTuple* |
|
112 OlsrState::FindNeighborTuple (Ipv4Address const &mainAddr, uint8_t willingness) |
|
113 { |
|
114 for (NeighborSet::iterator it = m_neighborSet.begin (); |
|
115 it != m_neighborSet.end (); it++) |
|
116 { |
|
117 if (it->neighborMainAddr == mainAddr && it->willingness == willingness) |
|
118 return &(*it); |
|
119 } |
|
120 return NULL; |
|
121 } |
|
122 |
|
123 void |
|
124 OlsrState::EraseNeighborTuple (const NeighborTuple &tuple) |
|
125 { |
|
126 for (NeighborSet::iterator it = m_neighborSet.begin (); |
|
127 it != m_neighborSet.end (); it++) |
|
128 { |
|
129 if (*it == tuple) |
|
130 { |
|
131 m_neighborSet.erase (it); |
|
132 break; |
|
133 } |
|
134 } |
|
135 } |
|
136 |
|
137 void |
|
138 OlsrState::EraseNeighborTuple (const Ipv4Address &mainAddr) |
|
139 { |
|
140 for (NeighborSet::iterator it = m_neighborSet.begin (); |
|
141 it != m_neighborSet.end (); it++) |
|
142 { |
|
143 if (it->neighborMainAddr == mainAddr) |
|
144 { |
|
145 it = m_neighborSet.erase (it); |
|
146 break; |
|
147 } |
|
148 } |
|
149 } |
|
150 |
|
151 void |
|
152 OlsrState::InsertNeighborTuple (NeighborTuple const &tuple) |
|
153 { |
|
154 m_neighborSet.push_back (tuple); |
|
155 } |
|
156 |
|
157 /********** Neighbor 2 Hop Set Manipulation **********/ |
|
158 |
|
159 TwoHopNeighborTuple* |
|
160 OlsrState::FindTwoHopNeighborTuple (Ipv4Address const &neighborMainAddr, |
|
161 Ipv4Address const &twoHopNeighborAddr) |
|
162 { |
|
163 for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin (); |
|
164 it != m_twoHopNeighborSet.end (); it++) |
|
165 { |
|
166 if (it->neighborMainAddr == neighborMainAddr |
|
167 && it->twoHopNeighborAddr == twoHopNeighborAddr) |
|
168 { |
|
169 return &(*it); |
|
170 } |
|
171 } |
|
172 return NULL; |
|
173 } |
|
174 |
|
175 void |
|
176 OlsrState::EraseTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple) |
|
177 { |
|
178 for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin (); |
|
179 it != m_twoHopNeighborSet.end (); it++) |
|
180 { |
|
181 if (*it == tuple) |
|
182 { |
|
183 m_twoHopNeighborSet.erase(it); |
|
184 break; |
|
185 } |
|
186 } |
|
187 } |
|
188 |
|
189 void |
|
190 OlsrState::EraseTwoHopNeighborTuples (const Ipv4Address &neighborMainAddr, |
|
191 const Ipv4Address &twoHopNeighborAddr) |
|
192 { |
|
193 for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin (); |
|
194 it != m_twoHopNeighborSet.end (); it++) |
|
195 { |
|
196 if (it->neighborMainAddr == neighborMainAddr |
|
197 && it->twoHopNeighborAddr == twoHopNeighborAddr) |
|
198 { |
|
199 it = m_twoHopNeighborSet.erase (it); |
|
200 it--; // FIXME: is this correct in the case 'it' pointed to the first element? |
|
201 } |
|
202 } |
|
203 } |
|
204 |
|
205 void |
|
206 OlsrState::EraseTwoHopNeighborTuples (const Ipv4Address &neighborMainAddr) |
|
207 { |
|
208 for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin (); |
|
209 it != m_twoHopNeighborSet.end (); it++) |
|
210 { |
|
211 if (it->neighborMainAddr == neighborMainAddr) { |
|
212 it = m_twoHopNeighborSet.erase (it); |
|
213 it--; |
|
214 } |
|
215 } |
|
216 } |
|
217 |
|
218 void |
|
219 OlsrState::InsertTwoHopNeighborTuple (TwoHopNeighborTuple const &tuple){ |
|
220 m_twoHopNeighborSet.push_back (tuple); |
|
221 } |
|
222 |
|
223 /********** MPR Set Manipulation **********/ |
|
224 |
|
225 bool |
|
226 OlsrState::FindMprAddress (Ipv4Address const &addr) |
|
227 { |
|
228 MprSet::iterator it = m_mprSet.find (addr); |
|
229 return (it != m_mprSet.end ()); |
|
230 } |
|
231 |
|
232 void |
|
233 OlsrState::InsertMprAddress (Ipv4Address const & addr) |
|
234 { |
|
235 m_mprSet.insert (addr); |
|
236 } |
|
237 |
|
238 void |
|
239 OlsrState::ClearMprSet () |
|
240 { |
|
241 m_mprSet.clear (); |
|
242 } |
|
243 |
|
244 /********** Duplicate Set Manipulation **********/ |
|
245 |
|
246 DuplicateTuple* |
|
247 OlsrState::FindDuplicateTuple (Ipv4Address const &addr, uint16_t sequenceNumber) |
|
248 { |
|
249 for (DuplicateSet::iterator it = m_duplicateSet.begin (); |
|
250 it != m_duplicateSet.end(); it++) |
|
251 { |
|
252 if (it->address == addr && it->sequenceNumber == sequenceNumber) |
|
253 return &(*it); |
|
254 } |
|
255 return NULL; |
|
256 } |
|
257 |
|
258 void |
|
259 OlsrState::EraseDuplicateTuple (const DuplicateTuple &tuple) |
|
260 { |
|
261 for (DuplicateSet::iterator it = m_duplicateSet.begin (); |
|
262 it != m_duplicateSet.end (); it++) |
|
263 { |
|
264 if (*it == tuple) |
|
265 { |
|
266 m_duplicateSet.erase (it); |
|
267 break; |
|
268 } |
|
269 } |
|
270 } |
|
271 |
|
272 void |
|
273 OlsrState::InsertDuplicateTuple (DuplicateTuple const &tuple) |
|
274 { |
|
275 m_duplicateSet.push_back (tuple); |
|
276 } |
|
277 |
|
278 /********** Link Set Manipulation **********/ |
|
279 |
|
280 LinkTuple* |
|
281 OlsrState::FindLinkTuple (Ipv4Address const & ifaceAddr) |
|
282 { |
|
283 for (LinkSet::iterator it = m_linkSet.begin (); |
|
284 it != m_linkSet.end (); it++) |
|
285 { |
|
286 if (it->neighborIfaceAddr == ifaceAddr) |
|
287 return &(*it); |
|
288 } |
|
289 return NULL; |
|
290 } |
|
291 |
|
292 LinkTuple* |
|
293 OlsrState::FindSymLinkTuple (Ipv4Address const &ifaceAddr, Time now) |
|
294 { |
|
295 for (LinkSet::iterator it = m_linkSet.begin (); |
|
296 it != m_linkSet.end (); it++) |
|
297 { |
|
298 if (it->neighborIfaceAddr == ifaceAddr) |
|
299 { |
|
300 if (it->symTime > now) |
|
301 return &(*it); |
|
302 else |
|
303 break; |
|
304 } |
|
305 } |
|
306 return NULL; |
|
307 } |
|
308 |
|
309 void |
|
310 OlsrState::EraseLinkTuple (const LinkTuple &tuple) |
|
311 { |
|
312 for (LinkSet::iterator it = m_linkSet.begin (); |
|
313 it != m_linkSet.end (); it++) |
|
314 { |
|
315 if (*it == tuple) |
|
316 { |
|
317 m_linkSet.erase (it); |
|
318 break; |
|
319 } |
|
320 } |
|
321 } |
|
322 |
|
323 LinkTuple& |
|
324 OlsrState::InsertLinkTuple (LinkTuple const &tuple) |
|
325 { |
|
326 m_linkSet.push_back (tuple); |
|
327 return m_linkSet.back (); |
|
328 } |
|
329 |
|
330 /********** Topology Set Manipulation **********/ |
|
331 |
|
332 TopologyTuple* |
|
333 OlsrState::FindTopologyTuple (Ipv4Address const &destAddr, |
|
334 Ipv4Address const &lastAddr) |
|
335 { |
|
336 for (TopologySet::iterator it = m_topologySet.begin (); |
|
337 it != m_topologySet.end (); it++) |
|
338 { |
|
339 if (it->destAddr == destAddr && it->lastAddr == lastAddr) |
|
340 return &(*it); |
|
341 } |
|
342 return NULL; |
|
343 } |
|
344 |
|
345 TopologyTuple* |
|
346 OlsrState::FindNewerTopologyTuple (Ipv4Address const & lastAddr, uint16_t ansn) |
|
347 { |
|
348 for (TopologySet::iterator it = m_topologySet.begin (); |
|
349 it != m_topologySet.end (); it++) |
|
350 { |
|
351 if (it->lastAddr == lastAddr && it->sequenceNumber > ansn) |
|
352 return &(*it); |
|
353 } |
|
354 return NULL; |
|
355 } |
|
356 |
|
357 void |
|
358 OlsrState::EraseTopologyTuple(const TopologyTuple &tuple) |
|
359 { |
|
360 for (TopologySet::iterator it = m_topologySet.begin (); |
|
361 it != m_topologySet.end (); it++) |
|
362 { |
|
363 if (*it == tuple) |
|
364 { |
|
365 m_topologySet.erase (it); |
|
366 break; |
|
367 } |
|
368 } |
|
369 } |
|
370 |
|
371 void |
|
372 OlsrState::EraseOlderTopologyTuples (const Ipv4Address &lastAddr, uint16_t ansn) |
|
373 { |
|
374 for (TopologySet::iterator it = m_topologySet.begin(); |
|
375 it != m_topologySet.end(); it++) |
|
376 { |
|
377 if (it->lastAddr == lastAddr && it->sequenceNumber < ansn) |
|
378 { |
|
379 it = m_topologySet.erase (it); |
|
380 it--; |
|
381 } |
|
382 } |
|
383 } |
|
384 |
|
385 void |
|
386 OlsrState::InsertTopologyTuple (TopologyTuple const &tuple) |
|
387 { |
|
388 m_topologySet.push_back (tuple); |
|
389 } |
|
390 |
|
391 /********** Interface Association Set Manipulation **********/ |
|
392 |
|
393 IfaceAssocTuple* |
|
394 OlsrState::FindIfaceAssocTuple (Ipv4Address const &ifaceAddr) |
|
395 { |
|
396 for (IfaceAssocSet::iterator it = m_ifaceAssocSet.begin (); |
|
397 it != m_ifaceAssocSet.end (); it++) |
|
398 { |
|
399 if (it->ifaceAddr == ifaceAddr) |
|
400 return &(*it); |
|
401 } |
|
402 return NULL; |
|
403 } |
|
404 |
|
405 void |
|
406 OlsrState::EraseIfaceAssocTuple (const IfaceAssocTuple &tuple) |
|
407 { |
|
408 for (IfaceAssocSet::iterator it = m_ifaceAssocSet.begin (); |
|
409 it != m_ifaceAssocSet.end (); it++) |
|
410 { |
|
411 if (*it == tuple) |
|
412 { |
|
413 m_ifaceAssocSet.erase (it); |
|
414 break; |
|
415 } |
|
416 } |
|
417 } |
|
418 |
|
419 void |
|
420 OlsrState::InsertIfaceAssocTuple (const IfaceAssocTuple &tuple) |
|
421 { |
|
422 m_ifaceAssocSet.push_back (tuple); |
|
423 } |
|
424 |
|
425 std::vector<Ipv4Address> |
|
426 OlsrState::FindNeighborInterfaces (const Ipv4Address &neighborMainAddr) const |
|
427 { |
|
428 std::vector<Ipv4Address> retval; |
|
429 for (IfaceAssocSet::const_iterator it = m_ifaceAssocSet.begin (); |
|
430 it != m_ifaceAssocSet.end (); it++) |
|
431 { |
|
432 if (it->mainAddr == neighborMainAddr) |
|
433 retval.push_back (it->ifaceAddr); |
|
434 } |
|
435 return retval; |
|
436 } |
|
437 |
|
438 }}; // namespace ns3, olsr |