1/*
2 * lib/cache_mngt.c	Cache Management
3 *
4 *	This library is free software; you can redistribute it and/or
5 *	modify it under the terms of the GNU Lesser General Public
6 *	License as published by the Free Software Foundation version 2.1
7 *	of the License.
8 *
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12/**
13 * @ingroup core
14 * @defgroup cache_mngt Caching
15 * @{
16 */
17
18#include <netlink-local.h>
19#include <netlink/netlink.h>
20#include <netlink/cache.h>
21#include <netlink/utils.h>
22
23static struct nl_cache_ops *cache_ops;
24
25/**
26 * @name Cache Operations Sets
27 * @{
28 */
29
30/**
31 * Lookup the set cache operations of a certain cache type
32 * @arg name		name of the cache type
33 *
34 * @return The cache operations or NULL if no operations
35 *         have been registered under the specified name.
36 */
37struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
38{
39	struct nl_cache_ops *ops;
40
41	for (ops = cache_ops; ops; ops = ops->co_next)
42		if (!strcmp(ops->co_name, name))
43			return ops;
44
45	return NULL;
46}
47
48/**
49 * Associate a message type to a set of cache operations
50 * @arg protocol		netlink protocol
51 * @arg msgtype			netlink message type
52 *
53 * Associates the specified netlink message type with
54 * a registered set of cache operations.
55 *
56 * @return The cache operations or NULL if no association
57 *         could be made.
58 */
59struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
60{
61	int i;
62	struct nl_cache_ops *ops;
63
64	for (ops = cache_ops; ops; ops = ops->co_next) {
65		if (ops->co_protocol != protocol)
66			continue;
67
68		for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
69			if (ops->co_msgtypes[i].mt_id == msgtype)
70				return ops;
71	}
72
73	return NULL;
74}
75
76/**
77 * Register a set of cache operations
78 * @arg ops		cache operations
79 *
80 * Called by users of caches to announce the avaibility of
81 * a certain cache type.
82 *
83 * @return 0 on success or a negative error code.
84 */
85int nl_cache_mngt_register(struct nl_cache_ops *ops)
86{
87	if (!ops->co_name || !ops->co_obj_ops)
88		return -NLE_INVAL;
89
90	if (nl_cache_ops_lookup(ops->co_name))
91		return -NLE_EXIST;
92
93	ops->co_next = cache_ops;
94	cache_ops = ops;
95
96	NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
97
98	return 0;
99}
100
101/**
102 * Unregister a set of cache operations
103 * @arg ops		cache operations
104 *
105 * Called by users of caches to announce a set of
106 * cache operations is no longer available. The
107 * specified cache operations must have been registered
108 * previously using nl_cache_mngt_register()
109 *
110 * @return 0 on success or a negative error code
111 */
112int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
113{
114	struct nl_cache_ops *t, **tp;
115
116	for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
117		if (t == ops)
118			break;
119
120	if (!t)
121		return -NLE_NOCACHE;
122
123	NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
124
125	*tp = t->co_next;
126	return 0;
127}
128
129/** @} */
130
131/** @} */
132