28 #include <netlink-private/netlink.h>
29 #include <netlink/netlink.h>
30 #include <netlink/utils.h>
31 #include <netlink/msg.h>
32 #include <netlink/handlers.h>
34 static void print_header_content(FILE *ofd,
struct nlmsghdr *n)
39 fprintf(ofd,
"type=%s length=%u flags=<%s> sequence-nr=%u pid=%u",
40 nl_nlmsgtype2str(n->nlmsg_type, type,
sizeof(type)),
41 n->nlmsg_len, nl_nlmsg_flags2str(n->nlmsg_flags, flags,
42 sizeof(flags)), n->nlmsg_seq, n->nlmsg_pid);
45 static int nl_valid_handler_verbose(
struct nl_msg *msg,
void *arg)
47 FILE *ofd = arg ? arg : stdout;
49 fprintf(ofd,
"-- Warning: unhandled valid message: ");
50 print_header_content(ofd,
nlmsg_hdr(msg));
56 static int nl_invalid_handler_verbose(
struct nl_msg *msg,
void *arg)
58 FILE *ofd = arg ? arg : stderr;
60 fprintf(ofd,
"-- Error: Invalid message: ");
61 print_header_content(ofd,
nlmsg_hdr(msg));
67 static int nl_overrun_handler_verbose(
struct nl_msg *msg,
void *arg)
69 FILE *ofd = arg ? arg : stderr;
71 fprintf(ofd,
"-- Error: Netlink Overrun: ");
72 print_header_content(ofd,
nlmsg_hdr(msg));
78 static int nl_error_handler_verbose(
struct sockaddr_nl *who,
79 struct nlmsgerr *e,
void *arg)
81 FILE *ofd = arg ? arg : stderr;
84 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
85 strerror_r(-e->error, buf,
sizeof(buf)));
86 print_header_content(ofd, &e->msg);
89 return -nl_syserr2nlerr(e->error);
92 static int nl_valid_handler_debug(
struct nl_msg *msg,
void *arg)
94 FILE *ofd = arg ? arg : stderr;
96 fprintf(ofd,
"-- Debug: Unhandled Valid message: ");
97 print_header_content(ofd,
nlmsg_hdr(msg));
103 static int nl_finish_handler_debug(
struct nl_msg *msg,
void *arg)
105 FILE *ofd = arg ? arg : stderr;
107 fprintf(ofd,
"-- Debug: End of multipart message block: ");
108 print_header_content(ofd,
nlmsg_hdr(msg));
114 static int nl_msg_in_handler_debug(
struct nl_msg *msg,
void *arg)
116 FILE *ofd = arg ? arg : stderr;
118 fprintf(ofd,
"-- Debug: Received Message:\n");
124 static int nl_msg_out_handler_debug(
struct nl_msg *msg,
void *arg)
126 FILE *ofd = arg ? arg : stderr;
128 fprintf(ofd,
"-- Debug: Sent Message:\n");
134 static int nl_skipped_handler_debug(
struct nl_msg *msg,
void *arg)
136 FILE *ofd = arg ? arg : stderr;
138 fprintf(ofd,
"-- Debug: Skipped message: ");
139 print_header_content(ofd,
nlmsg_hdr(msg));
145 static int nl_ack_handler_debug(
struct nl_msg *msg,
void *arg)
147 FILE *ofd = arg ? arg : stderr;
149 fprintf(ofd,
"-- Debug: ACK: ");
150 print_header_content(ofd,
nlmsg_hdr(msg));
206 if (kind < 0 || kind > NL_CB_KIND_MAX)
209 cb = calloc(1,
sizeof(*cb));
214 cb->cb_active = NL_CB_TYPE_MAX + 1;
216 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
238 memcpy(cb, orig,
sizeof(*orig));
244 struct nl_cb *nl_cb_get(
struct nl_cb *cb)
251 void nl_cb_put(
struct nl_cb *cb)
258 if (cb->cb_refcnt < 0)
261 if (cb->cb_refcnt <= 0)
273 return cb->cb_active;
296 if (type < 0 || type > NL_CB_TYPE_MAX)
299 if (kind < 0 || kind > NL_CB_KIND_MAX)
303 cb->cb_set[type] = func;
304 cb->cb_args[type] = arg;
306 cb->cb_set[type] = cb_def[type][kind];
307 cb->cb_args[type] = arg;
327 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
346 if (kind < 0 || kind > NL_CB_KIND_MAX)
351 cb->cb_err_arg = arg;
353 cb->cb_err = cb_err_def[kind];
354 cb->cb_err_arg = arg;
373 int (*func)(
struct nl_sock *,
struct nl_cb *))
375 cb->cb_recvmsgs_ow = func;
384 int (*func)(
struct nl_sock *,
struct sockaddr_nl *,
385 unsigned char **,
struct ucred **))
387 cb->cb_recv_ow = func;
396 int (*func)(
struct nl_sock *,
struct nl_msg *))
398 cb->cb_send_ow = func;