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 >:

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.
 
 ~BasicDhcpClient ()
 stop receiving messages and destroy the instance.
 
DhcpPacket compose (uint8_t type) const
 build a message carrying what every message a client sends has in common.
 
int exchange (DhcpPacket &request, DhcpPacket &answer, const IpAddress &destination, uint8_t expected, std::chrono::milliseconds timeout=std::chrono::seconds(1)) noexcept
 send a message and wait for its answer.
 
std::string reason () const
 get the reason the server gave for the last refusal.
 
int discover (DhcpPacket &offer, const IpAddress &wants=IpAddress::ipv4Wildcard, std::chrono::milliseconds timeout=std::chrono::seconds(1)) noexcept
 broadcast a DISCOVER message and wait for an OFFER.
 
int request (DhcpPacket &ack, const IpAddress &wants, const IpAddress &server, std::chrono::milliseconds timeout=std::chrono::seconds(1)) noexcept
 broadcast a REQUEST message and wait for an ACK.
 
int renew (DhcpPacket &ack, const IpAddress &client, const IpAddress &server, std::chrono::milliseconds timeout=std::chrono::seconds(1)) noexcept
 send a REQUEST message to the server holding the lease and wait for an ACK.
 
int inform (DhcpPacket &ack, const IpAddress &client, std::chrono::milliseconds timeout=std::chrono::seconds(1)) noexcept
 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)) noexcept
 give a lease back to the server holding it.
 
int decline (const IpAddress &address, const IpAddress &server, const std::string &message={}) noexcept
 tell the server that the address it offered is already in use.
 

Additional Inherited Members

- Protected Types inherited from join::BasicDhcp< Protocol >
using Socket = typename Protocol::Socket
 
- Protected Member Functions inherited from join::BasicDhcp< Protocol >
struct __attribute__ ((packed, aligned(2))) Frame
 link, internet and transport headers a DHCP message is framed with.
 
struct __attribute__ ((packed, aligned(2))) Pseudo
 header the UDP checksum is computed over, RFC 768.
 
void attach ()
 register with the reactor, to be called by the final class once fully constructed.
 
void detach () noexcept
 unregister from the reactor, to be called by the final class before destruction starts.
 
int send (const char *data, size_t size, const MacAddress &to, const IpAddress &source, const IpAddress &destination) noexcept
 frame a message and write it on the wire.
 
ssize_t receive (char *data, size_t maxSize, MacAddress &from, MacAddress &to) noexcept
 read a message that passed the link, internet and transport checks.
 
 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.
 
BasicDhcp & operator= (const BasicDhcp &other)=delete
 assign instance by copy.
 
 BasicDhcp (BasicDhcp &&other)=delete
 create instance by move.
 
BasicDhcp & operator= (BasicDhcp &&other)=delete
 assign instance by move.
 
virtual ~BasicDhcp ()=default
 destroy the instance.
 
const std::string & interface () const noexcept
 get the name of the interface the instance is bound to.
 
const MacAddress & hardware () const noexcept
 get the hardware address of the interface the instance is bound to.
 
- 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.
 
 EventHandler ()=default
 create instance.
 
 EventHandler (const EventHandler &other)=default
 copy constructor.
 
EventHandler & operator= (const EventHandler &other)=default
 copy assignment operator.
 
 EventHandler (EventHandler &&other)=default
 move constructor.
 
EventHandler & operator= (EventHandler &&other)=default
 move assignment operator.
 
virtual ~EventHandler ()=default
 destroy instance.
 
- Static Protected Member Functions inherited from join::BasicDhcp< Protocol >
static uint16_t udpChecksum (const Frame &frame, const char *payload, size_t size) noexcept
 compute the checksum of a UDP datagram, RFC 768.
 
- 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.
 

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 >
join::BasicDhcpClient< Protocol >::~BasicDhcpClient ( )
inline

stop receiving messages and 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 = {} )
inlinenoexcept

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 >
int join::BasicDhcpClient< Protocol >::discover ( DhcpPacket & offer,
const IpAddress & wants = IpAddress::ipv4Wildcard,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inlinenoexcept

broadcast a DISCOVER message and wait for an OFFER.

Parameters
offeroffer received.
wantsaddress the client would like to get, wildcard to let the server choose.
timeoutanswer timeout.
Returns
0 on success, -1 on failure.

◆ exchange()

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

send a message and wait for its answer.

Parameters
requestmessage to send.
answeranswer received.
destinationdestination address.
expectedmessage type the answer must carry.
timeoutanswer timeout.
Returns
0 on success, -1 on failure.

◆ inform()

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

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

Parameters
ackacknowledgement received.
clientaddress the client already owns.
timeoutanswer timeout.
Returns
0 on success, -1 on failure.

◆ 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) )
inlinenoexcept

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 >
int join::BasicDhcpClient< Protocol >::renew ( DhcpPacket & ack,
const IpAddress & client,
const IpAddress & server,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inlinenoexcept

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

Parameters
ackacknowledgement received.
clientaddress the client currently owns.
serveraddress of the server holding the lease.
timeoutanswer timeout.
Returns
0 on success, -1 on failure.

◆ request()

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

broadcast a REQUEST message and wait for an ACK.

Parameters
ackacknowledgement received.
wantsaddress the client asks to be assigned.
serveraddress of the server the offer came from.
timeoutanswer timeout.
Returns
0 on success, -1 on failure.

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