join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
join::NetlinkManager Class Referenceabstract

base class for netlink-based managers. More...

#include <netlink_manager.hpp>

Inheritance diagram for join::NetlinkManager:
Collaboration diagram for join::NetlinkManager:

Classes

struct  PendingRequest
 pending synchronous request. More...
 

Public Member Functions

 NetlinkManager (uint32_t groups, Reactor &reactor=ReactorThread::reactor())
 create instance.
 
 NetlinkManager (const NetlinkManager &)=delete
 create instance by copy.
 
 NetlinkManager (NetlinkManager &&)=delete
 create instance by move.
 
NetlinkManageroperator= (const NetlinkManager &)=delete
 assign instance by copy.
 
NetlinkManageroperator= (NetlinkManager &&)=delete
 assign instance by move.
 
virtual ~NetlinkManager ()=default
 destroy instance.
 
Reactorreactor () const noexcept
 get the event loop reactor.
 
- 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 start ()
 start listening for netlink events.
 
void stop ()
 stop listening for netlink events.
 
int sendRequest (struct nlmsghdr *nlh, bool sync, std::chrono::milliseconds timeout=std::chrono::seconds(5))
 send a netlink request, optionally waiting for the ack.
 
virtual void onReadable (int fd) override final
 method called when data are ready to be read on handle.
 
virtual void onMessage (struct nlmsghdr *nlh)=0
 dispatch a single RTM_* message to the derived class.
 
void notifyRequest (uint32_t seq, const std::error_code &error=std::error_code())
 notify a pending synchronous request.
 
void notifyAllRequests (const std::error_code &error)
 notify every pending synchronous requests with an error.
 
- 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.
 

Static Protected Member Functions

static void addAttributes (struct nlmsghdr *nlh, int type, const void *data, int alen)
 add an attribute to a netlink message.
 
static struct rtattr * startNestedAttributes (struct nlmsghdr *nlh, int type)
 open a nested attribute block.
 
static int stopNestedAttributes (struct nlmsghdr *nlh, struct rtattr *nested)
 close a nested attribute block.
 
template<typename T , typename Flag >
static Flag updateValue (T &oldVal, const T &newVal, Flag changed)
 update a value in place and report whether it changed.
 

Protected Attributes

Netlink::Socket _socket
 underlying socket.
 
std::unique_ptr< char[]> _buffer
 internal read buffer.
 
std::atomic< uint32_t > _seq
 sequence number.
 
std::unordered_map< uint32_t, std::unique_ptr< PendingRequest > > _pending
 synchronous requests indexed by sequence number.
 
Mutex _syncMutex
 protection mutex.
 
Reactor_reactor
 event loop reactor.
 

Static Protected Attributes

static constexpr int _rcvBufferSize = 1 << 20
 socket receive buffer size.
 
static constexpr size_t _bufferSize = 16384
 internal buffer size.
 

Detailed Description

base class for netlink-based managers.

Constructor & Destructor Documentation

◆ NetlinkManager() [1/3]

NetlinkManager::NetlinkManager ( uint32_t groups,
Reactor & reactor = ReactorThread::reactor () )

create instance.

Parameters
groupsnetlink multicast group bitmask to subscribe to.
reactorevent loop reactor.

◆ NetlinkManager() [2/3]

join::NetlinkManager::NetlinkManager ( const NetlinkManager & )
delete

create instance by copy.

◆ NetlinkManager() [3/3]

join::NetlinkManager::NetlinkManager ( NetlinkManager && )
delete

create instance by move.

◆ ~NetlinkManager()

virtual join::NetlinkManager::~NetlinkManager ( )
virtualdefault

destroy instance.

Member Function Documentation

◆ addAttributes()

void NetlinkManager::addAttributes ( struct nlmsghdr * nlh,
int type,
const void * data,
int alen )
staticprotected

add an attribute to a netlink message.

Parameters
nlhnetlink message header.
typeattribute type (RTA_*, IFA_*, NDA_*, …).
dataattribute payload.
alenpayload length in bytes.

