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
void
33
ArpHeader::SetRequest
(
Address
sourceHardwareAddress,
34
Ipv4Address
sourceProtocolAddress,
35
Address
destinationHardwareAddress,
36
Ipv4Address
destinationProtocolAddress)
37
{
38
NS_LOG_FUNCTION
(
this
<< sourceHardwareAddress << sourceProtocolAddress << destinationHardwareAddress << destinationProtocolAddress);
39
m_type
=
ARP_TYPE_REQUEST
;
40
m_macSource
= sourceHardwareAddress;
41
m_macDest
= destinationHardwareAddress;
42
m_ipv4Source
= sourceProtocolAddress;
43
m_ipv4Dest
= destinationProtocolAddress;
44
}
45
void
46
ArpHeader::SetReply
(
Address
sourceHardwareAddress,
47
Ipv4Address
sourceProtocolAddress,
48
Address
destinationHardwareAddress,
49
Ipv4Address
destinationProtocolAddress)
50
{
51
NS_LOG_FUNCTION
(
this
<< sourceHardwareAddress << sourceProtocolAddress << destinationHardwareAddress << destinationProtocolAddress);
52
m_type
=
ARP_TYPE_REPLY
;
53
m_macSource
= sourceHardwareAddress;
54
m_macDest
= destinationHardwareAddress;
55
m_ipv4Source
= sourceProtocolAddress;
56
m_ipv4Dest
= destinationProtocolAddress;
57
}
58
bool
59
ArpHeader::IsRequest
(
void
)
const
60
{
61
NS_LOG_FUNCTION
(
this
);
62
return
(
m_type
==
ARP_TYPE_REQUEST
) ?
true
:
false
;
63
}
64
bool
65
ArpHeader::IsReply
(
void
)
const
66
{
67
NS_LOG_FUNCTION
(
this
);
68
return
(
m_type
==
ARP_TYPE_REPLY
) ?
true
:
false
;
69
}
70
Address
71
ArpHeader::GetSourceHardwareAddress
(
void
)
72
{
73
NS_LOG_FUNCTION
(
this
);
74
return
m_macSource
;
75
}
76
Address
77
ArpHeader::GetDestinationHardwareAddress
(
void
)
78
{
79
NS_LOG_FUNCTION
(
this
);
80
return
m_macDest
;
81
}
82
Ipv4Address
83
ArpHeader::GetSourceIpv4Address
(
void
)
84
{
85
NS_LOG_FUNCTION
(
this
);
86
return
m_ipv4Source
;
87
}
88
Ipv4Address
89
ArpHeader::GetDestinationIpv4Address
(
void
)
90
{
91
NS_LOG_FUNCTION
(
this
);
92
return
m_ipv4Dest
;
93
}
94
95
96
TypeId
97
ArpHeader::GetTypeId
(
void
)
98
{
99
static
TypeId
tid =
TypeId
(
"ns3::ArpHeader"
)
100
.
SetParent
<
Header
> ()
101
.AddConstructor<ArpHeader> ()
102
;
103
return
tid;
104
}
105
TypeId
106
ArpHeader::GetInstanceTypeId
(
void
)
const
107
{
108
NS_LOG_FUNCTION
(
this
);
109
return
GetTypeId
();
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
}
123
else
124
{
125
NS_ASSERT
(
IsReply
());
126
os <<
"reply "
127
<<
"source mac: "
<<
m_macSource
<<
" "
128
<<
"source ipv4: "
<<
m_ipv4Source
<<
" "
129
<<
"dest mac: "
<<
m_macDest
<<
" "
130
<<
"dest ipv4: "
<<
m_ipv4Dest
131
;
132
}
133
}
134
uint32_t
135
ArpHeader::GetSerializedSize
(
void
)
const
136
{
137
NS_LOG_FUNCTION
(
this
);
138
NS_ASSERT
((
m_macSource
.
GetLength
() == 6) || (
m_macSource
.
GetLength
() == 8));
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
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:42
ns3::ArpHeader::SetReply
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Definition:
arp-header.cc:46
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:311
ns3::ArpHeader::GetTypeId
static TypeId GetTypeId(void)
Definition:
arp-header.cc:97
ns3::ArpHeader::m_macSource
Address m_macSource
Definition:
arp-header.h:64
ns3::ArpHeader::ARP_TYPE_REQUEST
Definition:
arp-header.h:60
ns3::ArpHeader::ARP_TYPE_REPLY
Definition:
arp-header.h:61
ns3::ReadFrom
void ReadFrom(Buffer::Iterator &i, Ipv4Address &ad)
Definition:
address-utils.cc:70
ns3::ArpHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
arp-header.cc:148
NS_ASSERT
#define NS_ASSERT(condition)
Definition:
assert.h:64
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Definition:
address-utils.cc:28
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Definition:
log.h:122
ns3::ArpHeader::GetSourceIpv4Address
Ipv4Address GetSourceIpv4Address(void)
Definition:
arp-header.cc:83
ns3::ArpHeader::SetRequest
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Definition:
arp-header.cc:33
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
Definition:
arp-header.h:66
ns3::ArpHeader::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition:
arp-header.cc:168
ns3::NS_OBJECT_ENSURE_REGISTERED
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
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)
Definition:
buffer.h:666
ns3::ArpHeader::m_macDest
Address m_macDest
Definition:
arp-header.h:65
ns3::ArpHeader::m_ipv4Dest
Ipv4Address m_ipv4Dest
Definition:
arp-header.h:67
ns3::ArpHeader::GetDestinationHardwareAddress
Address GetDestinationHardwareAddress(void)
Definition:
arp-header.cc:77
ns3::ArpHeader::GetDestinationIpv4Address
Ipv4Address GetDestinationIpv4Address(void)
Definition:
arp-header.cc:89
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
Definition:
arp-header.h:63
ns3::ArpHeader::GetSourceHardwareAddress
Address GetSourceHardwareAddress(void)
Definition:
arp-header.cc:71
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8(void)
Definition:
buffer.h:819
ns3::ArpHeader::IsReply
bool IsReply(void) const
Definition:
arp-header.cc:65
ns3::ArpHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition:
arp-header.cc:106
ns3::ArpHeader::Print
virtual void Print(std::ostream &os) const
Definition:
arp-header.cc:112
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:610
ns3::ArpHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
arp-header.cc:135
ns3::ArpHeader::IsRequest
bool IsRequest(void) const
Definition:
arp-header.cc:59
src
internet
model
arp-header.cc
Generated on Sat Nov 16 2013 12:55:27 for ns-3 by
1.8.5