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);
31 m_macSource = sourceHardwareAddress;
32 m_macDest = destinationHardwareAddress;
33 m_ipv4Source = sourceProtocolAddress;
34 m_ipv4Dest = destinationProtocolAddress;
35}
36
37void
38ArpHeader::SetReply(Address sourceHardwareAddress,
39 Ipv4Address sourceProtocolAddress,
40 Address destinationHardwareAddress,
41 Ipv4Address destinationProtocolAddress)
42{
43 NS_LOG_FUNCTION(this << sourceHardwareAddress << sourceProtocolAddress
44 << destinationHardwareAddress << destinationProtocolAddress);
46 m_macSource = sourceHardwareAddress;
47 m_macDest = destinationHardwareAddress;
48 m_ipv4Source = sourceProtocolAddress;
49 m_ipv4Dest = destinationProtocolAddress;
50}
51
52bool
54{
55 NS_LOG_FUNCTION(this);
56 return m_type == ARP_TYPE_REQUEST;
57}
58
59bool
61{
62 NS_LOG_FUNCTION(this);
63 return m_type == ARP_TYPE_REPLY;
64}
65
72
79
86
93
96{
97 static TypeId tid = TypeId("ns3::ArpHeader")
99 .SetGroupName("Internet")
100 .AddConstructor<ArpHeader>();
101 return tid;
102}
103
104TypeId
106{
107 NS_LOG_FUNCTION(this);
108 return GetTypeId();
109}
110
111void
112ArpHeader::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 {
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
135{
136 NS_LOG_FUNCTION(this);
137 NS_ASSERT((m_macSource.GetLength() == 6) || (m_macSource.GetLength() == 8) ||
138 (m_macSource.GetLength() == 1));
140
141 uint32_t length = 16; // Length minus two hardware addresses
142 length += m_macSource.GetLength() * 2;
143
144 return length;
145}
146
147void
149{
150 NS_LOG_FUNCTION(this << &start);
151 Buffer::Iterator i = start;
153
154 /* ethernet */
155 i.WriteHtonU16(0x0001);
156 /* ipv4 */
157 i.WriteHtonU16(0x0800);
159 i.WriteU8(4);
163 WriteTo(i, m_macDest);
165}
166
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
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:25
uint32_t Deserialize(Buffer::Iterator start) override
Address m_macSource
hardware source address
Definition arp-header.h:109
void Print(std::ostream &os) const override
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition arp-header.cc:38
bool IsReply() const
Check if the ARP is a reply.
Definition arp-header.cc:60
bool IsRequest() const
Check if the ARP is a request.
Definition arp-header.cc:53
Address GetDestinationHardwareAddress() const
Returns the destination hardware address.
Definition arp-header.cc:74
uint16_t m_type
type of the ICMP (ARP_TYPE_REQUEST)
Definition arp-header.h:108
Ipv4Address GetDestinationIpv4Address() const
Returns the destination IP address.
Definition arp-header.cc:88
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:110
void Serialize(Buffer::Iterator start) const override
Ipv4Address m_ipv4Dest
IP destination address.
Definition arp-header.h:112
Ipv4Address GetSourceIpv4Address() const
Returns the source IP address.
Definition arp-header.cc:81
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static TypeId GetTypeId()
Get the type ID.
Definition arp-header.cc:95
Ipv4Address m_ipv4Source
IP source address.
Definition arp-header.h:111
uint32_t GetSerializedSize() const override
Address GetSourceHardwareAddress() const
Returns the source hardware address.
Definition arp-header.cc:67
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
void Next()
go forward by one byte
Definition buffer.h:842
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.
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.