A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsdv-rtable.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Hemanth Narra
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Hemanth Narra <hemanth@ittc.ku.com>
18 *
19 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20 * ResiliNets Research Group https://resilinets.org/
21 * Information and Telecommunication Technology Center (ITTC)
22 * and Department of Electrical Engineering and Computer Science
23 * The University of Kansas Lawrence, KS USA.
24 *
25 * Work supported in part by NSF FIND (Future Internet Design) Program
26 * under grant CNS-0626918 (Postmodern Internet Architecture),
27 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28 * US Department of Defense (DoD), and ITTC at The University of Kansas.
29 */
30#include "dsdv-rtable.h"
31
32#include "ns3/log.h"
33#include "ns3/simulator.h"
34
35#include <iomanip>
36
37namespace ns3
38{
39
40NS_LOG_COMPONENT_DEFINE("DsdvRoutingTable");
41
42namespace dsdv
43{
45 Ipv4Address dst,
46 uint32_t seqNo,
48 uint32_t hops,
49 Ipv4Address nextHop,
50 Time lifetime,
51 Time settlingTime,
52 bool areChanged)
53 : m_seqNo(seqNo),
54 m_hops(hops),
55 m_lifeTime(lifetime),
56 m_iface(iface),
57 m_flag(VALID),
58 m_settlingTime(settlingTime),
59 m_entriesChanged(areChanged)
60{
61 m_ipv4Route = Create<Ipv4Route>();
62 m_ipv4Route->SetDestination(dst);
63 m_ipv4Route->SetGateway(nextHop);
64 m_ipv4Route->SetSource(m_iface.GetLocal());
65 m_ipv4Route->SetOutputDevice(dev);
66}
67
69{
70}
71
73{
74}
75
76bool
78{
79 if (m_ipv4AddressEntry.empty())
80 {
81 return false;
82 }
83 std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.find(id);
84 if (i == m_ipv4AddressEntry.end())
85 {
86 return false;
87 }
88 rt = i->second;
89 return true;
90}
91
92bool
94{
95 if (m_ipv4AddressEntry.empty())
96 {
97 return false;
98 }
99 std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.find(id);
100 if (i == m_ipv4AddressEntry.end())
101 {
102 return false;
103 }
104 if (forRouteInput && id == i->second.GetInterface().GetBroadcast())
105 {
106 return false;
107 }
108 rt = i->second;
109 return true;
110}
111
112bool
114{
115 return m_ipv4AddressEntry.erase(dst) != 0;
116}
117
120{
121 return m_ipv4AddressEntry.size();
122}
123
124bool
126{
127 std::pair<std::map<Ipv4Address, RoutingTableEntry>::iterator, bool> result =
128 m_ipv4AddressEntry.insert(std::make_pair(rt.GetDestination(), rt));
129 return result.second;
130}
131
132bool
134{
135 std::map<Ipv4Address, RoutingTableEntry>::iterator i =
137 if (i == m_ipv4AddressEntry.end())
138 {
139 return false;
140 }
141 i->second = rt;
142 return true;
143}
144
145void
147{
148 if (m_ipv4AddressEntry.empty())
149 {
150 return;
151 }
152 for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin();
153 i != m_ipv4AddressEntry.end();)
154 {
155 if (i->second.GetInterface() == iface)
156 {
157 std::map<Ipv4Address, RoutingTableEntry>::iterator tmp = i;
158 ++i;
159 m_ipv4AddressEntry.erase(tmp);
160 }
161 else
162 {
163 ++i;
164 }
165 }
166}
167
168void
169RoutingTable::GetListOfAllRoutes(std::map<Ipv4Address, RoutingTableEntry>& allRoutes)
170{
171 for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin();
172 i != m_ipv4AddressEntry.end();
173 ++i)
174 {
175 if (i->second.GetDestination() != Ipv4Address("127.0.0.1") && i->second.GetFlag() == VALID)
176 {
177 allRoutes.insert(std::make_pair(i->first, i->second));
178 }
179 }
180}
181
182void
184 std::map<Ipv4Address, RoutingTableEntry>& unreachable)
185{
186 unreachable.clear();
187 for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.begin();
188 i != m_ipv4AddressEntry.end();
189 ++i)
190 {
191 if (i->second.GetNextHop() == nextHop)
192 {
193 unreachable.insert(std::make_pair(i->first, i->second));
194 }
195 }
196}
197
198void
200{
201 std::ostream* os = stream->GetStream();
202 // Copy the current ostream state
203 std::ios oldState(nullptr);
204 oldState.copyfmt(*os);
205
206 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
207
208 std::ostringstream dest;
209 std::ostringstream gw;
210 std::ostringstream iface;
211 std::ostringstream ltime;
212 std::ostringstream stime;
213 dest << m_ipv4Route->GetDestination();
214 gw << m_ipv4Route->GetGateway();
215 iface << m_iface.GetLocal();
216 ltime << std::setprecision(3) << (Simulator::Now() - m_lifeTime).As(unit);
217 stime << m_settlingTime.As(unit);
218
219 *os << std::setw(16) << dest.str();
220 *os << std::setw(16) << gw.str();
221 *os << std::setw(16) << iface.str();
222 *os << std::setw(16) << m_hops;
223 *os << std::setw(16) << m_seqNo;
224 *os << std::setw(16) << ltime.str();
225 *os << stime.str() << std::endl;
226 // Restore the previous ostream state
227 (*os).copyfmt(oldState);
228}
229
230void
231RoutingTable::Purge(std::map<Ipv4Address, RoutingTableEntry>& removedAddresses)
232{
233 if (m_ipv4AddressEntry.empty())
234 {
235 return;
236 }
237 for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin();
238 i != m_ipv4AddressEntry.end();)
239 {
240 std::map<Ipv4Address, RoutingTableEntry>::iterator itmp = i;
241 if (i->second.GetLifeTime() > m_holddownTime && (i->second.GetHop() > 0))
242 {
243 for (std::map<Ipv4Address, RoutingTableEntry>::iterator j = m_ipv4AddressEntry.begin();
244 j != m_ipv4AddressEntry.end();)
245 {
246 if ((j->second.GetNextHop() == i->second.GetDestination()) &&
247 (i->second.GetHop() != j->second.GetHop()))
248 {
249 std::map<Ipv4Address, RoutingTableEntry>::iterator jtmp = j;
250 removedAddresses.insert(std::make_pair(j->first, j->second));
251 ++j;
252 m_ipv4AddressEntry.erase(jtmp);
253 }
254 else
255 {
256 ++j;
257 }
258 }
259 removedAddresses.insert(std::make_pair(i->first, i->second));
260 ++i;
261 m_ipv4AddressEntry.erase(itmp);
262 }
264 /* else if (i->second.GetLifeTime() > m_holddownTime)
265 {
266 ++i;
267 itmp->second.SetFlag(INVALID);
268 }*/
269 else
270 {
271 ++i;
272 }
273 }
274}
275
276void
278{
279 std::ostream* os = stream->GetStream();
280 // Copy the current ostream state
281 std::ios oldState(nullptr);
282 oldState.copyfmt(*os);
283
284 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
285
286 *os << "\nDSDV Routing table\n";
287 *os << std::setw(16) << "Destination";
288 *os << std::setw(16) << "Gateway";
289 *os << std::setw(16) << "Interface";
290 *os << std::setw(16) << "HopCount";
291 *os << std::setw(16) << "SeqNum";
292 *os << std::setw(16) << "LifeTime";
293 *os << "SettlingTime" << std::endl;
294 for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.begin();
295 i != m_ipv4AddressEntry.end();
296 ++i)
297 {
298 i->second.Print(stream, unit);
299 }
300 *os << std::endl;
301 // Restore the previous ostream state
302 (*os).copyfmt(oldState);
303}
304
305bool
307{
308 std::pair<std::map<Ipv4Address, EventId>::iterator, bool> result =
309 m_ipv4Events.insert(std::make_pair(address, id));
310 return result.second;
311}
312
313bool
315{
316 EventId event;
317 std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find(address);
318 if (m_ipv4Events.empty())
319 {
320 return false;
321 }
322 if (i == m_ipv4Events.end())
323 {
324 return false;
325 }
326 event = i->second;
327 return event.IsRunning();
328}
329
330bool
332{
333 EventId event;
334 std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find(address);
335 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
336 {
337 return false;
338 }
339 event = i->second;
340 Simulator::Cancel(event);
341 m_ipv4Events.erase(address);
342 return true;
343}
344
345bool
347{
348 EventId event;
349 std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find(address);
350 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
351 {
352 return false;
353 }
354 event = i->second;
355 if (event.IsRunning())
356 {
357 return false;
358 }
359 if (event.IsExpired())
360 {
361 event.Cancel();
362 m_ipv4Events.erase(address);
363 return true;
364 }
365 else
366 {
367 m_ipv4Events.erase(address);
368 return true;
369 }
370}
371
374{
375 std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find(address);
376 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
377 {
378 return EventId();
379 }
380 else
381 {
382 return i->second;
383 }
384}
385} // namespace dsdv
386} // namespace ns3
An identifier for simulation events.
Definition: event-id.h:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a class to store IPv4 address information on an interface
Ipv4Address GetLocal() const
Get the local address.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:276
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:417
Unit
The unit to use to interpret a number representing time.
Definition: nstime.h:111
Routing table entry.
Definition: dsdv-rtable.h:59
uint32_t m_seqNo
Destination Sequence Number.
Definition: dsdv-rtable.h:295
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table entry.
Definition: dsdv-rtable.cc:199
Time m_lifeTime
Expiration or deletion time of the route Lifetime field in the routing table plays dual role – for an...
Definition: dsdv-rtable.h:304
Ipv4Address GetDestination() const
Get destination IP address.
Definition: dsdv-rtable.h:90
~RoutingTableEntry()
Definition: dsdv-rtable.cc:68
Ipv4InterfaceAddress m_iface
Output interface address.
Definition: dsdv-rtable.h:313
RoutingTableEntry(Ptr< NetDevice > dev=nullptr, Ipv4Address dst=Ipv4Address(), uint32_t seqNo=0, Ipv4InterfaceAddress iface=Ipv4InterfaceAddress(), uint32_t hops=0, Ipv4Address nextHop=Ipv4Address(), Time lifetime=Simulator::Now(), Time settlingTime=Simulator::Now(), bool changedEntries=false)
c-tor
Definition: dsdv-rtable.cc:44
Ptr< Ipv4Route > m_ipv4Route
Ip route, include.
Definition: dsdv-rtable.h:311
Time m_settlingTime
Time for which the node retains an update with changed metric before broadcasting it.
Definition: dsdv-rtable.h:318
uint32_t m_hops
Hop Count (number of hops needed to reach destination)
Definition: dsdv-rtable.h:297
bool LookupRoute(Ipv4Address dst, RoutingTableEntry &rt)
Lookup routing table entry with destination address dst.
Definition: dsdv-rtable.cc:77
Time m_holddownTime
hold down time of an expired route
Definition: dsdv-rtable.h:466
bool DeleteRoute(Ipv4Address dst)
Delete routing table entry with destination address dst, if it exists.
Definition: dsdv-rtable.cc:113
bool ForceDeleteIpv4Event(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
Definition: dsdv-rtable.cc:331
bool AddRoute(RoutingTableEntry &r)
Add routing table entry if it doesn't yet exist in routing table.
Definition: dsdv-rtable.cc:125
bool DeleteIpv4Event(Ipv4Address address)
Clear up the entry from the map after the event is completed.
Definition: dsdv-rtable.cc:346
bool Update(RoutingTableEntry &rt)
Updating the routing Table with routing table entry rt.
Definition: dsdv-rtable.cc:133
std::map< Ipv4Address, RoutingTableEntry > m_ipv4AddressEntry
an entry in the routing table.
Definition: dsdv-rtable.h:462
void GetListOfDestinationWithNextHop(Ipv4Address nxtHp, std::map< Ipv4Address, RoutingTableEntry > &dstList)
Lookup list of addresses for which nxtHp is the next Hop address.
Definition: dsdv-rtable.cc:183
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table.
Definition: dsdv-rtable.cc:277
void DeleteAllRoutesFromInterface(Ipv4InterfaceAddress iface)
Delete all route from interface with address iface.
Definition: dsdv-rtable.cc:146
EventId GetEventId(Ipv4Address address)
Get the EventId associated with that address.
Definition: dsdv-rtable.cc:373
bool AddIpv4Event(Ipv4Address address, EventId id)
Add an event for a destination address so that the update to for that destination is sent after the e...
Definition: dsdv-rtable.cc:306
uint32_t RoutingTableSize()
Provides the number of routes present in that nodes routing table.
Definition: dsdv-rtable.cc:119
std::map< Ipv4Address, EventId > m_ipv4Events
an entry in the event table.
Definition: dsdv-rtable.h:464
bool AnyRunningEvent(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
Definition: dsdv-rtable.cc:314
void Purge(std::map< Ipv4Address, RoutingTableEntry > &removedAddresses)
Delete all outdated entries if Lifetime is expired.
Definition: dsdv-rtable.cc:231
void GetListOfAllRoutes(std::map< Ipv4Address, RoutingTableEntry > &allRoutes)
Lookup list of all addresses in the routing table.
Definition: dsdv-rtable.cc:169
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Every class exported by the ns3 library is enclosed in the ns3 namespace.