A Discrete-Event Network Simulator
API
ipv6-option-header.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2009 Strasbourg University
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: David Gross <gdavid.devel@gmail.com>
18  */
19 
20 #include "ipv6-option-header.h"
21 
22 #include "ns3/assert.h"
23 #include "ns3/header.h"
24 #include "ns3/log.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("Ipv6OptionHeader");
30 
31 NS_OBJECT_ENSURE_REGISTERED(Ipv6OptionHeader);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId("ns3::Ipv6OptionHeader")
38  .SetParent<Header>()
39  .SetGroupName("Internet");
40  return tid;
41 }
42 
43 TypeId
45 {
46  return GetTypeId();
47 }
48 
50  : m_type(0),
51  m_length(0)
52 {
53 }
54 
56 {
57 }
58 
59 void
61 {
62  m_type = type;
63 }
64 
65 uint8_t
67 {
68  return m_type;
69 }
70 
71 void
73 {
74  m_length = length;
75 }
76 
77 uint8_t
79 {
80  return m_length;
81 }
82 
83 void
84 Ipv6OptionHeader::Print(std::ostream& os) const
85 {
86  os << "( type = " << (uint32_t)m_type << " )";
87 }
88 
89 uint32_t
91 {
92  return m_length + 2;
93 }
94 
95 void
97 {
99 
100  i.WriteU8(m_type);
101  i.WriteU8(m_length);
102 
103  i.Write(m_data.Begin(), m_data.End());
104 }
105 
106 uint32_t
108 {
110 
111  m_type = i.ReadU8();
112  m_length = i.ReadU8();
113 
114  m_data = Buffer();
116  Buffer::Iterator dataStart = i;
117  i.Next(m_length);
118  Buffer::Iterator dataEnd = i;
119  m_data.Begin().Write(dataStart, dataEnd);
120 
121  return GetSerializedSize();
122 }
123 
126 {
127  return (Alignment){1, 0};
128 }
129 
131 
132 TypeId
134 {
135  static TypeId tid = TypeId("ns3::Ipv6OptionPad1Header")
137  .SetParent<Ipv6OptionHeader>()
138  .SetGroupName("Internet");
139  return tid;
140 }
141 
142 TypeId
144 {
145  return GetTypeId();
146 }
147 
149 {
150  SetType(0);
151 }
152 
154 {
155 }
156 
157 void
158 Ipv6OptionPad1Header::Print(std::ostream& os) const
159 {
160  os << "( type = " << (uint32_t)GetType() << " )";
161 }
162 
163 uint32_t
165 {
166  return 1;
167 }
168 
169 void
171 {
173 
174  i.WriteU8(GetType());
175 }
176 
177 uint32_t
179 {
181 
182  SetType(i.ReadU8());
183 
184  return GetSerializedSize();
185 }
186 
188 
189 TypeId
191 {
192  static TypeId tid = TypeId("ns3::Ipv6OptionPadnHeader")
194  .SetParent<Ipv6OptionHeader>()
195  .SetGroupName("Internet");
196  return tid;
197 }
198 
199 TypeId
201 {
202  return GetTypeId();
203 }
204 
206 {
207  SetType(1);
208  NS_ASSERT_MSG(pad >= 2, "PadN must be at least 2 bytes long");
209  SetLength(pad - 2);
210 }
211 
213 {
214 }
215 
216 void
217 Ipv6OptionPadnHeader::Print(std::ostream& os) const
218 {
219  os << "( type = " << (uint32_t)GetType() << " length = " << (uint32_t)GetLength() << " )";
220 }
221 
222 uint32_t
224 {
225  return GetLength() + 2;
226 }
227 
228 void
230 {
232 
233  i.WriteU8(GetType());
234  i.WriteU8(GetLength());
235 
236  for (int padding = 0; padding < GetLength(); padding++)
237  {
238  i.WriteU8(0);
239  }
240 }
241 
242 uint32_t
244 {
246 
247  SetType(i.ReadU8());
248  SetLength(i.ReadU8());
249 
250  return GetSerializedSize();
251 }
252 
254 
255 TypeId
257 {
258  static TypeId tid = TypeId("ns3::Ipv6OptionJumbogramHeader")
260  .SetParent<Ipv6OptionHeader>()
261  .SetGroupName("Internet");
262  return tid;
263 }
264 
265 TypeId
267 {
268  return GetTypeId();
269 }
270 
272 {
273  SetType(0xC2);
274  SetLength(4);
275  m_dataLength = 0;
276 }
277 
279 {
280 }
281 
282 void
284 {
285  m_dataLength = dataLength;
286 }
287 
288 uint32_t
290 {
291  return m_dataLength;
292 }
293 
294 void
295 Ipv6OptionJumbogramHeader::Print(std::ostream& os) const
296 {
297  os << "( type = " << (uint32_t)GetType() << " length = " << (uint32_t)GetLength()
298  << " data length = " << (uint32_t)m_dataLength << " )";
299 }
300 
301 uint32_t
303 {
304  return GetLength() + 2;
305 }
306 
307 void
309 {
311 
312  i.WriteU8(GetType());
313  i.WriteU8(GetLength());
315 }
316 
317 uint32_t
319 {
321 
322  SetType(i.ReadU8());
323  SetLength(i.ReadU8());
325 
326  return GetSerializedSize();
327 }
328 
331 {
332  return (Alignment){4, 2}; // 4n+2
333 }
334 
336 
337 TypeId
339 {
340  static TypeId tid = TypeId("ns3::Ipv6OptionRouterAlertHeader")
342  .SetParent<Ipv6OptionHeader>()
343  .SetGroupName("Internet");
344  return tid;
345 }
346 
347 TypeId
349 {
350  return GetTypeId();
351 }
352 
354  : m_value(0)
355 {
356  SetType(5);
357  SetLength(2);
358 }
359 
361 {
362 }
363 
364 void
366 {
367  m_value = value;
368 }
369 
370 uint16_t
372 {
373  return m_value;
374 }
375 
376 void
377 Ipv6OptionRouterAlertHeader::Print(std::ostream& os) const
378 {
379  os << "( type = " << (uint32_t)GetType() << " length = " << (uint32_t)GetLength()
380  << " value = " << (uint32_t)m_value << " )";
381 }
382 
383 uint32_t
385 {
386  return GetLength() + 2;
387 }
388 
389 void
391 {
393 
394  i.WriteU8(GetType());
395  i.WriteU8(GetLength());
397 }
398 
399 uint32_t
401 {
403 
404  SetType(i.ReadU8());
405  SetLength(i.ReadU8());
406  m_value = i.ReadNtohU16();
407 
408  return GetSerializedSize();
409 }
410 
413 {
414  return (Alignment){2, 0}; // 2n+0
415 }
416 
417 } /* namespace ns3 */
iterator in a Buffer instance
Definition: buffer.h:100
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
void Write(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:948
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
void WriteHtonU32(uint32_t data)
Definition: buffer.h:933
uint16_t ReadNtohU16()
Definition: buffer.h:954
void Next()
go forward by one byte
Definition: buffer.h:853
automatically resized byte buffer
Definition: buffer.h:94
Buffer::Iterator Begin() const
Definition: buffer.h:1074
void AddAtEnd(uint32_t end)
Definition: buffer.cc:360
Buffer::Iterator End() const
Definition: buffer.h:1081
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
Header for IPv6 Option.
Ipv6OptionHeader()
Constructor.
uint8_t GetLength() const
Get the option length.
uint8_t m_type
The type of the option.
void SetLength(uint8_t length)
Set the option length.
void SetType(uint8_t type)
Set the type of the option.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
void Print(std::ostream &os) const override
Print some information about the packet.
uint8_t GetType() const
Get the type of the option.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
uint8_t m_length
The option length.
~Ipv6OptionHeader() override
Destructor.
static TypeId GetTypeId()
Get the type identificator.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
Buffer m_data
The anonymous data of this option.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Header of IPv6 Option Jumbogram.
static TypeId GetTypeId()
Get the type identificator.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Alignment GetAlignment() const override
Get the Alignment requirement of this option header.
~Ipv6OptionJumbogramHeader() override
Destructor.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void SetDataLength(uint32_t dataLength)
Set the data length.
uint32_t m_dataLength
The data length.
void Print(std::ostream &os) const override
Print some information about the packet.
uint32_t GetDataLength() const
Get the data length.
Header of IPv6 Option Pad1.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Print(std::ostream &os) const override
Print some information about the packet.
static TypeId GetTypeId()
Get the type identificator.
~Ipv6OptionPad1Header() override
Destructor.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of IPv6 Option Padn.
static TypeId GetTypeId()
Get the type identificator.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
Ipv6OptionPadnHeader(uint32_t pad=2)
Constructor.
void Print(std::ostream &os) const override
Print some information about the packet.
~Ipv6OptionPadnHeader() override
Destructor.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of IPv6 Option Router Alert.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
void SetValue(uint16_t value)
Set the field "value".
TypeId GetInstanceTypeId() const override
Get the instance type ID.
static TypeId GetTypeId()
Get the type identificator.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Alignment GetAlignment() const override
Get the Alignment requirement of this option header.
uint16_t GetValue() const
Get the field "value".
void Print(std::ostream &os) const override
Print some information about the packet.
~Ipv6OptionRouterAlertHeader() override
Destructor.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:651
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
represents the alignment requirements of an option header