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
dsr-fs-header.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2011 Yufei Cheng
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: Yufei Cheng <yfcheng@ittc.ku.edu>
19
*
20
* James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21
* ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22
* Information and Telecommunication Technology Center (ITTC)
23
* and Department of Electrical Engineering and Computer Science
24
* The University of Kansas Lawrence, KS USA.
25
*
26
* Work supported in part by NSF FIND (Future Internet Design) Program
27
* under grant CNS-0626918 (Postmodern Internet Architecture),
28
* NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29
* US Department of Defense (DoD), and ITTC at The University of Kansas.
30
*/
31
32
#include "ns3/assert.h"
33
#include "ns3/log.h"
34
#include "ns3/header.h"
35
#include "
dsr-fs-header.h
"
36
37
namespace
ns3 {
38
namespace
dsr {
39
40
NS_LOG_COMPONENT_DEFINE
(
"DsrFsHeader"
);
41
42
NS_OBJECT_ENSURE_REGISTERED
(DsrFsHeader);
43
44
TypeId
DsrFsHeader::GetTypeId
()
45
{
46
static
TypeId
tid =
TypeId
(
"ns3::dsr::DsrFsHeader"
)
47
.
AddConstructor
<
DsrFsHeader
> ()
48
.SetParent<Header> ()
49
;
50
return
tid;
51
}
52
53
TypeId
DsrFsHeader::GetInstanceTypeId
()
const
54
{
55
return
GetTypeId
();
56
}
57
58
DsrFsHeader::DsrFsHeader
()
59
: m_nextHeader (0),
60
m_messageType (0),
61
m_payloadLen (0),
62
m_sourceId (0),
63
m_destId (0),
64
m_data (0)
65
{
66
}
67
68
DsrFsHeader::~DsrFsHeader
()
69
{
70
}
71
72
void
DsrFsHeader::SetNextHeader
(uint8_t protocol)
73
{
74
m_nextHeader
= protocol;
75
}
76
77
uint8_t
DsrFsHeader::GetNextHeader
()
const
78
{
79
return
m_nextHeader
;
80
}
81
82
void
DsrFsHeader::SetMessageType
(uint8_t messageType)
83
{
84
m_messageType
= messageType;
85
}
86
87
uint8_t
DsrFsHeader::GetMessageType
()
const
88
{
89
return
m_messageType
;
90
}
91
92
void
DsrFsHeader::SetPayloadLength
(uint16_t length)
93
{
94
m_payloadLen
= length;
95
}
96
97
uint16_t
DsrFsHeader::GetPayloadLength
()
const
98
{
99
return
m_payloadLen
;
100
}
101
102
void
DsrFsHeader::SetSourceId
(uint16_t sourceId)
103
{
104
m_sourceId
= sourceId;
105
}
106
107
uint16_t
DsrFsHeader::GetSourceId
()
const
108
{
109
return
m_sourceId
;
110
}
111
112
void
DsrFsHeader::SetDestId
(uint16_t destId)
113
{
114
m_destId
= destId;
115
}
116
117
uint16_t
DsrFsHeader::GetDestId
()
const
118
{
119
return
m_destId
;
120
}
121
122
void
DsrFsHeader::Print
(std::ostream &os)
const
123
{
124
os
125
<<
" nextHeader: "
<< (uint32_t)
GetNextHeader
() <<
" messageType: "
<< (uint32_t)
GetMessageType
()
126
<<
" sourceId: "
<< (uint32_t)
GetSourceId
() <<
" destinationId: "
<< (uint32_t)
GetDestId
()
127
<<
" length: "
<< (uint32_t)
GetPayloadLength
();
128
}
129
130
uint32_t
DsrFsHeader::GetSerializedSize
()
const
131
{
132
return
8;
133
}
134
135
void
DsrFsHeader::Serialize
(
Buffer::Iterator
start
)
const
136
{
137
Buffer::Iterator
i =
start
;
138
139
i.
WriteU8
(
m_nextHeader
);
140
i.
WriteU8
(
m_messageType
);
141
i.
WriteU16
(
m_sourceId
);
142
i.
WriteU16
(
m_destId
);
143
i.
WriteU16
(
m_payloadLen
);
144
145
i.
Write
(
m_data
.
PeekData
(),
m_data
.
GetSize
());
146
}
147
148
uint32_t
DsrFsHeader::Deserialize
(
Buffer::Iterator
start
)
149
{
150
Buffer::Iterator
i =
start
;
151
152
m_nextHeader
= i.
ReadU8
();
153
m_messageType
= i.
ReadU8
();
154
m_sourceId
= i.
ReadU16
();
155
m_destId
= i.
ReadU16
();
156
m_payloadLen
= i.
ReadU16
();
157
158
uint32_t dataLength =
GetPayloadLength
();
159
uint8_t
data
[dataLength];
160
i.
Read
(data, dataLength);
161
162
if
(dataLength >
m_data
.
GetSize
())
163
{
164
m_data
.
AddAtEnd
(dataLength -
m_data
.
GetSize
());
165
}
166
else
167
{
168
m_data
.
RemoveAtEnd
(
m_data
.
GetSize
() - dataLength);
169
}
170
171
i =
m_data
.
Begin
();
172
i.
Write
(data, dataLength);
173
174
return
GetSerializedSize
();
175
}
176
177
DsrOptionField::DsrOptionField
(uint32_t optionsOffset)
178
: m_optionData (0),
179
m_optionsOffset (optionsOffset)
180
{
181
}
182
183
DsrOptionField::~DsrOptionField
()
184
{
185
}
186
187
uint32_t
DsrOptionField::GetSerializedSize
()
const
188
{
189
DsrOptionHeader::Alignment
align = {4,0};
190
return
m_optionData
.
GetSize
() +
CalculatePad
(align);
191
}
192
193
void
DsrOptionField::Serialize
(
Buffer::Iterator
start
)
const
194
{
195
start.
Write
(
m_optionData
.
Begin
(),
m_optionData
.
End
());
196
DsrOptionHeader::Alignment
align = {4,0};
197
uint32_t fill =
CalculatePad
(align);
198
NS_LOG_LOGIC
(
"fill with "
<< fill <<
" bytes padding"
);
199
switch
(fill)
200
{
201
case
0:
202
return
;
203
case
1:
204
DsrOptionPad1Header
().
Serialize
(start);
205
return
;
206
default
:
207
DsrOptionPadnHeader
(fill).
Serialize
(start);
208
return
;
209
}
210
}
211
212
uint32_t
DsrOptionField::Deserialize
(
Buffer::Iterator
start
, uint32_t length)
213
{
214
uint8_t buf[length];
215
start.
Read
(buf, length);
216
m_optionData
=
Buffer
();
217
m_optionData
.
AddAtEnd
(length);
218
m_optionData
.
Begin
().
Write
(buf, length);
219
return
length;
220
}
221
222
void
DsrOptionField::AddDsrOption
(
DsrOptionHeader
const
& option)
223
{
224
NS_LOG_FUNCTION_NOARGS
();
225
226
uint32_t pad =
CalculatePad
(option.
GetAlignment
());
227
NS_LOG_LOGIC
(
"need "
<< pad <<
" bytes padding"
);
228
switch
(pad)
229
{
230
case
0:
231
break
;
// no padding needed
232
case
1:
233
AddDsrOption
(
DsrOptionPad1Header
());
234
break
;
235
default
:
236
AddDsrOption
(
DsrOptionPadnHeader
(pad));
237
break
;
238
}
239
240
m_optionData
.
AddAtEnd
(option.
GetSerializedSize
());
241
Buffer::Iterator
it =
m_optionData
.
End
();
242
it.
Prev
(option.
GetSerializedSize
());
243
option.
Serialize
(it);
244
}
245
246
uint32_t
DsrOptionField::CalculatePad
(
DsrOptionHeader::Alignment
alignment)
const
247
{
248
return
(alignment.
offset
- (
m_optionData
.
GetSize
() +
m_optionsOffset
)) % alignment.
factor
;
249
}
250
251
uint32_t
DsrOptionField::GetDsrOptionsOffset
()
252
{
253
return
m_optionsOffset
;
254
}
255
256
Buffer
DsrOptionField::GetDsrOptionBuffer
()
257
{
258
return
m_optionData
;
259
}
260
261
NS_OBJECT_ENSURE_REGISTERED
(
DsrRoutingHeader
);
262
263
TypeId
DsrRoutingHeader::GetTypeId
()
264
{
265
static
TypeId
tid =
TypeId
(
"ns3::DsrRoutingHeader"
)
266
.
AddConstructor
<
DsrRoutingHeader
> ()
267
.SetParent<DsrFsHeader> ()
268
;
269
return
tid;
270
}
271
272
TypeId
DsrRoutingHeader::GetInstanceTypeId
()
const
273
{
274
return
GetTypeId
();
275
}
276
277
DsrRoutingHeader::DsrRoutingHeader
()
278
:
DsrOptionField
(8)
279
{
280
}
281
282
DsrRoutingHeader::~DsrRoutingHeader
()
283
{
284
}
285
286
void
DsrRoutingHeader::Print
(std::ostream &os)
const
287
{
288
os
289
<<
" nextHeader: "
<< (uint32_t)
GetNextHeader
() <<
" messageType: "
<< (uint32_t)
GetMessageType
()
290
<<
" sourceId: "
<< (uint32_t)
GetSourceId
() <<
" destinationId: "
<< (uint32_t)
GetDestId
()
291
<<
" length: "
<< (uint32_t)
GetPayloadLength
();
292
}
293
294
uint32_t
DsrRoutingHeader::GetSerializedSize
()
const
295
{
296
// 8 bytes is the DsrFsHeader length
297
return
8 +
DsrOptionField::GetSerializedSize
();
298
}
299
300
void
DsrRoutingHeader::Serialize
(
Buffer::Iterator
start
)
const
301
{
302
Buffer::Iterator
i =
start
;
303
304
i.
WriteU8
(
GetNextHeader
());
305
i.
WriteU8
(
GetMessageType
());
306
i.
WriteU16
(
GetSourceId
());
307
i.
WriteU16
(
GetDestId
());
308
i.
WriteU16
(
GetPayloadLength
());
309
310
DsrOptionField::Serialize
(i);
311
}
312
313
uint32_t
DsrRoutingHeader::Deserialize
(
Buffer::Iterator
start
)
314
{
315
Buffer::Iterator
i =
start
;
316
317
SetNextHeader
(i.
ReadU8
());
318
SetMessageType
(i.
ReadU8
());
319
SetSourceId
(i.
ReadU16
());
320
SetDestId
(i.
ReadU16
());
321
SetPayloadLength
(i.
ReadU16
());
322
323
DsrOptionField::Deserialize
(i,
GetPayloadLength
());
324
325
return
GetSerializedSize
();
326
}
327
328
}
/* namespace dsr */
329
}
/* namespace ns3 */
src
dsr
model
dsr-fs-header.cc
Generated on Tue Oct 9 2012 16:45:36 for ns-3 by
1.8.1.2