A Discrete-Event Network Simulator
API
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 
39 NS_LOG_COMPONENT_DEFINE ("DsrFsHeader");
40 
41 namespace dsr {
42 
43 NS_OBJECT_ENSURE_REGISTERED (DsrFsHeader);
44 
46 {
47  static TypeId tid = TypeId ("ns3::dsr::DsrFsHeader")
49  .SetParent<Header> ()
50  .SetGroupName ("Dsr")
51  ;
52  return tid;
53 }
54 
56 {
57  return GetTypeId ();
58 }
59 
61  : m_nextHeader (0),
62  m_messageType (0),
63  m_payloadLen (0),
64  m_sourceId (0),
65  m_destId (0),
66  m_data (0)
67 {
68 }
69 
71 {
72 }
73 
74 void DsrFsHeader::SetNextHeader (uint8_t protocol)
75 {
76  m_nextHeader = protocol;
77 }
78 
80 {
81  return m_nextHeader;
82 }
83 
84 void DsrFsHeader::SetMessageType (uint8_t messageType)
85 {
86  m_messageType = messageType;
87 }
88 
90 {
91  return m_messageType;
92 }
93 
94 void DsrFsHeader::SetPayloadLength (uint16_t length)
95 {
96  m_payloadLen = length;
97 }
98 
100 {
101  return m_payloadLen;
102 }
103 
104 void DsrFsHeader::SetSourceId (uint16_t sourceId)
105 {
106  m_sourceId = sourceId;
107 }
108 
109 uint16_t DsrFsHeader::GetSourceId () const
110 {
111  return m_sourceId;
112 }
113 
114 void DsrFsHeader::SetDestId (uint16_t destId)
115 {
116  m_destId = destId;
117 }
118 
119 uint16_t DsrFsHeader::GetDestId () const
120 {
121  return m_destId;
122 }
123 
124 void DsrFsHeader::Print (std::ostream &os) const
125 {
126  os
127  << "nextHeader: " << (uint32_t)GetNextHeader () << " messageType: " << (uint32_t)GetMessageType ()
128  << " sourceId: " << (uint32_t)GetSourceId () << " destinationId: " << (uint32_t)GetDestId ()
129  << " length: " << (uint32_t)GetPayloadLength ();
130 }
131 
133 {
134  return 8;
135 }
136 
138 {
140 
141  i.WriteU8 (m_nextHeader);
143  i.WriteU16 (m_sourceId);
144  i.WriteU16 (m_destId);
146 
147  i.Write (m_data.PeekData (), m_data.GetSize ());
148 }
149 
151 {
153 
154  m_nextHeader = i.ReadU8 ();
155  m_messageType = i.ReadU8 ();
156  m_sourceId = i.ReadU16 ();
157  m_destId = i.ReadU16 ();
158  m_payloadLen = i.ReadU16 ();
159 
160  uint32_t dataLength = GetPayloadLength ();
161  uint8_t data[dataLength];
162  i.Read (data, dataLength);
163 
164  if (dataLength > m_data.GetSize ())
165  {
166  m_data.AddAtEnd (dataLength - m_data.GetSize ());
167  }
168  else
169  {
170  m_data.RemoveAtEnd (m_data.GetSize () - dataLength);
171  }
172 
173  i = m_data.Begin ();
174  i.Write (data, dataLength);
175 
176  return GetSerializedSize ();
177 }
178 
179 DsrOptionField::DsrOptionField (uint32_t optionsOffset)
180  : m_optionData (0),
181  m_optionsOffset (optionsOffset)
182 {
183 }
184 
186 {
187 }
188 
190 {
191  DsrOptionHeader::Alignment align = {4,0};
192  return m_optionData.GetSize () + CalculatePad (align);
193 }
194 
196 {
197  start.Write (m_optionData.Begin (), m_optionData.End ());
198  DsrOptionHeader::Alignment align = {4,0};
199  uint32_t fill = CalculatePad (align);
200  NS_LOG_LOGIC ("fill with " << fill << " bytes padding");
201  switch (fill)
202  {
203  case 0:
204  return;
205  case 1:
207  return;
208  default:
210  return;
211  }
212 }
213 
215 {
216  uint8_t buf[length];
217  start.Read (buf, length);
218  m_optionData = Buffer ();
219  m_optionData.AddAtEnd (length);
220  m_optionData.Begin ().Write (buf, length);
221  return length;
222 }
223 
225 {
227 
228  uint32_t pad = CalculatePad (option.GetAlignment ());
229  NS_LOG_LOGIC ("need " << pad << " bytes padding");
230  switch (pad)
231  {
232  case 0:
233  break; // no padding needed
234  case 1:
236  break;
237  default:
239  break;
240  }
241 
244  it.Prev (option.GetSerializedSize ());
245  option.Serialize (it);
246 }
247 
249 {
250  return (alignment.offset - (m_optionData.GetSize () + m_optionsOffset)) % alignment.factor;
251 }
252 
254 {
255  return m_optionsOffset;
256 }
257 
259 {
260  return m_optionData;
261 }
262 
264 
266 {
267  static TypeId tid = TypeId ("ns3::DsrRoutingHeader")
269  .SetParent<DsrFsHeader> ()
270  ;
271  return tid;
272 }
273 
275 {
276  return GetTypeId ();
277 }
278 
280  : DsrOptionField (8)
281 {
282 }
283 
285 {
286 }
287 
288 void DsrRoutingHeader::Print (std::ostream &os) const
289 {
290  os
291  << " nextHeader: " << (uint32_t)GetNextHeader () << " messageType: " << (uint32_t)GetMessageType ()
292  << " sourceId: " << (uint32_t)GetSourceId () << " destinationId: " << (uint32_t)GetDestId ()
293  << " length: " << (uint32_t)GetPayloadLength ();
294 }
295 
297 {
298  // 8 bytes is the DsrFsHeader length
299  return 8 + DsrOptionField::GetSerializedSize ();
300 }
301 
303 {
305 
306  i.WriteU8 (GetNextHeader ());
307  i.WriteU8 (GetMessageType ());
308  i.WriteU16 (GetSourceId ());
309  i.WriteU16 (GetDestId ());
310  i.WriteU16 (GetPayloadLength ());
311 
313 }
314 
316 {
318 
319  SetNextHeader (i.ReadU8 ());
320  SetMessageType (i.ReadU8 ());
321  SetSourceId (i.ReadU16 ());
322  SetDestId (i.ReadU16 ());
323  SetPayloadLength (i.ReadU16 ());
324 
326 
327  return GetSerializedSize ();
328 }
329 
330 } /* namespace dsr */
331 } /* namespace ns3 */
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::Buffer::Iterator::Write
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:954
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::dsr::DsrOptionHeader::Alignment
represents the alignment requirements of an option header
Definition: dsr-option-header.h:58
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::dsr::DsrFsHeader::GetTypeId
static TypeId GetTypeId()
Get the type identificator.
Definition: dsr-fs-header.cc:45
ns3::Buffer::AddAtEnd
void AddAtEnd(uint32_t end)
Definition: buffer.cc:354
ns3::dsr::DsrFsHeader::GetDestId
uint16_t GetDestId() const
brief Get the dest ID of the header.
Definition: dsr-fs-header.cc:119
ns3::dsr::DsrFsHeader::m_destId
uint16_t m_destId
The destination node id.
Definition: dsr-fs-header.h:192
ns3::dsr::DsrOptionField::AddDsrOption
void AddDsrOption(DsrOptionHeader const &option)
Serialize the option, prepending pad1 or padn option as necessary.
Definition: dsr-fs-header.cc:224
ns3::dsr::DsrFsHeader::SetMessageType
void SetMessageType(uint8_t messageType)
brief Set the message type of the header.
Definition: dsr-fs-header.cc:84
ns3::dsr::DsrRoutingHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: dsr-fs-header.cc:302
ns3::Buffer::Iterator::Read
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1124
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
dsr-fs-header.h
ns3::dsr::DsrFsHeader::GetNextHeader
uint8_t GetNextHeader() const
Get the next header.
Definition: dsr-fs-header.cc:79
ns3::dsr::DsrRoutingHeader::GetSerializedSize
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Definition: dsr-fs-header.cc:296
ns3::dsr::DsrOptionField::m_optionData
Buffer m_optionData
Data payload.
Definition: dsr-fs-header.h:265
ns3::Buffer::GetSize
uint32_t GetSize(void) const
Definition: buffer.h:1063
ns3::dsr::DsrFsHeader::DsrFsHeader
DsrFsHeader()
Constructor.
Definition: dsr-fs-header.cc:60
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8(void)
Definition: buffer.h:1021
ns3::dsr::DsrRoutingHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
Definition: dsr-fs-header.cc:274
ns3::dsr::DsrRoutingHeader::DsrRoutingHeader
DsrRoutingHeader()
Constructor.
Definition: dsr-fs-header.cc:279
ns3::dsr::DsrFsHeader::SetNextHeader
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
Definition: dsr-fs-header.cc:74
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition: buffer.h:869
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition: buffer.cc:871
ns3::dsr::DsrFsHeader
Dsr fixed size header Format.
Definition: dsr-fs-header.h:80
ns3::Buffer::End
Buffer::Iterator End(void) const
Definition: buffer.h:1075
ns3::dsr::DsrRoutingHeader::~DsrRoutingHeader
virtual ~DsrRoutingHeader()
Destructor.
Definition: dsr-fs-header.cc:284
ns3::dsr::DsrFsHeader::GetMessageType
uint8_t GetMessageType() const
brief Get the message type of the header.
Definition: dsr-fs-header.cc:89
ns3::dsr::DsrOptionField
Option field for an DsrFsHeader Enables adding options to an DsrFsHeader.
Definition: dsr-fs-header.h:210
ns3::dsr::DsrOptionPad1Header
Header of Dsr Option Pad1.
Definition: dsr-option-header.h:150
ns3::dsr::DsrFsHeader::~DsrFsHeader
virtual ~DsrFsHeader()
Destructor.
Definition: dsr-fs-header.cc:70
ns3::dsr::DsrOptionField::Deserialize
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Definition: dsr-fs-header.cc:214
ns3::dsr::DsrFsHeader::m_payloadLen
uint16_t m_payloadLen
The "payload length" field.
Definition: dsr-fs-header.h:184
ns3::dsr::DsrOptionField::~DsrOptionField
~DsrOptionField()
Destructor.
Definition: dsr-fs-header.cc:185
ns3::dsr::DsrOptionHeader::Alignment::offset
uint8_t offset
Offset.
Definition: dsr-option-header.h:60
visualizer.core.start
def start()
Definition: core.py:1855
ns3::dsr::DsrRoutingHeader
Header of Dsr Routing.
Definition: dsr-fs-header.h:278
ns3::dsr::DsrOptionHeader::Alignment::factor
uint8_t factor
Factor.
Definition: dsr-option-header.h:59
data
uint8_t data[writeSize]
Definition: socket-bound-tcp-static-routing.cc:53
ns3::dsr::DsrFsHeader::m_messageType
uint8_t m_messageType
The type of the message.
Definition: dsr-fs-header.h:180
ns3::dsr::DsrFsHeader::GetSourceId
uint16_t GetSourceId() const
brief Get the source ID of the header.
Definition: dsr-fs-header.cc:109
ns3::dsr::DsrOptionField::DsrOptionField
DsrOptionField(uint32_t optionsOffset)
Constructor.
Definition: dsr-fs-header.cc:179
ns3::dsr::DsrOptionHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: dsr-option-header.cc:104
ns3::dsr::DsrFsHeader::m_nextHeader
uint8_t m_nextHeader
The "next header" field.
Definition: dsr-fs-header.h:176
ns3::Buffer
automatically resized byte buffer
Definition: buffer.h:93
ns3::dsr::DsrFsHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: dsr-fs-header.cc:137
ns3::Buffer::PeekData
uint8_t const * PeekData(void) const
Definition: buffer.cc:710
ns3::dsr::DsrRoutingHeader::GetTypeId
static TypeId GetTypeId()
Get the type identificator.
Definition: dsr-fs-header.cc:265
ns3::dsr::DsrFsHeader::GetPayloadLength
uint16_t GetPayloadLength() const
Get the payload length of the header.
Definition: dsr-fs-header.cc:99
ns3::Buffer::Begin
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
ns3::dsr::DsrOptionField::CalculatePad
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
Definition: dsr-fs-header.cc:248
ns3::dsr::DsrOptionPadnHeader
Header of Dsr Option Padn.
Definition: dsr-option-header.h:198
ns3::dsr::DsrFsHeader::Print
virtual void Print(std::ostream &os) const
Print some information about the packet.
Definition: dsr-fs-header.cc:124
ns3::dsr::DsrFsHeader::m_sourceId
uint16_t m_sourceId
The source node id.
Definition: dsr-fs-header.h:188
ns3::dsr::DsrOptionField::GetDsrOptionBuffer
Buffer GetDsrOptionBuffer()
Get the buffer.
Definition: dsr-fs-header.cc:258
ns3::Buffer::Iterator::ReadU16
uint16_t ReadU16(void)
Definition: buffer.h:1029
ns3::Header::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
ns3::dsr::DsrFsHeader::m_data
Buffer m_data
The data of the extension.
Definition: dsr-fs-header.h:196
ns3::dsr::DsrOptionField::Serialize
void Serialize(Buffer::Iterator start) const
Serialize all added options.
Definition: dsr-fs-header.cc:195
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition: buffer.h:99
ns3::Buffer::RemoveAtEnd
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:488
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
ns3::dsr::DsrFsHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
Definition: dsr-fs-header.cc:55
ns3::dsr::DsrOptionField::m_optionsOffset
uint32_t m_optionsOffset
Offset.
Definition: dsr-fs-header.h:269
ns3::dsr::DsrOptionHeader
Header for Dsr Options.
Definition: dsr-option-header.h:51
ns3::dsr::DsrFsHeader::SetPayloadLength
void SetPayloadLength(uint16_t length)
brief Set the payload length of the header.
Definition: dsr-fs-header.cc:94
ns3::dsr::DsrOptionPad1Header::Serialize
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: dsr-option-header.cc:172
ns3::dsr::DsrFsHeader::SetSourceId
void SetSourceId(uint16_t sourceId)
brief Set the source ID of the header.
Definition: dsr-fs-header.cc:104
ns3::dsr::DsrRoutingHeader::Print
virtual void Print(std::ostream &os) const
Print some information about the packet.
Definition: dsr-fs-header.cc:288
ns3::dsr::DsrOptionPadnHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: dsr-option-header.cc:226
ns3::TypeId::AddConstructor
TypeId AddConstructor(void)
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:638
ns3::dsr::DsrOptionHeader::GetAlignment
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
Definition: dsr-option-header.cc:130
ns3::dsr::DsrOptionHeader::GetSerializedSize
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Definition: dsr-option-header.cc:99
ns3::dsr::DsrFsHeader::GetSerializedSize
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Definition: dsr-fs-header.cc:132
ns3::Buffer::Iterator::Prev
void Prev(void)
go backward by one byte
Definition: buffer.h:851
ns3::dsr::DsrOptionField::GetSerializedSize
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Definition: dsr-fs-header.cc:189
ns3::dsr::DsrOptionField::GetDsrOptionsOffset
uint32_t GetDsrOptionsOffset()
Get the offset where the options begin, measured from the start of the extension header.
Definition: dsr-fs-header.cc:253
ns3::dsr::DsrFsHeader::SetDestId
void SetDestId(uint16_t destId)
brief Set the dest ID of the header.
Definition: dsr-fs-header.cc:114