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 #ifndef OLSR_STATE_H
25 #define OLSR_STATE_H
26 
27 #include "olsr-repositories.h"
28 
29 namespace ns3 {
30 namespace olsr {
31 
32 /// \ingroup olsr
33 /// This class encapsulates all data structures needed for maintaining internal state of an OLSR node.
34 class OlsrState
35 {
36   //  friend class Olsr;
37 
38 protected:
39   LinkSet m_linkSet;    //!< Link Set (\RFC{3626}, section 4.2.1).
40   NeighborSet m_neighborSet;            //!< Neighbor Set (\RFC{3626}, section 4.3.1).
41   TwoHopNeighborSet m_twoHopNeighborSet;        //!< 2-hop Neighbor Set (\RFC{3626}, section 4.3.2).
42   TopologySet m_topologySet;    //!< Topology Set (\RFC{3626}, section 4.4).
43   MprSet m_mprSet;      //!< MPR Set (\RFC{3626}, section 4.3.3).
44   MprSelectorSet m_mprSelectorSet;      //!< MPR Selector Set (\RFC{3626}, section 4.3.4).
45   DuplicateSet m_duplicateSet;  //!< Duplicate Set (\RFC{3626}, section 3.4).
46   IfaceAssocSet m_ifaceAssocSet;        //!< Interface Association Set (\RFC{3626}, section 4.1).
47   AssociationSet m_associationSet; //!<	Association Set (\RFC{3626}, section12.2). Associations obtained from HNA messages generated by other nodes.
48   Associations m_associations;  //!< The node's local Host Network Associations that will be advertised using HNA messages.
49 
50 public:
OlsrState()51   OlsrState ()
52   {
53   }
54 
55   // MPR selector
56 
57   /**
58    * Gets the MPR selectors.
59    * \returns The MPR selectors.
60    */
GetMprSelectors()61   const MprSelectorSet & GetMprSelectors () const
62   {
63     return m_mprSelectorSet;
64   }
65 
66   /**
67    * Finds a MPR selector tuple.
68    * \param mainAddr The MPR selector main address.
69    * \returns The MPR selector, if found. Else it returns a null pointer.
70    */
71   MprSelectorTuple* FindMprSelectorTuple (const Ipv4Address &mainAddr);
72 
73   /**
74    * Erases a MPR selector tuple.
75    * \param tuple The MPR selector tuple.
76    */
77   void EraseMprSelectorTuple (const MprSelectorTuple &tuple);
78 
79   /**
80    * Erases all MPR selector tuples belonging to the same address.
81    * \param mainAddr The MPR selector main address.
82    */
83   void EraseMprSelectorTuples (const Ipv4Address &mainAddr);
84 
85   /**
86    * Inserts a MPR selector tuple
87    * \param tuple The MPR selector tuple.
88    */
89   void InsertMprSelectorTuple (const MprSelectorTuple &tuple);
90 
91   /**
92    * Prints the MPR selector sets.
93    * \return a string with the output data.
94    */
95   std::string PrintMprSelectorSet () const;
96 
97   // Neighbor
98 
99   /**
100    * Gets the neighbor set.
101    * \returns The neighbor set.
102    */
GetNeighbors()103   const NeighborSet & GetNeighbors () const
104   {
105     return m_neighborSet;
106   }
107   /**
108    * Gets the neighbor set.
109    * \returns The neighbor set.
110    */
GetNeighbors()111   NeighborSet & GetNeighbors ()
112   {
113     return m_neighborSet;
114   }
115 
116   /**
117    * Finds a neighbor tuple.
118    * \param mainAddr The neighbor tuple main address.
119    * \returns The neighbor tuple, if found. Else it returns a null pointer.
120    */
121   NeighborTuple* FindNeighborTuple (const Ipv4Address &mainAddr);
122 
123   /**
124    * Finds a symmetrical neighbor tuple.
125    * \param mainAddr The neighbor tuple main address.
126    * \returns The neighbor tuple, if found. Else it returns a null pointer.
127    */
128   const NeighborTuple* FindSymNeighborTuple (const Ipv4Address &mainAddr) const;
129 
130   /**
131    * Finds a neighbor tuple.
132    * \param mainAddr The neighbor tuple main address.
133    * \param willingness The neighbor willingness.
134    * \returns The neighbor tuple, if found. Else it returns a null pointer.
135    */
136   NeighborTuple* FindNeighborTuple (const Ipv4Address &mainAddr,
137                                     uint8_t willingness);
138 
139   /**
140    * Erases a neighbor tuple.
141    * \param neighborTuple The neighbor tuple.
142    */
143   void EraseNeighborTuple (const NeighborTuple &neighborTuple);
144   /**
145    * Erases a neighbor tuple.
146    * \param mainAddr The neighbor tuple main address.
147    */
148   void EraseNeighborTuple (const Ipv4Address &mainAddr);
149 
150   /**
151    * Inserts a neighbor tuple.
152    * \param tuple The neighbor tuple.
153    */
154   void InsertNeighborTuple (const NeighborTuple &tuple);
155 
156   // Two-hop neighbor
157 
158   /**
159    * Gets the 2-hop neighbor set.
160    * \returns The 2-hop neighbor set.
161    */
GetTwoHopNeighbors()162   const TwoHopNeighborSet & GetTwoHopNeighbors () const
163   {
164     return m_twoHopNeighborSet;
165   }
166   /**
167    * Gets the 2-hop neighbor set.
168    * \returns The 2-hop neighbor set.
169    */
GetTwoHopNeighbors()170   TwoHopNeighborSet & GetTwoHopNeighbors ()
171   {
172     return m_twoHopNeighborSet;
173   }
174 
175   /**
176    * Finds a 2-hop neighbor tuple.
177    * \param neighbor The neighbor main address.
178    * \param twoHopNeighbor The 2-hop neighbor main address.
179    * \returns The 2-hop neighbor tuple, if found. Else it returns a null pointer.
180    */
181   TwoHopNeighborTuple* FindTwoHopNeighborTuple (const Ipv4Address &neighbor,
182                                                 const Ipv4Address &twoHopNeighbor);
183 
184   /**
185    * Erases a 2-hop neighbor tuple.
186    * \param tuple The 2-hop neighbor tuple.
187    */
188   void EraseTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple);
189   /**
190    * Erases the 2-hop neighbor tuples with the same 1-hop neighbor.
191    * \param neighbor The neighbor address.
192    */
193   void EraseTwoHopNeighborTuples (const Ipv4Address &neighbor);
194   /**
195    * Erases the 2-hop neighbor tuples with matching predicates.
196    * \param neighbor The neighbor address.
197    * \param twoHopNeighbor The 2-hop neighbor main address.
198    */
199   void EraseTwoHopNeighborTuples (const Ipv4Address &neighbor,
200                                   const Ipv4Address &twoHopNeighbor);
201   /**
202    * Inserts a 2-hop neighbor tuple.
203    * \param tuple The 2-hop neighbor tuple.
204    */
205   void InsertTwoHopNeighborTuple (const TwoHopNeighborTuple &tuple);
206 
207   // MPR
208 
209   /**
210    * Checks if there's an MPR with a specific address.
211    * \param address The address to test.
212    * \return True if a MPR with the specified address exists.
213    */
214   bool FindMprAddress (const Ipv4Address &address);
215 
216   /**
217    * Sets the MPR set to the one specified.
218    * \param mprSet The new MPR set.
219    */
220   void SetMprSet (MprSet mprSet);
221 
222   /**
223    * Gets the MPR set.
224    * \return The MPR set.
225    */
226   MprSet GetMprSet () const;
227 
228   // Duplicate
229 
230   /**
231    * Finds a duplicate tuple.
232    * \param address The duplicate tuple address.
233    * \param sequenceNumber The duplicate tuple sequence number.
234    * \returns The duplicate tuple, or a null pointer if no match.
235    */
236   DuplicateTuple* FindDuplicateTuple (const Ipv4Address &address,
237                                       uint16_t sequenceNumber);
238 
239   /**
240    * Erases a duplicate tuple.
241    * \param tuple The tuple to erase.
242    */
243   void EraseDuplicateTuple (const DuplicateTuple &tuple);
244   /**
245    * Inserts a duplicate tuple.
246    * \param tuple The tuple to insert.
247    */
248   void InsertDuplicateTuple (const DuplicateTuple &tuple);
249 
250   // Link
251 
252   /**
253    * Gets the Link set.
254    * \return The Link set.
255    */
GetLinks()256   const LinkSet & GetLinks () const
257   {
258     return m_linkSet;
259   }
260   /**
261    * Finds a link tuple.
262    * \param ifaceAddr The interface address of the link.
263    * \returns The link tuple, or a null pointer if no match.
264    */
265   LinkTuple* FindLinkTuple (const Ipv4Address &ifaceAddr);
266   /**
267    * Finds a symmetrical link tuple.
268    * \param ifaceAddr The interface address of the link.
269    * \param time The time at which the link should be considered symmetrical.
270    * \returns The link tuple, or a null pointer if no match.
271    */
272   LinkTuple* FindSymLinkTuple (const Ipv4Address &ifaceAddr, Time time);
273   /**
274    * Erases a link tuple.
275    * \param tuple The tuple to erase.
276    */
277   void EraseLinkTuple (const LinkTuple &tuple);
278   /**
279    * Inserts a link tuple.
280    * \param tuple The tuple to insert.
281    * \returns A reference to the inserted tuple.
282    */
283   LinkTuple& InsertLinkTuple (const LinkTuple &tuple);
284 
285   // Topology
286 
287   /**
288    * Gets the topology set.
289    * \returns The topology set.
290    */
GetTopologySet()291   const TopologySet & GetTopologySet () const
292   {
293     return m_topologySet;
294   }
295   /**
296    * Finds a topology tuple.
297    * \param destAddr The destination address.
298    * \param lastAddr The address of the node previous to the destination.
299    * \returns The topology tuple, or a null pointer if no match.
300    */
301   TopologyTuple* FindTopologyTuple (const Ipv4Address &destAddr,
302                                     const Ipv4Address &lastAddr);
303   /**
304    * Finds a topology tuple.
305    * \param lastAddr The address of the node previous to the destination.
306    * \param ansn The Advertised Neighbor Sequence Number.
307    * \returns The topology tuple, or a null pointer if no match.
308    */
309   TopologyTuple* FindNewerTopologyTuple (const Ipv4Address &lastAddr,
310                                          uint16_t ansn);
311   /**
312    * Erases a topology tuple.
313    * \param tuple The tuple to erase.
314    */
315   void EraseTopologyTuple (const TopologyTuple &tuple);
316   /**
317    * Erases a topology tuple.
318    * \param lastAddr The address of the node previous to the destination.
319    * \param ansn The Advertised Neighbor Sequence Number.
320    */
321   void EraseOlderTopologyTuples (const Ipv4Address &lastAddr,
322                                  uint16_t ansn);
323   /**
324    * Inserts a topology tuple.
325    * \param tuple The tuple to insert.
326    */
327   void InsertTopologyTuple (const TopologyTuple &tuple);
328 
329   // Interface association
330 
331   /**
332    * Gets the interface association set.
333    * \returns The interface association set.
334    */
GetIfaceAssocSet()335   const IfaceAssocSet & GetIfaceAssocSet () const
336   {
337     return m_ifaceAssocSet;
338   }
339   /**
340    * Gets a mutable reference to the interface association set.
341    * \returns The interface association set.
342    */
GetIfaceAssocSetMutable()343   IfaceAssocSet & GetIfaceAssocSetMutable ()
344   {
345     return m_ifaceAssocSet;
346   }
347 
348   /**
349    * Finds a interface association tuple.
350    * \param ifaceAddr The interface address.
351    * \returns The interface association  tuple, or a null pointer if no match.
352    */
353   IfaceAssocTuple* FindIfaceAssocTuple (const Ipv4Address &ifaceAddr);
354   /**
355    * Finds a interface association tuple.
356    * \param ifaceAddr The interface address.
357    * \returns The interface association  tuple, or a null pointer if no match.
358    */
359   const IfaceAssocTuple* FindIfaceAssocTuple (const Ipv4Address &ifaceAddr) const;
360   /**
361    * Erases a interface association tuple.
362    * \param tuple The tuple to erase.
363    */
364   void EraseIfaceAssocTuple (const IfaceAssocTuple &tuple);
365   /**
366    * Inserts a interface association tuple.
367    * \param tuple The tuple to insert.
368    */
369   void InsertIfaceAssocTuple (const IfaceAssocTuple &tuple);
370 
371   // Host-Network Association
372   /**
373    * Gets the association set known to the node.
374    * \returns The association set known to the node.
375    */
GetAssociationSet()376   const AssociationSet & GetAssociationSet () const  // Associations known to the node
377   {
378     return m_associationSet;
379   }
380 
381   /**
382    * Gets the association set the node has.
383    * \returns The association set the node has.
384    */
GetAssociations()385   const Associations & GetAssociations () const  // Set of associations that the node has
386   {
387     return m_associations;
388   }
389 
390   /**
391    * Finds an association tuple.
392    * \param gatewayAddr The gateway address.
393    * \param networkAddr The network address.
394    * \param netmask The network mask.
395    * \returns The association  tuple, or a null pointer if no match.
396    */
397   AssociationTuple* FindAssociationTuple (const Ipv4Address &gatewayAddr, \
398                                           const Ipv4Address &networkAddr, \
399                                           const Ipv4Mask &netmask);
400   /**
401    * Erases a known association tuple.
402    * \param tuple The tuple to erase.
403    */
404   void EraseAssociationTuple (const AssociationTuple &tuple);
405   /**
406    * Inserts a known association tuple.
407    * \param tuple The tuple to insert.
408    */
409   void InsertAssociationTuple (const AssociationTuple &tuple);
410   /**
411    * Erases an association.
412    * \param tuple The tuple to erase.
413    */
414   void EraseAssociation (const Association &tuple);
415   /**
416    * Inserts an association tuple.
417    * \param tuple The tuple to insert.
418    */
419   void InsertAssociation (const Association &tuple);
420 
421   /**
422    * Returns a vector of all interfaces of a given neighbor, with the
423    * exception of the "main" one.
424    * \param neighborMainAddr The neighbor main address
425    * \returns A container of the neighbor addresses (excluding the main one).
426    */
427   std::vector<Ipv4Address>
428   FindNeighborInterfaces (const Ipv4Address &neighborMainAddr) const;
429 
430 };
431 
432 }
433 }  // namespace olsr,ns3
434 
435 #endif /* OLSR_STATE_H */
436