diff options
Diffstat (limited to 'networking/libiproute')
-rw-r--r-- | networking/libiproute/rt_names.c | 190 |
1 files changed, 85 insertions, 105 deletions
diff --git a/networking/libiproute/rt_names.c b/networking/libiproute/rt_names.c index ed21fbe26..69eb55e79 100644 --- a/networking/libiproute/rt_names.c +++ b/networking/libiproute/rt_names.c @@ -9,11 +9,8 @@ * * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdint.h> +#include "libbb.h" #include "rt_names.h" static void rtnl_tab_initialize(char *file, const char **tab, int size) @@ -37,57 +34,56 @@ static void rtnl_tab_initialize(char *file, const char **tab, int size) sscanf(p, "0x%x %s #", &id, namebuf) != 2 && sscanf(p, "%d %s\n", &id, namebuf) != 2 && sscanf(p, "%d %s #", &id, namebuf) != 2) { - fprintf(stderr, "Database %s is corrupted at %s\n", + bb_error_msg("database %s is corrupted at %s", file, p); return; } - if (id<0 || id>size) + if (id < 0 || id > size) continue; - tab[id] = strdup(namebuf); + tab[id] = xstrdup(namebuf); } fclose(fp); } -static const char * rtnl_rtprot_tab[256] = { - "none", - "redirect", - "kernel", - "boot", - "static", - NULL, - NULL, - NULL, - "gated", - "ra", - "mrt", - "zebra", - "bird", -}; - - - -static int rtnl_rtprot_init; +static const char **rtnl_rtprot_tab; /* [256] */ static void rtnl_rtprot_initialize(void) { - rtnl_rtprot_init = 1; + 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) + +const char* rtnl_rtprot_n2a(int id, char *buf, int len) { - if (id<0 || id>=256) { + if (id < 0 || id >= 256) { snprintf(buf, len, "%d", id); return buf; } - if (!rtnl_rtprot_tab[id]) { - if (!rtnl_rtprot_init) - rtnl_rtprot_initialize(); - } + + rtnl_rtprot_initialize(); + if (rtnl_rtprot_tab[id]) return rtnl_rtprot_tab[id]; snprintf(buf, len, "%d", id); @@ -98,7 +94,6 @@ int rtnl_rtprot_a2n(uint32_t *id, char *arg) { static const char *cache = NULL; static unsigned long res; - char *end; int i; if (cache && strcmp(cache, arg) == 0) { @@ -106,10 +101,9 @@ int rtnl_rtprot_a2n(uint32_t *id, char *arg) return 0; } - if (!rtnl_rtprot_init) - rtnl_rtprot_initialize(); + rtnl_rtprot_initialize(); - for (i=0; i<256; i++) { + for (i = 0; i < 256; i++) { if (rtnl_rtprot_tab[i] && strcmp(rtnl_rtprot_tab[i], arg) == 0) { cache = rtnl_rtprot_tab[i]; @@ -119,24 +113,21 @@ int rtnl_rtprot_a2n(uint32_t *id, char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + res = bb_strtoul(arg, NULL, 0); + if (errno || res > 255) return -1; *id = res; return 0; } - -static const char * rtnl_rtscope_tab[256] = { - "global", -}; - -static int rtnl_rtscope_init; +static const char **rtnl_rtscope_tab; /* [256] */ static void rtnl_rtscope_initialize(void) { - rtnl_rtscope_init = 1; + 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"; @@ -145,16 +136,16 @@ static void rtnl_rtscope_initialize(void) rtnl_rtscope_tab, 256); } -const char * rtnl_rtscope_n2a(int id, char *buf, int len) + +const char* rtnl_rtscope_n2a(int id, char *buf, int len) { - if (id<0 || id>=256) { + if (id < 0 || id >= 256) { snprintf(buf, len, "%d", id); return buf; } - if (!rtnl_rtscope_tab[id]) { - if (!rtnl_rtscope_init) - rtnl_rtscope_initialize(); - } + + rtnl_rtscope_initialize(); + if (rtnl_rtscope_tab[id]) return rtnl_rtscope_tab[id]; snprintf(buf, len, "%d", id); @@ -165,7 +156,6 @@ int rtnl_rtscope_a2n(uint32_t *id, char *arg) { static const char *cache = NULL; static unsigned long res; - char *end; int i; if (cache && strcmp(cache, arg) == 0) { @@ -173,10 +163,9 @@ int rtnl_rtscope_a2n(uint32_t *id, char *arg) return 0; } - if (!rtnl_rtscope_init) - rtnl_rtscope_initialize(); + rtnl_rtscope_initialize(); - for (i=0; i<256; i++) { + for (i = 0; i < 256; i++) { if (rtnl_rtscope_tab[i] && strcmp(rtnl_rtscope_tab[i], arg) == 0) { cache = rtnl_rtscope_tab[i]; @@ -186,33 +175,30 @@ int rtnl_rtscope_a2n(uint32_t *id, char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + res = bb_strtoul(arg, NULL, 0); + if (errno || res > 255) return -1; *id = res; return 0; } - -static const char * rtnl_rtrealm_tab[256] = { - "unknown", -}; - -static int rtnl_rtrealm_init; +static const char **rtnl_rtrealm_tab; /* [256] */ static void rtnl_rtrealm_initialize(void) { - rtnl_rtrealm_init = 1; + 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; - char *end; int i; if (cache && strcmp(cache, arg) == 0) { @@ -220,10 +206,9 @@ int rtnl_rtrealm_a2n(uint32_t *id, char *arg) return 0; } - if (!rtnl_rtrealm_init) - rtnl_rtrealm_initialize(); + rtnl_rtrealm_initialize(); - for (i=0; i<256; i++) { + for (i = 0; i < 256; i++) { if (rtnl_rtrealm_tab[i] && strcmp(rtnl_rtrealm_tab[i], arg) == 0) { cache = rtnl_rtrealm_tab[i]; @@ -233,24 +218,23 @@ int rtnl_rtrealm_a2n(uint32_t *id, char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + 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) +const char* rtnl_rtrealm_n2a(int id, char *buf, int len) { if (id<0 || id>=256) { snprintf(buf, len, "%d", id); return buf; } - if (!rtnl_rtrealm_tab[id]) { - if (!rtnl_rtrealm_init) - rtnl_rtrealm_initialize(); - } + + rtnl_rtrealm_initialize(); + if (rtnl_rtrealm_tab[id]) return rtnl_rtrealm_tab[id]; snprintf(buf, len, "%d", id); @@ -258,29 +242,28 @@ const char * rtnl_rtrealm_n2a(int id, char *buf, int len) } #endif -static const char * rtnl_rtdsfield_tab[256] = { - "0", -}; -static int rtnl_rtdsfield_init; +static const char **rtnl_rtdsfield_tab; /* [256] */ static void rtnl_rtdsfield_initialize(void) { - rtnl_rtdsfield_init = 1; + 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; } - if (!rtnl_rtdsfield_tab[id]) { - if (!rtnl_rtdsfield_init) - rtnl_rtdsfield_initialize(); - } + + rtnl_rtdsfield_initialize(); + if (rtnl_rtdsfield_tab[id]) return rtnl_rtdsfield_tab[id]; snprintf(buf, len, "0x%02x", id); @@ -292,7 +275,6 @@ int rtnl_dsfield_a2n(uint32_t *id, char *arg) { static const char *cache = NULL; static unsigned long res; - char *end; int i; if (cache && strcmp(cache, arg) == 0) { @@ -300,10 +282,9 @@ int rtnl_dsfield_a2n(uint32_t *id, char *arg) return 0; } - if (!rtnl_rtdsfield_init) - rtnl_rtdsfield_initialize(); + rtnl_rtdsfield_initialize(); - for (i=0; i<256; i++) { + for (i = 0; i < 256; i++) { if (rtnl_rtdsfield_tab[i] && strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { cache = rtnl_rtdsfield_tab[i]; @@ -313,37 +294,38 @@ int rtnl_dsfield_a2n(uint32_t *id, char *arg) } } - res = strtoul(arg, &end, 16); - if (!end || end == arg || *end || res > 255) + res = bb_strtoul(arg, NULL, 16); + if (errno || res > 255) return -1; *id = res; return 0; } + #if ENABLE_FEATURE_IP_RULE -static int rtnl_rttable_init; -static const char * rtnl_rttable_tab[256] = { - "unspec", -}; +static const char **rtnl_rttable_tab; /* [256] */ + static void rtnl_rttable_initialize(void) { - rtnl_rttable_init = 1; + 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; } - if (!rtnl_rttable_tab[id]) { - if (!rtnl_rttable_init) - rtnl_rttable_initialize(); - } + + rtnl_rttable_initialize(); + if (rtnl_rttable_tab[id]) return rtnl_rttable_tab[id]; snprintf(buf, len, "%d", id); @@ -354,7 +336,6 @@ int rtnl_rttable_a2n(uint32_t * id, char *arg) { static char *cache = NULL; static unsigned long res; - char *end; int i; if (cache && strcmp(cache, arg) == 0) { @@ -362,8 +343,7 @@ int rtnl_rttable_a2n(uint32_t * id, char *arg) return 0; } - if (!rtnl_rttable_init) - rtnl_rttable_initialize(); + rtnl_rttable_initialize(); for (i = 0; i < 256; i++) { if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) { @@ -374,8 +354,8 @@ int rtnl_rttable_a2n(uint32_t * id, char *arg) } } - i = strtoul(arg, &end, 0); - if (!end || end == arg || *end || i > 255) + i = bb_strtoul(arg, NULL, 0); + if (errno || i > 255) return -1; *id = i; return 0; |