A Discrete-Event Network Simulator
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 
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 
50 void
52 {
53  m_calcChecksum = true;
54 }
55 
56 void
58 {
60 }
61 
62 void
64 {
66 }
67 
68 uint16_t
70 {
71  return m_sourcePort;
72 }
73 
74 uint16_t
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
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);
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 
138 bool
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
158 {
159  static TypeId tid = TypeId("ns3::UdpHeader")
160  .SetParent<Header>()
161  .SetGroupName("Internet")
162  .AddConstructor<UdpHeader>();
163  return tid;
164 }
165 
166 TypeId
168 {
169  return GetTypeId();
170 }
171 
172 void
173 UdpHeader::Print(std::ostream& os) const
174 {
175  os << "length: " << m_payloadSize + GetSerializedSize() << " " << m_sourcePort << " > "
177 }
178 
179 uint32_t
181 {
182  return 8;
183 }
184 
185 void
187 {
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  {
218  i.WriteU16(m_checksum);
219  }
220 }
221 
222 uint32_t
224 {
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
245 {
246  return m_checksum;
247 }
248 
249 } // namespace ns3
a polymophic address class
Definition: address.h:100
iterator in a Buffer instance
Definition: buffer.h:100
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1138
void WriteU8(uint8_t data)
Definition: buffer.h:881
void WriteU16(uint16_t data)
Definition: buffer.cc:862
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:311
Buffer::Iterator Begin() const
Definition: buffer.h:1074
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
static bool IsMatchingType(const Address &address)
Describes an IPv6 address.
Definition: ipv6-address.h:50
static bool IsMatchingType(const Address &address)
If the Address matches the type.
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
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
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:45
#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.