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 #ifndef ARP_L3_PROTOCOL_H
21 #define ARP_L3_PROTOCOL_H
22 
23 #include <list>
24 #include "ns3/ipv4-header.h"
25 #include "ns3/net-device.h"
26 #include "ns3/address.h"
27 #include "ns3/ptr.h"
28 #include "ns3/traced-callback.h"
29 #include "ns3/random-variable-stream.h"
30 
31 namespace ns3 {
32 
33 class ArpCache;
34 class NetDevice;
35 class Node;
36 class Packet;
37 class Ipv4Interface;
38 class TrafficControlLayer;
39 
40 /**
41  * \ingroup ipv4
42  * \defgroup arp ARP protocol.
43  *
44  * The ARP protocol and its associated tables are responsible
45  * for the IPv4 - MAC address translation.
46  * Each NetDevice has its own ARP table.
47  */
48 
49 /**
50  * \ingroup arp
51  * \brief An implementation of the ARP protocol.
52  */
53 class ArpL3Protocol : public Object
54 {
55 public:
56   /**
57    * \brief Get the type ID.
58    * \return the object TypeId
59    */
60   static TypeId GetTypeId (void);
61   static const uint16_t PROT_NUMBER; //!< ARP protocol number (0x0806)
62 
63   ArpL3Protocol ();
64   virtual ~ArpL3Protocol ();
65 
66   /**
67    * \brief Set the node the ARP L3 protocol is associated with
68    * \param node the node
69    */
70   void SetNode (Ptr<Node> node);
71 
72   /**
73    * \brief Set the TrafficControlLayer.
74    * \param tc TrafficControlLayer object
75    */
76   void SetTrafficControl (Ptr<TrafficControlLayer> tc);
77 
78   /**
79    * \brief Create an ARP cache for the device/interface
80    * \param device the NetDevice
81    * \param interface the Ipv4Interface
82    * \returns a smart pointer to the ARP cache
83    */
84   Ptr<ArpCache> CreateCache (Ptr<NetDevice> device, Ptr<Ipv4Interface> interface);
85 
86   /**
87    * \brief Receive a packet
88    * \param device the source NetDevice
89    * \param p the packet
90    * \param protocol the protocol
91    * \param from the source address
92    * \param to the destination address
93    * \param packetType type of packet (i.e., unicast, multicast, etc.)
94    */
95   void Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from, const Address &to,
96                 NetDevice::PacketType packetType);
97   /**
98    * \brief Perform an ARP lookup
99    * \param p the packet
100    * \param ipHeader the IPv4 header
101    * \param destination destination IP address
102    * \param device outgoing device
103    * \param cache ARP cache
104    * \param hardwareDestination filled with the destination MAC address (if the entry exists)
105    * \return true if there is a matching ARP Entry
106    */
107   bool Lookup (Ptr<Packet> p, const Ipv4Header & ipHeader, Ipv4Address destination,
108                Ptr<NetDevice> device,
109                Ptr<ArpCache> cache,
110                Address *hardwareDestination);
111 
112   /**
113    * Assign a fixed random variable stream number to the random variables
114    * used by this model.  Return the number of streams (possibly zero) that
115    * have been assigned.
116    *
117    * \param stream first stream index to use
118    * \return the number of stream indices assigned by this model
119    */
120   int64_t AssignStreams (int64_t stream);
121 
122 protected:
123   virtual void DoDispose (void);
124   /*
125    * This function will notify other components connected to the node that a new stack member is now connected
126    * This will be used to notify Layer 3 protocol of layer 4 protocol stack to connect them together.
127    */
128   virtual void NotifyNewAggregate ();
129 private:
130   typedef std::list<Ptr<ArpCache> > CacheList; //!< container of the ARP caches
131   /**
132    * \brief Copy constructor
133    *
134    * Defined and unimplemented to avoid misuse
135    * \param o
136    */
137   ArpL3Protocol (const ArpL3Protocol &o);
138   /**
139    * \brief Copy constructor
140    *
141    * Defined and unimplemented to avoid misuse
142    * \param o
143    * \returns
144    */
145   ArpL3Protocol &operator = (const ArpL3Protocol &o);
146 
147   /**
148    * \brief Finds the cache associated with a NetDevice
149    * \param device the NetDevice
150    * \returns the ARP cache, or null if no cache is found
151    */
152   Ptr<ArpCache> FindCache (Ptr<NetDevice> device);
153 
154   /**
155    * \brief Send an ARP request to an host
156    * \param cache the ARP cache to use
157    * \param to the destination IP
158    */
159   void SendArpRequest (Ptr<const ArpCache>cache, Ipv4Address to);
160   /**
161    * \brief Send an ARP reply to an host
162    * \param cache the ARP cache to use
163    * \param myIp the source IP address
164    * \param toIp the destination IP
165    * \param toMac the destination MAC address
166    */
167   void SendArpReply (Ptr<const ArpCache> cache, Ipv4Address myIp, Ipv4Address toIp, Address toMac);
168 
169   CacheList m_cacheList; //!< ARP cache container
170   Ptr<Node> m_node; //!< node the ARP L3 protocol is associated with
171   TracedCallback<Ptr<const Packet> > m_dropTrace; //!< trace for packets dropped by ARP
172   Ptr<RandomVariableStream> m_requestJitter; //!< jitter to de-sync ARP requests
173   Ptr<TrafficControlLayer> m_tc; //!< The associated TrafficControlLayer
174 
175 };
176 
177 } // namespace ns3
178 
179 
180 #endif /* ARP_L3_PROTOCOL_H */
181