A Discrete-Event Network Simulator
API
dhcp-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 UPB
3  * Copyright (c) 2017 NITK Surathkal
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: Radu Lupu <rlupu@elcom.pub.ro>
19  * Ankit Deepak <adadeepak8@gmail.com>
20  * Deepti Rajagopal <deeptir96@gmail.com>
21  *
22  */
23 
24 #include "dhcp-helper.h"
25 
26 #include "ns3/dhcp-client.h"
27 #include "ns3/dhcp-server.h"
28 #include "ns3/ipv4.h"
29 #include "ns3/log.h"
30 #include "ns3/loopback-net-device.h"
31 #include "ns3/names.h"
32 #include "ns3/net-device-queue-interface.h"
33 #include "ns3/traffic-control-helper.h"
34 #include "ns3/traffic-control-layer.h"
35 #include "ns3/uinteger.h"
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("DhcpHelper");
41 
43 {
46 }
47 
48 void
49 DhcpHelper::SetClientAttribute(std::string name, const AttributeValue& value)
50 {
51  m_clientFactory.Set(name, value);
52 }
53 
54 void
55 DhcpHelper::SetServerAttribute(std::string name, const AttributeValue& value)
56 {
57  m_serverFactory.Set(name, value);
58 }
59 
62 {
64 }
65 
68 {
70  for (auto i = netDevices.Begin(); i != netDevices.End(); ++i)
71  {
72  apps.Add(InstallDhcpClientPriv(*i));
73  }
74  return apps;
75 }
76 
79 {
80  Ptr<Node> node = netDevice->GetNode();
81  NS_ASSERT_MSG(node, "DhcpClientHelper: NetDevice is not not associated with any node -> fail");
82 
83  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
85  "DhcpHelper: NetDevice is associated"
86  " with a node without IPv4 stack installed -> fail "
87  "(maybe need to use InternetStackHelper?)");
88 
89  int32_t interface = ipv4->GetInterfaceForDevice(netDevice);
90  if (interface == -1)
91  {
92  interface = ipv4->AddInterface(netDevice);
93  }
94  NS_ASSERT_MSG(interface >= 0, "DhcpHelper: Interface index not found");
95 
96  ipv4->SetMetric(interface, 1);
97  ipv4->SetUp(interface);
98 
99  // Install the default traffic control configuration if the traffic
100  // control layer has been aggregated, if this is not
101  // a loopback interface, and there is no queue disc installed already
103  if (tc && !DynamicCast<LoopbackNetDevice>(netDevice) &&
104  !tc->GetRootQueueDiscOnDevice(netDevice))
105  {
106  Ptr<NetDeviceQueueInterface> ndqi = netDevice->GetObject<NetDeviceQueueInterface>();
107  // It is useless to install a queue disc if the device has no
108  // NetDeviceQueueInterface attached: the device queue is never
109  // stopped and every packet enqueued in the queue disc is
110  // immediately dequeued, hence there will never be backlog
111  if (ndqi)
112  {
113  std::size_t nTxQueues = ndqi->GetNTxQueues();
114  NS_LOG_LOGIC("DhcpHelper - Installing default traffic control configuration ("
115  << nTxQueues << " device queue(s))");
117  tcHelper.Install(netDevice);
118  }
119  }
120 
121  Ptr<DhcpClient> app = DynamicCast<DhcpClient>(m_clientFactory.Create<DhcpClient>());
122  app->SetDhcpClientNetDevice(netDevice);
123  node->AddApplication(app);
124 
125  return app;
126 }
127 
130  Ipv4Address serverAddr,
131  Ipv4Address poolAddr,
132  Ipv4Mask poolMask,
133  Ipv4Address minAddr,
134  Ipv4Address maxAddr,
135  Ipv4Address gateway)
136 {
137  m_serverFactory.Set("PoolAddresses", Ipv4AddressValue(poolAddr));
138  m_serverFactory.Set("PoolMask", Ipv4MaskValue(poolMask));
139  m_serverFactory.Set("FirstAddress", Ipv4AddressValue(minAddr));
140  m_serverFactory.Set("LastAddress", Ipv4AddressValue(maxAddr));
141  m_serverFactory.Set("Gateway", Ipv4AddressValue(gateway));
142 
143  Ptr<Node> node = netDevice->GetNode();
144  NS_ASSERT_MSG(node, "DhcpHelper: NetDevice is not not associated with any node -> fail");
145 
146  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
148  "DhcpHelper: NetDevice is associated"
149  " with a node without IPv4 stack installed -> fail "
150  "(maybe need to use InternetStackHelper?)");
151 
152  int32_t interface = ipv4->GetInterfaceForDevice(netDevice);
153  if (interface == -1)
154  {
155  interface = ipv4->AddInterface(netDevice);
156  }
157  NS_ASSERT_MSG(interface >= 0, "DhcpHelper: Interface index not found");
158 
159  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(serverAddr, poolMask);
160  ipv4->AddAddress(interface, ipv4Addr);
161  ipv4->SetMetric(interface, 1);
162  ipv4->SetUp(interface);
163 
164  // Install the default traffic control configuration if the traffic
165  // control layer has been aggregated, if this is not
166  // a loopback interface, and there is no queue disc installed already
168  if (tc && !DynamicCast<LoopbackNetDevice>(netDevice) &&
169  !tc->GetRootQueueDiscOnDevice(netDevice))
170  {
171  Ptr<NetDeviceQueueInterface> ndqi = netDevice->GetObject<NetDeviceQueueInterface>();
172  // It is useless to install a queue disc if the device has no
173  // NetDeviceQueueInterface attached: the device queue is never
174  // stopped and every packet enqueued in the queue disc is
175  // immediately dequeued, hence there will never be backlog
176  if (ndqi)
177  {
178  std::size_t nTxQueues = ndqi->GetNTxQueues();
179  NS_LOG_LOGIC("DhcpHelper - Installing default traffic control configuration ("
180  << nTxQueues << " device queue(s))");
182  tcHelper.Install(netDevice);
183  }
184  }
185 
186  // check that the already fixed addresses are not in conflict with the pool
187  for (auto iter = m_fixedAddresses.begin(); iter != m_fixedAddresses.end(); iter++)
188  {
189  if (iter->Get() >= minAddr.Get() && iter->Get() <= maxAddr.Get())
190  {
191  NS_ABORT_MSG("DhcpHelper: Fixed address can not conflict with a pool: "
192  << *iter << " is in [" << minAddr << ", " << maxAddr << "]");
193  }
194  }
195  m_addressPools.emplace_back(minAddr, maxAddr);
196 
198  node->AddApplication(app);
199  return ApplicationContainer(app);
200 }
201 
204 {
205  Ipv4InterfaceContainer retval;
206 
207  Ptr<Node> node = netDevice->GetNode();
208  NS_ASSERT_MSG(node, "DhcpHelper: NetDevice is not not associated with any node -> fail");
209 
210  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
212  "DhcpHelper: NetDevice is associated"
213  " with a node without IPv4 stack installed -> fail "
214  "(maybe need to use InternetStackHelper?)");
215 
216  int32_t interface = ipv4->GetInterfaceForDevice(netDevice);
217  if (interface == -1)
218  {
219  interface = ipv4->AddInterface(netDevice);
220  }
221  NS_ASSERT_MSG(interface >= 0, "DhcpHelper: Interface index not found");
222 
223  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress(addr, mask);
224  ipv4->AddAddress(interface, ipv4Addr);
225  ipv4->SetMetric(interface, 1);
226  ipv4->SetUp(interface);
227  retval.Add(ipv4, interface);
228 
229  // Install the default traffic control configuration if the traffic
230  // control layer has been aggregated, if this is not
231  // a loopback interface, and there is no queue disc installed already
233  if (tc && !DynamicCast<LoopbackNetDevice>(netDevice) &&
234  !tc->GetRootQueueDiscOnDevice(netDevice))
235  {
236  Ptr<NetDeviceQueueInterface> ndqi = netDevice->GetObject<NetDeviceQueueInterface>();
237  // It is useless to install a queue disc if the device has no
238  // NetDeviceQueueInterface attached: the device queue is never
239  // stopped and every packet enqueued in the queue disc is
240  // immediately dequeued, hence there will never be backlog
241  if (ndqi)
242  {
243  std::size_t nTxQueues = ndqi->GetNTxQueues();
244  NS_LOG_LOGIC("DhcpHelper - Installing default traffic control configuration ("
245  << nTxQueues << " device queue(s))");
247  tcHelper.Install(netDevice);
248  }
249  }
250 
251  // check that the already fixed addresses are not in conflict with the pool
252  for (auto iter = m_addressPools.begin(); iter != m_addressPools.end(); iter++)
253  {
254  if (addr.Get() >= iter->first.Get() && addr.Get() <= iter->second.Get())
255  {
256  NS_ABORT_MSG("DhcpHelper: Fixed address can not conflict with a pool: "
257  << addr << " is in [" << iter->first << ", " << iter->second << "]");
258  }
259  }
260  m_fixedAddresses.push_back(addr);
261  return retval;
262 }
263 
264 } // namespace ns3
holds a vector of ns3::Application pointers.
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:70
Implements the functionality of a DHCP client.
Definition: dhcp-client.h:48
static TypeId GetTypeId()
Get the type ID.
Definition: dhcp-client.cc:44
ObjectFactory m_serverFactory
DHCP server factory.
Definition: dhcp-helper.h:118
std::list< std::pair< Ipv4Address, Ipv4Address > > m_addressPools
list of address pools.
Definition: dhcp-helper.h:120
std::list< Ipv4Address > m_fixedAddresses
list of fixed addresses already allocated.
Definition: dhcp-helper.h:119
Ipv4InterfaceContainer InstallFixedAddress(Ptr< NetDevice > netDevice, Ipv4Address addr, Ipv4Mask mask)
Assign a fixed IP addresses to a net device.
Definition: dhcp-helper.cc:203
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:129
ApplicationContainer InstallDhcpClient(Ptr< NetDevice > netDevice) const
Install DHCP client of a nodes / NetDevice.
Definition: dhcp-helper.cc:61
void SetClientAttribute(std::string name, const AttributeValue &value)
Set DHCP client attributes.
Definition: dhcp-helper.cc:49
void SetServerAttribute(std::string name, const AttributeValue &value)
Set DHCP server attributes.
Definition: dhcp-helper.cc:55
Ptr< Application > InstallDhcpClientPriv(Ptr< NetDevice > netDevice) const
Function to install DHCP client on a node.
Definition: dhcp-helper.cc:78
ObjectFactory m_clientFactory
DHCP client factory.
Definition: dhcp-helper.h:117
Implements the functionality of a DHCP server.
Definition: dhcp-server.h:48
static TypeId GetTypeId()
Get the type ID.
Definition: dhcp-server.cc:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
uint32_t Get() const
Get the host-order 32-bit IP address.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:257
holds a vector of ns3::NetDevice pointers
Iterator Begin() const
Get an iterator which refers to the first NetDevice in the container.
Iterator End() const
Get an iterator which indicates past-the-last NetDevice in the container.
Network device transmission queue interface.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Build a set of QueueDisc objects.
QueueDiscContainer Install(NetDeviceContainer c)
static TrafficControlHelper Default(std::size_t nTxQueues=1)
The Traffic Control layer aims at introducing an equivalent of the Linux Traffic Control infrastructu...
#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
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
Every class exported by the ns3 library is enclosed in the ns3 namespace.