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
udp-header.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2005 INRIA
3
*
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation;
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*
17
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
*/
19
20
#include "
udp-header.h
"
21
22
#include "ns3/address-utils.h"
23
24
namespace
ns3
25
{
26
27
NS_OBJECT_ENSURE_REGISTERED
(UdpHeader);
28
29
/* The magic values below are used only for debugging.
30
* They can be used to easily detect memory corruption
31
* problems so you can see the patterns in memory.
32
*/
33
UdpHeader::UdpHeader
()
34
: m_sourcePort(0xfffd),
35
m_destinationPort(0xfffd),
36
m_payloadSize(0),
37
m_checksum(0),
38
m_calcChecksum(false),
39
m_goodChecksum(true)
40
{
41
}
42
43
UdpHeader::~UdpHeader
()
44
{
45
m_sourcePort
= 0xfffe;
46
m_destinationPort
= 0xfffe;
47
m_payloadSize
= 0xfffe;
48
}
49
50
void
51
UdpHeader::EnableChecksums
()
52
{
53
m_calcChecksum
=
true
;
54
}
55
56
void
57
UdpHeader::SetDestinationPort
(uint16_t
port
)
58
{
59
m_destinationPort
=
port
;
60
}
61
62
void
63
UdpHeader::SetSourcePort
(uint16_t
port
)
64
{
65
m_sourcePort
=
port
;
66
}
67
68
uint16_t
69
UdpHeader::GetSourcePort
()
const
70
{
71
return
m_sourcePort
;
72
}
73
74
uint16_t
75
UdpHeader::GetDestinationPort
()
const
76
{
77
return
m_destinationPort
;
78
}
79
80
void
81
UdpHeader::InitializeChecksum
(
Address
source,
Address
destination, uint8_t protocol)
82
{
83
m_source
= source;
84
m_destination
= destination;
85
m_protocol
= protocol;
86
}
87
88
void
89
UdpHeader::InitializeChecksum
(
Ipv4Address
source,
Ipv4Address
destination, uint8_t protocol)
90
{
91
m_source
= source;
92
m_destination
= destination;
93
m_protocol
= protocol;
94
}
95
96
void
97
UdpHeader::InitializeChecksum
(
Ipv6Address
source,
Ipv6Address
destination, uint8_t protocol)
98
{
99
m_source
= source;
100
m_destination
= destination;
101
m_protocol
= protocol;
102
}
103
104
uint16_t
105
UdpHeader::CalculateHeaderChecksum
(uint16_t size)
const
106
{
107
Buffer
buf =
Buffer
((2 *
Address::MAX_SIZE
) + 8);
108
buf.
AddAtStart
((2 *
Address::MAX_SIZE
) + 8);
109
Buffer::Iterator
it = buf.
Begin
();
110
uint32_t
hdrSize = 0;
111
112
WriteTo
(it,
m_source
);
113
WriteTo
(it,
m_destination
);
114
if
(
Ipv4Address::IsMatchingType
(
m_source
))
115
{
116
it.
WriteU8
(0);
/* protocol */
117
it.
WriteU8
(
m_protocol
);
/* protocol */
118
it.
WriteU8
(size >> 8);
/* length */
119
it.
WriteU8
(size & 0xff);
/* length */
120
hdrSize = 12;
121
}
122
else
if
(
Ipv6Address::IsMatchingType
(
m_source
))
123
{
124
it.
WriteU16
(0);
125
it.
WriteU8
(size >> 8);
/* length */
126
it.
WriteU8
(size & 0xff);
/* length */
127
it.
WriteU16
(0);
128
it.
WriteU8
(0);
129
it.
WriteU8
(
m_protocol
);
/* protocol */
130
hdrSize = 40;
131
}
132
133
it = buf.
Begin
();
134
/* we don't CompleteChecksum ( ~ ) now */
135
return
~(it.
CalculateIpChecksum
(hdrSize));
136
}
137
138
bool
139
UdpHeader::IsChecksumOk
()
const
140
{
141
return
m_goodChecksum
;
142
}
143
144
void
145
UdpHeader::ForceChecksum
(uint16_t checksum)
146
{
147
m_checksum
= checksum;
148
}
149
150
void
151
UdpHeader::ForcePayloadSize
(uint16_t payloadSize)
152
{
153
m_payloadSize
= payloadSize;
154
}
155
156
TypeId
157
UdpHeader::GetTypeId
()
158
{
159
static
TypeId
tid =
TypeId
(
"ns3::UdpHeader"
)
160
.
SetParent
<
Header
>()
161
.SetGroupName(
"Internet"
)
162
.AddConstructor<
UdpHeader
>();
163
return
tid;
164
}
165
166
TypeId
167
UdpHeader::GetInstanceTypeId
()
const
168
{
169
return
GetTypeId
();
170
}
171
172
void
173
UdpHeader::Print
(std::ostream& os)
const
174
{
175
os <<
"length: "
<<
m_payloadSize
+
GetSerializedSize
() <<
" "
<<
m_sourcePort
<<
" > "
176
<<
m_destinationPort
;
177
}
178
179
uint32_t
180
UdpHeader::GetSerializedSize
()
const
181
{
182
return
8;
183
}
184
185
void
186
UdpHeader::Serialize
(
Buffer::Iterator
start)
const
187
{
188
Buffer::Iterator
i = start;
189
190
i.
WriteHtonU16
(
m_sourcePort
);
191
i.
WriteHtonU16
(
m_destinationPort
);
192
if
(
m_payloadSize
== 0)
193
{
194
i.
WriteHtonU16
(start.GetSize());
195
}
196
else
197
{
198
i.
WriteHtonU16
(
m_payloadSize
);
199
}
200
201
if
(
m_checksum
== 0)
202
{
203
i.
WriteU16
(0);
204
205
if
(
m_calcChecksum
)
206
{
207
uint16_t headerChecksum =
CalculateHeaderChecksum
(start.GetSize());
208
i = start;
209
uint16_t checksum = i.
CalculateIpChecksum
(start.GetSize(), headerChecksum);
210
211
i = start;
212
i.
Next
(6);
213
i.
WriteU16
(checksum);
214
}
215
}
216
else
217
{
218
i.
WriteU16
(
m_checksum
);
219
}
220
}
221
222
uint32_t
223
UdpHeader::Deserialize
(
Buffer::Iterator
start)
224
{
225
Buffer::Iterator
i = start;
226
m_sourcePort
= i.
ReadNtohU16
();
227
m_destinationPort
= i.
ReadNtohU16
();
228
m_payloadSize
= i.
ReadNtohU16
() -
GetSerializedSize
();
229
m_checksum
= i.
ReadU16
();
230
231
if
(
m_calcChecksum
)
232
{
233
uint16_t headerChecksum =
CalculateHeaderChecksum
(start.GetSize());
234
i = start;
235
uint16_t checksum = i.
CalculateIpChecksum
(start.GetSize(), headerChecksum);
236
237
m_goodChecksum
= (checksum == 0);
238
}
239
240
return
GetSerializedSize
();
241
}
242
243
uint16_t
244
UdpHeader::GetChecksum
()
const
245
{
246
return
m_checksum
;
247
}
248
249
}
// namespace ns3
ns3::Address
a polymophic address class
Definition:
address.h:100
ns3::Address::MAX_SIZE
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
Definition:
address.h:106
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:100
ns3::Buffer::Iterator::CalculateIpChecksum
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition:
buffer.cc:1141
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition:
buffer.h:881
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition:
buffer.cc:865
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition:
buffer.h:915
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition:
buffer.h:954
ns3::Buffer::Iterator::ReadU16
uint16_t ReadU16()
Definition:
buffer.h:1035
ns3::Buffer::Iterator::Next
void Next()
go forward by one byte
Definition:
buffer.h:853
ns3::Buffer
automatically resized byte buffer
Definition:
buffer.h:94
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition:
buffer.cc:314
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition:
buffer.h:1074
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:44
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition:
ipv4-address.h:42
ns3::Ipv4Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition:
ipv4-address.cc:331
ns3::Ipv6Address
Describes an IPv6 address.
Definition:
ipv6-address.h:49
ns3::Ipv6Address::IsMatchingType
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Definition:
ipv6-address.cc:658
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:936
ns3::UdpHeader
Packet header for UDP packets.
Definition:
udp-header.h:41
ns3::UdpHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition:
udp-header.cc:180
ns3::UdpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition:
udp-header.cc:186
ns3::UdpHeader::~UdpHeader
~UdpHeader() override
Definition:
udp-header.cc:43
ns3::UdpHeader::m_destination
Address m_destination
Destination IP address.
Definition:
udp-header.h:175
ns3::UdpHeader::CalculateHeaderChecksum
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition:
udp-header.cc:105
ns3::UdpHeader::EnableChecksums
void EnableChecksums()
Enable checksum calculation for UDP.
Definition:
udp-header.cc:51
ns3::UdpHeader::m_protocol
uint8_t m_protocol
Protocol number.
Definition:
udp-header.h:176
ns3::UdpHeader::UdpHeader
UdpHeader()
Constructor.
Definition:
udp-header.cc:33
ns3::UdpHeader::m_destinationPort
uint16_t m_destinationPort
Destination port.
Definition:
udp-header.h:171
ns3::UdpHeader::GetDestinationPort
uint16_t GetDestinationPort() const
Definition:
udp-header.cc:75
ns3::UdpHeader::m_source
Address m_source
Source IP address.
Definition:
udp-header.h:174
ns3::UdpHeader::m_payloadSize
uint16_t m_payloadSize
Payload size.
Definition:
udp-header.h:172
ns3::UdpHeader::ForceChecksum
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition:
udp-header.cc:145
ns3::UdpHeader::m_sourcePort
uint16_t m_sourcePort
Source port.
Definition:
udp-header.h:170
ns3::UdpHeader::GetSourcePort
uint16_t GetSourcePort() const
Definition:
udp-header.cc:69
ns3::UdpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition:
udp-header.cc:167
ns3::UdpHeader::m_calcChecksum
bool m_calcChecksum
Flag to calculate checksum.
Definition:
udp-header.h:178
ns3::UdpHeader::Print
void Print(std::ostream &os) const override
Definition:
udp-header.cc:173
ns3::UdpHeader::ForcePayloadSize
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition:
udp-header.cc:151
ns3::UdpHeader::IsChecksumOk
bool IsChecksumOk() const
Is the UDP checksum correct ?
Definition:
udp-header.cc:139
ns3::UdpHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition:
udp-header.cc:223
ns3::UdpHeader::GetChecksum
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
Definition:
udp-header.cc:244
ns3::UdpHeader::m_checksum
uint16_t m_checksum
Forced Checksum value.
Definition:
udp-header.h:177
ns3::UdpHeader::InitializeChecksum
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition:
udp-header.cc:81
ns3::UdpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition:
udp-header.cc:157
ns3::UdpHeader::SetSourcePort
void SetSourcePort(uint16_t port)
Definition:
udp-header.cc:63
ns3::UdpHeader::m_goodChecksum
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition:
udp-header.h:179
ns3::UdpHeader::SetDestinationPort
void SetDestinationPort(uint16_t port)
Definition:
udp-header.cc:57
uint32_t
port
uint16_t port
Definition:
dsdv-manet.cc:44
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:46
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:31
udp-header.h
src
internet
model
udp-header.cc
Generated on Sun Jul 2 2023 18:21:42 for ns-3 by
1.9.6