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 "arp-queue-disc-item.h"
34#include "ipv4-interface.h"
35#include "ns3/traffic-control-layer.h"
36
37namespace ns3 {
38
39NS_LOG_COMPONENT_DEFINE ("ArpL3Protocol");
40
41const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806;
42
43NS_OBJECT_ENSURE_REGISTERED (ArpL3Protocol);
44
45TypeId
47{
48 static TypeId tid = TypeId ("ns3::ArpL3Protocol")
49 .SetParent<Object> ()
50 .AddConstructor<ArpL3Protocol> ()
51 .SetGroupName ("Internet")
52 .AddAttribute ("CacheList",
53 "The list of ARP caches",
56 MakeObjectVectorChecker<ArpCache> ())
57 .AddAttribute ("RequestJitter",
58 "The jitter in ms a node is allowed to wait "
59 "before sending an ARP request. Some jitter aims "
60 "to prevent collisions. By default, the model "
61 "will wait for a duration in ms defined by "
62 "a uniform random-variable between 0 and RequestJitter",
63 StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]"),
65 MakePointerChecker<RandomVariableStream> ())
66 .AddTraceSource ("Drop",
67 "Packet dropped because not enough room "
68 "in pending queue for a specific cache entry.",
70 "ns3::Packet::TracedCallback")
71 ;
72 return tid;
73}
74
76 : m_tc (0)
77{
78 NS_LOG_FUNCTION (this);
79}
80
82{
83 NS_LOG_FUNCTION (this);
84}
85
86int64_t
88{
89 NS_LOG_FUNCTION (this << stream);
90 m_requestJitter->SetStream (stream);
91 return 1;
92}
93
94void
96{
97 NS_LOG_FUNCTION (this << node);
98 m_node = node;
99}
100
101void
103{
104 NS_LOG_FUNCTION (this << tc);
105 m_tc = tc;
106}
107
108/*
109 * This method is called by AggregateObject and completes the aggregation
110 * by setting the node in the ipv4 stack
111 */
112void
114{
115 NS_LOG_FUNCTION (this);
116 if (m_node == 0)
117 {
118 Ptr<Node>node = this->GetObject<Node> ();
119 //verify that it's a valid node and that
120 //the node was not set before
121 if (node != 0)
122 {
123 this->SetNode (node);
124 }
125 }
127}
128
129void
131{
132 NS_LOG_FUNCTION (this);
133 for (CacheList::iterator i = m_cacheList.begin (); i != m_cacheList.end (); ++i)
134 {
135 Ptr<ArpCache> cache = *i;
136 cache->Dispose ();
137 }
138 m_cacheList.clear ();
139 m_node = 0;
140 m_tc = 0;
142}
143
146{
147 NS_LOG_FUNCTION (this << device << interface);
149 Ptr<ArpCache> cache = CreateObject<ArpCache> ();
150 cache->SetDevice (device, interface);
151 NS_ASSERT (device->IsBroadcast ());
153 cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
154 m_cacheList.push_back (cache);
155 return cache;
156}
157
160{
161 NS_LOG_FUNCTION (this << device);
162 for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
163 {
164 if ((*i)->GetDevice () == device)
165 {
166 return *i;
167 }
168 }
169 NS_ASSERT (false);
170 // quiet compiler
171 return 0;
172}
173
174void
175ArpL3Protocol::Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
176 const Address &to, NetDevice::PacketType packetType)
177{
178 NS_LOG_FUNCTION (this << device << p->GetSize () << protocol << from << to << packetType);
179
180 Ptr<Packet> packet = p->Copy ();
181
182 NS_LOG_LOGIC ("ARP: received packet of size "<< packet->GetSize ());
183
184 Ptr<ArpCache> cache = FindCache (device);
185
186 //
187 // If we're connected to a real world network, then some of the fields sizes
188 // in an ARP packet can vary in ways not seen in simulations. We need to be
189 // able to detect ARP packets with headers we don't recongnize and not process
190 // them instead of crashing. The ArpHeader will return 0 if it can't deal
191 // with the received header.
192 //
193 ArpHeader arp;
194 uint32_t size = packet->RemoveHeader (arp);
195 if (size == 0)
196 {
197 NS_LOG_LOGIC ("ARP: Cannot remove ARP header");
198 return;
199 }
200 NS_LOG_LOGIC ("ARP: received " << (arp.IsRequest () ? "request" : "reply") <<
201 " node=" << m_node->GetId () <<
202 ", got " <<
203 (arp.IsRequest () ? "request" : "reply") <<
204 " from " << arp.GetSourceIpv4Address () <<
205 " for address " << arp.GetDestinationIpv4Address () <<
206 "; we have addresses: ");
207 for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
208 {
209 NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
210 }
211
217 bool found = false;
218 for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
219 {
220 if (arp.IsRequest () && arp.GetDestinationIpv4Address () ==
221 cache->GetInterface ()->GetAddress (i).GetLocal ())
222 {
223 found = true;
224 NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " <<
225 arp.GetSourceIpv4Address () << " -- send reply");
228 break;
229 }
230 else if (arp.IsReply () &&
231 arp.GetDestinationIpv4Address () == cache->GetInterface ()->GetAddress (i).GetLocal () &&
232 arp.GetDestinationHardwareAddress () == device->GetAddress ())
233 {
234 found = true;
235 Ipv4Address from = arp.GetSourceIpv4Address ();
236 ArpCache::Entry *entry = cache->Lookup (from);
237 if (entry != 0)
238 {
239 if (entry->IsWaitReply ())
240 {
241 NS_LOG_LOGIC ("node="<< m_node->GetId () <<
242 ", got reply from " << arp.GetSourceIpv4Address ()
243 << " for waiting entry -- flush");
244 Address from_mac = arp.GetSourceHardwareAddress ();
245 entry->MarkAlive (from_mac);
247 while (pending.first != 0)
248 {
249 cache->GetInterface ()->Send (pending.first, pending.second,
250 arp.GetSourceIpv4Address ());
251 pending = entry->DequeuePending ();
252 }
253 }
254 else
255 {
256 // ignore this reply which might well be an attempt
257 // at poisening my arp cache.
258 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply from " <<
259 arp.GetSourceIpv4Address () <<
260 " for non-waiting entry -- drop");
261 m_dropTrace (packet);
262 }
263 }
264 else
265 {
266 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
267 m_dropTrace (packet);
268 }
269 break;
270 }
271 }
272 if (found == false)
273 {
274 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
275 arp.GetSourceIpv4Address () << " for unknown address " <<
276 arp.GetDestinationIpv4Address () << " -- drop");
277 }
278}
279
280bool
281ArpL3Protocol::Lookup (Ptr<Packet> packet, const Ipv4Header & ipHeader, Ipv4Address destination,
282 Ptr<NetDevice> device,
283 Ptr<ArpCache> cache,
284 Address *hardwareDestination)
285{
286 NS_LOG_FUNCTION (this << packet << destination << device << cache << hardwareDestination);
287 ArpCache::Entry *entry = cache->Lookup (destination);
288 if (entry != 0)
289 {
290 if (entry->IsExpired ())
291 {
292 if (entry->IsDead ())
293 {
294 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
295 ", dead entry for " << destination << " expired -- send arp request");
296 entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
298 }
299 else if (entry->IsAlive ())
300 {
301 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
302 ", alive entry for " << destination << " expired -- send arp request");
303 entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
305 }
306 else
307 {
308 NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
309 }
310 }
311 else
312 {
313 if (entry->IsDead ())
314 {
315 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
316 ", dead entry for " << destination << " valid -- drop");
317 // add the Ipv4 header for tracing purposes
318 packet->AddHeader (ipHeader);
319 m_dropTrace (packet);
320 }
321 else if (entry->IsAlive ())
322 {
323 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
324 ", alive entry for " << destination << " valid -- send");
325 *hardwareDestination = entry->GetMacAddress ();
326 return true;
327 }
328 else if (entry->IsWaitReply ())
329 {
330 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
331 ", wait reply for " << destination << " valid -- drop previous");
332 if (!entry->UpdateWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader)))
333 {
334 // add the Ipv4 header for tracing purposes
335 packet->AddHeader (ipHeader);
336 m_dropTrace (packet);
337 }
338 }
339 else if (entry-> IsPermanent ())
340 {
341 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
342 ", permanent for " << destination << "valid -- send");
343 *hardwareDestination = entry->GetMacAddress ();
344 return true;
345 }
346 else
347 {
348 NS_LOG_LOGIC ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
349 }
350 }
351 }
352 else
353 {
354 // This is our first attempt to transmit data to this destination.
355 NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
356 ", no entry for " << destination << " -- send arp request");
357 entry = cache->Add (destination);
358 entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
360 }
361 return false;
362}
363
364void
366{
367 NS_LOG_FUNCTION (this << cache << to);
368 ArpHeader arp;
369 // need to pick a source address; use routing implementation to select
371 Ptr<NetDevice> device = cache->GetDevice ();
372 NS_ASSERT (device != 0);
373 Ptr<Packet> packet = Create<Packet> ();
374 Ipv4Address source = ipv4->SelectSourceAddress (device, to, Ipv4InterfaceAddress::GLOBAL);
375 NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
376 " || src: " << device->GetAddress () << " / " << source <<
377 " || dst: " << device->GetBroadcast () << " / " << to);
378 arp.SetRequest (device->GetAddress (), source, device->GetBroadcast (), to);
379 NS_ASSERT (m_tc != 0);
380 m_tc->Send (device, Create<ArpQueueDiscItem> (packet, device->GetBroadcast (), PROT_NUMBER, arp));
381}
382
383void
385{
386 NS_LOG_FUNCTION (this << cache << myIp << toIp << toMac);
387 ArpHeader arp;
388 NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
389 "|| src: " << cache->GetDevice ()->GetAddress () <<
390 " / " << myIp <<
391 " || dst: " << toMac << " / " << toIp);
392 arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
393 Ptr<Packet> packet = Create<Packet> ();
394 NS_ASSERT (m_tc != 0);
395 m_tc->Send (cache->GetDevice (), Create<ArpQueueDiscItem> (packet, toMac, PROT_NUMBER, arp));
396}
397
398} // namespace ns3
a polymophic address class
Definition: address.h:91
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:178
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:424
bool IsAlive(void)
Definition: arp-cache.cc:375
Ipv4PayloadHeaderPair DequeuePending(void)
Definition: arp-cache.cc:510
bool IsWaitReply(void)
Definition: arp-cache.cc:381
bool IsDead(void)
Definition: arp-cache.cc:369
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:404
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:440
Address GetMacAddress(void) const
Definition: arp-cache.cc:454
bool IsExpired(void) const
Definition: arp-cache.cc:497
Ptr< Ipv4Interface > GetInterface(void) const
Returns the Ipv4Interface that this ARP cache is associated with.
Definition: arp-cache.cc:129
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:330
void Flush(void)
Clear the ArpCache of all entries.
Definition: arp-cache.cc:244
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:318
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: arp-cache.h:173
The packet header for an ARP packet.
Definition: arp-header.h:35
Ipv4Address GetDestinationIpv4Address(void) const
Returns the destination IP address.
Definition: arp-header.cc:89
Address GetSourceHardwareAddress(void) const
Returns the source hardware address.
Definition: arp-header.cc:71
bool IsRequest(void) const
Check if the ARP is a request.
Definition: arp-header.cc:59
Ipv4Address GetSourceIpv4Address(void) const
Returns the source IP address.
Definition: arp-header.cc:83
Address GetDestinationHardwareAddress(void) const
Returns the destination hardware address.
Definition: arp-header.cc:77
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition: arp-header.cc:46
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition: arp-header.cc:33
bool IsReply(void) const
Check if the ARP is a reply.
Definition: arp-header.cc:65
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
virtual void DoDispose(void)
Destructor implementation.
Ptr< RandomVariableStream > m_requestJitter
jitter to de-sync ARP requests
bool Lookup(Ptr< Packet > p, const Ipv4Header &ipHeader, Ipv4Address destination, Ptr< NetDevice > device, Ptr< ArpCache > cache, Address *hardwareDestination)
Perform an ARP lookup.
static TypeId GetTypeId(void)
Get the type ID.
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 SendArpReply(Ptr< const ArpCache > cache, Ipv4Address myIp, Ipv4Address toIp, Address toMac)
Send an ARP reply to an host.
Ptr< TrafficControlLayer > m_tc
The associated TrafficControlLayer.
CacheList m_cacheList
ARP cache container.
Ptr< ArpCache > FindCache(Ptr< NetDevice > device)
Finds the cache associated with a NetDevice.
virtual void NotifyNewAggregate()
Notify all Objects aggregated to this one of a new Object being aggregated.
void SetNode(Ptr< Node > node)
Set the node the ARP L3 protocol is associated with.
static const uint16_t PROT_NUMBER
ARP protocol number (0x0806)
void SetTrafficControl(Ptr< TrafficControlLayer > tc)
Set the TrafficControlLayer.
Ptr< ArpCache > CreateCache(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Create an ARP cache for the device/interface.
void SendArpRequest(Ptr< const ArpCache >cache, Ipv4Address to)
Send an ARP request to an host.
Ptr< Node > m_node
node the ARP L3 protocol is associated with
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by ARP
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
static Ipv4Address GetBroadcast(void)
Packet header for IPv4.
Definition: ipv4-header.h:34
Ipv4Address GetLocal(void) const
Get the local address.
Ipv4InterfaceAddress GetAddress(uint32_t index) const
void Send(Ptr< Packet > p, const Ipv4Header &hdr, Ipv4Address dest)
Implement the IPv4 layer.
virtual Address GetAddress(void) const =0
virtual bool IsBroadcast(void) const =0
virtual void AddLinkChangeCallback(Callback< void > callback)=0
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
uint32_t GetId(void) const
Definition: node.cc:109
A base class which provides memory management and object aggregation.
Definition: object.h:88
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
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.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:227
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648