A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
arp-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "arp-header.h"
10
11#include "ns3/address-utils.h"
12#include "ns3/assert.h"
13#include "ns3/iana-ieee802-numbers.h"
14#include "ns3/log.h"
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("ArpHeader");
20
22
23void
24ArpHeader::SetRequest(Address sourceHardwareAddress,
25 Ipv4Address sourceProtocolAddress,
26 Address destinationHardwareAddress,
27 Ipv4Address destinationProtocolAddress)
28{
29 NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
30 << destinationHardwareAddress << destinationProtocolAddress);
31 m_hardwareType = DetermineHardwareType(sourceHardwareAddress);
33 m_macSource = sourceHardwareAddress;
34 m_macDest = destinationHardwareAddress;
35 m_ipv4Source = sourceProtocolAddress;
36 m_ipv4Dest = destinationProtocolAddress;
37}
38
39void
40ArpHeader::SetReply(Address sourceHardwareAddress,
41 Ipv4Address sourceProtocolAddress,
42 Address destinationHardwareAddress,
43 Ipv4Address destinationProtocolAddress)
44{
45 NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
46 << destinationHardwareAddress << destinationProtocolAddress);
47 m_hardwareType = DetermineHardwareType(sourceHardwareAddress);
49 m_macSource = sourceHardwareAddress;
50 m_macDest = destinationHardwareAddress;
51 m_ipv4Source = sourceProtocolAddress;
52 m_ipv4Dest = destinationProtocolAddress;
53}
54
57{
58 NS_LOG_FUNCTION(this << address);
59 uint8_t addressLength = address.GetLength();
60 switch (addressLength)
61 {
62 case 6:
64 case 8:
66 default:
68 }
69}
70
71bool
73{
74 NS_LOG_FUNCTION(this);
75 return m_type == ARP_TYPE_REQUEST;
76}
77
78bool
80{
81 NS_LOG_FUNCTION(this);
82 return m_type == ARP_TYPE_REPLY;
83}
84
87{
88 NS_LOG_FUNCTION(this);
89 return m_hardwareType;
90}
91
98
105
108{
109 NS_LOG_FUNCTION(this);
110 return m_ipv4Source;
111}
112
115{
116 NS_LOG_FUNCTION(this);
117 return m_ipv4Dest;
118}
119
120TypeId
122{
123 static TypeId tid = TypeId("ns3::ArpHeader")
124 .SetParent<Header>()
125 .SetGroupName("Internet")
126 .AddConstructor<ArpHeader>();
127 return tid;
128}
129
130TypeId
132{
133 NS_LOG_FUNCTION(this);
134 return GetTypeId();
135}
136
137void
138ArpHeader::Print(std::ostream& os) const
139{
140 NS_LOG_FUNCTION(this << &os);
141 if (IsRequest())
142 {
143 os << "hardware type: " << GetHardwareType() << " "
144 << "request "
145 << "source mac: " << m_macSource << " "
146 << "source ipv4: " << m_ipv4Source << " "
147 << "dest ipv4: " << m_ipv4Dest;
148 }
149 else
150 {
152 os << "hardware type: " << GetHardwareType() << " "
153 << "reply "
154 << "source mac: " << m_macSource << " "
155 << "source ipv4: " << m_ipv4Source << " "
156 << "dest mac: " << m_macDest << " "
157 << "dest ipv4: " << m_ipv4Dest;
158 }
159}
160
163{
164 NS_LOG_FUNCTION(this);
165 NS_ASSERT((m_macSource.GetLength() == 6) || (m_macSource.GetLength() == 8) ||
166 (m_macSource.GetLength() == 1));
167 NS_ASSERT(m_macSource.GetLength() == m_macDest.GetLength());
168
169 uint32_t length = 16; // Length minus two hardware addresses
170 length += m_macSource.GetLength() * 2;
171
172 return length;
173}
174
175void
177{
178 NS_LOG_FUNCTION(this << &start);
179 Buffer::Iterator i = start;
180 NS_ASSERT(m_macSource.GetLength() == m_macDest.GetLength());
181
182 i.WriteHtonU16(static_cast<uint16_t>(m_hardwareType));
183 /* ipv4 */
185 i.WriteU8(m_macSource.GetLength());
186 i.WriteU8(4);
190 WriteTo(i, m_macDest);
192}
193
196{
197 NS_LOG_FUNCTION(this << &start);
198 Buffer::Iterator i = start;
199 m_hardwareType = static_cast<HardwareType>(i.ReadNtohU16()); // Read HTYPE
200 uint32_t protocolType = i.ReadNtohU16(); // Read PRO
201 uint32_t hardwareAddressLen = i.ReadU8(); // Read HLN
202 uint32_t protocolAddressLen = i.ReadU8(); // Read PLN
203
204 //
205 // It is implicit here that we have a protocol type of 0x800 (IP).
206 // It is also implicit here that we are using Ipv4 (PLN == 4).
207 // If this isn't the case, we need to return an error since we don't want to
208 // be too fragile if we get connected to real networks.
209 //
210 if (protocolType != 0x800 || protocolAddressLen != 4)
211 {
212 return 0;
213 }
214
215 m_type = static_cast<ArpType_e>(i.ReadNtohU16()); // Read OP
216
217 m_macSource.SetType("MacAddress", hardwareAddressLen);
218 m_macDest.SetType("MacAddress", hardwareAddressLen);
219
220 ReadFrom(i, m_macSource, hardwareAddressLen); // Read SHA (size HLN)
221 ReadFrom(i, m_ipv4Source); // Read SPA (size PLN == 4)
222 ReadFrom(i, m_macDest, hardwareAddressLen); // Read THA (size HLN)
223 ReadFrom(i, m_ipv4Dest); // Read TPA (size PLN == 4)
224 return GetSerializedSize();
225}
226
227std::ostream&
228operator<<(std::ostream& os, ArpHeader::HardwareType hardwareType)
229{
230 switch (hardwareType)
231 {
233 return (os << "Ethernet");
235 return (os << "EUI-64");
237 return (os << "Unknown Hardware Type");
238 }
239 return os << "Unrecognized Hardware Type(" << static_cast<uint16_t>(hardwareType) << ")";
240}
241
242} // namespace ns3
a polymophic address class
Definition address.h:114
The packet header for an ARP packet.
Definition arp-header.h:23
uint32_t Deserialize(Buffer::Iterator start) override
Address m_macSource
hardware source address
Definition arp-header.h:148
HardwareType GetHardwareType() const
Get the hardware type.
Definition arp-header.cc:86
void Print(std::ostream &os) const override
ArpType_e m_type
type of the ICMP packet
Definition arp-header.h:147
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition arp-header.cc:40
bool IsReply() const
Check if the ARP is a reply.
Definition arp-header.cc:79
bool IsRequest() const
Check if the ARP is a request.
Definition arp-header.cc:72
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Ipv4Address GetDestinationIpv4Address() const
Returns the destination IP address.
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition arp-header.cc:24
Address m_macDest
hardware destination address
Definition arp-header.h:149
HardwareType
Enumeration listing the supported hardware types.
Definition arp-header.h:46
void Serialize(Buffer::Iterator start) const override
Ipv4Address m_ipv4Dest
IP destination address.
Definition arp-header.h:151
Ipv4Address GetSourceIpv4Address() const
Returns the source IP address.
HardwareType DetermineHardwareType(const Address &address) const
Determines the hardware type based on the length of the address.
Definition arp-header.cc:56
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static TypeId GetTypeId()
Get the type ID.
HardwareType m_hardwareType
hardware type
Definition arp-header.h:146
Ipv4Address m_ipv4Source
IP source address.
Definition arp-header.h:150
uint32_t GetSerializedSize() const override
Address GetSourceHardwareAddress() const
Returns the source hardware address.
Definition arp-header.cc:93
ArpType_e
Enumeration listing the possible ARP types.
Definition arp-header.h:32
iterator in a Buffer instance
Definition buffer.h:109
void WriteU8(uint8_t data)
Definition buffer.h:944
void WriteHtonU16(uint16_t data)
Definition buffer.h:978
uint16_t ReadNtohU16()
Definition buffer.h:1017
Protocol header serialization and deserialization.
Definition header.h:36
Ipv4 addresses are stored in host order in this class.
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#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:35
constexpr uint16_t IPV4
IPv4 EtherType [RFC894].
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
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.