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/log.h"
14
15namespace ns3
16{
17
18NS_LOG_COMPONENT_DEFINE("ArpHeader");
19
21
22void
23ArpHeader::SetRequest(Address sourceHardwareAddress,
24 Ipv4Address sourceProtocolAddress,
25 Address destinationHardwareAddress,
26 Ipv4Address destinationProtocolAddress)
27{
28 NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
29 << destinationHardwareAddress << destinationProtocolAddress);
30 m_hardwareType = DetermineHardwareType(sourceHardwareAddress);
32 m_macSource = sourceHardwareAddress;
33 m_macDest = destinationHardwareAddress;
34 m_ipv4Source = sourceProtocolAddress;
35 m_ipv4Dest = destinationProtocolAddress;
36}
37
38void
39ArpHeader::SetReply(Address sourceHardwareAddress,
40 Ipv4Address sourceProtocolAddress,
41 Address destinationHardwareAddress,
42 Ipv4Address destinationProtocolAddress)
43{
44 NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
45 << destinationHardwareAddress << destinationProtocolAddress);
46 m_hardwareType = DetermineHardwareType(sourceHardwareAddress);
48 m_macSource = sourceHardwareAddress;
49 m_macDest = destinationHardwareAddress;
50 m_ipv4Source = sourceProtocolAddress;
51 m_ipv4Dest = destinationProtocolAddress;
52}
53
56{
57 NS_LOG_FUNCTION(this << address);
58 uint8_t addressLength = address.GetLength();
59 switch (addressLength)
60 {
61 case 6:
63 case 8:
65 default:
67 }
68}
69
70bool
72{
73 NS_LOG_FUNCTION(this);
74 return m_type == ARP_TYPE_REQUEST;
75}
76
77bool
79{
80 NS_LOG_FUNCTION(this);
81 return m_type == ARP_TYPE_REPLY;
82}
83
86{
87 NS_LOG_FUNCTION(this);
88 return m_hardwareType;
89}
90
97
104
107{
108 NS_LOG_FUNCTION(this);
109 return m_ipv4Source;
110}
111
114{
115 NS_LOG_FUNCTION(this);
116 return m_ipv4Dest;
117}
118
119TypeId
121{
122 static TypeId tid = TypeId("ns3::ArpHeader")
123 .SetParent<Header>()
124 .SetGroupName("Internet")
125 .AddConstructor<ArpHeader>();
126 return tid;
127}
128
129TypeId
131{
132 NS_LOG_FUNCTION(this);
133 return GetTypeId();
134}
135
136void
137ArpHeader::Print(std::ostream& os) const
138{
139 NS_LOG_FUNCTION(this << &os);
140 if (IsRequest())
141 {
142 os << "hardware type: " << GetHardwareType() << " "
143 << "request "
144 << "source mac: " << m_macSource << " "
145 << "source ipv4: " << m_ipv4Source << " "
146 << "dest ipv4: " << m_ipv4Dest;
147 }
148 else
149 {
151 os << "hardware type: " << GetHardwareType() << " "
152 << "reply "
153 << "source mac: " << m_macSource << " "
154 << "source ipv4: " << m_ipv4Source << " "
155 << "dest mac: " << m_macDest << " "
156 << "dest ipv4: " << m_ipv4Dest;
157 }
158}
159
162{
163 NS_LOG_FUNCTION(this);
164 NS_ASSERT((m_macSource.GetLength() == 6) || (m_macSource.GetLength() == 8) ||
165 (m_macSource.GetLength() == 1));
167
168 uint32_t length = 16; // Length minus two hardware addresses
169 length += m_macSource.GetLength() * 2;
170
171 return length;
172}
173
174void
176{
177 NS_LOG_FUNCTION(this << &start);
178 Buffer::Iterator i = start;
180
181 i.WriteHtonU16(static_cast<uint16_t>(m_hardwareType));
182 /* ipv4 */
183 i.WriteHtonU16(0x0800);
185 i.WriteU8(4);
189 WriteTo(i, m_macDest);
191}
192
195{
196 NS_LOG_FUNCTION(this << &start);
197 Buffer::Iterator i = start;
198 m_hardwareType = static_cast<HardwareType>(i.ReadNtohU16()); // Read HTYPE
199 uint32_t protocolType = i.ReadNtohU16(); // Read PRO
200 uint32_t hardwareAddressLen = i.ReadU8(); // Read HLN
201 uint32_t protocolAddressLen = i.ReadU8(); // Read PLN
202
203 //
204 // It is implicit here that we have a protocol type of 0x800 (IP).
205 // It is also implicit here that we are using Ipv4 (PLN == 4).
206 // If this isn't the case, we need to return an error since we don't want to
207 // be too fragile if we get connected to real networks.
208 //
209 if (protocolType != 0x800 || protocolAddressLen != 4)
210 {
211 return 0;
212 }
213
214 m_type = static_cast<ArpType_e>(i.ReadNtohU16()); // Read OP
215 ReadFrom(i, m_macSource, hardwareAddressLen); // Read SHA (size HLN)
216 ReadFrom(i, m_ipv4Source); // Read SPA (size PLN == 4)
217 ReadFrom(i, m_macDest, hardwareAddressLen); // Read THA (size HLN)
218 ReadFrom(i, m_ipv4Dest); // Read TPA (size PLN == 4)
219 return GetSerializedSize();
220}
221
222std::ostream&
223operator<<(std::ostream& os, ArpHeader::HardwareType hardwareType)
224{
225 switch (hardwareType)
226 {
228 return (os << "Ethernet");
230 return (os << "EUI-64");
232 return (os << "Unknown Hardware Type");
233 }
234 return os << "Unrecognized Hardware Type(" << static_cast<uint16_t>(hardwareType) << ")";
235}
236
237} // namespace ns3
a polymophic address class
Definition address.h:90
uint8_t GetLength() const
Get the length of the underlying address.
Definition address.cc:67
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:85
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:39
bool IsReply() const
Check if the ARP is a reply.
Definition arp-header.cc:78
bool IsRequest() const
Check if the ARP is a request.
Definition arp-header.cc:71
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Definition arp-header.cc:99
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:23
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:55
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:92
ArpType_e
Enumeration listing the possible ARP types.
Definition arp-header.h:32
iterator in a Buffer instance
Definition buffer.h:89
void WriteU8(uint8_t data)
Definition buffer.h:870
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint16_t ReadNtohU16()
Definition buffer.h:943
Protocol header serialization and deserialization.
Definition header.h:33
Ipv4 addresses are stored in host order in this class.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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:191
#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
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.