A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
*/
32
UdpHeader::UdpHeader
()
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
48
UdpHeader::EnableChecksums
(
void
)
49
{
50
m_calcChecksum
=
true
;
51
}
52
53
void
54
UdpHeader::SetDestinationPort
(uint16_t
port
)
55
{
56
m_destinationPort
=
port
;
57
}
58
void
59
UdpHeader::SetSourcePort
(uint16_t
port
)
60
{
61
m_sourcePort
=
port
;
62
}
63
uint16_t
64
UdpHeader::GetSourcePort
(
void
)
const
65
{
66
return
m_sourcePort
;
67
}
68
uint16_t
69
UdpHeader::GetDestinationPort
(
void
)
const
70
{
71
return
m_destinationPort
;
72
}
73
void
74
UdpHeader::InitializeChecksum
(
Address
source,
75
Address
destination,
76
uint8_t protocol)
77
{
78
m_source
= source;
79
m_destination
= destination;
80
m_protocol
= protocol;
81
}
82
void
83
UdpHeader::InitializeChecksum
(
Ipv4Address
source,
84
Ipv4Address
destination,
85
uint8_t protocol)
86
{
87
m_source
= source;
88
m_destination
= destination;
89
m_protocol
= protocol;
90
}
91
void
92
UdpHeader::InitializeChecksum
(
Ipv6Address
source,
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
135
UdpHeader::IsChecksumOk
(
void
)
const
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
151
UdpHeader::GetInstanceTypeId
(
void
)
const
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
165
UdpHeader::GetSerializedSize
(
void
)
const
166
{
167
return
8;
168
}
169
170
void
171
UdpHeader::Serialize
(
Buffer::Iterator
start
)
const
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
192
UdpHeader::Deserialize
(
Buffer::Iterator
start
)
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
src
internet
model
udp-header.cc
Generated on Tue Oct 9 2012 16:45:39 for ns-3 by
1.8.1.2