A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ipv6-extension-header.cc
Go to the documentation of this file.
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-extension-header.h"
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE ("Ipv6ExtensionHeader")
30  ;
31 
32 NS_OBJECT_ENSURE_REGISTERED (Ipv6ExtensionHeader)
33  ;
34 
36 {
37  static TypeId tid = TypeId ("ns3::Ipv6ExtensionHeader")
39  .SetParent<Header> ()
40  ;
41  return tid;
42 }
43 
45 {
46  return GetTypeId ();
47 }
48 
50  : m_nextHeader (0),
51  m_length (0),
52  m_data (0)
53 {
54 }
55 
57 {
58 }
59 
60 void Ipv6ExtensionHeader::SetNextHeader (uint8_t nextHeader)
61 {
62  m_nextHeader = nextHeader;
63 }
64 
66 {
67  return m_nextHeader;
68 }
69 
70 void Ipv6ExtensionHeader::SetLength (uint16_t length)
71 {
72  m_length = (length >> 3) - 1;
73 }
74 
76 {
77  return (m_length + 1) << 3;
78 }
79 
80 void Ipv6ExtensionHeader::Print (std::ostream &os) const
81 {
82  os << "( nextHeader = " << (uint32_t)GetNextHeader () << " length = " << (uint32_t)GetLength () << " )";
83 }
84 
86 {
87  return 2;
88 }
89 
91 {
93 
95  i.WriteU8 (m_length);
96  i.Write (m_data.PeekData (), m_data.GetSize ());
97 }
98 
100 {
102 
103  m_nextHeader = i.ReadU8 ();
104  m_length = i.ReadU8 ();
105 
106  uint32_t dataLength = GetLength () - 2;
107  uint8_t* data = new uint8_t[dataLength];
108  i.Read (data, dataLength);
109 
110  if (dataLength > m_data.GetSize ())
111  {
112  m_data.AddAtEnd (dataLength - m_data.GetSize ());
113  }
114  else
115  {
116  m_data.RemoveAtEnd (m_data.GetSize () - dataLength);
117  }
118 
119  i = m_data.Begin ();
120  i.Write (data, dataLength);
121 
122  delete[] data;
123  return GetSerializedSize ();
124 }
125 
126 OptionField::OptionField (uint32_t optionsOffset)
127  : m_optionData (0),
128  m_optionsOffset (optionsOffset)
129 {
130 }
131 
133 {
134 }
135 
137 {
139 }
140 
142 {
143  start.Write (m_optionData.Begin (), m_optionData.End ());
144  uint32_t fill = CalculatePad ((Ipv6OptionHeader::Alignment) { 8,0});
145  NS_LOG_LOGIC ("fill with " << fill << " bytes padding");
146  switch (fill)
147  {
148  case 0: return;
149  case 1: Ipv6OptionPad1Header ().Serialize (start);
150  return;
151  default: Ipv6OptionPadnHeader (fill).Serialize (start);
152  return;
153  }
154 }
155 
157 {
158  uint8_t* buf = new uint8_t[length];
159  start.Read (buf, length);
160  m_optionData = Buffer ();
161  m_optionData.AddAtEnd (length);
162  m_optionData.Begin ().Write (buf, length);
163  delete[] buf;
164  return length;
165 }
166 
168 {
170 
171  uint32_t pad = CalculatePad (option.GetAlignment ());
172  NS_LOG_LOGIC ("need " << pad << " bytes padding");
173  switch (pad)
174  {
175  case 0: break; //no padding needed
176  case 1: AddOption (Ipv6OptionPad1Header ());
177  break;
178  default: AddOption (Ipv6OptionPadnHeader (pad));
179  break;
180  }
181 
184  it.Prev (option.GetSerializedSize ());
185  option.Serialize (it);
186 }
187 
189 {
190  return (alignment.offset - (m_optionData.GetSize () + m_optionsOffset)) % alignment.factor;
191 }
192 
194 {
195  return m_optionsOffset;
196 }
197 
199 {
200  return m_optionData;
201 }
202 
203 
205  ;
206 
208 {
209  static TypeId tid = TypeId ("ns3::Ipv6ExtensionHopByHopHeader")
211  .SetParent<Ipv6ExtensionHeader> ()
212  ;
213  return tid;
214 }
215 
217 {
218  return GetTypeId ();
219 }
220 
222  : OptionField (2)
223 {
224 }
225 
227 {
228 }
229 
230 void Ipv6ExtensionHopByHopHeader::Print (std::ostream &os) const
231 {
232  os << "( nextHeader = " << (uint32_t)GetNextHeader () << " length = " << (uint32_t)GetLength () << " )";
233 }
234 
236 {
237  return 2 + OptionField::GetSerializedSize ();
238 }
239 
241 {
243 
244  i.WriteU8 (GetNextHeader ());
245  i.WriteU8 ((GetSerializedSize () >> 3) - 1);
247 }
248 
250 {
252 
253  SetNextHeader (i.ReadU8 ());
254  SetLength ((i.ReadU8 () + 1) << 3);
256 
257  return GetSerializedSize ();
258 }
259 
261  ;
262 
264 {
265  static TypeId tid = TypeId ("ns3::Ipv6ExtensionDestinationHeader")
267  .SetParent<Ipv6ExtensionHeader> ()
268  ;
269  return tid;
270 }
271 
273 {
274  return GetTypeId ();
275 }
276 
278  : OptionField (2)
279 {
280 }
281 
283 {
284 }
285 
286 void Ipv6ExtensionDestinationHeader::Print (std::ostream &os) const
287 {
288  os << "( nextHeader = " << (uint32_t)GetNextHeader () << " length = " << (uint32_t)GetLength () << " )";
289 }
290 
292 {
293  return 2 + OptionField::GetSerializedSize ();
294 }
295 
297 {
299 
300  i.WriteU8 (GetNextHeader ());
301  i.WriteU8 ((GetSerializedSize () >> 3) - 1);
303 }
304 
306 {
308 
309  SetNextHeader (i.ReadU8 ());
310  SetLength ((i.ReadU8 () + 1) << 3);
312 
313  return GetSerializedSize ();
314 }
315 
317  ;
318 
320 {
321  static TypeId tid = TypeId ("ns3::Ipv6ExtensionFragmentHeader")
323  .SetParent<Ipv6ExtensionHeader> ()
324  ;
325  return tid;
326 }
327 
329 {
330  return GetTypeId ();
331 }
332 
334  : m_offset (0),
335  m_identification (0)
336 {
337  SetLength (0);
338 }
339 
341 {
342 }
343 
345 {
346  // Clear the offset, and save the MF bit
347  m_offset &= 1;
348  m_offset |= offset & (~7);
349 }
350 
352 {
353  return m_offset & (~1);
354 }
355 
357 {
358  m_offset = moreFragment ? m_offset | 1 : m_offset & (~1);
359 }
360 
362 {
363  return m_offset & 1;
364 }
365 
367 {
368  m_identification = identification;
369 }
370 
372 {
373  return m_identification;
374 }
375 
376 void Ipv6ExtensionFragmentHeader::Print (std::ostream &os) const
377 {
378  os << "( nextHeader = " << (uint32_t)GetNextHeader () << " length = " << (uint32_t)GetLength ()
379  << " offset = " << (uint32_t)GetOffset () << " MF = " << (uint32_t)GetMoreFragment () << " identification = " << (uint32_t)m_identification << " )";
380 }
381 
383 {
384  return 8;
385 }
386 
388 {
390 
391  i.WriteU8 (GetNextHeader ());
392  i.WriteU8 (0);
395 }
396 
398 {
400 
401  SetNextHeader (i.ReadU8 ());
402  i.ReadU8();
403  SetLength (0);
404  m_offset = i.ReadNtohU16 ();
406 
407  return GetSerializedSize ();
408 }
409 
411  ;
412 
414 {
415  static TypeId tid = TypeId ("ns3::Ipv6ExtensionRoutingHeader")
417  .SetParent<Ipv6ExtensionHeader> ()
418  ;
419  return tid;
420 }
421 
423 {
424  return GetTypeId ();
425 }
426 
428  : m_typeRouting (0),
429  m_segmentsLeft (0)
430 {
431 }
432 
434 {
435 }
436 
438 {
439  m_typeRouting = typeRouting;
440 }
441 
443 {
444  return m_typeRouting;
445 }
446 
448 {
449  m_segmentsLeft = segmentsLeft;
450 }
451 
453 {
454  return m_segmentsLeft;
455 }
456 
457 void Ipv6ExtensionRoutingHeader::Print (std::ostream &os) const
458 {
459  os << "( nextHeader = " << (uint32_t)GetNextHeader () << " length = " << (uint32_t)GetLength ()
460  << " typeRouting = " << (uint32_t)m_typeRouting << " segmentsLeft = " << (uint32_t)m_segmentsLeft << " )";
461 }
462 
464 {
465  return 4;
466 }
467 
469 {
471 
472  i.WriteU8 (GetNextHeader ());
473  i.WriteU8 ((GetLength () >> 3) - 1);
476 }
477 
479 {
481 
482  SetNextHeader (i.ReadU8 ());
483  SetLength ((i.ReadU8 () + 1) << 3);
484  m_typeRouting = i.ReadU8 ();
485  m_segmentsLeft = i.ReadU8 ();
486 
487  return GetSerializedSize ();
488 }
489 
491  ;
492 
494 {
495  static TypeId tid = TypeId ("ns3::Ipv6ExtensionLooseRoutingHeader")
497  .SetParent<Ipv6ExtensionRoutingHeader> ()
498  ;
499  return tid;
500 }
501 
503 {
504  return GetTypeId ();
505 }
506 
508  : m_routersAddress (0)
509 {
510 }
511 
513 {
514 }
515 
517 {
518  m_routersAddress.clear ();
519  m_routersAddress.assign (n, Ipv6Address (""));
520 }
521 
522 void Ipv6ExtensionLooseRoutingHeader::SetRoutersAddress (std::vector<Ipv6Address> routersAddress)
523 {
524  m_routersAddress = routersAddress;
525 }
526 
527 std::vector<Ipv6Address> Ipv6ExtensionLooseRoutingHeader::GetRoutersAddress () const
528 {
529  return m_routersAddress;
530 }
531 
533 {
534  m_routersAddress.at (index) = addr;
535 }
536 
538 {
539  return m_routersAddress.at (index);
540 }
541 
542 void Ipv6ExtensionLooseRoutingHeader::Print (std::ostream &os) const
543 {
544  os << "( nextHeader = " << (uint32_t)GetNextHeader () << " length = " << (uint32_t)GetLength ()
545  << " typeRouting = " << (uint32_t)GetTypeRouting () << " segmentsLeft = " << (uint32_t)GetSegmentsLeft () << " ";
546 
547  for (std::vector<Ipv6Address>::const_iterator it = m_routersAddress.begin (); it != m_routersAddress.end (); it++)
548  {
549  os << *it << " ";
550  }
551 
552  os << " )";
553 }
554 
556 {
557  return 8 + m_routersAddress.size () * 16;
558 }
559 
561 {
563  uint8_t buff[16];
564 
565  i.WriteU8 (GetNextHeader ());
566  i.WriteU8 ((GetLength () >> 3) - 1);
567  i.WriteU8 (GetTypeRouting ());
568  i.WriteU8 (GetSegmentsLeft ());
569  i.WriteU32 (0);
570 
571  for (VectorIpv6Address_t::const_iterator it = m_routersAddress.begin (); it != m_routersAddress.end (); it++)
572  {
573  it->Serialize (buff);
574  i.Write (buff, 16);
575  }
576 }
577 
579 {
581  uint8_t buff[16];
582 
583  SetNextHeader (i.ReadU8 ());
584  SetLength ((i.ReadU8 () + 1) << 3);
585  SetTypeRouting (i.ReadU8 ());
586  SetSegmentsLeft (i.ReadU8 ());
587  i.ReadU32 ();
588 
589  for (std::vector<Ipv6Address>::iterator it = m_routersAddress.begin (); it != m_routersAddress.end (); it++)
590  {
591  i.Read (buff, 16);
592  it->Set (buff);
593  }
594 
595  return GetSerializedSize ();
596 }
597 
599  ;
600 
602 {
603  static TypeId tid = TypeId ("ns3::Ipv6ExtensionESPHeader")
605  .SetParent<Ipv6ExtensionHeader> ()
606  ;
607  return tid;
608 }
609 
611 {
612  return GetTypeId ();
613 }
614 
616 {
617 }
618 
620 {
621 }
622 
623 void Ipv6ExtensionESPHeader::Print (std::ostream &os) const
624 {
626 }
627 
629 {
631  return 0;
632 }
633 
635 {
637 }
638 
640 {
642  return 0;
643 }
644 
646  ;
647 
649 {
650  static TypeId tid = TypeId ("ns3::Ipv6ExtensionAHHeader")
652  .SetParent<Ipv6ExtensionHeader> ()
653  ;
654  return tid;
655 }
656 
658 {
659  return GetTypeId ();
660 }
661 
663 {
664 }
665 
667 {
668 }
669 
670 void Ipv6ExtensionAHHeader::Print (std::ostream &os) const
671 {
673 }
674 
676 {
678  return 0;
679 }
680 
682 {
684 }
685 
687 {
689  return 0;
690 }
691 
692 } /* namespace ns3 */
693 
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
uint8_t m_length
The "length" field.
static TypeId GetTypeId()
Get the type identificator.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
uint32_t ReadU32(void)
Definition: buffer.cc:997
TypeId AddConstructor(void)
Definition: type-id.h:418
void RemoveAtEnd(uint32_t end)
Definition: buffer.cc:497
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Doxygen introspection did not find any typical Config paths.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
Doxygen introspection did not find any typical Config paths.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
uint32_t CalculatePad(Ipv6OptionHeader::Alignment alignment) const
Calculate padding.
Doxygen introspection did not find any typical Config paths.
void SetTypeRouting(uint8_t typeRouting)
Set the "Type of Routing" field.
uint16_t m_offset
Offset of the fragment and More Fragment bit.
automatically resized byte buffer
Definition: buffer.h:92
virtual ~Ipv6ExtensionRoutingHeader()
Destructor.
static TypeId GetTypeId()
Get the type identificator.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Doxygen introspection did not find any typical Config paths.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
Buffer m_optionData
Data payload.
OptionField(uint32_t optionsOffset)
Constructor.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void SetOffset(uint16_t offset)
Set the "Offset" field.
static TypeId GetTypeId()
Get the type identificator.
uint32_t ReadNtohU32(void)
Definition: buffer.h:791
static TypeId GetTypeId()
Get the type identificator.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
represents the alignment requirements of an option header
Doxygen introspection did not find any typical Config paths.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
iterator in a Buffer instance
Definition: buffer.h:98
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
static TypeId GetTypeId()
Get the type identificator.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual ~Ipv6ExtensionDestinationHeader()
Destructor.
uint8_t m_nextHeader
The "next header" field.
Doxygen introspection did not find any typical Config paths.
Option field for an IPv6ExtensionHeader Enables adding options to an IPv6ExtensionHeader.
static TypeId GetTypeId()
Get the type identificator.
void Prev(void)
go backward by one byte
Definition: buffer.h:672
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Doxygen introspection did not find any typical Config paths.
uint8_t data[writeSize]
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual ~Ipv6ExtensionESPHeader()
Destructor.
void WriteHtonU16(uint16_t data)
Definition: buffer.h:726
Buffer::Iterator End(void) const
Definition: buffer.h:881
uint8_t const * PeekData(void) const
Definition: buffer.cc:729
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
VectorIpv6Address_t m_routersAddress
The vector of Routers' IPv6 Address.
void SetMoreFragment(bool moreFragment)
Set the status of "More Fragment" bit.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void SetIdentification(uint32_t identification)
Set the "Identification" field.
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
Buffer::Iterator Begin(void) const
Definition: buffer.h:875
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
uint32_t m_optionsOffset
Offset.
virtual ~Ipv6ExtensionFragmentHeader()
Destructor.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
void SetNextHeader(uint8_t nextHeader)
Set the "Next header" field.
virtual ~Ipv6ExtensionAHHeader()
Destructor.
Buffer m_data
The data of the extension.
uint32_t GetOptionsOffset()
Get the offset where the options begin, measured from the start of the extension header.
virtual ~Ipv6ExtensionHeader()
Destructor.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1148
Ipv6Address GetRouterAddress(uint8_t index) const
Get a Router IPv6 Address.
uint8_t GetTypeRouting() const
Get the field "Type of Routing".
virtual void Print(std::ostream &os) const
Print some informations about the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
void WriteHtonU32(uint32_t data)
Definition: buffer.h:745
uint32_t GetSize(void) const
Definition: buffer.h:869
bool GetMoreFragment() const
Get the status of "More Fragment" bit.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
uint32_t m_identification
Identifier of the packet.
uint8_t GetSegmentsLeft() const
Get the field "Segments left".
bool AddAtEnd(uint32_t end)
Definition: buffer.cc:356
uint8_t GetNextHeader() const
Get the next header.
Describes an IPv6 address.
Definition: ipv6-address.h:46
void SetRoutersAddress(std::vector< Ipv6Address > routersAddress)
Set the vector of routers' address.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
void WriteU8(uint8_t data)
Definition: buffer.h:690
void AddOption(Ipv6OptionHeader const &option)
Serialize the option, prepending pad1 or padn option as necessary.
uint32_t GetIdentification() const
Get the field "Identification".
Doxygen introspection did not find any typical Config paths.
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Buffer GetOptionBuffer()
Get the buffer.
Doxygen introspection did not find any typical Config paths.
std::vector< Ipv6Address > GetRoutersAddress() const
Get the vector of routers' address.
uint8_t m_typeRouting
Type of routing.
virtual uint32_t GetSerializedSize() const
Get the serialized size of 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
void SetLength(uint16_t length)
brief Set the length of the extension.
virtual ~Ipv6ExtensionHopByHopHeader()
Destructor.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
static TypeId GetTypeId()
Get the type identificator.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
uint8_t m_segmentsLeft
Number of left segments.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
uint16_t GetOffset() const
Get the field "Offset".
uint16_t GetLength() const
Get the length of the extension.
virtual TypeId GetInstanceTypeId() const
Get the instance type ID.
uint16_t ReadNtohU16(void)
Definition: buffer.h:767
virtual void Print(std::ostream &os) const
Print some informations about the packet.
void WriteU32(uint32_t data)
Definition: buffer.cc:903
void SetRouterAddress(uint8_t index, Ipv6Address addr)
Set a Router IPv6 Address.
virtual uint32_t GetSerializedSize() const
Get the serialized size of the packet.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void SetNumberAddress(uint8_t n)
Set the number of routers' address.
a unique identifier for an interface.
Definition: type-id.h:49
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
Doxygen introspection did not find any typical Config paths.
virtual ~Ipv6ExtensionLooseRoutingHeader()
Destructor.
virtual void Print(std::ostream &os) const
Print some informations about the packet.
static TypeId GetTypeId()
Get the type identificator.
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the "Segments left" field.