26 #include "ns3/assert.h"
27 #include "ns3/ipv4-address.h"
28 #include "ns3/nstime.h"
29 #include "ns3/inet-socket-address.h"
30 #include "ns3/ipv4-packet-info-tag.h"
31 #include "ns3/socket.h"
32 #include "ns3/simulator.h"
33 #include "ns3/socket-factory.h"
34 #include "ns3/packet.h"
35 #include "ns3/uinteger.h"
36 #include "ns3/config.h"
53 .AddConstructor<DhcpServer> ()
54 .SetGroupName (
"Internet-Apps")
55 .AddAttribute (
"LeaseTime",
56 "Lease for which address will be leased.",
60 .AddAttribute (
"RenewTime",
61 "Time after which client should renew.",
65 .AddAttribute (
"RebindTime",
66 "Time after which client should rebind.",
70 .AddAttribute (
"PoolAddresses",
71 "Pool of addresses to provide on request.",
75 .AddAttribute (
"FirstAddress",
76 "The First valid address that can be given.",
80 .AddAttribute (
"LastAddress",
81 "The Last valid address that can be given.",
85 .AddAttribute (
"PoolMask",
86 "Mask of the pool of addresses.",
90 .AddAttribute (
"Gateway",
91 "Address of default gateway",
126 NS_ABORT_MSG (
"DHCP daemon is not (yet) meant to be started twice or more.");
137 NS_ABORT_MSG (
"DHCP daemon must be run on the same subnet it is assigning the addresses.");
140 for (addrIndex = 0; addrIndex < ipv4->GetNAddresses (ifIndex); addrIndex++)
143 ipv4->GetAddress (ifIndex, addrIndex).GetLocal ().Get () >=
m_minAddress.
Get () &&
144 ipv4->GetAddress (ifIndex, addrIndex).GetLocal ().Get () <=
m_maxAddress.
Get ())
148 myOwnAddress = ipv4->GetAddress (ifIndex, addrIndex).GetLocal ();
163 for (uint32_t searchSeq = 0; searchSeq < range; searchSeq ++)
166 if (poolAddress != myOwnAddress)
168 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 <<
206 "IP address " << i->second.first);
207 i->second.second = 0;
229 NS_ABORT_MSG (
"No incoming interface on DHCP message, aborting.");
231 uint32_t incomingIf = interfaceInfo.
GetRecvIf ();
244 SendAck (iDev, header, senderAddr);
254 uint32_t tran = header.
GetTran ();
266 NS_LOG_LOGIC (
"This client is sending a DISCOVER but it has still a lease active - perhaps it didn't shut down gracefully: " << sourceChaddr);
299 packet = Create<Packet> ();
303 newDhcpHeader.
SetYiaddr (offeredAddress);
306 Ipv4Address myAddress = ipv4->SelectSourceAddress (iDev, offeredAddress, Ipv4InterfaceAddress::InterfaceAddressScope_e::GLOBAL);
319 packet->AddHeader (newDhcpHeader);
323 NS_LOG_INFO (
"DHCP OFFER" <<
" Offered Address: " << offeredAddress);
338 uint32_t tran = header.
GetTran ();
343 " source port: " << from.
GetPort () <<
344 " - refreshed addr: " <<
address);
352 packet = Create<Packet> ();
372 packet = Create<Packet> ();
388 NS_LOG_INFO (
"IP addr does not exists or released!");
404 uint32_t len = chaddr.
CopyTo (buffer);
405 NS_ASSERT_MSG (len <= 16,
"DHCP server can not handle a chaddr larger than 16 bytes");
409 "Client has already an active lease: " <<
m_leasedAddresses[cleanedCaddr].first);
413 "Required address is not available (perhaps it has been already assigned): " << addr);
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Ipv4Address m_minAddress
The first address in the address pool.
Ptr< Socket > m_socket
The socket bound to port 67.
AvailableAddress m_availableAddresses
Available addresses to be used (IP addresses)
static TypeId GetTypeId(void)
Get the type ID.
Ipv4Address GetIpv4(void) const
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ptr< const AttributeAccessor > MakeIpv4MaskAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Ipv4Address m_maxAddress
The last address in the address pool.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Callback< R > MakeNullCallback(void)
std::map< Address, std::pair< Ipv4Address, uint32_t > >::iterator LeasedAddressIter
Leased address iterator - chaddr + IP addr / lease time.
Ipv4Address m_gateway
The gateway address.
a polymophic address class
void AddStaticDhcpEntry(Address chaddr, Ipv4Address addr)
Add a static entry to the pool.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Ipv4Address m_poolAddress
The network address available to the server.
Time m_lease
The granted lease time for an address.
NS_ASSERT_MSG(false,"Ipv4AddressGenerator::MaskToIndex(): Impossible")
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
void SendAck(Ptr< NetDevice > iDev, DhcpHeader header, InetSocketAddress from)
Sends DHCP ACK (or NACK) after receiving Request.
The base class for all ns3 applications.
AttributeValue implementation for Time.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
virtual void DoDispose(void)
Destructor implementation.
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
uint32_t GetRecvIf(void) const
Get the tag's receiving interface.
Ptr< Node > GetNode() const
LeasedAddress m_leasedAddresses
Leased address and their status (cache memory)
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Time m_renew
The renewal time for an address.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
void SetRecvCallback(Callback< void, Ptr< Socket > >)
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...
Access to the IPv4 forwarding table, interfaces, and configuration.
virtual void DoDispose(void)
Destructor implementation.
static void Remove(const EventId &id)
Remove an event from the event list.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
uint32_t CopyFrom(const uint8_t *buffer, uint8_t len)
static const int PORT
Port number of DHCP server.
Ipv4Mask m_poolMask
The network mask of the pool.
ExpiredAddress m_expiredAddresses
Expired addresses to be reused (chaddr of the clients)
AttributeValue implementation for Ipv4Address.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
virtual void StartApplication(void)
Starts the DHCP Server application.
EventId m_expiredEvent
The Event to trigger TimerHandler.
Ipv4 addresses are stored in host order in this class.
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Time m_rebind
The rebinding time for an address.
void SendOffer(Ptr< NetDevice > iDev, DhcpHeader header, InetSocketAddress from)
Sends DHCP offer after receiving DHCP Discover.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
void TimerHandler(void)
Modifies the remaining lease time of addresses.
Time Seconds(double value)
Construct a Time in the indicated unit.
AttributeValue implementation for Ipv4Mask.
Ptr< const AttributeChecker > MakeIpv4MaskChecker(void)
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
uint16_t GetPort(void) const
virtual void StopApplication(void)
Stops the DHCP client application.
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.
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
void AddHeader(const Header &header)
Add header to this packet.
static TypeId LookupByName(std::string name)
Get a TypeId by name.