32 #include "ns3/double.h" 33 #include "ns3/ipv4-address.h" 34 #include "ns3/nstime.h" 35 #include "ns3/inet-socket-address.h" 36 #include "ns3/socket.h" 37 #include "ns3/simulator.h" 38 #include "ns3/socket-factory.h" 39 #include "ns3/packet.h" 40 #include "ns3/ipv4-static-routing-helper.h" 41 #include "ns3/uinteger.h" 42 #include "ns3/random-variable-stream.h" 43 #include "ns3/pointer.h" 44 #include "ns3/string.h" 45 #include "ns3/ipv4-routing-table-entry.h" 59 .AddConstructor<DhcpClient> ()
60 .SetGroupName (
"Internet-Apps")
61 .AddAttribute (
"RTRS",
"Time for retransmission of Discover message",
65 .AddAttribute (
"Collect",
"Time for which offer collection starts",
69 .AddAttribute (
"ReRequest",
"Time after which request will be resent to next server",
73 .AddAttribute (
"Transactions",
74 "The possible value of transaction numbers ",
75 StringValue (
"ns3::UniformRandomVariable[Min=0.0|Max=1000000.0]"),
77 MakePointerChecker<RandomVariableStream> ())
78 .AddTraceSource (
"NewLease",
81 "ns3::Ipv4Address::TracedCallback")
82 .AddTraceSource (
"ExpireLease",
85 "ns3::Ipv4Address::TracedCallback");
164 uint32_t ifIndex = ipv4->GetInterfaceForDevice (
m_device);
172 uint32_t len = myAddress.
CopyTo (addr);
173 NS_ASSERT_MSG (len <= 16,
"DHCP client can not handle a chaddr larger than 16 bytes");
178 for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
180 if (ipv4->GetAddress (ifIndex, i).GetLocal () ==
m_myAddress)
218 int32_t ifIndex = ipv4->GetInterfaceForDevice (
m_device);
219 for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
221 if (ipv4->GetAddress (ifIndex,i).GetLocal () ==
m_myAddress)
223 ipv4->RemoveAddress (ifIndex, i);
251 int32_t ifIndex = ipv4MN->GetInterfaceForDevice (
m_device);
253 for (uint32_t i = 0; i < ipv4MN->GetNAddresses (ifIndex); i++)
255 if (ipv4MN->GetAddress (ifIndex,i).GetLocal () ==
m_myAddress)
257 ipv4MN->RemoveAddress (ifIndex, i);
265 for (i = 0; i < staticRouting->GetNRoutes (); i++)
267 if (staticRouting->GetRoute (i).GetGateway () ==
m_gateway)
269 staticRouting->RemoveRoute (i);
313 packet = Create<Packet> ();
380 packet = Create<Packet> ();
395 packet = Create<Packet> ((uint8_t*)&addr,
sizeof(addr));
426 int32_t ifIndex = ipv4->GetInterfaceForDevice (
m_device);
428 for (uint32_t i = 0; i < ipv4->GetNAddresses (ifIndex); i++)
430 if (ipv4->GetAddress (ifIndex,i).GetLocal () ==
m_myAddress)
433 ipv4->RemoveAddress (ifIndex, i);
439 ipv4->SetUp (ifIndex);
459 staticRouting->SetDefaultRoute (
m_gateway, ifIndex, 0);
481 int32_t ifIndex = ipv4MN->GetInterfaceForDevice (
m_device);
483 for (uint32_t i = 0; i < ipv4MN->GetNAddresses (ifIndex); i++)
485 if (ipv4MN->GetAddress (ifIndex,i).GetLocal () ==
m_myAddress)
487 ipv4MN->RemoveAddress (ifIndex, i);
495 for (i = 0; i < staticRouting->GetNRoutes (); i++)
497 if (staticRouting->GetRoute (i).GetGateway () ==
m_gateway)
499 staticRouting->RemoveRoute (i);
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
virtual void StartApplication(void)
Application specific startup code.
Ipv4Mask m_myMask
Mask of the address assigned.
void OfferHandler(DhcpHeader header)
Stores DHCP offers in m_offerList.
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 "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Time m_renew
Store the renew time of address.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Hold variables of type string.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
uint8_t m_state
State of the DHCP client.
Ptr< NetDevice > GetDhcpClientNetDevice(void)
Get the the NetDevice DHCP should work on.
a class to represent an Ipv4 address mask
EventId m_refreshEvent
Message refresh event.
#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.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Callback< R > MakeNullCallback(void)
void Request(void)
Sends the DHCP REQUEST message and changes the client state to WAIT_ACK.
static TypeId GetTypeId(void)
Get the type ID.
Ipv4Address m_myAddress
Address assigned to the client.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
a polymophic address class
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
void AcceptAck(DhcpHeader header, Address from)
Receives the DHCP ACK and configures IP address of the client.
Ipv4Address m_server
Address of the DHCP server.
Ipv4Address GetDhcpServer(void)
Get the IPv4Address of current DHCP server.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
uint32_t m_tran
Stores the current transaction number to be used.
Ptr< RandomVariableStream > m_ran
Uniform random variable for transaction ID.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
bool m_offered
Specify if the client has got any offer.
The base class for all ns3 applications.
AttributeValue implementation for Time.
void LinkStateHandler(void)
Handles changes in LinkState.
EventId m_rebindEvent
Message rebind event.
void RemoveAndStart()
Remove the current DHCP information and restart the process.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Ipv4Address m_gateway
Address of the gateway.
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...
void Select(void)
Selects an OFFER from m_offerList.
Ptr< Node > GetNode() const
Time m_collect
Time for which client should collect offers.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
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.
EventId m_timeout
The timeout period.
EventId m_requestEvent
Address refresh event.
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 DHCP_PEER_PORT
DHCP server port.
void SetDhcpClientNetDevice(Ptr< NetDevice > netDevice)
Set the NetDevice DHCP should work on.
virtual void StopApplication(void)
Application specific shutdown code.
TracedCallback< const Ipv4Address & > m_newLease
Trace of new lease.
void NetHandler(Ptr< Socket > socket)
Handles incoming packets from the network.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Ptr< NetDevice > m_device
NetDevice pointer.
static Time Now(void)
Return the current simulation virtual time.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
virtual void DoDispose(void)
Destructor implementation.
virtual void BindToNetDevice(Ptr< NetDevice > netdevice)
Bind a socket to specific device.
Time m_nextoffer
Time to try the next offer (if request gets no reply)
Ptr< Socket > m_socket
Socket for remote communication.
Ipv4 addresses are stored in host order in this class.
Ipv4Address m_offeredAddress
Address offered to the client.
a class to store IPv4 address information on an interface
An identifier for simulation events.
std::list< DhcpHeader > m_offerList
Stores all the offers given to the client.
Address m_chaddr
chaddr of the interface (stored as an Address for convenience).
Helper class that adds ns3::Ipv4StaticRouting objects.
State of a client that needs to refresh the lease.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Time Seconds(double value)
Construct a Time in the indicated unit.
EventId m_nextOfferEvent
Message next offer event.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
State of a client that waits for the offer.
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.
EventId m_discoverEvent
Message retransmission event.
State of a client that waits for acknowledgment.
Time m_rtrs
Defining the time for retransmission.
virtual int Close(void)=0
Close a socket.
void Boot(void)
Sends DHCP DISCOVER and changes the client state to WAIT_OFFER.
Time m_lease
Store the lease time of address.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
TracedCallback< const Ipv4Address & > m_expiry
Trace of lease expire.
void AddHeader(const Header &header)
Add header to this packet.
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...
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.
Ipv4Address m_remoteAddress
Initially set to 255.255.255.255 to start DHCP.
Time m_rebind
Store the rebind time of address.