A Discrete-Event Network Simulator
API
arp-cache.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 INRIA
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef ARP_CACHE_H
20 #define ARP_CACHE_H
21 
22 #include "ns3/address.h"
23 #include "ns3/callback.h"
24 #include "ns3/ipv4-address.h"
25 #include "ns3/net-device.h"
26 #include "ns3/nstime.h"
27 #include "ns3/object.h"
28 #include "ns3/output-stream-wrapper.h"
29 #include "ns3/packet.h"
30 #include "ns3/ptr.h"
31 #include "ns3/simulator.h"
32 #include "ns3/traced-callback.h"
33 
34 #include <list>
35 #include <map>
36 #include <stdint.h>
37 
38 namespace ns3
39 {
40 
41 class NetDevice;
42 class Ipv4Interface;
43 class Ipv4Header;
44 
52 class ArpCache : public Object
53 {
54  public:
59  static TypeId GetTypeId();
60  class Entry;
61 
62  ArpCache();
63  ~ArpCache() override;
64 
65  // Delete copy constructor and assignment operator to avoid misuse
66  ArpCache(const ArpCache&) = delete;
67  ArpCache& operator=(const ArpCache&) = delete;
68 
75  void SetDevice(Ptr<NetDevice> device, Ptr<Ipv4Interface> interface);
80  Ptr<NetDevice> GetDevice() const;
86 
91  void SetAliveTimeout(Time aliveTimeout);
96  void SetDeadTimeout(Time deadTimeout);
101  void SetWaitReplyTimeout(Time waitReplyTimeout);
102 
107  Time GetAliveTimeout() const;
112  Time GetDeadTimeout() const;
117  Time GetWaitReplyTimeout() const;
118 
126  void SetArpRequestCallback(Callback<void, Ptr<const ArpCache>, Ipv4Address> arpRequestCallback);
132  void StartWaitReplyTimer();
139  ArpCache::Entry* Lookup(Ipv4Address destination);
146  std::list<ArpCache::Entry*> LookupInverse(Address destination);
157  void Remove(ArpCache::Entry* entry);
161  void Flush();
162 
169 
174 
178  typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
179 
183  class Entry
184  {
185  public:
190  Entry(ArpCache* arp);
191 
195  void MarkDead();
199  void MarkAlive(Address macAddress);
203  void MarkWaitReply(Ipv4PayloadHeaderPair waiting);
209  void MarkPermanent();
215  void MarkAutoGenerated();
224  bool IsDead();
228  bool IsAlive();
232  bool IsWaitReply();
236  bool IsPermanent();
240  bool IsAutoGenerated();
244  Address GetMacAddress() const;
248  Ipv4Address GetIpv4Address() const;
252  void SetMacAddress(Address macAddress);
256  void SetIpv4Address(Ipv4Address destination);
263  bool IsExpired() const;
272  void ClearPendingPacket();
277  uint32_t GetRetries() const;
281  void IncrementRetries();
285  void ClearRetries();
286 
290  void UpdateSeen();
291 
292  private:
297  {
303  };
304 
309  Time GetTimeout() const;
310 
316  std::list<Ipv4PayloadHeaderPair> m_pending;
317  uint32_t m_retries;
318  };
319 
320  private:
324  typedef std::map<Ipv4Address, ArpCache::Entry*> Cache;
328  typedef std::map<Ipv4Address, ArpCache::Entry*>::iterator CacheI;
329 
330  void DoDispose() override;
331 
340  uint32_t m_maxRetries;
341 
347  void HandleWaitReplyTimeout();
352 };
353 
354 } // namespace ns3
355 
356 #endif /* ARP_CACHE_H */
a polymophic address class
Definition: address.h:101
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:184
bool IsDead()
Definition: arp-cache.cc:390
void SetIpv4Address(Ipv4Address destination)
Definition: arp-cache.cc:520
void MarkAutoGenerated()
Changes the state of this entry to auto-generated.
Definition: arp-cache.cc:457
bool IsAlive()
Definition: arp-cache.cc:397
std::list< Ipv4PayloadHeaderPair > m_pending
list of pending packets for the entry's IP
Definition: arp-cache.h:316
ArpCache * m_arp
pointer to the ARP cache owning the entry
Definition: arp-cache.h:311
Ipv4Address m_ipv4Address
entry's IP address
Definition: arp-cache.h:315
Time m_lastSeen
last moment a packet from that address has been seen
Definition: arp-cache.h:313
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:468
Address m_macAddress
entry's MAC address
Definition: arp-cache.h:314
Address GetMacAddress() const
Definition: arp-cache.cc:499
void ClearPendingPacket()
Clear the pending packet list.
Definition: arp-cache.cc:574
void MarkPermanent()
Changes the state of this entry to Permanent.
Definition: arp-cache.cc:446
bool IsExpired() const
Definition: arp-cache.cc:547
void ClearRetries()
Zero the counter of number of retries for an entry.
Definition: arp-cache.cc:603
ArpCacheEntryState_e
ARP cache entry states.
Definition: arp-cache.h:297
@ WAIT_REPLY
Definition: arp-cache.h:299
@ ALIVE
Definition: arp-cache.h:298
@ STATIC_AUTOGENERATED
Definition: arp-cache.h:302
@ PERMANENT
Definition: arp-cache.h:301
@ DEAD
Definition: arp-cache.h:300
uint32_t GetRetries() const
Definition: arp-cache.cc:588
void UpdateSeen()
Update the entry when seeing a packet.
Definition: arp-cache.cc:581
uint32_t m_retries
rerty counter
Definition: arp-cache.h:317
bool IsWaitReply()
Definition: arp-cache.cc:404
void IncrementRetries()
Increment the counter of number of retries for an entry.
Definition: arp-cache.cc:595
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:435
bool IsAutoGenerated()
Definition: arp-cache.cc:418
void MarkDead()
Changes the state of this entry to dead.
Definition: arp-cache.cc:425
void SetMacAddress(Address macAddress)
Definition: arp-cache.cc:506
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:485
Ipv4PayloadHeaderPair DequeuePending()
Definition: arp-cache.cc:557
ArpCacheEntryState_e m_state
state of the entry
Definition: arp-cache.h:312
Ipv4Address GetIpv4Address() const
Definition: arp-cache.cc:513
bool IsPermanent()
Definition: arp-cache.cc:411
Time GetTimeout() const
Returns the entry timeout.
Definition: arp-cache.cc:527
Entry(ArpCache *arp)
Constructor.
Definition: arp-cache.cc:381
An ARP cache.
Definition: arp-cache.h:53
Time GetWaitReplyTimeout() const
Get the time the entry will be in WAIT_REPLY state.
Definition: arp-cache.cc:170
void Remove(ArpCache::Entry *entry)
Remove an entry.
Definition: arp-cache.cc:364
void SetWaitReplyTimeout(Time waitReplyTimeout)
Set the time the entry will be in WAIT_REPLY state.
Definition: arp-cache.cc:149
void HandleWaitReplyTimeout()
This function is an event handler for the event that the ArpCache wants to check whether it must retr...
Definition: arp-cache.cc:197
Time GetAliveTimeout() const
Get the time the entry will be in ALIVE state (unless refreshed)
Definition: arp-cache.cc:156
std::map< Ipv4Address, ArpCache::Entry * > Cache
ARP Cache container.
Definition: arp-cache.h:324
uint32_t m_maxRetries
max retries for a resolution
Definition: arp-cache.h:340
EventId m_waitReplyTimer
cache alive state timer
Definition: arp-cache.h:337
Time m_aliveTimeout
cache alive state timeout
Definition: arp-cache.h:334
void DoDispose() override
Destructor implementation.
Definition: arp-cache.cc:99
void SetArpRequestCallback(Callback< void, Ptr< const ArpCache >, Ipv4Address > arpRequestCallback)
This callback is set when the ArpCache is set up and allows the cache to generate an Arp request when...
Definition: arp-cache.cc:177
Time m_deadTimeout
cache dead state timeout
Definition: arp-cache.h:335
Time m_waitReplyTimeout
cache reply state timeout
Definition: arp-cache.h:336
Ptr< Ipv4Interface > m_interface
Ipv4Interface associated with the cache.
Definition: arp-cache.h:333
void PrintArpCache(Ptr< OutputStreamWrapper > stream)
Print the ARP cache entries.
Definition: arp-cache.cc:262
void Flush()
Clear the ArpCache of all entries.
Definition: arp-cache.cc:245
void SetAliveTimeout(Time aliveTimeout)
Set the time the entry will be in ALIVE state (unless refreshed)
Definition: arp-cache.cc:135
uint32_t m_pendingQueueSize
number of packets waiting for a resolution
Definition: arp-cache.h:348
ArpCache & operator=(const ArpCache &)=delete
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by the ARP cache queue
Definition: arp-cache.h:351
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:352
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:113
Ptr< Ipv4Interface > GetInterface() const
Returns the Ipv4Interface that this ARP cache is associated with.
Definition: arp-cache.cc:128
std::list< ArpCache::Entry * > LookupInverse(Address destination)
Do lookup in the ARP cache against a MAC address.
Definition: arp-cache.cc:323
Callback< void, Ptr< const ArpCache >, Ipv4Address > m_arpRequestCallback
reply timeout callback
Definition: arp-cache.h:339
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:340
Cache m_arpCache
the ARP cache
Definition: arp-cache.h:349
void RemoveAutoGeneratedEntries()
Clear the ArpCache of all Auto-Generated entries.
Definition: arp-cache.cc:306
~ArpCache() override
Definition: arp-cache.cc:93
Ptr< NetDevice > m_device
NetDevice associated with the cache.
Definition: arp-cache.h:332
void StartWaitReplyTimer()
This method will schedule a timeout at WaitReplyTimeout interval in the future, unless a timer is alr...
Definition: arp-cache.cc:184
Time GetDeadTimeout() const
Get the time the entry will be in DEAD state before being removed.
Definition: arp-cache.cc:163
static TypeId GetTypeId()
Get the type ID.
Definition: arp-cache.cc:41
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: arp-cache.h:178
void SetDeadTimeout(Time deadTimeout)
Set the time the entry will be in DEAD state before being removed.
Definition: arp-cache.cc:142
Ptr< NetDevice > GetDevice() const
Returns the NetDevice that this ARP cache is associated with.
Definition: arp-cache.cc:121
ArpCache(const ArpCache &)=delete
std::map< Ipv4Address, ArpCache::Entry * >::iterator CacheI
ARP Cache container iterator.
Definition: arp-cache.h:328
Callback template class.
Definition: callback.h:438
An identifier for simulation events.
Definition: event-id.h:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.