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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19
20#include "ripng-header.h"
21
22#include "ns3/log.h"
23
24namespace ns3
25{
26
27/*
28 * RipNgRte
29 */
31
33 : m_prefix("::"),
34 m_tag(0),
35 m_prefixLen(0),
36 m_metric(16)
37{
38}
39
42{
43 static TypeId tid = TypeId("ns3::RipNgRte")
45 .SetGroupName("Internet")
46 .AddConstructor<RipNgRte>();
47 return tid;
48}
49
52{
53 return GetTypeId();
54}
55
56void
57RipNgRte::Print(std::ostream& os) const
58{
59 os << "prefix " << m_prefix << "/" << int(m_prefixLen) << " Metric " << int(m_metric) << " Tag "
60 << int(m_tag);
61}
62
65{
66 return 20;
67}
68
69void
71{
72 uint8_t tmp[16];
73
75 i.Write(tmp, 16);
76
80}
81
84{
85 uint8_t tmp[16];
86
87 i.Read(tmp, 16);
88 m_prefix.Set(tmp);
89 m_tag = i.ReadNtohU16();
90 m_prefixLen = i.ReadU8();
91 m_metric = i.ReadU8();
92
93 return GetSerializedSize();
94}
95
96void
98{
99 m_prefix = prefix;
100}
101
104{
105 return m_prefix;
106}
107
108void
109RipNgRte::SetPrefixLen(uint8_t prefixLen)
110{
111 m_prefixLen = prefixLen;
112}
113
114uint8_t
116{
117 return m_prefixLen;
118}
119
120void
121RipNgRte::SetRouteTag(uint16_t routeTag)
122{
123 m_tag = routeTag;
124}
125
126uint16_t
128{
129 return m_tag;
130}
131
132void
133RipNgRte::SetRouteMetric(uint8_t routeMetric)
134{
135 m_metric = routeMetric;
136}
137
138uint8_t
140{
141 return m_metric;
142}
143
144std::ostream&
145operator<<(std::ostream& os, const RipNgRte& h)
146{
147 h.Print(os);
148 return os;
149}
150
151/*
152 * RipNgHeader
153 */
154NS_LOG_COMPONENT_DEFINE("RipNgHeader");
155NS_OBJECT_ENSURE_REGISTERED(RipNgHeader);
156
158 : m_command(0)
159{
160}
161
162TypeId
164{
165 static TypeId tid = TypeId("ns3::RipNgHeader")
166 .SetParent<Header>()
167 .SetGroupName("Internet")
168 .AddConstructor<RipNgHeader>();
169 return tid;
170}
171
172TypeId
174{
175 return GetTypeId();
176}
177
178void
179RipNgHeader::Print(std::ostream& os) const
180{
181 os << "command " << int(m_command);
182 for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
183 {
184 os << " | ";
185 iter->Print(os);
186 }
187}
188
191{
192 RipNgRte rte;
193 return 4 + m_rteList.size() * rte.GetSerializedSize();
194}
195
196void
198{
199 Buffer::Iterator i = start;
200
201 i.WriteU8(uint8_t(m_command));
202 i.WriteU8(1);
203 i.WriteU16(0);
204
205 for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
206 {
207 iter->Serialize(i);
208 i.Next(iter->GetSerializedSize());
209 }
210}
211
214{
215 Buffer::Iterator i = start;
216
217 uint8_t temp;
218 temp = i.ReadU8();
219 if ((temp == REQUEST) || (temp == RESPONSE))
220 {
221 m_command = temp;
222 }
223 else
224 {
225 return 0;
226 }
227
228 if (i.ReadU8() != 1)
229 {
230 NS_LOG_LOGIC("RIP received a message with mismatch version, ignoring.");
231 return 0;
232 }
233
234 if (i.ReadU16() != 0)
235 {
236 NS_LOG_LOGIC("RIP received a message with invalid filled flags, ignoring.");
237 return 0;
238 }
239
240 uint8_t rteNumber = i.GetRemainingSize() / 20;
241 for (uint8_t n = 0; n < rteNumber; n++)
242 {
243 RipNgRte rte;
244 i.Next(rte.Deserialize(i));
245 m_rteList.push_back(rte);
246 }
247
248 return GetSerializedSize();
249}
250
251void
253{
254 m_command = command;
255}
256
259{
261}
262
263void
265{
266 m_rteList.push_back(rte);
267}
268
269void
271{
272 m_rteList.clear();
273}
274
275uint16_t
277{
278 return m_rteList.size();
279}
280
281std::list<RipNgRte>
283{
284 return m_rteList;
285}
286
287std::ostream&
288operator<<(std::ostream& os, const RipNgHeader& h)
289{
290 h.Print(os);
291 return os;
292}
293
294} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
uint32_t GetRemainingSize() const
Definition: buffer.cc:1173
uint8_t ReadU8()
Definition: buffer.h:1027
void WriteU8(uint8_t data)
Definition: buffer.h:881
void Write(const uint8_t *buffer, uint32_t size)
Definition: buffer.cc:948
void WriteU16(uint16_t data)
Definition: buffer.cc:859
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1125
void WriteHtonU16(uint16_t data)
Definition: buffer.h:915
uint16_t ReadNtohU16()
Definition: buffer.h:954
uint16_t ReadU16()
Definition: buffer.h:1035
void Next()
go forward by one byte
Definition: buffer.h:853
Protocol header serialization and deserialization.
Definition: header.h:44
Describes an IPv6 address.
Definition: ipv6-address.h:49
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
Definition: ripng-header.h:147
uint8_t m_command
command type
Definition: ripng-header.h:229
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
Definition: ripng-header.h:230
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.
Definition: ripng-header.h:188
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
Definition: ripng-header.h:40
uint16_t m_tag
route tag
Definition: ripng-header.h:127
static TypeId GetTypeId()
Get the type ID.
Definition: ripng-header.cc:41
uint8_t m_metric
route metric
Definition: ripng-header.h:129
Ipv6Address m_prefix
prefix
Definition: ripng-header.h:126
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
Definition: ripng-header.h:128
void SetPrefix(Ipv6Address prefix)
Set the prefix.
Definition: ripng-header.cc:97
void Print(std::ostream &os) const override
Definition: ripng-header.cc:57
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition: ripng-header.cc:70
void SetPrefixLen(uint8_t prefixLen)
Set the prefix length.
uint32_t Deserialize(Buffer::Iterator start) override
Deserialize the packet.
Definition: ripng-header.cc:83
void SetRouteMetric(uint8_t routeMetric)
Set the route metric.
TypeId GetInstanceTypeId() const override
Return the instance type identifier.
Definition: ripng-header.cc:51
uint32_t GetSerializedSize() const override
Get the serialized size of the packet.
Definition: ripng-header.cc:64
void SetRouteTag(uint16_t routeTag)
Set the route tag.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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:159