A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
rip-header.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 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 "rip-header.h"
10
11#include "ns3/log.h"
12
13namespace ns3
14{
15
16/*
17 * RipRte
18 */
20
22 : m_tag(0),
23 m_prefix("127.0.0.1"),
24 m_subnetMask("0.0.0.0"),
25 m_nextHop("0.0.0.0"),
26 m_metric(16)
27{
28}
29
32{
33 static TypeId tid =
34 TypeId("ns3::RipRte").SetParent<Header>().SetGroupName("Internet").AddConstructor<RipRte>();
35 return tid;
36}
37
40{
41 return GetTypeId();
42}
43
44void
45RipRte::Print(std::ostream& os) const
46{
47 os << "prefix " << m_prefix << "/" << m_subnetMask.GetPrefixLength() << " Metric "
48 << int(m_metric);
49 os << " Tag " << int(m_tag) << " Next Hop " << m_nextHop;
50}
51
54{
55 return 20;
56}
57
58void
69
72{
73 uint16_t tmp;
74
75 tmp = i.ReadNtohU16();
76 if (tmp != 2)
77 {
78 return 0;
79 }
80
81 m_tag = i.ReadNtohU16();
85
87
88 return GetSerializedSize();
89}
90
91void
93{
94 m_prefix = prefix;
95}
96
99{
100 return m_prefix;
101}
102
103void
105{
106 m_subnetMask = subnetMask;
107}
108
111{
112 return m_subnetMask;
113}
114
115void
116RipRte::SetRouteTag(uint16_t routeTag)
117{
118 m_tag = routeTag;
119}
120
121uint16_t
123{
124 return m_tag;
125}
126
127void
129{
130 m_metric = routeMetric;
131}
132
135{
136 return m_metric;
137}
138
139void
141{
142 m_nextHop = nextHop;
143}
144
147{
148 return m_nextHop;
149}
150
151std::ostream&
152operator<<(std::ostream& os, const RipRte& h)
153{
154 h.Print(os);
155 return os;
156}
157
158/*
159 * RipHeader
160 */
161NS_LOG_COMPONENT_DEFINE("RipHeader");
163
165 : m_command(0)
166{
167}
168
169TypeId
171{
172 static TypeId tid = TypeId("ns3::RipHeader")
173 .SetParent<Header>()
174 .SetGroupName("Internet")
175 .AddConstructor<RipHeader>();
176 return tid;
177}
178
179TypeId
181{
182 return GetTypeId();
183}
184
185void
186RipHeader::Print(std::ostream& os) const
187{
188 os << "command " << int(m_command);
189 for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
190 {
191 os << " | ";
192 iter->Print(os);
193 }
194}
195
198{
199 RipRte rte;
200 return 4 + m_rteList.size() * rte.GetSerializedSize();
201}
202
203void
205{
206 Buffer::Iterator i = start;
207
208 i.WriteU8(uint8_t(m_command));
209 i.WriteU8(2);
210 i.WriteU16(0);
211
212 for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
213 {
214 iter->Serialize(i);
215 i.Next(iter->GetSerializedSize());
216 }
217}
218
221{
222 Buffer::Iterator i = start;
223
224 uint8_t temp;
225 temp = i.ReadU8();
226 if ((temp == REQUEST) || (temp == RESPONSE))
227 {
228 m_command = temp;
229 }
230 else
231 {
232 return 0;
233 }
234
235 if (i.ReadU8() != 2)
236 {
237 NS_LOG_LOGIC("RIP received a message with mismatch version, ignoring.");
238 return 0;
239 }
240
241 if (i.ReadU16() != 0)
242 {
243 NS_LOG_LOGIC("RIP received a message with invalid filled flags, ignoring.");
244 return 0;
245 }
246
247 uint8_t rteNumber = i.GetRemainingSize() / 20;
248 for (uint8_t n = 0; n < rteNumber; n++)
249 {
250 RipRte rte;
251 i.Next(rte.Deserialize(i));
252 m_rteList.push_back(rte);
253 }
254
255 return GetSerializedSize();
256}
257
258void
260{
261 m_command = command;
262}
263
269
270void
272{
273 m_rteList.push_back(rte);
274}
275
276void
278{
279 m_rteList.clear();
280}
281
282uint16_t
284{
285 return m_rteList.size();
286}
287
288std::list<RipRte>
290{
291 return m_rteList;
292}
293
294std::ostream&
295operator<<(std::ostream& os, const RipHeader& h)
296{
297 h.Print(os);
298 return os;
299}
300
301} // 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 WriteU16(uint16_t data)
Definition buffer.cc:848
void WriteHtonU16(uint16_t data)
Definition buffer.h:904
uint32_t ReadNtohU32()
Definition buffer.h:967
void WriteHtonU32(uint32_t data)
Definition buffer.h:922
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
Ipv4 addresses are stored in host order in this class.
void Set(uint32_t address)
input address is in host order.
uint32_t Get() const
Get the host-order 32-bit IP address.
a class to represent an Ipv4 address mask
void Set(uint32_t mask)
input mask is in host order.
uint16_t GetPrefixLength() const
uint32_t Get() const
Get the host-order 32-bit IP mask.
RipHeader - see RFC 2453
Definition rip-header.h:147
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
uint16_t GetRteNumber() const
Get the number of RTE included in the message.
void AddRte(RipRte rte)
Add a RTE to the message.
std::list< RipRte > m_rteList
list of the RTEs in the message
Definition rip-header.h:230
void SetCommand(Command_e command)
Set the command.
Command_e
Commands to be used in Rip headers.
Definition rip-header.h:188
uint8_t m_command
command type
Definition rip-header.h:229
void ClearRtes()
Clear all the RTEs from the header.
static TypeId GetTypeId()
Get the type ID.
std::list< RipRte > GetRteList() const
Get the list of the RTEs included in the message.
Command_e GetCommand() const
Get the command.
Rip v2 Routing Table Entry (RTE) - see RFC 2453 .
Definition rip-header.h:28
Ipv4Mask GetSubnetMask() const
Get the subnet mask.
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition rip-header.cc:59
void SetSubnetMask(Ipv4Mask subnetMask)
Set the subnet mask.
Ipv4Address m_prefix
Advertised prefix.
Definition rip-header.h:127
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition rip-header.cc:53
uint32_t m_metric
Route metric.
Definition rip-header.h:130
Ipv4Mask m_subnetMask
Subnet mask.
Definition rip-header.h:128
void SetRouteMetric(uint32_t routeMetric)
Set the route metric.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition rip-header.cc:71
uint16_t m_tag
Route tag.
Definition rip-header.h:126
void SetPrefix(Ipv4Address prefix)
Set the prefix.
Definition rip-header.cc:92
Ipv4Address GetNextHop() const
Get the next hop.
void Print(std::ostream &os) const override
Definition rip-header.cc:45
uint32_t GetRouteMetric() const
Get the route metric.
static TypeId GetTypeId()
Get the type ID.
Definition rip-header.cc:31
Ipv4Address GetPrefix() const
Get the prefix.
Definition rip-header.cc:98
uint16_t GetRouteTag() const
Get the route tag.
Ipv4Address m_nextHop
Next hop.
Definition rip-header.h:129
void SetRouteTag(uint16_t routeTag)
Set the route tag.
void SetNextHop(Ipv4Address nextHop)
Set the next hop.
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
Definition rip-header.cc:39
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