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