A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
internet
model
udp-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 "
udp-header.h
"
22
#include "ns3/address-utils.h"
23
24
namespace
ns3
{
25
26
NS_OBJECT_ENSURE_REGISTERED
(UdpHeader);
27
28
/* The magic values below are used only for debugging.
29
* They can be used to easily detect memory corruption
30
* problems so you can see the patterns in memory.
31
*/
32
UdpHeader::UdpHeader
()
33
: m_sourcePort (0xfffd),
34
m_destinationPort (0xfffd),
35
m_payloadSize (0),
36
m_checksum (0),
37
m_calcChecksum (false),
38
m_goodChecksum (true)
39
{
40
}
41
UdpHeader::~UdpHeader
()
42
{
43
m_sourcePort
= 0xfffe;
44
m_destinationPort
= 0xfffe;
45
m_payloadSize
= 0xfffe;
46
}
47
48
void
49
UdpHeader::EnableChecksums
(
void
)
50
{
51
m_calcChecksum
=
true
;
52
}
53
54
void
55
UdpHeader::SetDestinationPort
(uint16_t
port
)
56
{
57
m_destinationPort
=
port
;
58
}
59
void
60
UdpHeader::SetSourcePort
(uint16_t
port
)
61
{
62
m_sourcePort
=
port
;
63
}
64
uint16_t
65
UdpHeader::GetSourcePort
(
void
)
const
66
{
67
return
m_sourcePort
;
68
}
69
uint16_t
70
UdpHeader::GetDestinationPort
(
void
)
const
71
{
72
return
m_destinationPort
;
73
}
74
void
75
UdpHeader::InitializeChecksum
(
Address
source,
76
Address
destination,
77
uint8_t protocol)
78
{
79
m_source
= source;
80
m_destination
= destination;
81
m_protocol
= protocol;
82
}
83
void
84
UdpHeader::InitializeChecksum
(
Ipv4Address
source,
85
Ipv4Address
destination,
86
uint8_t protocol)
87
{
88
m_source
= source;
89
m_destination
= destination;
90
m_protocol
= protocol;
91
}
92
void
93
UdpHeader::InitializeChecksum
(
Ipv6Address
source,
94
Ipv6Address
destination,
95
uint8_t protocol)
96
{
97
m_source
= source;
98
m_destination
= destination;
99
m_protocol
= protocol;
100
}
101
uint16_t
102
UdpHeader::CalculateHeaderChecksum
(uint16_t size)
const
103
{
104
Buffer
buf =
Buffer
((2 *
Address::MAX_SIZE
) + 8);
105
buf.
AddAtStart
((2 *
Address::MAX_SIZE
) + 8);
106
Buffer::Iterator
it = buf.
Begin
();
107
uint32_t hdrSize = 0;
108
109
WriteTo
(it,
m_source
);
110
WriteTo
(it,
m_destination
);
111
if
(
Ipv4Address::IsMatchingType
(
m_source
))
112
{
113
it.
WriteU8
(0);
/* protocol */
114
it.
WriteU8
(
m_protocol
);
/* protocol */
115
it.
WriteU8
(size >> 8);
/* length */
116
it.
WriteU8
(size & 0xff);
/* length */
117
hdrSize = 12;
118
}
119
else
if
(
Ipv6Address::IsMatchingType
(
m_source
))
120
{
121
it.
WriteU16
(0);
122
it.
WriteU8
(size >> 8);
/* length */
123
it.
WriteU8
(size & 0xff);
/* length */
124
it.
WriteU16
(0);
125
it.
WriteU8
(0);
126
it.
WriteU8
(
m_protocol
);
/* protocol */
127
hdrSize = 40;
128
}
129
130
it = buf.
Begin
();
131
/* we don't CompleteChecksum ( ~ ) now */
132
return
~(it.
CalculateIpChecksum
(hdrSize));
133
}
134
135
bool
136
UdpHeader::IsChecksumOk
(
void
)
const
137
{
138
return
m_goodChecksum
;
139
}
140
141
void
142
UdpHeader::ForceChecksum
(uint16_t checksum)
143
{
144
m_checksum
= checksum;
145
}
146
147
void
148
UdpHeader::ForcePayloadSize
(uint16_t payloadSize)
149
{
150
m_payloadSize
= payloadSize;
151
}
152
153
TypeId
154
UdpHeader::GetTypeId
(
void
)
155
{
156
static
TypeId
tid =
TypeId
(
"ns3::UdpHeader"
)
157
.
SetParent
<
Header
> ()
158
.SetGroupName (
"Internet"
)
159
.AddConstructor<
UdpHeader
> ()
160
;
161
return
tid;
162
}
163
TypeId
164
UdpHeader::GetInstanceTypeId
(
void
)
const
165
{
166
return
GetTypeId
();
167
}
168
void
169
UdpHeader::Print
(std::ostream &os)
const
170
{
171
os <<
"length: "
<<
m_payloadSize
+
GetSerializedSize
()
172
<<
" "
173
<<
m_sourcePort
<<
" > "
<<
m_destinationPort
174
;
175
}
176
177
uint32_t
178
UdpHeader::GetSerializedSize
(
void
)
const
179
{
180
return
8;
181
}
182
183
void
184
UdpHeader::Serialize
(
Buffer::Iterator
start
)
const
185
{
186
Buffer::Iterator
i =
start
;
187
188
i.
WriteHtonU16
(
m_sourcePort
);
189
i.
WriteHtonU16
(
m_destinationPort
);
190
if
(
m_payloadSize
== 0)
191
{
192
i.
WriteHtonU16
(
start
.GetSize ());
193
}
194
else
195
{
196
i.
WriteHtonU16
(
m_payloadSize
);
197
}
198
199
if
(
m_checksum
== 0)
200
{
201
i.
WriteU16
(0);
202
203
if
(
m_calcChecksum
)
204
{
205
uint16_t headerChecksum =
CalculateHeaderChecksum
(
start
.GetSize ());
206
i =
start
;
207
uint16_t checksum = i.CalculateIpChecksum (
start
.GetSize (), headerChecksum);
208
209
i =
start
;
210
i.Next (6);
211
i.WriteU16 (checksum);
212
}
213
}
214
else
215
{
216
i.
WriteU16
(
m_checksum
);
217
}
218
}
219
uint32_t
220
UdpHeader::Deserialize
(
Buffer::Iterator
start
)
221
{
222
Buffer::Iterator
i =
start
;
223
m_sourcePort
= i.
ReadNtohU16
();
224
m_destinationPort
= i.
ReadNtohU16
();
225
m_payloadSize
= i.
ReadNtohU16
() -
GetSerializedSize
();
226
m_checksum
= i.
ReadU16
();
227
228
if
(
m_calcChecksum
)
229
{
230
uint16_t headerChecksum =
CalculateHeaderChecksum
(
start
.GetSize ());
231
i =
start
;
232
uint16_t checksum = i.CalculateIpChecksum (
start
.GetSize (), headerChecksum);
233
234
m_goodChecksum
= (checksum == 0);
235
}
236
237
return
GetSerializedSize
();
238
}
239
240
uint16_t
241
UdpHeader::GetChecksum
()
242
{
243
return
m_checksum
;
244
}
245
246
}
// namespace ns3
ns3::Buffer::Iterator::ReadU16
uint16_t ReadU16(void)
Definition:
buffer.h:1029
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:42
ns3::Ipv6Address::IsMatchingType
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Definition:
ipv6-address.cc:757
ns3::Buffer::Iterator::CalculateIpChecksum
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition:
buffer.cc:1133
ns3::UdpHeader::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
udp-header.cc:154
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition:
buffer.cc:309
ns3::UdpHeader::InitializeChecksum
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition:
udp-header.cc:75
ns3::Address::MAX_SIZE
Definition:
address.h:98
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:45
ns3::UdpHeader::m_goodChecksum
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition:
udp-header.h:185
ns3::UdpHeader::m_source
Address m_source
Source IP address.
Definition:
udp-header.h:180
ns3::UdpHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition:
udp-header.cc:164
ns3::Buffer
automatically resized byte buffer
Definition:
buffer.h:92
visualizer.core.start
def start()
Definition:
core.py:1844
ns3::UdpHeader::SetDestinationPort
void SetDestinationPort(uint16_t port)
Definition:
udp-header.cc:55
ns3::UdpHeader::m_checksum
uint16_t m_checksum
Forced Checksum value.
Definition:
udp-header.h:183
ns3::WriteTo
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
Definition:
address-utils.cc:28
ns3::UdpHeader::SetSourcePort
void SetSourcePort(uint16_t port)
Definition:
udp-header.cc:60
port
uint16_t port
Definition:
dsdv-manet.cc:45
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:98
ns3::Address
a polymophic address class
Definition:
address.h:90
udp-header.h
ns3::UdpHeader::ForceChecksum
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition:
udp-header.cc:142
ns3::UdpHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
udp-header.cc:184
ns3::UdpHeader::EnableChecksums
void EnableChecksums(void)
Enable checksum calculation for UDP.
Definition:
udp-header.cc:49
ns3::UdpHeader::GetDestinationPort
uint16_t GetDestinationPort(void) const
Definition:
udp-header.cc:70
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition:
buffer.cc:870
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition:
buffer.h:905
ns3::UdpHeader::ForcePayloadSize
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition:
udp-header.cc:148
ns3::UdpHeader::~UdpHeader
~UdpHeader()
Definition:
udp-header.cc:41
ns3::UdpHeader::Print
virtual void Print(std::ostream &os) const
Definition:
udp-header.cc:169
ns3::UdpHeader::UdpHeader
UdpHeader()
Constructor.
Definition:
udp-header.cc:32
ns3::Ipv4Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition:
ipv4-address.cc:343
ns3::UdpHeader::GetChecksum
uint16_t GetChecksum()
Return the checksum (only known after a Deserialize)
Definition:
udp-header.cc:241
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::UdpHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
udp-header.cc:178
ns3::UdpHeader
Packet header for UDP packets.
Definition:
udp-header.h:39
ns3::UdpHeader::m_payloadSize
uint16_t m_payloadSize
Payload size.
Definition:
udp-header.h:178
ns3::UdpHeader::m_calcChecksum
bool m_calcChecksum
Flag to calculate checksum.
Definition:
udp-header.h:184
ns3::Ipv6Address
Describes an IPv6 address.
Definition:
ipv6-address.h:49
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition:
ipv4-address.h:40
ns3::UdpHeader::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition:
udp-header.cc:220
ns3::UdpHeader::m_sourcePort
uint16_t m_sourcePort
Source port.
Definition:
udp-header.h:176
ns3::UdpHeader::m_destination
Address m_destination
Destination IP address.
Definition:
udp-header.h:181
ns3::UdpHeader::CalculateHeaderChecksum
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition:
udp-header.cc:102
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition:
buffer.h:869
ns3::UdpHeader::m_protocol
uint8_t m_protocol
Protocol number.
Definition:
udp-header.h:182
ns3::UdpHeader::GetSourcePort
uint16_t GetSourcePort(void) const
Definition:
udp-header.cc:65
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16(void)
Definition:
buffer.h:946
ns3::UdpHeader::m_destinationPort
uint16_t m_destinationPort
Destination port.
Definition:
udp-header.h:177
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:58
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:915
ns3::Buffer::Begin
Buffer::Iterator Begin(void) const
Definition:
buffer.h:1069
ns3::UdpHeader::IsChecksumOk
bool IsChecksumOk(void) const
Is the UDP checksum correct ?
Definition:
udp-header.cc:136
Generated on Wed Nov 7 2018 10:01:57 for ns-3 by
1.8.14