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