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

basic DNS resolver over datagram socket. More...

#include <resolver.hpp>

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

Classes

struct  PendingRequest
 pending synchronous request. More...
 

Public Types

using Socket = typename Protocol::Socket
 
using Endpoint = typename Protocol::Endpoint
 
using DnsNotify = std::function<void (const DnsPacket&)>
 notification callback definition.
 

Public Member Functions

 BasicDatagramResolver (const std::string &server={}, uint16_t port=Protocol::defaultPort, Reactor &reactor=ReactorThread::reactor())
 
 BasicDatagramResolver (Socket &&socket, const std::string &server={}, uint16_t port=Protocol::defaultPort, Reactor &reactor=ReactorThread::reactor())
 construct the resolver instance.
 
 BasicDatagramResolver (const BasicDatagramResolver &other)=delete
 copy constructor.
 
BasicDatagramResolveroperator= (const BasicDatagramResolver &other)=delete
 copy assignment operator.
 
 BasicDatagramResolver (BasicDatagramResolver &&other)=delete
 move constructor.
 
BasicDatagramResolveroperator= (BasicDatagramResolver &&other)=delete
 move assignment operator.
 
virtual ~BasicDatagramResolver () noexcept
 destroy instance.
 
IpAddressList resolveAllAddress (const std::string &host, int family, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host name and return all IP addresses found.
 
IpAddressList resolveAllAddress (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host name and return all IP addresses found.
 
IpAddress resolveAddress (const std::string &host, int family, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host name using address family.
 
IpAddress resolveAddress (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host name.
 
AliasList resolveAllName (const IpAddress &address, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve all host address.
 
std::string resolveName (const IpAddress &address, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host address.
 
ServerList resolveAllNameServer (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve all host name server.
 
std::string resolveNameServer (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host name server.
 
std::string resolveAuthority (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host start of authority name server.
 
ExchangerList resolveAllMailExchanger (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve all host mail exchanger.
 
std::string resolveMailExchanger (const std::string &host, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 resolve host mail exchanger.
 
- 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.
 

Static Public Member Functions

static IpAddressList lookupAllAddress (const std::string &host, int family)
 resolve host name using system name servers and return all IP addresses found.
 
static IpAddressList lookupAllAddress (const std::string &host)
 resolve host name using system name servers and return all IP addresses found.
 
static IpAddress lookupAddress (const std::string &host, int family)
 resolve host name using system name servers.
 
static IpAddress lookupAddress (const std::string &host)
 resolve host name using system name servers.
 
static AliasList lookupAllName (const IpAddress &address)
 resolve all host address.
 
static std::string lookupName (const IpAddress &address)
 resolve host address.
 
static ServerList lookupAllNameServer (const std::string &host)
 resolve all host name server.
 
static std::string lookupNameServer (const std::string &host)
 resolve host name server.
 
static std::string lookupAuthority (const std::string &host)
 resolve host start of authority name server.
 
static ExchangerList lookupAllMailExchanger (const std::string &host)
 resolve all host mail exchanger.
 
static std::string lookupMailExchanger (const std::string &host)
 resolve host mail exchanger.
 
static IpAddressList nameServers () noexcept
 get IP address of the currently configured name servers.
 
static uint16_t resolveService (const std::string &service) noexcept
 resolve service name.
 

Public Attributes

DnsNotify onSuccess
 callback called when a lookup sequence succeed.
 
DnsNotify onFailure
 callback called when a lookup sequence failed.
 

Protected Member Functions

virtual int connect (const Endpoint &endpoint, std::chrono::milliseconds timeout=std::chrono::seconds(5)) noexcept
 make a connection to the given endpoint.
 
virtual int disconnect (std::chrono::milliseconds timeout=std::chrono::seconds(5)) noexcept
 shutdown the connection.
 
virtual bool needReconnection () noexcept
 check if client must reconnect.
 
int reconnect (const Endpoint &endpoint, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 reconnect to the remote DNS server.
 
virtual ssize_t read (char *data, size_t maxSize) noexcept
 read a DNS message.
 
virtual ssize_t write (const char *data, size_t size) noexcept
 write a DNS message.
 
int query (DnsPacket &packet, std::chrono::milliseconds timeout)
 serialize and send a DNS query, waiting for a response.
 
void onReadable (int fd) override final
 method called when data are ready to be read on handle.
 
void onClose (int fd) override final
 method called when handle is closed.
 
void notify (const DnsNotify &func, const DnsPacket &packet) const noexcept
 safe way to notify DNS events.
 
- 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 onError (int fd)
 method called when an error occurred on handle.
 

Protected Attributes

DnsMessage _message
 DNS message codec.
 
Socket _socket
 underlying socket.
 
std::string _server
 remote DNS server.
 
uint16_t _port
 remote DNS server port.
 
Reactor_reactor
 event loop reactor.
 
std::unique_ptr< char[]> _buffer
 reception buffer.
 
std::unordered_map< uint16_t, std::unique_ptr< PendingRequest > > _pending
 synchronous requests indexed by sequence number.
 
Mutex _syncMutex
 protection mutex.
 

Static Protected Attributes

static constexpr size_t _headerSize = 12
 DNS message header size.
 

Detailed Description

template<class Protocol>
class join::BasicDatagramResolver< Protocol >

basic DNS resolver over datagram socket.

Member Typedef Documentation

◆ DnsNotify

template<class Protocol >
using join::BasicDatagramResolver< Protocol >::DnsNotify = std::function<void (const DnsPacket&)>

notification callback definition.

◆ Endpoint

template<class Protocol >
using join::BasicDatagramResolver< Protocol >::Endpoint = typename Protocol::Endpoint

◆ Socket

template<class Protocol >
using join::BasicDatagramResolver< Protocol >::Socket = typename Protocol::Socket

Constructor & Destructor Documentation

◆ BasicDatagramResolver() [1/4]

template<class Protocol >
join::BasicDatagramResolver< Protocol >::BasicDatagramResolver ( const std::string & server = {},
uint16_t port = Protocol::defaultPort,
Reactor & reactor = ReactorThread::reactor () )
inlineexplicit

◆ BasicDatagramResolver() [2/4]

template<class Protocol >
join::BasicDatagramResolver< Protocol >::BasicDatagramResolver ( Socket && socket,
const std::string & server = {},
uint16_t port = Protocol::defaultPort,
Reactor & reactor = ReactorThread::reactor () )
inlineexplicit

construct the resolver instance.

Parameters
serverremote DNS server hostname or IP address.
portremote DNS server port.
reactorreactor instance.

◆ BasicDatagramResolver() [3/4]

template<class Protocol >
join::BasicDatagramResolver< Protocol >::BasicDatagramResolver ( const BasicDatagramResolver< Protocol > & other)
delete

copy constructor.

Parameters
otherother object to copy.

◆ BasicDatagramResolver() [4/4]

template<class Protocol >
join::BasicDatagramResolver< Protocol >::BasicDatagramResolver ( BasicDatagramResolver< Protocol > && other)
delete

move constructor.

Parameters
otherother object to move.

◆ ~BasicDatagramResolver()

template<class Protocol >
virtual join::BasicDatagramResolver< Protocol >::~BasicDatagramResolver ( )
inlinevirtualnoexcept

destroy instance.

Member Function Documentation

◆ connect()

template<class Protocol >
virtual int join::BasicDatagramResolver< Protocol >::connect ( const Endpoint & endpoint,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inlineprotectedvirtualnoexcept

make a connection to the given endpoint.

Parameters
endpointendpoint to connect to.
timeouttimeout duration.
Returns
0 on success, -1 on failure.

◆ disconnect()

template<class Protocol >
virtual int join::BasicDatagramResolver< Protocol >::disconnect ( std::chrono::milliseconds timeout = std::chrono::seconds (5))
inlineprotectedvirtualnoexcept

shutdown the connection.

Returns
0 on success, -1 on failure.

◆ lookupAddress() [1/2]

template<class Protocol >
static IpAddress join::BasicDatagramResolver< Protocol >::lookupAddress ( const std::string & host)
inlinestatic

resolve host name using system name servers.

Parameters
hosthost name to resolve.
Returns
the first resolved IP address found, wildcard address on error.

◆ lookupAddress() [2/2]

template<class Protocol >
static IpAddress join::BasicDatagramResolver< Protocol >::lookupAddress ( const std::string & host,
int family )
inlinestatic

resolve host name using system name servers.

Parameters
hosthost name to resolve.
familyaddress family.
Returns
the first resolved IP address found, wildcard address on error.

◆ lookupAllAddress() [1/2]

template<class Protocol >
static IpAddressList join::BasicDatagramResolver< Protocol >::lookupAllAddress ( const std::string & host)
inlinestatic

resolve host name using system name servers and return all IP addresses found.

Parameters
hosthost name to resolve.
Returns
the resolved IP address list, empty on error.

◆ lookupAllAddress() [2/2]

template<class Protocol >
static IpAddressList join::BasicDatagramResolver< Protocol >::lookupAllAddress ( const std::string & host,
int family )
inlinestatic

resolve host name using system name servers and return all IP addresses found.

Parameters
hosthost name to resolve.
familyaddress family.
Returns
the resolved IP address list, empty on error.

◆ lookupAllMailExchanger()

template<class Protocol >
static ExchangerList join::BasicDatagramResolver< Protocol >::lookupAllMailExchanger ( const std::string & host)
inlinestatic

resolve all host mail exchanger.

Parameters
hosthost name to resolve.
Returns
the resolved mail exchanger list.

◆ lookupAllName()

template<class Protocol >
static AliasList join::BasicDatagramResolver< Protocol >::lookupAllName ( const IpAddress & address)
inlinestatic

resolve all host address.

Parameters
addresshost address to resolve.
Returns
the resolved alias list.

◆ lookupAllNameServer()

template<class Protocol >
static ServerList join::BasicDatagramResolver< Protocol >::lookupAllNameServer ( const std::string & host)
inlinestatic

resolve all host name server.

Parameters
hosthost name to resolve.
Returns
the resolved name server list.

◆ lookupAuthority()

template<class Protocol >
static std::string join::BasicDatagramResolver< Protocol >::lookupAuthority ( const std::string & host)
inlinestatic

resolve host start of authority name server.

Parameters
hosthost name to resolve.
Returns
the start of authority name server.

◆ lookupMailExchanger()

template<class Protocol >
static std::string join::BasicDatagramResolver< Protocol >::lookupMailExchanger ( const std::string & host)
inlinestatic

resolve host mail exchanger.

Parameters
hosthost name to resolve.
Returns
the first resolved mail exchanger.

◆ lookupName()

template<class Protocol >
static std::string join::BasicDatagramResolver< Protocol >::lookupName ( const IpAddress & address)
inlinestatic

resolve host address.

Parameters
addresshost address to resolve.
Returns
the first resolved alias.

◆ lookupNameServer()

template<class Protocol >
static std::string join::BasicDatagramResolver< Protocol >::lookupNameServer ( const std::string & host)
inlinestatic

resolve host name server.

Parameters
hosthost name to resolve.
Returns
the first resolved name server.

◆ nameServers()

template<class Protocol >
static IpAddressList join::BasicDatagramResolver< Protocol >::nameServers ( )
inlinestaticnoexcept

get IP address of the currently configured name servers.

Returns
a list of configured name servers.

◆ needReconnection()

template<class Protocol >
virtual bool join::BasicDatagramResolver< Protocol >::needReconnection ( )
inlineprotectedvirtualnoexcept

check if client must reconnect.

Returns
true if reconnection is required.

◆ notify()

template<class Protocol >
void join::BasicDatagramResolver< Protocol >::notify ( const DnsNotify & func,
const DnsPacket & packet ) const
inlineprotectednoexcept

safe way to notify DNS events.

Parameters
funcfunction to call.
packetDNS packet.

◆ onClose()

template<class Protocol >
void join::BasicDatagramResolver< Protocol >::onClose ( int fd)
inlinefinaloverrideprotectedvirtual

method called when handle is closed.

Parameters
fdfile descriptor.

Reimplemented from join::EventHandler.

◆ onReadable()

template<class Protocol >
void join::BasicDatagramResolver< Protocol >::onReadable ( int fd)
inlinefinaloverrideprotectedvirtual

method called when data are ready to be read on handle.

Parameters
fdfile descriptor.

Reimplemented from join::EventHandler.

◆ operator=() [1/2]

template<class Protocol >
BasicDatagramResolver & join::BasicDatagramResolver< Protocol >::operator= ( BasicDatagramResolver< Protocol > && other)
delete

move assignment operator.

Parameters
otherother object to move.
Returns
a reference to the current object.

◆ operator=() [2/2]

template<class Protocol >
BasicDatagramResolver & join::BasicDatagramResolver< Protocol >::operator= ( const BasicDatagramResolver< Protocol > & other)
delete

copy assignment operator.

Parameters
otherother object to copy.
Returns
a reference to the current object.

◆ query()

template<class Protocol >
int join::BasicDatagramResolver< Protocol >::query ( DnsPacket & packet,
std::chrono::milliseconds timeout )
inlineprotected

serialize and send a DNS query, waiting for a response.

Parameters
packetDNS packet to send, filled with the response on success.
timeoutquery timeout.
Returns
0 on success, -1 on error.

◆ read()

template<class Protocol >
virtual ssize_t join::BasicDatagramResolver< Protocol >::read ( char * data,
size_t maxSize )
inlineprotectedvirtualnoexcept

read a DNS message.

Parameters
datadestination buffer.
maxSizemaximum number of bytes to read.
Returns
number of bytes read, or -1 on error.

◆ reconnect()

template<class Protocol >
int join::BasicDatagramResolver< Protocol >::reconnect ( const Endpoint & endpoint,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inlineprotected

reconnect to the remote DNS server.

Parameters
endpointendpoint to connect to.
timeouttimeout duration.
Returns
0 on success, -1 on failure.

◆ resolveAddress() [1/2]

template<class Protocol >
IpAddress join::BasicDatagramResolver< Protocol >::resolveAddress ( const std::string & host,
int family,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host name using address family.

Parameters
hosthost name to resolve.
familyaddress family.
timeouttimeout duration (default: 5 s).
Returns
the first resolved IP address found matching address family.

◆ resolveAddress() [2/2]

template<class Protocol >
IpAddress join::BasicDatagramResolver< Protocol >::resolveAddress ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host name.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the first resolved IP address found.

◆ resolveAllAddress() [1/2]

template<class Protocol >
IpAddressList join::BasicDatagramResolver< Protocol >::resolveAllAddress ( const std::string & host,
int family,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host name and return all IP addresses found.

Parameters
hosthost name to resolve.
familyaddress family.
timeouttimeout duration (default: 5 s).
Returns
the resolved IP address list.

◆ resolveAllAddress() [2/2]

template<class Protocol >
IpAddressList join::BasicDatagramResolver< Protocol >::resolveAllAddress ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host name and return all IP addresses found.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the resolved IP address list.

◆ resolveAllMailExchanger()

template<class Protocol >
ExchangerList join::BasicDatagramResolver< Protocol >::resolveAllMailExchanger ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve all host mail exchanger.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the resolved mail exchanger list.

◆ resolveAllName()

template<class Protocol >
AliasList join::BasicDatagramResolver< Protocol >::resolveAllName ( const IpAddress & address,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve all host address.

Parameters
addresshost address to resolve.
timeouttimeout duration (default: 5 s).
Returns
the resolved alias list.

◆ resolveAllNameServer()

template<class Protocol >
ServerList join::BasicDatagramResolver< Protocol >::resolveAllNameServer ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve all host name server.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the resolved name server list.

◆ resolveAuthority()

template<class Protocol >
std::string join::BasicDatagramResolver< Protocol >::resolveAuthority ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host start of authority name server.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the start of authority name server.

◆ resolveMailExchanger()

template<class Protocol >
std::string join::BasicDatagramResolver< Protocol >::resolveMailExchanger ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host mail exchanger.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the first resolved mail exchanger.

◆ resolveName()

template<class Protocol >
std::string join::BasicDatagramResolver< Protocol >::resolveName ( const IpAddress & address,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host address.

Parameters
addresshost address to resolve.
timeouttimeout duration (default: 5 s).
Returns
the first resolved alias.

◆ resolveNameServer()

template<class Protocol >
std::string join::BasicDatagramResolver< Protocol >::resolveNameServer ( const std::string & host,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
inline

resolve host name server.

Parameters
hosthost name to resolve.
timeouttimeout duration (default: 5 s).
Returns
the first resolved name server.

◆ resolveService()

template<class Protocol >
static uint16_t join::BasicDatagramResolver< Protocol >::resolveService ( const std::string & service)
inlinestaticnoexcept

resolve service name.

Parameters
serviceservice name to resolve (ex. "http", "ftp", "ssh" etc...).
Returns
the port resolved.

◆ write()

template<class Protocol >
virtual ssize_t join::BasicDatagramResolver< Protocol >::write ( const char * data,
size_t size )
inlineprotectedvirtualnoexcept

write a DNS message.

Parameters
datasource buffer.
sizenumber of bytes to write.
Returns
number of bytes written, or -1 on error.

Member Data Documentation

◆ _buffer

template<class Protocol >
std::unique_ptr<char[]> join::BasicDatagramResolver< Protocol >::_buffer
protected

reception buffer.

◆ _headerSize

template<class Protocol >
size_t join::BasicDatagramResolver< Protocol >::_headerSize = 12
staticconstexprprotected

DNS message header size.

◆ _message

template<class Protocol >
DnsMessage join::BasicDatagramResolver< Protocol >::_message
protected

DNS message codec.

◆ _pending

template<class Protocol >
std::unordered_map<uint16_t, std::unique_ptr<PendingRequest> > join::BasicDatagramResolver< Protocol >::_pending
protected

synchronous requests indexed by sequence number.

◆ _port

template<class Protocol >
uint16_t join::BasicDatagramResolver< Protocol >::_port
protected

remote DNS server port.

◆ _reactor

template<class Protocol >
Reactor& join::BasicDatagramResolver< Protocol >::_reactor
protected

event loop reactor.

◆ _server

template<class Protocol >
std::string join::BasicDatagramResolver< Protocol >::_server
protected

remote DNS server.

◆ _socket

template<class Protocol >
Socket join::BasicDatagramResolver< Protocol >::_socket
protected

underlying socket.

◆ _syncMutex

template<class Protocol >
Mutex join::BasicDatagramResolver< Protocol >::_syncMutex
protected

protection mutex.

◆ onFailure

template<class Protocol >
DnsNotify join::BasicDatagramResolver< Protocol >::onFailure

callback called when a lookup sequence failed.

◆ onSuccess

template<class Protocol >
DnsNotify join::BasicDatagramResolver< Protocol >::onSuccess

callback called when a lookup sequence succeed.


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