2 * src/utils.c Utilities
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
16 int nltool_cbset = NL_CB_VERBOSE;
18 int nltool_init(int argc, char *argv[])
20 char *nlcb = getenv("NLCB");
21 char *nldbg = getenv("NLDBG");
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;
31 fprintf(stderr, "Unknown value for NLCB, valid values: "
32 "{default | verbose | debug}\n");
38 long dbg = strtol(nldbg, NULL, 0);
40 if (dbg == LONG_MIN || dbg == LONG_MAX) {
41 fprintf(stderr, "Invalid value for NLDBG.\n");
54 int nltool_connect(struct nl_handle *nlh, int protocol)
58 err = nl_connect(nlh, protocol);
60 fprintf(stderr, "Unable to connect netlink socket%s\n",
66 struct nl_addr *nltool_addr_parse(const char *str)
70 addr = nl_addr_parse(str, AF_UNSPEC);
72 fprintf(stderr, "Unable to parse address \"%s\": %s\n",
78 int nltool_parse_dumptype(const char *str)
80 if (!strcasecmp(str, "brief"))
82 else if (!strcasecmp(str, "detailed"))
84 else if (!strcasecmp(str, "stats"))
86 else if (!strcasecmp(str, "xml"))
88 else if (!strcasecmp(str, "env"))
91 fprintf(stderr, "Invalid dump type \"%s\".\n", str);
96 struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh)
98 struct nl_cache *cache;
100 cache = rtnl_link_alloc_cache(nlh);
102 fprintf(stderr, "Unable to retrieve link cache: %s\n",
105 nl_cache_mngt_provide(cache);
110 struct nl_cache *nltool_alloc_addr_cache(struct nl_handle *nlh)
112 struct nl_cache *cache;
114 cache = rtnl_addr_alloc_cache(nlh);
116 fprintf(stderr, "Unable to retrieve address cache: %s\n",
119 nl_cache_mngt_provide(cache);
124 struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh)
126 struct nl_cache *cache;
128 cache = rtnl_neigh_alloc_cache(nlh);
130 fprintf(stderr, "Unable to retrieve neighbour cache: %s\n",
133 nl_cache_mngt_provide(cache);
138 struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh)
140 struct nl_cache *cache;
142 cache = rtnl_neightbl_alloc_cache(nlh);
144 fprintf(stderr, "Unable to retrieve neighbour table "
145 "cache: %s\n", nl_geterror());
147 nl_cache_mngt_provide(cache);
152 struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh)
154 struct nl_cache *cache;
156 cache = rtnl_route_alloc_cache(nlh);
158 fprintf(stderr, "Unable to retrieve route cache: %s\n",
161 nl_cache_mngt_provide(cache);
166 struct nl_cache *nltool_alloc_rule_cache(struct nl_handle *nlh)
168 struct nl_cache *cache;
170 cache = rtnl_rule_alloc_cache(nlh);
172 fprintf(stderr, "Unable to retrieve rule cache: %s\n",
175 nl_cache_mngt_provide(cache);
180 struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh)
182 struct nl_cache *cache;
184 cache = rtnl_qdisc_alloc_cache(nlh);
186 fprintf(stderr, "Unable to retrieve qdisc cache: %s\n",
189 nl_cache_mngt_provide(cache);