17#include "ns3/assert.h"
18#include "ns3/ipv4-packet-info-tag.h"
21#include "ns3/nstime.h"
22#include "ns3/packet.h"
23#include "ns3/simulator.h"
24#include "ns3/socket.h"
39 .AddConstructor<DhcpServer>()
40 .SetGroupName(
"Internet-Apps")
41 .AddAttribute(
"LeaseTime",
42 "Lease for which address will be leased.",
46 .AddAttribute(
"RenewTime",
47 "Time after which client should renew.",
51 .AddAttribute(
"RebindTime",
52 "Time after which client should rebind.",
56 .AddAttribute(
"PoolAddresses",
57 "Pool of addresses to provide on request.",
61 .AddAttribute(
"FirstAddress",
62 "The First valid address that can be given.",
66 .AddAttribute(
"LastAddress",
67 "The Last valid address that can be given.",
71 .AddAttribute(
"PoolMask",
72 "Mask of the pool of addresses.",
76 .AddAttribute(
"Gateway",
77 "Address of default gateway",
112 NS_ABORT_MSG(
"DHCP daemon is not (yet) meant to be started twice or more.");
123 NS_ABORT_MSG(
"DHCP daemon must be run on the same subnet it is assigning the addresses.");
126 for (addrIndex = 0; addrIndex < ipv4->GetNAddresses(ifIndex); addrIndex++)
128 if (ipv4->GetAddress(ifIndex, addrIndex).GetLocal().CombineMask(
m_poolMask) ==
130 ipv4->GetAddress(ifIndex, addrIndex).GetLocal().Get() >=
m_minAddress.
Get() &&
131 ipv4->GetAddress(ifIndex, addrIndex).GetLocal().Get() <=
m_maxAddress.
Get())
135 myOwnAddress = ipv4->GetAddress(ifIndex, addrIndex).GetLocal();
150 for (
uint32_t searchSeq = 0; searchSeq < range; searchSeq++)
153 if (poolAddress != myOwnAddress)
155 NS_LOG_LOGIC(
"Adding " << poolAddress <<
" to the pool");
188 if (i->second.second != 0xffffffff && i->second.second != 0)
191 if (i->second.second == 0)
193 NS_LOG_INFO(
"Address leased state expired, address removed - "
194 <<
"chaddr: " << i->first <<
" IP address " << i->second.first);
195 i->second.second = 0;
216 if (!packet->RemovePacketTag(interfaceInfo))
218 NS_ABORT_MSG(
"No incoming interface on DHCP message, aborting.");
223 if (packet->RemoveHeader(header) == 0)
234 SendAck(iDev, header, senderAddr);
258 NS_LOG_LOGIC(
"This client is sending a DISCOVER but it has still a lease active - "
259 "perhaps it didn't shut down gracefully: "
300 ipv4->SelectSourceAddress(iDev,
315 packet->AddHeader(newDhcpHeader);
323 <<
" Offered Address: " << offeredAddress);
344 <<
" - refreshed addr: " << address);
359 packet->AddHeader(newDhcpHeader);
381 packet->AddHeader(newDhcpHeader);
392 NS_LOG_INFO(
"IP addr does not exists or released!");
403 "Required address is not in the pool " << addr <<
" is not in [" <<
m_minAddress
411 NS_ASSERT_MSG(len <= 16,
"DHCP server can not handle a chaddr larger than 16 bytes");
415 "Client has already an active lease: " <<
m_leasedAddresses[cleanedCaddr].first);
420 "Required address is not available (perhaps it has been already assigned): " << addr);
a polymophic address class
uint32_t CopyFrom(const uint8_t *buffer, uint8_t len)
static constexpr uint32_t MAX_SIZE
The maximum size of a byte buffer which can be stored in an Address instance.
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
The base class for all ns3 applications.
void DoDispose() override
Destructor implementation.
Ptr< Node > GetNode() const
EventId m_expiredEvent
The Event to trigger TimerHandler.
void StopApplication() override
Application specific shutdown code.
static const int PORT
Port number of DHCP server.
AvailableAddress m_availableAddresses
Available addresses to be used (IP addresses)
Time m_rebind
The rebinding time for an address.
Time m_lease
The granted lease time for an address.
Ipv4Mask m_poolMask
The network mask of the pool.
Ptr< Socket > m_socket
The socket bound to port 67.
ExpiredAddress m_expiredAddresses
Expired addresses to be reused (chaddr of the clients)
void StartApplication() override
Application specific startup code.
void AddStaticDhcpEntry(Address chaddr, Ipv4Address addr)
Add a static entry to the pool.
LeasedAddress m_leasedAddresses
Leased address and their status (cache memory)
Ipv4Address m_minAddress
The first address in the address pool.
Ipv4Address m_gateway
The gateway address.
static TypeId GetTypeId()
Get the type ID.
Time m_renew
The renewal time for an address.
Ipv4Address m_maxAddress
The last address in the address pool.
Ipv4Address m_poolAddress
The network address available to the server.
void TimerHandler()
Modifies the remaining lease time of addresses.
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
void SendAck(Ptr< NetDevice > iDev, DhcpHeader header, InetSocketAddress from)
Sends DHCP ACK (or NACK) after receiving Request.
std::map< Address, std::pair< Ipv4Address, uint32_t > >::iterator LeasedAddressIter
Leased address iterator - chaddr + IP addr / lease time.
void SendOffer(Ptr< NetDevice > iDev, DhcpHeader header, InetSocketAddress from)
Sends DHCP offer after receiving DHCP Discover.
void DoDispose() override
Destructor implementation.
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
Ipv4 addresses are stored in host order in this class.
uint32_t Get() const
Get the host-order 32-bit IP address.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
uint32_t Get() const
Get the host-order 32-bit IP mask.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
uint32_t GetRecvIf() const
Get the tag's receiving interface.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
a unique identifier for an interface.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Callback< R, Args... > MakeNullCallback()
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Time Seconds(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Ptr< const AttributeChecker > MakeIpv4AddressChecker()
Ptr< const AttributeAccessor > MakeIpv4MaskAccessor(T1 a1)
Ptr< const AttributeChecker > MakeIpv4MaskChecker()
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.