A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
13
namespace
ns3
14
{
15
16
/*
17
* RipRte
18
*/
19
NS_OBJECT_ENSURE_REGISTERED
(
RipRte
);
20
21
RipRte::RipRte
()
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
30
TypeId
31
RipRte::GetTypeId
()
32
{
33
static
TypeId
tid =
34
TypeId
(
"ns3::RipRte"
).
SetParent
<
Header
>().SetGroupName(
"Internet"
).AddConstructor<
RipRte
>();
35
return
tid;
36
}
37
38
TypeId
39
RipRte::GetInstanceTypeId
()
const
40
{
41
return
GetTypeId
();
42
}
43
44
void
45
RipRte::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
52
uint32_t
53
RipRte::GetSerializedSize
()
const
54
{
55
return
20;
56
}
57
58
void
59
RipRte::Serialize
(
Buffer::Iterator
i)
const
60
{
61
i.
WriteHtonU16
(2);
62
i.
WriteHtonU16
(
m_tag
);
63
64
i.
WriteHtonU32
(
m_prefix
.Get());
65
i.
WriteHtonU32
(
m_subnetMask
.Get());
66
i.
WriteHtonU32
(
m_nextHop
.Get());
67
i.
WriteHtonU32
(
m_metric
);
68
}
69
70
uint32_t
71
RipRte::Deserialize
(
Buffer::Iterator
i)
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
();
82
m_prefix
.Set(i.
ReadNtohU32
());
83
m_subnetMask
.Set(i.
ReadNtohU32
());
84
m_nextHop
.Set(i.
ReadNtohU32
());
85
86
m_metric
= i.
ReadNtohU32
();
87
88
return
GetSerializedSize
();
89
}
90
91
void
92
RipRte::SetPrefix
(
Ipv4Address
prefix)
93
{
94
m_prefix
= prefix;
95
}
96
97
Ipv4Address
98
RipRte::GetPrefix
()
const
99
{
100
return
m_prefix
;
101
}
102
103
void
104
RipRte::SetSubnetMask
(
Ipv4Mask
subnetMask)
105
{
106
m_subnetMask
= subnetMask;
107
}
108
109
Ipv4Mask
110
RipRte::GetSubnetMask
()
const
111
{
112
return
m_subnetMask
;
113
}
114
115
void
116
RipRte::SetRouteTag
(uint16_t routeTag)
117
{
118
m_tag
= routeTag;
119
}
120
121
uint16_t
122
RipRte::GetRouteTag
()
const
123
{
124
return
m_tag
;
125
}
126
127
void
128
RipRte::SetRouteMetric
(
uint32_t
routeMetric)
129
{
130
m_metric
= routeMetric;
131
}
132
133
uint32_t
134
RipRte::GetRouteMetric
()
const
135
{
136
return
m_metric
;
137
}
138
139
void
140
RipRte::SetNextHop
(
Ipv4Address
nextHop)
141
{
142
m_nextHop
= nextHop;
143
}
144
145
Ipv4Address
146
RipRte::GetNextHop
()
const
147
{
148
return
m_nextHop
;
149
}
150
151
std::ostream&
152
operator<<
(std::ostream& os,
const
RipRte
& h)
153
{
154
h.
Print
(os);
155
return
os;
156
}
157
158
/*
159
* RipHeader
160
*/
161
NS_LOG_COMPONENT_DEFINE
(
"RipHeader"
);
162
NS_OBJECT_ENSURE_REGISTERED
(RipHeader);
163
164
RipHeader::RipHeader
()
165
:
m_command
(0)
166
{
167
}
168
169
TypeId
170
RipHeader::GetTypeId
()
171
{
172
static
TypeId
tid =
TypeId
(
"ns3::RipHeader"
)
173
.
SetParent
<
Header
>()
174
.SetGroupName(
"Internet"
)
175
.AddConstructor<
RipHeader
>();
176
return
tid;
177
}
178
179
TypeId
180
RipHeader::GetInstanceTypeId
()
const
181
{
182
return
GetTypeId
();
183
}
184
185
void
186
RipHeader::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
196
uint32_t
197
RipHeader::GetSerializedSize
()
const
198
{
199
RipRte
rte;
200
return
4 +
m_rteList
.size() * rte.
GetSerializedSize
();
201
}
202
203
void
204
RipHeader::Serialize
(
Buffer::Iterator
start)
const
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
219
uint32_t
220
RipHeader::Deserialize
(
Buffer::Iterator
start)
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
258
void
259
RipHeader::SetCommand
(
RipHeader::Command_e
command)
260
{
261
m_command
= command;
262
}
263
264
RipHeader::Command_e
265
RipHeader::GetCommand
()
const
266
{
267
return
RipHeader::Command_e
(
m_command
);
268
}
269
270
void
271
RipHeader::AddRte
(
RipRte
rte)
272
{
273
m_rteList
.push_back(rte);
274
}
275
276
void
277
RipHeader::ClearRtes
()
278
{
279
m_rteList
.clear();
280
}
281
282
uint16_t
283
RipHeader::GetRteNumber
()
const
284
{
285
return
m_rteList
.size();
286
}
287
288
std::list<RipRte>
289
RipHeader::GetRteList
()
const
290
{
291
return
m_rteList
;
292
}
293
294
std::ostream&
295
operator<<
(std::ostream& os,
const
RipHeader
& h)
296
{
297
h.
Print
(os);
298
return
os;
299
}
300
301
}
// namespace ns3
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Buffer::Iterator::GetRemainingSize
uint32_t GetRemainingSize() const
Definition
buffer.cc:1162
ns3::Buffer::Iterator::ReadU8
uint8_t ReadU8()
Definition
buffer.h:1016
ns3::Buffer::Iterator::WriteU8
void WriteU8(uint8_t data)
Definition
buffer.h:870
ns3::Buffer::Iterator::WriteU16
void WriteU16(uint16_t data)
Definition
buffer.cc:848
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition
buffer.h:904
ns3::Buffer::Iterator::ReadNtohU32
uint32_t ReadNtohU32()
Definition
buffer.h:967
ns3::Buffer::Iterator::WriteHtonU32
void WriteHtonU32(uint32_t data)
Definition
buffer.h:922
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16()
Definition
buffer.h:943
ns3::Buffer::Iterator::ReadU16
uint16_t ReadU16()
Definition
buffer.h:1024
ns3::Buffer::Iterator::Next
void Next()
go forward by one byte
Definition
buffer.h:842
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::Ipv4Address
Ipv4 addresses are stored in host order in this class.
Definition
ipv4-address.h:31
ns3::Ipv4Mask
a class to represent an Ipv4 address mask
Definition
ipv4-address.h:254
ns3::RipHeader
RipHeader - see RFC 2453
Definition
rip-header.h:147
ns3::RipHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
rip-header.cc:204
ns3::RipHeader::Print
void Print(std::ostream &os) const override
Definition
rip-header.cc:186
ns3::RipHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
Definition
rip-header.cc:180
ns3::RipHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition
rip-header.cc:220
ns3::RipHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
rip-header.cc:197
ns3::RipHeader::GetRteNumber
uint16_t GetRteNumber() const
Get the number of RTE included in the message.
Definition
rip-header.cc:283
ns3::RipHeader::AddRte
void AddRte(RipRte rte)
Add a RTE to the message.
Definition
rip-header.cc:271
ns3::RipHeader::m_rteList
std::list< RipRte > m_rteList
list of the RTEs in the message
Definition
rip-header.h:230
ns3::RipHeader::SetCommand
void SetCommand(Command_e command)
Set the command.
Definition
rip-header.cc:259
ns3::RipHeader::Command_e
Command_e
Commands to be used in Rip headers.
Definition
rip-header.h:188
ns3::RipHeader::RESPONSE
@ RESPONSE
Definition
rip-header.h:190
ns3::RipHeader::REQUEST
@ REQUEST
Definition
rip-header.h:189
ns3::RipHeader::m_command
uint8_t m_command
command type
Definition
rip-header.h:229
ns3::RipHeader::ClearRtes
void ClearRtes()
Clear all the RTEs from the header.
Definition
rip-header.cc:277
ns3::RipHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
rip-header.cc:170
ns3::RipHeader::RipHeader
RipHeader()
Definition
rip-header.cc:164
ns3::RipHeader::GetRteList
std::list< RipRte > GetRteList() const
Get the list of the RTEs included in the message.
Definition
rip-header.cc:289
ns3::RipHeader::GetCommand
Command_e GetCommand() const
Get the command.
Definition
rip-header.cc:265
ns3::RipRte
Rip v2 Routing Table Entry (RTE) - see RFC 2453.
Definition
rip-header.h:28
ns3::RipRte::GetSubnetMask
Ipv4Mask GetSubnetMask() const
Get the subnet mask.
Definition
rip-header.cc:110
ns3::RipRte::Serialize
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition
rip-header.cc:59
ns3::RipRte::SetSubnetMask
void SetSubnetMask(Ipv4Mask subnetMask)
Set the subnet mask.
Definition
rip-header.cc:104
ns3::RipRte::m_prefix
Ipv4Address m_prefix
Advertised prefix.
Definition
rip-header.h:127
ns3::RipRte::GetSerializedSize
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition
rip-header.cc:53
ns3::RipRte::m_metric
uint32_t m_metric
Route metric.
Definition
rip-header.h:130
ns3::RipRte::m_subnetMask
Ipv4Mask m_subnetMask
Subnet mask.
Definition
rip-header.h:128
ns3::RipRte::SetRouteMetric
void SetRouteMetric(uint32_t routeMetric)
Set the route metric.
Definition
rip-header.cc:128
ns3::RipRte::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition
rip-header.cc:71
ns3::RipRte::m_tag
uint16_t m_tag
Route tag.
Definition
rip-header.h:126
ns3::RipRte::SetPrefix
void SetPrefix(Ipv4Address prefix)
Set the prefix.
Definition
rip-header.cc:92
ns3::RipRte::GetNextHop
Ipv4Address GetNextHop() const
Get the next hop.
Definition
rip-header.cc:146
ns3::RipRte::Print
void Print(std::ostream &os) const override
Definition
rip-header.cc:45
ns3::RipRte::GetRouteMetric
uint32_t GetRouteMetric() const
Get the route metric.
Definition
rip-header.cc:134
ns3::RipRte::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
rip-header.cc:31
ns3::RipRte::GetPrefix
Ipv4Address GetPrefix() const
Get the prefix.
Definition
rip-header.cc:98
ns3::RipRte::GetRouteTag
uint16_t GetRouteTag() const
Get the route tag.
Definition
rip-header.cc:122
ns3::RipRte::m_nextHop
Ipv4Address m_nextHop
Next hop.
Definition
rip-header.h:129
ns3::RipRte::SetRouteTag
void SetRouteTag(uint16_t routeTag)
Set the route tag.
Definition
rip-header.cc:116
ns3::RipRte::SetNextHop
void SetNextHop(Ipv4Address nextHop)
Set the next hop.
Definition
rip-header.cc:140
ns3::RipRte::RipRte
RipRte()
Definition
rip-header.cc:21
ns3::RipRte::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
Definition
rip-header.cc:39
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
uint32_t
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition
log.h:271
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition
object-base.h:35
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition
angles.cc:148
rip-header.h
src
internet
model
rip-header.cc
Generated on Wed Jun 11 2025 13:15:30 for ns-3 by
1.13.2