28 #include <netlink-local.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;
83 fprintf(ofd,
"-- Error received: %s\n-- Original message: ",
85 print_header_content(ofd, &e->msg);
88 return -nl_syserr2nlerr(e->error);
91 static int nl_valid_handler_debug(
struct nl_msg *msg,
void *arg)
93 FILE *ofd = arg ? arg : stderr;
95 fprintf(ofd,
"-- Debug: Unhandled Valid message: ");
96 print_header_content(ofd,
nlmsg_hdr(msg));
102 static int nl_finish_handler_debug(
struct nl_msg *msg,
void *arg)
104 FILE *ofd = arg ? arg : stderr;
106 fprintf(ofd,
"-- Debug: End of multipart message block: ");
107 print_header_content(ofd,
nlmsg_hdr(msg));
113 static int nl_msg_in_handler_debug(
struct nl_msg *msg,
void *arg)
115 FILE *ofd = arg ? arg : stderr;
117 fprintf(ofd,
"-- Debug: Received Message:\n");
123 static int nl_msg_out_handler_debug(
struct nl_msg *msg,
void *arg)
125 FILE *ofd = arg ? arg : stderr;
127 fprintf(ofd,
"-- Debug: Sent Message:\n");
133 static int nl_skipped_handler_debug(
struct nl_msg *msg,
void *arg)
135 FILE *ofd = arg ? arg : stderr;
137 fprintf(ofd,
"-- Debug: Skipped message: ");
138 print_header_content(ofd,
nlmsg_hdr(msg));
144 static int nl_ack_handler_debug(
struct nl_msg *msg,
void *arg)
146 FILE *ofd = arg ? arg : stderr;
148 fprintf(ofd,
"-- Debug: ACK: ");
149 print_header_content(ofd,
nlmsg_hdr(msg));
205 if (kind < 0 || kind > NL_CB_KIND_MAX)
208 cb = calloc(1,
sizeof(*cb));
214 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
236 memcpy(cb, orig,
sizeof(*orig));
242 struct nl_cb *nl_cb_get(
struct nl_cb *cb)
249 void nl_cb_put(
struct nl_cb *cb)
256 if (cb->cb_refcnt < 0)
259 if (cb->cb_refcnt <= 0)
283 if (type < 0 || type > NL_CB_TYPE_MAX)
286 if (kind < 0 || kind > NL_CB_KIND_MAX)
290 cb->cb_set[type] = func;
291 cb->cb_args[type] = arg;
293 cb->cb_set[type] = cb_def[type][kind];
294 cb->cb_args[type] = arg;
314 for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
333 if (kind < 0 || kind > NL_CB_KIND_MAX)
338 cb->cb_err_arg = arg;
340 cb->cb_err = cb_err_def[kind];
341 cb->cb_err_arg = arg;
360 int (*func)(
struct nl_sock *,
struct nl_cb *))
362 cb->cb_recvmsgs_ow = func;
371 int (*func)(
struct nl_sock *,
struct sockaddr_nl *,
372 unsigned char **,
struct ucred **))
374 cb->cb_recv_ow = func;
383 int (*func)(
struct nl_sock *,
struct nl_msg *))
385 cb->cb_send_ow = func;