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

carries NDP messages over an ICMPv6 socket. More...

#include <ndp.hpp>

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

Classes

struct  NeighborRequest
 neighbor solicitation waiting for the advertisement of its target. More...
 

Public Types

using Socket = typename Protocol::Socket
 
using Endpoint = typename Protocol::Endpoint
 
using NeighborSolicitationNotify = Function<void (const NeighborSolicitation& solicitation)>
 neighbor solicitation notification callback.
 
using NeighborAdvertisementNotify = Function<void (const NeighborAdvertisement& advert)>
 neighbor advertisement notification callback.
 

Public Member Functions

 BasicNdp ()=delete
 create the BasicNdp instance.
 
 BasicNdp (const std::string &interface, uint8_t filter, const IpAddress &group, Reactor &reactor)
 create the instance bound to the given interface.
 
 BasicNdp (const BasicNdp &other)=delete
 create instance by copy.
 
BasicNdp & operator= (const BasicNdp &other)=delete
 assign instance by copy.
 
 BasicNdp (BasicNdp &&other)=delete
 create instance by move.
 
BasicNdp & operator= (BasicNdp &&other)=delete
 assign instance by move.
 
virtual ~BasicNdp ()=default
 destroy the instance.
 
int neighborSolicit (const IpAddress &target, NeighborAdvertisement &advert, std::chrono::milliseconds timeout=std::chrono::seconds(1)) noexcept
 send a neighbor solicitation and wait for the advertisement of the target.
 
int neighborAdvertise (const NeighborAdvertisement &advert, const IpAddress &destination=IpAddress::ipv6AllNodes) noexcept
 send a neighbor advertisement, the interface hardware address is used if it carries none.
 
int neighborAdvertise (const NeighborAdvertisement &advert, const NeighborSolicitation &solicitation) noexcept
 answer a neighbor solicitation, to the solicitor or to all the nodes of the link if it has no address.
 
int setNeighborSolicitationListener (NeighborSolicitationNotify cb) noexcept
 set the callback called on every neighbor solicitation received.
 
int unsetNeighborSolicitationListener () noexcept
 unset the neighbor solicitation callback, it is no longer called once this returns.
 
int setNeighborAdvertisementListener (NeighborAdvertisementNotify cb) noexcept
 set the callback called on every neighbor advertisement received, solicited or not.
 
int unsetNeighborAdvertisementListener () noexcept
 unset the neighbor advertisement callback, it is no longer called once this returns.
 
const std::string & interface () const noexcept
 get the name of the interface the instance is bound to.
 
unsigned int index () const noexcept
 get the index 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.
 
- Public Member Functions inherited from join::EventHandler
 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.
 

Protected Member Functions

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 applyFilter (uint8_t type, bool pass) noexcept
 pass or block a message type in the ICMPv6 filter.
 
int send (const char *data, size_t size, const IpAddress &destination)
 write a message on the interface.
 
ssize_t receive (char *data, size_t maxSize, IpAddress &from)
 read a message that passed the checks common to every NDP message.
 
bool onNeighborMessage (size_t size, const IpAddress &from) noexcept
 hand a received neighbor message to its listener, and an advertisement to the solicitations waiting for it.
 
- 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 onReadable (int fd)
 method called when data are ready to be read 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.
 

Static Protected Member Functions

static unsigned int lookup (const std::string &interface)
 get the index of the given interface.
 

Protected Attributes

Socket _socket
 underlying socket.
 
NdpMessage _message
 NDP message codec.
 
std::unique_ptr< char[]> _buffer
 receive buffer.
 
const std::string _interface
 interface name.
 
const unsigned int _index
 interface index.
 
const MacAddress _hardware
 hardware address of the interface.
 
Reactor & _reactor
 event loop reactor.
 
struct icmp6_filter _filter
 ICMPv6 filter applied to the socket, only accessed from the reactor thread once constructed.
 
std::vector< NeighborRequest * > _neighborPending
 neighbor solicitations waiting for an advertisement, only accessed from the reactor thread.
 
Mutex _neighborMutex
 neighbor solicitations protection mutex.
 
NeighborSolicitationNotify _neighborSolicitationListener
 neighbor solicitation listener, only accessed from the reactor thread.
 
bool _neighborSolicitationCalling = false
 
NeighborAdvertisementNotify _neighborAdvertisementListener
 neighbor advertisement listener, only accessed from the reactor thread.
 
bool _neighborAdvertisementCalling = false
 

Detailed Description

template<class Protocol>
class join::BasicNdp< Protocol >