◆ notifyAllRequests()

void NetlinkManager::notifyAllRequests ( const std::error_code & error)
protected

notify every pending synchronous requests with an error.

Parameters
errorerror reported to each pending request.

◆ notifyRequest()

void NetlinkManager::notifyRequest ( uint32_t seq,
const std::error_code & error = std::error_code () )
protected

notify a pending synchronous request.

Parameters
seqsequence number of the completed request.
errorerror reported to the request (default: success).

◆ onMessage()

virtual void join::NetlinkManager::onMessage ( struct nlmsghdr * nlh)
protectedpure virtual

dispatch a single RTM_* message to the derived class.

Parameters
nlhthe netlink message to process.

◆ onReadable()

void NetlinkManager::onReadable ( int fd)
finaloverrideprotectedvirtual

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

Parameters
fdfile descriptor.

Reimplemented from join::EventHandler.

◆ operator=() [1/2]

NetlinkManager & join::NetlinkManager::operator= ( const NetlinkManager & )
delete

assign instance by copy.

◆ operator=() [2/2]

NetlinkManager & join::NetlinkManager::operator= ( NetlinkManager && )
delete

assign instance by move.

◆ reactor()

Reactor & NetlinkManager::reactor ( ) const
noexcept

get the event loop reactor.

Returns
reference to the reactor.

◆ sendRequest()

int NetlinkManager::sendRequest ( struct nlmsghdr * nlh,
bool sync,
std::chrono::milliseconds timeout = std::chrono::seconds (5) )
protected

send a netlink request, optionally waiting for the ack.

Parameters
nlhnetlink message to send.
syncif true, block until NLMSG_ERROR/NLMSG_DONE is received.
timeoutmaximum wait duration when sync is true (default: 5 seconds).
Returns
0 on success, -1 on failure.

◆ start()

void NetlinkManager::start ( )
protected

start listening for netlink events.

◆ startNestedAttributes()

struct rtattr * NetlinkManager::startNestedAttributes ( struct nlmsghdr * nlh,
int type )
staticprotected

open a nested attribute block.

Parameters
nlhnetlink message header.
typecontainer attribute type.
Returns
pointer to the container rtattr (pass to stopNestedAttributes).

◆ stop()

void NetlinkManager::stop ( )
protected

stop listening for netlink events.

◆ stopNestedAttributes()

int NetlinkManager::stopNestedAttributes ( struct nlmsghdr * nlh,
struct rtattr * nested )
staticprotected

close a nested attribute block.

Parameters
nlhnetlink message header.
nestedpointer returned by startNestedAttributes.
Returns
current message length.

◆ updateValue()

template<typename T , typename Flag >
static Flag join::NetlinkManager::updateValue ( T & oldVal,
const T & newVal,
Flag changed )
inlinestaticprotected

update a value in place and report whether it changed.

Parameters
oldValvalue to update.
newValcandidate new value.
Returns
true if the value was changed, false otherwise.

Member Data Documentation

◆ _buffer

std::unique_ptr<char[]> join::NetlinkManager::_buffer
protected

internal read buffer.

◆ _bufferSize

size_t join::NetlinkManager::_bufferSize = 16384
staticconstexprprotected

internal buffer size.

◆ _pending

std::unordered_map<uint32_t, std::unique_ptr<PendingRequest> > join::NetlinkManager::_pending
protected

synchronous requests indexed by sequence number.

◆ _rcvBufferSize

int join::NetlinkManager::_rcvBufferSize = 1 << 20
staticconstexprprotected

socket receive buffer size.

◆ _reactor

Reactor& join::NetlinkManager::_reactor
protected

event loop reactor.

◆ _seq

std::atomic<uint32_t> join::NetlinkManager::_seq
protected

sequence number.

◆ _socket

Netlink::Socket join::NetlinkManager::_socket
protected

underlying socket.

◆ _syncMutex

Mutex join::NetlinkManager::_syncMutex
protected

protection mutex.


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