A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
aodv-neighbor.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 IITP RAS
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Based on
7 * NS-2 AODV model developed by the CMU/MONARCH group and optimized and
8 * tuned by Samir Das and Mahesh Marina, University of Cincinnati;
9 *
10 * AODV-UU implementation by Erik Nordström of Uppsala University
11 * https://web.archive.org/web/20100527072022/http://core.it.uu.se/core/index.php/AODV-UU
12 *
13 * Authors: Elena Buchatskaia <borovkovaes@iitp.ru>
14 * Pavel Boyko <boyko@iitp.ru>
15 */
16
17#include "aodv-neighbor.h"
18
19#include "ns3/log.h"
20#include "ns3/wifi-mac-header.h"
21
22#include <algorithm>
23
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("AodvNeighbors");
28
29namespace aodv
30{
32 : m_ntimer(Timer::CANCEL_ON_DESTROY)
33{
34 m_ntimer.SetDelay(delay);
37}
38
39bool
41{
42 Purge();
43 for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
44 {
45 if (i->m_neighborAddress == addr)
46 {
47 return true;
48 }
49 }
50 return false;
51}
52
53Time
55{
56 Purge();
57 for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
58 {
59 if (i->m_neighborAddress == addr)
60 {
61 return (i->m_expireTime - Simulator::Now());
62 }
63 }
64 return Seconds(0);
65}
66
67void
69{
70 for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
71 {
72 if (i->m_neighborAddress == addr)
73 {
74 i->m_expireTime = std::max(expire + Simulator::Now(), i->m_expireTime);
75 if (i->m_hardwareAddress == Mac48Address())
76 {
77 i->m_hardwareAddress = LookupMacAddress(i->m_neighborAddress);
78 }
79 return;
80 }
81 }
82
83 NS_LOG_LOGIC("Open link to " << addr);
84 Neighbor neighbor(addr, LookupMacAddress(addr), expire + Simulator::Now());
85 m_nb.push_back(neighbor);
86 Purge();
87}
88
89/**
90 * \brief CloseNeighbor structure
91 */
93{
94 /**
95 * Check if the entry is expired
96 *
97 * \param nb Neighbors::Neighbor entry
98 * \return true if expired, false otherwise
99 */
100 bool operator()(const Neighbors::Neighbor& nb) const
101 {
102 return ((nb.m_expireTime < Simulator::Now()) || nb.close);
103 }
104};
105
106void
108{
109 if (m_nb.empty())
110 {
111 return;
112 }
113
114 CloseNeighbor pred;
115 if (!m_handleLinkFailure.IsNull())
116 {
117 for (auto j = m_nb.begin(); j != m_nb.end(); ++j)
118 {
119 if (pred(*j))
120 {
121 NS_LOG_LOGIC("Close link to " << j->m_neighborAddress);
122 m_handleLinkFailure(j->m_neighborAddress);
123 }
124 }
125 }
126 m_nb.erase(std::remove_if(m_nb.begin(), m_nb.end(), pred), m_nb.end());
129}
130
131void
137
138void
140{
141 m_arp.push_back(a);
142}
143
144void
146{
147 m_arp.erase(std::remove(m_arp.begin(), m_arp.end(), a), m_arp.end());
148}
149
152{
153 Mac48Address hwaddr;
154 for (auto i = m_arp.begin(); i != m_arp.end(); ++i)
155 {
156 ArpCache::Entry* entry = (*i)->Lookup(addr);
157 if (entry != nullptr && (entry->IsAlive() || entry->IsPermanent()) && !entry->IsExpired())
158 {
159 hwaddr = Mac48Address::ConvertFrom(entry->GetMacAddress());
160 break;
161 }
162 }
163 return hwaddr;
164}
165
166void
168{
169 Mac48Address addr = hdr.GetAddr1();
170
171 for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
172 {
173 if (i->m_hardwareAddress == addr)
174 {
175 i->close = true;
176 }
177 }
178 Purge();
179}
180
181} // namespace aodv
182} // namespace ns3
A record that that holds information about an ArpCache entry.
Definition arp-cache.h:173
bool IsAlive()
Definition arp-cache.cc:386
Address GetMacAddress() const
Definition arp-cache.cc:488
bool IsExpired() const
Definition arp-cache.cc:535
bool IsPermanent()
Definition arp-cache.cc:400
Ipv4 addresses are stored in host order in this class.
an EUI-48 address
static Mac48Address ConvertFrom(const Address &address)
Smart pointer class similar to boost::intrusive_ptr.
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
A simple virtual Timer class.
Definition timer.h:67
void SetDelay(const Time &delay)
Definition timer.cc:65
void SetFunction(FN fn)
Definition timer.h:268
void Cancel()
Cancel the currently-running event if there is one.
Definition timer.cc:97
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition timer.cc:151
Implements the IEEE 802.11 MAC header.
Mac48Address GetAddr1() const
Return the address in the Address 1 field.
Time GetExpireTime(Ipv4Address addr)
Return expire time for neighbor node with address addr, if exists, else return 0.
void ScheduleTimer()
Schedule m_ntimer.
Neighbors(Time delay)
constructor
Callback< void, const WifiMacHeader & > m_txErrorCallback
TX error callback.
void Purge()
Remove all expired entries.
std::vector< Ptr< ArpCache > > m_arp
list of ARP cached to be used for layer 2 notifications processing
Mac48Address LookupMacAddress(Ipv4Address addr)
Find MAC address by IP using list of ARP caches.
void Update(Ipv4Address addr, Time expire)
Update expire time for entry with address addr, if it exists, else add new entry.
std::vector< Neighbor > m_nb
vector of entries
Timer m_ntimer
Timer for neighbor's list. Schedule Purge().
Callback< void, Ipv4Address > m_handleLinkFailure
link failure callback
void ProcessTxError(const WifiMacHeader &hdr)
Process layer 2 TX error notification.
void DelArpCache(Ptr< ArpCache > a)
Don't use given ARP cache any more (interface is down)
bool IsNeighbor(Ipv4Address addr)
Check that node with address addr is neighbor.
void AddArpCache(Ptr< ArpCache > a)
Add ARP cache to be used to allow layer 2 notifications processing.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
CloseNeighbor structure.
bool operator()(const Neighbors::Neighbor &nb) const
Check if the entry is expired.
Neighbor description.
bool close
Neighbor close indicator.
Time m_expireTime
Neighbor expire time.