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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Hemanth Narra <hemanth@ittc.ku.com>
7 *
8 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
9 * ResiliNets Research Group https://resilinets.org/
10 * Information and Telecommunication Technology Center (ITTC)
11 * and Department of Electrical Engineering and Computer Science
12 * The University of Kansas Lawrence, KS USA.
13 *
14 * Work supported in part by NSF FIND (Future Internet Design) Program
15 * under grant CNS-0626918 (Postmodern Internet Architecture),
16 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
17 * US Department of Defense (DoD), and ITTC at The University of Kansas.
18 */
19#include "dsdv-rtable.h"
20
21#include "ns3/log.h"
22#include "ns3/simulator.h"
23
24#include <iomanip>
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("DsdvRoutingTable");
30
31namespace dsdv
32{
34 Ipv4Address dst,
35 uint32_t seqNo,
37 uint32_t hops,
38 Ipv4Address nextHop,
39 Time lifetime,
40 Time settlingTime,
41 bool areChanged)
42 : m_seqNo(seqNo),
43 m_hops(hops),
44 m_lifeTime(lifetime),
45 m_iface(iface),
46 m_flag(VALID),
47 m_settlingTime(settlingTime),
48 m_entriesChanged(areChanged)
49{
51 m_ipv4Route->SetDestination(dst);
52 m_ipv4Route->SetGateway(nextHop);
53 m_ipv4Route->SetSource(m_iface.GetLocal());
54 m_ipv4Route->SetOutputDevice(dev);
55}
56
60
64
65bool
67{
68 if (m_ipv4AddressEntry.empty())
69 {
70 return false;
71 }
72 auto i = m_ipv4AddressEntry.find(id);
73 if (i == m_ipv4AddressEntry.end())
74 {
75 return false;
76 }
77 rt = i->second;
78 return true;
79}
80
81bool
83{
84 if (m_ipv4AddressEntry.empty())
85 {
86 return false;
87 }
88 auto i = m_ipv4AddressEntry.find(id);
89 if (i == m_ipv4AddressEntry.end())
90 {
91 return false;
92 }
93 if (forRouteInput && id == i->second.GetInterface().GetBroadcast())
94 {
95 return false;
96 }
97 rt = i->second;
98 return true;
99}
100
101bool
103{
104 return m_ipv4AddressEntry.erase(dst) != 0;
105}
106
112
113bool
115{
116 auto result = m_ipv4AddressEntry.insert(std::make_pair(rt.GetDestination(), rt));
117 return result.second;
118}
119
120bool
122{
123 auto i = m_ipv4AddressEntry.find(rt.GetDestination());
124 if (i == m_ipv4AddressEntry.end())
125 {
126 return false;
127 }
128 i->second = rt;
129 return true;
130}
131
132void
134{
135 if (m_ipv4AddressEntry.empty())
136 {
137 return;
138 }
139 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end();)
140 {
141 if (i->second.GetInterface() == iface)
142 {
143 auto tmp = i;
144 ++i;
145 m_ipv4AddressEntry.erase(tmp);
146 }
147 else
148 {
149 ++i;
150 }
151 }
152}
153
154void
155RoutingTable::GetListOfAllRoutes(std::map<Ipv4Address, RoutingTableEntry>& allRoutes)
156{
157 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end(); ++i)
158 {
159 if (i->second.GetDestination() != Ipv4Address("127.0.0.1") && i->second.GetFlag() == VALID)
160 {
161 allRoutes.insert(std::make_pair(i->first, i->second));
162 }
163 }
164}
165
166void
168 std::map<Ipv4Address, RoutingTableEntry>& unreachable)
169{
170 unreachable.clear();
171 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end(); ++i)
172 {
173 if (i->second.GetNextHop() == nextHop)
174 {
175 unreachable.insert(std::make_pair(i->first, i->second));
176 }
177 }
178}
179
180void
182{
183 std::ostream* os = stream->GetStream();
184 // Copy the current ostream state
185 std::ios oldState(nullptr);
186 oldState.copyfmt(*os);
187
188 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
189
190 std::ostringstream dest;
191 std::ostringstream gw;
192 std::ostringstream iface;
193 std::ostringstream ltime;
194 std::ostringstream stime;
195 dest << m_ipv4Route->GetDestination();
196 gw << m_ipv4Route->GetGateway();
197 iface << m_iface.GetLocal();
198 ltime << std::setprecision(3) << (Simulator::Now() - m_lifeTime).As(unit);
199 stime << m_settlingTime.As(unit);
200
201 *os << std::setw(16) << dest.str();
202 *os << std::setw(16) << gw.str();
203 *os << std::setw(16) << iface.str();
204 *os << std::setw(16) << m_hops;
205 *os << std::setw(16) << m_seqNo;
206 *os << std::setw(16) << ltime.str();
207 *os << stime.str() << std::endl;
208 // Restore the previous ostream state
209 (*os).copyfmt(oldState);
210}
211
212void
213RoutingTable::Purge(std::map<Ipv4Address, RoutingTableEntry>& removedAddresses)
214{
215 if (m_ipv4AddressEntry.empty())
216 {
217 return;
218 }
219 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end();)
220 {
221 auto itmp = i;
222 if (i->second.GetLifeTime() > m_holddownTime && (i->second.GetHop() > 0))
223 {
224 for (auto j = m_ipv4AddressEntry.begin(); j != m_ipv4AddressEntry.end();)
225 {
226 if ((j->second.GetNextHop() == i->second.GetDestination()) &&
227 (i->second.GetHop() != j->second.GetHop()))
228 {
229 auto jtmp = j;
230 removedAddresses.insert(std::make_pair(j->first, j->second));
231 ++j;
232 m_ipv4AddressEntry.erase(jtmp);
233 }
234 else
235 {
236 ++j;
237 }
238 }
239 removedAddresses.insert(std::make_pair(i->first, i->second));
240 ++i;
241 m_ipv4AddressEntry.erase(itmp);
242 }
243 /** \todo Need to decide when to invalidate a route */
244 /* else if (i->second.GetLifeTime() > m_holddownTime)
245 {
246 ++i;
247 itmp->second.SetFlag(INVALID);
248 }*/
249 else
250 {
251 ++i;
252 }
253 }
254}
255
256void
258{
259 std::ostream* os = stream->GetStream();
260 // Copy the current ostream state
261 std::ios oldState(nullptr);
262 oldState.copyfmt(*os);
263
264 *os << std::resetiosflags(std::ios::adjustfield) << std::setiosflags(std::ios::left);
265
266 *os << "\nDSDV Routing table\n";
267 *os << std::setw(16) << "Destination";
268 *os << std::setw(16) << "Gateway";
269 *os << std::setw(16) << "Interface";
270 *os << std::setw(16) << "HopCount";
271 *os << std::setw(16) << "SeqNum";
272 *os << std::setw(16) << "LifeTime";
273 *os << "SettlingTime" << std::endl;
274 for (auto i = m_ipv4AddressEntry.begin(); i != m_ipv4AddressEntry.end(); ++i)
275 {
276 i->second.Print(stream, unit);
277 }
278 *os << std::endl;
279 // Restore the previous ostream state
280 (*os).copyfmt(oldState);
281}
282
283bool
285{
286 auto result = m_ipv4Events.insert(std::make_pair(address, id));
287 return result.second;
288}
289
290bool
292{
293 EventId event;
294 auto i = m_ipv4Events.find(address);
295 if (m_ipv4Events.empty())
296 {
297 return false;
298 }
299 if (i == m_ipv4Events.end())
300 {
301 return false;
302 }
303 event = i->second;
304 return event.IsPending();
305}
306
307bool
309{
310 EventId event;
311 auto i = m_ipv4Events.find(address);
312 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
313 {
314 return false;
315 }
316 event = i->second;
317 Simulator::Cancel(event);
318 m_ipv4Events.erase(address);
319 return true;
320}
321
322bool
324{
325 EventId event;
326 auto i = m_ipv4Events.find(address);
327 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
328 {
329 return false;
330 }
331 event = i->second;
332 if (event.IsPending())
333 {
334 return false;
335 }
336 if (event.IsExpired())
337 {
338 event.Cancel();
339 m_ipv4Events.erase(address);
340 return true;
341 }
342 else
343 {
344 m_ipv4Events.erase(address);
345 return true;
346 }
347}
348
351{
352 auto i = m_ipv4Events.find(address);
353 if (m_ipv4Events.empty() || i == m_ipv4Events.end())
354 {
355 return EventId();
356 }
357 else
358 {
359 return i->second;
360 }
361}
362} // namespace dsdv
363} // namespace ns3
An identifier for simulation events.
Definition event-id.h:45
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
bool IsPending() const
This method is syntactic sugar for !IsExpired().
Definition event-id.cc:65
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
Ipv4 addresses are stored in host order in this class.
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.
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:274
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
Unit
The unit to use to interpret a number representing time.
Definition nstime.h:100
Routing table entry.
Definition dsdv-rtable.h:48
uint32_t m_seqNo
Destination Sequence Number.
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table entry.
Time m_lifeTime
Expiration or deletion time of the route Lifetime field in the routing table plays dual role – for an...
Ipv4Address GetDestination() const
Get destination IP address.
Definition dsdv-rtable.h:79
~RoutingTableEntry()
Ipv4InterfaceAddress m_iface
Output interface address.
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
Ptr< Ipv4Route > m_ipv4Route
Ip route, include.
Time m_settlingTime
Time for which the node retains an update with changed metric before broadcasting it.
uint32_t m_hops
Hop Count (number of hops needed to reach destination)
bool LookupRoute(Ipv4Address dst, RoutingTableEntry &rt)
Lookup routing table entry with destination address dst.
Time m_holddownTime
hold down time of an expired route
bool DeleteRoute(Ipv4Address dst)
Delete routing table entry with destination address dst, if it exists.
bool ForceDeleteIpv4Event(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
bool AddRoute(RoutingTableEntry &r)
Add routing table entry if it doesn't yet exist in routing table.
bool DeleteIpv4Event(Ipv4Address address)
Clear up the entry from the map after the event is completed.
bool Update(RoutingTableEntry &rt)
Updating the routing Table with routing table entry rt.
std::map< Ipv4Address, RoutingTableEntry > m_ipv4AddressEntry
an entry in the routing table.
void GetListOfDestinationWithNextHop(Ipv4Address nxtHp, std::map< Ipv4Address, RoutingTableEntry > &dstList)
Lookup list of addresses for which nxtHp is the next Hop address.
void Print(Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S) const
Print routing table.
void DeleteAllRoutesFromInterface(Ipv4InterfaceAddress iface)
Delete all route from interface with address iface.
EventId GetEventId(Ipv4Address address)
Get the EventId associated with that address.
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...
uint32_t RoutingTableSize()
Provides the number of routes present in that nodes routing table.
std::map< Ipv4Address, EventId > m_ipv4Events
an entry in the event table.
bool AnyRunningEvent(Ipv4Address address)
Force delete an update waiting for settling time to complete as a better update to same destination w...
void Purge(std::map< Ipv4Address, RoutingTableEntry > &removedAddresses)
Delete all outdated entries if Lifetime is expired.
void GetListOfAllRoutes(std::map< Ipv4Address, RoutingTableEntry > &allRoutes)
Lookup list of all addresses in the routing table.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.