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;
139 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
140 strerror_r(-e->
error, buf,
sizeof(buf)));
141 print_header_content(ofd, &e->
msg);
147 static 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));
158 static 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));
169 static 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");
179 static 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");
189 static 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));
200 static 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));
300 struct nl_cb *nl_cb_get(
struct nl_cb *cb)
307 void 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;
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.
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.
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.
int(* nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla, struct nlmsgerr *nlerr, void *arg)
nl_recvmsgs() callback for error message processing customization
Debug handlers for debugging.
int(* nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg)
nl_recvmsgs() callback for message processing customization
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)