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 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("DsdvRoutingTable");
39 
40 namespace 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> ();
60  m_ipv4Route->SetGateway (nextHop);
63 }
65 {
66 }
68 {
69 }
70 
71 bool
73  RoutingTableEntry & rt)
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 
88 bool
90  RoutingTableEntry & rt,
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 
110 bool
112 {
113  if (m_ipv4AddressEntry.erase (dst) != 0)
114  {
115  // NS_LOG_DEBUG("Route erased");
116  return true;
117  }
118  return false;
119 }
120 
121 uint32_t
123 {
124  return m_ipv4AddressEntry.size ();
125 }
126 
127 bool
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 
135 bool
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 
147 void
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 
169 void
170 RoutingTable::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 
182 void
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 
197 void
199 {
200  *stream->GetStream () << std::setiosflags (std::ios::fixed) << m_ipv4Route->GetDestination () << "\t\t" << m_ipv4Route->GetGateway () << "\t\t"
201  << m_iface.GetLocal () << "\t\t" << std::setiosflags (std::ios::left)
202  << std::setw (10) << m_hops << "\t" << std::setw (10) << m_seqNo << "\t"
203  << std::setprecision (3) << (Simulator::Now () - m_lifeTime).GetSeconds ()
204  << "s\t\t" << m_settlingTime.GetSeconds () << "s\n";
205 }
206 
207 void
208 RoutingTable::Purge (std::map<Ipv4Address, RoutingTableEntry> & removedAddresses)
209 {
210  if (m_ipv4AddressEntry.empty ())
211  {
212  return;
213  }
214  for (std::map<Ipv4Address, RoutingTableEntry>::iterator i = m_ipv4AddressEntry.begin (); i != m_ipv4AddressEntry.end (); )
215  {
216  std::map<Ipv4Address, RoutingTableEntry>::iterator itmp = i;
217  if (i->second.GetLifeTime () > m_holddownTime && (i->second.GetHop () > 0))
218  {
219  for (std::map<Ipv4Address, RoutingTableEntry>::iterator j = m_ipv4AddressEntry.begin (); j != m_ipv4AddressEntry.end (); )
220  {
221  if ((j->second.GetNextHop () == i->second.GetDestination ()) && (i->second.GetHop () != j->second.GetHop ()))
222  {
223  std::map<Ipv4Address, RoutingTableEntry>::iterator jtmp = j;
224  removedAddresses.insert (std::make_pair (j->first,j->second));
225  ++j;
226  m_ipv4AddressEntry.erase (jtmp);
227  }
228  else
229  {
230  ++j;
231  }
232  }
233  removedAddresses.insert (std::make_pair (i->first,i->second));
234  ++i;
235  m_ipv4AddressEntry.erase (itmp);
236  }
238  /* else if (i->second.GetLifeTime() > m_holddownTime)
239  {
240  ++i;
241  itmp->second.SetFlag(INVALID);
242  }*/
243  else
244  {
245  ++i;
246  }
247  }
248  return;
249 }
250 
251 void
253 {
254  *stream->GetStream () << "\nDSDV Routing table\n" << "Destination\t\tGateway\t\tInterface\t\tHopCount\t\tSeqNum\t\tLifeTime\t\tSettlingTime\n";
255  for (std::map<Ipv4Address, RoutingTableEntry>::const_iterator i = m_ipv4AddressEntry.begin (); i
256  != m_ipv4AddressEntry.end (); ++i)
257  {
258  i->second.Print (stream);
259  }
260  *stream->GetStream () << "\n";
261 }
262 
263 bool
265  EventId id)
266 {
267  std::pair<std::map<Ipv4Address, EventId>::iterator, bool> result = m_ipv4Events.insert (std::make_pair (address,id));
268  return result.second;
269 }
270 
271 bool
273 {
274  EventId event;
275  std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
276  if (m_ipv4Events.empty ())
277  {
278  return false;
279  }
280  if (i == m_ipv4Events.end ())
281  {
282  return false;
283  }
284  event = i->second;
285  if (event.IsRunning ())
286  {
287  return true;
288  }
289  else
290  {
291  return false;
292  }
293 }
294 
295 bool
297 {
298  EventId event;
299  std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
300  if (m_ipv4Events.empty () || i == m_ipv4Events.end ())
301  {
302  return false;
303  }
304  event = i->second;
305  Simulator::Cancel (event);
306  m_ipv4Events.erase (address);
307  return true;
308 }
309 
310 bool
312 {
313  EventId event;
314  std::map<Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
315  if (m_ipv4Events.empty () || i == m_ipv4Events.end ())
316  {
317  return false;
318  }
319  event = i->second;
320  if (event.IsRunning ())
321  {
322  return false;
323  }
324  if (event.IsExpired ())
325  {
326  event.Cancel ();
327  m_ipv4Events.erase (address);
328  return true;
329  }
330  else
331  {
332  m_ipv4Events.erase (address);
333  return true;
334  }
335 }
336 
337 EventId
339 {
340  std::map <Ipv4Address, EventId>::const_iterator i = m_ipv4Events.find (address);
341  if (m_ipv4Events.empty () || i == m_ipv4Events.end ())
342  {
343  return EventId ();
344  }
345  else
346  {
347  return i->second;
348  }
349 }
350 }
351 }
Time m_holddownTime
hold down time of an expired route
Definition: dsdv-rtable.h:469
uint32_t m_hops
Hop Count (number of hops needed to reach destination)
Definition: dsdv-rtable.h:288
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Routing table entry.
Definition: dsdv-rtable.h:56
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
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:272
Time m_settlingTime
Time for which the node retains an update with changed metric before broadcasting it...
Definition: dsdv-rtable.h:309
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
EventId GetEventId(Ipv4Address address)
Get the EcentId associated with that address.
Definition: dsdv-rtable.cc:338
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void GetListOfAllRoutes(std::map< Ipv4Address, RoutingTableEntry > &allRoutes)
Lookup list of all addresses in the routing table.
Definition: dsdv-rtable.cc:170
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:346
bool Update(RoutingTableEntry &rt)
Updating the routing Table with routing table entry rt.
Definition: dsdv-rtable.cc:136
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:59
void SetSource(Ipv4Address src)
Definition: ipv4-route.cc:49
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:296
void SetGateway(Ipv4Address gw)
Definition: ipv4-route.cc:63
uint32_t m_seqNo
Destination Sequence Number.
Definition: dsdv-rtable.h:286
~RoutingTableEntry()
Definition: dsdv-rtable.cc:64
void Purge(std::map< Ipv4Address, RoutingTableEntry > &removedAddresses)
Delete all outdated entries if Lifetime is expired.
Definition: dsdv-rtable.cc:208
Ipv4Address GetDestination(void) const
Definition: ipv4-route.cc:42
void Print(Ptr< OutputStreamWrapper > stream) const
Print routing table entry.
Definition: dsdv-rtable.cc:198
Ipv4InterfaceAddress m_iface
Output interface address.
Definition: dsdv-rtable.h:304
std::map< Ipv4Address, EventId > m_ipv4Events
an entry in the event table.
Definition: dsdv-rtable.h:467
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
void DeleteAllRoutesFromInterface(Ipv4InterfaceAddress iface)
Delete all route from interface with address iface.
Definition: dsdv-rtable.cc:148
bool AddRoute(RoutingTableEntry &r)
Add routing table entry if it doesn&#39;t yet exist in routing table.
Definition: dsdv-rtable.cc:128
uint32_t RoutingTableSize()
Provides the number of routes present in that nodes routing table.
Definition: dsdv-rtable.cc:122
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void SetOutputDevice(Ptr< NetDevice > outputDevice)
Equivalent in Linux to dst_entry.dev.
Definition: ipv4-route.cc:77
Ipv4Address GetGateway(void) const
Definition: ipv4-route.cc:70
std::map< Ipv4Address, RoutingTableEntry > m_ipv4AddressEntry
an entry in the routing table.
Definition: dsdv-rtable.h:465
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
bool DeleteIpv4Event(Ipv4Address address)
Clear up the entry from the map after the event is completed.
Definition: dsdv-rtable.cc:311
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
bool DeleteRoute(Ipv4Address dst)
Delete routing table entry with destination address dst, if it exists.
Definition: dsdv-rtable.cc:111
a class to store IPv4 address information on an interface
An identifier for simulation events.
Definition: event-id.h:53
void Print(Ptr< OutputStreamWrapper > stream) const
Print routing table.
Definition: dsdv-rtable.cc:252
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
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
Ipv4Address GetDestination() const
Get destination IP address.
Definition: dsdv-rtable.h:82
Ipv4Address GetLocal(void) const
Get the local address.
Time m_lifeTime
Expiration or deletion time of the route Lifetime field in the routing table plays dual role – for a...
Definition: dsdv-rtable.h:295
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:264
bool LookupRoute(Ipv4Address dst, RoutingTableEntry &rt)
Lookup routing table entry with destination address dst.
Definition: dsdv-rtable.cc:72
Ptr< Ipv4Route > m_ipv4Route
Ip route, include.
Definition: dsdv-rtable.h:302
std::ostream * GetStream(void)
Return a pointer to an ostream previously set in the wrapper.
void SetDestination(Ipv4Address dest)
Definition: ipv4-route.cc:35