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>
18 static void rtnl_tab_initialize(char *file, char **tab, int size)
23 fp = fopen(file, "r");
26 while (fgets(buf, sizeof(buf), fp)) {
31 while (*p == ' ' || *p == '\t')
33 if (*p == '#' || *p == '\n' || *p == 0)
35 if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
36 sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
37 sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
38 sscanf(p, "%d %s #", &id, namebuf) != 2) {
39 fprintf(stderr, "Database %s is corrupted at %s\n",
47 tab[id] = strdup(namebuf);
53 static char * rtnl_rtprot_tab[256] = {
71 static int rtnl_rtprot_init;
73 static void rtnl_rtprot_initialize(void)
76 rtnl_tab_initialize("/etc/iproute2/rt_protos",
77 rtnl_rtprot_tab, 256);
80 const char * rtnl_rtprot_n2a(int id, char *buf, int len)
82 if (id<0 || id>=256) {
83 snprintf(buf, len, "%d", id);
86 if (!rtnl_rtprot_tab[id]) {
87 if (!rtnl_rtprot_init)
88 rtnl_rtprot_initialize();
90 if (rtnl_rtprot_tab[id])
91 return rtnl_rtprot_tab[id];
92 snprintf(buf, len, "%d", id);
96 int rtnl_rtprot_a2n(uint32_t *id, char *arg)
98 static char *cache = NULL;
99 static unsigned long res;
103 if (cache && strcmp(cache, arg) == 0) {
108 if (!rtnl_rtprot_init)
109 rtnl_rtprot_initialize();
111 for (i=0; i<256; i++) {
112 if (rtnl_rtprot_tab[i] &&
113 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
114 cache = rtnl_rtprot_tab[i];
121 res = strtoul(arg, &end, 0);
122 if (!end || end == arg || *end || res > 255)
130 static char * rtnl_rtscope_tab[256] = {
134 static int rtnl_rtscope_init;
136 static void rtnl_rtscope_initialize(void)
138 rtnl_rtscope_init = 1;
139 rtnl_rtscope_tab[255] = "nowhere";
140 rtnl_rtscope_tab[254] = "host";
141 rtnl_rtscope_tab[253] = "link";
142 rtnl_rtscope_tab[200] = "site";
143 rtnl_tab_initialize("/etc/iproute2/rt_scopes",
144 rtnl_rtscope_tab, 256);
147 const char * rtnl_rtscope_n2a(int id, char *buf, int len)
149 if (id<0 || id>=256) {
150 snprintf(buf, len, "%d", id);
153 if (!rtnl_rtscope_tab[id]) {
154 if (!rtnl_rtscope_init)
155 rtnl_rtscope_initialize();
157 if (rtnl_rtscope_tab[id])
158 return rtnl_rtscope_tab[id];
159 snprintf(buf, len, "%d", id);
163 int rtnl_rtscope_a2n(uint32_t *id, char *arg)
165 static char *cache = NULL;
166 static unsigned long res;
170 if (cache && strcmp(cache, arg) == 0) {
175 if (!rtnl_rtscope_init)
176 rtnl_rtscope_initialize();
178 for (i=0; i<256; i++) {
179 if (rtnl_rtscope_tab[i] &&
180 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
181 cache = rtnl_rtscope_tab[i];
188 res = strtoul(arg, &end, 0);
189 if (!end || end == arg || *end || res > 255)
197 static char * rtnl_rtrealm_tab[256] = {
201 static int rtnl_rtrealm_init;
203 static void rtnl_rtrealm_initialize(void)
205 rtnl_rtrealm_init = 1;
206 rtnl_tab_initialize("/etc/iproute2/rt_realms",
207 rtnl_rtrealm_tab, 256);
210 int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
212 static char *cache = NULL;
213 static unsigned long res;
217 if (cache && strcmp(cache, arg) == 0) {
222 if (!rtnl_rtrealm_init)
223 rtnl_rtrealm_initialize();
225 for (i=0; i<256; i++) {
226 if (rtnl_rtrealm_tab[i] &&
227 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
228 cache = rtnl_rtrealm_tab[i];
235 res = strtoul(arg, &end, 0);
236 if (!end || end == arg || *end || res > 255)
244 static char * rtnl_rtdsfield_tab[256] = {
248 static int rtnl_rtdsfield_init;
250 static void rtnl_rtdsfield_initialize(void)
252 rtnl_rtdsfield_init = 1;
253 rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
254 rtnl_rtdsfield_tab, 256);
257 const char * rtnl_dsfield_n2a(int id, char *buf, int len)
259 if (id<0 || id>=256) {
260 snprintf(buf, len, "%d", id);
263 if (!rtnl_rtdsfield_tab[id]) {
264 if (!rtnl_rtdsfield_init)
265 rtnl_rtdsfield_initialize();
267 if (rtnl_rtdsfield_tab[id])
268 return rtnl_rtdsfield_tab[id];
269 snprintf(buf, len, "0x%02x", id);
274 int rtnl_dsfield_a2n(uint32_t *id, char *arg)
276 static char *cache = NULL;
277 static unsigned long res;
281 if (cache && strcmp(cache, arg) == 0) {
286 if (!rtnl_rtdsfield_init)
287 rtnl_rtdsfield_initialize();
289 for (i=0; i<256; i++) {
290 if (rtnl_rtdsfield_tab[i] &&
291 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
292 cache = rtnl_rtdsfield_tab[i];
299 res = strtoul(arg, &end, 16);
300 if (!end || end == arg || *end || res > 255)