A Discrete-Event Network Simulator
API
arp-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "arp-header.h"
21 
22 #include "ns3/address-utils.h"
23 #include "ns3/assert.h"
24 #include "ns3/log.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("ArpHeader");
30 
32 
33 void
34 ArpHeader::SetRequest(Address sourceHardwareAddress,
35  Ipv4Address sourceProtocolAddress,
36  Address destinationHardwareAddress,
37  Ipv4Address destinationProtocolAddress)
38 {
39  NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
40  << destinationHardwareAddress << destinationProtocolAddress);
42  m_macSource = sourceHardwareAddress;
43  m_macDest = destinationHardwareAddress;
44  m_ipv4Source = sourceProtocolAddress;
45  m_ipv4Dest = destinationProtocolAddress;
46 }
47 
48 void
49 ArpHeader::SetReply(Address sourceHardwareAddress,
50  Ipv4Address sourceProtocolAddress,
51  Address destinationHardwareAddress,
52  Ipv4Address destinationProtocolAddress)
53 {
54  NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
55  << destinationHardwareAddress << destinationProtocolAddress);
57  m_macSource = sourceHardwareAddress;
58  m_macDest = destinationHardwareAddress;
59  m_ipv4Source = sourceProtocolAddress;
60  m_ipv4Dest = destinationProtocolAddress;
61 }
62 
63 bool
65 {
66  NS_LOG_FUNCTION(this);
67  return m_type == ARP_TYPE_REQUEST;
68 }
69 
70 bool
72 {
73  NS_LOG_FUNCTION(this);
74  return m_type == ARP_TYPE_REPLY;
75 }
76 
77 Address
79 {
80  NS_LOG_FUNCTION(this);
81  return m_macSource;
82 }
83 
84 Address
86 {
87  NS_LOG_FUNCTION(this);
88  return m_macDest;
89 }
90 
93 {
94  NS_LOG_FUNCTION(this);
95  return m_ipv4Source;
96 }
97 
100 {
101  NS_LOG_FUNCTION(this);
102  return m_ipv4Dest;
103 }
104 
105 TypeId
107 {
108  static TypeId tid = TypeId("ns3::ArpHeader")
109  .SetParent<Header>()
110  .SetGroupName("Internet")
111  .AddConstructor<ArpHeader>();
112  return tid;
113 }
114 
115 TypeId
117 {
118  NS_LOG_FUNCTION(this);
119  return GetTypeId();
120 }
121 
122 void
123 ArpHeader::Print(std::ostream& os) const
124 {
125  NS_LOG_FUNCTION(this << &os);
126  if (IsRequest())
127  {
128  os << "request "
129  << "source mac: " << m_macSource << " "
130  << "source ipv4: " << m_ipv4Source << " "
131  << "dest ipv4: " << m_ipv4Dest;
132  }
133  else
134  {
135  NS_ASSERT(IsReply());
136  os << "reply "
137  << "source mac: " << m_macSource << " "
138  << "source ipv4: " << m_ipv4Source << " "
139  << "dest mac: " << m_macDest << " "
140  << "dest ipv4: " << m_ipv4Dest;
141  }
142 }
143 
144 uint32_t
146 {
147  NS_LOG_FUNCTION(this);
148  NS_ASSERT((m_macSource.GetLength() == 6) || (m_macSource.GetLength() == 8) ||
149  (m_macSource.GetLength() == 1));
151 
152  uint32_t length = 16; // Length minus two hardware addresses
153  length += m_macSource.GetLength() * 2;
154 
155  return length;
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION(this << &start);
164 
165  /* ethernet */
166  i.WriteHtonU16(0x0001);
167  /* ipv4 */
168  i.WriteHtonU16(0x0800);
170  i.WriteU8(4);
171  i.WriteHtonU16(m_type);
172  WriteTo(i, m_macSource);
173  WriteTo(i, m_ipv4Source);
174  WriteTo(i, m_macDest);
175  WriteTo(i, m_ipv4Dest);
176 }
177 
178 uint32_t
180 {
181  NS_LOG_FUNCTION(this << &start);
183  i.Next(2); // Skip HRD
184  uint32_t protocolType = i.ReadNtohU16(); // Read PRO
185  uint32_t hardwareAddressLen = i.ReadU8(); // Read HLN
186  uint32_t protocolAddressLen = i.ReadU8(); // Read PLN
187 
188  //
189  // It is implicit here that we have a protocol type of 0x800 (IP).
190  // It is also implicit here that we are using Ipv4 (PLN == 4).
191  // If this isn't the case, we need to return an error since we don't want to
192  // be too fragile if we get connected to real networks.
193  //
194  if (protocolType != 0x800 || protocolAddressLen != 4)
195  {
196  return 0;
197  }
198 
199  m_type = i.ReadNtohU16(); // Read OP
200  ReadFrom(i, m_macSource, hardwareAddressLen); // Read SHA (size HLN)
201  ReadFrom(i, m_ipv4Source); // Read SPA (size PLN == 4)
202  ReadFrom(i, m_macDest, hardwareAddressLen); // Read THA (size HLN)
203  ReadFrom(i, m_ipv4Dest); // Read TPA (size PLN == 4)
204  return GetSerializedSize();
205 }
206 
207 } // namespace ns3
a polymophic address class
Definition: address.h:101
uint8_t GetLength() const
Get the length of the underlying address.
Definition: address.cc:78
The packet header for an ARP packet.
Definition: arp-header.h:36
Address m_macSource
hardware source address
Definition: arp-header.h:120
void Print(std::ostream &os) const override
Definition: arp-header.cc:123
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition: arp-header.cc:49
bool IsReply() const
Check if the ARP is a reply.
Definition: arp-header.cc:71
bool IsRequest() const
Check if the ARP is a request.
Definition: arp-header.cc:64
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Definition: arp-header.cc:85
uint16_t m_type
type of the ICMP (ARP_TYPE_REQUEST)
Definition: arp-header.h:119
Ipv4Address GetDestinationIpv4Address() const
Returns the destination IP address.
Definition: arp-header.cc:99
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition: arp-header.cc:34
Address m_macDest
hardware destination address
Definition: arp-header.h:121
void Serialize(Buffer::Iterator start) const override
Definition: arp-header.cc:159
Ipv4Address m_ipv4Dest
IP destination address.
Definition: arp-header.h:123
Ipv4Address GetSourceIpv4Address() const
Returns the source IP address.
Definition: arp-header.cc:92
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: arp-header.cc:116
static TypeId GetTypeId()
Get the type ID.
Definition: arp-header.cc:106
Ipv4Address m_ipv4Source
IP source address.
Definition: arp-header.h:122
uint32_t GetSerializedSize() const override
Definition: arp-header.cc:145
Address GetSourceHardwareAddress() const
Returns the source hardware address.
Definition: arp-header.cc:78
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
uint16_t ReadNtohU16()
Definition: buffer.h:954
void Next()
go forward by one byte
Definition: buffer.h:853
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.