28 #include "ns3/assert.h"
29 #include "ns3/ipv4-packet-info-tag.h"
32 #include "ns3/nstime.h"
33 #include "ns3/packet.h"
34 #include "ns3/simulator.h"
35 #include "ns3/socket.h"
50 .AddConstructor<DhcpServer>()
51 .SetGroupName(
"Internet-Apps")
52 .AddAttribute(
"LeaseTime",
53 "Lease for which address will be leased.",
57 .AddAttribute(
"RenewTime",
58 "Time after which client should renew.",
62 .AddAttribute(
"RebindTime",
63 "Time after which client should rebind.",
67 .AddAttribute(
"PoolAddresses",
68 "Pool of addresses to provide on request.",
71 MakeIpv4AddressChecker())
72 .AddAttribute(
"FirstAddress",
73 "The First valid address that can be given.",
76 MakeIpv4AddressChecker())
77 .AddAttribute(
"LastAddress",
78 "The Last valid address that can be given.",
81 MakeIpv4AddressChecker())
82 .AddAttribute(
"PoolMask",
83 "Mask of the pool of addresses.",
86 MakeIpv4MaskChecker())
87 .AddAttribute(
"Gateway",
88 "Address of default gateway",
91 MakeIpv4AddressChecker());
123 NS_ABORT_MSG(
"DHCP daemon is not (yet) meant to be started twice or more.");
134 NS_ABORT_MSG(
"DHCP daemon must be run on the same subnet it is assigning the addresses.");
137 for (addrIndex = 0; addrIndex <
ipv4->GetNAddresses(ifIndex); addrIndex++)
139 if (
ipv4->GetAddress(ifIndex, addrIndex).GetLocal().CombineMask(
m_poolMask) ==
146 myOwnAddress =
ipv4->GetAddress(ifIndex, addrIndex).GetLocal();
161 for (uint32_t searchSeq = 0; searchSeq < range; searchSeq++)
164 if (poolAddress != myOwnAddress)
166 NS_LOG_LOGIC(
"Adding " << poolAddress <<
" to the pool");
199 if (i->second.second != 0xffffffff && i->second.second != 0)
202 if (i->second.second == 0)
204 NS_LOG_INFO(
"Address leased state expired, address removed - "
205 <<
"chaddr: " << i->first <<
" IP address " << i->second.first);
206 i->second.second = 0;
229 NS_ABORT_MSG(
"No incoming interface on DHCP message, aborting.");
231 uint32_t incomingIf = interfaceInfo.
GetRecvIf();
245 SendAck(iDev, header, senderAddr);
256 uint32_t tran = header.
GetTran();
269 NS_LOG_LOGIC(
"This client is sending a DISCOVER but it has still a lease active - "
270 "perhaps it didn't shut down gracefully: "
303 packet = Create<Packet>();
311 ipv4->SelectSourceAddress(iDev,
313 Ipv4InterfaceAddress::InterfaceAddressScope_e::GLOBAL);
334 <<
" Offered Address: " << offeredAddress);
350 uint32_t tran = header.
GetTran();
355 <<
" - refreshed addr: " <<
address);
363 packet = Create<Packet>();
385 packet = Create<Packet>();
403 NS_LOG_INFO(
"IP addr does not exists or released!");
414 "Required address is not in the pool " << addr <<
" is not in [" <<
m_minAddress
421 uint32_t len = chaddr.
CopyTo(buffer);
422 NS_ASSERT_MSG(len <= 16,
"DHCP server can not handle a chaddr larger than 16 bytes");
426 "Client has already an active lease: " <<
m_leasedAddresses[cleanedCaddr].first);
431 "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
Stops the DHCP client application.
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
Starts the DHCP Server application.
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.
void SendOffer(Ptr< NetDevice > iDev, DhcpHeader header, InetSocketAddress from)
Sends DHCP offer after receiving DHCP Discover.
void DoDispose() override
Destructor implementation.
std::map< Address, std::pair< Ipv4Address, uint32_t > >::iterator LeasedAddressIter
Leased address iterator - chaddr + IP addr / lease time.
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.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
void AddHeader(const Header &header)
Add header to this packet.
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 bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
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 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 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.
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 AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.