|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2009 IITP RAS |
|
4 * |
|
5 * 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 * published by the Free Software Foundation; |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 * |
|
18 * Based on |
|
19 * NS-2 AODV model developed by the CMU/MONARCH group and optimized and |
|
20 * tuned by Samir Das and Mahesh Marina, University of Cincinnati; |
|
21 * |
|
22 * AODV-UU implementation by Erik Nordström of Uppsala University |
|
23 * http://core.it.uu.se/core/index.php/AODV-UU |
|
24 * |
|
25 * Authors: Elena Buchatskaia <borovkovaes@iitp.ru> |
|
26 * Pavel Boyko <boyko@iitp.ru> |
|
27 */ |
|
28 #ifndef AODV_RTABLE_H |
|
29 #define AODV_RTABLE_H |
|
30 |
|
31 #include <cassert> |
|
32 #include <map> |
|
33 #include <sys/types.h> |
|
34 #include "ns3/ipv4.h" |
|
35 #include "ns3/ipv4-route.h" |
|
36 #include "ns3/timer.h" |
|
37 #include "ns3/net-device.h" |
|
38 |
|
39 namespace ns3 { |
|
40 namespace aodv { |
|
41 |
|
42 /** |
|
43 * \ingroup aodv |
|
44 * \brief Route record states |
|
45 */ |
|
46 enum RouteFlags |
|
47 { |
|
48 VALID = 0, //!< VALID |
|
49 INVALID = 1, //!< INVALID |
|
50 IN_SEARCH = 2, //!< IN_SEARCH |
|
51 }; |
|
52 |
|
53 /** |
|
54 * \ingroup aodv |
|
55 * \brief Routing table entry |
|
56 */ |
|
57 class RoutingTableEntry |
|
58 { |
|
59 public: |
|
60 /// c-to |
|
61 RoutingTableEntry (Ptr<NetDevice> dev = 0,Ipv4Address dst = Ipv4Address(), bool vSeqNo = false, u_int32_t m_seqNo = 0, |
|
62 Ipv4InterfaceAddress iface = Ipv4InterfaceAddress(), u_int16_t hops = 0, |
|
63 Ipv4Address nextHop = Ipv4Address(), Time lifetime = Simulator::Now()); |
|
64 |
|
65 ~RoutingTableEntry (); |
|
66 |
|
67 ///\name Precursors management |
|
68 //\{ |
|
69 /** |
|
70 * Insert precursor in precursor list if it doesn't yet exist in the list |
|
71 * \param id precursor address |
|
72 * \return true on success |
|
73 */ |
|
74 bool InsertPrecursor (Ipv4Address id); |
|
75 /** |
|
76 * Lookup precursor by address |
|
77 * \param id precursor address |
|
78 * \return true on success |
|
79 */ |
|
80 bool LookupPrecursor (Ipv4Address id); |
|
81 /** |
|
82 * \brief Delete precursor |
|
83 * \param id precursor address |
|
84 * \return true on success |
|
85 */ |
|
86 bool DeletePrecursor (Ipv4Address id); |
|
87 /// Delete all precursors |
|
88 void DeleteAllPrecursors (); |
|
89 /** |
|
90 * Check that precursor list empty |
|
91 * \return true if precursor list empty |
|
92 */ |
|
93 bool IsPrecursorListEmpty () const; |
|
94 /** |
|
95 * Inserts precursors in vector prec if they does not yet exist in vector |
|
96 */ |
|
97 void GetPrecursors (std::vector<Ipv4Address> & prec) const; |
|
98 //\} |
|
99 |
|
100 /// Mark entry as "down" (i.e. disable it) |
|
101 void Invalidate (Time badLinkLifetime); |
|
102 ///\name Fields |
|
103 //\{ |
|
104 Ipv4Address GetDestination () const { return m_ipv4Route->GetDestination(); } |
|
105 Ptr<Ipv4Route> GetRoute () const { return m_ipv4Route; } |
|
106 void SetRoute (Ptr<Ipv4Route> r) { m_ipv4Route = r; } |
|
107 void SetNextHop (Ipv4Address nextHop) { m_ipv4Route->SetGateway(nextHop); } |
|
108 Ipv4Address GetNextHop () const { return m_ipv4Route->GetGateway(); } |
|
109 void SetOutputDevice (Ptr<NetDevice> dev) { m_ipv4Route->SetOutputDevice(dev); } |
|
110 Ptr<NetDevice> GetOutputDevice () const { return m_ipv4Route->GetOutputDevice(); } |
|
111 Ipv4InterfaceAddress GetInterface () const { return m_iface;} |
|
112 void SetInterface (Ipv4InterfaceAddress iface) { m_iface = iface; } |
|
113 void SetValidSeqNo (bool s) { m_validSeqNo = s; } |
|
114 bool GetValidSeqNo () const { return m_validSeqNo; } |
|
115 void SetSeqNo (uint32_t sn) { m_seqNo = sn; } |
|
116 uint32_t GetSeqNo () const { return m_seqNo; } |
|
117 void SetHop (uint16_t hop) { m_hops = hop; } |
|
118 uint16_t GetHop () const {return m_hops; } |
|
119 void SetLifeTime (Time lt) { m_lifeTime = lt + Simulator::Now(); } |
|
120 Time GetLifeTime () const { return m_lifeTime - Simulator::Now(); } |
|
121 void SetFlag (RouteFlags flag) { m_flag = flag; } |
|
122 RouteFlags GetFlag () const { return m_flag; } |
|
123 void SetRreqCnt (uint8_t n) { m_reqCount = n; } |
|
124 uint8_t GetRreqCnt () const { return m_reqCount; } |
|
125 void IncrementRreqCnt () { m_reqCount++; } |
|
126 void SetUnidirectional (bool u) { m_blackListState = u; } |
|
127 bool IsUnidirectional () const { return m_blackListState; } |
|
128 void SetBalcklistTimeout (Time t) { m_blackListTimeout = t; } |
|
129 Time GetBlacklistTimeout () const { return m_blackListTimeout; } |
|
130 /// RREP_ACK timer |
|
131 Timer m_ackTimer; |
|
132 //\} |
|
133 |
|
134 /** |
|
135 * \brief Compare destination address |
|
136 * \return true if equal |
|
137 */ |
|
138 bool operator== (Ipv4Address const dst) const |
|
139 { |
|
140 return (m_ipv4Route->GetDestination () == dst); |
|
141 } |
|
142 void Print(std::ostream & os) const; |
|
143 |
|
144 private: |
|
145 /// Valid Destination Sequence Number flag |
|
146 bool m_validSeqNo; |
|
147 /// Destination Sequence Number, if m_validSeqNo = true |
|
148 uint32_t m_seqNo; |
|
149 /// Hop Count (number of hops needed to reach destination) |
|
150 uint16_t m_hops; |
|
151 /** |
|
152 * \brief Expiration or deletion time of the route |
|
153 * Lifetime field in the routing table plays dual role -- |
|
154 * for an active route it is the expiration time, and for an invalid route |
|
155 * it is the deletion time. |
|
156 */ |
|
157 Time m_lifeTime; |
|
158 /** Ip route, include |
|
159 * - destination address |
|
160 * - source address |
|
161 * - next hop address (gateway) |
|
162 * - output device |
|
163 */ |
|
164 Ptr<Ipv4Route> m_ipv4Route; |
|
165 /// Output interface address |
|
166 Ipv4InterfaceAddress m_iface; |
|
167 /// Routing flags: valid, invalid or in search |
|
168 RouteFlags m_flag; |
|
169 |
|
170 /// List of precursors |
|
171 std::vector<Ipv4Address> m_precursorList; |
|
172 /// When I can send another request |
|
173 Time m_routeRequestTimout; |
|
174 /// Number of route requests |
|
175 uint8_t m_reqCount; |
|
176 /// Indicate if this entry is in "blacklist" |
|
177 bool m_blackListState; |
|
178 /// Time for which the node is put into the blacklist |
|
179 Time m_blackListTimeout; |
|
180 }; |
|
181 |
|
182 /** |
|
183 * \ingroup aodv |
|
184 * \brief The Routing table used by AODV protocol |
|
185 */ |
|
186 class RoutingTable |
|
187 { |
|
188 public: |
|
189 /// c-tor |
|
190 RoutingTable (Time t); |
|
191 ///\name Handle life time of invalid route |
|
192 //\{ |
|
193 Time GetBadLinkLifetime () const { return m_badLinkLifetime; } |
|
194 void SetBadLinkLifetime (Time t) { m_badLinkLifetime = t; } |
|
195 //\} |
|
196 /** |
|
197 * Add routing table entry if it doesn't yet exist in routing table |
|
198 * \param r routing table entry |
|
199 * \return true in success |
|
200 */ |
|
201 bool AddRoute (RoutingTableEntry & r); |
|
202 /** |
|
203 * Delete routing table entry with destination address dst, if it exists. |
|
204 * \param dst destination address |
|
205 * \return true on success |
|
206 */ |
|
207 bool DeleteRoute (Ipv4Address dst); |
|
208 /** |
|
209 * Lookup routing table entry with destination address dst |
|
210 * \param dst destination address |
|
211 * \param rt entry with destination address dst, if exists |
|
212 * \return true on success |
|
213 */ |
|
214 bool LookupRoute (Ipv4Address dst, RoutingTableEntry & rt); |
|
215 /// Update routing table |
|
216 bool Update (RoutingTableEntry & rt); |
|
217 /// Set routing table entry flags |
|
218 bool SetEntryState (Ipv4Address dst, RouteFlags state); |
|
219 /// Lookup routing entries with next hop Address dst and not empty list of precursors. |
|
220 void GetListOfDestinationWithNextHop (Ipv4Address nextHop, std::map<Ipv4Address, uint32_t> & unreachable); |
|
221 /** |
|
222 * Update routing entries with this destinations as follows: |
|
223 * 1. The destination sequence number of this routing entry, if it |
|
224 * exists and is valid, is incremented. |
|
225 * 2. The entry is invalidated by marking the route entry as invalid |
|
226 * 3. The Lifetime field is updated to current time plus DELETE_PERIOD. |
|
227 */ |
|
228 void InvalidateRoutesWithDst (std::map<Ipv4Address, uint32_t> const & unreachable); |
|
229 /// Delete all route from interface with address iface |
|
230 void DeleteAllRoutesFromInterface (Ipv4InterfaceAddress iface); |
|
231 /// Delete all entries from routing table |
|
232 void Clear () { m_ipv4AddressEntry.clear (); } |
|
233 /// Delete all outdated entries and invalidate valid entry if Lifetime is expired |
|
234 void Purge (); |
|
235 /** Mark entry as unidirectional (e.g. add this neighbor to "blacklist" for blacklistTimeout period) |
|
236 * \param neighbor - neighbor address link to which assumed to be unidirectional |
|
237 * \param blacklistTimeout - time for which the neighboring node is put into the blacklist |
|
238 * \return true on success |
|
239 */ |
|
240 bool MarkLinkAsUnidirectional(Ipv4Address neighbor, Time blacklistTimeout); |
|
241 /// Print routing table |
|
242 void Print(std::ostream &os); |
|
243 |
|
244 private: |
|
245 std::map<Ipv4Address, RoutingTableEntry> m_ipv4AddressEntry; |
|
246 /// Deletion time for invalid routes |
|
247 Time m_badLinkLifetime; |
|
248 }; |
|
249 |
|
250 }} |
|
251 |
|
252 #endif /* AODV_RTABLE_H */ |