1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* src/utils.c Utilities
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
*/
#include "utils.h"
#include <stdlib.h>
void fatal(int err, const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "Error: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(err);
}
int nltool_init(int argc, char *argv[])
{
return 0;
}
int nltool_connect(struct nl_handle *nlh, int protocol)
{
int err;
err = nl_connect(nlh, protocol);
if (err < 0)
fatal(err, "Unable to connect netlink socket: %s",
nl_geterror());
return err;
}
struct nl_handle *nltool_alloc_handle(void)
{
struct nl_handle *sock;
if (!(sock = nl_handle_alloc()))
fatal(ENOBUFS, "Unable to allocate netlink socket");
return sock;
}
struct nl_addr *nltool_addr_parse(const char *str)
{
struct nl_addr *addr;
addr = nl_addr_parse(str, AF_UNSPEC);
if (!addr)
fprintf(stderr, "Unable to parse address \"%s\": %s\n",
str, nl_geterror());
return addr;
}
int nltool_parse_dumptype(const char *str)
{
if (!strcasecmp(str, "brief"))
return NL_DUMP_BRIEF;
else if (!strcasecmp(str, "detailed"))
return NL_DUMP_FULL;
else if (!strcasecmp(str, "stats"))
return NL_DUMP_STATS;
else if (!strcasecmp(str, "xml"))
return NL_DUMP_XML;
else if (!strcasecmp(str, "env"))
return NL_DUMP_ENV;
else {
fprintf(stderr, "Invalid dump type \"%s\".\n", str);
return -1;
}
}
struct nl_cache *nltool_alloc_link_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_link_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve link cache: %s",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_addr_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_addr_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve address cache: %s\n",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_neigh_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_neigh_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve neighbour cache: %s\n",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_neightbl_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_neightbl_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve neighbour table "
"cache: %s", nl_geterror());
else
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_route_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_route_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve route cache: %s\n",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_rule_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_rule_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve rule cache: %s\n",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_qdisc_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = rtnl_qdisc_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve qdisc cache: %s\n",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
struct nl_cache *nltool_alloc_genl_family_cache(struct nl_handle *nlh)
{
struct nl_cache *cache;
cache = genl_ctrl_alloc_cache(nlh);
if (!cache)
fatal(nl_get_errno(), "Unable to retrieve genl family cache: %s\n",
nl_geterror());
nl_cache_mngt_provide(cache);
return cache;
}
|