A Discrete-Event Network Simulator
API
arp-l3-protocol.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/packet.h"
21 #include "ns3/log.h"
22 #include "ns3/node.h"
23 #include "ns3/net-device.h"
24 #include "ns3/object-vector.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/pointer.h"
27 #include "ns3/string.h"
28 
29 #include "ipv4-l3-protocol.h"
30 #include "arp-l3-protocol.h"
31 #include "arp-header.h"
32 #include "arp-cache.h"
33 #include "ipv4-interface.h"
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("ArpL3Protocol");
38 
39 const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806;
40 
41 NS_OBJECT_ENSURE_REGISTERED (ArpL3Protocol);
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::ArpL3Protocol")
47  .SetParent<Object> ()
48  .AddConstructor<ArpL3Protocol> ()
49  .SetGroupName ("Internet")
50  .AddAttribute ("CacheList",
51  "The list of ARP caches",
54  MakeObjectVectorChecker<ArpCache> ())
55  .AddAttribute ("RequestJitter",
56  "The jitter in ms a node is allowed to wait "
57  "before sending an ARP request. Some jitter aims "
58  "to prevent collisions. By default, the model "
59  "will wait for a duration in ms defined by "
60  "a uniform random-variable between 0 and RequestJitter",
61  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]"),
63  MakePointerChecker<RandomVariableStream> ())
64  .AddTraceSource ("Drop",
65  "Packet dropped because not enough room "
66  "in pending queue for a specific cache entry.",
68  "ns3::Packet::TracedCallback")
69  ;
70  return tid;
71 }
72 
74 {
75  NS_LOG_FUNCTION (this);
76 }
77 
79 {
80  NS_LOG_FUNCTION (this);
81 }
82 
83 int64_t
85 {
86  NS_LOG_FUNCTION (this << stream);
87  m_requestJitter->SetStream (stream);
88  return 1;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION (this << node);
95  m_node = node;
96 }
97 
98 /*
99  * This method is called by AddAgregate and completes the aggregation
100  * by setting the node in the ipv4 stack
101  */
102 void
104 {
105  NS_LOG_FUNCTION (this);
106  if (m_node == 0)
107  {
108  Ptr<Node>node = this->GetObject<Node> ();
109  //verify that it's a valid node and that
110  //the node was not set before
111  if (node != 0)
112  {
113  this->SetNode (node);
114  }
115  }
117 }
118 
119 void
121 {
122  NS_LOG_FUNCTION (this);
123  for (CacheList::iterator i = m_cacheList.begin (); i != m_cacheList.end (); ++i)
124  {
125  Ptr<ArpCache> cache = *i;
126  cache->Dispose ();
127  }
128  m_cacheList.clear ();
129  m_node = 0;
131 }
132 
135 {
136  NS_LOG_FUNCTION (this << device << interface);
138  Ptr<ArpCache> cache = CreateObject<ArpCache> ();
139  cache->SetDevice (device, interface);
140  NS_ASSERT (device->IsBroadcast ());
141  device->AddLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
142  cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
143  m_cacheList.push_back (cache);
144  return cache;
145 }
146 
149 {
150  NS_LOG_FUNCTION (this << device);
151  for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
152  {
153  if ((*i)->GetDevice () == device)
154  {
155  return *i;
156  }
157  }
158  NS_ASSERT (false);
159  // quiet compiler
160  return 0;
161 }
162 
163 void
164 ArpL3Protocol::Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
165  const Address &to, NetDevice::PacketType packetType)
166 {
167  NS_LOG_FUNCTION (this << device << p->GetSize () << protocol << from << to << packetType);
168 
169  Ptr<Packet> packet = p->Copy ();
170 
171  NS_LOG_LOGIC ("ARP: received packet of size "<< packet->GetSize ());
172 
173  Ptr<ArpCache> cache = FindCache (device);
174 
175  //
176  // If we're connected to a real world network, then some of the fields sizes
177  // in an ARP packet can vary in ways not seen in simulations. We need to be
178  // able to detect ARP packets with headers we don't recongnize and not process
179  // them instead of crashing. The ArpHeader will return 0 if it can't deal
180  // with the received header.
181  //
182  ArpHeader arp;
183  uint32_t size = packet->RemoveHeader (arp);
184  if (size == 0)
185  {
186  NS_LOG_LOGIC ("ARP: Cannot remove ARP header");
187  return;
188  }
189  NS_LOG_LOGIC ("ARP: received "<< (arp.IsRequest () ? "request" : "reply") <<
190  " node="<<m_node->GetId ()<<", got request from " <<
191  arp.GetSourceIpv4Address () << " for address " <<
192  arp.GetDestinationIpv4Address () << "; we have addresses: ");
193  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
194  {
195  NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
196  }
197 
203  bool found = false;
204  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
205  {
206  if (arp.IsRequest () && arp.GetDestinationIpv4Address () ==
207  cache->GetInterface ()->GetAddress (i).GetLocal ())
208  {
209  found = true;
210  NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " <<
211  arp.GetSourceIpv4Address () << " -- send reply");
213  arp.GetSourceHardwareAddress ());
214  break;
215  }
216  else if (arp.IsReply () &&
217  arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress (i).GetLocal ()) &&
218  arp.GetDestinationHardwareAddress () == device->GetAddress ())
219  {
220  found = true;
221  Ipv4Address from = arp.GetSourceIpv4Address ();
222  ArpCache::Entry *entry = cache->Lookup (from);
223  if (entry != 0)
224  {
225  if (entry->IsWaitReply ())
226  {
227  NS_LOG_LOGIC ("node="<< m_node->GetId () <<
228  ", got reply from " << arp.GetSourceIpv4Address ()
229  << " for waiting entry -- flush");
230  Address from_mac = arp.GetSourceHardwareAddress ();
231  entry->MarkAlive (from_mac);
232  Ptr<Packet> pending = entry->DequeuePending ();
233  while (pending != 0)
234  {
235  cache->GetInterface ()->Send (pending,
236  arp.GetSourceIpv4Address ());
237  pending = entry->DequeuePending ();
238  }
239  }
240  else
241  {
242  // ignore this reply which might well be an attempt
243  // at poisening my arp cache.
244  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply from " <<
245  arp.GetSourceIpv4Address () <<
246  " for non-waiting entry -- drop");
247  m_dropTrace (packet);
248  }
249  }
250  else
251  {
252  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
253  m_dropTrace (packet);
254  }
255  break;
256  }
257  }
258  if (found == false)
259  {
260  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
261  arp.GetSourceIpv4Address () << " for unknown address " <<
262  arp.GetDestinationIpv4Address () << " -- drop");
263  }
264 }
265 
266 bool
268  Ptr<NetDevice> device,
269  Ptr<ArpCache> cache,
270  Address *hardwareDestination)
271 {
272  NS_LOG_FUNCTION (this << packet << destination << device << cache << hardwareDestination);
273  ArpCache::Entry *entry = cache->Lookup (destination);
274  if (entry != 0)
275  {
276  if (entry->IsExpired ())
277  {
278  if (entry->IsDead ())
279  {
280  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
281  ", dead entry for " << destination << " expired -- send arp request");
282  entry->MarkWaitReply (packet);
284  }
285  else if (entry->IsAlive ())
286  {
287  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
288  ", alive entry for " << destination << " expired -- send arp request");
289  entry->MarkWaitReply (packet);
291  }
292  else if (entry->IsWaitReply ())
293  {
294  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
295  }
296  }
297  else
298  {
299  if (entry->IsDead ())
300  {
301  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
302  ", dead entry for " << destination << " valid -- drop");
303  m_dropTrace (packet);
304  }
305  else if (entry->IsAlive ())
306  {
307  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
308  ", alive entry for " << destination << " valid -- send");
309  *hardwareDestination = entry->GetMacAddress ();
310  return true;
311  }
312  else if (entry->IsWaitReply ())
313  {
314  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
315  ", wait reply for " << destination << " valid -- drop previous");
316  if (!entry->UpdateWaitReply (packet))
317  {
318  m_dropTrace (packet);
319  }
320  }
321  }
322  }
323  else
324  {
325  // This is our first attempt to transmit data to this destination.
326  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
327  ", no entry for " << destination << " -- send arp request");
328  entry = cache->Add (destination);
329  entry->MarkWaitReply (packet);
331  }
332  return false;
333 }
334 
335 void
337 {
338  NS_LOG_FUNCTION (this << cache << to);
339  ArpHeader arp;
340  // need to pick a source address; use routing implementation to select
342  Ptr<NetDevice> device = cache->GetDevice ();
343  NS_ASSERT (device != 0);
344  Ipv4Header header;
345  header.SetDestination (to);
346  Ptr<Packet> packet = Create<Packet> ();
347  Ipv4Address source = ipv4->SelectSourceAddress (device, to, Ipv4InterfaceAddress::GLOBAL);
348  NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
349  " || src: " << device->GetAddress () << " / " << source <<
350  " || dst: " << device->GetBroadcast () << " / " << to);
351  arp.SetRequest (device->GetAddress (), source, device->GetBroadcast (), to);
352  packet->AddHeader (arp);
353  cache->GetDevice ()->Send (packet, device->GetBroadcast (), PROT_NUMBER);
354 }
355 
356 void
358 {
359  NS_LOG_FUNCTION (this << cache << myIp << toIp << toMac);
360  ArpHeader arp;
361  NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
362  "|| src: " << cache->GetDevice ()->GetAddress () <<
363  " / " << myIp <<
364  " || dst: " << toMac << " / " << toIp);
365  arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
366  Ptr<Packet> packet = Create<Packet> ();
367  packet->AddHeader (arp);
368  cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER);
369 }
370 
371 } // namespace ns3
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:113
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition: arp-header.cc:46
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Receive a packet.
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:298
void SendArpReply(Ptr< const ArpCache > cache, Ipv4Address myIp, Ipv4Address toIp, Address toMac)
Send an ARP reply to an host.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:305
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Hold variables of type string.
Definition: string.h:41
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:293
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:272
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:874
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
Ipv4Address GetSourceIpv4Address(void)
Returns the source IP address.
Definition: arp-header.cc:83
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition: arp-header.cc:33
The packet header for an ARP packet.
Definition: arp-header.h:34
Ptr< ArpCache > CreateCache(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Create an ARP cache for the device/interface.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by ARP
bool IsAlive(void)
Definition: arp-cache.cc:332
void SendArpRequest(Ptr< const ArpCache >cache, Ipv4Address to)
Send an ARP request to an host.
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Packet header for IPv4.
Definition: ipv4-header.h:31
virtual void NotifyNewAggregate()
Notify all Objects aggregated to this one of a new Object being aggregated.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:210
virtual void DoDispose(void)
Destructor implementation.
void MarkWaitReply(Ptr< Packet > waiting)
Definition: arp-cache.cc:381
bool IsDead(void)
Definition: arp-cache.cc:326
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
bool IsExpired(void) const
Definition: arp-cache.cc:428
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Address GetMacAddress(void) const
Definition: arp-cache.cc:393
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:318
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
Implement the Ipv4 layer.
static const uint16_t PROT_NUMBER
ARP protocol number (0x0806)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Node > m_node
node the ARP L3 protocol is associated with
bool IsEqual(const Ipv4Address &other) const
Comparison operation between two Ipv4Addresses.
Definition: ipv4-address.h:83
Address GetDestinationHardwareAddress(void)
Returns the destination hardware address.
Definition: arp-header.cc:77
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:354
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:168
void Flush(void)
Clear the ArpCache of all entries.
Definition: arp-cache.cc:241
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
bool UpdateWaitReply(Ptr< Packet > waiting)
Definition: arp-cache.cc:365
Ipv4Address GetDestinationIpv4Address(void)
Returns the destination IP address.
Definition: arp-header.cc:89
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t GetId(void) const
Definition: node.cc:107
bool Lookup(Ptr< Packet > p, Ipv4Address destination, Ptr< NetDevice > device, Ptr< ArpCache > cache, Address *hardwareDestination)
Perform an ARP lookup.
void SetNode(Ptr< Node > node)
Set the node the ARP L3 protocol is associated with.
Address GetSourceHardwareAddress(void)
Returns the source hardware address.
Definition: arp-header.cc:71
Ptr< RandomVariableStream > m_requestJitter
jitter to de-sync ARP requests
bool IsWaitReply(void)
Definition: arp-cache.cc:338
Ptr< Packet > DequeuePending(void)
Definition: arp-cache.cc:441
bool IsReply(void) const
Check if the ARP is a reply.
Definition: arp-header.cc:65
A base class which provides memory management and object aggregation.
Definition: object.h:87
Container for a set of ns3::Object pointers.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
CacheList m_cacheList
ARP cache container.
a unique identifier for an interface.
Definition: type-id.h:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
void Dispose(void)
Dispose of this Object.
Definition: object.cc:208
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Ptr< ArpCache > FindCache(Ptr< NetDevice > device)
Finds the cache associated with a NetDevice.
static TypeId GetTypeId(void)
Get the type ID.
bool IsRequest(void) const
Check if the ARP is a request.
Definition: arp-header.cc:59