83#include <netlink-local.h>
84#include <netlink/netlink.h>
85#include <netlink/utils.h>
86#include <netlink/msg.h>
87#include <netlink/handlers.h>
89static void print_header_content(FILE *ofd,
struct nlmsghdr *n)
94 fprintf(ofd,
"type=%s length=%u flags=<%s> sequence-nr=%u pid=%u",
95 nl_nlmsgtype2str(n->
nlmsg_type, type,
sizeof(type)),
100static int nl_valid_handler_verbose(
struct nl_msg *msg,
void *arg)
102 FILE *ofd = arg ? arg : stdout;
104 fprintf(ofd,
"-- Warning: unhandled valid message: ");
105 print_header_content(ofd,
nlmsg_hdr(msg));
111static int nl_invalid_handler_verbose(
struct nl_msg *msg,
void *arg)
113 FILE *ofd = arg ? arg : stderr;
115 fprintf(ofd,
"-- Error: Invalid message: ");
116 print_header_content(ofd,
nlmsg_hdr(msg));
122static int nl_overrun_handler_verbose(
struct nl_msg *msg,
void *arg)
124 FILE *ofd = arg ? arg : stderr;
126 fprintf(ofd,
"-- Error: Netlink Overrun: ");
127 print_header_content(ofd,
nlmsg_hdr(msg));
133static int nl_error_handler_verbose(
struct sockaddr_nl *who,
136 FILE *ofd = arg ? arg : stderr;
139 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
140 strerror_r(-e->
error, buf,
sizeof(buf)));
141 print_header_content(ofd, &e->
msg);
147static int nl_valid_handler_debug(
struct nl_msg *msg,
void *arg)
149 FILE *ofd = arg ? arg : stderr;
151 fprintf(ofd,
"-- Debug: Unhandled Valid message: ");
152 print_header_content(ofd,
nlmsg_hdr(msg));
158static int nl_finish_handler_debug(
struct nl_msg *msg,
void *arg)
160 FILE *ofd = arg ? arg : stderr;
162 fprintf(ofd,
"-- Debug: End of multipart message block: ");
163 print_header_content(ofd,
nlmsg_hdr(msg));
169static int nl_msg_in_handler_debug(
struct nl_msg *msg,
void *arg)
171 FILE *ofd = arg ? arg : stderr;
173 fprintf(ofd,
"-- Debug: Received Message:\n");
179static int nl_msg_out_handler_debug(
struct nl_msg *msg,
void *arg)
181 FILE *ofd = arg ? arg : stderr;
183 fprintf(ofd,
"-- Debug: Sent Message:\n");
189static int nl_skipped_handler_debug(
struct nl_msg *msg,
void *arg)
191 FILE *ofd = arg ? arg : stderr;
193 fprintf(ofd,
"-- Debug: Skipped message: ");
194 print_header_content(ofd,
nlmsg_hdr(msg));
200static int nl_ack_handler_debug(
struct nl_msg *msg,
void *arg)
202 FILE *ofd = arg ? arg : stderr;
204 fprintf(ofd,
"-- Debug: ACK: ");
205 print_header_content(ofd,
nlmsg_hdr(msg));
261 if (kind < 0 || kind > NL_CB_KIND_MAX)
264 cb = calloc(1,
sizeof(*cb));
272 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
294 memcpy(cb, orig,
sizeof(*orig));
300struct nl_cb *nl_cb_get(
struct nl_cb *cb)
307void nl_cb_put(
struct nl_cb *cb)
314 if (cb->cb_refcnt < 0)
317 if (cb->cb_refcnt <= 0)
341 if (type < 0 || type > NL_CB_TYPE_MAX)
342 return nl_error(ERANGE,
"Callback type out of range");
344 if (kind < 0 || kind > NL_CB_KIND_MAX)
345 return nl_error(ERANGE,
"Callback kind out of range");
348 cb->cb_set[type] = func;
349 cb->cb_args[type] = arg;
351 cb->cb_set[type] = cb_def[type][kind];
352 cb->cb_args[type] = arg;
372 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
391 if (kind < 0 || kind > NL_CB_KIND_MAX)
392 return nl_error(ERANGE,
"Callback kind out of range");
396 cb->cb_err_arg = arg;
398 cb->cb_err = cb_err_def[kind];
399 cb->cb_err_arg = arg;
418 int (*func)(
struct nl_handle *,
struct nl_cb *))
420 cb->cb_recvmsgs_ow = func;
429 int (*func)(
struct nl_handle *,
struct sockaddr_nl *,
430 unsigned char **,
struct ucred **))
432 cb->cb_recv_ow = func;
441 int (*func)(
struct nl_handle *,
struct nl_msg *))
443 cb->cb_send_ow = func;
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_handle *, 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.
void nl_cb_overwrite_send(struct nl_cb *cb, int(*func)(struct nl_handle *, struct nl_msg *))
Overwrite internal calls to nl_send()
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.
void nl_cb_overwrite_recvmsgs(struct nl_cb *cb, int(*func)(struct nl_handle *, struct nl_cb *))
Overwrite internal calls to nl_recvmsgs()
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new callback handle.
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 wathever 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 acknowledge.
@ 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.
struct nlmsghdr msg
Original netlink message causing the error.
int error
Error code (errno number)
uint32_t nlmsg_pid
Netlink PID of the proccess sending the message.
uint32_t nlmsg_len
Length of message including header.
uint16_t nlmsg_type
Message type (content type)
uint16_t nlmsg_flags
Message flags.
uint32_t nlmsg_seq
Sequence number.