join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
join::BasicDhcpClient< Protocol > Class Template Reference

DHCP client. More...

#include <dhcp.hpp>

Inheritance diagram for join::BasicDhcpClient< Protocol >:
Collaboration diagram for join::BasicDhcpClient< Protocol >:

Classes

struct  PendingRequest
 message waiting for its answer. More...
 

Public Member Functions

 BasicDhcpClient ()=delete
 create the BasicDhcpClient instance.
 
 BasicDhcpClient (const std::string &interface, uint16_t maxSize=0, const std::string &hostname={}, Reactor &reactor=ReactorThread::reactor())
 create the instance bound to the given interface.
 
virtual ~BasicDhcpClient ()=default
 destroy the instance.
 
DhcpPacket compose (uint8_t type) const
 build a message carrying what every message a client sends has in common.
 
DhcpPacket::Ptr exchange (DhcpPacket &request, const IpAddress &destination, uint8_t expected, std::chrono::milliseconds timeout=std::chrono::seconds(1))
 send a message and wait for its answer.
 
std::string reason () const
 get the reason the server gave for the last refusal.
 
DhcpPacket::Ptr discover (const IpAddress &wants=IpAddress::ipv4Wildcard, std::chrono::milliseconds timeout=std::chrono::seconds(1))
 broadcast a DISCOVER message and wait for an OFFER.
 
DhcpPacket::Ptr request (const IpAddress &wants, const IpAddress &server, std::chrono::milliseconds timeout=std::chrono::seconds(1))
 broadcast a REQUEST message and wait for an ACK.
 
DhcpPacket::Ptr renew (const IpAddress &client, const IpAddress &server, std::chrono::milliseconds timeout=std::chrono::seconds(1))
 send a REQUEST message to the server holding the lease and wait for an ACK.
 
DhcpPacket::Ptr inform (const IpAddress &client, std::chrono::milliseconds timeout=std::chrono::seconds(1))
 ask a server for the parameters of an externally configured address.
 
int release (const IpAddress &client, const IpAddress &server, std::chrono::milliseconds timeout=std::chrono::seconds(1))
 give a lease back to the server holding it.
 
int decline (const IpAddress &address, const IpAddress &server, const std::string &message={})
 tell the server that the address it offered is already in use.
 
- Public Member Functions inherited from join::BasicDhcp< Protocol >
 BasicDhcp ()=delete
 create the BasicDhcp instance.
 
 BasicDhcp (const std::string &interface, Reactor &reactor=ReactorThread::reactor())
 create the instance bound to the given interface.
 
 BasicDhcp (const BasicDhcp &other)=delete
 create instance by copy.
 
BasicDhcpoperator= (const BasicDhcp &other)=delete
 assign instance by copy.
 
 BasicDhcp (BasicDhcp &&other)=delete
 create instance by move.
 
BasicDhcpoperator= (BasicDhcp &&other)=delete
 assign instance by move.
 
virtual ~BasicDhcp ()
 destroy the instance.
 
const std::string & interface () const noexcept
 get the name of the interface the instance is bound to.
 
const MacAddresshardware () const noexcept
 get the hardware address of the interface the instance is bound to.
 
- Public Member Functions inherited from join::EventHandler
 EventHandler ()=default
 create instance.
 
 EventHandler (const EventHandler &other)=default
 copy constructor.
 
EventHandleroperator= (const EventHandler &other)=default
 copy assignment operator.
 
 EventHandler (EventHandler &&other)=default
 move constructor.
 
EventHandleroperator= (EventHandler &&other)=default
 move assignment operator.
 
virtual ~EventHandler ()=default
 destroy instance.
 

Protected Member Functions

void onMessage (DhcpPacket::Ptr packet) override final
 hand a received message to the request waiting for it.
 
- Protected Member Functions inherited from join::BasicDhcp< Protocol >
struct __attribute__ ((packed)) Frame
 link, internet and transport headers a DHCP message is framed with.
 
struct __attribute__ ((packed)) Pseudo
 header the UDP checksum is computed over, RFC 768.
 
