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 
23 namespace ns3 {
24 
25 /*
26  * RipRte
27  */
29 
30 
32  : m_tag (0), m_prefix ("127.0.0.1"), m_subnetMask ("0.0.0.0"), m_nextHop ("0.0.0.0"), m_metric (16)
33 {
34 }
35 
37 {
38  static TypeId tid = TypeId ("ns3::RipRte")
39  .SetParent<Header> ()
40  .SetGroupName ("Internet")
41  .AddConstructor<RipRte> ();
42  return tid;
43 }
44 
46 {
47  return GetTypeId ();
48 }
49 
50 void RipRte::Print (std::ostream & os) const
51 {
52  os << "prefix " << m_prefix << "/" << m_subnetMask.GetPrefixLength () << " Metric " << int(m_metric);
53  os << " Tag " << int(m_tag) << " Next Hop " << m_nextHop;
54 }
55 
56 uint32_t RipRte::GetSerializedSize () const
57 {
58  return 20;
59 }
60 
62 {
63  i.WriteHtonU16 (2);
64  i.WriteHtonU16 (m_tag);
65 
66  i.WriteHtonU32 (m_prefix.Get ());
68  i.WriteHtonU32 (m_nextHop.Get ());
70 }
71 
73 {
74  uint16_t tmp;
75 
76  tmp = i.ReadNtohU16 ();
77  if (tmp != 2)
78  {
79  return 0;
80  }
81 
82  m_tag = i.ReadNtohU16 ();
83  m_prefix.Set (i.ReadNtohU32 ());
85  m_nextHop.Set (i.ReadNtohU32 ());
86 
87  m_metric = i.ReadNtohU32 ();
88 
89  return GetSerializedSize ();
90 }
91 
93 {
94  m_prefix = prefix;
95 }
96 
98 {
99  return m_prefix;
100 }
101 
103 {
104  m_subnetMask = subnetMask;
105 }
106 
108 {
109  return m_subnetMask;
110 }
111 
112 void RipRte::SetRouteTag (uint16_t routeTag)
113 {
114  m_tag = routeTag;
115 }
116 
117 uint16_t RipRte::GetRouteTag () const
118 {
119  return m_tag;
120 }
121 
122 void RipRte::SetRouteMetric (uint32_t routeMetric)
123 {
124  m_metric = routeMetric;
125 }
126 
127 uint32_t RipRte::GetRouteMetric () const
128 {
129  return m_metric;
130 }
131 
133 {
134  m_nextHop = nextHop;
135 }
136 
138 {
139  return m_nextHop;
140 }
141 
142 
143 std::ostream & operator << (std::ostream & os, const RipRte & h)
144 {
145  h.Print (os);
146  return os;
147 }
148 
149 /*
150  * RipHeader
151  */
152 NS_OBJECT_ENSURE_REGISTERED (RipHeader)
153  ;
154 
156  : m_command (0)
157 {
158 }
159 
161 {
162  static TypeId tid = TypeId ("ns3::RipHeader")
163  .SetParent<Header> ()
164  .SetGroupName ("Internet")
165  .AddConstructor<RipHeader> ();
166  return tid;
167 }
168 
170 {
171  return GetTypeId ();
172 }
173 
174 void RipHeader::Print (std::ostream & os) const
175 {
176  os << "command " << int(m_command);
177  for (std::list<RipRte>::const_iterator iter = m_rteList.begin ();
178  iter != m_rteList.end (); iter ++)
179  {
180  os << " | ";
181  iter->Print (os);
182  }
183 }
184 
186 {
187  RipRte rte;
188  return 4 + m_rteList.size () * rte.GetSerializedSize ();
189 }
190 
192 {
194 
195  i.WriteU8 (uint8_t (m_command));
196  i.WriteU8 (1);
197  i.WriteU16 (0);
198 
199  for (std::list<RipRte>::const_iterator iter = m_rteList.begin ();
200  iter != m_rteList.end (); iter ++)
201  {
202  iter->Serialize (i);
203  i.Next(iter->GetSerializedSize ());
204  }
205 }
206 
208 {
210 
211  uint8_t temp;
212  temp = i.ReadU8 ();
213  if ((temp == REQUEST) || (temp == RESPONSE))
214  {
215  m_command = temp;
216  }
217  else
218  {
219  return 0;
220  }
221 
222  temp = i.ReadU8 ();
223  NS_ASSERT_MSG (temp == 1, "RIP received a message with mismatch version, aborting.");
224 
225  uint16_t temp16 = i.ReadU16 ();
226  NS_ASSERT_MSG (temp16 == 0, "RIP received a message with invalid filled flags, aborting.");
227 
228  uint8_t rteNumber = i.GetRemainingSize ()/20;
229  for (uint8_t n=0; n<rteNumber; n++)
230  {
231  RipRte rte;
232  i.Next (rte.Deserialize (i));
233  m_rteList.push_back (rte);
234  }
235 
236  return GetSerializedSize ();
237 }
238 
240 {
241  m_command = command;
242 }
243 
245 {
247 }
248 
250 {
251  m_rteList.push_back (rte);
252 }
253 
255 {
256  m_rteList.clear ();
257 }
258 
259 uint16_t RipHeader::GetRteNumber (void) const
260 {
261  return m_rteList.size ();
262 }
263 
264 std::list<RipRte> RipHeader::GetRteList (void) const
265 {
266  return m_rteList;
267 }
268 
269 
270 std::ostream & operator << (std::ostream & os, const RipHeader & h)
271 {
272  h.Print (os);
273  return os;
274 }
275 
276 
277 }
278 
uint16_t ReadU16(void)
Definition: buffer.h:1028
Protocol header serialization and deserialization.
Definition: header.h:42
static TypeId GetTypeId(void)
Get the type ID.
Definition: rip-header.cc:160
void SetPrefix(Ipv4Address prefix)
Set the prefix.
Definition: rip-header.cc:92
uint16_t GetRteNumber(void) const
Get the number of RTE included in the message.
Definition: rip-header.cc:259
uint16_t m_tag
Route tag.
Definition: rip-header.h:137
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: rip-header.cc:45
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
Ipv4Mask GetSubnetMask(void) const
Get the subnet mask.
Definition: rip-header.cc:107
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: rip-header.cc:61
def start()
Definition: core.py:1482
std::list< RipRte > GetRteList(void) const
Get the list of the RTEs included in the message.
Definition: rip-header.cc:264
Ipv4Address m_nextHop
Next hop.
Definition: rip-header.h:140
virtual void Print(std::ostream &os) const
Definition: rip-header.cc:50
std::list< RipRte > m_rteList
list of the RTEs in the message
Definition: rip-header.h:241
Command_e GetCommand(void) const
Get the command.
Definition: rip-header.cc:244
uint32_t ReadNtohU32(void)
Definition: buffer.h:969
iterator in a Buffer instance
Definition: buffer.h:98
uint32_t GetRouteMetric(void) const
Get the route metric.
Definition: rip-header.cc:127
void SetRouteMetric(uint32_t routeMetric)
Set the route metric.
Definition: rip-header.cc:122
RipRte(void)
Definition: rip-header.cc:31
uint32_t Get(void) const
Get the host-order 32-bit IP address.
void WriteU16(uint16_t data)
Definition: buffer.cc:870
void WriteHtonU16(uint16_t data)
Definition: buffer.h:904
void SetSubnetMask(Ipv4Mask subnetMask)
Set the subnet mask.
Definition: rip-header.cc:102
void Next(void)
go forward by one byte
Definition: buffer.h:844
uint32_t GetRemainingSize(void) const
Definition: buffer.cc:1165
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: rip-header.cc:207
RipHeader - see RFC 2453
Definition: rip-header.h:157
Ipv4Mask m_subnetMask
Subnet mask.
Definition: rip-header.h:139
Ipv4Address GetNextHop(void) const
Get the next hop.
Definition: rip-header.cc:137
static TypeId GetTypeId(void)
Get the type ID.
Definition: rip-header.cc:36
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:239
uint8_t m_command
command type
Definition: rip-header.h:240
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: rip-header.cc:56
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:923
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: rip-header.cc:72
Ipv4Address GetPrefix(void) const
Get the prefix.
Definition: rip-header.cc:97
Ipv4Address m_prefix
Advertised prefix.
Definition: rip-header.h:138
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
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 m_metric
Route metric.
Definition: rip-header.h:141
void WriteU8(uint8_t data)
Definition: buffer.h:868
void ClearRtes()
Clear all the RTEs from the header.
Definition: rip-header.cc:254
void SetNextHop(Ipv4Address nextHop)
Set the next hop.
Definition: rip-header.cc:132
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: rip-header.cc:185
uint8_t ReadU8(void)
Definition: buffer.h:1020
uint16_t GetRouteTag(void) const
Get the route tag.
Definition: rip-header.cc:117
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
void SetRouteTag(uint16_t routeTag)
Set the route tag.
Definition: rip-header.cc:112
Command_e
Commands to be used in Rip headers.
Definition: rip-header.h:198
uint16_t ReadNtohU16(void)
Definition: buffer.h:945
virtual void Print(std::ostream &os) const
Definition: rip-header.cc:174
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:249
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
uint16_t GetPrefixLength(void) const
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: rip-header.cc:169
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: rip-header.cc:191