A Discrete-Event Network Simulator
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
24namespace ns3
25{
26
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 */
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
44{
45 m_sourcePort = 0xfffe;
46 m_destinationPort = 0xfffe;
47 m_payloadSize = 0xfffe;
48}
49
50void
52{
53 m_calcChecksum = true;
54}
55
56void
58{
60}
61
62void
64{
66}
67
68uint16_t
70{
71 return m_sourcePort;
72}
73
74uint16_t
76{
77 return m_destinationPort;
78}
79
80void
81UdpHeader::InitializeChecksum(Address source, Address destination, uint8_t protocol)
82{
83 m_source = source;
84 m_destination = destination;
85 m_protocol = protocol;
86}
87
88void
89UdpHeader::InitializeChecksum(Ipv4Address source, Ipv4Address destination, uint8_t protocol)
90{
91 m_source = source;
92 m_destination = destination;
93 m_protocol = protocol;
94}
95
96void
97UdpHeader::InitializeChecksum(Ipv6Address source, Ipv6Address destination, uint8_t protocol)
98{
99 m_source = source;
100 m_destination = destination;
101 m_protocol = protocol;
102}
103
104uint16_t
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);
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 }
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
138bool
140{
141 return m_goodChecksum;
142}
143
144void
145UdpHeader::ForceChecksum(uint16_t checksum)
146{
147 m_checksum = checksum;
148}
149
150void
151UdpHeader::ForcePayloadSize(uint16_t payloadSize)
152{
153 m_payloadSize = payloadSize;
154}
155
156TypeId
158{
159 static TypeId tid = TypeId("ns3::UdpHeader")
160 .SetParent<Header>()
161 .SetGroupName("Internet")
162 .AddConstructor<UdpHeader>();
163 return tid;
164}
165
166TypeId
168{
169 return GetTypeId();
170}
171
172void
173UdpHeader::Print(std::ostream& os) const
174{
175 os << "length: " << m_payloadSize + GetSerializedSize() << " " << m_sourcePort << " > "
177}
178
181{
182 return 8;
183}
184
185void
187{
188 Buffer::Iterator i = start;
189
192 if (m_payloadSize == 0)
193 {
194 i.WriteHtonU16(start.GetSize());
195 }
196 else
197 {
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 {
219 }
220}
221
224{
225 Buffer::Iterator i = start;
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
243uint16_t
245{
246 return m_checksum;
247}
248
249} // namespace ns3
a polymophic address class
Definition: address.h:100
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
iterator in a Buffer instance
Definition: buffer.h:100
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1141
void WriteU8(uint8_t data)
Definition: buffer.h:881
void WriteU16(uint16_t data)
Definition: buffer.cc:865
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
uint16_t ReadNtohU16()
Definition: buffer.h:954
uint16_t ReadU16()
Definition: buffer.h:1035
void Next()
go forward by one byte
Definition: buffer.h:853
automatically resized byte buffer
Definition: buffer.h:94
void AddAtStart(uint32_t start)
Definition: buffer.cc:314
Buffer::Iterator Begin() const
Definition: buffer.h:1074
Protocol header serialization and deserialization.
Definition: header.h:44
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static bool IsMatchingType(const Address &address)
Describes an IPv6 address.
Definition: ipv6-address.h:49
static bool IsMatchingType(const Address &address)
If the Address matches the type.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
Packet header for UDP packets.
Definition: udp-header.h:41
uint32_t GetSerializedSize() const override
Definition: udp-header.cc:180
void Serialize(Buffer::Iterator start) const override
Definition: udp-header.cc:186
~UdpHeader() override
Definition: udp-header.cc:43
Address m_destination
Destination IP address.
Definition: udp-header.h:175
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition: udp-header.cc:105
void EnableChecksums()
Enable checksum calculation for UDP.
Definition: udp-header.cc:51
uint8_t m_protocol
Protocol number.
Definition: udp-header.h:176
UdpHeader()
Constructor.
Definition: udp-header.cc:33
uint16_t m_destinationPort
Destination port.
Definition: udp-header.h:171
uint16_t GetDestinationPort() const
Definition: udp-header.cc:75
Address m_source
Source IP address.
Definition: udp-header.h:174
uint16_t m_payloadSize
Payload size.
Definition: udp-header.h:172
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition: udp-header.cc:145
uint16_t m_sourcePort
Source port.
Definition: udp-header.h:170
uint16_t GetSourcePort() const
Definition: udp-header.cc:69
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: udp-header.cc:167
bool m_calcChecksum
Flag to calculate checksum.
Definition: udp-header.h:178
void Print(std::ostream &os) const override
Definition: udp-header.cc:173
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition: udp-header.cc:151
bool IsChecksumOk() const
Is the UDP checksum correct ?
Definition: udp-header.cc:139
uint32_t Deserialize(Buffer::Iterator start) override
Definition: udp-header.cc:223
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
Definition: udp-header.cc:244
uint16_t m_checksum
Forced Checksum value.
Definition: udp-header.h:177
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition: udp-header.cc:81
static TypeId GetTypeId()
Get the type ID.
Definition: udp-header.cc:157
void SetSourcePort(uint16_t port)
Definition: udp-header.cc:63
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition: udp-header.h:179
void SetDestinationPort(uint16_t port)
Definition: udp-header.cc:57
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.