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 
23 namespace ns3 {
24 
25 /*
26  * RipNgRte
27  */
29  ;
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_OBJECT_ENSURE_REGISTERED (RipNgHeader)
136  ;
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  temp = i.ReadU8 ();
206  NS_ASSERT_MSG (temp == 1, "RipNG received a message with mismatch version, aborting.");
207 
208  uint16_t temp16 = i.ReadU16 ();
209  NS_ASSERT_MSG (temp16 == 0, "RipNG received a message with invalid filled flags, aborting.");
210 
211  uint8_t rteNumber = i.GetRemainingSize ()/20;
212  for (uint8_t n=0; n<rteNumber; n++)
213  {
214  RipNgRte rte;
215  i.Next (rte.Deserialize (i));
216  m_rteList.push_back (rte);
217  }
218 
219  return GetSerializedSize ();
220 }
221 
223 {
224  m_command = command;
225 }
226 
228 {
230 }
231 
233 {
234  m_rteList.push_back (rte);
235 }
236 
238 {
239  m_rteList.clear ();
240 }
241 
242 uint16_t RipNgHeader::GetRteNumber (void) const
243 {
244  return m_rteList.size ();
245 }
246 
247 std::list<RipNgRte> RipNgHeader::GetRteList (void) const
248 {
249  return m_rteList;
250 }
251 
252 
253 std::ostream & operator << (std::ostream & os, const RipNgHeader & h)
254 {
255  h.Print (os);
256  return os;
257 }
258 
259 
260 }
261 
uint16_t ReadU16(void)
Definition: buffer.h:1028
Protocol header serialization and deserialization.
Definition: header.h:42
uint16_t GetRteNumber(void) const
Get the number of RTE included in the message.
Command_e
Commands to be used in RipNg headers.
Definition: ripng-header.h:187
std::list< RipNgRte > GetRteList(void) const
Get the list of the RTEs included in the message.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: ripng-header.cc:45
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: ripng-header.cc:72
def start()
Definition: core.py:1482
void SetPrefix(Ipv6Address prefix)
Set the prefix.
Definition: ripng-header.cc:85
uint8_t m_prefixLen
prefix length
Definition: ripng-header.h:128
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
iterator in a Buffer instance
Definition: buffer.h:98
Ipv6Address m_prefix
prefix
Definition: ripng-header.h:126
void AddRte(RipNgRte rte)
Add a RTE to the message.
uint8_t m_command
command type
Definition: ripng-header.h:229
uint8_t m_metric
route metric
Definition: ripng-header.h:129
void WriteU16(uint16_t data)
Definition: buffer.cc:870
Ipv6Address GetPrefix(void) const
Get the prefix.
Definition: ripng-header.cc:90
void WriteHtonU16(uint16_t data)
Definition: buffer.h:904
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
void Next(void)
go forward by one byte
Definition: buffer.h:844
uint32_t GetRemainingSize(void) const
Definition: buffer.cc:1165
void SetRouteTag(uint16_t routeTag)
Set the route tag.
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
static TypeId GetTypeId(void)
Get the type ID.
Definition: ripng-header.cc:36
RipNgHeader - see RFC 2080
Definition: ripng-header.h:146
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: ripng-header.cc:60
void Set(char const *address)
Sets an Ipv6Address by parsing the input C-string.
uint16_t GetRouteTag(void) const
Get the route tag.
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
virtual void Print(std::ostream &os) const
Definition: ripng-header.cc:50
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1123
virtual void Print(std::ostream &os) const
uint16_t m_tag
route tag
Definition: ripng-header.h:127
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
uint8_t GetPrefixLen(void) const
Get the prefix length.
#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
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ripng-header.cc:55
Describes an IPv6 address.
Definition: ipv6-address.h:48
void WriteU8(uint8_t data)
Definition: buffer.h:868
static TypeId GetTypeId(void)
Get the type ID.
uint8_t GetRouteMetric(void) const
Get the route metric.
void SetCommand(Command_e command)
Set the command.
uint8_t ReadU8(void)
Definition: buffer.h:1020
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:953
Command_e GetCommand(void) const
Get the command.
void ClearRtes()
Clear all the RTEs from the header.
uint16_t ReadNtohU16(void)
Definition: buffer.h:945
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:904
void SetPrefixLen(uint8_t prefixLen)
Set the prefix length.
Definition: ripng-header.cc:95
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.