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

DHCP server. More...

#include <dhcp.hpp>

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

Public Types

using RequestNotify = Function<void (DhcpMessage::MessageType type, const DhcpPacket& request)>
 request notification callback.
 

Public Member Functions

 BasicDhcpServer ()=delete
 create the BasicDhcpServer instance.
 
 BasicDhcpServer (const std::string &interface, Reactor &reactor=ReactorThread::reactor())
 create the instance bound to the given interface.
 
 ~BasicDhcpServer ()
 stop receiving messages and destroy the instance.
 
int offer (const DhcpPacket &request, const IpAddress &address, const DhcpOption &options={}) noexcept
 answer a DISCOVER message with an OFFER.
 
int ack (const DhcpPacket &request, const IpAddress &address, const DhcpOption &options={}) noexcept
 answer a REQUEST message with an ACK.
 
int nak (const DhcpPacket &request, const std::string &message={}) noexcept
 refuse a REQUEST message with a NAK.
 
int setRequestListener (RequestNotify cb) noexcept
 set the callback called on every request received, whatever its message type.
 
int unsetRequestListener () noexcept
 unset the request callback, it is no longer called once this returns.
 

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

DHCP server.

Member Typedef Documentation

◆ RequestNotify

template<class Protocol >
using join::BasicDhcpServer< Protocol >::RequestNotify = Function<void (DhcpMessage::MessageType type, const DhcpPacket& request)>

request notification callback.

Constructor & Destructor Documentation

◆ BasicDhcpServer() [1/2]

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

create the BasicDhcpServer instance.

◆ BasicDhcpServer() [2/2]

template<class Protocol >
join::BasicDhcpServer< Protocol >::BasicDhcpServer ( const std::string & interface,
Reactor & reactor = ReactorThread::reactor () )
inlineexplicit

create the instance bound to the given interface.

Parameters
interfaceinterface name.
reactorreactor instance.
Exceptions
std::system_errorif the interface is unknown or the socket could not be bound to it.

◆ ~BasicDhcpServer()

template<class Protocol >
join::BasicDhcpServer< Protocol >::~BasicDhcpServer ( )
inline

stop receiving messages and destroy the instance.

Member Function Documentation

◆ ack()

template<class Protocol >
int join::BasicDhcpServer< Protocol >::ack ( const DhcpPacket & request,
const IpAddress & address,
const DhcpOption & options = {} )
inlinenoexcept

answer a REQUEST message with an ACK.

Parameters
requestmessage being answered.
addressaddress assigned to the client.
optionsoptions to advertise, the message type and the server identifier are added.
Returns
0 on success, -1 on failure.

◆ nak()

template<class Protocol >
int join::BasicDhcpServer< Protocol >::nak ( const DhcpPacket & request,
const std::string & message = {} )
inlinenoexcept

refuse a REQUEST message with a NAK.

Parameters
requestmessage being refused.
messagehuman readable reason, empty to give none.
Returns
0 on success, -1 on failure.

◆ offer()

template<class Protocol >
int join::BasicDhcpServer< Protocol >::offer ( const DhcpPacket & request,
const IpAddress & address,
const DhcpOption & options = {} )
inlinenoexcept

answer a DISCOVER message with an OFFER.

Parameters
requestmessage being answered.
addressaddress offered to the client.
optionsoptions to advertise, the message type and the server identifier are added.
Returns
0 on success, -1 on failure.

◆ setRequestListener()

template<class Protocol >
int join::BasicDhcpServer< Protocol >::setRequestListener ( RequestNotify cb)
inlinenoexcept

set the callback called on every request received, whatever its message type.

Parameters
cbcallback, called from the reactor thread, must not throw nor destroy the instance.
Returns
0 on success, -1 on failure.

◆ unsetRequestListener()

template<class Protocol >
int join::BasicDhcpServer< Protocol >::unsetRequestListener ( )
inlinenoexcept

unset the request callback, it is no longer called once this returns.

Returns
0 on success, -1 on failure.

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