A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-fs-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
18 *
19 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
20 * ResiliNets Research Group https://resilinets.org/
21 * Information and Telecommunication Technology Center (ITTC)
22 * and Department of Electrical Engineering and Computer Science
23 * The University of Kansas Lawrence, KS USA.
24 *
25 * Work supported in part by NSF FIND (Future Internet Design) Program
26 * under grant CNS-0626918 (Postmodern Internet Architecture),
27 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
28 * US Department of Defense (DoD), and ITTC at The University of Kansas.
29 */
30
31#include "dsr-fs-header.h"
32
33#include "ns3/assert.h"
34#include "ns3/header.h"
35#include "ns3/log.h"
36
37#include <vector>
38
39namespace ns3
40{
41
42NS_LOG_COMPONENT_DEFINE("DsrFsHeader");
43
44namespace dsr
45{
46
48
49TypeId
51{
52 static TypeId tid = TypeId("ns3::dsr::DsrFsHeader")
54 .SetParent<Header>()
55 .SetGroupName("Dsr");
56 return tid;
57}
58
61{
62 return GetTypeId();
63}
64
66 : m_nextHeader(0),
67 m_messageType(0),
68 m_payloadLen(0),
69 m_sourceId(0),
70 m_destId(0),
71 m_data(0)
72{
73}
74
76{
77}
78
79void
81{
82 m_nextHeader = protocol;
83}
84
85uint8_t
87{
88 return m_nextHeader;
89}
90
91void
92DsrFsHeader::SetMessageType(uint8_t messageType)
93{
94 m_messageType = messageType;
95}
96
97uint8_t
99{
100 return m_messageType;
101}
102
103void
105{
106 m_payloadLen = length;
107}
108
109uint16_t
111{
112 return m_payloadLen;
113}
114
115void
116DsrFsHeader::SetSourceId(uint16_t sourceId)
117{
118 m_sourceId = sourceId;
119}
120
121uint16_t
123{
124 return m_sourceId;
125}
126
127void
129{
130 m_destId = destId;
131}
132
133uint16_t
135{
136 return m_destId;
137}
138
139void
140DsrFsHeader::Print(std::ostream& os) const
141{
142 os << "nextHeader: " << (uint32_t)GetNextHeader()
143 << " messageType: " << (uint32_t)GetMessageType() << " sourceId: " << (uint32_t)GetSourceId()
144 << " destinationId: " << (uint32_t)GetDestId()
145 << " length: " << (uint32_t)GetPayloadLength();
146}
147
150{
151 return 8;
152}
153
154void
156{
157 Buffer::Iterator i = start;
158
164
166}
167
170{
171 Buffer::Iterator i = start;
172
173 m_nextHeader = i.ReadU8();
174 m_messageType = i.ReadU8();
175 m_sourceId = i.ReadU16();
176 m_destId = i.ReadU16();
177 m_payloadLen = i.ReadU16();
178
179 const uint32_t dataLength = GetPayloadLength();
180 std::vector<uint8_t> data(dataLength);
181 i.Read(data.data(), dataLength);
182
183 if (dataLength > m_data.GetSize())
184 {
185 m_data.AddAtEnd(dataLength - m_data.GetSize());
186 }
187 else
188 {
189 m_data.RemoveAtEnd(m_data.GetSize() - dataLength);
190 }
191
192 i = m_data.Begin();
193 i.Write(data.data(), data.size());
194
195 return GetSerializedSize();
196}
197
199 : m_optionData(0),
200 m_optionsOffset(optionsOffset)
201{
202}
203
205{
206}
207
210{
211 DsrOptionHeader::Alignment align = {4, 0};
212 return m_optionData.GetSize() + CalculatePad(align);
213}
214
215void
217{
218 start.Write(m_optionData.Begin(), m_optionData.End());
219 DsrOptionHeader::Alignment align = {4, 0};
220 uint32_t fill = CalculatePad(align);
221 NS_LOG_LOGIC("fill with " << fill << " bytes padding");
222 switch (fill)
223 {
224 case 0:
225 return;
226 case 1:
228 return;
229 default:
230 DsrOptionPadnHeader(fill).Serialize(start);
231 return;
232 }
233}
234
237{
238 std::vector<uint8_t> buf(length);
239 start.Read(buf.data(), length);
241 m_optionData.AddAtEnd(length);
242 m_optionData.Begin().Write(buf.data(), buf.size());
243 return length;
244}
245
246void
248{
250
251 uint32_t pad = CalculatePad(option.GetAlignment());
252 NS_LOG_LOGIC("need " << pad << " bytes padding");
253 switch (pad)
254 {
255 case 0:
256 break; // no padding needed
257 case 1:
259 break;
260 default:
262 break;
263 }
264
267 it.Prev(option.GetSerializedSize());
268 option.Serialize(it);
269}
270
273{
274 return (alignment.offset - (m_optionData.GetSize() + m_optionsOffset)) % alignment.factor;
275}
276
279{
280 return m_optionsOffset;
281}
282
283Buffer
285{
286 return m_optionData;
287}
288
290
291TypeId
293{
294 static TypeId tid =
295 TypeId("ns3::DsrRoutingHeader").AddConstructor<DsrRoutingHeader>().SetParent<DsrFsHeader>();
296 return tid;
297}
298
299TypeId
301{
302 return GetTypeId();
303}
304
306 : DsrOptionField(8)
307{
308}
309
311{
312}
313
314void
315DsrRoutingHeader::Print(std::ostream& os) const
316{
317 os << " nextHeader: " << (uint32_t)GetNextHeader()
318 << " messageType: " << (uint32_t)GetMessageType() << " sourceId: " << (uint32_t)GetSourceId()
319 << " destinationId: " << (uint32_t)GetDestId()
320 << " length: " << (uint32_t)GetPayloadLength();
321}
322
325{
326 // 8 bytes is the DsrFsHeader length
328}
329
330void
332{
333 Buffer::Iterator i = start;
334
338 i.WriteU16(GetDestId());
340
342}
343
346{
347 Buffer::Iterator i = start;
348
351 SetSourceId(i.ReadU16());
352 SetDestId(i.ReadU16());
354
356
357 return GetSerializedSize();
358}
359
360} /* namespace dsr */
361} /* namespace ns3 */
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
void Write(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:948
void WriteU16(uint16_t data)
Definition: buffer.cc:859
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1125
void Prev()
go backward by one byte
Definition: buffer.h:860
uint16_t ReadU16()
Definition: buffer.h:1035
automatically resized byte buffer
Definition: buffer.h:94
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:493
uint32_t GetSize() const
Definition: buffer.h:1068
Buffer::Iterator Begin() const
Definition: buffer.h:1074
void AddAtEnd(uint32_t end)
Definition: buffer.cc:360
Buffer::Iterator End() const
Definition: buffer.h:1081
const uint8_t * PeekData() const
Definition: buffer.cc:703
a unique identifier for an interface.
Definition: type-id.h:59
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:651
Dsr fixed size header Format.
Definition: dsr-fs-header.h:82
void SetSourceId(uint16_t sourceId)
brief Set the source ID of the header.
DsrFsHeader()
Constructor.
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
void SetDestId(uint16_t destId)
brief Set the dest ID of the header.
uint8_t GetMessageType() const
brief Get the message type of the header.
static TypeId GetTypeId()
Get the type identificator.
uint8_t GetNextHeader() const
Get the next header.
uint16_t m_destId
The destination node id.
uint16_t m_sourceId
The source node id.
uint16_t GetSourceId() const
brief Get the source ID of the header.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint16_t GetDestId() const
brief Get the dest ID of the header.
uint8_t m_messageType
The type of the message.
uint16_t m_payloadLen
The "payload length" field.
void SetMessageType(uint8_t messageType)
brief Set the message type of the header.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
uint16_t GetPayloadLength() const
Get the payload length of the header.
void SetPayloadLength(uint16_t length)
brief Set the payload length of the header.
uint8_t m_nextHeader
The "next header" field.
Buffer m_data
The data of the extension.
void Print(std::ostream &os) const override
Print some information about the packet.
~DsrFsHeader() override
Destructor.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Option field for an DsrFsHeader Enables adding options to an DsrFsHeader.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Buffer m_optionData
Data payload.
Buffer GetDsrOptionBuffer()
Get the buffer.
uint32_t m_optionsOffset
Offset.
uint32_t GetDsrOptionsOffset() const
Get the offset where the options begin, measured from the start of the extension header.
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
DsrOptionField(uint32_t optionsOffset)
Constructor.
Header for Dsr Options.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Header of Dsr Option Pad1.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of Dsr Option Padn.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of Dsr Routing.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
~DsrRoutingHeader() override
Destructor.
void Print(std::ostream &os) const override
Print some information about the packet.
static TypeId GetTypeId()
Get the type identificator.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
represents the alignment requirements of an option header