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 void
31 {
32  m_calcChecksum = true;
33 }
34 
35 void
37 {
39 }
40 
41 void
43 {
45 }
46 
47 uint16_t
49 {
50  return m_sourcePort;
51 }
52 
53 uint16_t
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
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);
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  }
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
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
137 {
138  static TypeId tid = TypeId("ns3::UdpHeader")
139  .SetParent<Header>()
140  .SetGroupName("Internet")
141  .AddConstructor<UdpHeader>();
142  return tid;
143 }
144 
145 TypeId
147 {
148  return GetTypeId();
149 }
150 
151 void
152 UdpHeader::Print(std::ostream& os) const
153 {
154  os << "length: " << m_payloadSize + GetSerializedSize() << " " << m_sourcePort << " > "
156 }
157 
158 uint32_t
160 {
161  return 8;
162 }
163 
164 void
166 {
168 
171  if (m_forcedPayloadSize == 0)
172  {
173  i.WriteHtonU16(start.GetSize());
174  }
175  else
176  {
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
203 {
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
224 {
225  return m_checksum;
226 }
227 
228 } // namespace ns3
a polymophic address class
Definition: address.h:101
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
iterator in a Buffer instance
Definition: buffer.h:100
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1135
void WriteU8(uint8_t data)
Definition: buffer.h:881
void WriteU16(uint16_t data)
Definition: buffer.cc:859
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
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: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:931
Packet header for UDP packets.
Definition: udp-header.h:41
uint32_t GetSerializedSize() const override
Definition: udp-header.cc:159
void Serialize(Buffer::Iterator start) const override
Definition: udp-header.cc:165
Address m_destination
Destination IP address.
Definition: udp-header.h:172
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition: udp-header.cc:84
void EnableChecksums()
Enable checksum calculation for UDP.
Definition: udp-header.cc:30
uint8_t m_protocol
Protocol number.
Definition: udp-header.h:173
uint16_t m_destinationPort
Destination port.
Definition: udp-header.h:167
uint16_t GetDestinationPort() const
Definition: udp-header.cc:54
Address m_source
Source IP address.
Definition: udp-header.h:171
uint16_t m_payloadSize
Payload size.
Definition: udp-header.h:168
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition: udp-header.cc:124
uint16_t m_sourcePort
Source port.
Definition: udp-header.h:166
uint16_t GetSourcePort() const
Definition: udp-header.cc:48
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition: udp-header.cc:146
bool m_calcChecksum
Flag to calculate checksum.
Definition: udp-header.h:175
void Print(std::ostream &os) const override
Definition: udp-header.cc:152
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition: udp-header.cc:130
bool IsChecksumOk() const
Is the UDP checksum correct ?
Definition: udp-header.cc:118
uint16_t m_forcedPayloadSize
Payload size (forced)
Definition: udp-header.h:169
uint16_t GetChecksum() const
Return the checksum (only known after a Deserialize)
Definition: udp-header.cc:223
uint16_t m_checksum
Forced Checksum value.
Definition: udp-header.h:174
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition: udp-header.cc:60
static TypeId GetTypeId()
Get the type ID.
Definition: udp-header.cc:136
void SetSourcePort(uint16_t port)
Definition: udp-header.cc:42
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition: udp-header.h:176
void SetDestinationPort(uint16_t port)
Definition: udp-header.cc:36
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.