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