oops
[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_cbset = NL_CB_VERBOSE;
17
18 int nltool_init(int argc, char *argv[])
19 {
20         char *nlcb = getenv("NLCB");
21         char *nldbg = getenv("NLDBG");
22         
23         if (nlcb) {
24                 if (!strcasecmp(nlcb, "default"))
25                         nltool_cbset = NL_CB_DEFAULT;
26                 else if (!strcasecmp(nlcb, "verbose"))
27                         nltool_cbset = NL_CB_VERBOSE;
28                 else if (!strcasecmp(nlcb, "debug"))
29                         nltool_cbset = NL_CB_DEBUG;
30                 else {
31                         fprintf(stderr, "Unknown value for NLCB, valid values: "
32                                 "{default | verbose | debug}\n");
33                         goto errout;
34                 }
35         }
36
37         if (nldbg) {
38                 long dbg = strtol(nldbg, NULL, 0);
39
40                 if (dbg == LONG_MIN || dbg == LONG_MAX) {
41                         fprintf(stderr, "Invalid value for NLDBG.\n");
42                         goto errout;
43                 }
44
45                 nl_debug = dbg;
46         }
47         
48         return 0;
49
50 errout:
51         return -1;
52 }
53
54 int nltool_connect(struct nl_handle *nlh, int protocol)
55 {
56         int err;
57
58         err = nl_connect(nlh, protocol);
59         if (err < 0)
60                 fprintf(stderr, "Unable to connect netlink socket%s\n",
61                         nl_geterror());
62
63         return err;
64 }
65
66 struct nl_addr *nltool_addr_parse(const char *str)
67 {
68         struct nl_addr *addr;
69
70         addr = nl_addr_parse(str, AF_UNSPEC);
71         if (!addr)
72                 fprintf(stderr, "Unable to parse address \"%s\": %s\n",
73                         str, nl_geterror());
74
75         return addr;
76 }
77
78 int nltool_parse_dumptype(const char *str)
79 {
80         if (!strcasecmp(str, "brief"))
81                 return NL_DUMP_BRIEF;
82         else if (!strcasecmp(str, "detailed"))
83                 return NL_DUMP_FULL;
84         else if (!strcasecmp(str, "stats"))
85                 return NL_DUMP_STATS;
86         else if (!strcasecmp(str, "xml"))
87                 return NL_DUMP_XML;
88         else if (!strcasecmp(str, "env"))
89                 return NL_DUMP_ENV;
90         else {
91                 fprintf(stderr, "Invalid dump type \"%s\".\n", str);
92                 return -1;
93         }
94 }
95
96 struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh)
97 {
98         struct nl_cache *cache;
99
100         cache = rtnl_link_alloc_cache(nlh);
101         if (!cache)
102                 fprintf(stderr, "Unable to retrieve link 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_addr_cache(struct nl_handle *nlh)
111 {
112         struct nl_cache *cache;
113
114         cache = rtnl_addr_alloc_cache(nlh);
115         if (!cache)
116                 fprintf(stderr, "Unable to retrieve address cache: %s\n",
117                         nl_geterror());
118         else
119                 nl_cache_mngt_provide(cache);
120
121         return cache;
122 }
123
124 struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh)
125 {
126         struct nl_cache *cache;
127
128         cache = rtnl_neigh_alloc_cache(nlh);
129         if (!cache)
130                 fprintf(stderr, "Unable to retrieve neighbour 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_neightbl_cache(struct nl_handle *nlh)
139 {
140         struct nl_cache *cache;
141
142         cache = rtnl_neightbl_alloc_cache(nlh);
143         if (!cache)
144                 fprintf(stderr, "Unable to retrieve neighbour table "
145                                 "cache: %s\n", nl_geterror());
146         else
147                 nl_cache_mngt_provide(cache);
148
149         return cache;
150 }
151
152 struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh)
153 {
154         struct nl_cache *cache;
155
156         cache = rtnl_route_alloc_cache(nlh);
157         if (!cache)
158                 fprintf(stderr, "Unable to retrieve route 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_rule_cache(struct nl_handle *nlh)
167 {
168         struct nl_cache *cache;
169
170         cache = rtnl_rule_alloc_cache(nlh);
171         if (!cache)
172                 fprintf(stderr, "Unable to retrieve rule cache: %s\n",
173                         nl_geterror());
174         else
175                 nl_cache_mngt_provide(cache);
176
177         return cache;
178 }
179
180 struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh)
181 {
182         struct nl_cache *cache;
183
184         cache = rtnl_qdisc_alloc_cache(nlh);
185         if (!cache)
186                 fprintf(stderr, "Unable to retrieve qdisc cache: %s\n",
187                         nl_geterror());
188         else
189                 nl_cache_mngt_provide(cache);
190
191         return cache;
192 }