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/net-device-queue-interface.h"
35 #include "ns3/log.h"
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("DhcpHelper");
40 
42 {
45 }
46 
48  std::string name,
49  const AttributeValue &value)
50 {
51  m_clientFactory.Set (name, value);
52 }
53 
55  std::string name,
56  const AttributeValue &value)
57 {
58  m_serverFactory.Set (name, value);
59 }
60 
62 {
63  return ApplicationContainer (InstallDhcpClientPriv (netDevice));
64 }
65 
67 {
69  for (NetDeviceContainer::Iterator i = netDevices.Begin (); i != netDevices.End (); ++i)
70  {
71  apps.Add (InstallDhcpClientPriv (*i));
72  }
73  return apps;
74 }
75 
77 {
78  Ptr<Node> node = netDevice->GetNode ();
79  NS_ASSERT_MSG (node != 0, "DhcpClientHelper: NetDevice is not not associated with any node -> fail");
80 
81  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
82  NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
83  " with a node without IPv4 stack installed -> fail "
84  "(maybe need to use InternetStackHelper?)");
85 
86  int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
87  if (interface == -1)
88  {
89  interface = ipv4->AddInterface (netDevice);
90  }
91  NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");
92 
93  ipv4->SetMetric (interface, 1);
94  ipv4->SetUp (interface);
95 
96  // Install the default traffic control configuration if the traffic
97  // control layer has been aggregated, if this is not
98  // a loopback interface, and there is no queue disc installed already
100  if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
101  {
102  Ptr<NetDeviceQueueInterface> ndqi = netDevice->GetObject<NetDeviceQueueInterface> ();
103  // It is useless to install a queue disc if the device has no
104  // NetDeviceQueueInterface attached: the device queue is never
105  // stopped and every packet enqueued in the queue disc is
106  // immediately dequeued, hence there will never be backlog
107  if (ndqi)
108  {
109  std::size_t nTxQueues = ndqi->GetNTxQueues ();
110  NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration ("
111  << nTxQueues << " device queue(s))");
113  tcHelper.Install (netDevice);
114  }
115  }
116 
117  Ptr<DhcpClient> app = DynamicCast <DhcpClient> (m_clientFactory.Create<DhcpClient> ());
118  app->SetDhcpClientNetDevice (netDevice);
119  node->AddApplication (app);
120 
121  return app;
122 }
123 
125  Ipv4Address poolAddr, Ipv4Mask poolMask,
126  Ipv4Address minAddr, Ipv4Address maxAddr,
127  Ipv4Address gateway)
128 {
129  m_serverFactory.Set ("PoolAddresses", Ipv4AddressValue (poolAddr));
130  m_serverFactory.Set ("PoolMask", Ipv4MaskValue (poolMask));
131  m_serverFactory.Set ("FirstAddress", Ipv4AddressValue (minAddr));
132  m_serverFactory.Set ("LastAddress", Ipv4AddressValue (maxAddr));
133  m_serverFactory.Set ("Gateway", Ipv4AddressValue (gateway));
134 
135  Ptr<Node> node = netDevice->GetNode ();
136  NS_ASSERT_MSG (node != 0, "DhcpHelper: NetDevice is not not associated with any node -> fail");
137 
138  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
139  NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
140  " with a node without IPv4 stack installed -> fail "
141  "(maybe need to use InternetStackHelper?)");
142 
143  int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
144  if (interface == -1)
145  {
146  interface = ipv4->AddInterface (netDevice);
147  }
148  NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");
149 
150  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (serverAddr, poolMask);
151  ipv4->AddAddress (interface, ipv4Addr);
152  ipv4->SetMetric (interface, 1);
153  ipv4->SetUp (interface);
154 
155  // Install the default traffic control configuration if the traffic
156  // control layer has been aggregated, if this is not
157  // a loopback interface, and there is no queue disc installed already
159  if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
160  {
161  Ptr<NetDeviceQueueInterface> ndqi = netDevice->GetObject<NetDeviceQueueInterface> ();
162  // It is useless to install a queue disc if the device has no
163  // NetDeviceQueueInterface attached: the device queue is never
164  // stopped and every packet enqueued in the queue disc is
165  // immediately dequeued, hence there will never be backlog
166  if (ndqi)
167  {
168  std::size_t nTxQueues = ndqi->GetNTxQueues ();
169  NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration ("
170  << nTxQueues << " device queue(s))");
172  tcHelper.Install (netDevice);
173  }
174  }
175 
176  // check that the already fixed addresses are not in conflict with the pool
177  std::list<Ipv4Address>::iterator iter;
178  for (iter=m_fixedAddresses.begin (); iter!=m_fixedAddresses.end (); iter ++)
179  {
180  if (iter->Get () >= minAddr.Get () && iter->Get () <= maxAddr.Get ())
181  {
182  NS_ABORT_MSG ("DhcpHelper: Fixed address can not conflict with a pool: " << *iter << " is in [" << minAddr << ", " << maxAddr << "]");
183  }
184  }
185  m_addressPools.push_back (std::make_pair (minAddr, maxAddr));
186 
188  node->AddApplication (app);
189  return ApplicationContainer (app);
190 }
191 
193 {
194  Ipv4InterfaceContainer retval;
195 
196  Ptr<Node> node = netDevice->GetNode ();
197  NS_ASSERT_MSG (node != 0, "DhcpHelper: NetDevice is not not associated with any node -> fail");
198 
199  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
200  NS_ASSERT_MSG (ipv4, "DhcpHelper: NetDevice is associated"
201  " with a node without IPv4 stack installed -> fail "
202  "(maybe need to use InternetStackHelper?)");
203 
204  int32_t interface = ipv4->GetInterfaceForDevice (netDevice);
205  if (interface == -1)
206  {
207  interface = ipv4->AddInterface (netDevice);
208  }
209  NS_ASSERT_MSG (interface >= 0, "DhcpHelper: Interface index not found");
210 
211  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (addr, mask);
212  ipv4->AddAddress (interface, ipv4Addr);
213  ipv4->SetMetric (interface, 1);
214  ipv4->SetUp (interface);
215  retval.Add (ipv4, interface);
216 
217  // Install the default traffic control configuration if the traffic
218  // control layer has been aggregated, if this is not
219  // a loopback interface, and there is no queue disc installed already
221  if (tc && DynamicCast<LoopbackNetDevice> (netDevice) == 0 && tc->GetRootQueueDiscOnDevice (netDevice) == 0)
222  {
223  Ptr<NetDeviceQueueInterface> ndqi = netDevice->GetObject<NetDeviceQueueInterface> ();
224  // It is useless to install a queue disc if the device has no
225  // NetDeviceQueueInterface attached: the device queue is never
226  // stopped and every packet enqueued in the queue disc is
227  // immediately dequeued, hence there will never be backlog
228  if (ndqi)
229  {
230  std::size_t nTxQueues = ndqi->GetNTxQueues ();
231  NS_LOG_LOGIC ("DhcpHelper - Installing default traffic control configuration ("
232  << nTxQueues << " device queue(s))");
234  tcHelper.Install (netDevice);
235  }
236  }
237 
238  // check that the already fixed addresses are not in conflict with the pool
239  std::list<std::pair<Ipv4Address, Ipv4Address> >::iterator iter;
240  for (iter=m_addressPools.begin (); iter!=m_addressPools.end (); iter ++)
241  {
242  if (addr.Get () >= iter->first.Get () && addr.Get () <= iter->second.Get ())
243  {
244  NS_ABORT_MSG ("DhcpHelper: Fixed address can not conflict with a pool: " << addr << " is in [" << iter->first << ", " << iter->second << "]");
245  }
246  }
247  m_fixedAddresses.push_back (addr);
248  return retval;
249 }
250 
251 } // 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:54
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:124
static TypeId GetTypeId(void)
Get the type ID.
Definition: dhcp-server.cc:43
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:47
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
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:263
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
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:192
ObjectFactory m_serverFactory
DHCP server factory.
Definition: dhcp-helper.h:112
static TypeId GetTypeId(void)
Get the type ID.
Definition: dhcp-client.cc:42
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
holds a vector of ns3::NetDevice pointers
Build a set of QueueDisc objects.
Network device transmission queue interface.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:461
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TrafficControlHelper Default(std::size_t nTxQueues=1)
Ptr< Application > InstallDhcpClientPriv(Ptr< NetDevice > netDevice) const
Function to install DHCP client on a node.
Definition: dhcp-helper.cc:76
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:357
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:61
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:358
uint32_t Get(void) const
Get the host-order 32-bit IP address.