123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- /* vi: set sw=4 ts=4: */
- /*
- * rt_names.c rtnetlink names DB.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version
- * 2 of the License, or (at your option) any later version.
- *
- * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
- */
- #include "libbb.h"
- #include "rt_names.h"
- static void rtnl_tab_initialize(const char *file, const char **tab, int size)
- {
- char buf[512];
- FILE *fp;
- fp = fopen(file, "r");
- if (!fp)
- return;
- while (fgets(buf, sizeof(buf), fp)) {
- char *p = buf;
- int id;
- char namebuf[512];
- while (*p == ' ' || *p == '\t')
- p++;
- if (*p == '#' || *p == '\n' || *p == 0)
- continue;
- if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2
- && sscanf(p, "0x%x %s #", &id, namebuf) != 2
- && sscanf(p, "%d %s\n", &id, namebuf) != 2
- && sscanf(p, "%d %s #", &id, namebuf) != 2
- ) {
- bb_error_msg("database %s is corrupted at %s",
- file, p);
- return;
- }
- if (id < 0 || id > size)
- continue;
- tab[id] = xstrdup(namebuf);
- }
- fclose(fp);
- }
- static const char **rtnl_rtprot_tab; /* [256] */
- static void rtnl_rtprot_initialize(void)
- {
- static const char *const init_tab[] = {
- "none",
- "redirect",
- "kernel",
- "boot",
- "static",
- NULL,
- NULL,
- NULL,
- "gated",
- "ra",
- "mrt",
- "zebra",
- "bird",
- };
- if (rtnl_rtprot_tab) return;
- rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
- memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
- rtnl_tab_initialize("/etc/iproute2/rt_protos",
- rtnl_rtprot_tab, 256);
- }
- const char* rtnl_rtprot_n2a(int id, char *buf, int len)
- {
- if (id < 0 || id >= 256) {
- snprintf(buf, len, "%d", id);
- return buf;
- }
- rtnl_rtprot_initialize();
- if (rtnl_rtprot_tab[id])
- return rtnl_rtprot_tab[id];
- snprintf(buf, len, "%d", id);
- return buf;
- }
- int rtnl_rtprot_a2n(uint32_t *id, char *arg)
- {
- static const char *cache = NULL;
- static unsigned long res;
- int i;
- if (cache && strcmp(cache, arg) == 0) {
- *id = res;
- return 0;
- }
- rtnl_rtprot_initialize();
- for (i = 0; i < 256; i++) {
- if (rtnl_rtprot_tab[i] &&
- strcmp(rtnl_rtprot_tab[i], arg) == 0) {
- cache = rtnl_rtprot_tab[i];
- res = i;
- *id = res;
- return 0;
- }
- }
- res = bb_strtoul(arg, NULL, 0);
- if (errno || res > 255)
- return -1;
- *id = res;
- return 0;
- }
- static const char **rtnl_rtscope_tab; /* [256] */
- static void rtnl_rtscope_initialize(void)
- {
- if (rtnl_rtscope_tab) return;
- rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
- rtnl_rtscope_tab[0] = "global";
- rtnl_rtscope_tab[255] = "nowhere";
- rtnl_rtscope_tab[254] = "host";
- rtnl_rtscope_tab[253] = "link";
- rtnl_rtscope_tab[200] = "site";
- rtnl_tab_initialize("/etc/iproute2/rt_scopes",
- rtnl_rtscope_tab, 256);
- }
- const char* rtnl_rtscope_n2a(int id, char *buf, int len)
- {
- if (id < 0 || id >= 256) {
- snprintf(buf, len, "%d", id);
- return buf;
- }
- rtnl_rtscope_initialize();
- if (rtnl_rtscope_tab[id])
- return rtnl_rtscope_tab[id];
- snprintf(buf, len, "%d", id);
- return buf;
- }
- int rtnl_rtscope_a2n(uint32_t *id, char *arg)
- {
- static const char *cache = NULL;
- static unsigned long res;
- int i;
- if (cache && strcmp(cache, arg) == 0) {
- *id = res;
- return 0;
- }
- rtnl_rtscope_initialize();
- for (i = 0; i < 256; i++) {
- if (rtnl_rtscope_tab[i] &&
- strcmp(rtnl_rtscope_tab[i], arg) == 0) {
- cache = rtnl_rtscope_tab[i];
- res = i;
- *id = res;
- return 0;
- }
- }
- res = bb_strtoul(arg, NULL, 0);
- if (errno || res > 255)
- return -1;
- *id = res;
- return 0;
- }
- static const char **rtnl_rtrealm_tab; /* [256] */
- static void rtnl_rtrealm_initialize(void)
- {
- if (rtnl_rtrealm_tab) return;
- rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
- rtnl_rtrealm_tab[0] = "unknown";
- rtnl_tab_initialize("/etc/iproute2/rt_realms",
- rtnl_rtrealm_tab, 256);
- }
- int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
- {
- static const char *cache = NULL;
- static unsigned long res;
- int i;
- if (cache && strcmp(cache, arg) == 0) {
- *id = res;
- return 0;
- }
- rtnl_rtrealm_initialize();
- for (i = 0; i < 256; i++) {
- if (rtnl_rtrealm_tab[i] &&
- strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
- cache = rtnl_rtrealm_tab[i];
- res = i;
- *id = res;
- return 0;
- }
- }
- res = bb_strtoul(arg, NULL, 0);
- if (errno || res > 255)
- return -1;
- *id = res;
- return 0;
- }
- #if ENABLE_FEATURE_IP_RULE
- const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
- {
- if (id < 0 || id >= 256) {
- snprintf(buf, len, "%d", id);
- return buf;
- }
- rtnl_rtrealm_initialize();
- if (rtnl_rtrealm_tab[id])
- return rtnl_rtrealm_tab[id];
- snprintf(buf, len, "%d", id);
- return buf;
- }
- #endif
- static const char **rtnl_rtdsfield_tab; /* [256] */
- static void rtnl_rtdsfield_initialize(void)
- {
- if (rtnl_rtdsfield_tab) return;
- rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
- rtnl_rtdsfield_tab[0] = "0";
- rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
- rtnl_rtdsfield_tab, 256);
- }
- const char * rtnl_dsfield_n2a(int id, char *buf, int len)
- {
- if (id < 0 || id >= 256) {
- snprintf(buf, len, "%d", id);
- return buf;
- }
- rtnl_rtdsfield_initialize();
- if (rtnl_rtdsfield_tab[id])
- return rtnl_rtdsfield_tab[id];
- snprintf(buf, len, "0x%02x", id);
- return buf;
- }
- int rtnl_dsfield_a2n(uint32_t *id, char *arg)
- {
- static const char *cache = NULL;
- static unsigned long res;
- int i;
- if (cache && strcmp(cache, arg) == 0) {
- *id = res;
- return 0;
- }
- rtnl_rtdsfield_initialize();
- for (i = 0; i < 256; i++) {
- if (rtnl_rtdsfield_tab[i] &&
- strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
- cache = rtnl_rtdsfield_tab[i];
- res = i;
- *id = res;
- return 0;
- }
- }
- res = bb_strtoul(arg, NULL, 16);
- if (errno || res > 255)
- return -1;
- *id = res;
- return 0;
- }
- #if ENABLE_FEATURE_IP_RULE
- static const char **rtnl_rttable_tab; /* [256] */
- static void rtnl_rttable_initialize(void)
- {
- if (rtnl_rtdsfield_tab) return;
- rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
- rtnl_rttable_tab[0] = "unspec";
- rtnl_rttable_tab[255] = "local";
- rtnl_rttable_tab[254] = "main";
- rtnl_rttable_tab[253] = "default";
- rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
- }
- const char *rtnl_rttable_n2a(int id, char *buf, int len)
- {
- if (id < 0 || id >= 256) {
- snprintf(buf, len, "%d", id);
- return buf;
- }
- rtnl_rttable_initialize();
- if (rtnl_rttable_tab[id])
- return rtnl_rttable_tab[id];
- snprintf(buf, len, "%d", id);
- return buf;
- }
- int rtnl_rttable_a2n(uint32_t * id, char *arg)
- {
- static char *cache = NULL;
- static unsigned long res;
- int i;
- if (cache && strcmp(cache, arg) == 0) {
- *id = res;
- return 0;
- }
- rtnl_rttable_initialize();
- for (i = 0; i < 256; i++) {
- if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
- cache = (char*)rtnl_rttable_tab[i];
- res = i;
- *id = res;
- return 0;
- }
- }
- i = bb_strtoul(arg, NULL, 0);
- if (errno || i > 255)
- return -1;
- *id = i;
- return 0;
- }
- #endif
|