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 
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 }
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 {
57 }
58 void
60 {
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
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);
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  }
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
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 {
174 
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 {
195  m_sourcePort = i.ReadNtohU16 ();
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