27 #include "ns3/assert.h"
28 #include "ns3/nstime.h"
29 #include "ns3/ipv4-packet-info-tag.h"
30 #include "ns3/socket.h"
31 #include "ns3/simulator.h"
32 #include "ns3/packet.h"
47 .AddConstructor<DhcpServer> ()
48 .SetGroupName (
"Internet-Apps")
49 .AddAttribute (
"LeaseTime",
50 "Lease for which address will be leased.",
54 .AddAttribute (
"RenewTime",
55 "Time after which client should renew.",
59 .AddAttribute (
"RebindTime",
60 "Time after which client should rebind.",
64 .AddAttribute (
"PoolAddresses",
65 "Pool of addresses to provide on request.",
69 .AddAttribute (
"FirstAddress",
70 "The First valid address that can be given.",
74 .AddAttribute (
"LastAddress",
75 "The Last valid address that can be given.",
79 .AddAttribute (
"PoolMask",
80 "Mask of the pool of addresses.",
84 .AddAttribute (
"Gateway",
85 "Address of default gateway",
120 NS_ABORT_MSG (
"DHCP daemon is not (yet) meant to be started twice or more.");
131 NS_ABORT_MSG (
"DHCP daemon must be run on the same subnet it is assigning the addresses.");
134 for (addrIndex = 0; addrIndex < ipv4->GetNAddresses (ifIndex); addrIndex++)
137 ipv4->GetAddress (ifIndex, addrIndex).GetLocal ().Get () >=
m_minAddress.
Get () &&
138 ipv4->GetAddress (ifIndex, addrIndex).GetLocal ().Get () <=
m_maxAddress.
Get ())
142 myOwnAddress = ipv4->GetAddress (ifIndex, addrIndex).GetLocal ();
157 for (uint32_t searchSeq = 0; searchSeq < range; searchSeq ++)
160 if (poolAddress != myOwnAddress)
162 NS_LOG_LOGIC (
"Adding " << poolAddress <<
" to the pool");
193 if (i->second.second != 0xffffffff && i->second.second != 0)
196 if (i->second.second == 0)
198 NS_LOG_INFO (
"Address leased state expired, address removed - " <<
199 "chaddr: " << i->first <<
200 " IP address " << i->second.first);
201 i->second.second = 0;
223 NS_ABORT_MSG (
"No incoming interface on DHCP message, aborting.");
225 uint32_t incomingIf = interfaceInfo.
GetRecvIf ();
238 SendAck (iDev, header, senderAddr);
248 uint32_t tran = header.
GetTran ();
260 NS_LOG_LOGIC (
"This client is sending a DISCOVER but it has still a lease active - perhaps it didn't shut down gracefully: " << sourceChaddr);
293 packet = Create<Packet> ();
297 newDhcpHeader.
SetYiaddr (offeredAddress);
300 Ipv4Address myAddress = ipv4->SelectSourceAddress (iDev, offeredAddress, Ipv4InterfaceAddress::InterfaceAddressScope_e::GLOBAL);
317 NS_LOG_INFO (
"DHCP OFFER" <<
" Offered Address: " << offeredAddress);
332 uint32_t tran = header.
GetTran ();
337 " source port: " << from.
GetPort () <<
338 " - refreshed addr: " <<
address);
346 packet = Create<Packet> ();
366 packet = Create<Packet> ();
382 NS_LOG_INFO (
"IP addr does not exists or released!");
398 uint32_t len = chaddr.
CopyTo (buffer);
399 NS_ASSERT_MSG (len <= 16,
"DHCP server can not handle a chaddr larger than 16 bytes");
403 "Client has already an active lease: " <<
m_leasedAddresses[cleanedCaddr].first);
407 "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)
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
The base class for all ns3 applications.
virtual void DoDispose(void)
Destructor implementation.
Ptr< Node > GetNode() const
EventId m_expiredEvent
The Event to trigger TimerHandler.
static const int PORT
Port number of DHCP server.
virtual void StopApplication(void)
Stops the DHCP client application.
AvailableAddress m_availableAddresses
Available addresses to be used (IP addresses)
virtual void DoDispose(void)
Destructor implementation.
void TimerHandler(void)
Modifies the remaining lease time of addresses.
Time m_rebind
The rebinding time for an address.
Time m_lease
The granted lease time for an address.
static TypeId GetTypeId(void)
Get the type ID.
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 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.
Time m_renew
The renewal time for an address.
Ipv4Address m_maxAddress
The last address in the address pool.
virtual void StartApplication(void)
Starts the DHCP Server application.
Ipv4Address m_poolAddress
The network address available to the server.
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.
std::map< Address, std::pair< Ipv4Address, uint32_t > >::iterator LeasedAddressIter
Leased address iterator - chaddr + IP addr / lease time.
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
uint16_t GetPort(void) const
Ipv4Address GetIpv4(void) 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(void) const
Get the host-order 32-bit IP address.
static Ipv4Address GetAny(void)
AttributeValue implementation for Ipv4Address.
Access to the IPv4 forwarding table, interfaces, and configuration.
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
AttributeValue implementation for Ipv4Mask.
This class implements Linux struct pktinfo in order to deliver ancillary information to the socket in...
uint32_t GetRecvIf(void) 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(void) 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(Time const &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.
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 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(void) const
Get an approximation of the time stored in this instance in the indicated unit.
AttributeValue implementation for Time.
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...
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
Ptr< const AttributeAccessor > MakeIpv4AddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeChecker > MakeIpv4MaskChecker(void)
Ptr< const AttributeAccessor > MakeIpv4MaskAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Callback< R, Ts... > MakeNullCallback(void)
#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 AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...