A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
16
namespace
ns3
17
{
18
19
NS_LOG_COMPONENT_DEFINE
(
"ArpHeader"
);
20
21
NS_OBJECT_ENSURE_REGISTERED
(
ArpHeader
);
22
23
void
24
ArpHeader::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);
32
m_type
=
ARP_TYPE_REQUEST
;
33
m_macSource
= sourceHardwareAddress;
34
m_macDest
= destinationHardwareAddress;
35
m_ipv4Source
= sourceProtocolAddress;
36
m_ipv4Dest
= destinationProtocolAddress;
37
}
38
39
void
40
ArpHeader::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);
48
m_type
=
ARP_TYPE_REPLY
;
49
m_macSource
= sourceHardwareAddress;
50
m_macDest
= destinationHardwareAddress;
51
m_ipv4Source
= sourceProtocolAddress;
52
m_ipv4Dest
= destinationProtocolAddress;
53
}
54
55
ArpHeader::HardwareType
56
ArpHeader::DetermineHardwareType
(
const
Address
& address)
const
57
{
58
NS_LOG_FUNCTION
(
this
<< address);
59
uint8_t addressLength = address.GetLength();
60
switch
(addressLength)
61
{
62
case
6:
63
return
HardwareType::ETHERNET
;
64
case
8:
65
return
HardwareType::EUI_64
;
66
default
:
67
return
HardwareType::UNKNOWN
;
68
}
69
}
70
71
bool
72
ArpHeader::IsRequest
()
const
73
{
74
NS_LOG_FUNCTION
(
this
);
75
return
m_type
==
ARP_TYPE_REQUEST
;
76
}
77
78
bool
79
ArpHeader::IsReply
()
const
80
{
81
NS_LOG_FUNCTION
(
this
);
82
return
m_type
==
ARP_TYPE_REPLY
;
83
}
84
85
ArpHeader::HardwareType
86
ArpHeader::GetHardwareType
()
const
87
{
88
NS_LOG_FUNCTION
(
this
);
89
return
m_hardwareType
;
90
}
91
92
Address
93
ArpHeader::GetSourceHardwareAddress
()
const
94
{
95
NS_LOG_FUNCTION
(
this
);
96
return
m_macSource
;
97
}
98
99
Address
100
ArpHeader::GetDestinationHardwareAddress
()
const
101
{
102
NS_LOG_FUNCTION
(
this
);
103
return
m_macDest
;
104
}
105
106
Ipv4Address
107
ArpHeader::GetSourceIpv4Address
()
const
108
{
109
NS_LOG_FUNCTION
(
this
);
110
return
m_ipv4Source
;
111
}
112
113
Ipv4Address
114
ArpHeader::GetDestinationIpv4Address
()
const
115
{
116
NS_LOG_FUNCTION
(
this
);
117
return
m_ipv4Dest
;
118
}
119
120
TypeId
121
ArpHeader::GetTypeId
()
122
{
123
static
TypeId
tid =
TypeId
(
"ns3::ArpHeader"
)
124
.
SetParent
<
Header
>()
125
.SetGroupName(
"Internet"
)
126
.AddConstructor<
ArpHeader
>();
127
return
tid;
128
}
129
130
TypeId
131
ArpHeader::GetInstanceTypeId
()
const
132
{
133
NS_LOG_FUNCTION
(
this
);
134
return
GetTypeId
();
135
}
136
137
void
138
ArpHeader::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
{
151
NS_ASSERT
(
IsReply
());
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
161
uint32_t
162
ArpHeader::GetSerializedSize
()
const
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
175
void
176
ArpHeader::Serialize
(
Buffer::Iterator
start)
const
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 */
184
i.
WriteHtonU16
(
iana::ieee802numbers::IPV4
);
185
i.
WriteU8
(
m_macSource
.GetLength());
186
i.
WriteU8
(4);
187
i.
WriteHtonU16
(
m_type
);
188
WriteTo
(i,
m_macSource
);
189
WriteTo
(i,
m_ipv4Source
);
190
WriteTo
(i,
m_macDest
);
191
WriteTo
(i,
m_ipv4Dest
);
192
}
193
194
uint32_t
195
ArpHeader::Deserialize
(
Buffer::Iterator
start)
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
227
std::ostream&
228
operator<<
(std::ostream& os,
ArpHeader::HardwareType
hardwareType)
229
{
230
switch
(hardwareType)
231
{
232
case
ArpHeader::HardwareType::ETHERNET
:
233
return
(os <<
"Ethernet"
);
234
case
ArpHeader::HardwareType::EUI_64
:
235
return
(os <<
"EUI-64"
);
236
case
ArpHeader::HardwareType::UNKNOWN
:
237
return
(os <<
"Unknown Hardware Type"
);
238
}
239
return
os <<
"Unrecognized Hardware Type("
<<
static_cast<
uint16_t
>
(hardwareType) <<
")"
;
240
}
241
242
}
// namespace ns3
arp-header.h
ns3::Address
a polymophic address class
Definition
address.h:114
ns3::ArpHeader
The packet header for an ARP packet.
Definition
arp-header.h:23
ns3::ArpHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
arp-header.cc:195
ns3::ArpHeader::m_macSource
Address m_macSource
hardware source address
Definition
arp-header.h:148
ns3::ArpHeader::GetHardwareType
HardwareType GetHardwareType() const
Get the hardware type.
Definition
arp-header.cc:86
ns3::ArpHeader::Print
void Print(std::ostream &os) const override
Definition
arp-header.cc:138
ns3::ArpHeader::m_type
ArpType_e m_type
type of the ICMP packet
Definition
arp-header.h:147
ns3::ArpHeader::SetReply
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition
arp-header.cc:40
ns3::ArpHeader::IsReply
bool IsReply() const
Check if the ARP is a reply.
Definition
arp-header.cc:79
ns3::ArpHeader::IsRequest
bool IsRequest() const
Check if the ARP is a request.
Definition
arp-header.cc:72
ns3::ArpHeader::GetDestinationHardwareAddress
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Definition
arp-header.cc:100
ns3::ArpHeader::GetDestinationIpv4Address
Ipv4Address GetDestinationIpv4Address() const
Returns the destination IP address.
Definition
arp-header.cc:114
ns3::ArpHeader::SetRequest
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition
arp-header.cc:24
ns3::ArpHeader::m_macDest
Address m_macDest
hardware destination address
Definition
arp-header.h:149
ns3::ArpHeader::HardwareType
HardwareType
Enumeration listing the supported hardware types.
Definition
arp-header.h:46
ns3::ArpHeader::HardwareType::EUI_64
@ EUI_64
Definition
arp-header.h:49
ns3::ArpHeader::HardwareType::UNKNOWN
@ UNKNOWN
Definition
arp-header.h:47
ns3::ArpHeader::HardwareType::ETHERNET
@ ETHERNET
Definition
arp-header.h:48
ns3::ArpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
arp-header.cc:176
ns3::ArpHeader::m_ipv4Dest
Ipv4Address m_ipv4Dest
IP destination address.
Definition
arp-header.h:151
ns3::ArpHeader::GetSourceIpv4Address
Ipv4Address GetSourceIpv4Address() const
Returns the source IP address.
Definition
arp-header.cc:107
ns3::ArpHeader::DetermineHardwareType
HardwareType DetermineHardwareType(const Address &address) const
Determines the hardware type based on the length of the address.
Definition
arp-header.cc:56
ns3::ArpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
arp-header.cc:131
ns3::ArpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
arp-header.cc:121
ns3::ArpHeader::m_hardwareType
HardwareType m_hardwareType
hardware type
Definition
arp-header.h:146
ns3::ArpHeader::m_ipv4Source
Ipv4Address m_ipv4Source
IP source address.
Definition
arp-header.h:150
ns3::ArpHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
arp-header.cc:162
ns3::ArpHeader::GetSourceHardwareAddress
Address GetSourceHardwareAddress() const
Returns the source hardware address.
Definition
arp-header.cc:93
ns3::ArpHeader::ArpType_e
ArpType_e
Enumeration listing the possible ARP types.
Definition
arp-header.h:32
ns3::ArpHeader::ARP_TYPE_REQUEST
@ ARP_TYPE_REQUEST
Definition
arp-header.h:33
ns3::ArpHeader::ARP_TYPE_REPLY
@ ARP_TYPE_REPLY
Definition
arp-header.h:34
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:109
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1090
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition
buffer.h:944
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition
buffer.h:978
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition
buffer.h:1017
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:36
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:33
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:50
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1003
uint32_t
NS_ASSERT
#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
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:201
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition
log-macros-enabled.h:214
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ns3::iana::ieee802numbers::IPV4
constexpr uint16_t IPV4
IPv4 EtherType [RFC894].
Definition
iana-ieee802-numbers.h:32
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition
angles.cc:148
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Definition
address-utils.cc:21
ns3::ReadFrom
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
Definition
address-utils.cc:74
src
internet
model
arp-header.cc
Generated on
for ns-3 by
1.16.1