libnl 1.1.4
Enumerations
Callbacks/Customization

Callbacks and overwriting capabilities are provided to take influence in various control flows inside the library. More...

Enumerations

enum  nl_cb_action { NL_OK , NL_SKIP , NL_STOP }
 Callback actions. More...
 
enum  nl_cb_kind {
  NL_CB_DEFAULT , NL_CB_VERBOSE , NL_CB_DEBUG , NL_CB_CUSTOM ,
  __NL_CB_KIND_MAX
}
 Callback kinds. More...
 
enum  nl_cb_type {
  NL_CB_VALID , NL_CB_FINISH , NL_CB_OVERRUN , NL_CB_SKIPPED ,
  NL_CB_ACK , NL_CB_MSG_IN , NL_CB_MSG_OUT , NL_CB_INVALID ,
  NL_CB_SEQ_CHECK , NL_CB_SEND_ACK , __NL_CB_TYPE_MAX
}
 Callback types. More...
 

Callback Typedefs

typedef int(* nl_recvmsg_msg_cb_t) (struct nl_msg *msg, void *arg)
 nl_recvmsgs() callback for message processing customization
 
typedef int(* nl_recvmsg_err_cb_t) (struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
 nl_recvmsgs() callback for error message processing customization
 

Callback Handle Management

struct nl_cb * nl_cb_alloc (enum nl_cb_kind kind)
 Allocate a new callback handle.
 
struct nl_cb * nl_cb_clone (struct nl_cb *orig)
 Clone an existing callback handle.
 
struct nl_cb * nl_cb_get (struct nl_cb *cb)
 
void nl_cb_put (struct nl_cb *cb)
 

Callback Setup

int nl_cb_set (struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
 Set up a callback.
 
int nl_cb_set_all (struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
 Set up a all callbacks.
 
int nl_cb_err (struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
 Set up an error callback.
 

Overwriting

void nl_cb_overwrite_recvmsgs (struct nl_cb *cb, int(*func)(struct nl_handle *, struct nl_cb *))
 Overwrite internal calls to nl_recvmsgs()
 
void nl_cb_overwrite_recv (struct nl_cb *cb, int(*func)(struct nl_handle *, struct sockaddr_nl *, unsigned char **, struct ucred **))
 Overwrite internal calls to nl_recv()
 
void nl_cb_overwrite_send (struct nl_cb *cb, int(*func)(struct nl_handle *, struct nl_msg *))
 Overwrite internal calls to nl_send()
 

Detailed Description

All callbacks are packed together in struct nl_cb which is then attached to a netlink socket or passed on to the respective functions directly.

Callbacks can control the flow of the underlying layer by returning the appropriate error codes:

Action ID | Description
-----------------+-------------------------------------------------------
NL_OK | Proceed with whatever comes next.
NL_SKIP | Skip message currently being processed and continue
| with next message.
NL_STOP | Stop parsing and discard all remaining messages in
| this set of messages.
@ NL_OK
Proceed with wathever would come next.
Definition: handlers.h:61

All callbacks are optional and a default action is performed if no application specific implementation is provided:

Callback ID | Default Return Value
------------------+----------------------
|
Error Callback | NL_STOP
@ NL_STOP
Stop parsing altogether and discard remaining messages.
Definition: handlers.h:65
@ NL_SKIP
Skip this message.
Definition: handlers.h:63
@ NL_CB_SKIPPED
Message wants to be skipped.
Definition: handlers.h:102
@ NL_CB_FINISH
Last message in a series of multi part messages received.
Definition: handlers.h:98
@ NL_CB_MSG_OUT
Called for every message sent out except for nl_sendto()
Definition: handlers.h:108
@ NL_CB_MSG_IN
Called for every message received.
Definition: handlers.h:106
@ NL_CB_OVERRUN
Report received that data was lost.
Definition: handlers.h:100
@ NL_CB_VALID
Message is valid.
Definition: handlers.h:96
@ NL_CB_ACK
Message is an acknowledge.
Definition: handlers.h:104
@ NL_CB_SEQ_CHECK
Called instead of internal sequence number checking.
Definition: handlers.h:112
@ NL_CB_SEND_ACK
Sending of an acknowledge message has been requested.
Definition: handlers.h:114
@ NL_CB_INVALID
Message is malformed and invalid.
Definition: handlers.h:110

In order to simplify typical usages of the library, different sets of default callback implementations exist:

NL_CB_DEFAULT: No additional actions
NL_CB_VERBOSE: Automatically print warning and error messages to a file
descriptor as appropriate. This is useful for CLI based
applications.
NL_CB_DEBUG: Print informal debugging information for each message
received. This will result in every message beint sent or
received to be printed to the screen in a decoded,
human-readable format.
@ NL_CB_DEFAULT
Default handlers (quiet)
Definition: handlers.h:78
@ NL_CB_VERBOSE
Verbose default handlers (error messages printed)
Definition: handlers.h:80
1) Setting up a callback set
// Allocate a callback set and initialize it to the verbose default set
struct nl_cb *cb = nl_cb_alloc(NL_CB_VERBOSE);
// Modify the set to call my_func() for all valid messages
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL);
// Set the error message handler to the verbose default implementation
// and direct it to print all errors to the given file descriptor.
FILE *file = fopen(...);
nl_cb_err(cb, NL_CB_VERBOSE, NULL, file);
int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Set up a callback.
Definition: handlers.c:338
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
Definition: handlers.c:256
int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, nl_recvmsg_err_cb_t func, void *arg)
Set up an error callback.
Definition: handlers.c:388
@ NL_CB_CUSTOM
Customized handler specified by the user.
Definition: handlers.h:84

Typedef Documentation

◆ nl_recvmsg_msg_cb_t

typedef int(* nl_recvmsg_msg_cb_t) (struct nl_msg *msg, void *arg)
Parameters
msgnetlink message being processed
argargument passwd on through caller

Definition at line 41 of file handlers.h.

◆ nl_recvmsg_err_cb_t

typedef int(* nl_recvmsg_err_cb_t) (struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
Parameters
nlanetlink address of the peer
nlerrnetlink error message being processed
argargument passed on through caller

Definition at line 50 of file handlers.h.

Enumeration Type Documentation

◆ nl_cb_action

Enumerator
NL_OK 

Proceed with wathever would come next.

NL_SKIP 

Skip this message.

NL_STOP 

Stop parsing altogether and discard remaining messages.

Definition at line 59 of file handlers.h.

59 {
60 /** Proceed with wathever would come next */
61 NL_OK,
62 /** Skip this message */
63 NL_SKIP,
64 /** Stop parsing altogether and discard remaining messages */
65 NL_STOP,
66};

◆ nl_cb_kind

enum nl_cb_kind
Enumerator
NL_CB_DEFAULT 

Default handlers (quiet)

NL_CB_VERBOSE 

Verbose default handlers (error messages printed)

NL_CB_DEBUG 

Debug handlers for debugging.

NL_CB_CUSTOM 

Customized handler specified by the user.

Definition at line 76 of file handlers.h.

76 {
77 /** Default handlers (quiet) */
79 /** Verbose default handlers (error messages printed) */
81 /** Debug handlers for debugging */
83 /** Customized handler specified by the user */
85 __NL_CB_KIND_MAX,
86};
@ NL_CB_DEBUG
Debug handlers for debugging.
Definition: handlers.h:82

◆ nl_cb_type

enum nl_cb_type
Enumerator
NL_CB_VALID 

Message is valid.

NL_CB_FINISH 

Last message in a series of multi part messages received.

NL_CB_OVERRUN 

Report received that data was lost.

NL_CB_SKIPPED 

Message wants to be skipped.

NL_CB_ACK 

Message is an acknowledge.

NL_CB_MSG_IN 

Called for every message received.

NL_CB_MSG_OUT 

Called for every message sent out except for nl_sendto()

NL_CB_INVALID 

Message is malformed and invalid.

NL_CB_SEQ_CHECK 

Called instead of internal sequence number checking.

NL_CB_SEND_ACK 

Sending of an acknowledge message has been requested.

Definition at line 94 of file handlers.h.

94 {
95 /** Message is valid */
97 /** Last message in a series of multi part messages received */
99 /** Report received that data was lost */
101 /** Message wants to be skipped */
103 /** Message is an acknowledge */
104 NL_CB_ACK,
105 /** Called for every message received */
107 /** Called for every message sent out except for nl_sendto() */
109 /** Message is malformed and invalid */
111 /** Called instead of internal sequence number checking */
113 /** Sending of an acknowledge message has been requested */
115 __NL_CB_TYPE_MAX,
116};

Function Documentation

◆ nl_cb_alloc()

struct nl_cb * nl_cb_alloc ( enum nl_cb_kind  kind)
Parameters
kindcallback kind to be used for initialization
Returns
Newly allocated callback handle or NULL

Definition at line 256 of file handlers.c.

257{
258 int i;
259 struct nl_cb *cb;
260
261 if (kind < 0 || kind > NL_CB_KIND_MAX)
262 return NULL;
263
264 cb = calloc(1, sizeof(*cb));
265 if (!cb) {
266 nl_errno(ENOMEM);
267 return NULL;
268 }
269
270 cb->cb_refcnt = 1;
271
272 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
273 nl_cb_set(cb, i, kind, NULL, NULL);
274
275 nl_cb_err(cb, kind, NULL, NULL);
276
277 return cb;
278}

References nl_cb_err(), and nl_cb_set().

Referenced by nl_cb_clone(), and nl_handle_alloc().

◆ nl_cb_clone()

struct nl_cb * nl_cb_clone ( struct nl_cb *  orig)
Parameters
origoriginal callback handle
Returns
Newly allocated callback handle being a duplicate of orig or NULL

Definition at line 286 of file handlers.c.

287{
288 struct nl_cb *cb;
289
291 if (!cb)
292 return NULL;
293
294 memcpy(cb, orig, sizeof(*orig));
295 cb->cb_refcnt = 1;
296
297 return cb;
298}

References nl_cb_alloc(), and NL_CB_DEFAULT.

Referenced by nl_wait_for_ack().

◆ nl_cb_get()

struct nl_cb * nl_cb_get ( struct nl_cb *  cb)

Definition at line 300 of file handlers.c.

301{
302 cb->cb_refcnt++;
303
304 return cb;
305}

◆ nl_cb_put()

void nl_cb_put ( struct nl_cb *  cb)

Definition at line 307 of file handlers.c.

308{
309 if (!cb)
310 return;
311
312 cb->cb_refcnt--;
313
314 if (cb->cb_refcnt < 0)
315 BUG();
316
317 if (cb->cb_refcnt <= 0)
318 free(cb);
319}

◆ nl_cb_set()

int nl_cb_set ( struct nl_cb *  cb,
enum nl_cb_type  type,
enum nl_cb_kind  kind,
nl_recvmsg_msg_cb_t  func,
void *  arg 
)
Parameters
cbcallback set
typecallback to modify
kindkind of implementation
funccallback function (NL_CB_CUSTOM)
argargument passed to callback
Returns
0 on success or a negative error code

Definition at line 338 of file handlers.c.

340{
341 if (type < 0 || type > NL_CB_TYPE_MAX)
342 return nl_error(ERANGE, "Callback type out of range");
343
344 if (kind < 0 || kind > NL_CB_KIND_MAX)
345 return nl_error(ERANGE, "Callback kind out of range");
346
347 if (kind == NL_CB_CUSTOM) {
348 cb->cb_set[type] = func;
349 cb->cb_args[type] = arg;
350 } else {
351 cb->cb_set[type] = cb_def[type][kind];
352 cb->cb_args[type] = arg;
353 }
354
355 return 0;
356}

References NL_CB_CUSTOM.

Referenced by nl_cb_alloc(), nl_cb_set_all(), nl_disable_sequence_check(), nl_socket_modify_cb(), and nl_wait_for_ack().

◆ nl_cb_set_all()

int nl_cb_set_all ( struct nl_cb *  cb,
enum nl_cb_kind  kind,
nl_recvmsg_msg_cb_t  func,
void *  arg 
)
Parameters
cbcallback set
kindkind of callback
funccallback function
argargument to be passwd to callback function
Returns
0 on success or a negative error code

Definition at line 367 of file handlers.c.

369{
370 int i, err;
371
372 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
373 err = nl_cb_set(cb, i, kind, func, arg);
374 if (err < 0)
375 return err;
376 }
377
378 return 0;
379}

References nl_cb_set().

◆ nl_cb_err()

int nl_cb_err ( struct nl_cb *  cb,
enum nl_cb_kind  kind,
nl_recvmsg_err_cb_t  func,
void *  arg 
)
Parameters
cbcallback set
kindkind of callback
funccallback function
argargument to be passed to callback function

Definition at line 388 of file handlers.c.

390{
391 if (kind < 0 || kind > NL_CB_KIND_MAX)
392 return nl_error(ERANGE, "Callback kind out of range");
393
394 if (kind == NL_CB_CUSTOM) {
395 cb->cb_err = func;
396 cb->cb_err_arg = arg;
397 } else {
398 cb->cb_err = cb_err_def[kind];
399 cb->cb_err_arg = arg;
400 }
401
402 return 0;
403}

References NL_CB_CUSTOM.

Referenced by nl_cb_alloc().

◆ nl_cb_overwrite_recvmsgs()

void nl_cb_overwrite_recvmsgs ( struct nl_cb *  cb,
int(*)(struct nl_handle *, struct nl_cb *)  func 
)
Parameters
cbcallback set
funcreplacement callback for nl_recvmsgs()

Definition at line 417 of file handlers.c.

419{
420 cb->cb_recvmsgs_ow = func;
421}

◆ nl_cb_overwrite_recv()

void nl_cb_overwrite_recv ( struct nl_cb *  cb,
int(*)(struct nl_handle *, struct sockaddr_nl *, unsigned char **, struct ucred **)  func 
)
Parameters
cbcallback set
funcreplacement callback for nl_recv()

Definition at line 428 of file handlers.c.

431{
432 cb->cb_recv_ow = func;
433}

◆ nl_cb_overwrite_send()

void nl_cb_overwrite_send ( struct nl_cb *  cb,
int(*)(struct nl_handle *, struct nl_msg *)  func 
)
Parameters
cbcallback set
funcreplacement callback for nl_send()

Definition at line 440 of file handlers.c.

442{
443 cb->cb_send_ow = func;
444}