A Discrete-Event Network Simulator
API
aodv-neighbor.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 IITP RAS
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  * Based on
18  * NS-2 AODV model developed by the CMU/MONARCH group and optimized and
19  * tuned by Samir Das and Mahesh Marina, University of Cincinnati;
20  *
21  * AODV-UU implementation by Erik Nordström of Uppsala University
22  * https://web.archive.org/web/20100527072022/http://core.it.uu.se/core/index.php/AODV-UU
23  *
24  * Authors: Elena Buchatskaia <borovkovaes@iitp.ru>
25  * Pavel Boyko <boyko@iitp.ru>
26  */
27 
28 #include "aodv-neighbor.h"
29 
30 #include "ns3/log.h"
31 #include "ns3/wifi-mac-header.h"
32 
33 #include <algorithm>
34 
35 namespace ns3
36 {
37 
38 NS_LOG_COMPONENT_DEFINE("AodvNeighbors");
39 
40 namespace aodv
41 {
43  : m_ntimer(Timer::CANCEL_ON_DESTROY)
44 {
45  m_ntimer.SetDelay(delay);
48 }
49 
50 bool
52 {
53  Purge();
54  for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
55  {
56  if (i->m_neighborAddress == addr)
57  {
58  return true;
59  }
60  }
61  return false;
62 }
63 
64 Time
66 {
67  Purge();
68  for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
69  {
70  if (i->m_neighborAddress == addr)
71  {
72  return (i->m_expireTime - Simulator::Now());
73  }
74  }
75  return Seconds(0);
76 }
77 
78 void
80 {
81  for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
82  {
83  if (i->m_neighborAddress == addr)
84  {
85  i->m_expireTime = std::max(expire + Simulator::Now(), i->m_expireTime);
86  if (i->m_hardwareAddress == Mac48Address())
87  {
88  i->m_hardwareAddress = LookupMacAddress(i->m_neighborAddress);
89  }
90  return;
91  }
92  }
93 
94  NS_LOG_LOGIC("Open link to " << addr);
95  Neighbor neighbor(addr, LookupMacAddress(addr), expire + Simulator::Now());
96  m_nb.push_back(neighbor);
97  Purge();
98 }
99 
104 {
111  bool operator()(const Neighbors::Neighbor& nb) const
112  {
113  return ((nb.m_expireTime < Simulator::Now()) || nb.close);
114  }
115 };
116 
117 void
119 {
120  if (m_nb.empty())
121  {
122  return;
123  }
124 
125  CloseNeighbor pred;
126  if (!m_handleLinkFailure.IsNull())
127  {
128  for (auto j = m_nb.begin(); j != m_nb.end(); ++j)
129  {
130  if (pred(*j))
131  {
132  NS_LOG_LOGIC("Close link to " << j->m_neighborAddress);
133  m_handleLinkFailure(j->m_neighborAddress);
134  }
135  }
136  }
137  m_nb.erase(std::remove_if(m_nb.begin(), m_nb.end(), pred), m_nb.end());
138  m_ntimer.Cancel();
139  m_ntimer.Schedule();
140 }
141 
142 void
144 {
145  m_ntimer.Cancel();
146  m_ntimer.Schedule();
147 }
148 
149 void
151 {
152  m_arp.push_back(a);
153 }
154 
155 void
157 {
158  m_arp.erase(std::remove(m_arp.begin(), m_arp.end(), a), m_arp.end());
159 }
160 
163 {
164  Mac48Address hwaddr;
165  for (auto i = m_arp.begin(); i != m_arp.end(); ++i)
166  {
167  ArpCache::Entry* entry = (*i)->Lookup(addr);
168  if (entry != nullptr && (entry->IsAlive() || entry->IsPermanent()) && !entry->IsExpired())
169  {
170  hwaddr = Mac48Address::ConvertFrom(entry->GetMacAddress());
171  break;
172  }
173  }
174  return hwaddr;
175 }
176 
177 void
179 {
180  Mac48Address addr = hdr.GetAddr1();
181 
182  for (auto i = m_nb.begin(); i != m_nb.end(); ++i)
183  {
184  if (i->m_hardwareAddress == addr)
185  {
186  i->close = true;
187  }
188  }
189  Purge();
190 }
191 
192 } // namespace aodv
193 } // namespace ns3
#define max(a, b)
Definition: 80211b.c:42
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:184
bool IsAlive()
Definition: arp-cache.cc:397
Address GetMacAddress() const
Definition: arp-cache.cc:499
bool IsExpired() const
Definition: arp-cache.cc:547
bool IsPermanent()
Definition: arp-cache.cc:411
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
an EUI-48 address
Definition: mac48-address.h:46
static Mac48Address ConvertFrom(const Address &address)
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A simple virtual Timer class.
Definition: timer.h:74
void SetDelay(const Time &delay)
Definition: timer.cc:76
void SetFunction(FN fn)
Definition: timer.h:275
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:108
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:162
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:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:704
CloseNeighbor structure.
bool operator()(const Neighbors::Neighbor &nb) const
Check if the entry is expired.
Neighbor description.
Definition: aodv-neighbor.h:64
bool close
Neighbor close indicator.
Definition: aodv-neighbor.h:72
Time m_expireTime
Neighbor expire time.
Definition: aodv-neighbor.h:70