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 (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
201  {
202  os << " | ";
203  iter->Print(os);
204  }
205 }
206 
207 uint32_t
209 {
210  RipRte rte;
211  return 4 + m_rteList.size() * rte.GetSerializedSize();
212 }
213 
214 void
216 {
218 
219  i.WriteU8(uint8_t(m_command));
220  i.WriteU8(2);
221  i.WriteU16(0);
222 
223  for (auto iter = m_rteList.begin(); iter != m_rteList.end(); iter++)
224  {
225  iter->Serialize(i);
226  i.Next(iter->GetSerializedSize());
227  }
228 }
229 
230 uint32_t
232 {
234 
235  uint8_t temp;
236  temp = i.ReadU8();
237  if ((temp == REQUEST) || (temp == RESPONSE))
238  {
239  m_command = temp;
240  }
241  else
242  {
243  return 0;
244  }
245 
246  if (i.ReadU8() != 2)
247  {
248  NS_LOG_LOGIC("RIP received a message with mismatch version, ignoring.");
249  return 0;
250  }
251 
252  if (i.ReadU16() != 0)
253  {
254  NS_LOG_LOGIC("RIP received a message with invalid filled flags, ignoring.");
255  return 0;
256  }
257 
258  uint8_t rteNumber = i.GetRemainingSize() / 20;
259  for (uint8_t n = 0; n < rteNumber; n++)
260  {
261  RipRte rte;
262  i.Next(rte.Deserialize(i));
263  m_rteList.push_back(rte);
264  }
265 
266  return GetSerializedSize();
267 }
268 
269 void
271 {
272  m_command = command;
273 }
274 
277 {
279 }
280 
281 void
283 {
284  m_rteList.push_back(rte);
285 }
286 
287 void
289 {
290  m_rteList.clear();
291 }
292 
293 uint16_t
295 {
296  return m_rteList.size();
297 }
298 
299 std::list<RipRte>
301 {
302  return m_rteList;
303 }
304 
305 std::ostream&
306 operator<<(std::ostream& os, const RipHeader& h)
307 {
308  h.Print(os);
309  return os;
310 }
311 
312 } // 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 WriteU16(uint16_t data)
Definition: buffer.cc:859
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:42
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:257
void Set(uint32_t mask)
input mask is in host order.
Definition: ipv4-address.cc:91
uint16_t GetPrefixLength() const
uint32_t Get() const
Get the host-order 32-bit IP mask.
Definition: ipv4-address.cc:84
RipHeader - see RFC 2453
Definition: rip-header.h:158
void Serialize(Buffer::Iterator start) const override
Serialize the packet.
Definition: rip-header.cc:215
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:208
uint16_t GetRteNumber() const
Get the number of RTE included in the message.
Definition: rip-header.cc:294
void AddRte(RipRte rte)
Add a RTE to the message.
Definition: rip-header.cc:282
std::list< RipRte > m_rteList
list of the RTEs in the message
Definition: rip-header.h:241
void SetCommand(Command_e command)
Set the command.
Definition: rip-header.cc:270
Command_e
Commands to be used in Rip headers.
Definition: rip-header.h:199
uint8_t m_command
command type
Definition: rip-header.h:240
void ClearRtes()
Clear all the RTEs from the header.
Definition: rip-header.cc:288
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:300
Command_e GetCommand() const
Get the command.
Definition: rip-header.cc:276
Rip v2 Routing Table Entry (RTE) - see RFC 2453.
Definition: rip-header.h:39
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:138
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:141
Ipv4Mask m_subnetMask
Subnet mask.
Definition: rip-header.h:139
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:137
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:140
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:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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