A Discrete-Event Network Simulator
API
 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 
46 {
47  static TypeId tid = TypeId ("ns3::dsr::DsrFsHeader")
49  .SetParent<Header> ()
50  ;
51  return tid;
52 }
53 
55 {
56  return GetTypeId ();
57 }
58 
60  : m_nextHeader (0),
61  m_messageType (0),
62  m_payloadLen (0),
63  m_sourceId (0),
64  m_destId (0),
65  m_data (0)
66 {
67 }
68 
70 {
71 }
72 
73 void DsrFsHeader::SetNextHeader (uint8_t protocol)
74 {
75  m_nextHeader = protocol;
76 }
77 
79 {
80  return m_nextHeader;
81 }
82 
83 void DsrFsHeader::SetMessageType (uint8_t messageType)
84 {
85  m_messageType = messageType;
86 }
87 
89 {
90  return m_messageType;
91 }
92 
93 void DsrFsHeader::SetPayloadLength (uint16_t length)
94 {
95  m_payloadLen = length;
96 }
97 
99 {
100  return m_payloadLen;
101 }
102 
103 void DsrFsHeader::SetSourceId (uint16_t sourceId)
104 {
105  m_sourceId = sourceId;
106 }
107 
108 uint16_t DsrFsHeader::GetSourceId () const
109 {
110  return m_sourceId;
111 }
112 
113 void DsrFsHeader::SetDestId (uint16_t destId)
114 {
115  m_destId = destId;
116 }
117 
118 uint16_t DsrFsHeader::GetDestId () const
119 {
120  return m_destId;
121 }
122 
123 void DsrFsHeader::Print (std::ostream &os) const
124 {
125  os
126  << "nextHeader: " << (uint32_t)GetNextHeader () << " messageType: " << (uint32_t)GetMessageType ()
127  << " sourceId: " << (uint32_t)GetSourceId () << " destinationId: " << (uint32_t)GetDestId ()
128  << " length: " << (uint32_t)GetPayloadLength ();
129 }
130 
132 {
133  return 8;
134 }
135 
137 {
139 
140  i.WriteU8 (m_nextHeader);
142  i.WriteU16 (m_sourceId);
143  i.WriteU16 (m_destId);
145 
146  i.Write (m_data.PeekData (), m_data.GetSize ());
147 }
148 
150 {
152 
153  m_nextHeader = i.ReadU8 ();
154  m_messageType = i.ReadU8 ();
155  m_sourceId = i.ReadU16 ();
156  m_destId = i.ReadU16 ();
157  m_payloadLen = i.ReadU16 ();
158 
159  uint32_t dataLength = GetPayloadLength ();
160  uint8_t data[dataLength];
161  i.Read (data, dataLength);
162 
163  if (dataLength > m_data.GetSize ())
164  {
165  m_data.AddAtEnd (dataLength - m_data.GetSize ());
166  }
167  else
168  {
169  m_data.RemoveAtEnd (m_data.GetSize () - dataLength);
170  }
171 
172  i = m_data.Begin ();
173  i.Write (data, dataLength);
174 
175  return GetSerializedSize ();
176 }
177 
178 DsrOptionField::DsrOptionField (uint32_t optionsOffset)
179  : m_optionData (0),
180  m_optionsOffset (optionsOffset)
181 {
182 }
183 
185 {
186 }
187 
189 {
190  DsrOptionHeader::Alignment align = {4,0};
191  return m_optionData.GetSize () + CalculatePad (align);
192 }
193 
195 {
196  start.Write (m_optionData.Begin (), m_optionData.End ());
197  DsrOptionHeader::Alignment align = {4,0};
198  uint32_t fill = CalculatePad (align);
199  NS_LOG_LOGIC ("fill with " << fill << " bytes padding");
200  switch (fill)
201  {
202  case 0:
203  return;
204  case 1:
205  DsrOptionPad1Header ().Serialize (start);
206  return;
207  default:
208  DsrOptionPadnHeader (fill).Serialize (start);
209  return;
210  }
211 }
212 
214 {
215  uint8_t buf[length];
216  start.Read (buf, length);
217  m_optionData = Buffer ();
218  m_optionData.AddAtEnd (length);
219  m_optionData.Begin ().Write (buf, length);
220  return length;
221 }
222 
224 {
226 
227  uint32_t pad = CalculatePad (option.GetAlignment ());
228  NS_LOG_LOGIC ("need " << pad << " bytes padding");
229  switch (pad)
230  {
231  case 0:
232  break; // no padding needed
233  case 1:
235  break;
236  default:
238  break;
239  }
240 
243  it.Prev (option.GetSerializedSize ());
244  option.Serialize (it);
245 }
246 
248 {
249  return (alignment.offset - (m_optionData.GetSize () + m_optionsOffset)) % alignment.factor;
250 }
251 
253 {
254  return m_optionsOffset;
255 }
256 
258 {
259  return m_optionData;
260 }
261 
263  ;
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 */
~DsrOptionField()
Destructor.
uint16_t ReadU16(void)
Definition: buffer.h:845
uint16_t GetPayloadLength() const
Get the payload length of the header.
static TypeId GetTypeId()
Get the type identificator.
TypeId AddConstructor(void)
Definition: type-id.h:418
DsrRoutingHeader()
Constructor.
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:497
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
automatically resized byte buffer
Definition: buffer.h:92
uint32_t m_optionsOffset
Offset.
uint8_t GetMessageType() const
brief Get the message type of the header.
Doxygen introspection did not find any typical Config paths.
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual ~DsrFsHeader()
Destructor.
void SetSourceId(uint16_t sourceId)
brief Set the source ID of the header.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
Dsr fixed size header Format.
Definition: dsr-fs-header.h:79
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
void SetDestId(uint16_t destId)
brief Set the dest ID of the header.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
represents the alignment requirements of an option header
iterator in a Buffer instance
Definition: buffer.h:98
virtual void Print(std::ostream &os) const
Print some informations about the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
uint32_t GetDsrOptionsOffset()
Get the offset where the options begin, measured from the start of the extension header.
uint16_t m_destId
The destination node id.
void Prev(void)
go backward by one byte
Definition: buffer.h:672
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint8_t m_nextHeader
The "next header" field.
uint8_t data[writeSize]
void WriteU16(uint16_t data)
Definition: buffer.cc:895
Buffer::Iterator End(void) const
Definition: buffer.h:881
uint8_t const * PeekData(void) const
Definition: buffer.cc:729
uint16_t m_payloadLen
The "payload length" field.
uint16_t GetSourceId() const
brief Get the source ID of the header.
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Buffer::Iterator Begin(void) const
Definition: buffer.h:875
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
void SetPayloadLength(uint16_t length)
brief Set the payload length of the header.
uint16_t GetDestId() const
brief Get the dest ID of the header.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
DsrFsHeader()
Constructor.
uint8_t GetNextHeader() const
Get the next header.
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1148
uint8_t m_messageType
The type of the message.
uint32_t GetSize(void) const
Definition: buffer.h:869
NS_LOG_COMPONENT_DEFINE("DsrFsHeader")
void SetMessageType(uint8_t messageType)
brief Set the message type of the header.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
bool AddAtEnd(uint32_t end)
Definition: buffer.cc:356
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint16_t m_sourceId
The source node id.
static TypeId GetTypeId()
Get the type identificator.
Buffer m_data
The data of the extension.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
void WriteU8(uint8_t data)
Definition: buffer.h:690
DsrOptionField(uint32_t optionsOffset)
Constructor.
void AddDsrOption(DsrOptionHeader const &option)
Serialize the option, prepending pad1 or padn option as necessary.
Doxygen introspection did not find any typical Config paths.
virtual ~DsrRoutingHeader()
Destructor.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
Doxygen introspection did not find any typical Config paths.
uint8_t ReadU8(void)
Definition: buffer.h:819
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:978
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
Header of Dsr Routing.
a unique identifier for an interface.
Definition: type-id.h:49
Buffer m_optionData
Data payload.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
Buffer GetDsrOptionBuffer()
Get the buffer.