A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv6-option-header.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
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: David Gross <gdavid.devel@gmail.com>
19  */
20 
21 #include "ns3/assert.h"
22 #include "ns3/log.h"
23 #include "ns3/header.h"
24 #include "ipv6-option-header.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE ("Ipv6OptionHeader");
30 
31 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionHeader);
32 
34 {
35  static TypeId tid = TypeId ("ns3::Ipv6OptionHeader")
37  .SetParent<Header> ()
38  ;
39  return tid;
40 }
41 
43 {
44  return GetTypeId ();
45 }
46 
48  : m_type (0),
49  m_length (0)
50 {
51 }
52 
54 {
55 }
56 
57 void Ipv6OptionHeader::SetType (uint8_t type)
58 {
59  m_type = type;
60 }
61 
62 uint8_t Ipv6OptionHeader::GetType () const
63 {
64  return m_type;
65 }
66 
67 void Ipv6OptionHeader::SetLength (uint8_t length)
68 {
69  m_length = length;
70 }
71 
72 uint8_t Ipv6OptionHeader::GetLength () const
73 {
74  return m_length;
75 }
76 
77 void Ipv6OptionHeader::Print (std::ostream &os) const
78 {
79  os << "( type = " << (uint32_t)m_type << " )";
80 }
81 
83 {
84  return m_length + 2;
85 }
86 
88 {
89  Buffer::Iterator i = start;
90 
91  i.WriteU8 (m_type);
92  i.WriteU8 (m_length);
93 
94  i.Write (m_data.Begin (), m_data.End ());
95 }
96 
98 {
99  Buffer::Iterator i = start;
100 
101  m_type = i.ReadU8 ();
102  m_length = i.ReadU8 ();
103 
104  m_data = Buffer ();
106  Buffer::Iterator dataStart = i;
107  i.Next (m_length);
108  Buffer::Iterator dataEnd = i;
109  m_data.Begin ().Write (dataStart, dataEnd);
110 
111  return GetSerializedSize ();
112 }
113 
115 {
116  return (Alignment){ 1,0};
117 }
118 
119 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionPad1Header);
120 
122 {
123  static TypeId tid = TypeId ("ns3::Ipv6OptionPad1Header")
125  .SetParent<Ipv6OptionHeader> ()
126  ;
127  return tid;
128 }
129 
131 {
132  return GetTypeId ();
133 }
134 
136 {
137  SetType (0);
138 }
139 
141 {
142 }
143 
144 void Ipv6OptionPad1Header::Print (std::ostream &os) const
145 {
146  os << "( type = " << (uint32_t)GetType () << " )";
147 }
148 
150 {
151  return 1;
152 }
153 
155 {
156  Buffer::Iterator i = start;
157 
158  i.WriteU8 (GetType ());
159 }
160 
162 {
163  Buffer::Iterator i = start;
164 
165  SetType (i.ReadU8 ());
166 
167  return GetSerializedSize ();
168 }
169 
170 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionPadnHeader);
171 
173 {
174  static TypeId tid = TypeId ("ns3::Ipv6OptionPadnHeader")
176  .SetParent<Ipv6OptionHeader> ()
177  ;
178  return tid;
179 }
180 
182 {
183  return GetTypeId ();
184 }
185 
187 {
188  SetType (1);
189  NS_ASSERT_MSG (pad >= 2, "PadN must be at least 2 bytes long");
190  SetLength (pad - 2);
191 }
192 
194 {
195 }
196 
197 void Ipv6OptionPadnHeader::Print (std::ostream &os) const
198 {
199  os << "( type = " << (uint32_t)GetType () << " length = " << (uint32_t)GetLength () << " )";
200 }
201 
203 {
204  return GetLength () + 2;
205 }
206 
208 {
209  Buffer::Iterator i = start;
210 
211  i.WriteU8 (GetType ());
212  i.WriteU8 (GetLength ());
213 
214  for (int padding = 0; padding < GetLength (); padding++)
215  {
216  i.WriteU8 (0);
217  }
218 }
219 
221 {
222  Buffer::Iterator i = start;
223 
224  SetType (i.ReadU8 ());
225  SetLength (i.ReadU8 ());
226 
227  return GetSerializedSize ();
228 }
229 
230 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionJumbogramHeader);
231 
233 {
234  static TypeId tid = TypeId ("ns3::Ipv6OptionJumbogramHeader")
236  .SetParent<Ipv6OptionHeader> ()
237  ;
238  return tid;
239 }
240 
242 {
243  return GetTypeId ();
244 }
245 
247 {
248  SetType (0xC2);
249  SetLength (4);
250 }
251 
253 {
254 }
255 
257 {
258  m_dataLength = dataLength;
259 }
260 
262 {
263  return m_dataLength;
264 }
265 
266 void Ipv6OptionJumbogramHeader::Print (std::ostream &os) const
267 {
268  os << "( type = " << (uint32_t)GetType () << " length = " << (uint32_t)GetLength () << " data length = " << (uint32_t)m_dataLength << " )";
269 }
270 
272 {
273  return GetLength () + 2;
274 }
275 
277 {
278  Buffer::Iterator i = start;
279 
280  i.WriteU8 (GetType ());
281  i.WriteU8 (GetLength ());
283 }
284 
286 {
287  Buffer::Iterator i = start;
288 
289  SetType (i.ReadU8 ());
290  SetLength (i.ReadU8 ());
291  m_dataLength = i.ReadNtohU16 ();
292 
293  return GetSerializedSize ();
294 }
295 
297 {
298  return (Alignment){ 4,2}; //4n+2
299 }
300 
301 NS_OBJECT_ENSURE_REGISTERED (Ipv6OptionRouterAlertHeader);
302 
304 {
305  static TypeId tid = TypeId ("ns3::Ipv6OptionRouterAlertHeader")
307  .SetParent<Ipv6OptionHeader> ()
308  ;
309  return tid;
310 }
311 
313 {
314  return GetTypeId ();
315 }
316 
318  : m_value (0)
319 {
320  SetLength (2);
321 }
322 
324 {
325 }
326 
328 {
329  m_value = value;
330 }
331 
333 {
334  return m_value;
335 }
336 
337 void Ipv6OptionRouterAlertHeader::Print (std::ostream &os) const
338 {
339  os << "( type = " << (uint32_t)GetType () << " length = " << (uint32_t)GetLength () << " value = " << (uint32_t)m_value << " )";
340 }
341 
343 {
344  return GetLength () + 2;
345 }
346 
348 {
349  Buffer::Iterator i = start;
350 
351  i.WriteU8 (GetType ());
352  i.WriteU8 (GetLength ());
353  i.WriteHtonU16 (m_value);
354 }
355 
357 {
358  Buffer::Iterator i = start;
359 
360  SetType (i.ReadU8 ());
361  SetLength (i.ReadU8 ());
362  m_value = i.ReadNtohU16 ();
363 
364  return GetSerializedSize ();
365 }
366 
368 {
369  return (Alignment){ 2,0}; //2n+0
370 }
371 
372 } /* namespace ns3 */
373 
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
TypeId AddConstructor(void)
Definition: type-id.h:388
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void SetType(uint8_t type)
Set the type of the option.
Header for IPv6 Option.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
virtual ~Ipv6OptionPad1Header()
Destructor.
Ipv6OptionPadnHeader(uint32_t pad=2)
Constructor.
automatically resized byte buffer
Definition: buffer.h:92
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
static TypeId GetTypeId()
Get the type identificator.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
uint8_t m_type
The type of the option.
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
Header of IPv6 Option Pad1.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
uint8_t GetType() const
Get the type of the option.
static TypeId GetTypeId()
Get the type identificator.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
represents the alignment requirements of an option header
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
iterator in a Buffer instance
Definition: buffer.h:98
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual ~Ipv6OptionHeader()
Destructor.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
static TypeId GetTypeId()
Get the type identificator.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
Buffer::Iterator End(void) const
Definition: buffer.h:881
void Next(void)
Definition: buffer.h:666
virtual ~Ipv6OptionJumbogramHeader()
Destructor.
Buffer m_data
The anonymous data of this option.
Header of IPv6 Option Jumbogram.
virtual ~Ipv6OptionRouterAlertHeader()
Destructor.
Buffer::Iterator Begin(void) const
Definition: buffer.h:875
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void SetDataLength(uint32_t dataLength)
Set the data length.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
virtual ~Ipv6OptionPadnHeader()
Destructor.
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.
void WriteHtonU32(uint32_t data)
Definition: buffer.h:745
void SetValue(uint16_t value)
Set the field "value".
uint32_t GetDataLength() const
Get the data length.
bool AddAtEnd(uint32_t end)
Definition: buffer.cc:356
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
static TypeId GetTypeId()
Get the type identificator.
void WriteU8(uint8_t data)
Definition: buffer.h:690
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
uint8_t GetLength() const
Get the option length.
uint8_t ReadU8(void)
Definition: buffer.h:819
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:978
Header of IPv6 Option Router Alert.
uint16_t ReadNtohU16(void)
Definition: buffer.h:767
uint16_t GetValue() const
Get the field "value".
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint8_t m_length
The option length.
static TypeId GetTypeId()
Get the type identificator.
a unique identifier for an interface.
Definition: type-id.h:44
void SetLength(uint8_t length)
Set the option length.
Header of IPv6 Option Padn.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
uint32_t m_dataLength
The data length.
Ipv6OptionHeader()
Constructor.