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>
89 static 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)),
100 static 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));
111 static 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));
122 static 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));
133 static int nl_error_handler_verbose(
struct sockaddr_nl *who,
136 FILE *ofd = arg ? arg : stderr;
138 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
139 strerror(-e->
error));
140 print_header_content(ofd, &e->
msg);
146 static int nl_valid_handler_debug(
struct nl_msg *msg,
void *arg)
148 FILE *ofd = arg ? arg : stderr;
150 fprintf(ofd,
"-- Debug: Unhandled Valid message: ");
151 print_header_content(ofd,
nlmsg_hdr(msg));
157 static int nl_finish_handler_debug(
struct nl_msg *msg,
void *arg)
159 FILE *ofd = arg ? arg : stderr;
161 fprintf(ofd,
"-- Debug: End of multipart message block: ");
162 print_header_content(ofd,
nlmsg_hdr(msg));
168 static int nl_msg_in_handler_debug(
struct nl_msg *msg,
void *arg)
170 FILE *ofd = arg ? arg : stderr;
172 fprintf(ofd,
"-- Debug: Received Message:\n");
178 static int nl_msg_out_handler_debug(
struct nl_msg *msg,
void *arg)
180 FILE *ofd = arg ? arg : stderr;
182 fprintf(ofd,
"-- Debug: Sent Message:\n");
188 static int nl_skipped_handler_debug(
struct nl_msg *msg,
void *arg)
190 FILE *ofd = arg ? arg : stderr;
192 fprintf(ofd,
"-- Debug: Skipped message: ");
193 print_header_content(ofd,
nlmsg_hdr(msg));
199 static int nl_ack_handler_debug(
struct nl_msg *msg,
void *arg)
201 FILE *ofd = arg ? arg : stderr;
203 fprintf(ofd,
"-- Debug: ACK: ");
204 print_header_content(ofd,
nlmsg_hdr(msg));
260 if (kind < 0 || kind > NL_CB_KIND_MAX)
263 cb = calloc(1,
sizeof(*cb));
271 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
293 memcpy(cb, orig,
sizeof(*orig));
299 struct nl_cb *nl_cb_get(
struct nl_cb *cb)
306 void nl_cb_put(
struct nl_cb *cb)
313 if (cb->cb_refcnt < 0)
316 if (cb->cb_refcnt <= 0)
340 if (type < 0 || type > NL_CB_TYPE_MAX)
341 return nl_error(ERANGE,
"Callback type out of range");
343 if (kind < 0 || kind > NL_CB_KIND_MAX)
344 return nl_error(ERANGE,
"Callback kind out of range");
347 cb->cb_set[type] = func;
348 cb->cb_args[type] = arg;
350 cb->cb_set[type] = cb_def[type][kind];
351 cb->cb_args[type] = arg;
371 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
390 if (kind < 0 || kind > NL_CB_KIND_MAX)
391 return nl_error(ERANGE,
"Callback kind out of range");
395 cb->cb_err_arg = arg;
397 cb->cb_err = cb_err_def[kind];
398 cb->cb_err_arg = arg;
417 int (*func)(
struct nl_handle *,
struct nl_cb *))
419 cb->cb_recvmsgs_ow = func;
428 int (*func)(
struct nl_handle *,
struct sockaddr_nl *,
429 unsigned char **,
struct ucred **))
431 cb->cb_recv_ow = func;
440 int (*func)(
struct nl_handle *,
struct nl_msg *))
442 cb->cb_send_ow = func;
Report received that data was lost.
Called for every message sent out except for nl_sendto()
Message is an acknowledge.
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_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
nl_recvmsgs() callback for error message processing customization
uint16_t nlmsg_type
Message type (content type)
Customized handler specified by the user.
uint32_t nlmsg_seq
Sequence number.
Message wants to be skipped.
int error
Error code (errno number)
Stop parsing altogether and discard remaining messages.
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()
Called for every message received.
uint32_t nlmsg_len
Length of message including header.
nl_cb_kind
Callback kinds.
struct nl_cb * nl_cb_clone(struct nl_cb *orig)
Clone an existing callback handle.
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_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
void nl_cb_overwrite_send(struct nl_cb *cb, int(*func)(struct nl_handle *, struct nl_msg *))
Overwrite internal calls to nl_send()
uint32_t nlmsg_pid
Netlink PID of the proccess sending the message.
Message is malformed and invalid.
void nl_msg_dump(struct nl_msg *msg, FILE *ofd)
Dump message in human readable format to file descriptor.
Last message in a series of multi part messages received.
struct nlmsghdr * nlmsg_hdr(struct nl_msg *n)
Return actual netlink message.
Debug handlers for debugging.
uint16_t nlmsg_flags
Message flags.
struct nlmsghdr msg
Original netlink message causing the error.
Proceed with wathever would come next.
nl_cb_type
Callback types.
struct nl_cb * nl_cb_alloc(enum nl_cb_kind kind)
Allocate a new 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()
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.
Verbose default handlers (error messages printed)