A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ripng-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 Universita' di Firenze, Italy
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "ripng-header.h"
10
11#include "ns3/log.h"
12
13namespace ns3
14{
15
16/*
17 * RipNgRte
18 */
20
22 : m_prefix("::"),
23 m_tag(0),
24 m_prefixLen(0),
25 m_metric(16)
26{
27}
28
31{
32 static TypeId tid = TypeId("ns3::RipNgRte")
34 .SetGroupName("Internet")
35 .AddConstructor<RipNgRte>();
36 return tid;
37}
38
41{
42 return GetTypeId();
43}
44
45void
46RipNgRte::Print(std::ostream& os) const
47{
48 os << "prefix " << m_prefix << "/" << int(m_prefixLen) << " Metric " << int(m_metric) << " Tag "
49 << int(m_tag);
50}
51
54{
55 return 20;
56}
57
58void
60{
61 uint8_t tmp[16];
62
64 i.Write(tmp, 16);
65
69}
70
73{
74 uint8_t tmp[16];
75
76 i.Read(tmp, 16);
77 m_prefix.Set(tmp);
78 m_tag = i.ReadNtohU16();
79 m_prefixLen = i.ReadU8();
80 m_metric = i.ReadU8();
81
82 return GetSerializedSize();
83}
84
85void
87{
88 m_prefix = prefix;
89}
90
93{
94 return m_prefix;
95}
96
97void
98RipNgRte::SetPrefixLen(uint8_t prefixLen)
99{
100 m_prefixLen = prefixLen;
101}
102
103uint8_t
105{
106 return m_prefixLen;
107}
108
109void
110RipNgRte::SetRouteTag(uint16_t routeTag)
111{
112 m_tag = routeTag;
113}
114
115uint16_t
117{
118 return m_tag;
119}
120
121void
122RipNgRte::SetRouteMetric(uint8_t routeMetric)
123{
124 m_metric = routeMetric;
125}
126
127uint8_t
129{
130 return m_metric;
131}
132
133std::ostream&
134operator<<(std::ostream& os, const RipNgRte& h)
135{
136 h.Print(os);
137 return os;
138}
139
140/*
141 * RipNgHeader
142 */
143NS_LOG_COMPONENT_DEFINE("RipNgHeader");
144NS_OBJECT_ENSURE_REGISTERED(RipNgHeader);
145
147 : m_command(0)
148{
149}
150
151TypeId
153{
154 static TypeId tid = TypeId("ns3::RipNgHeader")
155 .SetParent<Header>()
156 .SetGroupName("Internet")
157 .AddConstructor<RipNgHeader>();
158 return tid;
159}
160
161TypeId
163{
164 return GetTypeId();
165}
166
167void
168RipNgHeader::Print(std::ostream& os) const
169{
170 os << "command " << int(m_command);
171 for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
172 {
173 os << " | ";
174 iter->Print(os);
175 }
176}
177
180{
181 RipNgRte rte;
182 return 4 + m_rteList.size() * rte.GetSerializedSize();
183}
184
185void
187{
188 Buffer::Iterator i = start;
189
190 i.WriteU8(uint8_t(m_command));
191 i.WriteU8(1);
192 i.WriteU16(0);
193
194 for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
195 {
196 iter->Serialize(i);
197 i.Next(iter->GetSerializedSize());
198 }
199}
200
203{
204 Buffer::Iterator i = start;
205
206 uint8_t temp;
207 temp = i.ReadU8();
208 if ((temp == REQUEST) || (temp == RESPONSE))
209 {
210 m_command = temp;
211 }
212 else
213 {
214 return 0;
215 }
216
217 if (i.ReadU8() != 1)
218 {
219 NS_LOG_LOGIC("RIP received a message with mismatch version, ignoring.");
220 return 0;
221 }
222
223 if (i.ReadU16() != 0)
224 {
225 NS_LOG_LOGIC("RIP received a message with invalid filled flags, ignoring.");
226 return 0;
227 }
228
229 uint8_t rteNumber = i.GetRemainingSize() / 20;
230 for (uint8_t n = 0; n < rteNumber; n++)
231 {
232 RipNgRte rte;
233 i.Next(rte.Deserialize(i));
234 m_rteList.push_back(rte);
235 }
236
237 return GetSerializedSize();
238}
239
240void
245
251
252void
254{
255 m_rteList.push_back(rte);
256}
257
258void
260{
261 m_rteList.clear();
262}
263
264uint16_t
266{
267 return m_rteList.size();
268}
269
270std::list<RipNgRte>
272{
273 return m_rteList;
274}
275
276std::ostream&
277operator<<(std::ostream& os, const RipNgHeader& h)
278{
279 h.Print(os);
280 return os;
281}
282
283} // namespace ns3
iterator in a Buffer instance
Definition buffer.h:89
uint32_t GetRemainingSize() const
Definition buffer.cc:1162
void WriteU8(uint8_t data)
Definition buffer.h:870
void Write(const uint8_t *buffer, uint32_t size)
Definition buffer.cc:937
void WriteU16(uint16_t data)
Definition buffer.cc:848
void Read(uint8_t *buffer, uint32_t size)
Definition buffer.cc:1114
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint16_t ReadNtohU16()
Definition buffer.h:943
uint16_t ReadU16()
Definition buffer.h:1024
void Next()
go forward by one byte
Definition buffer.h:842
Protocol header serialization and deserialization.
Definition header.h:33
Describes an IPv6 address.
void Set(const char *address)
Sets an Ipv6Address by parsing the input C-string.
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.
RipNgHeader - see RFC 2080
uint8_t m_command
command type
void SetCommand(Command_e command)
Set the command.
void ClearRtes()
Clear all the RTEs from the header.
uint16_t GetRteNumber() const
Get the number of RTE included in the message.
static TypeId GetTypeId()
Get the type ID.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
std::list< RipNgRte > m_rteList
list of the RTEs in the message
void Print(std::ostream &os) const override
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Command_e
Commands to be used in RipNg headers.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Command_e GetCommand() const
Get the command.
std::list< RipNgRte > GetRteList() const
Get the list of the RTEs included in the message.
void AddRte(RipNgRte rte)
Add a RTE to the message.
RipNg Routing Table Entry (RTE) - see RFC 2080
uint16_t m_tag
route tag
static TypeId GetTypeId()
Get the type ID.
uint8_t m_metric
route metric
Ipv6Address m_prefix
prefix
Ipv6Address GetPrefix() const
Get the prefix.
uint8_t GetRouteMetric() const
Get the route metric.
uint8_t GetPrefixLen() const
Get the prefix length.
uint16_t GetRouteTag() const
Get the route tag.
uint8_t m_prefixLen
prefix length
void SetPrefix(Ipv6Address prefix)
Set the prefix.
void Print(std::ostream &os) const override
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
void SetPrefixLen(uint8_t prefixLen)
Set the prefix length.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
void SetRouteMetric(uint8_t routeMetric)
Set the route metric.
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
void SetRouteTag(uint16_t routeTag)
Set the route tag.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#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_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.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148