A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
dsr-routing-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 Yufei Cheng
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Yufei Cheng <yfcheng@ittc.ku.edu>
7 * Modified by: Tommaso Pecorella <tommaso.pecorella@unifi.it>
8 * Lorenzo Bartolini <l.bartolini02@gmail.com>
9 *
10 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
11 * ResiliNets Research Group https://resilinets.org/
12 * Information and Telecommunication Technology Center (ITTC)
13 * and Department of Electrical Engineering and Computer Science
14 * The University of Kansas Lawrence, KS USA.
15 *
16 * Work supported in part by NSF FIND (Future Internet Design) Program
17 * under grant CNS-0626918 (Postmodern Internet Architecture),
18 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
19 * US Department of Defense (DoD), and ITTC at The University of Kansas.
20 */
21
22#include "dsr-routing-header.h"
23
24#include "dsr-options.h"
25#include "dsr-routing.h"
26
27#include "ns3/assert.h"
28#include "ns3/header.h"
29#include "ns3/iana-internet-protocol-numbers.h"
30#include "ns3/log.h"
31
32#include <vector>
33
34namespace ns3
35{
36
37NS_LOG_COMPONENT_DEFINE("DsrRoutingHeader");
38
39namespace dsr
40{
41
43
44TypeId
46{
47 static TypeId tid = TypeId("ns3::DsrRoutingHeader")
49 .SetParent<Header>()
50 .SetGroupName("Dsr");
51 return tid;
52}
53
59
61 : m_optionData(0),
62 m_nextHeader(iana::internetprotocolnumbers::NO_NEXT_HEADER)
63{
64}
65
69
70void
71DsrRoutingHeader::Print(std::ostream& os) const
72{
73 os << " nextHeader: " << (uint32_t)GetNextHeader()
74 << " length: " << (uint32_t)GetPayloadLength();
75}
76
77void
79{
80 m_nextHeader = protocol;
81}
82
83uint8_t
88
91{
92 // 4 bytes is the DsrFsHeader length: 1 byte for next header, 1 byte reserved, 2 bytes for
93 // payload length
94
95 DsrOptionHeader::Alignment align = {4, 0};
96 return 4 + m_optionData.GetSize() + CalculatePad(align);
97}
98
99void
101{
102 Buffer::Iterator i = start;
103
105 i.WriteU8(0); // reserved field
107
109}
110
113{
114 Buffer::Iterator i = start;
115
117 i.ReadU8(); // reserved field
118 uint16_t payloadLength = i.ReadNtohU16();
119
120 DeserializeOptions(i, payloadLength);
121
122 return GetSerializedSize();
123}
124
125uint16_t
127{
128 // Return the actual size of the options field including padding
129 // This is the correct value for the Payload Length field per RFC 4728
130 return static_cast<uint16_t>(GetSerializedSize() - 4);
131}
132
133void
135{
136 start.Write(m_optionData.Begin(), m_optionData.End());
137 DsrOptionHeader::Alignment align = {4, 0};
138 uint32_t fill = CalculatePad(align);
139 NS_LOG_LOGIC("fill with " << fill << " bytes padding");
140 switch (fill)
141 {
142 case 0:
143 return;
144 case 1:
146 return;
147 default:
148 DsrOptionPadnHeader(fill).Serialize(start);
149 return;
150 }
151}
152
155{
156 std::vector<uint8_t> buf(length);
157 start.Read(buf.data(), length);
159 m_optionData.AddAtEnd(length);
160 m_optionData.Begin().Write(buf.data(), buf.size());
161 return length;
162}
163
164void
166{
168
169 uint32_t pad = CalculatePad(option.GetAlignment());
170 NS_LOG_LOGIC("need " << pad << " bytes padding");
171 switch (pad)
172 {
173 case 0:
174 break; // no padding needed
175 case 1:
177 break;
178 default:
180 break;
181 }
182
183 m_optionData.AddAtEnd(option.GetSerializedSize());
185 it.Prev(option.GetSerializedSize());
186 option.Serialize(it);
187}
188
191{
192 return 4;
193}
194
197{
198 return (alignment.offset - (m_optionData.GetSize() + 4)) % alignment.factor;
199}
200
201} /* namespace dsr */
202} /* namespace ns3 */
iterator in a Buffer instance
Definition buffer.h:109
void WriteU8(uint8_t data)
Definition buffer.h:944
void WriteHtonU16(uint16_t data)
Definition buffer.h:978
uint16_t ReadNtohU16()
Definition buffer.h:1017
void Prev()
go backward by one byte
Definition buffer.h:923
automatically resized byte buffer
Definition buffer.h:103
a unique identifier for an interface.
Definition type-id.h:50
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition type-id.h:719
header for Dsr Options.
virtual Alignment GetAlignment() const
Get the Alignment requirement of this option header.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Header of Dsr Option Pad1.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Header of Dsr Option Padn.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Dsr fixed size header Format.
void SetNextHeader(uint8_t protocol)
Set the "Next header" field.
void AddDsrOption(const DsrOptionHeader &option)
Serialize the option, prepending pad1 or padn option as necessary.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
uint16_t GetPayloadLength() const
Get the payload length of the header.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint32_t DeserializeOptions(Buffer::Iterator start, uint32_t length)
Deserialize the packet.
~DsrRoutingHeader() override
Destructor.
uint32_t CalculatePad(DsrOptionHeader::Alignment alignment) const
Calculate padding.
uint32_t GetDsrOptionsOffset() const
Get the offset where the options begin, measured from the start of the extension header.
uint8_t GetNextHeader() const
Get the next header.
void Print(std::ostream &os) const override
Print some information about the packet.
uint8_t m_nextHeader
The "next header" field.
void SerializeOptions(Buffer::Iterator start) const
Serialize all added options.
static TypeId GetTypeId()
Get the type identificator.
TypeId GetInstanceTypeId() const override
Get the instance type ID.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Buffer m_optionData
Data payload.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#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.
Represents the alignment requirements of an option header.