void onReadable (int fd) override final
 method called when data are ready to be read.
 
int transmit (const DhcpPacket &packet, const IpAddress &source, const IpAddress &destination)
 frame a message and write it on the wire.
 
DhcpPacket::Ptr receive (const char *data, size_t size) const
 decode a frame received on the wire.
 
- Protected Member Functions inherited from join::EventHandler
virtual void onEvent (int fd, uint32_t revents)
 method called when events are reported on handle.
 
virtual void onWriteable (int fd)
 method called when data are ready to be written on handle.
 
virtual void onClose (int fd)
 method called when handle was closed by the peer.
 
virtual void onError (int fd)
 method called when an error occurred on handle.
 

Protected Attributes

std::unordered_map< uint32_t, std::unique_ptr< PendingRequest > > _pending
 messages waiting for their answer, indexed by transaction identifier.
 
Mutex _syncMutex
 mutex for synchronous operations.
 
std::string _reason
 reason the server gave for the last refusal.
 
const std::string _hostname
 host name to advertise.
 
const uint16_t _maxSize
 biggest message the server may send back.
 
- Protected Attributes inherited from join::BasicDhcp< Protocol >
Socket _socket
 underlying socket.
 
DhcpMessage _message
 DHCP message codec.
 
std::unique_ptr< char[]> _buffer
 receive buffer.
 
const std::string _interface
 interface name.
 
const MacAddress _hardware
 hardware address of the interface.
 
Reactor_reactor
 event loop reactor.
 

Static Protected Attributes

static const ByteList _defaultParams
 options a client asks for by default.
 

Additional Inherited Members

- Public Types inherited from join::BasicDhcp< Protocol >
using Socket = typename Protocol::Socket
 
- Static Protected Member Functions inherited from join::BasicDhcp< Protocol >
static uint16_t udpChecksum (const Frame &frame, const char *payload, size_t size)
 compute the checksum of a UDP datagram, RFC 768.
 

Detailed Description

template<class Protocol>
class join::BasicDhcpClient< Protocol >

DHCP client.

Constructor & Destructor Documentation

◆ BasicDhcpClient() [1/2]

template<class Protocol >
join::BasicDhcpClient< Protocol >::BasicDhcpClient ( )
delete

create the BasicDhcpClient instance.

◆ BasicDhcpClient() [2/2]

template<class Protocol >
join::BasicDhcpClient< Protocol >::BasicDhcpClient ( const std::string & interface,
uint16_t maxSize = 0,
const std::string & hostname = {},
Reactor & reactor = ReactorThread::reactor () )
inlineexplicit

create the instance bound to the given interface.

Parameters
interfaceinterface name.
maxSizebiggest message the server may send back, zero to leave it unspecified.
hostnamehost name to advertise, empty to advertise none.
reactorreactor instance.
Exceptions
std::system_errorif the interface is unknown, the socket could not be bound to it, or the maximum message size is below what RFC 2132 allows.

◆ ~BasicDhcpClient()

template<class Protocol >
virtual join::BasicDhcpClient< Protocol >::~BasicDhcpClient ( )
virtualdefault

destroy the instance.

Member Function Documentation

◆ compose()

template<class Protocol >
DhcpPacket join::BasicDhcpClient< Protocol >::compose ( uint8_t type) const
inline

build a message carrying what every message a client sends has in common.

Parameters
typemessage type.
Returns
the message.

◆ decline()

template<class Protocol >
int join::BasicDhcpClient< Protocol >::decline ( const IpAddress & address,
const IpAddress & server,
const std::string & message = {} )
inline

tell the server that the address it offered is already in use.

Parameters
addressaddress the client refuses.
serveraddress of the server that offered it.
messagehuman readable reason, empty to give none.
Returns
0 on success, -1 on failure.

◆ discover()

