A Discrete-Event Network Simulator
API
ripng-header.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 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 "ripng-header.h"
22 #include "ns3/log.h"
23 
24 namespace ns3 {
25 
26 /*
27  * RipNgRte
28  */
30 
32  : m_prefix ("::"), m_tag (0), m_prefixLen (0), m_metric (16)
33 {
34 }
35 
37 {
38  static TypeId tid = TypeId ("ns3::RipNgRte")
39  .SetParent<Header> ()
40  .SetGroupName ("Internet")
41  .AddConstructor<RipNgRte> ();
42  return tid;
43 }
44 
46 {
47  return GetTypeId ();
48 }
49 
50 void RipNgRte::Print (std::ostream & os) const
51 {
52  os << "prefix " << m_prefix << "/" << int(m_prefixLen) << " Metric " << int(m_metric) << " Tag " << int(m_tag);
53 }
54 
55 uint32_t RipNgRte::GetSerializedSize () const
56 {
57  return 20;
58 }
59 
61 {
62  uint8_t tmp[16];
63 
64  m_prefix.Serialize (tmp);
65  i.Write (tmp, 16);
66 
67  i.WriteHtonU16 (m_tag);
68  i.WriteU8 (m_prefixLen);
69  i.WriteU8 (m_metric);
70 }
71 
73 {
74  uint8_t tmp[16];
75 
76  i.Read (tmp, 16);
77  m_prefix.Set (tmp);
78  m_tag = i.ReadNtohU16 ();
79  m_prefixLen = i.ReadU8 ();
80  m_metric = i.ReadU8 ();
81 
82  return GetSerializedSize ();
83 }
84 
86 {
87  m_prefix = prefix;
88 }
89 
91 {
92  return m_prefix;
93 }
94 
95 void RipNgRte::SetPrefixLen (uint8_t prefixLen)
96 {
97  m_prefixLen = prefixLen;
98 }
99 
100 uint8_t RipNgRte::GetPrefixLen () const
101 {
102  return m_prefixLen;
103 }
104 
105 void RipNgRte::SetRouteTag (uint16_t routeTag)
106 {
107  m_tag = routeTag;
108 }
109 
110 uint16_t RipNgRte::GetRouteTag () const
111 {
112  return m_tag;
113 }
114 
115 void RipNgRte::SetRouteMetric (uint8_t routeMetric)
116 {
117  m_metric = routeMetric;
118 }
119 
120 uint8_t RipNgRte::GetRouteMetric () const
121 {
122  return m_metric;
123 }
124 
125 
126 std::ostream & operator << (std::ostream & os, const RipNgRte & h)
127 {
128  h.Print (os);
129  return os;
130 }
131 
132 /*
133  * RipNgHeader
134  */
135 NS_LOG_COMPONENT_DEFINE ("RipNgHeader");
136 NS_OBJECT_ENSURE_REGISTERED (RipNgHeader);
137 
139  : m_command (0)
140 {
141 }
142 
144 {
145  static TypeId tid = TypeId ("ns3::RipNgHeader")
146  .SetParent<Header> ()
147  .SetGroupName ("Internet")
148  .AddConstructor<RipNgHeader> ();
149  return tid;
150 }
151 
153 {
154  return GetTypeId ();
155 }
156 
157 void RipNgHeader::Print (std::ostream & os) const
158 {
159  os << "command " << int(m_command);
160  for (std::list<RipNgRte>::const_iterator iter = m_rteList.begin ();
161  iter != m_rteList.end (); iter ++)
162  {
163  os << " | ";
164  iter->Print (os);
165  }
166 }
167 
169 {
170  RipNgRte rte;
171  return 4 + m_rteList.size () * rte.GetSerializedSize ();
172 }
173 
175 {
177 
178  i.WriteU8 (uint8_t (m_command));
179  i.WriteU8 (1);
180  i.WriteU16 (0);
181 
182  for (std::list<RipNgRte>::const_iterator iter = m_rteList.begin ();
183  iter != m_rteList.end (); iter ++)
184  {
185  iter->Serialize (i);
186  i.Next(iter->GetSerializedSize ());
187  }
188 }
189 
191 {
193 
194  uint8_t temp;
195  temp = i.ReadU8 ();
196  if ((temp == REQUEST) || (temp == RESPONSE))
197  {
198  m_command = temp;
199  }
200  else
201  {
202  return 0;
203  }
204 
205  if (i.ReadU8 () != 1)
206  {
207  NS_LOG_LOGIC ("RIP received a message with mismatch version, ignoring.");
208  return 0;
209  }
210 
211  if (i.ReadU16 () != 0)
212  {
213  NS_LOG_LOGIC ("RIP received a message with invalid filled flags, ignoring.");
214  return 0;
215  }
216 
217  uint8_t rteNumber = i.GetRemainingSize ()/20;
218  for (uint8_t n=0; n<rteNumber; n++)
219  {
220  RipNgRte rte;
221  i.Next (rte.Deserialize (i));
222  m_rteList.push_back (rte);
223  }
224 
225  return GetSerializedSize ();
226 }
227 
229 {
230  m_command = command;
231 }
232 
234 {
236 }
237 
239 {
240  m_rteList.push_back (rte);
241 }
242 
244 {
245  m_rteList.clear ();
246 }
247 
248 uint16_t RipNgHeader::GetRteNumber (void) const
249 {
250  return m_rteList.size ();
251 }
252 
253 std::list<RipNgRte> RipNgHeader::GetRteList (void) const
254 {
255  return m_rteList;
256 }
257 
258 
259 std::ostream & operator << (std::ostream & os, const RipNgHeader & h)
260 {
261  h.Print (os);
262  return os;
263 }
264 
265 
266 }
267 
uint16_t ReadU16(void)
Definition: buffer.h:1029
Protocol header serialization and deserialization.
Definition: header.h:42
virtual void Print(std::ostream &os) const
Command_e
Commands to be used in RipNg headers.
Definition: ripng-header.h:187
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: ripng-header.cc:72
uint32_t GetRemainingSize(void) const
Definition: buffer.cc:1165
def start()
Definition: core.py:1844
void SetPrefix(Ipv6Address prefix)
Set the prefix.
Definition: ripng-header.cc:85
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint8_t m_prefixLen
prefix length
Definition: ripng-header.h:128
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: ripng-header.cc:45
uint8_t GetRouteMetric(void) const
Get the route metric.
iterator in a Buffer instance
Definition: buffer.h:98
Ipv6Address m_prefix
prefix
Definition: ripng-header.h:126
uint16_t GetRteNumber(void) const
Get the number of RTE included in the message.
void AddRte(RipNgRte rte)
Add a RTE to the message.
uint8_t m_command
command type
Definition: ripng-header.h:229
Ipv6Address GetPrefix(void) const
Get the prefix.
Definition: ripng-header.cc:90
uint8_t m_metric
route metric
Definition: ripng-header.h:129
void WriteU16(uint16_t data)
Definition: buffer.cc:870
void WriteHtonU16(uint16_t data)
Definition: buffer.h:905
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
void Next(void)
go forward by one byte
Definition: buffer.h:845
void SetRouteTag(uint16_t routeTag)
Set the route tag.
static TypeId GetTypeId(void)
Get the type ID.
Definition: ripng-header.cc:36
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
RipNgHeader - see RFC 2080
Definition: ripng-header.h:146
void Set(char const *address)
Sets an Ipv6Address by parsing the input C-string.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
void SetRouteMetric(uint8_t routeMetric)
Set the route metric.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::list< RipNgRte > m_rteList
list of the RTEs in the message
Definition: ripng-header.h:230
Command_e GetCommand(void) const
Get the command.
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: ripng-header.cc:60
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1123
uint16_t m_tag
route tag
Definition: ripng-header.h:127
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Describes an IPv6 address.
Definition: ipv6-address.h:49
uint8_t GetPrefixLen(void) const
Get the prefix length.
void WriteU8(uint8_t data)
Definition: buffer.h:869
static TypeId GetTypeId(void)
Get the type ID.
virtual void Print(std::ostream &os) const
Definition: ripng-header.cc:50
std::list< RipNgRte > GetRteList(void) const
Get the list of the RTEs included in the message.
void SetCommand(Command_e command)
Set the command.
uint8_t ReadU8(void)
Definition: buffer.h:1021
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:953
uint16_t GetRouteTag(void) const
Get the route tag.
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ripng-header.cc:55
void ClearRtes()
Clear all the RTEs from the header.
uint16_t ReadNtohU16(void)
Definition: buffer.h:946
RipNg Routing Table Entry (RTE) - see RFC 2080
Definition: ripng-header.h:38
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.
void SetPrefixLen(uint8_t prefixLen)
Set the prefix length.
Definition: ripng-header.cc:95