22#include <netlink-private/netlink.h>
23#include <netlink-private/utils.h>
24#include <netlink/netlink.h>
25#include <netlink/utils.h>
26#include <netlink/msg.h>
27#include <netlink/handlers.h>
29static void print_header_content(FILE *ofd,
struct nlmsghdr *n)
34 fprintf(ofd,
"type=%s length=%u flags=<%s> sequence-nr=%u pid=%u",
35 nl_nlmsgtype2str(n->nlmsg_type, type,
sizeof(type)),
36 n->nlmsg_len, nl_nlmsg_flags2str(n->nlmsg_flags, flags,
37 sizeof(flags)), n->nlmsg_seq, n->nlmsg_pid);
40static int nl_valid_handler_verbose(
struct nl_msg *msg,
void *arg)
42 FILE *ofd = arg ? arg : stdout;
44 fprintf(ofd,
"-- Warning: unhandled valid message: ");
45 print_header_content(ofd,
nlmsg_hdr(msg));
51static int nl_invalid_handler_verbose(
struct nl_msg *msg,
void *arg)
53 FILE *ofd = arg ? arg : stderr;
55 fprintf(ofd,
"-- Error: Invalid message: ");
56 print_header_content(ofd,
nlmsg_hdr(msg));
62static int nl_overrun_handler_verbose(
struct nl_msg *msg,
void *arg)
64 FILE *ofd = arg ? arg : stderr;
66 fprintf(ofd,
"-- Error: Netlink Overrun: ");
67 print_header_content(ofd,
nlmsg_hdr(msg));
73static int nl_error_handler_verbose(
struct sockaddr_nl *who,
74 struct nlmsgerr *e,
void *arg)
76 FILE *ofd = arg ? arg : stderr;
78 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
79 nl_strerror_l(-e->error));
80 print_header_content(ofd, &e->msg);
83 return -nl_syserr2nlerr(e->error);
86static int nl_valid_handler_debug(
struct nl_msg *msg,
void *arg)
88 FILE *ofd = arg ? arg : stderr;
90 fprintf(ofd,
"-- Debug: Unhandled Valid message: ");
91 print_header_content(ofd,
nlmsg_hdr(msg));
97static int nl_finish_handler_debug(
struct nl_msg *msg,
void *arg)
99 FILE *ofd = arg ? arg : stderr;
101 fprintf(ofd,
"-- Debug: End of multipart message block: ");
102 print_header_content(ofd,
nlmsg_hdr(msg));
108static int nl_msg_in_handler_debug(
struct nl_msg *msg,
void *arg)
110 FILE *ofd = arg ? arg : stderr;
112 fprintf(ofd,
"-- Debug: Received Message:\n");
118static int nl_msg_out_handler_debug(
struct nl_msg *msg,
void *arg)
120 FILE *ofd = arg ? arg : stderr;
122 fprintf(ofd,
"-- Debug: Sent Message:\n");
128static int nl_skipped_handler_debug(
struct nl_msg *msg,
void *arg)
130 FILE *ofd = arg ? arg : stderr;
132 fprintf(ofd,
"-- Debug: Skipped message: ");
133 print_header_content(ofd,
nlmsg_hdr(msg));
139static int nl_ack_handler_debug(
struct nl_msg *msg,
void *arg)
141 FILE *ofd = arg ? arg : stderr;
143 fprintf(ofd,
"-- Debug: ACK: ");
144 print_header_content(ofd,
nlmsg_hdr(msg));
200 if ((
unsigned int) kind > NL_CB_KIND_MAX)
203 cb = calloc(1,
sizeof(*cb));
208 cb->cb_active = NL_CB_TYPE_MAX + 1;
210 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
232 memcpy(cb, orig,
sizeof(*orig));
238struct nl_cb *nl_cb_get(
struct nl_cb *cb)
245void nl_cb_put(
struct nl_cb *cb)
252 if (cb->cb_refcnt < 0)
255 if (cb->cb_refcnt <= 0)
267 return cb->cb_active;
290 if ((
unsigned int) type > NL_CB_TYPE_MAX)
293 if ((
unsigned int) kind > NL_CB_KIND_MAX)
297 cb->cb_set[type] = func;
298 cb->cb_args[type] = arg;
300 cb->cb_set[type] = cb_def[type][kind];
301 cb->cb_args[type] = arg;
321 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
340 if ((
unsigned int) kind > NL_CB_KIND_MAX)
345 cb->cb_err_arg = arg;
347 cb->cb_err = cb_err_def[kind];
348 cb->cb_err_arg = arg;
367 int (*func)(
struct nl_sock *,
struct nl_cb *))
369 cb->cb_recvmsgs_ow = func;
378 int (*func)(
struct nl_sock *,
struct sockaddr_nl *,
379 unsigned char **,
struct ucred **))
381 cb->cb_recv_ow = func;
390 int (*func)(
struct nl_sock *,
struct nl_msg *))
392 cb->cb_send_ow = func;
void nl_cb_overwrite_send(struct nl_cb *cb, int(*func)(struct nl_sock *, struct nl_msg *))
Overwrite internal calls to nl_send()
int(* nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
nl_recvmsgs() callback for error message processing customization
void nl_cb_overwrite_recv(struct nl_cb *cb, int(*func)(struct nl_sock *, struct sockaddr_nl *, unsigned char **, struct ucred **))
Overwrite internal calls to nl_recv()
nl_cb_type
Callback types.
int(* nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
nl_cb_kind
Callback kinds.
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.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
enum nl_cb_type nl_cb_active_type(struct nl_cb *cb)
Obtain type of current active callback.
void nl_cb_overwrite_recvmsgs(struct nl_cb *cb, int(*func)(struct nl_sock *, struct nl_cb *))
Overwrite internal calls to nl_recvmsgs()
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.
@ NL_STOP
Stop parsing altogether and discard remaining messages.
@ NL_OK
Proceed with whatever would come next.
@ NL_SKIP
Skip this message.
@ NL_CB_SKIPPED
Message wants to be skipped.
@ NL_CB_FINISH
Last message in a series of multi part messages received.
@ NL_CB_MSG_OUT
Called for every message sent out except for nl_sendto()
@ NL_CB_MSG_IN
Called for every message received.
@ NL_CB_OVERRUN
Report received that data was lost.
@ NL_CB_VALID
Message is valid.
@ NL_CB_ACK
Message is an acknowledgement.
@ NL_CB_INVALID
Message is malformed and invalid.
@ NL_CB_DEFAULT
Default handlers (quiet)
@ NL_CB_CUSTOM
Customized handler specified by the user.
@ NL_CB_VERBOSE
Verbose default handlers (error messages printed)
@ NL_CB_DEBUG
Debug handlers for debugging.
void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
Dump message in human readable format to file descriptor.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.