Initial import package libnl: Convenience library for kernel netlink sockets
[external/libnl.git] / src / utils.c
1 /*
2  * src/utils.c          Utilities
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-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 #include <stdlib.h>
15
16 int nltool_init(int argc, char *argv[])
17 {
18         return 0;
19 }
20
21 int nltool_connect(struct nl_handle *nlh, int protocol)
22 {
23         int err;
24
25         err = nl_connect(nlh, protocol);
26         if (err < 0)
27                 fprintf(stderr, "Unable to connect netlink socket%s\n",
28                         nl_geterror());
29
30         return err;
31 }
32
33 struct nl_handle *nltool_alloc_handle(void)
34 {
35         return nl_handle_alloc();
36 }
37
38 struct nl_addr *nltool_addr_parse(const char *str)
39 {
40         struct nl_addr *addr;
41
42         addr = nl_addr_parse(str, AF_UNSPEC);
43         if (!addr)
44                 fprintf(stderr, "Unable to parse address \"%s\": %s\n",
45                         str, nl_geterror());
46
47         return addr;
48 }
49
50 int nltool_parse_dumptype(const char *str)
51 {
52         if (!strcasecmp(str, "brief"))
53                 return NL_DUMP_BRIEF;
54         else if (!strcasecmp(str, "detailed"))
55                 return NL_DUMP_FULL;
56         else if (!strcasecmp(str, "stats"))
57                 return NL_DUMP_STATS;
58         else if (!strcasecmp(str, "xml"))
59                 return NL_DUMP_XML;
60         else if (!strcasecmp(str, "env"))
61                 return NL_DUMP_ENV;
62         else {
63                 fprintf(stderr, "Invalid dump type \"%s\".\n", str);
64                 return -1;
65         }
66 }
67
68 struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh)
69 {
70         struct nl_cache *cache;
71
72         cache = rtnl_link_alloc_cache(nlh);
73         if (!cache)
74                 fprintf(stderr, "Unable to retrieve link cache: %s\n",
75                         nl_geterror());
76         else
77                 nl_cache_mngt_provide(cache);
78
79         return cache;
80 }
81
82 struct nl_cache *nltool_alloc_addr_cache(struct nl_handle *nlh)
83 {
84         struct nl_cache *cache;
85
86         cache = rtnl_addr_alloc_cache(nlh);
87         if (!cache)
88                 fprintf(stderr, "Unable to retrieve address cache: %s\n",
89                         nl_geterror());
90         else
91                 nl_cache_mngt_provide(cache);
92
93         return cache;
94 }
95
96 struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh)
97 {
98         struct nl_cache *cache;
99
100         cache = rtnl_neigh_alloc_cache(nlh);
101         if (!cache)
102                 fprintf(stderr, "Unable to retrieve neighbour cache: %s\n",
103                         nl_geterror());
104         else
105                 nl_cache_mngt_provide(cache);
106
107         return cache;
108 }
109
110 struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh)
111 {
112         struct nl_cache *cache;
113
114         cache = rtnl_neightbl_alloc_cache(nlh);
115         if (!cache)
116                 fprintf(stderr, "Unable to retrieve neighbour table "
117                                 "cache: %s\n", nl_geterror());
118         else
119                 nl_cache_mngt_provide(cache);
120
121         return cache;
122 }
123
124 struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh)
125 {
126         struct nl_cache *cache;
127
128         cache = rtnl_route_alloc_cache(nlh);
129         if (!cache)
130                 fprintf(stderr, "Unable to retrieve route cache: %s\n",
131                         nl_geterror());
132         else
133                 nl_cache_mngt_provide(cache);
134
135         return cache;
136 }
137
138 struct nl_cache *nltool_alloc_rule_cache(struct nl_handle *nlh)
139 {
140         struct nl_cache *cache;
141
142         cache = rtnl_rule_alloc_cache(nlh);
143         if (!cache)
144                 fprintf(stderr, "Unable to retrieve rule cache: %s\n",
145                         nl_geterror());
146         else
147                 nl_cache_mngt_provide(cache);
148
149         return cache;
150 }
151
152 struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh)
153 {
154         struct nl_cache *cache;
155
156         cache = rtnl_qdisc_alloc_cache(nlh);
157         if (!cache)
158                 fprintf(stderr, "Unable to retrieve qdisc cache: %s\n",
159                         nl_geterror());
160         else
161                 nl_cache_mngt_provide(cache);
162
163         return cache;
164 }
165
166 struct nl_cache *nltool_alloc_genl_family_cache(struct nl_handle *nlh)
167 {
168         struct nl_cache *cache;
169
170         cache = genl_ctrl_alloc_cache(nlh);
171         if (!cache)
172                 fprintf(stderr, "Unable to retrieve genl family cache: %s\n",
173                         nl_geterror());
174         else
175                 nl_cache_mngt_provide(cache);
176
177         return cache;
178 }