A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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").SetParent<Header> ().AddConstructor<RipNgRte> ();
39  return tid;
40 }
41 
43 {
44  return GetTypeId ();
45 }
46 
47 void RipNgRte::Print (std::ostream & os) const
48 {
49  os << "prefix " << m_prefix << "/" << int(m_prefixLen) << " Metric " << int(m_metric) << " Tag " << int(m_tag);
50 }
51 
52 uint32_t RipNgRte::GetSerializedSize () const
53 {
54  return 20;
55 }
56 
58 {
59  uint8_t tmp[16];
60 
61  m_prefix.Serialize (tmp);
62  i.Write (tmp, 16);
63 
64  i.WriteHtonU16 (m_tag);
65  i.WriteU8 (m_prefixLen);
66  i.WriteU8 (m_metric);
67 }
68 
70 {
71  uint8_t tmp[16];
72 
73  i.Read (tmp, 16);
74  m_prefix.Set (tmp);
75  m_tag = i.ReadNtohU16 ();
76  m_prefixLen = i.ReadU8 ();
77  m_metric = i.ReadU8 ();
78 
79  return GetSerializedSize ();
80 }
81 
83 {
84  m_prefix = prefix;
85 }
86 
88 {
89  return m_prefix;
90 }
91 
92 void RipNgRte::SetPrefixLen (uint8_t prefixLen)
93 {
94  m_prefixLen = prefixLen;
95 }
96 
97 uint8_t RipNgRte::GetPrefixLen () const
98 {
99  return m_prefixLen;
100 }
101 
102 void RipNgRte::SetRouteTag (uint16_t routeTag)
103 {
104  m_tag = routeTag;
105 }
106 
107 uint16_t RipNgRte::GetRouteTag () const
108 {
109  return m_tag;
110 }
111 
112 void RipNgRte::SetRouteMetric (uint8_t routeMetric)
113 {
114  m_metric = routeMetric;
115 }
116 
117 uint8_t RipNgRte::GetRouteMetric () const
118 {
119  return m_metric;
120 }
121 
122 
123 std::ostream & operator << (std::ostream & os, const RipNgRte & h)
124 {
125  h.Print (os);
126  return os;
127 }
128 
129 /*
130  * RipNgHeader
131  */
132 NS_OBJECT_ENSURE_REGISTERED (RipNgHeader)
133  ;
134 
136  : m_command (0)
137 {
138 }
139 
141 {
142  static TypeId tid = TypeId ("ns3::RipNgHeader").SetParent<Header> ().AddConstructor<RipNgHeader> ();
143  return tid;
144 }
145 
147 {
148  return GetTypeId ();
149 }
150 
151 void RipNgHeader::Print (std::ostream & os) const
152 {
153  os << "command " << int(m_command);
154  for (std::list<RipNgRte>::const_iterator iter = m_rteList.begin ();
155  iter != m_rteList.end (); iter ++)
156  {
157  os << " | ";
158  iter->Print (os);
159  }
160 }
161 
163 {
164  RipNgRte rte;
165  return 4 + m_rteList.size () * rte.GetSerializedSize ();
166 }
167 
169 {
171 
172  i.WriteU8 (uint8_t (m_command));
173  i.WriteU8 (1);
174  i.WriteU16 (0);
175 
176  for (std::list<RipNgRte>::const_iterator iter = m_rteList.begin ();
177  iter != m_rteList.end (); iter ++)
178  {
179  iter->Serialize (i);
180  i.Next(iter->GetSerializedSize ());
181  }
182 }
183 
185 {
187 
188  uint8_t temp;
189  temp = i.ReadU8 ();
190  if ((temp == REQUEST) || (temp == RESPONSE))
191  {
192  m_command = temp;
193  }
194  else
195  {
196  return 0;
197  }
198 
199  temp = i.ReadU8 ();
200  NS_ASSERT_MSG (temp == 1, "RipNG received a message with mismatch version, aborting.");
201 
202  uint16_t temp16 = i.ReadU16 ();
203  NS_ASSERT_MSG (temp16 == 0, "RipNG received a message with invalid filled flags, aborting.");
204 
205  uint8_t rteNumber = (i.GetSize () - 4)/20;
206  for (uint8_t n=0; n<rteNumber; n++)
207  {
208  RipNgRte rte;
209  i.Next (rte.Deserialize (i));
210  m_rteList.push_back (rte);
211  }
212 
213  return GetSerializedSize ();
214 }
215 
217 {
218  m_command = command;
219 }
220 
222 {
224 }
225 
227 {
228  m_rteList.push_back (rte);
229 }
230 
232 {
233  m_rteList.clear ();
234 }
235 
236 uint16_t RipNgHeader::GetRteNumber (void) const
237 {
238  return m_rteList.size ();
239 }
240 
241 std::list<RipNgRte> RipNgHeader::GetRteList (void) const
242 {
243  return m_rteList;
244 }
245 
246 
247 std::ostream & operator << (std::ostream & os, const RipNgHeader & h)
248 {
249  h.Print (os);
250  return os;
251 }
252 
253 
254 }
255 
uint16_t ReadU16(void)
Definition: buffer.h:1036
Protocol header serialization and deserialization.
Definition: header.h:42
uint16_t GetRteNumber(void) const
Get the number of RTE included in the message.
std::list< RipNgRte > GetRteList(void) const
Get the list of the RTEs included in the message.
Command_e
Commands to be used in RipNg headers.
Definition: ripng-header.h:177
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
Definition: ripng-header.cc:42
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
Definition: ripng-header.cc:69
void SetPrefix(Ipv6Address prefix)
Set the prefix.
Definition: ripng-header.cc:82
uint8_t m_prefixLen
prefix length
Definition: ripng-header.h:127
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:125
void AddRte(RipNgRte rte)
Add a RTE to the message.
uint8_t m_command
command type
Definition: ripng-header.h:219
uint8_t m_metric
route metric
Definition: ripng-header.h:128
void WriteU16(uint16_t data)
Definition: buffer.cc:899
Ipv6Address GetPrefix(void) const
Get the prefix.
Definition: ripng-header.cc:87
void WriteHtonU16(uint16_t data)
Definition: buffer.h:912
virtual uint32_t Deserialize(Buffer::Iterator start)
Deserialize the packet.
void Next(void)
go forward by one byte
Definition: buffer.h:852
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:136
virtual void Serialize(Buffer::Iterator start) const
Serialize the packet.
Definition: ripng-header.cc:57
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:43
void SetRouteMetric(uint8_t routeMetric)
Set the route metric.
std::list< RipNgRte > m_rteList
list of the RTEs in the message
Definition: ripng-header.h:220
virtual void Print(std::ostream &os) const
Definition: ripng-header.cc:47
void Read(uint8_t *buffer, uint32_t size)
Definition: buffer.cc:1152
virtual void Print(std::ostream &os) const
uint16_t m_tag
route tag
Definition: ripng-header.h:126
virtual TypeId GetInstanceTypeId(void) const
Return the instance type identifier.
uint8_t GetPrefixLen(void) const
Get the prefix length.
Definition: ripng-header.cc:97
#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:84
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ripng-header.cc:52
Describes an IPv6 address.
Definition: ipv6-address.h:46
void WriteU8(uint8_t data)
Definition: buffer.h:876
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:1028
void Write(uint8_t const *buffer, uint32_t size)
Definition: buffer.cc:982
Command_e GetCommand(void) const
Get the command.
void ClearRtes()
Clear all the RTEs from the header.
uint16_t ReadNtohU16(void)
Definition: buffer.h:953
RipNg Routing Table Entry (RTE) - see RFC 2080
Definition: ripng-header.h:37
uint32_t GetSize(void) const
Definition: buffer.cc:1187
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void SetPrefixLen(uint8_t prefixLen)
Set the prefix length.
Definition: ripng-header.cc:92
void Serialize(uint8_t buf[16]) const
Serialize this address to a 16-byte buffer.