carries NDP messages over an ICMPv6 socket.

Member Typedef Documentation

◆ Endpoint

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

◆ NeighborAdvertisementNotify

template<class Protocol >
using join::BasicNdp< Protocol >::NeighborAdvertisementNotify = Function<void (const NeighborAdvertisement& advert)>

neighbor advertisement notification callback.

◆ NeighborSolicitationNotify

template<class Protocol >
using join::BasicNdp< Protocol >::NeighborSolicitationNotify = Function<void (const NeighborSolicitation& solicitation)>

neighbor solicitation notification callback.

◆ Socket

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

Constructor & Destructor Documentation

◆ BasicNdp() [1/4]

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

create the BasicNdp instance.

◆ BasicNdp() [2/4]

template<class Protocol >
join::BasicNdp< Protocol >::BasicNdp ( const std::string & interface,
uint8_t filter,
const IpAddress & group,
Reactor & reactor )
inline

create the instance bound to the given interface.

Parameters
interfaceinterface name.
filtermessage type the instance accepts.
groupmulticast group the instance joins, wildcard to join none.
reactorreactor instance.
Exceptions
std::system_errorif the interface is unknown or the socket could not be set up.

◆ BasicNdp() [3/4]

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

create instance by copy.

Parameters
otherother object to copy.

◆ BasicNdp() [4/4]

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

create instance by move.

Parameters
otherother object to move.

◆ ~BasicNdp()

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

destroy the instance.

Member Function Documentation

◆ applyFilter()

template<class Protocol >
int join::BasicNdp< Protocol >::applyFilter ( uint8_t type,
bool pass )
inlineprotectednoexcept

pass or block a message type in the ICMPv6 filter.

Parameters
typemessage type.
passtrue to pass the message type, false to block it.
Returns
0 on success, -1 on failure.

◆ attach()

template<class Protocol >
void join::BasicNdp< Protocol >::attach ( )
inlineprotected

register with the reactor, to be called by the final class once fully constructed.

Exceptions
std::system_errorif the registration failed.

◆ detach()

template<class Protocol >
void join::BasicNdp< Protocol >::detach ( )
inlineprotectednoexcept

unregister from the reactor, to be called by the final class before destruction starts.

◆ hardware()

template<class Protocol >
const MacAddress & join::BasicNdp< Protocol >::hardware ( ) const
inlinenoexcept

get the hardware address of the interface the instance is bound to.

Returns
the hardware address.

◆ index()

template<class Protocol >
unsigned int join::BasicNdp< Protocol >::index ( ) const
inlinenoexcept

get the index of the interface the instance is bound to.

Returns
the interface index.

◆ interface()

template<class Protocol >
const std::string & join::BasicNdp< Protocol >::interface ( ) const
inlinenoexcept

get the name of the interface the instance is bound to.

Returns
the interface name.

◆ lookup()

template<class Protocol >
static unsigned int join::BasicNdp< Protocol >::lookup ( const std::string & interface)
inlinestaticprotected

get the index of the given interface.

Parameters
interfaceinterface name.
Returns
the interface index.
Exceptions
std::system_errorif the interface is unknown.

◆ neighborAdvertise() [1/2]

template<class Protocol >
int join::BasicNdp< Protocol >::neighborAdvertise ( const NeighborAdvertisement & advert,
const IpAddress & destination = IpAddress::ipv6AllNodes )
inlinenoexcept

send a neighbor advertisement, the interface hardware address is used if it carries none.

Parameters
advertadvertisement to send.
destinationdestination address, all the nodes of the link by default.
Returns
0 on success, -1 on failure.

◆ neighborAdvertise() [2/2]

template<class Protocol >
int join::BasicNdp< Protocol >::neighborAdvertise ( const NeighborAdvertisement & advert,
const NeighborSolicitation & solicitation )
inlinenoexcept

answer a neighbor solicitation, to the solicitor or to all the nodes of the link if it has no address.

Parameters
advertadvertisement to send, the interface hardware address is used if it carries none.
solicitationsolicitation to answer.
Returns
0 on success, -1 on failure.

◆ neighborSolicit()

template<class Protocol >
int join::BasicNdp< Protocol >::neighborSolicit ( const IpAddress & target,
NeighborAdvertisement & advert,
std::chrono::milliseconds timeout = std::chrono::seconds (1) )
inlinenoexcept

send a neighbor solicitation and wait for the advertisement of the target.

