1 /* vi: set sw=4 ts=4: */
3 * rt_names.c rtnetlink names DB.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 static void rtnl_tab_initialize(char *file, const char **tab, int size)
24 fp = fopen(file, "r");
27 while (fgets(buf, sizeof(buf), fp)) {
32 while (*p == ' ' || *p == '\t')
34 if (*p == '#' || *p == '\n' || *p == 0)
36 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
37 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
38 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
39 sscanf(p, "%d %s #", &id, namebuf) != 2) {
40 fprintf(stderr, "Database %s is corrupted at %s\n",
48 tab[id] = strdup(namebuf);
54 static const char * rtnl_rtprot_tab[256] = {
72 static int rtnl_rtprot_init;
74 static void rtnl_rtprot_initialize(void)
77 rtnl_tab_initialize("/etc/iproute2/rt_protos",
78 rtnl_rtprot_tab, 256);
81 const char * rtnl_rtprot_n2a(int id, char *buf, int len)
83 if (id<0 || id>=256) {
84 snprintf(buf, len, "%d", id);
87 if (!rtnl_rtprot_tab[id]) {
88 if (!rtnl_rtprot_init)
89 rtnl_rtprot_initialize();
91 if (rtnl_rtprot_tab[id])
92 return rtnl_rtprot_tab[id];
93 snprintf(buf, len, "%d", id);
97 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
99 static const char *cache = NULL;
100 static unsigned long res;
104 if (cache && strcmp(cache, arg) == 0) {
109 if (!rtnl_rtprot_init)
110 rtnl_rtprot_initialize();
112 for (i=0; i<256; i++) {
113 if (rtnl_rtprot_tab[i] &&
114 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
115 cache = rtnl_rtprot_tab[i];
122 res = strtoul(arg, &end, 0);
123 if (!end || end == arg || *end || res > 255)
131 static const char * rtnl_rtscope_tab[256] = {
135 static int rtnl_rtscope_init;
137 static void rtnl_rtscope_initialize(void)
139 rtnl_rtscope_init = 1;
140 rtnl_rtscope_tab[255] = "nowhere";
141 rtnl_rtscope_tab[254] = "host";
142 rtnl_rtscope_tab[253] = "link";
143 rtnl_rtscope_tab[200] = "site";
144 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
145 rtnl_rtscope_tab, 256);
148 const char * rtnl_rtscope_n2a(int id, char *buf, int len)
150 if (id<0 || id>=256) {
151 snprintf(buf, len, "%d", id);
154 if (!rtnl_rtscope_tab[id]) {
155 if (!rtnl_rtscope_init)
156 rtnl_rtscope_initialize();
158 if (rtnl_rtscope_tab[id])
159 return rtnl_rtscope_tab[id];
160 snprintf(buf, len, "%d", id);
164 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
166 static const char *cache = NULL;
167 static unsigned long res;
171 if (cache && strcmp(cache, arg) == 0) {
176 if (!rtnl_rtscope_init)
177 rtnl_rtscope_initialize();
179 for (i=0; i<256; i++) {
180 if (rtnl_rtscope_tab[i] &&
181 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
182 cache = rtnl_rtscope_tab[i];
189 res = strtoul(arg, &end, 0);
190 if (!end || end == arg || *end || res > 255)
198 static const char * rtnl_rtrealm_tab[256] = {
202 static int rtnl_rtrealm_init;
204 static void rtnl_rtrealm_initialize(void)
206 rtnl_rtrealm_init = 1;
207 rtnl_tab_initialize("/etc/iproute2/rt_realms",
208 rtnl_rtrealm_tab, 256);
211 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
213 static const char *cache = NULL;
214 static unsigned long res;
218 if (cache && strcmp(cache, arg) == 0) {
223 if (!rtnl_rtrealm_init)
224 rtnl_rtrealm_initialize();
226 for (i=0; i<256; i++) {
227 if (rtnl_rtrealm_tab[i] &&
228 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
229 cache = rtnl_rtrealm_tab[i];
236 res = strtoul(arg, &end, 0);
237 if (!end || end == arg || *end || res > 255)
245 static const char * rtnl_rtdsfield_tab[256] = {
249 static int rtnl_rtdsfield_init;
251 static void rtnl_rtdsfield_initialize(void)
253 rtnl_rtdsfield_init = 1;
254 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
255 rtnl_rtdsfield_tab, 256);
258 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
260 if (id<0 || id>=256) {
261 snprintf(buf, len, "%d", id);
264 if (!rtnl_rtdsfield_tab[id]) {
265 if (!rtnl_rtdsfield_init)
266 rtnl_rtdsfield_initialize();
268 if (rtnl_rtdsfield_tab[id])
269 return rtnl_rtdsfield_tab[id];
270 snprintf(buf, len, "0x%02x", id);
275 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
277 static const char *cache = NULL;
278 static unsigned long res;
282 if (cache && strcmp(cache, arg) == 0) {
287 if (!rtnl_rtdsfield_init)
288 rtnl_rtdsfield_initialize();
290 for (i=0; i<256; i++) {
291 if (rtnl_rtdsfield_tab[i] &&
292 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
293 cache = rtnl_rtdsfield_tab[i];
300 res = strtoul(arg, &end, 16);
301 if (!end || end == arg || *end || res > 255)