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 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::ArpL3Protocol")
48  .SetParent<Object> ()
49  .AddConstructor<ArpL3Protocol> ()
50  .AddAttribute ("CacheList",
51  "The list of ARP caches",
54  MakeObjectVectorChecker<ArpCache> ())
55  .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",
56  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]"),
57  MakePointerAccessor (&ArpL3Protocol::m_requestJitter),
58  MakePointerChecker<RandomVariableStream> ())
59  .AddTraceSource ("Drop",
60  "Packet dropped because not enough room in pending queue for a specific cache entry.",
62  ;
63  return tid;
64 }
65 
67 {
68  NS_LOG_FUNCTION (this);
69 }
70 
72 {
73  NS_LOG_FUNCTION (this);
74 }
75 
76 int64_t
78 {
79  NS_LOG_FUNCTION (this << stream);
80  m_requestJitter->SetStream (stream);
81  return 1;
82 }
83 
84 void
86 {
87  NS_LOG_FUNCTION (this << node);
88  m_node = node;
89 }
90 
91 /*
92  * This method is called by AddAgregate and completes the aggregation
93  * by setting the node in the ipv4 stack
94  */
95 void
97 {
98  NS_LOG_FUNCTION (this);
99  if (m_node == 0)
100  {
101  Ptr<Node>node = this->GetObject<Node> ();
102  //verify that it's a valid node and that
103  //the node was not set before
104  if (node != 0)
105  {
106  this->SetNode (node);
107  }
108  }
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this);
116  for (CacheList::iterator i = m_cacheList.begin (); i != m_cacheList.end (); ++i)
117  {
118  Ptr<ArpCache> cache = *i;
119  cache->Dispose ();
120  }
121  m_cacheList.clear ();
122  m_node = 0;
124 }
125 
128 {
129  NS_LOG_FUNCTION (this << device << interface);
131  Ptr<ArpCache> cache = CreateObject<ArpCache> ();
132  cache->SetDevice (device, interface);
133  NS_ASSERT (device->IsBroadcast ());
134  device->AddLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
135  cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
136  m_cacheList.push_back (cache);
137  return cache;
138 }
139 
142 {
143  NS_LOG_FUNCTION (this << device);
144  for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
145  {
146  if ((*i)->GetDevice () == device)
147  {
148  return *i;
149  }
150  }
151  NS_ASSERT (false);
152  // quiet compiler
153  return 0;
154 }
155 
156 void
157 ArpL3Protocol::Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
158  const Address &to, NetDevice::PacketType packetType)
159 {
160  NS_LOG_FUNCTION (this << device << p->GetSize () << protocol << from << to << packetType);
161 
162  Ptr<Packet> packet = p->Copy ();
163 
164  NS_LOG_LOGIC ("ARP: received packet of size "<< packet->GetSize ());
165 
166  Ptr<ArpCache> cache = FindCache (device);
167 
168  //
169  // If we're connected to a real world network, then some of the fields sizes
170  // in an ARP packet can vary in ways not seen in simulations. We need to be
171  // able to detect ARP packets with headers we don't recongnize and not process
172  // them instead of crashing. The ArpHeader will return 0 if it can't deal
173  // with the received header.
174  //
175  ArpHeader arp;
176  uint32_t size = packet->RemoveHeader (arp);
177  if (size == 0)
178  {
179  NS_LOG_LOGIC ("ARP: Cannot remove ARP header");
180  return;
181  }
182  NS_LOG_LOGIC ("ARP: received "<< (arp.IsRequest () ? "request" : "reply") <<
183  " node="<<m_node->GetId ()<<", got request from " <<
184  arp.GetSourceIpv4Address () << " for address " <<
185  arp.GetDestinationIpv4Address () << "; we have addresses: ");
186  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
187  {
188  NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
189  }
190 
195  bool found = false;
196  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
197  {
198  if (arp.IsRequest () && arp.GetDestinationIpv4Address () ==
199  cache->GetInterface ()->GetAddress (i).GetLocal ())
200  {
201  found = true;
202  NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " <<
203  arp.GetSourceIpv4Address () << " -- send reply");
205  arp.GetSourceHardwareAddress ());
206  break;
207  }
208  else if (arp.IsReply () &&
209  arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress (i).GetLocal ()) &&
210  arp.GetDestinationHardwareAddress () == device->GetAddress ())
211  {
212  found = true;
213  Ipv4Address from = arp.GetSourceIpv4Address ();
214  ArpCache::Entry *entry = cache->Lookup (from);
215  if (entry != 0)
216  {
217  if (entry->IsWaitReply ())
218  {
219  NS_LOG_LOGIC ("node="<< m_node->GetId () <<
220  ", got reply from " << arp.GetSourceIpv4Address ()
221  << " for waiting entry -- flush");
222  Address from_mac = arp.GetSourceHardwareAddress ();
223  entry->MarkAlive (from_mac);
224  Ptr<Packet> pending = entry->DequeuePending ();
225  while (pending != 0)
226  {
227  cache->GetInterface ()->Send (pending,
228  arp.GetSourceIpv4Address ());
229  pending = entry->DequeuePending ();
230  }
231  }
232  else
233  {
234  // ignore this reply which might well be an attempt
235  // at poisening my arp cache.
236  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply from " <<
237  arp.GetSourceIpv4Address () <<
238  " for non-waiting entry -- drop");
239  m_dropTrace (packet);
240  }
241  }
242  else
243  {
244  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
245  m_dropTrace (packet);
246  }
247  break;
248  }
249  }
250  if (found == false)
251  {
252  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
253  arp.GetSourceIpv4Address () << " for unknown address " <<
254  arp.GetDestinationIpv4Address () << " -- drop");
255  }
256 }
257 
258 bool
260  Ptr<NetDevice> device,
261  Ptr<ArpCache> cache,
262  Address *hardwareDestination)
263 {
264  NS_LOG_FUNCTION (this << packet << destination << device << cache << hardwareDestination);
265  ArpCache::Entry *entry = cache->Lookup (destination);
266  if (entry != 0)
267  {
268  if (entry->IsExpired ())
269  {
270  if (entry->IsDead ())
271  {
272  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
273  ", dead entry for " << destination << " expired -- send arp request");
274  entry->MarkWaitReply (packet);
275  Simulator::Schedule (Time (MilliSeconds (m_requestJitter->GetValue ())), &ArpL3Protocol::SendArpRequest, this, cache, destination);
276  }
277  else if (entry->IsAlive ())
278  {
279  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
280  ", alive entry for " << destination << " expired -- send arp request");
281  entry->MarkWaitReply (packet);
282  Simulator::Schedule (Time (MilliSeconds (m_requestJitter->GetValue ())), &ArpL3Protocol::SendArpRequest, this, cache, destination);
283  }
284  else if (entry->IsWaitReply ())
285  {
286  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
287  }
288  }
289  else
290  {
291  if (entry->IsDead ())
292  {
293  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
294  ", dead entry for " << destination << " valid -- drop");
295  m_dropTrace (packet);
296  }
297  else if (entry->IsAlive ())
298  {
299  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
300  ", alive entry for " << destination << " valid -- send");
301  *hardwareDestination = entry->GetMacAddress ();
302  return true;
303  }
304  else if (entry->IsWaitReply ())
305  {
306  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
307  ", wait reply for " << destination << " valid -- drop previous");
308  if (!entry->UpdateWaitReply (packet))
309  {
310  m_dropTrace (packet);
311  }
312  }
313  }
314  }
315  else
316  {
317  // This is our first attempt to transmit data to this destination.
318  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
319  ", no entry for " << destination << " -- send arp request");
320  entry = cache->Add (destination);
321  entry->MarkWaitReply (packet);
322  Simulator::Schedule (Time (MilliSeconds (m_requestJitter->GetValue ())), &ArpL3Protocol::SendArpRequest, this, cache, destination);
323  }
324  return false;
325 }
326 
327 void
329 {
330  NS_LOG_FUNCTION (this << cache << to);
331  ArpHeader arp;
332  // need to pick a source address; use routing implementation to select
334  Ptr<NetDevice> device = cache->GetDevice ();
335  NS_ASSERT (device != 0);
336  Ipv4Header header;
337  header.SetDestination (to);
338  Ptr<Packet> packet = Create<Packet> ();
339  Ipv4Address source = ipv4->SelectSourceAddress (device, to, Ipv4InterfaceAddress::GLOBAL);
340  NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
341  " || src: " << device->GetAddress () << " / " << source <<
342  " || dst: " << device->GetBroadcast () << " / " << to);
343  arp.SetRequest (device->GetAddress (), source, device->GetBroadcast (), to);
344  packet->AddHeader (arp);
345  cache->GetDevice ()->Send (packet, device->GetBroadcast (), PROT_NUMBER);
346 }
347 
348 void
350 {
351  NS_LOG_FUNCTION (this << cache << myIp << toIp << toMac);
352  ArpHeader arp;
353  NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
354  "|| src: " << cache->GetDevice ()->GetAddress () <<
355  " / " << myIp <<
356  " || dst: " << toMac << " / " << toIp);
357  arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
358  Ptr<Packet> packet = Create<Packet> ();
359  packet->AddHeader (arp);
360  cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER);
361 }
362 
363 } // namespace ns3
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:103
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:47
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
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)
Definition: log.h:345
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:259
hold variables of type string
Definition: string.h:19
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:247
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
Definition: packet.h:650
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:336
Ipv4Address GetSourceIpv4Address(void)
Returns the source IP address.
Definition: arp-header.cc:84
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition: arp-header.cc:34
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:824
NS_LOG_COMPONENT_DEFINE("ArpL3Protocol")
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by ARP
bool IsAlive(void)
Definition: arp-cache.cc:286
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.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
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:335
bool IsDead(void)
Definition: arp-cache.cc:280
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
bool IsExpired(void) const
Definition: arp-cache.cc:383
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Address GetMacAddress(void) const
Definition: arp-cache.cc:347
virtual void NotifyNewAggregate(void)
This method is invoked whenever two sets of objects are aggregated together.
Definition: object.cc:315
Ptr< Packet > Copy(void) const
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:78
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:308
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:231
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:319
Ipv4Address GetDestinationIpv4Address(void)
Returns the destination IP address.
Definition: arp-header.cc:90
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
uint32_t GetId(void) const
Definition: node.cc:104
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:72
Ptr< RandomVariableStream > m_requestJitter
jitter to de-sync ARP requests
bool IsWaitReply(void)
Definition: arp-cache.cc:292
Ptr< Packet > DequeuePending(void)
Definition: arp-cache.cc:396
bool IsReply(void) const
Check if the ARP is a reply.
Definition: arp-header.cc:66
a base class which provides memory management and object aggregation
Definition: object.h:63
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:270
contain a set of ns3::Object pointers.
Ptr< T > GetObject(void) const
Definition: object.h:361
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:611
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:205
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:60