Parameters
targetaddress of the neighbor to resolve.
advertreceives the advertisement.
timeoutmaximum wait duration.
Returns
0 on success, -1 on failure.

◆ onNeighborMessage()

template<class Protocol >
bool join::BasicNdp< Protocol >::onNeighborMessage ( size_t size,
const IpAddress & from )
inlineprotectednoexcept

hand a received neighbor message to its listener, and an advertisement to the solicitations waiting for it.

Parameters
sizemessage size.
fromaddress the message came from.
Returns
true if the message is a neighbor solicitation or advertisement, false otherwise.

◆ operator=() [1/2]

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

assign instance by move.

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

◆ operator=() [2/2]

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

assign instance by copy.

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

◆ receive()

template<class Protocol >
ssize_t join::BasicNdp< Protocol >::receive ( char * data,
size_t maxSize,
IpAddress & from )
inlineprotected

read a message that passed the checks common to every NDP message.

Parameters
databuffer used to store the message.
maxSizemaximum number of bytes to read.
fromaddress the message came from.
Returns
the message size, -1 if nothing valid was read.

◆ send()

template<class Protocol >
int join::BasicNdp< Protocol >::send ( const char * data,
size_t size,
const IpAddress & destination )
inlineprotected

write a message on the interface.

Parameters
datamessage to write.
sizemessage size.
destinationdestination address.
Returns
0 on success, -1 on failure.

◆ setNeighborAdvertisementListener()

template<class Protocol >
int join::BasicNdp< Protocol >::setNeighborAdvertisementListener ( NeighborAdvertisementNotify cb)
inlinenoexcept

set the callback called on every neighbor advertisement received, solicited or not.

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

◆ setNeighborSolicitationListener()

template<class Protocol >
int join::BasicNdp< Protocol >::setNeighborSolicitationListener ( NeighborSolicitationNotify cb)
inlinenoexcept

set the callback called on every neighbor solicitation received.

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

◆ unsetNeighborAdvertisementListener()

template<class Protocol >
int join::BasicNdp< Protocol >::unsetNeighborAdvertisementListener ( )
inlinenoexcept

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

Returns
0 on success, -1 on failure.

◆ unsetNeighborSolicitationListener()

template<class Protocol >
int join::BasicNdp< Protocol >::unsetNeighborSolicitationListener ( )
inlinenoexcept

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

Returns
0 on success, -1 on failure.

Member Data Documentation

◆ _buffer

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

receive buffer.

◆ _filter

template<class Protocol >
struct icmp6_filter join::BasicNdp< Protocol >::_filter
protected

ICMPv6 filter applied to the socket, only accessed from the reactor thread once constructed.

◆ _hardware

template<class Protocol >
const MacAddress join::BasicNdp< Protocol >::_hardware
protected

hardware address of the interface.

◆ _index

template<class Protocol >
const unsigned int join::BasicNdp< Protocol >::_index
protected

interface index.

◆ _interface

template<class Protocol >
const std::string join::BasicNdp< Protocol >::_interface
protected

interface name.

◆ _message

template<class Protocol >
NdpMessage join::BasicNdp< Protocol >::_message
protected

NDP message codec.

◆ _neighborAdvertisementCalling

template<class Protocol >
bool join::BasicNdp< Protocol >::_neighborAdvertisementCalling = false
protected

set while the neighbor advertisement listener is being called and still set, only accessed from the reactor thread.

◆ _neighborAdvertisementListener

template<class Protocol >
NeighborAdvertisementNotify join::BasicNdp< Protocol >::_neighborAdvertisementListener
protected

neighbor advertisement listener, only accessed from the reactor thread.

◆ _neighborMutex

template<class Protocol >
Mutex join::BasicNdp< Protocol >::_neighborMutex
protected

neighbor solicitations protection mutex.

◆ _neighborPending

template<class Protocol >
std::vector<NeighborRequest*> join::BasicNdp< Protocol >::_neighborPending
protected

neighbor solicitations waiting for an advertisement, only accessed from the reactor thread.

◆ _neighborSolicitationCalling

template<class Protocol >
bool join::BasicNdp< Protocol >::_neighborSolicitationCalling = false
protected

set while the neighbor solicitation listener is being called and still set, only accessed from the reactor thread.

◆ _neighborSolicitationListener

template<class Protocol >
NeighborSolicitationNotify join::BasicNdp< Protocol >::_neighborSolicitationListener
protected

neighbor solicitation listener, only accessed from the reactor thread.

◆ _reactor

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

event loop reactor.

◆ _socket

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

underlying socket.


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