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_type
=
ARP_TYPE_REQUEST
;
31
m_macSource
= sourceHardwareAddress;
32
m_macDest
= destinationHardwareAddress;
33
m_ipv4Source
= sourceProtocolAddress;
34
m_ipv4Dest
= destinationProtocolAddress;
35
}
36
37
void
38
ArpHeader::SetReply
(
Address
sourceHardwareAddress,
39
Ipv4Address
sourceProtocolAddress,
40
Address
destinationHardwareAddress,
41
Ipv4Address
destinationProtocolAddress)
42
{
43
NS_LOG_FUNCTION
(
this
<< sourceHardwareAddress << sourceProtocolAddress
44
<< destinationHardwareAddress << destinationProtocolAddress);
45
m_type
=
ARP_TYPE_REPLY
;
46
m_macSource
= sourceHardwareAddress;
47
m_macDest
= destinationHardwareAddress;
48
m_ipv4Source
= sourceProtocolAddress;
49
m_ipv4Dest
= destinationProtocolAddress;
50
}
51
52
bool
53
ArpHeader::IsRequest
()
const
54
{
55
NS_LOG_FUNCTION
(
this
);
56
return
m_type
==
ARP_TYPE_REQUEST
;
57
}
58
59
bool
60
ArpHeader::IsReply
()
const
61
{
62
NS_LOG_FUNCTION
(
this
);
63
return
m_type
==
ARP_TYPE_REPLY
;
64
}
65
66
Address
67
ArpHeader::GetSourceHardwareAddress
()
const
68
{
69
NS_LOG_FUNCTION
(
this
);
70
return
m_macSource
;
71
}
72
73
Address
74
ArpHeader::GetDestinationHardwareAddress
()
const
75
{
76
NS_LOG_FUNCTION
(
this
);
77
return
m_macDest
;
78
}
79
80
Ipv4Address
81
ArpHeader::GetSourceIpv4Address
()
const
82
{
83
NS_LOG_FUNCTION
(
this
);
84
return
m_ipv4Source
;
85
}
86
87
Ipv4Address
88
ArpHeader::GetDestinationIpv4Address
()
const
89
{
90
NS_LOG_FUNCTION
(
this
);
91
return
m_ipv4Dest
;
92
}
93
94
TypeId
95
ArpHeader::GetTypeId
()
96
{
97
static
TypeId
tid =
TypeId
(
"ns3::ArpHeader"
)
98
.
SetParent
<
Header
>()
99
.SetGroupName(
"Internet"
)
100
.AddConstructor<
ArpHeader
>();
101
return
tid;
102
}
103
104
TypeId
105
ArpHeader::GetInstanceTypeId
()
const
106
{
107
NS_LOG_FUNCTION
(
this
);
108
return
GetTypeId
();
109
}
110
111
void
112
ArpHeader::Print
(std::ostream& os)
const
113
{
114
NS_LOG_FUNCTION
(
this
<< &os);
115
if
(
IsRequest
())
116
{
117
os <<
"request "
118
<<
"source mac: "
<<
m_macSource
<<
" "
119
<<
"source ipv4: "
<<
m_ipv4Source
<<
" "
120
<<
"dest ipv4: "
<<
m_ipv4Dest
;
121
}
122
else
123
{
124
NS_ASSERT
(
IsReply
());
125
os <<
"reply "
126
<<
"source mac: "
<<
m_macSource
<<
" "
127
<<
"source ipv4: "
<<
m_ipv4Source
<<
" "
128
<<
"dest mac: "
<<
m_macDest
<<
" "
129
<<
"dest ipv4: "
<<
m_ipv4Dest
;
130
}
131
}
132
133
uint32_t
134
ArpHeader::GetSerializedSize
()
const
135
{
136
NS_LOG_FUNCTION
(
this
);
137
NS_ASSERT
((
m_macSource
.
GetLength
() == 6) || (
m_macSource
.
GetLength
() == 8) ||
138
(
m_macSource
.
GetLength
() == 1));
139
NS_ASSERT
(
m_macSource
.
GetLength
() ==
m_macDest
.
GetLength
());
140
141
uint32_t
length = 16;
// Length minus two hardware addresses
142
length +=
m_macSource
.
GetLength
() * 2;
143
144
return
length;
145
}
146
147
void
148
ArpHeader::Serialize
(
Buffer::Iterator
start)
const
149
{
150
NS_LOG_FUNCTION
(
this
<< &start);
151
Buffer::Iterator
i = start;
152
NS_ASSERT
(
m_macSource
.
GetLength
() ==
m_macDest
.
GetLength
());
153
154
/* ethernet */
155
i.
WriteHtonU16
(0x0001);
156
/* ipv4 */
157
i.
WriteHtonU16
(0x0800);
158
i.
WriteU8
(
m_macSource
.
GetLength
());
159
i.
WriteU8
(4);
160
i.
WriteHtonU16
(
m_type
);
161
WriteTo
(i,
m_macSource
);
162
WriteTo
(i,
m_ipv4Source
);
163
WriteTo
(i,
m_macDest
);
164
WriteTo
(i,
m_ipv4Dest
);
165
}
166
167
uint32_t
168
ArpHeader::Deserialize
(
Buffer::Iterator
start)
169
{
170
NS_LOG_FUNCTION
(
this
<< &start);
171
Buffer::Iterator
i = start;
172
i.
Next
(2);
// Skip HRD
173
uint32_t
protocolType = i.
ReadNtohU16
();
// Read PRO
174
uint32_t
hardwareAddressLen = i.
ReadU8
();
// Read HLN
175
uint32_t
protocolAddressLen = i.
ReadU8
();
// Read PLN
176
177
//
178
// It is implicit here that we have a protocol type of 0x800 (IP).
179
// It is also implicit here that we are using Ipv4 (PLN == 4).
180
// If this isn't the case, we need to return an error since we don't want to
181
// be too fragile if we get connected to real networks.
182
//
183
if
(protocolType != 0x800 || protocolAddressLen != 4)
184
{
185
return
0;
186
}
187
188
m_type
= i.
ReadNtohU16
();
// Read OP
189
ReadFrom
(i,
m_macSource
, hardwareAddressLen);
// Read SHA (size HLN)
190
ReadFrom
(i,
m_ipv4Source
);
// Read SPA (size PLN == 4)
191
ReadFrom
(i,
m_macDest
, hardwareAddressLen);
// Read THA (size HLN)
192
ReadFrom
(i,
m_ipv4Dest
);
// Read TPA (size PLN == 4)
193
return
GetSerializedSize
();
194
}
195
196
}
// 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:25
ns3::ArpHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
arp-header.cc:168
ns3::ArpHeader::m_macSource
Address m_macSource
hardware source address
Definition
arp-header.h:109
ns3::ArpHeader::Print
void Print(std::ostream &os) const override
Definition
arp-header.cc:112
ns3::ArpHeader::SetReply
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition
arp-header.cc:38
ns3::ArpHeader::IsReply
bool IsReply() const
Check if the ARP is a reply.
Definition
arp-header.cc:60
ns3::ArpHeader::IsRequest
bool IsRequest() const
Check if the ARP is a request.
Definition
arp-header.cc:53
ns3::ArpHeader::GetDestinationHardwareAddress
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Definition
arp-header.cc:74
ns3::ArpHeader::m_type
uint16_t m_type
type of the ICMP (ARP_TYPE_REQUEST)
Definition
arp-header.h:108
ns3::ArpHeader::ARP_TYPE_REQUEST
@ ARP_TYPE_REQUEST
Definition
arp-header.h:104
ns3::ArpHeader::ARP_TYPE_REPLY
@ ARP_TYPE_REPLY
Definition
arp-header.h:105
ns3::ArpHeader::GetDestinationIpv4Address
Ipv4Address GetDestinationIpv4Address() const
Returns the destination IP address.
Definition
arp-header.cc:88
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:110
ns3::ArpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
arp-header.cc:148
ns3::ArpHeader::m_ipv4Dest
Ipv4Address m_ipv4Dest
IP destination address.
Definition
arp-header.h:112
ns3::ArpHeader::GetSourceIpv4Address
Ipv4Address GetSourceIpv4Address() const
Returns the source IP address.
Definition
arp-header.cc:81
ns3::ArpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
arp-header.cc:105
ns3::ArpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
arp-header.cc:95
ns3::ArpHeader::m_ipv4Source
Ipv4Address m_ipv4Source
IP source address.
Definition
arp-header.h:111
ns3::ArpHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
arp-header.cc:134
ns3::ArpHeader::GetSourceHardwareAddress
Address GetSourceHardwareAddress() const
Returns the source hardware address.
Definition
arp-header.cc:67
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::Buffer::Iterator::Next
void Next()
go forward by one byte
Definition
buffer.h:842
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::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 Fri Nov 8 2024 13:59:00 for ns-3 by
1.11.0