1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2004 Francisco J. Ros
4  * Copyright (c) 2007 INESC Porto
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * Authors: Francisco J. Ros  <fjrm@dif.um.es>
20  *          Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
21  */
22 
23 ///
24 /// \file	olsr-state.cc
25 /// \brief	Implementation of all functions needed for manipulating the internal
26 ///		state of an OLSR node.
27 ///
28 
29 #include "olsr-state.h"
30 
31 
32 namespace ns3 {
33 namespace olsr {
34 
35 /********** MPR Selector Set Manipulation **********/
36 
37 MprSelectorTuple*
FindMprSelectorTuple(Ipv4Address const & mainAddr)38 OlsrState::FindMprSelectorTuple (Ipv4Address const &mainAddr)
39 {
40   for (MprSelectorSet::iterator it = m_mprSelectorSet.begin ();
41        it != m_mprSelectorSet.end (); it++)
42     {
43       if (it->mainAddr == mainAddr)
44         {
45           return &(*it);
46         }
47     }
48   return NULL;
49 }
50 
51 void
EraseMprSelectorTuple(const MprSelectorTuple & tuple)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
EraseMprSelectorTuples(const Ipv4Address & mainAddr)66 OlsrState::EraseMprSelectorTuples (const Ipv4Address &mainAddr)
67 {
68   for (MprSelectorSet::iterator it = m_mprSelectorSet.begin ();
69        it != m_mprSelectorSet.end (); )
70     {
71       if (it->mainAddr == mainAddr)
72         {
73           it = m_mprSelectorSet.erase (it);
74         }
75       else
76         {
77           it++;
78         }
79     }
80 }
81 
82 void
InsertMprSelectorTuple(MprSelectorTuple const & tuple)83 OlsrState::InsertMprSelectorTuple (MprSelectorTuple const &tuple)
84 {
85   m_mprSelectorSet.push_back (tuple);
86 }
87 
88 std::string
PrintMprSelectorSet() const89 OlsrState::PrintMprSelectorSet () const
90 {
91   std::ostringstream os;
92   os << "[";
93   for (MprSelectorSet::const_iterator iter = m_mprSelectorSet.begin ();
94        iter != m_mprSelectorSet.end (); iter++)
95     {
96       MprSelectorSet::const_iterator next = iter;
97       next++;
98       os << iter->mainAddr;
99       if (next != m_mprSelectorSet.end ())
100         {
101           os << ", ";
102         }
103     }
104   os << "]";
105   return os.str ();
106 }
107 
108 
109 /********** Neighbor Set Manipulation **********/
110 
111 NeighborTuple*
FindNeighborTuple(Ipv4Address const & mainAddr)112 OlsrState::FindNeighborTuple (Ipv4Address const &mainAddr)
113 {
114   for (NeighborSet::iterator it = m_neighborSet.begin ();
115        it != m_neighborSet.end (); it++)
116     {
117       if (it->neighborMainAddr == mainAddr)
118         {
119           return &(*it);
120         }
121     }
122   return NULL;
123 }
124 
125 const NeighborTuple*
FindSymNeighborTuple(Ipv4Address const & mainAddr) const126 OlsrState::FindSymNeighborTuple (Ipv4Address const &mainAddr) const
127 {
128   for (NeighborSet::const_iterator it = m_neighborSet.begin ();
129        it != m_neighborSet.end (); it++)
130     {
131       if (it->neighborMainAddr == mainAddr && it->status == NeighborTuple::STATUS_SYM)
132         {
133           return &(*it);
134         }
135     }
136   return NULL;
137 }
138 
139 NeighborTuple*
FindNeighborTuple(Ipv4Address const & mainAddr,uint8_t willingness)140 OlsrState::FindNeighborTuple (Ipv4Address const &mainAddr, uint8_t willingness)
141 {
142   for (NeighborSet::iterator it = m_neighborSet.begin ();
143        it != m_neighborSet.end (); it++)
144     {
145       if (it->neighborMainAddr == mainAddr && it->willingness == willingness)
146         {
147           return &(*it);
148         }
149     }
150   return NULL;
151 }
152 
153 void
EraseNeighborTuple(const NeighborTuple & tuple)154 OlsrState::EraseNeighborTuple (const NeighborTuple &tuple)
155 {
156   for (NeighborSet::iterator it = m_neighborSet.begin ();
157        it != m_neighborSet.end (); it++)
158     {
159       if (*it == tuple)
160         {
161           m_neighborSet.erase (it);
162           break;
163         }
164     }
165 }
166 
167 void
EraseNeighborTuple(const Ipv4Address & mainAddr)168 OlsrState::EraseNeighborTuple (const Ipv4Address &mainAddr)
169 {
170   for (NeighborSet::iterator it = m_neighborSet.begin ();
171        it != m_neighborSet.end (); it++)
172     {
173       if (it->neighborMainAddr == mainAddr)
174         {
175           it = m_neighborSet.erase (it);
176           break;
177         }
178     }
179 }
180 
181 void
InsertNeighborTuple(NeighborTuple const & tuple)182 OlsrState::InsertNeighborTuple (NeighborTuple const &tuple)
183 {
184   for (NeighborSet::iterator it = m_neighborSet.begin ();
185        it != m_neighborSet.end (); it++)
186     {
187       if (it->neighborMainAddr == tuple.neighborMainAddr)
188         {
189           // Update it
190           *it = tuple;
191           return;
192         }
193     }
194   m_neighborSet.push_back (tuple);
195 }
196 
197 /********** Neighbor 2 Hop Set Manipulation **********/
198 
199 TwoHopNeighborTuple*
FindTwoHopNeighborTuple(Ipv4Address const & neighborMainAddr,Ipv4Address const & twoHopNeighborAddr)200 OlsrState::FindTwoHopNeighborTuple (Ipv4Address const &neighborMainAddr,
201                                     Ipv4Address const &twoHopNeighborAddr)
202 {
203   for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin ();
204        it != m_twoHopNeighborSet.end (); it++)
205     {
206       if (it->neighborMainAddr == neighborMainAddr
207           && it->twoHopNeighborAddr == twoHopNeighborAddr)
208         {
209           return &(*it);
210         }
211     }
212   return NULL;
213 }
214 
215 void
EraseTwoHopNeighborTuple(const TwoHopNeighborTuple & tuple)216 OlsrState::EraseTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple)
217 {
218   for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin ();
219        it != m_twoHopNeighborSet.end (); it++)
220     {
221       if (*it == tuple)
222         {
223           m_twoHopNeighborSet.erase (it);
224           break;
225         }
226     }
227 }
228 
229 void
EraseTwoHopNeighborTuples(const Ipv4Address & neighborMainAddr,const Ipv4Address & twoHopNeighborAddr)230 OlsrState::EraseTwoHopNeighborTuples (const Ipv4Address &neighborMainAddr,
231                                       const Ipv4Address &twoHopNeighborAddr)
232 {
233   for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin ();
234        it != m_twoHopNeighborSet.end (); )
235     {
236       if (it->neighborMainAddr == neighborMainAddr
237           && it->twoHopNeighborAddr == twoHopNeighborAddr)
238         {
239           it = m_twoHopNeighborSet.erase (it);
240         }
241       else
242         {
243           it++;
244         }
245     }
246 }
247 
248 void
EraseTwoHopNeighborTuples(const Ipv4Address & neighborMainAddr)249 OlsrState::EraseTwoHopNeighborTuples (const Ipv4Address &neighborMainAddr)
250 {
251   for (TwoHopNeighborSet::iterator it = m_twoHopNeighborSet.begin ();
252        it != m_twoHopNeighborSet.end (); )
253     {
254       if (it->neighborMainAddr == neighborMainAddr)
255         {
256           it = m_twoHopNeighborSet.erase (it);
257         }
258       else
259         {
260           it++;
261         }
262     }
263 }
264 
265 void
InsertTwoHopNeighborTuple(TwoHopNeighborTuple const & tuple)266 OlsrState::InsertTwoHopNeighborTuple (TwoHopNeighborTuple const &tuple)
267 {
268   m_twoHopNeighborSet.push_back (tuple);
269 }
270 
271 /********** MPR Set Manipulation **********/
272 
273 bool
FindMprAddress(Ipv4Address const & addr)274 OlsrState::FindMprAddress (Ipv4Address const &addr)
275 {
276   MprSet::iterator it = m_mprSet.find (addr);
277   return (it != m_mprSet.end ());
278 }
279 
280 void
SetMprSet(MprSet mprSet)281 OlsrState::SetMprSet (MprSet mprSet)
282 {
283   m_mprSet = mprSet;
284 }
285 MprSet
GetMprSet() const286 OlsrState::GetMprSet () const
287 {
288   return m_mprSet;
289 }
290 
291 /********** Duplicate Set Manipulation **********/
292 
293 DuplicateTuple*
FindDuplicateTuple(Ipv4Address const & addr,uint16_t sequenceNumber)294 OlsrState::FindDuplicateTuple (Ipv4Address const &addr, uint16_t sequenceNumber)
295 {
296   for (DuplicateSet::iterator it = m_duplicateSet.begin ();
297        it != m_duplicateSet.end (); it++)
298     {
299       if (it->address == addr && it->sequenceNumber == sequenceNumber)
300         {
301           return &(*it);
302         }
303     }
304   return NULL;
305 }
306 
307 void
EraseDuplicateTuple(const DuplicateTuple & tuple)308 OlsrState::EraseDuplicateTuple (const DuplicateTuple &tuple)
309 {
310   for (DuplicateSet::iterator it = m_duplicateSet.begin ();
311        it != m_duplicateSet.end (); it++)
312     {
313       if (*it == tuple)
314         {
315           m_duplicateSet.erase (it);
316           break;
317         }
318     }
319 }
320 
321 void
InsertDuplicateTuple(DuplicateTuple const & tuple)322 OlsrState::InsertDuplicateTuple (DuplicateTuple const &tuple)
323 {
324   m_duplicateSet.push_back (tuple);
325 }
326 
327 /********** Link Set Manipulation **********/
328 
329 LinkTuple*
FindLinkTuple(Ipv4Address const & ifaceAddr)330 OlsrState::FindLinkTuple (Ipv4Address const & ifaceAddr)
331 {
332   for (LinkSet::iterator it = m_linkSet.begin ();
333        it != m_linkSet.end (); it++)
334     {
335       if (it->neighborIfaceAddr == ifaceAddr)
336         {
337           return &(*it);
338         }
339     }
340   return NULL;
341 }
342 
343 LinkTuple*
FindSymLinkTuple(Ipv4Address const & ifaceAddr,Time now)344 OlsrState::FindSymLinkTuple (Ipv4Address const &ifaceAddr, Time now)
345 {
346   for (LinkSet::iterator it = m_linkSet.begin ();
347        it != m_linkSet.end (); it++)
348     {
349       if (it->neighborIfaceAddr == ifaceAddr)
350         {
351           if (it->symTime > now)
352             {
353               return &(*it);
354             }
355           else
356             {
357               break;
358             }
359         }
360     }
361   return NULL;
362 }
363 
364 void
EraseLinkTuple(const LinkTuple & tuple)365 OlsrState::EraseLinkTuple (const LinkTuple &tuple)
366 {
367   for (LinkSet::iterator it = m_linkSet.begin ();
368        it != m_linkSet.end (); it++)
369     {
370       if (*it == tuple)
371         {
372           m_linkSet.erase (it);
373           break;
374         }
375     }
376 }
377 
378 LinkTuple&
InsertLinkTuple(LinkTuple const & tuple)379 OlsrState::InsertLinkTuple (LinkTuple const &tuple)
380 {
381   m_linkSet.push_back (tuple);
382   return m_linkSet.back ();
383 }
384 
385 /********** Topology Set Manipulation **********/
386 
387 TopologyTuple*
FindTopologyTuple(Ipv4Address const & destAddr,Ipv4Address const & lastAddr)388 OlsrState::FindTopologyTuple (Ipv4Address const &destAddr,
389                               Ipv4Address const &lastAddr)
390 {
391   for (TopologySet::iterator it = m_topologySet.begin ();
392        it != m_topologySet.end (); it++)
393     {
394       if (it->destAddr == destAddr && it->lastAddr == lastAddr)
395         {
396           return &(*it);
397         }
398     }
399   return NULL;
400 }
401 
402 TopologyTuple*
FindNewerTopologyTuple(Ipv4Address const & lastAddr,uint16_t ansn)403 OlsrState::FindNewerTopologyTuple (Ipv4Address const & lastAddr, uint16_t ansn)
404 {
405   for (TopologySet::iterator it = m_topologySet.begin ();
406        it != m_topologySet.end (); it++)
407     {
408       if (it->lastAddr == lastAddr && it->sequenceNumber > ansn)
409         {
410           return &(*it);
411         }
412     }
413   return NULL;
414 }
415 
416 void
EraseTopologyTuple(const TopologyTuple & tuple)417 OlsrState::EraseTopologyTuple (const TopologyTuple &tuple)
418 {
419   for (TopologySet::iterator it = m_topologySet.begin ();
420        it != m_topologySet.end (); it++)
421     {
422       if (*it == tuple)
423         {
424           m_topologySet.erase (it);
425           break;
426         }
427     }
428 }
429 
430 void
EraseOlderTopologyTuples(const Ipv4Address & lastAddr,uint16_t ansn)431 OlsrState::EraseOlderTopologyTuples (const Ipv4Address &lastAddr, uint16_t ansn)
432 {
433   for (TopologySet::iterator it = m_topologySet.begin ();
434        it != m_topologySet.end (); )
435     {
436       if (it->lastAddr == lastAddr && it->sequenceNumber < ansn)
437         {
438           it = m_topologySet.erase (it);
439         }
440       else
441         {
442           it++;
443         }
444     }
445 }
446 
447 void
InsertTopologyTuple(TopologyTuple const & tuple)448 OlsrState::InsertTopologyTuple (TopologyTuple const &tuple)
449 {
450   m_topologySet.push_back (tuple);
451 }
452 
453 /********** Interface Association Set Manipulation **********/
454 
455 IfaceAssocTuple*
FindIfaceAssocTuple(Ipv4Address const & ifaceAddr)456 OlsrState::FindIfaceAssocTuple (Ipv4Address const &ifaceAddr)
457 {
458   for (IfaceAssocSet::iterator it = m_ifaceAssocSet.begin ();
459        it != m_ifaceAssocSet.end (); it++)
460     {
461       if (it->ifaceAddr == ifaceAddr)
462         {
463           return &(*it);
464         }
465     }
466   return NULL;
467 }
468 
469 const IfaceAssocTuple*
FindIfaceAssocTuple(Ipv4Address const & ifaceAddr) const470 OlsrState::FindIfaceAssocTuple (Ipv4Address const &ifaceAddr) const
471 {
472   for (IfaceAssocSet::const_iterator it = m_ifaceAssocSet.begin ();
473        it != m_ifaceAssocSet.end (); it++)
474     {
475       if (it->ifaceAddr == ifaceAddr)
476         {
477           return &(*it);
478         }
479     }
480   return NULL;
481 }
482 
483 void
EraseIfaceAssocTuple(const IfaceAssocTuple & tuple)484 OlsrState::EraseIfaceAssocTuple (const IfaceAssocTuple &tuple)
485 {
486   for (IfaceAssocSet::iterator it = m_ifaceAssocSet.begin ();
487        it != m_ifaceAssocSet.end (); it++)
488     {
489       if (*it == tuple)
490         {
491           m_ifaceAssocSet.erase (it);
492           break;
493         }
494     }
495 }
496 
497 void
InsertIfaceAssocTuple(const IfaceAssocTuple & tuple)498 OlsrState::InsertIfaceAssocTuple (const IfaceAssocTuple &tuple)
499 {
500   m_ifaceAssocSet.push_back (tuple);
501 }
502 
503 std::vector<Ipv4Address>
FindNeighborInterfaces(const Ipv4Address & neighborMainAddr) const504 OlsrState::FindNeighborInterfaces (const Ipv4Address &neighborMainAddr) const
505 {
506   std::vector<Ipv4Address> retval;
507   for (IfaceAssocSet::const_iterator it = m_ifaceAssocSet.begin ();
508        it != m_ifaceAssocSet.end (); it++)
509     {
510       if (it->mainAddr == neighborMainAddr)
511         {
512           retval.push_back (it->ifaceAddr);
513         }
514     }
515   return retval;
516 }
517 
518 /********** Host-Network Association Set Manipulation **********/
519 
520 AssociationTuple*
FindAssociationTuple(const Ipv4Address & gatewayAddr,const Ipv4Address & networkAddr,const Ipv4Mask & netmask)521 OlsrState::FindAssociationTuple (const Ipv4Address &gatewayAddr, const Ipv4Address &networkAddr, const Ipv4Mask &netmask)
522 {
523   for (AssociationSet::iterator it = m_associationSet.begin ();
524        it != m_associationSet.end (); it++)
525     {
526       if (it->gatewayAddr == gatewayAddr and it->networkAddr == networkAddr and it->netmask == netmask)
527         {
528           return &(*it);
529         }
530     }
531   return NULL;
532 }
533 
534 void
EraseAssociationTuple(const AssociationTuple & tuple)535 OlsrState::EraseAssociationTuple (const AssociationTuple &tuple)
536 {
537   for (AssociationSet::iterator it = m_associationSet.begin ();
538        it != m_associationSet.end (); it++)
539     {
540       if (*it == tuple)
541         {
542           m_associationSet.erase (it);
543           break;
544         }
545     }
546 }
547 
548 void
InsertAssociationTuple(const AssociationTuple & tuple)549 OlsrState::InsertAssociationTuple (const AssociationTuple &tuple)
550 {
551   m_associationSet.push_back (tuple);
552 }
553 
554 void
EraseAssociation(const Association & tuple)555 OlsrState::EraseAssociation (const Association &tuple)
556 {
557   for (Associations::iterator it = m_associations.begin ();
558        it != m_associations.end (); it++)
559     {
560       if (*it == tuple)
561         {
562           m_associations.erase (it);
563           break;
564         }
565     }
566 }
567 
568 void
InsertAssociation(const Association & tuple)569 OlsrState::InsertAssociation (const Association &tuple)
570 {
571   m_associations.push_back (tuple);
572 }
573 
574 }
575 }  // namespace olsr, ns3
576