1ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker/*
2ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * Copyright (C) 2011 The Android Open Source Project
3ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker *
4ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * Licensed under the Apache License, Version 2.0 (the "License");
5ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * you may not use this file except in compliance with the License.
6ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * You may obtain a copy of the License at
7ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker *
8ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker *	http://www.apache.org/licenses/LICENSE-2.0
9ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker *
10ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * Unless required by applicable law or agreed to in writing, software
11ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * distributed under the License is distributed on an "AS IS" BASIS,
12ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * See the License for the specific language governing permissions and
14ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker * limitations under the License.
15ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker */
16ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
17ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker/* NOTICE: This is a clean room re-implementation of libnl */
18ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
19ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker#include <malloc.h>
20ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker#include "netlink-types.h"
21ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker#include "netlink/handlers.h"
22ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
23ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker/* Allocate a new callback handle. */
24ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerstruct nl_cb *nl_cb_alloc(enum nl_cb_kind kind)
25ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker{
26ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	struct nl_cb *cb;
27ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
28ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb = (struct nl_cb *) malloc(sizeof(struct nl_cb));
29ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	if (cb == NULL)
30ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker		goto fail;
31ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	memset(cb, 0, sizeof(*cb));
32ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
33ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return nl_cb_get(cb);
34ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerfail:
35ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return NULL;
36ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker}
37ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
38ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker/* Clone an existing callback handle */
39ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerstruct nl_cb *nl_cb_clone(struct nl_cb *orig)
40ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker{
41ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	struct nl_cb *new_cb;
42ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
43ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	new_cb = nl_cb_alloc(NL_CB_DEFAULT);
44ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	if (new_cb == NULL)
45ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker		goto fail;
46ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
47d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7Dmitry Shmidt	/* Copy original and set refcount to 1 */
48ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	memcpy(new_cb, orig, sizeof(*orig));
49d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7Dmitry Shmidt	new_cb->cb_refcnt = 1;
50ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
51ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return new_cb;
52ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerfail:
53ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return NULL;
54ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker}
55ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
56ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker/* Set up a callback. */
57ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerint nl_cb_set(struct nl_cb *cb, enum nl_cb_type type, enum nl_cb_kind kind, \
58ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	nl_recvmsg_msg_cb_t func, void *arg)
59ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker{
60ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb->cb_set[type] = func;
61ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb->cb_args[type] = arg;
62ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return 0;
63ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker}
64ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
65ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
66ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
67ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker/* Set up an error callback. */
68ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerint nl_cb_err(struct nl_cb *cb, enum nl_cb_kind kind, \
69ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	nl_recvmsg_err_cb_t func, void *arg)
70ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker{
71ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb->cb_err = func;
72ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb->cb_err_arg = arg;
73ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return 0;
74ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
75ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker}
76ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
77ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makerstruct nl_cb *nl_cb_get(struct nl_cb *cb)
78ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker{
79ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb->cb_refcnt++;
80ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	return cb;
81ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker}
82ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker
83ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Makervoid nl_cb_put(struct nl_cb *cb)
84ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker{
85d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7Dmitry Shmidt	if (!cb)
86d99fe5ec960b5eae96585d5ceb59fe287fe0c5e7Dmitry Shmidt		return;
87ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	cb->cb_refcnt--;
88ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker	if (cb->cb_refcnt <= 0)
89ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker		free(cb);
90ed6b39cc7746fabdd0d01c96afcf60b9544913d3Frank Maker}
91