33 #include <netlink-local.h>
34 #include <netlink/netlink.h>
35 #include <netlink/utils.h>
36 #include <netlink/msg.h>
37 #include <netlink/handlers.h>
39 static void print_header_content(FILE *ofd,
struct nlmsghdr *n)
44 fprintf(ofd,
"type=%s length=%u flags=<%s> sequence-nr=%u pid=%u",
45 nl_nlmsgtype2str(n->nlmsg_type, type,
sizeof(type)),
46 n->nlmsg_len, nl_nlmsg_flags2str(n->nlmsg_flags, flags,
47 sizeof(flags)), n->nlmsg_seq, n->nlmsg_pid);
50 static int nl_valid_handler_verbose(
struct nl_msg *msg,
void *arg)
52 FILE *ofd = arg ? arg : stdout;
54 fprintf(ofd,
"-- Warning: unhandled valid message: ");
55 print_header_content(ofd,
nlmsg_hdr(msg));
61 static int nl_invalid_handler_verbose(
struct nl_msg *msg,
void *arg)
63 FILE *ofd = arg ? arg : stderr;
65 fprintf(ofd,
"-- Error: Invalid message: ");
66 print_header_content(ofd,
nlmsg_hdr(msg));
72 static int nl_overrun_handler_verbose(
struct nl_msg *msg,
void *arg)
74 FILE *ofd = arg ? arg : stderr;
76 fprintf(ofd,
"-- Error: Netlink Overrun: ");
77 print_header_content(ofd,
nlmsg_hdr(msg));
83 static int nl_error_handler_verbose(
struct sockaddr_nl *who,
84 struct nlmsgerr *e,
void *arg)
86 FILE *ofd = arg ? arg : stderr;
88 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
90 print_header_content(ofd, &e->msg);
93 return -nl_syserr2nlerr(e->error);
96 static int nl_valid_handler_debug(
struct nl_msg *msg,
void *arg)
98 FILE *ofd = arg ? arg : stderr;
100 fprintf(ofd,
"-- Debug: Unhandled Valid message: ");
101 print_header_content(ofd,
nlmsg_hdr(msg));
107 static int nl_finish_handler_debug(
struct nl_msg *msg,
void *arg)
109 FILE *ofd = arg ? arg : stderr;
111 fprintf(ofd,
"-- Debug: End of multipart message block: ");
112 print_header_content(ofd,
nlmsg_hdr(msg));
118 static int nl_msg_in_handler_debug(
struct nl_msg *msg,
void *arg)
120 FILE *ofd = arg ? arg : stderr;
122 fprintf(ofd,
"-- Debug: Received Message:\n");
128 static int nl_msg_out_handler_debug(
struct nl_msg *msg,
void *arg)
130 FILE *ofd = arg ? arg : stderr;
132 fprintf(ofd,
"-- Debug: Sent Message:\n");
138 static int nl_skipped_handler_debug(
struct nl_msg *msg,
void *arg)
140 FILE *ofd = arg ? arg : stderr;
142 fprintf(ofd,
"-- Debug: Skipped message: ");
143 print_header_content(ofd,
nlmsg_hdr(msg));
149 static int nl_ack_handler_debug(
struct nl_msg *msg,
void *arg)
151 FILE *ofd = arg ? arg : stderr;
153 fprintf(ofd,
"-- Debug: ACK: ");
154 print_header_content(ofd,
nlmsg_hdr(msg));
210 if (kind < 0 || kind > NL_CB_KIND_MAX)
213 cb = calloc(1,
sizeof(*cb));
219 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
241 memcpy(cb, orig,
sizeof(*orig));
247 struct nl_cb *nl_cb_get(
struct nl_cb *cb)
254 void nl_cb_put(
struct nl_cb *cb)
261 if (cb->cb_refcnt < 0)
264 if (cb->cb_refcnt <= 0)
288 if (type < 0 || type > NL_CB_TYPE_MAX)
291 if (kind < 0 || kind > NL_CB_KIND_MAX)
295 cb->cb_set[type] = func;
296 cb->cb_args[type] = arg;
298 cb->cb_set[type] = cb_def[type][kind];
299 cb->cb_args[type] = arg;
319 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
338 if (kind < 0 || kind > NL_CB_KIND_MAX)
343 cb->cb_err_arg = arg;
345 cb->cb_err = cb_err_def[kind];
346 cb->cb_err_arg = arg;
365 int (*func)(
struct nl_sock *,
struct nl_cb *))
367 cb->cb_recvmsgs_ow = func;
376 int (*func)(
struct nl_sock *,
struct sockaddr_nl *,
377 unsigned char **,
struct ucred **))
379 cb->cb_recv_ow = func;
388 int (*func)(
struct nl_sock *,
struct nl_msg *))
390 cb->cb_send_ow = func;