template<class Protocol >
DhcpPacket::Ptr join::BasicDhcpClient< Protocol >::discover ( const IpAddress & wants = IpAddress::ipv4Wildcard,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inline

broadcast a DISCOVER message and wait for an OFFER.

Parameters
wantsaddress the client would like to get, wildcard to let the server choose.
timeoutanswer timeout.
Returns
the offer received, nullptr on failure.

◆ exchange()

template<class Protocol >
DhcpPacket::Ptr join::BasicDhcpClient< Protocol >::exchange ( DhcpPacket & request,
const IpAddress & destination,
uint8_t expected,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inline

send a message and wait for its answer.

Parameters
requestmessage to send.
destinationdestination address.
expectedmessage type the answer must carry.
timeoutanswer timeout.
Returns
the answer received, nullptr on failure.

◆ inform()

template<class Protocol >
DhcpPacket::Ptr join::BasicDhcpClient< Protocol >::inform ( const IpAddress & client,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inline

ask a server for the parameters of an externally configured address.

Parameters
clientaddress the client already owns.
timeoutanswer timeout.
Returns
the acknowledgement received, nullptr on failure.

◆ onMessage()

template<class Protocol >
void join::BasicDhcpClient< Protocol >::onMessage ( DhcpPacket::Ptr packet)
inlinefinaloverrideprotectedvirtual

hand a received message to the request waiting for it.

Parameters
packetmessage received.

Implements join::BasicDhcp< Protocol >.

◆ reason()

template<class Protocol >
std::string join::BasicDhcpClient< Protocol >::reason ( ) const
inline

get the reason the server gave for the last refusal.

Returns
the reason, empty if the server gave none or if nothing was refused.

◆ release()

template<class Protocol >
int join::BasicDhcpClient< Protocol >::release ( const IpAddress & client,
const IpAddress & server,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inline

give a lease back to the server holding it.

Parameters
clientaddress the client currently owns.
serveraddress of the server holding the lease.
timeouttime allowed to resolve the server hardware address.
Returns
0 on success, -1 on failure.

◆ renew()

template<class Protocol >
DhcpPacket::Ptr join::BasicDhcpClient< Protocol >::renew ( const IpAddress & client,
const IpAddress & server,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inline

send a REQUEST message to the server holding the lease and wait for an ACK.

Parameters
clientaddress the client currently owns.
serveraddress of the server holding the lease.
timeoutanswer timeout.
Returns
the acknowledgement received, nullptr on failure.

◆ request()

template<class Protocol >
DhcpPacket::Ptr join::BasicDhcpClient< Protocol >::request ( const IpAddress & wants,
const IpAddress & server,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inline

broadcast a REQUEST message and wait for an ACK.

Parameters
wantsaddress the client asks to be assigned.
serveraddress of the server the offer came from.
timeoutanswer timeout.
Returns
the acknowledgement received, nullptr on failure.

Member Data Documentation

◆ _defaultParams

template<class Protocol >
const ByteList join::BasicDhcpClient< Protocol >::_defaultParams
staticprotected
Initial value:
@ Router
Definition dhcp_option.hpp:71
@ BroadcastAddress
Definition dhcp_option.hpp:96
@ DomainNameServer
Definition dhcp_option.hpp:74
@ DomainName
Definition dhcp_option.hpp:83
@ SubnetMask
Definition dhcp_option.hpp:69
@ InterfaceMtu
Definition dhcp_option.hpp:94

options a client asks for by default.

◆ _hostname

template<class Protocol >
const std::string join::BasicDhcpClient< Protocol >::_hostname
protected

host name to advertise.

◆ _maxSize

template<class Protocol >
const uint16_t join::BasicDhcpClient< Protocol >::_maxSize
protected

biggest message the server may send back.

◆ _pending

template<class Protocol >
std::unordered_map<uint32_t, std::unique_ptr<PendingRequest> > join::BasicDhcpClient< Protocol >::_pending
protected

messages waiting for their answer, indexed by transaction identifier.

◆ _reason

template<class Protocol >
std::string join::BasicDhcpClient< Protocol >::_reason
protected

reason the server gave for the last refusal.

◆ _syncMutex

template<class Protocol >
Mutex join::BasicDhcpClient< Protocol >::_syncMutex
mutableprotected

mutex for synchronous operations.


The documentation for this class was generated from the following files: