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/log.h"
14
15
namespace
ns3
16
{
17
18
NS_LOG_COMPONENT_DEFINE
(
"ArpHeader"
);
19
20
NS_OBJECT_ENSURE_REGISTERED
(ArpHeader);
21
22
void
23
ArpHeader::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);
31
m_type
=
ARP_TYPE_REQUEST
;
32
m_macSource
= sourceHardwareAddress;
33
m_macDest
= destinationHardwareAddress;
34
m_ipv4Source
= sourceProtocolAddress;
35
m_ipv4Dest
= destinationProtocolAddress;
36
}
37
38
void
39
ArpHeader::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);
47
m_type
=
ARP_TYPE_REPLY
;
48
m_macSource
= sourceHardwareAddress;
49
m_macDest
= destinationHardwareAddress;
50
m_ipv4Source
= sourceProtocolAddress;
51
m_ipv4Dest
= destinationProtocolAddress;
52
}
53
54
ArpHeader::HardwareType
55
ArpHeader::DetermineHardwareType
(
const
Address
& address)
const
56
{
57
NS_LOG_FUNCTION
(
this
<< address);
58
uint8_t addressLength = address.GetLength();
59
switch
(addressLength)
60
{
61
case
6:
62
return
HardwareType::ETHERNET
;
63
case
8:
64
return
HardwareType::EUI_64
;
65
default
:
66
return
HardwareType::UNKNOWN
;
67
}
68
}
69
70
bool
71
ArpHeader::IsRequest
()
const
72
{
73
NS_LOG_FUNCTION
(
this
);
74
return
m_type
==
ARP_TYPE_REQUEST
;
75
}
76
77
bool
78
ArpHeader::IsReply
()
const
79
{
80
NS_LOG_FUNCTION
(
this
);
81
return
m_type
==
ARP_TYPE_REPLY
;
82
}
83
84
ArpHeader::HardwareType
85
ArpHeader::GetHardwareType
()
const
86
{
87
NS_LOG_FUNCTION
(
this
);
88
return
m_hardwareType
;
89
}
90
91
Address
92
ArpHeader::GetSourceHardwareAddress
()
const
93
{
94
NS_LOG_FUNCTION
(
this
);
95
return
m_macSource
;
96
}
97
98
Address
99
ArpHeader::GetDestinationHardwareAddress
()
const
100
{
101
NS_LOG_FUNCTION
(
this
);
102
return
m_macDest
;
103
}
104
105
Ipv4Address
106
ArpHeader::GetSourceIpv4Address
()
const
107
{
108
NS_LOG_FUNCTION
(
this
);
109
return
m_ipv4Source
;
110
}
111
112
Ipv4Address
113
ArpHeader::GetDestinationIpv4Address
()
const
114
{
115
NS_LOG_FUNCTION
(
this
);
116
return
m_ipv4Dest
;
117
}
118
119
TypeId
120
ArpHeader::GetTypeId
()
121
{
122
static
TypeId
tid =
TypeId
(
"ns3::ArpHeader"
)
123
.
SetParent
<
Header
>()
124
.SetGroupName(
"Internet"
)
125
.AddConstructor<
ArpHeader
>();
126
return
tid;
127
}
128
129
TypeId
130
ArpHeader::GetInstanceTypeId
()
const
131
{
132
NS_LOG_FUNCTION
(
this
);
133
return
GetTypeId
();
134
}
135
136
void
137
ArpHeader::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
{
150
NS_ASSERT
(
IsReply
());
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
160
uint32_t
161
ArpHeader::GetSerializedSize
()
const
162
{
163
NS_LOG_FUNCTION
(
this
);
164
NS_ASSERT
((
m_macSource
.
GetLength
() == 6) || (
m_macSource
.
GetLength
() == 8) ||
165
(
m_macSource
.
GetLength
() == 1));
166
NS_ASSERT
(
m_macSource
.
GetLength
() ==
m_macDest
.
GetLength
());
167
168
uint32_t
length = 16;
// Length minus two hardware addresses
169
length +=
m_macSource
.
GetLength
() * 2;
170
171
return
length;
172
}
173
174
void
175
ArpHeader::Serialize
(
Buffer::Iterator
start)
const
176
{
177
NS_LOG_FUNCTION
(
this
<< &start);
178
Buffer::Iterator
i = start;
179
NS_ASSERT
(
m_macSource
.
GetLength
() ==
m_macDest
.
GetLength
());
180
181
i.
WriteHtonU16
(
static_cast<
uint16_t
>
(
m_hardwareType
));
182
/* ipv4 */
183
i.
WriteHtonU16
(0x0800);
184
i.
WriteU8
(
m_macSource
.
GetLength
());
185
i.
WriteU8
(4);
186
i.
WriteHtonU16
(
m_type
);
187
WriteTo
(i,
m_macSource
);
188
WriteTo
(i,
m_ipv4Source
);
189
WriteTo
(i,
m_macDest
);
190
WriteTo
(i,
m_ipv4Dest
);
191
}
192
193
uint32_t
194
ArpHeader::Deserialize
(
Buffer::Iterator
start)
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
222
std::ostream&
223
operator<<
(std::ostream& os,
ArpHeader::HardwareType
hardwareType)
224
{
225
switch
(hardwareType)
226
{
227
case
ArpHeader::HardwareType::ETHERNET
:
228
return
(os <<
"Ethernet"
);
229
case
ArpHeader::HardwareType::EUI_64
:
230
return
(os <<
"EUI-64"
);
231
case
ArpHeader::HardwareType::UNKNOWN
:
232
return
(os <<
"Unknown Hardware Type"
);
233
}
234
return
os <<
"Unrecognized Hardware Type("
<<
static_cast<
uint16_t
>
(hardwareType) <<
")"
;
235
}
236
237
}
// namespace ns3
arp-header.h
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::Address::GetLength
uint8_t GetLength() const
Get the length of the underlying address.
Definition
address.cc:67
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:194
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:85
ns3::ArpHeader::Print
void Print(std::ostream &os) const override
Definition
arp-header.cc:137
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:39
ns3::ArpHeader::IsReply
bool IsReply() const
Check if the ARP is a reply.
Definition
arp-header.cc:78
ns3::ArpHeader::IsRequest
bool IsRequest() const
Check if the ARP is a request.
Definition
arp-header.cc:71
ns3::ArpHeader::GetDestinationHardwareAddress
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Definition
arp-header.cc:99
ns3::ArpHeader::GetDestinationIpv4Address
Ipv4Address GetDestinationIpv4Address() const
Returns the destination IP address.
Definition
arp-header.cc:113
ns3::ArpHeader::SetRequest
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition
arp-header.cc:23
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
ns3::ArpHeader::HardwareType::UNKNOWN
@ UNKNOWN
ns3::ArpHeader::HardwareType::ETHERNET
@ ETHERNET
ns3::ArpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
arp-header.cc:175
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:106
ns3::ArpHeader::DetermineHardwareType
HardwareType DetermineHardwareType(const Address &address) const
Determines the hardware type based on the length of the address.
Definition
arp-header.cc:55
ns3::ArpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
arp-header.cc:130
ns3::ArpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
arp-header.cc:120
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:161
ns3::ArpHeader::GetSourceHardwareAddress
Address GetSourceHardwareAddress() const
Returns the source hardware address.
Definition
arp-header.cc:92
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:89
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1016
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition
buffer.h:870
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition
buffer.h:904
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition
buffer.h:943
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
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:191
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:229
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
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 Tue Jan 21 2025 09:20:43 for ns-3 by
1.11.0