A Discrete-Event Network Simulator
API
ripng-helper.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 "ns3/node.h"
22 #include "ns3/node-list.h"
23 #include "ns3/ipv6-list-routing.h"
24 #include "ns3/ripng.h"
25 #include "ripng-helper.h"
26 
27 namespace ns3 {
28 
30 {
31  m_factory.SetTypeId ("ns3::RipNg");
32 }
33 
35  : m_factory (o.m_factory)
36 {
39 }
40 
42 {
43  m_interfaceExclusions.clear ();
44  m_interfaceMetrics.clear ();
45 }
46 
48 RipNgHelper::Copy (void) const
49 {
50  return new RipNgHelper (*this);
51 }
52 
55 {
56  Ptr<RipNg> ripng = m_factory.Create<RipNg> ();
57 
58  std::map<Ptr<Node>, std::set<uint32_t> >::const_iterator it = m_interfaceExclusions.find (node);
59 
60  if(it != m_interfaceExclusions.end ())
61  {
62  ripng->SetInterfaceExclusions (it->second);
63  }
64 
65  std::map< Ptr<Node>, std::map<uint32_t, uint8_t> >::const_iterator iter = m_interfaceMetrics.find (node);
66 
67  if(iter != m_interfaceMetrics.end ())
68  {
69  std::map<uint32_t, uint8_t>::const_iterator subiter;
70  for (subiter = iter->second.begin (); subiter != iter->second.end (); subiter++)
71  {
72  ripng->SetInterfaceMetric (subiter->first, subiter->second);
73  }
74  }
75 
76  node->AggregateObject (ripng);
77  return ripng;
78 }
79 
80 void
81 RipNgHelper::Set (std::string name, const AttributeValue &value)
82 {
83  m_factory.Set (name, value);
84 }
85 
86 
87 int64_t
89 {
90  int64_t currentStream = stream;
91  Ptr<Node> node;
92  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
93  {
94  node = (*i);
95  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
96  NS_ASSERT_MSG (ipv6, "Ipv6 not installed on node");
97  Ptr<Ipv6RoutingProtocol> proto = ipv6->GetRoutingProtocol ();
98  NS_ASSERT_MSG (proto, "Ipv6 routing not installed on node");
99  Ptr<RipNg> ripng = DynamicCast<RipNg> (proto);
100  if (ripng)
101  {
102  currentStream += ripng->AssignStreams (currentStream);
103  continue;
104  }
105  // RIPng may also be in a list
106  Ptr<Ipv6ListRouting> list = DynamicCast<Ipv6ListRouting> (proto);
107  if (list)
108  {
109  int16_t priority;
110  Ptr<Ipv6RoutingProtocol> listProto;
111  Ptr<RipNg> listRipng;
112  for (uint32_t i = 0; i < list->GetNRoutingProtocols (); i++)
113  {
114  listProto = list->GetRoutingProtocol (i, priority);
115  listRipng = DynamicCast<RipNg> (listProto);
116  if (listRipng)
117  {
118  currentStream += listRipng->AssignStreams (currentStream);
119  break;
120  }
121  }
122  }
123  }
124  return (currentStream - stream);
125 }
126 
127 void RipNgHelper::SetDefaultRouter (Ptr<Node> node, Ipv6Address nextHop, uint32_t interface)
128 {
129  Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
130  NS_ASSERT_MSG (ipv6, "Ipv6 not installed on node");
131  Ptr<Ipv6RoutingProtocol> proto = ipv6->GetRoutingProtocol ();
132  NS_ASSERT_MSG (proto, "Ipv6 routing not installed on node");
133  Ptr<RipNg> ripng = DynamicCast<RipNg> (proto);
134  if (ripng)
135  {
136  ripng->AddDefaultRouteTo (nextHop, interface);
137  }
138  // RIPng may also be in a list
139  Ptr<Ipv6ListRouting> list = DynamicCast<Ipv6ListRouting> (proto);
140  if (list)
141  {
142  int16_t priority;
143  Ptr<Ipv6RoutingProtocol> listProto;
144  Ptr<RipNg> listRipng;
145  for (uint32_t i = 0; i < list->GetNRoutingProtocols (); i++)
146  {
147  listProto = list->GetRoutingProtocol (i, priority);
148  listRipng = DynamicCast<RipNg> (listProto);
149  if (listRipng)
150  {
151  listRipng->AddDefaultRouteTo (nextHop, interface);
152  break;
153  }
154  }
155  }
156 }
157 
158 void
159 RipNgHelper::ExcludeInterface (Ptr<Node> node, uint32_t interface)
160 {
161  std::map< Ptr<Node>, std::set<uint32_t> >::iterator it = m_interfaceExclusions.find (node);
162 
163  if (it == m_interfaceExclusions.end ())
164  {
165  std::set<uint32_t> interfaces;
166  interfaces.insert (interface);
167 
168  m_interfaceExclusions.insert (std::make_pair (node, interfaces));
169  }
170  else
171  {
172  it->second.insert (interface);
173  }
174 }
175 
176 void RipNgHelper::SetInterfaceMetric (Ptr<Node> node, uint32_t interface, uint8_t metric)
177 {
178  m_interfaceMetrics[node][interface] = metric;
179 }
180 
181 }
182 
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void SetDefaultRouter(Ptr< Node > node, Ipv6Address nextHop, uint32_t interface)
Install a default route in the node.
ObjectFactory m_factory
Object Factory.
Definition: ripng-helper.h:144
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
Hold a value for an Attribute.
Definition: attribute.h:68
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
void ExcludeInterface(Ptr< Node > node, uint32_t interface)
Exclude an interface from RIPng protocol.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Helper class that adds RIPng routing to nodes.
Definition: ripng-helper.h:40
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
std::map< Ptr< Node >, std::map< uint32_t, uint8_t > > m_interfaceMetrics
Interface Metric set.
Definition: ripng-helper.h:147
RIPng Routing Protocol, defined in RFC 2080.
Definition: ripng.h:174
virtual Ptr< Ipv6RoutingProtocol > Create(Ptr< Node > node) const
Definition: ripng-helper.cc:54
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetInterfaceMetric(Ptr< Node > node, uint32_t interface, uint8_t metric)
Set a metric for an interface.
#define list
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
virtual ~RipNgHelper()
Definition: ripng-helper.cc:41
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void Set(std::string name, const AttributeValue &value)
Definition: ripng-helper.cc:81
RipNgHelper * Copy(void) const
Definition: ripng-helper.cc:48
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
Describes an IPv6 address.
Definition: ipv6-address.h:49
std::map< Ptr< Node >, std::set< uint32_t > > m_interfaceExclusions
Interface Exclusion set.
Definition: ripng-helper.h:146
int64_t AssignStreams(NodeContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: ripng-helper.cc:88
interfaces
Definition: first.py:41
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.