A Discrete-Event Network Simulator
API
rip-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "rip-header.h"
22 #include "ns3/log.h"
23 
24 namespace ns3 {
25 
26 /*
27  * RipRte
28  */
30 
31 
33  : m_tag (0), m_prefix ("127.0.0.1"), m_subnetMask ("0.0.0.0"), m_nextHop ("0.0.0.0"), m_metric (16)
34 {
35 }
36 
38 {
39  static TypeId tid = TypeId ("ns3::RipRte")
40  .SetParent<Header> ()
41  .SetGroupName ("Internet")
42  .AddConstructor<RipRte> ();
43  return tid;
44 }
45 
47 {
48  return GetTypeId ();
49 }
50 
51 void RipRte::Print (std::ostream & os) const
52 {
53  os << "prefix " << m_prefix << "/" << m_subnetMask.GetPrefixLength () << " Metric " << int(m_metric);
54  os << " Tag " << int(m_tag) << " Next Hop " << m_nextHop;
55 }
56 
57 uint32_t RipRte::GetSerializedSize () const
58 {
59  return 20;
60 }
61 
63 {
64  i.WriteHtonU16 (2);
65  i.WriteHtonU16 (m_tag);
66 
67  i.WriteHtonU32 (m_prefix.Get ());
69  i.WriteHtonU32 (m_nextHop.Get ());
71 }
72 
74 {
75  uint16_t tmp;
76 
77  tmp = i.ReadNtohU16 ();
78  if (tmp != 2)
79  {
80  return 0;
81  }
82 
83  m_tag = i.ReadNtohU16 ();
84  m_prefix.Set (i.ReadNtohU32 ());
86  m_nextHop.Set (i.ReadNtohU32 ());
87 
88  m_metric = i.ReadNtohU32 ();
89 
90  return GetSerializedSize ();
91 }
92 
94 {
95  m_prefix = prefix;
96 }
97 
99 {
100  return m_prefix;
101 }
102 
104 {
105  m_subnetMask = subnetMask;
106 }
107 
109 {
110  return m_subnetMask;
111 }
112 
113 void RipRte::SetRouteTag (uint16_t routeTag)
114 {
115  m_tag = routeTag;
116 }
117 
118 uint16_t RipRte::GetRouteTag () const
119 {
120  return m_tag;
121 }
122 
123 void RipRte::SetRouteMetric (uint32_t routeMetric)
124 {
125  m_metric = routeMetric;
126 }
127 
128 uint32_t RipRte::GetRouteMetric () const
129 {
130  return m_metric;
131 }
132 
134 {
135  m_nextHop = nextHop;
136 }
137 
139 {
140  return m_nextHop;
141 }
142 
143 
144 std::ostream & operator << (std::ostream & os, const RipRte & h)
145 {
146  h.Print (os);
147  return os;
148 }
149 
150 /*
151  * RipHeader
152  */
153 NS_LOG_COMPONENT_DEFINE ("RipHeader");
154 NS_OBJECT_ENSURE_REGISTERED (RipHeader);
155 
156 
158  : m_command (0)
159 {
160 }
161 
163 {
164  static TypeId tid = TypeId ("ns3::RipHeader")
165  .SetParent<Header> ()
166  .SetGroupName ("Internet")
167  .AddConstructor<RipHeader> ();
168  return tid;
169 }
170 
172 {
173  return GetTypeId ();
174 }
175 
176 void RipHeader::Print (std::ostream & os) const
177 {
178  os << "command " << int(m_command);
179  for (std::list<RipRte>::const_iterator iter = m_rteList.begin ();
180  iter != m_rteList.end (); iter ++)
181  {
182  os << " | ";
183  iter->Print (os);
184  }
185 }
186 
188 {
189  RipRte rte;
190  return 4 + m_rteList.size () * rte.GetSerializedSize ();
191 }
192 
194 {
196 
197  i.WriteU8 (uint8_t (m_command));
198  i.WriteU8 (2);
199  i.WriteU16 (0);
200 
201  for (std::list<RipRte>::const_iterator iter = m_rteList.begin ();
202  iter != m_rteList.end (); iter ++)
203  {
204  iter->Serialize (i);
205  i.Next(iter->GetSerializedSize ());
206  }
207 }
208 
210 {
212 
213  uint8_t temp;
214  temp = i.ReadU8 ();
215  if ((temp == REQUEST) || (temp == RESPONSE))
216  {
217  m_command = temp;
218  }
219  else
220  {
221  return 0;
222  }
223 
224  if (i.ReadU8 () != 2)
225  {
226  NS_LOG_LOGIC ("RIP received a message with mismatch version, ignoring.");
227  return 0;
228  }
229 
230  if (i.ReadU16 () != 0)
231  {
232  NS_LOG_LOGIC ("RIP received a message with invalid filled flags, ignoring.");
233  return 0;
234  }
235 
236  uint8_t rteNumber = i.GetRemainingSize ()/20;
237  for (uint8_t n=0; n<rteNumber; n++)
238  {
239  RipRte rte;
240  i.Next (rte.Deserialize (i));
241  m_rteList.push_back (rte);
242  }
243 
244  return GetSerializedSize ();
245 }
246 
248 {
249  m_command = command;
250 }
251 
253 {
255 }
256 
258 {
259  m_rteList.push_back (rte);
260 }
261 
263 {
264  m_rteList.clear ();
265 }
266 
267 uint16_t RipHeader::GetRteNumber (void) const
268 {
269  return m_rteList.size ();
270 }
271 
272 std::list<RipRte> RipHeader::GetRteList (void) const
273 {
274  return m_rteList;
275 }
276 
277 
278 std::ostream & operator << (std::ostream & os, const RipHeader & h)
279 {
280  h.Print (os);
281  return os;
282 }
283 
284 
285 }
286 
uint16_t ReadU16(void)
Definition: buffer.h:1029
Protocol header serialization and deserialization.
Definition: header.h:42
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: rip-header.cc:57
static TypeId GetTypeId(void)
Get the type ID.
Definition: rip-header.cc:162
void SetPrefix(Ipv4Address prefix)
Set the prefix.
Definition: rip-header.cc:93
uint16_t m_tag
Route tag.
Definition: rip-header.h:137
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Ipv4Mask GetSubnetMask(void) const
Get the subnet mask.
Definition: rip-header.cc:108
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
uint32_t GetRemainingSize(void) const
Definition: buffer.cc:1165
def start()
Definition: core.py:1858
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
Ipv4Address m_nextHop
Next hop.
Definition: rip-header.h:140
Ipv4Address GetPrefix(void) const
Get the prefix.
Definition: rip-header.cc:98
std::list< RipRte > m_rteList
list of the RTEs in the message
Definition: rip-header.h:241
uint32_t ReadNtohU32(void)
Definition: buffer.h:970
iterator in a Buffer instance
Definition: buffer.h:98
Command_e GetCommand(void) const
Get the command.
Definition: rip-header.cc:252
void SetRouteMetric(uint32_t routeMetric)
Set the route metric.
Definition: rip-header.cc:123
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: rip-header.cc:46
RipRte(void)
Definition: rip-header.cc:32
void WriteU16(uint16_t data)
Definition: buffer.cc:870
void WriteHtonU16(uint16_t data)
Definition: buffer.h:905
void SetSubnetMask(Ipv4Mask subnetMask)
Set the subnet mask.
Definition: rip-header.cc:103
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: rip-header.cc:62
void Next(void)
go forward by one byte
Definition: buffer.h:845
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: rip-header.cc:209
RipHeader - see RFC 2453
Definition: rip-header.h:157
Ipv4Mask m_subnetMask
Subnet mask.
Definition: rip-header.h:139
static TypeId GetTypeId(void)
Get the type ID.
Definition: rip-header.cc:37
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
void SetCommand(Command_e command)
Set the command.
Definition: rip-header.cc:247
uint8_t m_command
command type
Definition: rip-header.h:240
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: rip-header.cc:171
Rip v2 Routing Table Entry (RTE) - see RFC 2453.
Definition: rip-header.h:37
void Set(uint32_t mask)
input mask is in host order.
void WriteHtonU32(uint32_t data)
Definition: buffer.h:924
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: rip-header.cc:73
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint16_t GetRouteTag(void) const
Get the route tag.
Definition: rip-header.cc:118
Ipv4Address m_prefix
Advertised prefix.
Definition: rip-header.h:138
void Set(uint32_t address)
input address is in host order.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
uint32_t GetRouteMetric(void) const
Get the route metric.
Definition: rip-header.cc:128
uint32_t m_metric
Route metric.
Definition: rip-header.h:141
uint16_t GetRteNumber(void) const
Get the number of RTE included in the message.
Definition: rip-header.cc:267
void WriteU8(uint8_t data)
Definition: buffer.h:869
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
void ClearRtes()
Clear all the RTEs from the header.
Definition: rip-header.cc:262
void SetNextHop(Ipv4Address nextHop)
Set the next hop.
Definition: rip-header.cc:133
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: rip-header.cc:187
Command_e
Commands to be used in Rip headers.
Definition: rip-header.h:198
virtual void Print(std::ostream &os) const
Definition: rip-header.cc:176
uint8_t ReadU8(void)
Definition: buffer.h:1021
uint16_t GetPrefixLength(void) const
void SetRouteTag(uint16_t routeTag)
Set the route tag.
Definition: rip-header.cc:113
uint32_t Get(void) const
Get the host-order 32-bit IP address.
uint16_t ReadNtohU16(void)
Definition: buffer.h:946
Ipv4Address GetNextHop(void) const
Get the next hop.
Definition: rip-header.cc:138
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: rip-header.cc:193
std::list< RipRte > GetRteList(void) const
Get the list of the RTEs included in the message.
Definition: rip-header.cc:272
a unique identifier for an interface.
Definition: type-id.h:58
void AddRte(RipRte rte)
Add a RTE to the message.
Definition: rip-header.cc:257
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual void Print(std::ostream &os) const
Definition: rip-header.cc:51