A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-extension-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: David Gross <gdavid.devel@gmail.com>
7 */
8
10
11#include "ns3/assert.h"
12#include "ns3/header.h"
13#include "ns3/log.h"
14
15namespace ns3
16{
17
18NS_LOG_COMPONENT_DEFINE("Ipv6ExtensionHeader");
19
20NS_OBJECT_ENSURE_REGISTERED(Ipv6ExtensionHeader);
21
22TypeId
24{
25 static TypeId tid = TypeId("ns3::Ipv6ExtensionHeader")
27 .SetParent<Header>()
28 .SetGroupName("Internet");
29 return tid;
30}
31
37
39 : m_length(0),
40 m_nextHeader(0),
41 m_data(0)
42{
43}
44
48
49void
51{
52 m_nextHeader = nextHeader;
53}
54
55uint8_t
60
61void
63{
64 NS_ASSERT_MSG(!(length & 0x7),
65 "Invalid Ipv6ExtensionHeader Length, must be a multiple of 8 bytes.");
66 NS_ASSERT_MSG(length > 0, "Invalid Ipv6ExtensionHeader Length, must be greater than 0.");
67 NS_ASSERT_MSG(length < 2048, "Invalid Ipv6ExtensionHeader Length, must be a lower than 2048.");
68
69 m_length = (length >> 3) - 1;
70}
71
72uint16_t
74{
75 return (m_length + 1) << 3;
76}
77
78void
79Ipv6ExtensionHeader::Print(std::ostream& os) const
80{
81 os << "( nextHeader = " << (uint32_t)GetNextHeader() << " length = " << (uint32_t)GetLength()
82 << " )";
83}
84
87{
88 return 2;
89}
90
91void
100
103{
104 Buffer::Iterator i = start;
105
106 m_nextHeader = i.ReadU8();
107 m_length = i.ReadU8();
108
109 uint32_t dataLength = GetLength() - 2;
110 auto data = new uint8_t[dataLength];
111 i.Read(data, dataLength);
112
113 if (dataLength > m_data.GetSize())
114 {
115 m_data.AddAtEnd(dataLength - m_data.GetSize());
116 }
117 else
118 {
119 m_data.RemoveAtEnd(m_data.GetSize() - dataLength);
120 }
121
122 i = m_data.Begin();
123 i.Write(data, dataLength);
124
125 delete[] data;
126 return GetSerializedSize();
127}
128
130 : m_optionData(0),
131 m_optionsOffset(optionsOffset)
132{
133}
134
138
144
145void
147{
148 start.Write(m_optionData.Begin(), m_optionData.End());
150 NS_LOG_LOGIC("fill with " << fill << " bytes padding");
151 switch (fill)
152 {
153 case 0:
154 return;
155 case 1:
157 return;
158 default:
159 Ipv6OptionPadnHeader(fill).Serialize(start);
160 return;
161 }
162}
163
166{
167 auto buf = new uint8_t[length];
168 start.Read(buf, length);
170 m_optionData.AddAtEnd(length);
171 m_optionData.Begin().Write(buf, length);
172 delete[] buf;
173 return length;
174}
175
176void
178{
179 NS_LOG_FUNCTION(option);
180
181 uint32_t pad = CalculatePad(option.GetAlignment());
182 NS_LOG_LOGIC("need " << pad << " bytes padding");
183 switch (pad)
184 {
185 case 0:
186 break; // no padding needed
187 case 1:
189 break;
190 default:
192 break;
193 }
194
197 it.Prev(option.GetSerializedSize());
198 option.Serialize(it);
199}
200
203{
204 return (alignment.offset - (m_optionData.GetSize() + m_optionsOffset)) % alignment.factor;
205}
206
209{
210 return m_optionsOffset;
211}
212
213Buffer
218
220
221TypeId
223{
224 static TypeId tid = TypeId("ns3::Ipv6ExtensionHopByHopHeader")
226 .SetParent<Ipv6ExtensionHeader>()
227 .SetGroupName("Internet");
228 return tid;
229}
230
231TypeId
236
241
245
246void
248{
249 os << "( nextHeader = " << (uint32_t)GetNextHeader() << " length = " << (uint32_t)GetLength()
250 << " )";
251}
252
258
259void
268
280
282
283TypeId
285{
286 static TypeId tid = TypeId("ns3::Ipv6ExtensionDestinationHeader")
288 .SetParent<Ipv6ExtensionHeader>()
289 .SetGroupName("Internet");
290 return tid;
291}
292
293TypeId
298
303
307
308void
310{
311 os << "( nextHeader = " << (uint32_t)GetNextHeader() << " length = " << (uint32_t)GetLength()
312 << " )";
313}
314
320
321void
330
342
344
345TypeId
347{
348 static TypeId tid = TypeId("ns3::Ipv6ExtensionFragmentHeader")
350 .SetParent<Ipv6ExtensionHeader>()
351 .SetGroupName("Internet");
352 return tid;
353}
354
355TypeId
360
362 : m_offset(0),
363 m_identification(0)
364{
365 m_length = 0;
366}
367
371
372void
374{
375 // Clear the offset, and save the MF bit
376 m_offset &= 1;
377 m_offset |= offset & (~7);
378}
379
380uint16_t
382{
383 return m_offset & (~1);
384}
385
386void
388{
389 m_offset = moreFragment ? m_offset | 1 : m_offset & (~1);
390}
391
392bool
397
398void
400{
401 m_identification = identification;
402}
403
409
410void
412{
413 os << "( nextHeader = " << (uint32_t)GetNextHeader() << " length = " << (uint32_t)GetLength()
414 << " offset = " << (uint32_t)GetOffset() << " MF = " << (uint32_t)GetMoreFragment()
415 << " identification = " << (uint32_t)m_identification << " )";
416}
417
423
424void
426{
427 Buffer::Iterator i = start;
428
430 // Fragment header does not carry an extension length
431 i.WriteU8(0);
434}
435
438{
439 Buffer::Iterator i = start;
440
442 // Fragment header does not carry an extension length
443 i.ReadU8();
444 m_offset = i.ReadNtohU16();
446
447 return GetSerializedSize();
448}
449
451
452TypeId
454{
455 static TypeId tid = TypeId("ns3::Ipv6ExtensionRoutingHeader")
457 .SetParent<Ipv6ExtensionHeader>()
458 .SetGroupName("Internet");
459 return tid;
460}
461
462TypeId
467
469 : m_typeRouting(0),
470 m_segmentsLeft(0)
471{
472}
473
477
478void
480{
481 m_typeRouting = typeRouting;
482}
483
484uint8_t
489
490void
492{
493 m_segmentsLeft = segmentsLeft;
494}
495
496uint8_t
501
502void
504{
505 os << "( nextHeader = " << (uint32_t)GetNextHeader() << " length = " << (uint32_t)GetLength()
506 << " typeRouting = " << (uint32_t)m_typeRouting
507 << " segmentsLeft = " << (uint32_t)m_segmentsLeft << " )";
508}
509
512{
513 return 4;
514}
515
516void
526
539
541
542TypeId
544{
545 static TypeId tid = TypeId("ns3::Ipv6ExtensionLooseRoutingHeader")
547 .SetParent<Ipv6ExtensionRoutingHeader>()
548 .SetGroupName("Internet");
549 return tid;
550}
551
552TypeId
557
562
566
567void
573
574void
575Ipv6ExtensionLooseRoutingHeader::SetRoutersAddress(std::vector<Ipv6Address> routersAddress)
576{
577 m_routersAddress = routersAddress;
578}
579
580std::vector<Ipv6Address>
585
586void
588{
589 m_routersAddress.at(index) = addr;
590}
591
594{
595 return m_routersAddress.at(index);
596}
597
598void
600{
601 os << "( nextHeader = " << (uint32_t)GetNextHeader() << " length = " << (uint32_t)GetLength()
602 << " typeRouting = " << (uint32_t)GetTypeRouting()
603 << " segmentsLeft = " << (uint32_t)GetSegmentsLeft() << " ";
604
605 for (auto it = m_routersAddress.begin(); it != m_routersAddress.end(); it++)
606 {
607 os << *it << " ";
608 }
609
610 os << " )";
611}
612
615{
616 return 8 + m_routersAddress.size() * 16;
617}
618
619void
621{
622 Buffer::Iterator i = start;
623 uint8_t buff[16];
624
625 uint8_t addressNum = m_routersAddress.size();
626
628 i.WriteU8(addressNum * 2);
631 i.WriteU32(0);
632
633 for (auto it = m_routersAddress.begin(); it != m_routersAddress.end(); it++)
634 {
635 it->Serialize(buff);
636 i.Write(buff, 16);
637 }
638}
639
642{
643 Buffer::Iterator i = start;
644 uint8_t buff[16];
645
647 m_length = i.ReadU8();
650 i.ReadU32();
651
652 uint8_t addressNum = m_length / 2;
653 SetNumberAddress(addressNum);
654 for (uint8_t index = 0; index < addressNum; index++)
655 {
656 i.Read(buff, 16);
657 SetRouterAddress(index, Ipv6Address(buff));
658 }
659
660 return GetSerializedSize();
661}
662
664
665TypeId
667{
668 static TypeId tid = TypeId("ns3::Ipv6ExtensionESPHeader")
670 .SetParent<Ipv6ExtensionHeader>()
671 .SetGroupName("Internet");
672 return tid;
673}
674
675TypeId
680
684
688
689void
690Ipv6ExtensionESPHeader::Print(std::ostream& os) const
691{
692 /** \todo */
693}
694
697{
698 /** \todo */
699 return 0;
700}
701
702void
704{
705 /** \todo */
706}
707
710{
711 /** \todo */
712 return 0;
713}
714
716
717TypeId
719{
720 static TypeId tid = TypeId("ns3::Ipv6ExtensionAHHeader")
722 .SetParent<Ipv6ExtensionHeader>()
723 .SetGroupName("Internet");
724 return tid;
725}
726
727TypeId
732
736
740
741void
742Ipv6ExtensionAHHeader::Print(std::ostream& os) const
743{
744 /** \todo */
745}
746
749{
750 /** \todo */
751 return 0;
752}
753
754void
756{
757 /** \todo */
758}
759
762{
763 /** \todo */
764 return 0;
765}
766
767} /* namespace ns3 */
iterator in a Buffer instance
Definition buffer.h:89
void WriteU32(uint32_t data)
Definition buffer.cc:857
void WriteU8(uint8_t data)
Definition buffer.h:870
void Write(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:937
void Read(uint8_t *buffer, uint32_t size)
Definition buffer.cc:1114
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint32_t ReadNtohU32()
Definition buffer.h:967
uint32_t ReadU32()
Definition buffer.cc:955
void WriteHtonU32(uint32_t data)
Definition buffer.h:922
uint16_t ReadNtohU16()
Definition buffer.h:943
void Prev()
go backward by one byte
Definition buffer.h:849
automatically resized byte buffer
Definition buffer.h:83
void RemoveAtEnd(uint32_t end)
Definition buffer.cc:482
uint32_t GetSize() const
Definition buffer.h:1057
Buffer::Iterator Begin() const
Definition buffer.h:1063
void AddAtEnd(uint32_t end)
Definition buffer.cc:349
Buffer::Iterator End() const
Definition buffer.h:1070
const uint8_t * PeekData() const
Definition buffer.cc:692
Describes an IPv6 address.
Header of IPv6 Extension AH.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
~Ipv6ExtensionAHHeader() override
Destructor.
static TypeId GetTypeId()
Get the type identificator.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void Print(std::ostream &os) const override
Print some information about the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of IPv6 Extension Destination.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
~Ipv6ExtensionDestinationHeader() override
Destructor.
void Print(std::ostream &os) const override
Print some information about the packet.
static TypeId GetTypeId()
Get the type identificator.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
Header of IPv6 Extension ESP.
static TypeId GetTypeId()
Get the type identificator.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void Print(std::ostream &os) const override
Print some information about the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
~Ipv6ExtensionESPHeader() override
Destructor.
Header of IPv6 Extension Fragment.
static TypeId GetTypeId()
Get the type identificator.
void SetIdentification(uint32_t identification)
Set the "Identification" field.
void SetOffset(uint16_t offset)
Set the "Offset" field.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint32_t m_identification
Identifier of the packet.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Print(std::ostream &os) const override
Print some information about the packet.
uint16_t m_offset
Offset of the fragment and More Fragment bit.
uint16_t GetOffset() const
Get the field "Offset".
bool GetMoreFragment() const
Get the status of "More Fragment" bit.
uint32_t GetIdentification() const
Get the field "Identification".
~Ipv6ExtensionFragmentHeader() override
Destructor.
void SetMoreFragment(bool moreFragment)
Set the status of "More Fragment" bit.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header for IPv6 Extension.
void Print(std::ostream &os) const override
Print some information about the packet.
uint8_t m_nextHeader
The "next header" field.
uint16_t GetLength() const
Get the length of the extension.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
static TypeId GetTypeId()
Get the type identificator.
void SetLength(uint16_t length)
brief Set the length of the extension.
Buffer m_data
The data of the extension.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
~Ipv6ExtensionHeader() override
Destructor.
void SetNextHeader(uint8_t nextHeader)
Set the "Next header" field.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
uint8_t GetNextHeader() const
Get the next header.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint8_t m_length
The "length" field.
Header of IPv6 Extension "Hop by Hop".
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
static TypeId GetTypeId()
Get the type identificator.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
~Ipv6ExtensionHopByHopHeader() override
Destructor.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void Print(std::ostream &os) const override
Print some information about the packet.
Header of IPv6 Extension Routing : Type 0 (Loose Routing)
void Print(std::ostream &os) const override
Print some information about the packet.
static TypeId GetTypeId()
Get the type identificator.
std::vector< Ipv6Address > GetRoutersAddress() const
Get the vector of routers' address.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void SetRoutersAddress(std::vector< Ipv6Address > routersAddress)
Set the vector of routers' address.
VectorIpv6Address_t m_routersAddress
The vector of Routers' IPv6 Address.
Ipv6Address GetRouterAddress(uint8_t index) const
Get a Router IPv6 Address.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void SetRouterAddress(uint8_t index, Ipv6Address addr)
Set a Router IPv6 Address.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
void SetNumberAddress(uint8_t n)
Set the number of routers' address.
Header of IPv6 Extension Routing.
void SetTypeRouting(uint8_t typeRouting)
Set the "Type of Routing" field.
uint8_t GetTypeRouting() const
Get the field "Type of Routing".
uint8_t GetSegmentsLeft() const
Get the field "Segments left".
static TypeId GetTypeId()
Get the type identificator.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
void SetSegmentsLeft(uint8_t segmentsLeft)
Set the "Segments left" field.
uint8_t m_segmentsLeft
Number of left segments.
void Print(std::ostream &os) const override
Print some information about the packet.
~Ipv6ExtensionRoutingHeader() override
Destructor.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Header for IPv6 Option.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Header of IPv6 Option Pad1.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of IPv6 Option Padn.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Option field for an IPv6ExtensionHeader.
uint32_t GetSerializedSize() const
Get the serialized size of the packet.
uint32_t CalculatePad(Ipv6OptionHeader::Alignment alignment) const
Calculate padding.
void Serialize(Buffer::Iterator start) const
Serialize all added options.
uint32_t GetOptionsOffset() const
Get the offset where the options begin, measured from the start of the extension header.
uint32_t Deserialize(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
Buffer GetOptionBuffer()
Get the buffer.
Buffer m_optionData
Data payload.
OptionField(uint32_t optionsOffset)
Constructor.
uint32_t m_optionsOffset
Offset.
void AddOption(const Ipv6OptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
a unique identifier for an interface.
Definition type-id.h:48
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition type-id.h:670
#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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]
represents the alignment requirements of an option header