A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
arp-header.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2005 INRIA
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
21
#include "ns3/assert.h"
22
#include "ns3/address-utils.h"
23
#include "
arp-header.h
"
24
#include "ns3/log.h"
25
26
NS_LOG_COMPONENT_DEFINE
(
"ArpHeader"
);
27
28
namespace
ns3 {
29
30
NS_OBJECT_ENSURE_REGISTERED
(ArpHeader)
31
;
32
33
void
34
ArpHeader::SetRequest
(
Address
sourceHardwareAddress,
35
Ipv4Address
sourceProtocolAddress,
36
Address
destinationHardwareAddress,
37
Ipv4Address
destinationProtocolAddress)
38
{
39
NS_LOG_FUNCTION
(
this
<< sourceHardwareAddress << sourceProtocolAddress << destinationHardwareAddress << destinationProtocolAddress);
40
m_type
=
ARP_TYPE_REQUEST
;
41
m_macSource
= sourceHardwareAddress;
42
m_macDest
= destinationHardwareAddress;
43
m_ipv4Source
= sourceProtocolAddress;
44
m_ipv4Dest
= destinationProtocolAddress;
45
}
46
void
47
ArpHeader::SetReply
(
Address
sourceHardwareAddress,
48
Ipv4Address
sourceProtocolAddress,
49
Address
destinationHardwareAddress,
50
Ipv4Address
destinationProtocolAddress)
51
{
52
NS_LOG_FUNCTION
(
this
<< sourceHardwareAddress << sourceProtocolAddress << destinationHardwareAddress << destinationProtocolAddress);
53
m_type
=
ARP_TYPE_REPLY
;
54
m_macSource
= sourceHardwareAddress;
55
m_macDest
= destinationHardwareAddress;
56
m_ipv4Source
= sourceProtocolAddress;
57
m_ipv4Dest
= destinationProtocolAddress;
58
}
59
bool
60
ArpHeader::IsRequest
(
void
)
const
61
{
62
NS_LOG_FUNCTION
(
this
);
63
return
(
m_type
==
ARP_TYPE_REQUEST
) ?
true
:
false
;
64
}
65
bool
66
ArpHeader::IsReply
(
void
)
const
67
{
68
NS_LOG_FUNCTION
(
this
);
69
return
(
m_type
==
ARP_TYPE_REPLY
) ?
true
:
false
;
70
}
71
Address
72
ArpHeader::GetSourceHardwareAddress
(
void
)
73
{
74
NS_LOG_FUNCTION
(
this
);
75
return
m_macSource
;
76
}
77
Address
78
ArpHeader::GetDestinationHardwareAddress
(
void
)
79
{
80
NS_LOG_FUNCTION
(
this
);
81
return
m_macDest
;
82
}
83
Ipv4Address
84
ArpHeader::GetSourceIpv4Address
(
void
)
85
{
86
NS_LOG_FUNCTION
(
this
);
87
return
m_ipv4Source
;
88
}
89
Ipv4Address
90
ArpHeader::GetDestinationIpv4Address
(
void
)
91
{
92
NS_LOG_FUNCTION
(
this
);
93
return
m_ipv4Dest
;
94
}
95
96
97
TypeId
98
ArpHeader::GetTypeId
(
void
)
99
{
100
static
TypeId
tid =
TypeId
(
"ns3::ArpHeader"
)
101
.
SetParent
<
Header
> ()
102
.AddConstructor<ArpHeader> ()
103
;
104
return
tid;
105
}
106
TypeId
107
ArpHeader::GetInstanceTypeId
(
void
)
const
108
{
109
NS_LOG_FUNCTION
(
this
);
110
return
GetTypeId
();
111
}
112
void
113
ArpHeader::Print
(std::ostream &os)
const
114
{
115
NS_LOG_FUNCTION
(
this
<< &os);
116
if
(
IsRequest
())
117
{
118
os <<
"request "
119
<<
"source mac: "
<<
m_macSource
<<
" "
120
<<
"source ipv4: "
<<
m_ipv4Source
<<
" "
121
<<
"dest ipv4: "
<<
m_ipv4Dest
122
;
123
}
124
else
125
{
126
NS_ASSERT
(
IsReply
());
127
os <<
"reply "
128
<<
"source mac: "
<<
m_macSource
<<
" "
129
<<
"source ipv4: "
<<
m_ipv4Source
<<
" "
130
<<
"dest mac: "
<<
m_macDest
<<
" "
131
<<
"dest ipv4: "
<<
m_ipv4Dest
132
;
133
}
134
}
135
uint32_t
136
ArpHeader::GetSerializedSize
(
void
)
const
137
{
138
NS_LOG_FUNCTION
(
this
);
139
NS_ASSERT
((
m_macSource
.
GetLength
() == 6) || (
m_macSource
.
GetLength
() == 8));
140
NS_ASSERT
(
m_macSource
.
GetLength
() ==
m_macDest
.
GetLength
());
141
142
uint32_t length = 16;
// Length minus two hardware addresses
143
length +=
m_macSource
.
GetLength
() * 2;
144
145
return
length;
146
}
147
148
void
149
ArpHeader::Serialize
(
Buffer::Iterator
start
)
const
150
{
151
NS_LOG_FUNCTION
(
this
<< &start);
152
Buffer::Iterator
i =
start
;
153
NS_ASSERT
(
m_macSource
.
GetLength
() ==
m_macDest
.
GetLength
());
154
155
/* ethernet */
156
i.
WriteHtonU16
(0x0001);
157
/* ipv4 */
158
i.
WriteHtonU16
(0x0800);
159
i.
WriteU8
(
m_macSource
.
GetLength
());
160
i.
WriteU8
(4);
161
i.
WriteHtonU16
(
m_type
);
162
WriteTo
(i,
m_macSource
);
163
WriteTo
(i,
m_ipv4Source
);
164
WriteTo
(i,
m_macDest
);
165
WriteTo
(i,
m_ipv4Dest
);
166
}
167
168
uint32_t
169
ArpHeader::Deserialize
(
Buffer::Iterator
start
)
170
{
171
NS_LOG_FUNCTION
(
this
<< &start);
172
Buffer::Iterator
i =
start
;
173
i.
Next
(2);
// Skip HRD
174
uint32_t protocolType = i.
ReadNtohU16
();
// Read PRO
175
uint32_t hardwareAddressLen = i.
ReadU8
();
// Read HLN
176
uint32_t protocolAddressLen = i.
ReadU8
();
// Read PLN
177
178
//
179
// It is implicit here that we have a protocol type of 0x800 (IP).
180
// It is also implicit here that we are using Ipv4 (PLN == 4).
181
// If this isn't the case, we need to return an error since we don't want to
182
// be too fragile if we get connected to real networks.
183
//
184
if
(protocolType != 0x800 || protocolAddressLen != 4)
185
{
186
return
0;
187
}
188
189
m_type
= i.
ReadNtohU16
();
// Read OP
190
ReadFrom
(i,
m_macSource
, hardwareAddressLen);
// Read SHA (size HLN)
191
ReadFrom
(i,
m_ipv4Source
);
// Read SPA (size PLN == 4)
192
ReadFrom
(i,
m_macDest
, hardwareAddressLen);
// Read THA (size HLN)
193
ReadFrom
(i,
m_ipv4Dest
);
// Read TPA (size PLN == 4)
194
return
GetSerializedSize
();
195
}
196
197
}
// namespace ns3
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:42
ns3::ArpHeader::SetReply
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition:
arp-header.cc:47
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:345
ns3::ArpHeader::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
arp-header.cc:98
ns3::ArpHeader::m_macSource
Address m_macSource
hardware source address
Definition:
arp-header.h:115
ns3::ArpHeader::ARP_TYPE_REQUEST
Definition:
arp-header.h:111
ns3::ArpHeader::ARP_TYPE_REPLY
Definition:
arp-header.h:112
ns3::ReadFrom
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Read an Ipv4Address from a Buffer.
Definition:
address-utils.cc:70
ns3::ArpHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
arp-header.cc:149
NS_ASSERT
#define NS_ASSERT(condition)
Definition:
assert.h:64
ns3::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Definition:
address-utils.cc:28
ns3::ArpHeader::GetSourceIpv4Address
Ipv4Address GetSourceIpv4Address(void)
Returns the source IP address.
Definition:
arp-header.cc:84
ns3::ArpHeader::SetRequest
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition:
arp-header.cc:34
visualizer.core.start
def start
Definition:
core.py:1482
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:98
ns3::Address
a polymophic address class
Definition:
address.h:86
ns3::ArpHeader::m_ipv4Source
Ipv4Address m_ipv4Source
IP source address.
Definition:
arp-header.h:117
ns3::ArpHeader::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition:
arp-header.cc:169
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition:
buffer.h:726
ns3::Address::GetLength
uint8_t GetLength(void) const
Definition:
address.cc:75
ns3::Buffer::Iterator::Next
void Next(void)
go forward by one byte
Definition:
buffer.h:666
NS_LOG_COMPONENT_DEFINE
NS_LOG_COMPONENT_DEFINE("ArpHeader")
ns3::ArpHeader::m_macDest
Address m_macDest
hardware destination address
Definition:
arp-header.h:116
ns3::ArpHeader::m_ipv4Dest
Ipv4Address m_ipv4Dest
IP destination address.
Definition:
arp-header.h:118
ns3::ArpHeader::GetDestinationHardwareAddress
Address GetDestinationHardwareAddress(void)
Returns the destination hardware address.
Definition:
arp-header.cc:78
ns3::ArpHeader::GetDestinationIpv4Address
Ipv4Address GetDestinationIpv4Address(void)
Returns the destination IP address.
Definition:
arp-header.cc:90
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition:
ipv4-address.h:38
arp-header.h
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition:
buffer.h:690
ns3::ArpHeader::m_type
uint16_t m_type
type of the ICMP (ARP_TYPE_REQUEST)
Definition:
arp-header.h:114
ns3::ArpHeader::GetSourceHardwareAddress
Address GetSourceHardwareAddress(void)
Returns the source hardware address.
Definition:
arp-header.cc:72
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8(void)
Definition:
buffer.h:819
ns3::ArpHeader::IsReply
bool IsReply(void) const
Check if the ARP is a reply.
Definition:
arp-header.cc:66
ns3::ArpHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition:
arp-header.cc:107
ns3::ArpHeader::Print
virtual void Print(std::ostream &os) const
Definition:
arp-header.cc:113
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16(void)
Definition:
buffer.h:767
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:611
ns3::ArpHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
arp-header.cc:136
ns3::ArpHeader::IsRequest
bool IsRequest(void) const
Check if the ARP is a request.
Definition:
arp-header.cc:60
src
internet
model
arp-header.cc
Generated on Sat Apr 19 2014 14:06:55 for ns-3 by
1.8.6