A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dsr-fs-header.cc
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 
45 {
46  static TypeId tid = TypeId ("ns3::dsr::DsrFsHeader")
48  .SetParent<Header> ()
49  ;
50  return tid;
51 }
52 
54 {
55  return GetTypeId ();
56 }
57 
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 
69 {
70 }
71 
72 void DsrFsHeader::SetNextHeader (uint8_t protocol)
73 {
74  m_nextHeader = protocol;
75 }
76 
78 {
79  return m_nextHeader;
80 }
81 
82 void DsrFsHeader::SetMessageType (uint8_t messageType)
83 {
84  m_messageType = messageType;
85 }
86 
88 {
89  return m_messageType;
90 }
91 
92 void DsrFsHeader::SetPayloadLength (uint16_t length)
93 {
94  m_payloadLen = length;
95 }
96 
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 
131 {
132  return 8;
133 }
134 
136 {
137  Buffer::Iterator i = start;
138 
139  i.WriteU8 (m_nextHeader);
141  i.WriteU16 (m_sourceId);
142  i.WriteU16 (m_destId);
144 
145  i.Write (m_data.PeekData (), m_data.GetSize ());
146 }
147 
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 
184 {
185 }
186 
188 {
189  DsrOptionHeader::Alignment align = {4,0};
190  return m_optionData.GetSize () + CalculatePad (align);
191 }
192 
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 
223 {
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:
234  break;
235  default:
237  break;
238  }
239 
242  it.Prev (option.GetSerializedSize ());
243  option.Serialize (it);
244 }
245 
247 {
248  return (alignment.offset - (m_optionData.GetSize () + m_optionsOffset)) % alignment.factor;
249 }
250 
252 {
253  return m_optionsOffset;
254 }
255 
257 {
258  return m_optionData;
259 }
260 
261 NS_OBJECT_ENSURE_REGISTERED (DsrRoutingHeader);
262 
264 {
265  static TypeId tid = TypeId ("ns3::DsrRoutingHeader")
267  .SetParent<DsrFsHeader> ()
268  ;
269  return tid;
270 }
271 
273 {
274  return GetTypeId ();
275 }
276 
278  : DsrOptionField (8)
279 {
280 }
281 
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 
295 {
296  // 8 bytes is the DsrFsHeader length
297  return 8 + DsrOptionField::GetSerializedSize ();
298 }
299 
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 
311 }
312 
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 
324 
325  return GetSerializedSize ();
326 }
327 
328 } /* namespace dsr */
329 } /* 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:388
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
Header of Dsr Option Pad1.
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual ~DsrFsHeader()
Destructor.
void SetSourceId(uint16_t sourceId)
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
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)
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)
Definition: buffer.h:672
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint8_t m_nextHeader
The "next header" field.
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
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
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)
uint16_t GetDestId() const
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
void SetMessageType(uint8_t messageType)
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.
Header for Dsr Options.
virtual ~DsrRoutingHeader()
Destructor.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
Header of Dsr Option Padn.
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:44
Buffer m_optionData
Data payload.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
Buffer GetDsrOptionBuffer()
Get the buffer.