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
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
*/
8
9
#include "
udp-header.h
"
10
11
#include "ns3/address-utils.h"
12
13
namespace
ns3
14
{
15
16
NS_OBJECT_ENSURE_REGISTERED
(UdpHeader);
17
18
void
19
UdpHeader::EnableChecksums
()
20
{
21
m_calcChecksum
=
true
;
22
}
23
24
void
25
UdpHeader::SetDestinationPort
(uint16_t
port
)
26
{
27
m_destinationPort
=
port
;
28
}
29
30
void
31
UdpHeader::SetSourcePort
(uint16_t
port
)
32
{
33
m_sourcePort
=
port
;
34
}
35
36
uint16_t
37
UdpHeader::GetSourcePort
()
const
38
{
39
return
m_sourcePort
;
40
}
41
42
uint16_t
43
UdpHeader::GetDestinationPort
()
const
44
{
45
return
m_destinationPort
;
46
}
47
48
void
49
UdpHeader::InitializeChecksum
(
Address
source,
Address
destination, uint8_t protocol)
50
{
51
m_source
= source;
52
m_destination
= destination;
53
m_protocol
= protocol;
54
}
55
56
void
57
UdpHeader::InitializeChecksum
(
Ipv4Address
source,
Ipv4Address
destination, uint8_t protocol)
58
{
59
m_source
= source;
60
m_destination
= destination;
61
m_protocol
= protocol;
62
}
63
64
void
65
UdpHeader::InitializeChecksum
(
Ipv6Address
source,
Ipv6Address
destination, uint8_t protocol)
66
{
67
m_source
= source;
68
m_destination
= destination;
69
m_protocol
= protocol;
70
}
71
72
uint16_t
73
UdpHeader::CalculateHeaderChecksum
(uint16_t size)
const
74
{
75
Buffer
buf =
Buffer
((2 *
Address::MAX_SIZE
) + 8);
76
buf.
AddAtStart
((2 *
Address::MAX_SIZE
) + 8);
77
Buffer::Iterator
it = buf.
Begin
();
78
uint32_t
hdrSize = 0;
79
80
WriteTo
(it,
m_source
);
81
WriteTo
(it,
m_destination
);
82
if
(
Ipv4Address::IsMatchingType
(
m_source
))
83
{
84
it.
WriteU8
(0);
/* protocol */
85
it.
WriteU8
(
m_protocol
);
/* protocol */
86
it.
WriteU8
(size >> 8);
/* length */
87
it.
WriteU8
(size & 0xff);
/* length */
88
hdrSize = 12;
89
}
90
else
if
(
Ipv6Address::IsMatchingType
(
m_source
))
91
{
92
it.
WriteU16
(0);
93
it.
WriteU8
(size >> 8);
/* length */
94
it.
WriteU8
(size & 0xff);
/* length */
95
it.
WriteU16
(0);
96
it.
WriteU8
(0);
97
it.
WriteU8
(
m_protocol
);
/* protocol */
98
hdrSize = 40;
99
}
100
101
it = buf.
Begin
();
102
/* we don't CompleteChecksum ( ~ ) now */
103
return
~(it.
CalculateIpChecksum
(hdrSize));
104
}
105
106
bool
107
UdpHeader::IsChecksumOk
()
const
108
{
109
return
m_goodChecksum
;
110
}
111
112
void
113
UdpHeader::ForceChecksum
(uint16_t checksum)
114
{
115
m_checksum
= checksum;
116
}
117
118
void
119
UdpHeader::ForcePayloadSize
(uint16_t payloadSize)
120
{
121
m_forcedPayloadSize
= payloadSize;
122
}
123
124
TypeId
125
UdpHeader::GetTypeId
()
126
{
127
static
TypeId
tid =
TypeId
(
"ns3::UdpHeader"
)
128
.
SetParent
<
Header
>()
129
.SetGroupName(
"Internet"
)
130
.AddConstructor<
UdpHeader
>();
131
return
tid;
132
}
133
134
TypeId
135
UdpHeader::GetInstanceTypeId
()
const
136
{
137
return
GetTypeId
();
138
}
139
140
void
141
UdpHeader::Print
(std::ostream& os)
const
142
{
143
os <<
"length: "
<<
m_payloadSize
+
GetSerializedSize
() <<
" "
<<
m_sourcePort
<<
" > "
144
<<
m_destinationPort
;
145
}
146
147
uint32_t
148
UdpHeader::GetSerializedSize
()
const
149
{
150
return
8;
151
}
152
153
void
154
UdpHeader::Serialize
(
Buffer::Iterator
start)
const
155
{
156
Buffer::Iterator
i = start;
157
158
i.
WriteHtonU16
(
m_sourcePort
);
159
i.
WriteHtonU16
(
m_destinationPort
);
160
if
(
m_forcedPayloadSize
== 0)
161
{
162
i.
WriteHtonU16
(start.GetSize());
163
}
164
else
165
{
166
i.
WriteHtonU16
(
m_forcedPayloadSize
);
167
}
168
169
if
(
m_checksum
== 0)
170
{
171
i.
WriteU16
(0);
172
173
if
(
m_calcChecksum
)
174
{
175
uint16_t headerChecksum =
CalculateHeaderChecksum
(start.GetSize());
176
i = start;
177
uint16_t checksum = i.
CalculateIpChecksum
(start.GetSize(), headerChecksum);
178
179
i = start;
180
i.
Next
(6);
181
182
// RFC 768: If the computed checksum is zero, it is transmitted as all ones
183
if
(checksum == 0)
184
{
185
checksum = 0xffff;
186
}
187
i.
WriteU16
(checksum);
188
}
189
}
190
else
191
{
192
i.
WriteU16
(
m_checksum
);
193
}
194
}
195
196
uint32_t
197
UdpHeader::Deserialize
(
Buffer::Iterator
start)
198
{
199
Buffer::Iterator
i = start;
200
m_sourcePort
= i.
ReadNtohU16
();
201
m_destinationPort
= i.
ReadNtohU16
();
202
m_payloadSize
= i.
ReadNtohU16
() -
GetSerializedSize
();
203
m_checksum
= i.
ReadU16
();
204
205
// RFC 768: An all zero transmitted checksum value means that the
206
// transmitter generated no checksum (for debugging or for higher
207
// level protocols that don't care).
208
//
209
// This is common in IPv4, while IPv6 requires UDP to use its checksum.
210
//
211
// As strange as it might sound, flipping from 0x0000 to 0xffff does not
212
// change anything in the verification.
213
//
214
// According to RFC 1141, the following holds:
215
// ~C' = ~(C + (-m) + m') = ~C + (m - m') = ~C + m + ~m'
216
// If ~C (the original CRC) is zero, m (the CRC field) is zero, and m' is 0xffff,
217
// then, according to the formula, we have that ~C' is zero.
218
// I.e., changing the CRC from 0 to 0xffff has no effect on the Rx verification.
219
//
220
// Fun fact: if you take an IPv4 header with an Identification field set to zero
221
// and you change it to 0xffff, the checksum will not change (~_^)
222
if
(
m_calcChecksum
&&
m_checksum
)
223
{
224
uint16_t headerChecksum =
CalculateHeaderChecksum
(start.GetSize());
225
i = start;
226
uint16_t checksum = i.
CalculateIpChecksum
(start.GetSize(), headerChecksum);
227
228
m_goodChecksum
= (checksum == 0);
229
}
230
231
return
GetSerializedSize
();
232
}
233
234
uint16_t
235
UdpHeader::GetChecksum
()
const
236
{
237
return
m_checksum
;
238
}
239
240
}
// namespace ns3
ns3::Address
a polymophic address class
Definition
address.h:90
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:96
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer::Iterator::CalculateIpChecksum
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition
buffer.cc:1124
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition
buffer.h:870
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition
buffer.cc:848
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition
buffer.h:904
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition
buffer.h:943
ns3::Buffer::Iterator::ReadU16
uint16_t ReadU16()
Definition
buffer.h:1024
ns3::Buffer::Iterator::Next
void Next()
go forward by one byte
Definition
buffer.h:842
ns3::Buffer
automatically resized byte buffer
Definition
buffer.h:83
ns3::Buffer::AddAtStart
void AddAtStart(uint32_t start)
Definition
buffer.cc:303
ns3::Buffer::Begin
Buffer::Iterator Begin() const
Definition
buffer.h:1063
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv4Address::IsMatchingType
static bool IsMatchingType(const Address &address)
Definition
ipv4-address.cc:321
ns3::Ipv6Address
Describes an IPv6 address.
Definition
ipv6-address.h:38
ns3::Ipv6Address::IsMatchingType
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Definition
ipv6-address.cc:649
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
ns3::UdpHeader
Packet header for UDP packets.
Definition
udp-header.h:30
ns3::UdpHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
udp-header.cc:148
ns3::UdpHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
udp-header.cc:154
ns3::UdpHeader::m_destination
Address m_destination
Destination IP address.
Definition
udp-header.h:161
ns3::UdpHeader::CalculateHeaderChecksum
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition
udp-header.cc:73
ns3::UdpHeader::EnableChecksums
void EnableChecksums()
Enable checksum calculation for UDP.
Definition
udp-header.cc:19
ns3::UdpHeader::m_protocol
uint8_t m_protocol
Protocol number.
Definition
udp-header.h:162
ns3::UdpHeader::m_destinationPort
uint16_t m_destinationPort
Destination port.
Definition
udp-header.h:156
ns3::UdpHeader::GetDestinationPort
uint16_t GetDestinationPort() const
Definition
udp-header.cc:43
ns3::UdpHeader::m_source
Address m_source
Source IP address.
Definition
udp-header.h:160
ns3::UdpHeader::m_payloadSize
uint16_t m_payloadSize
Payload size.
Definition
udp-header.h:157
ns3::UdpHeader::ForceChecksum
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition
udp-header.cc:113
ns3::UdpHeader::m_sourcePort
uint16_t m_sourcePort
Source port.
Definition
udp-header.h:155
ns3::UdpHeader::GetSourcePort
uint16_t GetSourcePort() const
Definition
udp-header.cc:37
ns3::UdpHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
udp-header.cc:135
ns3::UdpHeader::m_calcChecksum
bool m_calcChecksum
Flag to calculate checksum.
Definition
udp-header.h:164
ns3::UdpHeader::Print
void Print(std::ostream &os) const override
Definition
udp-header.cc:141
ns3::UdpHeader::ForcePayloadSize
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition
udp-header.cc:119
ns3::UdpHeader::IsChecksumOk
bool IsChecksumOk() const
Is the UDP checksum correct ?
Definition
udp-header.cc:107
ns3::UdpHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
udp-header.cc:197
ns3::UdpHeader::m_forcedPayloadSize
uint16_t m_forcedPayloadSize
Payload size (forced)
Definition
udp-header.h:158
ns3::UdpHeader::GetChecksum
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
Definition
udp-header.cc:235
ns3::UdpHeader::m_checksum
uint16_t m_checksum
Forced Checksum value.
Definition
udp-header.h:163
ns3::UdpHeader::InitializeChecksum
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition
udp-header.cc:49
ns3::UdpHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
udp-header.cc:125
ns3::UdpHeader::SetSourcePort
void SetSourcePort(uint16_t port)
Definition
udp-header.cc:31
ns3::UdpHeader::m_goodChecksum
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition
udp-header.h:165
ns3::UdpHeader::SetDestinationPort
void SetDestinationPort(uint16_t port)
Definition
udp-header.cc:25
uint32_t
port
uint16_t port
Definition
dsdv-manet.cc:33
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
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:21
udp-header.h
src
internet
model
udp-header.cc
Generated on Fri Dec 13 2024 18:20:41 for ns-3 by
1.11.0