A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("ArpL3Protocol");
36 
37 namespace ns3 {
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  .AddAttribute ("CacheList",
50  "The list of ARP caches",
53  MakeObjectVectorChecker<ArpCache> ())
54  .AddAttribute ("RequestJitter", "The jitter in ms a node is allowed to wait before sending an ARP request. Some jitter aims to prevent collisions. By default, the model will wait for a duration in ms defined by a uniform random-variable between 0 and RequestJitter",
55  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]"),
56  MakePointerAccessor (&ArpL3Protocol::m_requestJitter),
57  MakePointerChecker<RandomVariableStream> ())
58  .AddTraceSource ("Drop",
59  "Packet dropped because not enough room in pending queue for a specific cache entry.",
61  ;
62  return tid;
63 }
64 
66 {
67  NS_LOG_FUNCTION (this);
68 }
69 
71 {
72  NS_LOG_FUNCTION (this);
73 }
74 
75 int64_t
77 {
78  NS_LOG_FUNCTION (this << stream);
79  m_requestJitter->SetStream (stream);
80  return 1;
81 }
82 
83 void
85 {
86  NS_LOG_FUNCTION (this << node);
87  m_node = node;
88 }
89 
90 /*
91  * This method is called by AddAgregate and completes the aggregation
92  * by setting the node in the ipv4 stack
93  */
94 void
96 {
97  NS_LOG_FUNCTION (this);
98  if (m_node == 0)
99  {
100  Ptr<Node>node = this->GetObject<Node> ();
101  //verify that it's a valid node and that
102  //the node was not set before
103  if (node != 0)
104  {
105  this->SetNode (node);
106  }
107  }
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION (this);
115  for (CacheList::iterator i = m_cacheList.begin (); i != m_cacheList.end (); ++i)
116  {
117  Ptr<ArpCache> cache = *i;
118  cache->Dispose ();
119  }
120  m_cacheList.clear ();
121  m_node = 0;
123 }
124 
127 {
128  NS_LOG_FUNCTION (this << device << interface);
130  Ptr<ArpCache> cache = CreateObject<ArpCache> ();
131  cache->SetDevice (device, interface);
132  NS_ASSERT (device->IsBroadcast ());
133  device->AddLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
134  cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
135  m_cacheList.push_back (cache);
136  return cache;
137 }
138 
141 {
142  NS_LOG_FUNCTION (this << device);
143  for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
144  {
145  if ((*i)->GetDevice () == device)
146  {
147  return *i;
148  }
149  }
150  NS_ASSERT (false);
151  // quiet compiler
152  return 0;
153 }
154 
155 void
156 ArpL3Protocol::Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
157  const Address &to, NetDevice::PacketType packetType)
158 {
159  NS_LOG_FUNCTION (this << device << p->GetSize () << protocol << from << to << packetType);
160 
161  Ptr<Packet> packet = p->Copy ();
162 
163  NS_LOG_LOGIC ("ARP: received packet of size "<< packet->GetSize ());
164 
165  Ptr<ArpCache> cache = FindCache (device);
166 
167  //
168  // If we're connected to a real world network, then some of the fields sizes
169  // in an ARP packet can vary in ways not seen in simulations. We need to be
170  // able to detect ARP packets with headers we don't recongnize and not process
171  // them instead of crashing. The ArpHeader will return 0 if it can't deal
172  // with the received header.
173  //
174  ArpHeader arp;
175  uint32_t size = packet->RemoveHeader (arp);
176  if (size == 0)
177  {
178  NS_LOG_LOGIC ("ARP: Cannot remove ARP header");
179  return;
180  }
181  NS_LOG_LOGIC ("ARP: received "<< (arp.IsRequest () ? "request" : "reply") <<
182  " node="<<m_node->GetId ()<<", got request from " <<
183  arp.GetSourceIpv4Address () << " for address " <<
184  arp.GetDestinationIpv4Address () << "; we have addresses: ");
185  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
186  {
187  NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
188  }
189 
194  bool found = false;
195  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
196  {
197  if (arp.IsRequest () && arp.GetDestinationIpv4Address () ==
198  cache->GetInterface ()->GetAddress (i).GetLocal ())
199  {
200  found = true;
201  NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " <<
202  arp.GetSourceIpv4Address () << " -- send reply");
204  arp.GetSourceHardwareAddress ());
205  break;
206  }
207  else if (arp.IsReply () &&
208  arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress (i).GetLocal ()) &&
209  arp.GetDestinationHardwareAddress () == device->GetAddress ())
210  {
211  found = true;
212  Ipv4Address from = arp.GetSourceIpv4Address ();
213  ArpCache::Entry *entry = cache->Lookup (from);
214  if (entry != 0)
215  {
216  if (entry->IsWaitReply ())
217  {
218  NS_LOG_LOGIC ("node="<< m_node->GetId () <<
219  ", got reply from " << arp.GetSourceIpv4Address ()
220  << " for waiting entry -- flush");
221  Address from_mac = arp.GetSourceHardwareAddress ();
222  entry->MarkAlive (from_mac);
223  Ptr<Packet> pending = entry->DequeuePending ();
224  while (pending != 0)
225  {
226  cache->GetInterface ()->Send (pending,
227  arp.GetSourceIpv4Address ());
228  pending = entry->DequeuePending ();
229  }
230  }
231  else
232  {
233  // ignore this reply which might well be an attempt
234  // at poisening my arp cache.
235  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply from " <<
236  arp.GetSourceIpv4Address () <<
237  " for non-waiting entry -- drop");
238  m_dropTrace (packet);
239  }
240  }
241  else
242  {
243  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
244  m_dropTrace (packet);
245  }
246  break;
247  }
248  }
249  if (found == false)
250  {
251  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
252  arp.GetSourceIpv4Address () << " for unknown address " <<
253  arp.GetDestinationIpv4Address () << " -- drop");
254  }
255 }
256 
257 bool
259  Ptr<NetDevice> device,
260  Ptr<ArpCache> cache,
261  Address *hardwareDestination)
262 {
263  NS_LOG_FUNCTION (this << packet << destination << device << cache << hardwareDestination);
264  ArpCache::Entry *entry = cache->Lookup (destination);
265  if (entry != 0)
266  {
267  if (entry->IsExpired ())
268  {
269  if (entry->IsDead ())
270  {
271  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
272  ", dead entry for " << destination << " expired -- send arp request");
273  entry->MarkWaitReply (packet);
274  Simulator::Schedule (Time (MilliSeconds (m_requestJitter->GetValue ())), &ArpL3Protocol::SendArpRequest, this, cache, destination);
275  }
276  else if (entry->IsAlive ())
277  {
278  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
279  ", alive entry for " << destination << " expired -- send arp request");
280  entry->MarkWaitReply (packet);
281  Simulator::Schedule (Time (MilliSeconds (m_requestJitter->GetValue ())), &ArpL3Protocol::SendArpRequest, this, cache, destination);
282  }
283  else if (entry->IsWaitReply ())
284  {
285  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
286  }
287  }
288  else
289  {
290  if (entry->IsDead ())
291  {
292  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
293  ", dead entry for " << destination << " valid -- drop");
294  m_dropTrace (packet);
295  }
296  else if (entry->IsAlive ())
297  {
298  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
299  ", alive entry for " << destination << " valid -- send");
300  *hardwareDestination = entry->GetMacAddress ();
301  return true;
302  }
303  else if (entry->IsWaitReply ())
304  {
305  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
306  ", wait reply for " << destination << " valid -- drop previous");
307  if (!entry->UpdateWaitReply (packet))
308  {
309  m_dropTrace (packet);
310  }
311  }
312  }
313  }
314  else
315  {
316  // This is our first attempt to transmit data to this destination.
317  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
318  ", no entry for " << destination << " -- send arp request");
319  entry = cache->Add (destination);
320  entry->MarkWaitReply (packet);
321  Simulator::Schedule (Time (MilliSeconds (m_requestJitter->GetValue ())), &ArpL3Protocol::SendArpRequest, this, cache, destination);
322  }
323  return false;
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this << cache << to);
330  ArpHeader arp;
331  // need to pick a source address; use routing implementation to select
333  Ptr<NetDevice> device = cache->GetDevice ();
334  NS_ASSERT (device != 0);
335  Ipv4Header header;
336  header.SetDestination (to);
337  Ptr<Packet> packet = Create<Packet> ();
338  Ipv4Address source = ipv4->SelectSourceAddress (device, to, Ipv4InterfaceAddress::GLOBAL);
339  NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
340  " || src: " << device->GetAddress () << " / " << source <<
341  " || dst: " << device->GetBroadcast () << " / " << to);
342  arp.SetRequest (device->GetAddress (), source, device->GetBroadcast (), to);
343  packet->AddHeader (arp);
344  cache->GetDevice ()->Send (packet, device->GetBroadcast (), PROT_NUMBER);
345 }
346 
347 void
349 {
350  NS_LOG_FUNCTION (this << cache << myIp << toIp << toMac);
351  ArpHeader arp;
352  NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
353  "|| src: " << cache->GetDevice ()->GetAddress () <<
354  " / " << myIp <<
355  " || dst: " << toMac << " / " << toIp);
356  arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
357  Ptr<Packet> packet = Create<Packet> ();
358  packet->AddHeader (arp);
359  cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER);
360 }
361 
362 } // namespace ns3
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:102
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
Definition: object-vector.h:51
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:79
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:258
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
hold variables of type string
Definition: string.h:18
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:246
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
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:95
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:825
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by ARP
bool IsAlive(void)
Definition: arp-cache.cc:285
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:86
Packet header for IPv4.
Definition: ipv4-header.h:31
virtual void NotifyNewAggregate()
This method is invoked whenever two sets of objects are aggregated together.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void MarkWaitReply(Ptr< Packet > waiting)
Definition: arp-cache.cc:334
bool IsDead(void)
Definition: arp-cache.cc:279
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
bool IsExpired(void) const
Definition: arp-cache.cc:382
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
Address GetMacAddress(void) const
Definition: arp-cache.cc:346
virtual void NotifyNewAggregate(void)
This method is invoked whenever two sets of objects are aggregated together.
Definition: object.cc:314
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)
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:81
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Address GetDestinationHardwareAddress(void)
Returns the destination hardware address.
Definition: arp-header.cc:77
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:307
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:160
void Flush(void)
Clear the ArpCache of all entries.
Definition: arp-cache.cc:230
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:318
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:38
uint32_t GetId(void) const
Definition: node.cc:106
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:291
Ptr< Packet > DequeuePending(void)
Definition: arp-cache.cc:395
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:64
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:274
contain a set of ns3::Object pointers.
Ptr< T > GetObject(void) const
Definition: object.h:362
CacheList m_cacheList
ARP cache container.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:204
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