blob: 48dcab4a82f8276212b4b18b6c706d91ddb60d1d [file] [log] [blame]
Frank Makered6b39c2011-05-23 21:14:58 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
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
17/* NOTICE: This is a clean room re-implementation of libnl */
18
19#include <malloc.h>
20#include "netlink-types.h"
21#include "netlink/handlers.h"
22
23/* Allocate a new callback handle. */
24struct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
25{
26 struct nl_cb *cb;
27
28 cb = (struct nl_cb *) malloc(sizeof(struct nl_cb));
29 if (cb == NULL)
30 goto fail;
31 memset(cb, 0, sizeof(*cb));
32
33 return nl_cb_get(cb);
34fail:
35 return NULL;
36}
37
38/* Clone an existing callback handle */
39struct nl_cb *nl_cb_clone(struct nl_cb *orig)
40{
41 struct nl_cb *new_cb;
Frank Makered6b39c2011-05-23 21:14:58 -070042
43 new_cb = nl_cb_alloc(NL_CB_DEFAULT);
44 if (new_cb == NULL)
45 goto fail;
46
Dmitry Shmidtd99fe5e2011-11-01 15:24:02 -070047 /* Copy original and set refcount to 1 */
Frank Makered6b39c2011-05-23 21:14:58 -070048 memcpy(new_cb, orig, sizeof(*orig));
Dmitry Shmidtd99fe5e2011-11-01 15:24:02 -070049 new_cb->cb_refcnt = 1;
Frank Makered6b39c2011-05-23 21:14:58 -070050
51 return new_cb;
52fail:
53 return NULL;
54}
55
56/* Set up a callback. */
57int nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, \
58 nl_recvmsg_msg_cb_t func, void *arg)
59{
60 cb->cb_set[type] = func;
61 cb->cb_args[type] = arg;
62 return 0;
63}
64
65
66
67/* Set up an error callback. */
68int nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, \
69 nl_recvmsg_err_cb_t func, void *arg)
70{
71 cb->cb_err = func;
72 cb->cb_err_arg = arg;
73 return 0;
74
75}
76
77struct nl_cb *nl_cb_get(struct nl_cb *cb)
78{
79 cb->cb_refcnt++;
80 return cb;
81}
82
83void nl_cb_put(struct nl_cb *cb)
84{
Dmitry Shmidtd99fe5e2011-11-01 15:24:02 -070085 if (!cb)
86 return;
Frank Makered6b39c2011-05-23 21:14:58 -070087 cb->cb_refcnt--;
88 if (cb->cb_refcnt <= 0)
89 free(cb);
Frank Makered6b39c2011-05-23 21:14:58 -070090}