• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
18 #include <netinet/in.h>
19 #include <net/if.h>
20 
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  */
ack_handler(struct nl_msg * msg,void * data)30 static int ack_handler(__attribute__((unused)) struct nl_msg *msg, void *data) {
31   int *retval = data;
32   *retval = 0;
33   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  */
error_handler(struct sockaddr_nl * nla,struct nlmsgerr * err,void * arg)42 static int error_handler(__attribute__((unused)) struct sockaddr_nl *nla,
43                          struct nlmsgerr *err, void *arg) {
44   int *retval = arg;
45   if(err->error < 0) {
46     *retval = err->error;
47   } else {
48     *retval = 0; // NLMSG_ERROR used as reply type on no error
49   }
50   return NL_OK;
51 }
52 
53 /* function: alloc_ack_callbacks
54  * allocates a set of netlink callbacks.  returns NULL on failure.  callbacks will modify retval with <0 meaning failure
55  * retval - shared state between caller and callback functions
56  */
alloc_ack_callbacks(int * retval)57 struct nl_cb *alloc_ack_callbacks(int *retval) {
58   struct nl_cb *callbacks;
59 
60   callbacks = nl_cb_alloc(NL_CB_DEFAULT);
61   if(!callbacks) {
62     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 }
68