blob: 804976dda37cd839f9f4af4ee943cfef8404f428 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2012 Daniel Drown <dan-android@drown.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * netlink_callbacks.c - generic callbacks for netlink responses
17 */
Daniel Drowna45056e2012-03-23 10:42:54 -050018#include <net/if.h>
junyulaic4e591a2018-11-26 22:36:10 +090019#include <netinet/in.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050020
21#include <linux/rtnetlink.h>
22#include <netlink/handlers.h>
23#include <netlink/msg.h>
24
25/* function: ack_handler
26 * generic netlink callback for ack messages
27 * msg - netlink message
28 * data - pointer to an int, stores the success code
29 */
Lorenzo Colitti56ec1612014-03-10 16:33:22 +090030static int ack_handler(__attribute__((unused)) struct nl_msg *msg, void *data) {
Daniel Drowna45056e2012-03-23 10:42:54 -050031 int *retval = data;
junyulaic4e591a2018-11-26 22:36:10 +090032 *retval = 0;
Daniel Drowna45056e2012-03-23 10:42:54 -050033 return NL_OK;
34}
35
36/* function: error_handler
37 * generic netlink callback for error messages
38 * nla - error source
39 * err - netlink error message
40 * arg - pointer to an int, stores the error code
41 */
junyulaic4e591a2018-11-26 22:36:10 +090042static int error_handler(__attribute__((unused)) struct sockaddr_nl *nla, struct nlmsgerr *err,
43 void *arg) {
Daniel Drowna45056e2012-03-23 10:42:54 -050044 int *retval = arg;
junyulaic4e591a2018-11-26 22:36:10 +090045 if (err->error < 0) {
Daniel Drowna45056e2012-03-23 10:42:54 -050046 *retval = err->error;
47 } else {
junyulaic4e591a2018-11-26 22:36:10 +090048 *retval = 0; // NLMSG_ERROR used as reply type on no error
Daniel Drowna45056e2012-03-23 10:42:54 -050049 }
50 return NL_OK;
51}
52
53/* function: alloc_ack_callbacks
junyulaic4e591a2018-11-26 22:36:10 +090054 * allocates a set of netlink callbacks. returns NULL on failure. callbacks will modify retval
55 * with <0 meaning failure retval - shared state between caller and callback functions
Daniel Drowna45056e2012-03-23 10:42:54 -050056 */
57struct nl_cb *alloc_ack_callbacks(int *retval) {
58 struct nl_cb *callbacks;
59
60 callbacks = nl_cb_alloc(NL_CB_DEFAULT);
junyulaic4e591a2018-11-26 22:36:10 +090061 if (!callbacks) {
Daniel Drowna45056e2012-03-23 10:42:54 -050062 return NULL;
63 }
64 nl_cb_set(callbacks, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, retval);
65 nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, retval);
66 return callbacks;
67}