A Discrete-Event Network Simulator
API
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 (std::list<RipNgRte>::const_iterator iter = m_rteList.begin(); iter != m_rteList.end();
183 iter++)
184 {
185 os << " | ";
186 iter->Print(os);
187 }
188}
189
192{
193 RipNgRte rte;
194 return 4 + m_rteList.size() * rte.GetSerializedSize();
195}
196
197void
199{
201
202 i.WriteU8(uint8_t(m_command));
203 i.WriteU8(1);
204 i.WriteU16(0);
205
206 for (std::list<RipNgRte>::const_iterator iter = m_rteList.begin(); iter != m_rteList.end();
207 iter++)
208 {
209 iter->Serialize(i);
210 i.Next(iter->GetSerializedSize());
211 }
212}
213
216{
218
219 uint8_t temp;
220 temp = i.ReadU8();
221 if ((temp == REQUEST) || (temp == RESPONSE))
222 {
223 m_command = temp;
224 }
225 else
226 {
227 return 0;
228 }
229
230 if (i.ReadU8() != 1)
231 {
232 NS_LOG_LOGIC("RIP received a message with mismatch version, ignoring.");
233 return 0;
234 }
235
236 if (i.ReadU16() != 0)
237 {
238 NS_LOG_LOGIC("RIP received a message with invalid filled flags, ignoring.");
239 return 0;
240 }
241
242 uint8_t rteNumber = i.GetRemainingSize() / 20;
243 for (uint8_t n = 0; n < rteNumber; n++)
244 {
245 RipNgRte rte;
246 i.Next(rte.Deserialize(i));
247 m_rteList.push_back(rte);
248 }
249
250 return GetSerializedSize();
251}
252
253void
255{
256 m_command = command;
257}
258
261{
263}
264
265void
267{
268 m_rteList.push_back(rte);
269}
270
271void
273{
274 m_rteList.clear();
275}
276
277uint16_t
279{
280 return m_rteList.size();
281}
282
283std::list<RipNgRte>
285{
286 return m_rteList;
287}
288
289std::ostream&
290operator<<(std::ostream& os, const RipNgHeader& h)
291{
292 h.Print(os);
293 return os;
294}
295
296} // namespace ns3
iterator in a Buffer instance
Definition: buffer.h:100
uint32_t GetRemainingSize() const
Definition: buffer.cc:1176
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:951
void WriteU16(uint16_t data)
Definition: buffer.cc:862
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1128
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:146
uint8_t m_command
command type
Definition: ripng-header.h:228
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:229
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:187
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:39
uint16_t m_tag
route tag
Definition: ripng-header.h:126
static TypeId GetTypeId()
Get the type ID.
Definition: ripng-header.cc:41
uint8_t m_metric
route metric
Definition: ripng-header.h:128
Ipv6Address m_prefix
prefix
Definition: ripng-header.h:125
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:127
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:935
#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:129