A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
udp-header.cc
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 
26 NS_OBJECT_ENSURE_REGISTERED (UdpHeader);
27 
28 /* The magic values below are used only for debugging.
29  * They can be used to easily detect memory corruption
30  * problems so you can see the patterns in memory.
31  */
33  : m_sourcePort (0xfffd),
34  m_destinationPort (0xfffd),
35  m_payloadSize (0xfffd),
36  m_calcChecksum (false),
37  m_goodChecksum (true)
38 {
39 }
40 UdpHeader::~UdpHeader ()
41 {
42  m_sourcePort = 0xfffe;
43  m_destinationPort = 0xfffe;
44  m_payloadSize = 0xfffe;
45 }
46 
47 void
49 {
50  m_calcChecksum = true;
51 }
52 
53 void
55 {
56  m_destinationPort = port;
57 }
58 void
59 UdpHeader::SetSourcePort (uint16_t port)
60 {
61  m_sourcePort = port;
62 }
63 uint16_t
65 {
66  return m_sourcePort;
67 }
68 uint16_t
70 {
71  return m_destinationPort;
72 }
73 void
75  Address destination,
76  uint8_t protocol)
77 {
78  m_source = source;
79  m_destination = destination;
80  m_protocol = protocol;
81 }
82 void
84  Ipv4Address destination,
85  uint8_t protocol)
86 {
87  m_source = source;
88  m_destination = destination;
89  m_protocol = protocol;
90 }
91 void
93  Ipv6Address destination,
94  uint8_t protocol)
95 {
96  m_source = source;
97  m_destination = destination;
98  m_protocol = protocol;
99 }
100 uint16_t
101 UdpHeader::CalculateHeaderChecksum (uint16_t size) const
102 {
103  Buffer buf = Buffer ((2 * Address::MAX_SIZE) + 8);
104  buf.AddAtStart ((2 * Address::MAX_SIZE) + 8);
105  Buffer::Iterator it = buf.Begin ();
106  uint32_t hdrSize = 0;
107 
108  WriteTo (it, m_source);
109  WriteTo (it, m_destination);
110  if (Ipv4Address::IsMatchingType(m_source))
111  {
112  it.WriteU8 (0); /* protocol */
113  it.WriteU8 (m_protocol); /* protocol */
114  it.WriteU8 (size >> 8); /* length */
115  it.WriteU8 (size & 0xff); /* length */
116  hdrSize = 12;
117  }
118  else if (Ipv6Address::IsMatchingType(m_source))
119  {
120  it.WriteU16 (0);
121  it.WriteU8 (size >> 8); /* length */
122  it.WriteU8 (size & 0xff); /* length */
123  it.WriteU16 (0);
124  it.WriteU8 (0);
125  it.WriteU8 (m_protocol); /* protocol */
126  hdrSize = 40;
127  }
128 
129  it = buf.Begin ();
130  /* we don't CompleteChecksum ( ~ ) now */
131  return ~(it.CalculateIpChecksum (hdrSize));
132 }
133 
134 bool
136 {
137  return m_goodChecksum;
138 }
139 
140 
141 TypeId
142 UdpHeader::GetTypeId (void)
143 {
144  static TypeId tid = TypeId ("ns3::UdpHeader")
145  .SetParent<Header> ()
146  .AddConstructor<UdpHeader> ()
147  ;
148  return tid;
149 }
150 TypeId
152 {
153  return GetTypeId ();
154 }
155 void
156 UdpHeader::Print (std::ostream &os) const
157 {
158  os << "length: " << m_payloadSize + GetSerializedSize ()
159  << " "
160  << m_sourcePort << " > " << m_destinationPort
161  ;
162 }
163 
164 uint32_t
166 {
167  return 8;
168 }
169 
170 void
172 {
173  Buffer::Iterator i = start;
174 
175  i.WriteHtonU16 (m_sourcePort);
176  i.WriteHtonU16 (m_destinationPort);
177  i.WriteHtonU16 (start.GetSize ());
178  i.WriteU16 (0);
179 
180  if (m_calcChecksum)
181  {
182  uint16_t headerChecksum = CalculateHeaderChecksum (start.GetSize ());
183  i = start;
184  uint16_t checksum = i.CalculateIpChecksum (start.GetSize (), headerChecksum);
185 
186  i = start;
187  i.Next (6);
188  i.WriteU16 (checksum);
189  }
190 }
191 uint32_t
193 {
194  Buffer::Iterator i = start;
195  m_sourcePort = i.ReadNtohU16 ();
196  m_destinationPort = i.ReadNtohU16 ();
197  m_payloadSize = i.ReadNtohU16 () - GetSerializedSize ();
198  i.Next (2);
199 
200  if(m_calcChecksum)
201  {
202  uint16_t headerChecksum = CalculateHeaderChecksum (start.GetSize ());
203  i = start;
204  uint16_t checksum = i.CalculateIpChecksum (start.GetSize (), headerChecksum);
205 
206  m_goodChecksum = (checksum == 0);
207  }
208 
209  return GetSerializedSize ();
210 }
211 
212 
213 } // namespace ns3
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
void InitializeChecksum(Address source, Address destination, uint8_t protocol)
Definition: udp-header.cc:74
automatically resized byte buffer
Definition: buffer.h:92
void SetDestinationPort(uint16_t port)
Definition: udp-header.cc:54
virtual void Print(std::ostream &os) const
Definition: udp-header.cc:156
void SetSourcePort(uint16_t port)
Definition: udp-header.cc:59
bool IsChecksumOk(void) const
Is the UDP checksum correct ?
Definition: udp-header.cc:135
iterator in a Buffer instance
Definition: buffer.h:98
a polymophic address class
Definition: address.h:86
void EnableChecksums(void)
Enable checksum calculation for UDP.
Definition: udp-header.cc:48
void WriteU16(uint16_t data)
Definition: buffer.cc:895
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
void Next(void)
Definition: buffer.h:666
UdpHeader()
Constructor.
Definition: udp-header.cc:32
static bool IsMatchingType(const Address &address)
Buffer::Iterator Begin(void) const
Definition: buffer.h:875
uint16_t GetSourcePort(void) const
Definition: udp-header.cc:64
virtual void Serialize(Buffer::Iterator start) const
Definition: udp-header.cc:171
Describes an IPv6 address.
Definition: ipv6-address.h:44
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:192
void WriteU8(uint8_t data)
Definition: buffer.h:690
virtual TypeId GetInstanceTypeId(void) const
Definition: udp-header.cc:151
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:69
uint32_t GetSize(void) const
Definition: buffer.cc:1183
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
virtual uint32_t GetSerializedSize(void) const
Definition: udp-header.cc:165