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 () <<
191  ", got " <<
192  (arp.IsRequest () ? "request" : "reply") <<
193  " from " << arp.GetSourceIpv4Address () <<
194  " for address " << arp.GetDestinationIpv4Address () <<
195  "; we have addresses: ");
196  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
197  {
198  NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
199  }
200 
206  bool found = false;
207  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
208  {
209  if (arp.IsRequest () && arp.GetDestinationIpv4Address () ==
210  cache->GetInterface ()->GetAddress (i).GetLocal ())
211  {
212  found = true;
213  NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " <<
214  arp.GetSourceIpv4Address () << " -- send reply");
216  arp.GetSourceHardwareAddress ());
217  break;
218  }
219  else if (arp.IsReply () &&
220  arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress (i).GetLocal ()) &&
221  arp.GetDestinationHardwareAddress () == device->GetAddress ())
222  {
223  found = true;
224  Ipv4Address from = arp.GetSourceIpv4Address ();
225  ArpCache::Entry *entry = cache->Lookup (from);
226  if (entry != 0)
227  {
228  if (entry->IsWaitReply ())
229  {
230  NS_LOG_LOGIC ("node="<< m_node->GetId () <<
231  ", got reply from " << arp.GetSourceIpv4Address ()
232  << " for waiting entry -- flush");
233  Address from_mac = arp.GetSourceHardwareAddress ();
234  entry->MarkAlive (from_mac);
236  while (pending.first != 0)
237  {
238  cache->GetInterface ()->Send (pending.first, pending.second,
239  arp.GetSourceIpv4Address ());
240  pending = entry->DequeuePending ();
241  }
242  }
243  else
244  {
245  // ignore this reply which might well be an attempt
246  // at poisening my arp cache.
247  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply from " <<
248  arp.GetSourceIpv4Address () <<
249  " for non-waiting entry -- drop");
250  m_dropTrace (packet);
251  }
252  }
253  else
254  {
255  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
256  m_dropTrace (packet);
257  }
258  break;
259  }
260  }
261  if (found == false)
262  {
263  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
264  arp.GetSourceIpv4Address () << " for unknown address " <<
265  arp.GetDestinationIpv4Address () << " -- drop");
266  }
267 }
268 
269 bool
270 ArpL3Protocol::Lookup (Ptr<Packet> packet, const Ipv4Header & ipHeader, Ipv4Address destination,
271  Ptr<NetDevice> device,
272  Ptr<ArpCache> cache,
273  Address *hardwareDestination)
274 {
275  NS_LOG_FUNCTION (this << packet << destination << device << cache << hardwareDestination);
276  ArpCache::Entry *entry = cache->Lookup (destination);
277  if (entry != 0)
278  {
279  if (entry->IsExpired ())
280  {
281  if (entry->IsDead ())
282  {
283  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
284  ", dead entry for " << destination << " expired -- send arp request");
285  entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
287  }
288  else if (entry->IsAlive ())
289  {
290  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
291  ", alive entry for " << destination << " expired -- send arp request");
292  entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
294  }
295  else
296  {
297  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
298  }
299  }
300  else
301  {
302  if (entry->IsDead ())
303  {
304  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
305  ", dead entry for " << destination << " valid -- drop");
306  // add the Ipv4 header for tracing purposes
307  packet->AddHeader (ipHeader);
308  m_dropTrace (packet);
309  }
310  else if (entry->IsAlive ())
311  {
312  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
313  ", alive entry for " << destination << " valid -- send");
314  *hardwareDestination = entry->GetMacAddress ();
315  return true;
316  }
317  else if (entry->IsWaitReply ())
318  {
319  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
320  ", wait reply for " << destination << " valid -- drop previous");
321  if (!entry->UpdateWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader)))
322  {
323  // add the Ipv4 header for tracing purposes
324  packet->AddHeader (ipHeader);
325  m_dropTrace (packet);
326  }
327  }
328  else if (entry-> IsPermanent ())
329  {
330  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
331  ", permanent for " << destination << "valid -- send");
332  *hardwareDestination = entry->GetMacAddress ();
333  return true;
334  }
335  else
336  {
337  NS_LOG_LOGIC ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
338  }
339  }
340  }
341  else
342  {
343  // This is our first attempt to transmit data to this destination.
344  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
345  ", no entry for " << destination << " -- send arp request");
346  entry = cache->Add (destination);
347  entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
349  }
350  return false;
351 }
352 
353 void
355 {
356  NS_LOG_FUNCTION (this << cache << to);
357  ArpHeader arp;
358  // need to pick a source address; use routing implementation to select
360  Ptr<NetDevice> device = cache->GetDevice ();
361  NS_ASSERT (device != 0);
362  Ipv4Header header;
363  header.SetDestination (to);
364  Ptr<Packet> packet = Create<Packet> ();
365  Ipv4Address source = ipv4->SelectSourceAddress (device, to, Ipv4InterfaceAddress::GLOBAL);
366  NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
367  " || src: " << device->GetAddress () << " / " << source <<
368  " || dst: " << device->GetBroadcast () << " / " << to);
369  arp.SetRequest (device->GetAddress (), source, device->GetBroadcast (), to);
370  packet->AddHeader (arp);
371  cache->GetDevice ()->Send (packet, device->GetBroadcast (), PROT_NUMBER);
372 }
373 
374 void
376 {
377  NS_LOG_FUNCTION (this << cache << myIp << toIp << toMac);
378  ArpHeader arp;
379  NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
380  "|| src: " << cache->GetDevice ()->GetAddress () <<
381  " / " << myIp <<
382  " || dst: " << toMac << " / " << toIp);
383  arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
384  Ptr<Packet> packet = Create<Packet> ();
385  packet->AddHeader (arp);
386  cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER);
387 }
388 
389 } // namespace ns3
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:114
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 the RngStream.
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:330
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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:318
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:296
#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:1001
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:796
Ipv4Address GetSourceIpv4Address(void)
Returns the source IP address.
Definition: arp-header.cc:83
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition: arp-header.cc:33
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
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.
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by ARP
bool IsAlive(void)
Definition: arp-cache.cc:375
void SendArpRequest(Ptr< const ArpCache >cache, Ipv4Address to)
Send an ARP request to an host.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the 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:33
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:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
virtual void DoDispose(void)
Destructor implementation.
bool IsDead(void)
Definition: arp-cache.cc:369
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
bool IsExpired(void) const
Definition: arp-cache.cc:503
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:440
Address GetMacAddress(void) const
Definition: arp-cache.cc:454
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
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:404
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:188
void Flush(void)
Clear the ArpCache of all entries.
Definition: arp-cache.cc:244
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:424
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
Ipv4PayloadHeaderPair DequeuePending(void)
Definition: arp-cache.cc:516
uint32_t GetId(void) const
Definition: node.cc:107
void SetNode(Ptr< Node > node)
Set the node the ARP L3 protocol is associated with.
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: arp-cache.h:183
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:381
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
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
Container for a set of ns3::Object pointers.
CacheList m_cacheList
ARP cache container.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
bool Lookup(Ptr< Packet > p, const Ipv4Header &ipHeader, Ipv4Address destination, Ptr< NetDevice > device, Ptr< ArpCache > cache, Address *hardwareDestination)
Perform an ARP lookup.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
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