Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame^] | 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 | */ |
| 30 | static int ack_handler(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 | */ |
| 42 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) { |
| 43 | int *retval = arg; |
| 44 | if(err->error < 0) { |
| 45 | *retval = err->error; |
| 46 | } else { |
| 47 | *retval = 0; // NLMSG_ERROR used as reply type on no error |
| 48 | } |
| 49 | return NL_OK; |
| 50 | } |
| 51 | |
| 52 | /* function: alloc_ack_callbacks |
| 53 | * allocates a set of netlink callbacks. returns NULL on failure. callbacks will modify retval with <0 meaning failure |
| 54 | * retval - shared state between caller and callback functions |
| 55 | */ |
| 56 | struct nl_cb *alloc_ack_callbacks(int *retval) { |
| 57 | struct nl_cb *callbacks; |
| 58 | |
| 59 | callbacks = nl_cb_alloc(NL_CB_DEFAULT); |
| 60 | if(!callbacks) { |
| 61 | return NULL; |
| 62 | } |
| 63 | nl_cb_set(callbacks, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, retval); |
| 64 | nl_cb_err(callbacks, NL_CB_CUSTOM, error_handler, retval); |
| 65 | return callbacks; |
| 66 | } |