Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rt_names.c rtnetlink names DB. |
| 3 | * |
| 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. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 13 | #include <string.h> |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame^] | 14 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | #include <stdint.h> |
| 16 | |
| 17 | static void rtnl_tab_initialize(char *file, char **tab, int size) |
| 18 | { |
| 19 | char buf[512]; |
| 20 | FILE *fp; |
| 21 | |
| 22 | fp = fopen(file, "r"); |
| 23 | if (!fp) |
| 24 | return; |
| 25 | while (fgets(buf, sizeof(buf), fp)) { |
| 26 | char *p = buf; |
| 27 | int id; |
| 28 | char namebuf[512]; |
| 29 | |
| 30 | while (*p == ' ' || *p == '\t') |
| 31 | p++; |
| 32 | if (*p == '#' || *p == '\n' || *p == 0) |
| 33 | continue; |
| 34 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 && |
| 35 | sscanf(p, "0x%x %s #", &id, namebuf) != 2 && |
| 36 | sscanf(p, "%d %s\n", &id, namebuf) != 2 && |
| 37 | sscanf(p, "%d %s #", &id, namebuf) != 2) { |
| 38 | fprintf(stderr, "Database %s is corrupted at %s\n", |
| 39 | file, p); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (id<0 || id>size) |
| 44 | continue; |
| 45 | |
| 46 | tab[id] = strdup(namebuf); |
| 47 | } |
| 48 | fclose(fp); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static char * rtnl_rtprot_tab[256] = { |
| 53 | "none", |
| 54 | "redirect", |
| 55 | "kernel", |
| 56 | "boot", |
| 57 | "static", |
| 58 | NULL, |
| 59 | NULL, |
| 60 | NULL, |
| 61 | "gated", |
| 62 | "ra", |
| 63 | "mrt", |
| 64 | "zebra", |
| 65 | "bird", |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | |
| 70 | static int rtnl_rtprot_init; |
| 71 | |
| 72 | static void rtnl_rtprot_initialize(void) |
| 73 | { |
| 74 | rtnl_rtprot_init = 1; |
| 75 | rtnl_tab_initialize("/etc/iproute2/rt_protos", |
| 76 | rtnl_rtprot_tab, 256); |
| 77 | } |
| 78 | |
| 79 | char * rtnl_rtprot_n2a(int id, char *buf, int len) |
| 80 | { |
| 81 | if (id<0 || id>=256) { |
| 82 | snprintf(buf, len, "%d", id); |
| 83 | return buf; |
| 84 | } |
| 85 | if (!rtnl_rtprot_tab[id]) { |
| 86 | if (!rtnl_rtprot_init) |
| 87 | rtnl_rtprot_initialize(); |
| 88 | } |
| 89 | if (rtnl_rtprot_tab[id]) |
| 90 | return rtnl_rtprot_tab[id]; |
| 91 | snprintf(buf, len, "%d", id); |
| 92 | return buf; |
| 93 | } |
| 94 | |
| 95 | int rtnl_rtprot_a2n(uint32_t *id, char *arg) |
| 96 | { |
| 97 | static char *cache = NULL; |
| 98 | static unsigned long res; |
| 99 | char *end; |
| 100 | int i; |
| 101 | |
| 102 | if (cache && strcmp(cache, arg) == 0) { |
| 103 | *id = res; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | if (!rtnl_rtprot_init) |
| 108 | rtnl_rtprot_initialize(); |
| 109 | |
| 110 | for (i=0; i<256; i++) { |
| 111 | if (rtnl_rtprot_tab[i] && |
| 112 | strcmp(rtnl_rtprot_tab[i], arg) == 0) { |
| 113 | cache = rtnl_rtprot_tab[i]; |
| 114 | res = i; |
| 115 | *id = res; |
| 116 | return 0; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | res = strtoul(arg, &end, 0); |
| 121 | if (!end || end == arg || *end || res > 255) |
| 122 | return -1; |
| 123 | *id = res; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | static char * rtnl_rtscope_tab[256] = { |
| 130 | "global", |
| 131 | }; |
| 132 | |
| 133 | static int rtnl_rtscope_init; |
| 134 | |
| 135 | static void rtnl_rtscope_initialize(void) |
| 136 | { |
| 137 | rtnl_rtscope_init = 1; |
| 138 | rtnl_rtscope_tab[255] = "nowhere"; |
| 139 | rtnl_rtscope_tab[254] = "host"; |
| 140 | rtnl_rtscope_tab[253] = "link"; |
| 141 | rtnl_rtscope_tab[200] = "site"; |
| 142 | rtnl_tab_initialize("/etc/iproute2/rt_scopes", |
| 143 | rtnl_rtscope_tab, 256); |
| 144 | } |
| 145 | |
| 146 | char * rtnl_rtscope_n2a(int id, char *buf, int len) |
| 147 | { |
| 148 | if (id<0 || id>=256) { |
| 149 | snprintf(buf, len, "%d", id); |
| 150 | return buf; |
| 151 | } |
| 152 | if (!rtnl_rtscope_tab[id]) { |
| 153 | if (!rtnl_rtscope_init) |
| 154 | rtnl_rtscope_initialize(); |
| 155 | } |
| 156 | if (rtnl_rtscope_tab[id]) |
| 157 | return rtnl_rtscope_tab[id]; |
| 158 | snprintf(buf, len, "%d", id); |
| 159 | return buf; |
| 160 | } |
| 161 | |
| 162 | int rtnl_rtscope_a2n(uint32_t *id, char *arg) |
| 163 | { |
| 164 | static char *cache = NULL; |
| 165 | static unsigned long res; |
| 166 | char *end; |
| 167 | int i; |
| 168 | |
| 169 | if (cache && strcmp(cache, arg) == 0) { |
| 170 | *id = res; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | if (!rtnl_rtscope_init) |
| 175 | rtnl_rtscope_initialize(); |
| 176 | |
| 177 | for (i=0; i<256; i++) { |
| 178 | if (rtnl_rtscope_tab[i] && |
| 179 | strcmp(rtnl_rtscope_tab[i], arg) == 0) { |
| 180 | cache = rtnl_rtscope_tab[i]; |
| 181 | res = i; |
| 182 | *id = res; |
| 183 | return 0; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | res = strtoul(arg, &end, 0); |
| 188 | if (!end || end == arg || *end || res > 255) |
| 189 | return -1; |
| 190 | *id = res; |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | static char * rtnl_rtrealm_tab[256] = { |
| 197 | "unknown", |
| 198 | }; |
| 199 | |
| 200 | static int rtnl_rtrealm_init; |
| 201 | |
| 202 | static void rtnl_rtrealm_initialize(void) |
| 203 | { |
| 204 | rtnl_rtrealm_init = 1; |
| 205 | rtnl_tab_initialize("/etc/iproute2/rt_realms", |
| 206 | rtnl_rtrealm_tab, 256); |
| 207 | } |
| 208 | |
| 209 | char * rtnl_rtrealm_n2a(int id, char *buf, int len) |
| 210 | { |
| 211 | if (id<0 || id>=256) { |
| 212 | snprintf(buf, len, "%d", id); |
| 213 | return buf; |
| 214 | } |
| 215 | if (!rtnl_rtrealm_tab[id]) { |
| 216 | if (!rtnl_rtrealm_init) |
| 217 | rtnl_rtrealm_initialize(); |
| 218 | } |
| 219 | if (rtnl_rtrealm_tab[id]) |
| 220 | return rtnl_rtrealm_tab[id]; |
| 221 | snprintf(buf, len, "%d", id); |
| 222 | return buf; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | int rtnl_rtrealm_a2n(uint32_t *id, char *arg) |
| 227 | { |
| 228 | static char *cache = NULL; |
| 229 | static unsigned long res; |
| 230 | char *end; |
| 231 | int i; |
| 232 | |
| 233 | if (cache && strcmp(cache, arg) == 0) { |
| 234 | *id = res; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | if (!rtnl_rtrealm_init) |
| 239 | rtnl_rtrealm_initialize(); |
| 240 | |
| 241 | for (i=0; i<256; i++) { |
| 242 | if (rtnl_rtrealm_tab[i] && |
| 243 | strcmp(rtnl_rtrealm_tab[i], arg) == 0) { |
| 244 | cache = rtnl_rtrealm_tab[i]; |
| 245 | res = i; |
| 246 | *id = res; |
| 247 | return 0; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | res = strtoul(arg, &end, 0); |
| 252 | if (!end || end == arg || *end || res > 255) |
| 253 | return -1; |
| 254 | *id = res; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | static char * rtnl_rttable_tab[256] = { |
| 261 | "unspec", |
| 262 | }; |
| 263 | |
| 264 | static int rtnl_rttable_init; |
| 265 | |
| 266 | static void rtnl_rttable_initialize(void) |
| 267 | { |
| 268 | rtnl_rttable_init = 1; |
| 269 | rtnl_rttable_tab[255] = "local"; |
| 270 | rtnl_rttable_tab[254] = "main"; |
| 271 | rtnl_tab_initialize("/etc/iproute2/rt_tables", |
| 272 | rtnl_rttable_tab, 256); |
| 273 | } |
| 274 | |
| 275 | char * rtnl_rttable_n2a(int id, char *buf, int len) |
| 276 | { |
| 277 | if (id<0 || id>=256) { |
| 278 | snprintf(buf, len, "%d", id); |
| 279 | return buf; |
| 280 | } |
| 281 | if (!rtnl_rttable_tab[id]) { |
| 282 | if (!rtnl_rttable_init) |
| 283 | rtnl_rttable_initialize(); |
| 284 | } |
| 285 | if (rtnl_rttable_tab[id]) |
| 286 | return rtnl_rttable_tab[id]; |
| 287 | snprintf(buf, len, "%d", id); |
| 288 | return buf; |
| 289 | } |
| 290 | |
| 291 | int rtnl_rttable_a2n(uint32_t *id, char *arg) |
| 292 | { |
| 293 | static char *cache = NULL; |
| 294 | static unsigned long res; |
| 295 | char *end; |
| 296 | int i; |
| 297 | |
| 298 | if (cache && strcmp(cache, arg) == 0) { |
| 299 | *id = res; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | if (!rtnl_rttable_init) |
| 304 | rtnl_rttable_initialize(); |
| 305 | |
| 306 | for (i=0; i<256; i++) { |
| 307 | if (rtnl_rttable_tab[i] && |
| 308 | strcmp(rtnl_rttable_tab[i], arg) == 0) { |
| 309 | cache = rtnl_rttable_tab[i]; |
| 310 | res = i; |
| 311 | *id = res; |
| 312 | return 0; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | i = strtoul(arg, &end, 0); |
| 317 | if (!end || end == arg || *end || i > 255) |
| 318 | return -1; |
| 319 | *id = i; |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | static char * rtnl_rtdsfield_tab[256] = { |
| 325 | "0", |
| 326 | }; |
| 327 | |
| 328 | static int rtnl_rtdsfield_init; |
| 329 | |
| 330 | static void rtnl_rtdsfield_initialize(void) |
| 331 | { |
| 332 | rtnl_rtdsfield_init = 1; |
| 333 | rtnl_tab_initialize("/etc/iproute2/rt_dsfield", |
| 334 | rtnl_rtdsfield_tab, 256); |
| 335 | } |
| 336 | |
| 337 | char * rtnl_dsfield_n2a(int id, char *buf, int len) |
| 338 | { |
| 339 | if (id<0 || id>=256) { |
| 340 | snprintf(buf, len, "%d", id); |
| 341 | return buf; |
| 342 | } |
| 343 | if (!rtnl_rtdsfield_tab[id]) { |
| 344 | if (!rtnl_rtdsfield_init) |
| 345 | rtnl_rtdsfield_initialize(); |
| 346 | } |
| 347 | if (rtnl_rtdsfield_tab[id]) |
| 348 | return rtnl_rtdsfield_tab[id]; |
| 349 | snprintf(buf, len, "0x%02x", id); |
| 350 | return buf; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | int rtnl_dsfield_a2n(uint32_t *id, char *arg) |
| 355 | { |
| 356 | static char *cache = NULL; |
| 357 | static unsigned long res; |
| 358 | char *end; |
| 359 | int i; |
| 360 | |
| 361 | if (cache && strcmp(cache, arg) == 0) { |
| 362 | *id = res; |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | if (!rtnl_rtdsfield_init) |
| 367 | rtnl_rtdsfield_initialize(); |
| 368 | |
| 369 | for (i=0; i<256; i++) { |
| 370 | if (rtnl_rtdsfield_tab[i] && |
| 371 | strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { |
| 372 | cache = rtnl_rtdsfield_tab[i]; |
| 373 | res = i; |
| 374 | *id = res; |
| 375 | return 0; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | res = strtoul(arg, &end, 16); |
| 380 | if (!end || end == arg || *end || res > 255) |
| 381 | return -1; |
| 382 | *id = res; |
| 383 | return 0; |
| 384 | } |
| 385 | |