18#include "ns3/ipv4-routing-table-entry.h"
19#include "ns3/ipv4-static-routing-helper.h"
22#include "ns3/pointer.h"
23#include "ns3/random-variable-stream.h"
24#include "ns3/string.h"
38 .AddConstructor<DhcpClient>()
39 .SetGroupName(
"Internet-Apps")
41 "Time for retransmission of Discover message",
45 .AddAttribute(
"Collect",
46 "Time for which offer collection starts",
50 .AddAttribute(
"ReRequest",
51 "Time after which request will be resent to next server",
55 .AddAttribute(
"Transactions",
56 "The possible value of transaction numbers",
57 StringValue(
"ns3::UniformRandomVariable[Min=0.0|Max=1000000.0]"),
60 .AddTraceSource(
"NewLease",
63 "ns3::Ipv4Address::TracedCallback")
64 .AddTraceSource(
"ExpireLease",
67 "ns3::Ipv4Address::TracedCallback");
170 NS_ASSERT_MSG(len <= 16,
"DHCP client can not handle a chaddr larger than 16 bytes");
175 for (
uint32_t i = 0; i < ipv4->GetNAddresses(ifIndex); i++)
177 if (ipv4->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
222 for (
uint32_t i = 0; i < ipv4->GetNAddresses(ifIndex); i++)
224 if (ipv4->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
226 ipv4->RemoveAddress(ifIndex, i);
265 for (
uint32_t i = 0; i < ipv4MN->GetNAddresses(ifIndex); i++)
267 if (ipv4MN->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
269 ipv4MN->RemoveAddress(ifIndex, i);
277 for (i = 0; i < staticRouting->GetNRoutes(); i++)
279 if (staticRouting->GetRoute(i).GetGateway() ==
m_gateway)
281 staticRouting->RemoveRoute(i);
300 if (packet->RemoveHeader(header) == 0)
338 packet->AddHeader(header);
410 packet->AddHeader(header);
429 packet->AddHeader(header);
457 for (
uint32_t i = 0; i < ipv4->GetNAddresses(ifIndex); i++)
459 if (ipv4->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
463 ipv4->RemoveAddress(ifIndex, i);
469 ipv4->SetUp(ifIndex);
491 staticRouting->SetDefaultRoute(
m_gateway, ifIndex, 0);
516 for (
uint32_t i = 0; i < ipv4MN->GetNAddresses(ifIndex); i++)
518 if (ipv4MN->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
520 ipv4MN->RemoveAddress(ifIndex, i);
528 for (i = 0; i < staticRouting->GetNRoutes(); i++)
530 if (staticRouting->GetRoute(i).GetGateway() ==
m_gateway)
532 staticRouting->RemoveRoute(i);
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_requestEvent
Address refresh event.
Ipv4Address m_gateway
Address of the gateway.
static TypeId GetTypeId()
Get the type ID.
Ipv4Mask m_myMask
Mask of the address assigned.
Time m_rebind
Store the rebind time of address.
void SetDhcpClientNetDevice(Ptr< NetDevice > netDevice)
Set the NetDevice DHCP should work on.
void LinkStateHandler()
Handles changes in LinkState.
void DoDispose() override
Destructor implementation.
uint32_t m_tran
Stores the current transaction number to be used.
void RemoveAndStart()
Remove the current DHCP information and restart the process.
Ptr< Socket > m_socket
Socket for remote communication.
bool m_firstBoot
First boot (used to add the link state change callback)
Time m_collect
Time for which client should collect offers.
Address m_chaddr
chaddr of the interface (stored as an Address for convenience).
@ WAIT_OFFER
State of a client that waits for the offer.
@ WAIT_ACK
State of a client that waits for acknowledgment.
@ REFRESH_LEASE
State of a client that needs to refresh the lease.
void StopApplication() override
Application specific shutdown code.
Ptr< NetDevice > m_device
NetDevice pointer.
EventId m_timeout
The timeout period.
Ptr< RandomVariableStream > m_ran
Uniform random variable for transaction ID.
EventId m_rebindEvent
Message rebind event.
static const int DHCP_PEER_PORT
DHCP server port.
std::list< DhcpHeader > m_offerList
Stores all the offers given to the client.
Ptr< NetDevice > GetDhcpClientNetDevice()
Get the the NetDevice DHCP should work on.
Ipv4Address m_server
Address of the DHCP server.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this Application object.
EventId m_nextOfferEvent
Message next offer event.
void StartApplication() override
Application specific startup code.
void Boot()
Sends DHCP DISCOVER and changes the client state to WAIT_OFFER.
bool m_offered
Specify if the client has got any offer.
void OfferHandler(DhcpHeader header)
Stores DHCP offers in m_offerList.
void Request()
Sends the DHCP REQUEST message and changes the client state to WAIT_ACK.
TracedCallback< const Ipv4Address & > m_expiry
Trace of lease expire.
TracedCallback< const Ipv4Address & > m_newLease
Trace of new lease.
Ipv4Address GetDhcpServer()
Get the IPv4Address of current DHCP server.
uint8_t m_state
State of the DHCP client.
EventId m_discoverEvent
Message retransmission event.
EventId m_refreshEvent
Message refresh event.
Ipv4Address m_myAddress
Address assigned to the client.
Time m_rtrs
Defining the time for retransmission.
void Select()
Selects an OFFER from m_offerList.
Time m_nextoffer
Time to try the next offer (if request gets no reply)
Ipv4Address m_remoteAddress
Initially set to 255.255.255.255 to start DHCP.
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
void AcceptAck(DhcpHeader header, Address from)
Receives the DHCP ACK and configures IP address of the client.
Ipv4Address m_offeredAddress
Address offered to the client.
Time m_renew
Store the renew time of address.
EventId m_collectEvent
Offer collection event.
Time m_lease
Store the lease time of address.
An identifier for simulation events.
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.
a class to store IPv4 address information on an interface
a class to represent an Ipv4 address mask
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Smart pointer class similar to boost::intrusive_ptr.
virtual double GetValue()=0
Get the next random value drawn from the distribution.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static Time Now()
Return the current simulation virtual time.
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.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
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 Close()=0
Close a socket.
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.
Hold variables of type string.
Simulation virtual time values and global simulation resolution.
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 AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Callback< R, Args... > MakeNullCallback()
#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.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
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()
Helper to make an unbounded Time checker.