29 #include "ns3/ipv4-routing-table-entry.h"
30 #include "ns3/ipv4-static-routing-helper.h"
33 #include "ns3/pointer.h"
34 #include "ns3/random-variable-stream.h"
35 #include "ns3/string.h"
49 .AddConstructor<DhcpClient>()
50 .SetGroupName(
"Internet-Apps")
52 "Time for retransmission of Discover message",
56 .AddAttribute(
"Collect",
57 "Time for which offer collection starts",
61 .AddAttribute(
"ReRequest",
62 "Time after which request will be resent to next server",
66 .AddAttribute(
"Transactions",
67 "The possible value of transaction numbers",
68 StringValue(
"ns3::UniformRandomVariable[Min=0.0|Max=1000000.0]"),
70 MakePointerChecker<RandomVariableStream>())
71 .AddTraceSource(
"NewLease",
74 "ns3::Ipv4Address::TracedCallback")
75 .AddTraceSource(
"ExpireLease",
78 "ns3::Ipv4Address::TracedCallback");
172 uint32_t ifIndex =
ipv4->GetInterfaceForDevice(
m_device);
180 uint32_t len = myAddress.
CopyTo(addr);
181 NS_ASSERT_MSG(len <= 16,
"DHCP client can not handle a chaddr larger than 16 bytes");
186 for (uint32_t i = 0; i <
ipv4->GetNAddresses(ifIndex); i++)
232 int32_t ifIndex =
ipv4->GetInterfaceForDevice(
m_device);
233 for (uint32_t i = 0; i <
ipv4->GetNAddresses(ifIndex); i++)
237 ipv4->RemoveAddress(ifIndex, i);
274 int32_t ifIndex = ipv4MN->GetInterfaceForDevice(
m_device);
276 for (uint32_t i = 0; i < ipv4MN->GetNAddresses(ifIndex); i++)
278 if (ipv4MN->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
280 ipv4MN->RemoveAddress(ifIndex, i);
288 for (i = 0; i < staticRouting->GetNRoutes(); i++)
290 if (staticRouting->GetRoute(i).GetGateway() ==
m_gateway)
292 staticRouting->RemoveRoute(i);
342 packet = Create<Packet>();
414 packet = Create<Packet>();
431 packet = Create<Packet>((uint8_t*)&addr,
sizeof(addr));
464 int32_t ifIndex =
ipv4->GetInterfaceForDevice(
m_device);
468 for (uint32_t i = 0; i <
ipv4->GetNAddresses(ifIndex); i++)
474 ipv4->RemoveAddress(ifIndex, i);
480 ipv4->SetUp(ifIndex);
502 staticRouting->SetDefaultRoute(
m_gateway, ifIndex, 0);
525 int32_t ifIndex = ipv4MN->GetInterfaceForDevice(
m_device);
527 for (uint32_t i = 0; i < ipv4MN->GetNAddresses(ifIndex); i++)
529 if (ipv4MN->GetAddress(ifIndex, i).GetLocal() ==
m_myAddress)
531 ipv4MN->RemoveAddress(ifIndex, i);
539 for (i = 0; i < staticRouting->GetNRoutes(); i++)
541 if (staticRouting->GetRoute(i).GetGateway() ==
m_gateway)
543 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.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
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.
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.
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.
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 bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
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.
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 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.
Hold variables of type string.
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_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.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
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 AttributeAccessor > MakePointerAccessor(T1 a1)
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.