]>
git.proxmox.com Git - mirror_iproute2.git/blob - lib/rt_names.c
2 * rt_names.c rtnetlink names DB.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
20 static void rtnl_tab_initialize(char *file
, char **tab
, int size
)
25 fp
= fopen(file
, "r");
28 while (fgets(buf
, sizeof(buf
), fp
)) {
33 while (*p
== ' ' || *p
== '\t')
35 if (*p
== '#' || *p
== '\n' || *p
== 0)
37 if (sscanf(p
, "0x%x %s\n", &id
, namebuf
) != 2 &&
38 sscanf(p
, "0x%x %s #", &id
, namebuf
) != 2 &&
39 sscanf(p
, "%d %s\n", &id
, namebuf
) != 2 &&
40 sscanf(p
, "%d %s #", &id
, namebuf
) != 2) {
41 fprintf(stderr
, "Database %s is corrupted at %s\n",
49 tab
[id
] = strdup(namebuf
);
55 static char * rtnl_rtprot_tab
[256] = {
73 static int rtnl_rtprot_init
;
75 static void rtnl_rtprot_initialize(void)
78 rtnl_tab_initialize("/etc/iproute2/rt_protos",
79 rtnl_rtprot_tab
, 256);
82 char * rtnl_rtprot_n2a(int id
, char *buf
, int len
)
84 if (id
<0 || id
>=256) {
85 snprintf(buf
, len
, "%d", id
);
88 if (!rtnl_rtprot_tab
[id
]) {
89 if (!rtnl_rtprot_init
)
90 rtnl_rtprot_initialize();
92 if (rtnl_rtprot_tab
[id
])
93 return rtnl_rtprot_tab
[id
];
94 snprintf(buf
, len
, "%d", id
);
98 int rtnl_rtprot_a2n(__u32
*id
, char *arg
)
100 static char *cache
= NULL
;
101 static unsigned long res
;
105 if (cache
&& strcmp(cache
, arg
) == 0) {
110 if (!rtnl_rtprot_init
)
111 rtnl_rtprot_initialize();
113 for (i
=0; i
<256; i
++) {
114 if (rtnl_rtprot_tab
[i
] &&
115 strcmp(rtnl_rtprot_tab
[i
], arg
) == 0) {
116 cache
= rtnl_rtprot_tab
[i
];
123 res
= strtoul(arg
, &end
, 0);
124 if (!end
|| end
== arg
|| *end
|| res
> 255)
132 static char * rtnl_rtscope_tab
[256] = {
136 static int rtnl_rtscope_init
;
138 static void rtnl_rtscope_initialize(void)
140 rtnl_rtscope_init
= 1;
141 rtnl_rtscope_tab
[255] = "nowhere";
142 rtnl_rtscope_tab
[254] = "host";
143 rtnl_rtscope_tab
[253] = "link";
144 rtnl_rtscope_tab
[200] = "site";
145 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
146 rtnl_rtscope_tab
, 256);
149 char * rtnl_rtscope_n2a(int id
, char *buf
, int len
)
151 if (id
<0 || id
>=256) {
152 snprintf(buf
, len
, "%d", id
);
155 if (!rtnl_rtscope_tab
[id
]) {
156 if (!rtnl_rtscope_init
)
157 rtnl_rtscope_initialize();
159 if (rtnl_rtscope_tab
[id
])
160 return rtnl_rtscope_tab
[id
];
161 snprintf(buf
, len
, "%d", id
);
165 int rtnl_rtscope_a2n(__u32
*id
, char *arg
)
167 static char *cache
= NULL
;
168 static unsigned long res
;
172 if (cache
&& strcmp(cache
, arg
) == 0) {
177 if (!rtnl_rtscope_init
)
178 rtnl_rtscope_initialize();
180 for (i
=0; i
<256; i
++) {
181 if (rtnl_rtscope_tab
[i
] &&
182 strcmp(rtnl_rtscope_tab
[i
], arg
) == 0) {
183 cache
= rtnl_rtscope_tab
[i
];
190 res
= strtoul(arg
, &end
, 0);
191 if (!end
|| end
== arg
|| *end
|| res
> 255)
199 static char * rtnl_rtrealm_tab
[256] = {
203 static int rtnl_rtrealm_init
;
205 static void rtnl_rtrealm_initialize(void)
207 rtnl_rtrealm_init
= 1;
208 rtnl_tab_initialize("/etc/iproute2/rt_realms",
209 rtnl_rtrealm_tab
, 256);
212 char * rtnl_rtrealm_n2a(int id
, char *buf
, int len
)
214 if (id
<0 || id
>=256) {
215 snprintf(buf
, len
, "%d", id
);
218 if (!rtnl_rtrealm_tab
[id
]) {
219 if (!rtnl_rtrealm_init
)
220 rtnl_rtrealm_initialize();
222 if (rtnl_rtrealm_tab
[id
])
223 return rtnl_rtrealm_tab
[id
];
224 snprintf(buf
, len
, "%d", id
);
229 int rtnl_rtrealm_a2n(__u32
*id
, char *arg
)
231 static char *cache
= NULL
;
232 static unsigned long res
;
236 if (cache
&& strcmp(cache
, arg
) == 0) {
241 if (!rtnl_rtrealm_init
)
242 rtnl_rtrealm_initialize();
244 for (i
=0; i
<256; i
++) {
245 if (rtnl_rtrealm_tab
[i
] &&
246 strcmp(rtnl_rtrealm_tab
[i
], arg
) == 0) {
247 cache
= rtnl_rtrealm_tab
[i
];
254 res
= strtoul(arg
, &end
, 0);
255 if (!end
|| end
== arg
|| *end
|| res
> 255)
263 static char * rtnl_rttable_tab
[256] = {
267 static int rtnl_rttable_init
;
269 static void rtnl_rttable_initialize(void)
271 rtnl_rttable_init
= 1;
272 rtnl_rttable_tab
[255] = "local";
273 rtnl_rttable_tab
[254] = "main";
274 rtnl_tab_initialize("/etc/iproute2/rt_tables",
275 rtnl_rttable_tab
, 256);
278 char * rtnl_rttable_n2a(int id
, char *buf
, int len
)
280 if (id
<0 || id
>=256) {
281 snprintf(buf
, len
, "%d", id
);
284 if (!rtnl_rttable_tab
[id
]) {
285 if (!rtnl_rttable_init
)
286 rtnl_rttable_initialize();
288 if (rtnl_rttable_tab
[id
])
289 return rtnl_rttable_tab
[id
];
290 snprintf(buf
, len
, "%d", id
);
294 int rtnl_rttable_a2n(__u32
*id
, char *arg
)
296 static char *cache
= NULL
;
297 static unsigned long res
;
301 if (cache
&& strcmp(cache
, arg
) == 0) {
306 if (!rtnl_rttable_init
)
307 rtnl_rttable_initialize();
309 for (i
=0; i
<256; i
++) {
310 if (rtnl_rttable_tab
[i
] &&
311 strcmp(rtnl_rttable_tab
[i
], arg
) == 0) {
312 cache
= rtnl_rttable_tab
[i
];
319 i
= strtoul(arg
, &end
, 0);
320 if (!end
|| end
== arg
|| *end
|| i
> 255)
327 static char * rtnl_rtdsfield_tab
[256] = {
331 static int rtnl_rtdsfield_init
;
333 static void rtnl_rtdsfield_initialize(void)
335 rtnl_rtdsfield_init
= 1;
336 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
337 rtnl_rtdsfield_tab
, 256);
340 char * rtnl_dsfield_n2a(int id
, char *buf
, int len
)
342 if (id
<0 || id
>=256) {
343 snprintf(buf
, len
, "%d", id
);
346 if (!rtnl_rtdsfield_tab
[id
]) {
347 if (!rtnl_rtdsfield_init
)
348 rtnl_rtdsfield_initialize();
350 if (rtnl_rtdsfield_tab
[id
])
351 return rtnl_rtdsfield_tab
[id
];
352 snprintf(buf
, len
, "0x%02x", id
);
357 int rtnl_dsfield_a2n(__u32
*id
, char *arg
)
359 static char *cache
= NULL
;
360 static unsigned long res
;
364 if (cache
&& strcmp(cache
, arg
) == 0) {
369 if (!rtnl_rtdsfield_init
)
370 rtnl_rtdsfield_initialize();
372 for (i
=0; i
<256; i
++) {
373 if (rtnl_rtdsfield_tab
[i
] &&
374 strcmp(rtnl_rtdsfield_tab
[i
], arg
) == 0) {
375 cache
= rtnl_rtdsfield_tab
[i
];
382 res
= strtoul(arg
, &end
, 16);
383 if (!end
|| end
== arg
|| *end
|| res
> 255)