A Discrete-Event Network Simulator
API
dhcp-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 UPB
4  * Copyright (c) 2017 NITK Surathkal
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Radu Lupu <rlupu@elcom.pub.ro>
20  * Ankit Deepak <adadeepak8@gmail.com>
21  * Deepti Rajagopal <deeptir96@gmail.com>
22  *
23  */
24 
25 #include "dhcp-helper.h"
26 #include "ns3/dhcp-server.h"
27 #include "ns3/dhcp-client.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/names.h"
30 #include "ns3/ipv4.h"
31 #include "ns3/loopback-net-device.h"
32 #include "ns3/traffic-control-layer.h"
33 #include "ns3/traffic-control-helper.h"
34 #include "ns3/log.h"
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("DhcpHelper");
39 
41 {
44 }
45 
47  std::string name,
48  const AttributeValue &value)
49 {
50  m_clientFactory.Set (name, value);
51 }
52 
54  std::string name,
55  const AttributeValue &value)
56 {
57  m_serverFactory.Set (name, value);
58 }
59 
61 {
62  return ApplicationContainer (InstallDhcpClientPriv (netDevice));
63 }
64 
66 {
68  for (NetDeviceContainer::Iterator i = netDevices.Begin (); i != netDevices.End (); ++i)
69  {
70  apps.Add (InstallDhcpClientPriv (*i));
71  }
72  return apps;
73 }
74 
76 {
77  Ptr<Node> node = netDevice->GetNode ();
78  NS_ASSERT_MSG (node != 0, "DhcpClientHelper: NetDevice is not not associated with any node -> fail");
79 
80  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
81  NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
82  " with a node without IPv4 stack installed -> fail "
83  "(maybe need to use InternetStackHelper?)");
84 
85  int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
86  if (interface == -1)
87  {
88  interface = ipv4->AddInterface (netDevice);
89  }
90  NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");
91 
92  ipv4->SetMetric (interface, 1);
93  ipv4->SetUp (interface);
94 
95  // Install the default traffic control configuration if the traffic
96  // control layer has been aggregated, if this is not
97  // a loopback interface, and there is no queue disc installed already
99  if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
100  {
101  NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration");
103  tcHelper.Install (netDevice);
104  }
105 
106  Ptr<DhcpClient> app = DynamicCast <DhcpClient> (m_clientFactory.Create<DhcpClient> ());
107  app->SetDhcpClientNetDevice (netDevice);
108  node->AddApplication (app);
109 
110  return app;
111 }
112 
114  Ipv4Address poolAddr, Ipv4Mask poolMask,
115  Ipv4Address minAddr, Ipv4Address maxAddr,
116  Ipv4Address gateway)
117 {
118  m_serverFactory.Set ("PoolAddresses", Ipv4AddressValue (poolAddr));
119  m_serverFactory.Set ("PoolMask", Ipv4MaskValue (poolMask));
120  m_serverFactory.Set ("FirstAddress", Ipv4AddressValue (minAddr));
121  m_serverFactory.Set ("LastAddress", Ipv4AddressValue (maxAddr));
122  m_serverFactory.Set ("Gateway", Ipv4AddressValue (gateway));
123 
124  Ptr<Node> node = netDevice->GetNode ();
125  NS_ASSERT_MSG (node != 0, "DhcpHelper: NetDevice is not not associated with any node -> fail");
126 
127  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
128  NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
129  " with a node without IPv4 stack installed -> fail "
130  "(maybe need to use InternetStackHelper?)");
131 
132  int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
133  if (interface == -1)
134  {
135  interface = ipv4->AddInterface (netDevice);
136  }
137  NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");
138 
139  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (serverAddr, poolMask);
140  ipv4->AddAddress (interface, ipv4Addr);
141  ipv4->SetMetric (interface, 1);
142  ipv4->SetUp (interface);
143 
144  // Install the default traffic control configuration if the traffic
145  // control layer has been aggregated, if this is not
146  // a loopback interface, and there is no queue disc installed already
148  if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
149  {
150  NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration");
152  tcHelper.Install (netDevice);
153  }
154 
155  // check that the already fixed addresses are not in conflict with the pool
156  std::list<Ipv4Address>::iterator iter;
157  for (iter=m_fixedAddresses.begin (); iter!=m_fixedAddresses.end (); iter ++)
158  {
159  if (iter->Get () >= minAddr.Get () && iter->Get () <= maxAddr.Get ())
160  {
161  NS_ABORT_MSG ("DhcpHelper: Fixed address can not conflict with a pool: " << *iter << " is in [" << minAddr << ", " << maxAddr << "]");
162  }
163  }
164  m_addressPools.push_back (std::make_pair (minAddr, maxAddr));
165 
167  node->AddApplication (app);
168  return ApplicationContainer (app);
169 }
170 
172 {
173  Ipv4InterfaceContainer retval;
174 
175  Ptr<Node> node = netDevice->GetNode ();
176  NS_ASSERT_MSG (node != 0, "DhcpHelper: NetDevice is not not associated with any node -> fail");
177 
178  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
179  NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
180  " with a node without IPv4 stack installed -> fail "
181  "(maybe need to use InternetStackHelper?)");
182 
183  int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
184  if (interface == -1)
185  {
186  interface = ipv4->AddInterface (netDevice);
187  }
188  NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");
189 
190  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (addr, mask);
191  ipv4->AddAddress (interface, ipv4Addr);
192  ipv4->SetMetric (interface, 1);
193  ipv4->SetUp (interface);
194  retval.Add (ipv4, interface);
195 
196  // Install the default traffic control configuration if the traffic
197  // control layer has been aggregated, if this is not
198  // a loopback interface, and there is no queue disc installed already
200  if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
201  {
202  NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration");
204  tcHelper.Install (netDevice);
205  }
206 
207  // check that the already fixed addresses are not in conflict with the pool
208  std::list<std::pair<Ipv4Address, Ipv4Address> >::iterator iter;
209  for (iter=m_addressPools.begin (); iter!=m_addressPools.end (); iter ++)
210  {
211  if (addr.Get () >= iter->first.Get () && addr.Get () <= iter->second.Get ())
212  {
213  NS_ABORT_MSG ("DhcpHelper: Fixed address can not conflict with a pool: " << addr << " is in [" << iter->first << ", " << iter->second << "]");
214  }
215  }
216  m_fixedAddresses.push_back (addr);
217  return retval;
218 }
219 
220 } // namespace ns3
holds a vector of ns3::Application pointers.
void SetServerAttribute(std::string name, const AttributeValue &value)
Set DHCP server attributes.
Definition: dhcp-helper.cc:53
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
ApplicationContainer InstallDhcpServer(Ptr< NetDevice > netDevice, Ipv4Address serverAddr, Ipv4Address poolAddr, Ipv4Mask poolMask, Ipv4Address minAddr, Ipv4Address maxAddr, Ipv4Address gateway=Ipv4Address())
Install DHCP server of a node / NetDevice.
Definition: dhcp-helper.cc:113
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
static TypeId GetTypeId(void)
Get the type ID.
Definition: dhcp-server.cc:49
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
QueueDiscContainer Install(NetDeviceContainer c)
void SetClientAttribute(std::string name, const AttributeValue &value)
Set DHCP client attributes.
Definition: dhcp-helper.cc:46
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
holds a vector of std::pair of Ptr and interface index.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Introspection did not find any typical Config paths.
std::list< Ipv4Address > m_fixedAddresses
list of fixed addresses already allocated.
Definition: dhcp-helper.h:113
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
Hold a value for an Attribute.
Definition: attribute.h:68
std::list< std::pair< Ipv4Address, Ipv4Address > > m_addressPools
list of address pools.
Definition: dhcp-helper.h:114
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Implements the functionality of a DHCP server.
Definition: dhcp-server.h:48
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
Definition: dhcp-helper.cc:171
ObjectFactory m_serverFactory
DHCP server factory.
Definition: dhcp-helper.h:112
static TypeId GetTypeId(void)
Get the type ID.
Definition: dhcp-client.cc:55
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
holds a vector of ns3::NetDevice pointers
Build a set of QueueDisc objects.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ObjectFactory m_clientFactory
DHCP client factory.
Definition: dhcp-helper.h:111
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:329
Ptr< Application > InstallDhcpClientPriv(Ptr< NetDevice > netDevice) const
Function to install DHCP client on a node.
Definition: dhcp-helper.cc:75
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:60
#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
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
a class to store IPv4 address information on an interface
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
AttributeValue implementation for Ipv4Mask.
Definition: ipv4-address.h:330
Implements the functionality of a DHCP client.
Definition: dhcp-client.h:48
static TrafficControlHelper Default(void)
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.