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