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;