A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
udp-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "udp-header.h"
22 #include "ns3/address-utils.h"
23 
24 namespace ns3 {
25 
27  ;
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 }
43 {
44  m_sourcePort = 0xfffe;
45  m_destinationPort = 0xfffe;
46  m_payloadSize = 0xfffe;
47 }
48 
49 void
51 {
52  m_calcChecksum = true;
53 }
54 
55 void
57 {
59 }
60 void
62 {
64 }
65 uint16_t
67 {
68  return m_sourcePort;
69 }
70 uint16_t
72 {
73  return m_destinationPort;
74 }
75 void
77  Address destination,
78  uint8_t protocol)
79 {
80  m_source = source;
81  m_destination = destination;
82  m_protocol = protocol;
83 }
84 void
86  Ipv4Address destination,
87  uint8_t protocol)
88 {
89  m_source = source;
90  m_destination = destination;
91  m_protocol = protocol;
92 }
93 void
95  Ipv6Address destination,
96  uint8_t protocol)
97 {
98  m_source = source;
99  m_destination = destination;
100  m_protocol = protocol;
101 }
102 uint16_t
104 {
105  Buffer buf = Buffer ((2 * Address::MAX_SIZE) + 8);
106  buf.AddAtStart ((2 * Address::MAX_SIZE) + 8);
107  Buffer::Iterator it = buf.Begin ();
108  uint32_t hdrSize = 0;
109 
110  WriteTo (it, m_source);
111  WriteTo (it, m_destination);
113  {
114  it.WriteU8 (0); /* protocol */
115  it.WriteU8 (m_protocol); /* protocol */
116  it.WriteU8 (size >> 8); /* length */
117  it.WriteU8 (size & 0xff); /* length */
118  hdrSize = 12;
119  }
121  {
122  it.WriteU16 (0);
123  it.WriteU8 (size >> 8); /* length */
124  it.WriteU8 (size & 0xff); /* length */
125  it.WriteU16 (0);
126  it.WriteU8 (0);
127  it.WriteU8 (m_protocol); /* protocol */
128  hdrSize = 40;
129  }
130 
131  it = buf.Begin ();
132  /* we don't CompleteChecksum ( ~ ) now */
133  return ~(it.CalculateIpChecksum (hdrSize));
134 }
135 
136 bool
138 {
139  return m_goodChecksum;
140 }
141 
142 void
143 UdpHeader::ForceChecksum (uint16_t checksum)
144 {
145  m_checksum = checksum;
146 }
147 
148 void
149 UdpHeader::ForcePayloadSize (uint16_t payloadSize)
150 {
151  m_payloadSize = payloadSize;
152 }
153 
154 TypeId
156 {
157  static TypeId tid = TypeId ("ns3::UdpHeader")
158  .SetParent<Header> ()
159  .AddConstructor<UdpHeader> ()
160  ;
161  return tid;
162 }
163 TypeId
165 {
166  return GetTypeId ();
167 }
168 void
169 UdpHeader::Print (std::ostream &os) const
170 {
171  os << "length: " << m_payloadSize + GetSerializedSize ()
172  << " "
173  << m_sourcePort << " > " << m_destinationPort
174  ;
175 }
176 
177 uint32_t
179 {
180  return 8;
181 }
182 
183 void
185 {
187 
190  if (m_payloadSize == 0)
191  {
192  i.WriteHtonU16 (start.GetSize ());
193  }
194  else
195  {
197  }
198 
199  if ( m_checksum == 0)
200  {
201  i.WriteU16 (0);
202 
203  if (m_calcChecksum)
204  {
205  uint16_t headerChecksum = CalculateHeaderChecksum (start.GetSize ());
206  i = start;
207  uint16_t checksum = i.CalculateIpChecksum (start.GetSize (), headerChecksum);
208 
209  i = start;
210  i.Next (6);
211  i.WriteU16 (checksum);
212  }
213  }
214  else
215  {
216  i.WriteU16 (m_checksum);
217  }
218 }
219 uint32_t
221 {
223  m_sourcePort = i.ReadNtohU16 ();
226  m_checksum = i.ReadU16 ();
227 
228  if (m_calcChecksum)
229  {
230  uint16_t headerChecksum = CalculateHeaderChecksum (start.GetSize ());
231  i = start;
232  uint16_t checksum = i.CalculateIpChecksum (start.GetSize (), headerChecksum);
233 
234  m_goodChecksum = (checksum == 0);
235  }
236 
237  return GetSerializedSize ();
238 }
239 
240 uint16_t
242 {
243  return m_checksum;
244 }
245 
246 } // namespace ns3
uint16_t ReadU16(void)
Definition: buffer.h:845
Protocol header serialization and deserialization.
Definition: header.h:42
static bool IsMatchingType(const Address &address)
If the Address matches the type.
uint16_t CalculateIpChecksum(uint16_t size)
Calculate the checksum.
Definition: buffer.cc:1158
static TypeId GetTypeId(void)
Get the type ID.
Definition: udp-header.cc:155
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition: udp-header.cc:76
bool m_goodChecksum
Flag to indicate that checksum is correct.
Definition: udp-header.h:185
Address m_source
Source IP address.
Definition: udp-header.h:180
automatically resized byte buffer
Definition: buffer.h:92
void SetDestinationPort(uint16_t port)
Definition: udp-header.cc:56
virtual void Print(std::ostream &os) const
Definition: udp-header.cc:169
uint16_t m_checksum
Forced Checksum value.
Definition: udp-header.h:183
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void WriteTo(Buffer::Iterator &i, Ipv4Address ad)
Write an Ipv4Address to a Buffer.
uint16_t CalculateHeaderChecksum(uint16_t size) const
Calculate the header checksum.
Definition: udp-header.cc:103
void SetSourcePort(uint16_t port)
Definition: udp-header.cc:61
bool IsChecksumOk(void) const
Is the UDP checksum correct ?
Definition: udp-header.cc:137
uint16_t port
Definition: dsdv-manet.cc:44
iterator in a Buffer instance
Definition: buffer.h:98
a polymophic address class
Definition: address.h:86
void ForceChecksum(uint16_t checksum)
Force the UDP checksum to a given value.
Definition: udp-header.cc:143
void EnableChecksums(void)
Enable checksum calculation for UDP.
Definition: udp-header.cc:50
void WriteU16(uint16_t data)
Definition: buffer.cc:895
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
void ForcePayloadSize(uint16_t payloadSize)
Force the UDP payload length to a given value.
Definition: udp-header.cc:149
UdpHeader()
Constructor.
Definition: udp-header.cc:33
static bool IsMatchingType(const Address &address)
Buffer::Iterator Begin(void) const
Definition: buffer.h:875
uint16_t GetChecksum()
Return the checksum (only known after a Deserialize)
Definition: udp-header.cc:241
uint16_t GetSourcePort(void) const
Definition: udp-header.cc:66
uint16_t m_payloadSize
Payload size.
Definition: udp-header.h:178
bool m_calcChecksum
Flag to calculate checksum.
Definition: udp-header.h:184
virtual void Serialize(Buffer::Iterator start) const
Definition: udp-header.cc:184
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition: udp-header.cc:220
uint16_t m_sourcePort
Source port.
Definition: udp-header.h:176
Address m_destination
Destination IP address.
Definition: udp-header.h:181
void WriteU8(uint8_t data)
Definition: buffer.h:690
virtual TypeId GetInstanceTypeId(void) const
Definition: udp-header.cc:164
uint8_t m_protocol
Protocol number.
Definition: udp-header.h:182
bool AddAtStart(uint32_t start)
Definition: buffer.cc:305
uint16_t ReadNtohU16(void)
Definition: buffer.h:767
uint16_t GetDestinationPort(void) const
Definition: udp-header.cc:71
uint32_t GetSize(void) const
Definition: buffer.cc:1183
uint16_t m_destinationPort
Destination port.
Definition: udp-header.h:177
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual uint32_t GetSerializedSize(void) const
Definition: udp-header.cc:178