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);
   313       packet->AddHeader (newDhcpHeader);
   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);
 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. 
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. 
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed. 
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node. 
uint32_t GetRecvIf(void) const
Get the tag's receiving interface. 
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit. 
#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. 
Ipv4Address m_poolAddress
The network address available to the server. 
Time m_lease
The granted lease time for an address. 
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. 
virtual void DoDispose(void)
Destructor implementation. 
Ptr< const AttributeChecker > MakeIpv4AddressChecker(void)
  
LeasedAddress m_leasedAddresses
Leased address and their status (cache memory) 
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...
Ptr< Node > GetNode() const
Access to the IPv4 forwarding table, interfaces, and configuration. 
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object. 
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. 
uint16_t GetPort(void) const
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 Get(void) const
Get the host-order 32-bit IP mask. 
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. 
virtual void StopApplication(void)
Stops the DHCP client application. 
uint32_t Get(void) const
Get the host-order 32-bit IP address. 
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. 
Ipv4Address GetIpv4(void) const
static TypeId LookupByName(std::string name)
Get a TypeId by name. 
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.