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 */
iterator in a Buffer instance
Definition: buffer.h:99
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:954
void WriteU8(uint8_t data)
Definition: buffer.h:869
void WriteU16(uint16_t data)
Definition: buffer.cc:871
uint16_t ReadU16(void)
Definition: buffer.h:1029
uint8_t ReadU8(void)
Definition: buffer.h:1021
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1124
void Prev(void)
go backward by one byte
Definition: buffer.h:851
automatically resized byte buffer
Definition: buffer.h:93
uint32_t GetSize(void) const
Definition: buffer.h:1063
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:488
void AddAtEnd(uint32_t end)
Definition: buffer.cc:354
Buffer::Iterator End(void) const
Definition: buffer.h:1075
uint8_t const * PeekData(void) const
Definition: buffer.cc:710
Buffer::Iterator Begin(void) const
Definition: buffer.h:1069
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId AddConstructor(void)
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:638
Dsr fixed size header Format.
Definition: dsr-fs-header.h:80
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.
virtual void Print(std::ostream &os) const
Print some information about the packet.
uint16_t m_sourceId
The source node id.
uint16_t GetSourceId() const
brief Get the source ID of the header.
uint16_t GetDestId() const
brief Get the dest ID of the header.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
uint8_t m_messageType
The type of the message.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint16_t m_payloadLen
The "payload length" field.
void SetMessageType(uint8_t messageType)
brief Set the message type of the header.
virtual ~DsrFsHeader()
Destructor.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
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.
Option field for an DsrFsHeader Enables adding options to an DsrFsHeader.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
uint32_t GetDsrOptionsOffset()
Get the offset where the options begin, measured from the start of the extension header.
Buffer m_optionData
Data payload.
Buffer GetDsrOptionBuffer()
Get the buffer.
uint32_t m_optionsOffset
Offset.
void AddDsrOption(DsrOptionHeader const &option)
Serialize the option, prepending pad1 or padn option as necessary.
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.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
Header of Dsr Option Pad1.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Header of Dsr Option Padn.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Header of Dsr Routing.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual ~DsrRoutingHeader()
Destructor.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
static TypeId GetTypeId()
Get the type identificator.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual void Print(std::ostream &os) const
Print some information about the packet.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
def start()
Definition: core.py:1853
uint8_t data[writeSize]
represents the alignment requirements of an option header