A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
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
void
30
UdpHeader::EnableChecksums
()
31
{
32
m_calcChecksum
=
true
;
33
}
34
35
void
36
UdpHeader::SetDestinationPort
(uint16_t
port
)
37
{
38
m_destinationPort
=
port
;
39
}
40
41
void
42
UdpHeader::SetSourcePort
(uint16_t
port
)
43
{
44
m_sourcePort
=
port
;
45
}
46
47
uint16_t
48
UdpHeader::GetSourcePort
()
const
49
{
50
return
m_sourcePort
;
51
}
52
53
uint16_t
54
UdpHeader::GetDestinationPort
()
const
55
{
56
return
m_destinationPort
;
57
}
58
59
void
60
UdpHeader::InitializeChecksum
(
Address
source,
Address
destination, uint8_t protocol)
61
{
62
m_source
= source;
63
m_destination
= destination;
64
m_protocol
= protocol;
65
}
66
67
void
68
UdpHeader::InitializeChecksum
(
Ipv4Address
source,
Ipv4Address
destination, uint8_t protocol)
69
{
70
m_source
= source;
71
m_destination
= destination;
72
m_protocol
= protocol;
73
}
74
75
void
76
UdpHeader::InitializeChecksum
(
Ipv6Address
source,
Ipv6Address
destination, uint8_t protocol)
77
{
78
m_source
= source;
79
m_destination
= destination;
80
m_protocol
= protocol;
81
}
82
83
uint16_t
84
UdpHeader::CalculateHeaderChecksum
(uint16_t size)
const
85
{
86
Buffer
buf =
Buffer
((2 *
Address::MAX_SIZE
) + 8);
87
buf.
AddAtStart
((2 *
Address::MAX_SIZE
) + 8);
88
Buffer::Iterator
it = buf.
Begin
();
89
uint32_t hdrSize = 0;
90
91
WriteTo
(it,
m_source
);
92
WriteTo
(it,
m_destination
);
93
if
(
Ipv4Address::IsMatchingType
(
m_source
))
94
{
95
it.
WriteU8
(0);
/* protocol */
96
it.
WriteU8
(
m_protocol
);
/* protocol */
97
it.
WriteU8
(size >> 8);
/* length */
98
it.
WriteU8
(size & 0xff);
/* length */
99
hdrSize = 12;
100
}
101
else
if
(
Ipv6Address::IsMatchingType
(
m_source
))
102
{
103
it.
WriteU16
(0);
104
it.
WriteU8
(size >> 8);
/* length */
105
it.
WriteU8
(size & 0xff);
/* length */
106
it.
WriteU16
(0);
107
it.
WriteU8
(0);
108
it.
WriteU8
(
m_protocol
);
/* protocol */
109
hdrSize = 40;
110
}
111
112
it = buf.
Begin
();
113
/* we don't CompleteChecksum ( ~ ) now */
114
return
~(it.
CalculateIpChecksum
(hdrSize));
115
}
116
117
bool
118
UdpHeader::IsChecksumOk
()
const
119
{
120
return
m_goodChecksum
;
121
}
122
123
void
124
UdpHeader::ForceChecksum
(uint16_t checksum)
125
{
126
m_checksum
= checksum;
127
}
128
129
void
130
UdpHeader::ForcePayloadSize
(uint16_t payloadSize)
131
{
132
m_forcedPayloadSize
= payloadSize;
133
}
134
135
TypeId
136
UdpHeader::GetTypeId
()
137
{
138
static
TypeId
tid =
TypeId
(
"ns3::UdpHeader"
)
139
.
SetParent
<
Header
>()
140
.SetGroupName(
"Internet"
)
141
.AddConstructor<
UdpHeader
>();
142
return
tid;
143
}
144
145
TypeId
146
UdpHeader::GetInstanceTypeId
()
const
147
{
148
return
GetTypeId
();
149
}
150
151
void
152
UdpHeader::Print
(std::ostream& os)
const
153
{
154
os <<
"length: "
<<
m_payloadSize
+
GetSerializedSize
() <<
" "
<<
m_sourcePort
<<
" > "
155
<<
m_destinationPort
;
156
}
157
158
uint32_t
159
UdpHeader::GetSerializedSize
()
const
160
{
161
return
8;
162
}
163
164
void
165
UdpHeader::Serialize
(
Buffer::Iterator
start
)
const
166
{
167
Buffer::Iterator
i =
start
;
168
169
i.
WriteHtonU16
(
m_sourcePort
);
170
i.
WriteHtonU16
(
m_destinationPort
);
171
if
(
m_forcedPayloadSize
== 0)
172
{
173
i.
WriteHtonU16
(
start
.GetSize());
174
}
175
else
176
{
177
i.
WriteHtonU16
(
m_forcedPayloadSize
);
178
}
179
180
if
(
m_checksum
== 0)
181
{
182
i.
WriteU16
(0);
183
184
if
(
m_calcChecksum
)
185
{
186
uint16_t headerChecksum =
CalculateHeaderChecksum
(
start
.GetSize());
187
i =
start
;
188
uint16_t checksum = i.
CalculateIpChecksum
(
start
.GetSize(), headerChecksum);
189
190
i =
start
;
191
i.
Next
(6);
192
i.
WriteU16
(checksum);
193
}
194
}
195
else
196
{
197
i.
WriteU16
(
m_checksum
);
198
}
199
}
200
201
uint32_t
202
UdpHeader::Deserialize
(
Buffer::Iterator
start
)
203
{
204
Buffer::Iterator
i =
start
;
205
m_sourcePort
= i.
ReadNtohU16
();
206
m_destinationPort
= i.
ReadNtohU16
();
207
m_payloadSize
= i.
ReadNtohU16
() -
GetSerializedSize
();
208
m_checksum
= i.
ReadU16
();
209
210
if
(
m_calcChecksum
)
211
{
212
uint16_t headerChecksum =
CalculateHeaderChecksum
(
start
.GetSize());
213
i =
start
;
214
uint16_t checksum = i.
CalculateIpChecksum
(
start
.GetSize(), headerChecksum);
215
216
m_goodChecksum
= (checksum == 0);
217
}
218
219
return
GetSerializedSize
();
220
}
221
222
uint16_t
223
UdpHeader::GetChecksum
()
const
224
{
225
return
m_checksum
;
226
}
227
228
}
// namespace ns3
ns3::Address
a polymophic address class
Definition:
address.h:101
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:107
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:1135
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:859
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::Header::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
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:654
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:931
ns3::UdpHeader
Packet header for UDP packets.
Definition:
udp-header.h:41
ns3::UdpHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition:
udp-header.cc:159
ns3::UdpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition:
udp-header.cc:165
ns3::UdpHeader::m_destination
Address m_destination
Destination IP address.
Definition:
udp-header.h:172
ns3::UdpHeader::CalculateHeaderChecksum
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition:
udp-header.cc:84
ns3::UdpHeader::EnableChecksums
void EnableChecksums()
Enable checksum calculation for UDP.
Definition:
udp-header.cc:30
ns3::UdpHeader::m_protocol
uint8_t m_protocol
Protocol number.
Definition:
udp-header.h:173
ns3::UdpHeader::m_destinationPort
uint16_t m_destinationPort
Destination port.
Definition:
udp-header.h:167
ns3::UdpHeader::GetDestinationPort
uint16_t GetDestinationPort() const
Definition:
udp-header.cc:54
ns3::UdpHeader::m_source
Address m_source
Source IP address.
Definition:
udp-header.h:171
ns3::UdpHeader::m_payloadSize
uint16_t m_payloadSize
Payload size.
Definition:
udp-header.h:168
ns3::UdpHeader::ForceChecksum
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition:
udp-header.cc:124
ns3::UdpHeader::m_sourcePort
uint16_t m_sourcePort
Source port.
Definition:
udp-header.h:166
ns3::UdpHeader::GetSourcePort
uint16_t GetSourcePort() const
Definition:
udp-header.cc:48
ns3::UdpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition:
udp-header.cc:146
ns3::UdpHeader::m_calcChecksum
bool m_calcChecksum
Flag to calculate checksum.
Definition:
udp-header.h:175
ns3::UdpHeader::Print
void Print(std::ostream &os) const override
Definition:
udp-header.cc:152
ns3::UdpHeader::ForcePayloadSize
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition:
udp-header.cc:130
ns3::UdpHeader::IsChecksumOk
bool IsChecksumOk() const
Is the UDP checksum correct ?
Definition:
udp-header.cc:118
ns3::UdpHeader::m_forcedPayloadSize
uint16_t m_forcedPayloadSize
Payload size (forced)
Definition:
udp-header.h:169
ns3::UdpHeader::GetChecksum
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
Definition:
udp-header.cc:223
ns3::UdpHeader::m_checksum
uint16_t m_checksum
Forced Checksum value.
Definition:
udp-header.h:174
ns3::UdpHeader::InitializeChecksum
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition:
udp-header.cc:60
ns3::UdpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition:
udp-header.cc:136
ns3::UdpHeader::SetSourcePort
void SetSourcePort(uint16_t port)
Definition:
udp-header.cc:42
ns3::UdpHeader::m_goodChecksum
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition:
udp-header.h:176
ns3::UdpHeader::SetDestinationPort
void SetDestinationPort(uint16_t port)
Definition:
udp-header.cc:36
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
two-ray-to-three-gpp-ch-calibration.start
start
Definition:
two-ray-to-three-gpp-ch-calibration.py:520
udp-header.h
src
internet
model
udp-header.cc
Generated on Sun Mar 3 2024 17:10:59 for ns-3 by
1.9.1