2 * lib/handlers.c default netlink message handlers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cb Callbacks/Customization
17 * @par 1) Setting up a callback set
19 * // Allocate a callback set and initialize it to the verbose default set
20 * struct nl_cb *cb = nl_cb_alloc(NL_CB_VERBOSE);
22 * // Modify the set to call my_func() for all valid messages
23 * nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, my_func, NULL);
25 * // Set the error message handler to the verbose default implementation
26 * // and direct it to print all errors to the given file descriptor.
27 * FILE *file = fopen(...);
28 * nl_cb_err(cb, NL_CB_VERBOSE, NULL, file);
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>
40 * @name Callback Handle Management
45 * Allocate a new callback handle
46 * @arg kind callback kind to be used for initialization
47 * @return Newly allocated callback handle or NULL
49 struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
54 if (kind < 0 || kind > NL_CB_KIND_MAX)
57 cb = calloc(1, sizeof(*cb));
63 for (i = 0; i <= NL_CB_TYPE_MAX; i++)
64 nl_cb_set(cb, i, kind, NULL, NULL);
66 nl_cb_err(cb, kind, NULL, NULL);
72 * Clone an existing callback handle
73 * @arg orig original callback handle
74 * @return Newly allocated callback handle being a duplicate of
77 struct nl_cb *nl_cb_clone(struct nl_cb *orig)
81 cb = nl_cb_alloc(NL_CB_DEFAULT);
85 memcpy(cb, orig, sizeof(*orig));
91 void nl_cb_put(struct nl_cb *cb)
98 if (cb->cb_refcnt < 0)
101 if (cb->cb_refcnt <= 0)
108 * @name Callback Setup
114 * @arg cb callback set
115 * @arg type callback to modify
116 * @arg kind kind of implementation
117 * @arg func callback function (NL_CB_CUSTOM)
118 * @arg arg argument passed to callback
120 * @return 0 on success or a negative error code
122 int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind,
123 nl_recvmsg_msg_cb_t func, void *arg)
125 if (type < 0 || type > NL_CB_TYPE_MAX)
128 if (kind < 0 || kind > NL_CB_KIND_MAX)
131 if (kind == NL_CB_CUSTOM) {
132 cb->cb_set[type] = func;
133 cb->cb_args[type] = arg;
140 * Set up an error callback
141 * @arg cb callback set
142 * @arg kind kind of callback
143 * @arg func callback function
144 * @arg arg argument to be passed to callback function
146 int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind,
147 nl_recvmsg_err_cb_t func, void *arg)
149 if (kind < 0 || kind > NL_CB_KIND_MAX)
152 if (kind == NL_CB_CUSTOM) {
154 cb->cb_err_arg = arg;