Tizen 2.1 base
[platform/upstream/libnl2.git] / lib / route / link / api.c
1 /*
2  * lib/route/link/api.c         Link Info API
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 link
14  * @defgroup link_info Link Info API
15  * @brief
16  *
17  * @par 1) Registering/Unregistering a new link info type
18  * @code
19  * static struct rtnl_link_info_ops vlan_info_ops = {
20  *      .io_name                = "vlan",
21  *      .io_alloc               = vlan_alloc,
22  *      .io_parse               = vlan_parse,
23  *      .io_dump[NL_DUMP_BRIEF] = vlan_dump_brief,
24  *      .io_dump[NL_DUMP_FULL]  = vlan_dump_full,
25  *      .io_free                = vlan_free,
26  * };
27  *
28  * static void __init vlan_init(void)
29  * {
30  *      rtnl_link_register_info(&vlan_info_ops);
31  * }
32  *
33  * static void __exit vlan_exit(void)
34  * {
35  *      rtnl_link_unregister_info(&vlan_info_ops);
36  * }
37  * @endcode
38  *
39  * @{
40  */
41
42 #include <netlink-local.h>
43 #include <netlink/netlink.h>
44 #include <netlink/utils.h>
45 #include <netlink/route/link.h>
46 #include <netlink/route/link/info-api.h>
47
48 static struct rtnl_link_info_ops *info_ops;
49
50 struct rtnl_link_info_ops *rtnl_link_info_ops_lookup(const char *name)
51 {
52         struct rtnl_link_info_ops *ops;
53
54         for (ops = info_ops; ops; ops = ops->io_next)
55                 if (!strcmp(ops->io_name, name))
56                         return ops;
57
58         return NULL;
59 }
60
61 int rtnl_link_register_info(struct rtnl_link_info_ops *ops)
62 {
63         if (ops->io_name == NULL)
64                 return -NLE_INVAL;
65
66         if (rtnl_link_info_ops_lookup(ops->io_name))
67                 return -NLE_EXIST;
68
69         NL_DBG(1, "Registered link info operations %s\n", ops->io_name);
70
71         ops->io_next = info_ops;
72         info_ops = ops;
73
74         return 0;
75 }
76
77 int rtnl_link_unregister_info(struct rtnl_link_info_ops *ops)
78 {
79         struct rtnl_link_info_ops *t, **tp;
80
81         for (tp = &info_ops; (t=*tp) != NULL; tp = &t->io_next)
82                 if (t == ops)
83                         break;
84
85         if (!t)
86                 return -NLE_OPNOTSUPP;
87
88         if (t->io_refcnt > 0)
89                 return -NLE_BUSY;
90
91         NL_DBG(1, "Unregistered link info perations %s\n", ops->io_name);
92
93         *tp = t->io_next;
94         return 0;
95 }
96
97 /** @} */
98