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

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

#include <netlinkmanager.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=nullptr)
 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.
 

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.
 
template<class Rep , class Period >
int waitResponse (ScopedLock< Mutex > &lock, uint32_t seq, std::chrono::duration< Rep, Period > timeout)
 wait for specific netlink response.
 
virtual void onReceive (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, int error=0)
 notify a pending synchronous request.
 

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

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 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 = nullptr )

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.

◆ notifyRequest()

void NetlinkManager::notifyRequest ( uint32_t seq,
int error = 0 )
protected

notify a pending synchronous request.

Parameters
seqsequence number of the completed request.
errorkernel error code (0 on success, positive errno).

◆ 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.

◆ onReceive()

void NetlinkManager::onReceive ( 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
pointer 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.

◆ waitResponse()

template<class Rep , class Period >
int join::NetlinkManager::waitResponse ( ScopedLock< Mutex > & lock,
uint32_t seq,
std::chrono::duration< Rep, Period > timeout )
inlineprotected

wait for specific netlink response.

Parameters
lockmutex previously locked by the calling thread.
seqsequence number to wait for.
timeoutmaximum wait duration.
Returns
0 on success, -1 on failure.

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.

◆ _reactor

Reactor* join::NetlinkManager::_reactor
protected

event loop reactor.

◆ _seq

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

sequence number.

◆ _syncMutex

Mutex join::NetlinkManager::_syncMutex
protected

protection mutex.


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