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;