ip link: add support for "address ETHADDR". Closes 4862
[oweals/busybox.git] / networking / libiproute / rt_names.c
index ff93c9e72a142fc177fb3084220d31d4ba7593f5..c474ab903e258f1b3b7d79c79270068a6982c4f1 100644 (file)
+/* 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.
  *
- *             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>
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "libbb.h"
+#include "rt_names.h"
 
-#include <stdint.h>
+typedef struct rtnl_tab_t {
+       const char *cached_str;
+       unsigned cached_result;
+       const char *tab[256];
+} rtnl_tab_t;
 
-static void rtnl_tab_initialize(char *file, char **tab, int size)
+static void rtnl_tab_initialize(const char *file, const char **tab)
 {
-       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) {
-                       fprintf(stderr, "Database %s is corrupted at %s\n",
-                               file, p);
-                       return;
+       char *token[2];
+       parser_t *parser = config_open2(file, fopen_for_read);
+
+       while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
+               unsigned id = bb_strtou(token[0], NULL, 0);
+               if (id > 256) {
+                       bb_error_msg("database %s is corrupted at line %d",
+                               file, parser->lineno);
+                       break;
                }
-
-               if (id<0 || id>size)
-                       continue;
-
-               tab[id] = strdup(namebuf);
-       }
-       fclose(fp);
-}
-
-
-static char * rtnl_rtprot_tab[256] = {
-       "none",
-       "redirect",
-       "kernel",
-       "boot",
-       "static",
-       NULL,
-       NULL,
-       NULL,
-       "gated",
-       "ra",
-       "mrt",
-       "zebra",
-       "bird",
-};
-
-
-
-static int rtnl_rtprot_init;
-
-static void rtnl_rtprot_initialize(void)
-{
-       rtnl_rtprot_init = 1;
-       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;
-       }
-       if (!rtnl_rtprot_tab[id]) {
-               if (!rtnl_rtprot_init)
-                       rtnl_rtprot_initialize();
+               tab[id] = xstrdup(token[1]);
        }
-       if (rtnl_rtprot_tab[id])
-               return rtnl_rtprot_tab[id];
-       snprintf(buf, len, "%d", id);
-       return buf;
+       config_close(parser);
 }
 
-int rtnl_rtprot_a2n(uint32_t *id, char *arg)
+static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
 {
-       static char *cache = NULL;
-       static unsigned long res;
-       char *end;
-       int i;
+       unsigned i;
 
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
+       if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
+               *id = tab->cached_result;
                return 0;
        }
 
-       if (!rtnl_rtprot_init)
-               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;
+       for (i = 0; i < 256; i++) {
+               if (tab->tab[i]
+                && strcmp(tab->tab[i], arg) == 0
+               ) {
+                       tab->cached_str = tab->tab[i];
+                       tab->cached_result = i;
+                       *id = i;
                        return 0;
                }
        }
 
-       res = strtoul(arg, &end, 0);
-       if (!end || end == arg || *end || res > 255)
+       i = bb_strtou(arg, NULL, base);
+       if (i > 255)
                return -1;
-       *id = res;
+       *id = i;
        return 0;
 }
 
 
+static rtnl_tab_t *rtnl_rtprot_tab;
 
-static char * rtnl_rtscope_tab[256] = {
-       "global",
-};
-
-static int rtnl_rtscope_init;
-
-static void rtnl_rtscope_initialize(void)
+static void rtnl_rtprot_initialize(void)
 {
-       rtnl_rtscope_init = 1;
-       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);
+       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(sizeof(*rtnl_rtprot_tab));
+       memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
+       rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
 }
 
-const char * rtnl_rtscope_n2a(int id, char *buf, int len)
+const char* FAST_FUNC rtnl_rtprot_n2a(int id, char *buf)
 {
-       if (id<0 || id>=256) {
-               snprintf(buf, len, "%d", id);
+       if (id < 0 || id >= 256) {
+               sprintf(buf, "%d", id);
                return buf;
        }
-       if (!rtnl_rtscope_tab[id]) {
-               if (!rtnl_rtscope_init)
-                       rtnl_rtscope_initialize();
-       }
-       if (rtnl_rtscope_tab[id])
-               return rtnl_rtscope_tab[id];
-       snprintf(buf, len, "%d", id);
+
+       rtnl_rtprot_initialize();
+
+       if (rtnl_rtprot_tab->tab[id])
+               return rtnl_rtprot_tab->tab[id];
+       /* buf is SPRINT_BSIZE big */
+       sprintf(buf, "%d", id);
        return buf;
 }
 
-int rtnl_rtscope_a2n(uint32_t *id, char *arg)
+int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
 {
-       static char *cache = NULL;
-       static unsigned long res;
-       char *end;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
-       if (!rtnl_rtscope_init)
-               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 = strtoul(arg, &end, 0);
-       if (!end || end == arg || *end || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+       rtnl_rtprot_initialize();
+       return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
 }
 
 
+static rtnl_tab_t *rtnl_rtscope_tab;
 
-static char * rtnl_rtrealm_tab[256] = {
-       "unknown",
-};
-
-static int rtnl_rtrealm_init;
-
-static void rtnl_rtrealm_initialize(void)
+static void rtnl_rtscope_initialize(void)
 {
-       rtnl_rtrealm_init = 1;
-       rtnl_tab_initialize("/etc/iproute2/rt_realms",
-                           rtnl_rtrealm_tab, 256);
+       if (rtnl_rtscope_tab)
+               return;
+       rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
+       rtnl_rtscope_tab->tab[0] = "global";
+       rtnl_rtscope_tab->tab[255] = "nowhere";
+       rtnl_rtscope_tab->tab[254] = "host";
+       rtnl_rtscope_tab->tab[253] = "link";
+       rtnl_rtscope_tab->tab[200] = "site";
+       rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
 }
 
-const char * rtnl_rtrealm_n2a(int id, char *buf, int len)
+const char* FAST_FUNC rtnl_rtscope_n2a(int id, char *buf)
 {
-       if (id<0 || id>=256) {
-               snprintf(buf, len, "%d", id);
+       if (id < 0 || id >= 256) {
+               sprintf(buf, "%d", id);
                return buf;
        }
-       if (!rtnl_rtrealm_tab[id]) {
-               if (!rtnl_rtrealm_init)
-                       rtnl_rtrealm_initialize();
-       }
-       if (rtnl_rtrealm_tab[id])
-               return rtnl_rtrealm_tab[id];
-       snprintf(buf, len, "%d", id);
+
+       rtnl_rtscope_initialize();
+
+       if (rtnl_rtscope_tab->tab[id])
+               return rtnl_rtscope_tab->tab[id];
+       /* buf is SPRINT_BSIZE big */
+       sprintf(buf, "%d", id);
        return buf;
 }
 
-
-int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
+int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
 {
-       static char *cache = NULL;
-       static unsigned long res;
-       char *end;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
-       }
-
-       if (!rtnl_rtrealm_init)
-               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 = strtoul(arg, &end, 0);
-       if (!end || end == arg || *end || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+       rtnl_rtscope_initialize();
+       return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
 }
 
 
+static rtnl_tab_t *rtnl_rtrealm_tab;
 
-static char * rtnl_rttable_tab[256] = {
-       "unspec",
-};
-
-static int rtnl_rttable_init;
-
-static void rtnl_rttable_initialize(void)
+static void rtnl_rtrealm_initialize(void)
 {
-       rtnl_rttable_init = 1;
-       rtnl_rttable_tab[255] = "local";
-       rtnl_rttable_tab[254] = "main";
-       rtnl_tab_initialize("/etc/iproute2/rt_tables",
-                           rtnl_rttable_tab, 256);
+       if (rtnl_rtrealm_tab) return;
+       rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
+       rtnl_rtrealm_tab->tab[0] = "unknown";
+       rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
 }
 
-const char * rtnl_rttable_n2a(int id, char *buf, int len)
+int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
 {
-       if (id<0 || id>=256) {
-               snprintf(buf, len, "%d", id);
-               return buf;
-       }
-       if (!rtnl_rttable_tab[id]) {
-               if (!rtnl_rttable_init)
-                       rtnl_rttable_initialize();
-       }
-       if (rtnl_rttable_tab[id])
-               return rtnl_rttable_tab[id];
-       snprintf(buf, len, "%d", id);
-       return buf;
+       rtnl_rtrealm_initialize();
+       return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
 }
 
-int rtnl_rttable_a2n(uint32_t *id, char *arg)
+#if ENABLE_FEATURE_IP_RULE
+const char* FAST_FUNC rtnl_rtrealm_n2a(int id, char *buf)
 {
-       static char *cache = NULL;
-       static unsigned long res;
-       char *end;
-       int i;
-
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
+       if (id < 0 || id >= 256) {
+               sprintf(buf, "%d", id);
+               return buf;
        }
 
-       if (!rtnl_rttable_init)
-               rtnl_rttable_initialize();
-
-       for (i=0; i<256; i++) {
-               if (rtnl_rttable_tab[i] &&
-                   strcmp(rtnl_rttable_tab[i], arg) == 0) {
-                       cache = rtnl_rttable_tab[i];
-                       res = i;
-                       *id = res;
-                       return 0;
-               }
-       }
+       rtnl_rtrealm_initialize();
 
-       i = strtoul(arg, &end, 0);
-       if (!end || end == arg || *end || i > 255)
-               return -1;
-       *id = i;
-       return 0;
+       if (rtnl_rtrealm_tab->tab[id])
+               return rtnl_rtrealm_tab->tab[id];
+       /* buf is SPRINT_BSIZE big */
+       sprintf(buf, "%d", id);
+       return buf;
 }
+#endif
 
 
-static char * rtnl_rtdsfield_tab[256] = {
-       "0",
-};
-
-static int rtnl_rtdsfield_init;
+static rtnl_tab_t *rtnl_rtdsfield_tab;
 
 static void rtnl_rtdsfield_initialize(void)
 {
-       rtnl_rtdsfield_init = 1;
-       rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
-                           rtnl_rtdsfield_tab, 256);
+       if (rtnl_rtdsfield_tab) return;
+       rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
+       rtnl_rtdsfield_tab->tab[0] = "0";
+       rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
 }
 
-const char * rtnl_dsfield_n2a(int id, char *buf, int len)
+const char* FAST_FUNC rtnl_dsfield_n2a(int id, char *buf)
 {
-       if (id<0 || id>=256) {
-               snprintf(buf, len, "%d", id);
+       if (id < 0 || id >= 256) {
+               sprintf(buf, "%d", id);
                return buf;
        }
-       if (!rtnl_rtdsfield_tab[id]) {
-               if (!rtnl_rtdsfield_init)
-                       rtnl_rtdsfield_initialize();
-       }
-       if (rtnl_rtdsfield_tab[id])
-               return rtnl_rtdsfield_tab[id];
-       snprintf(buf, len, "0x%02x", id);
+
+       rtnl_rtdsfield_initialize();
+
+       if (rtnl_rtdsfield_tab->tab[id])
+               return rtnl_rtdsfield_tab->tab[id];
+       /* buf is SPRINT_BSIZE big */
+       sprintf(buf, "0x%02x", id);
        return buf;
 }
 
+int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
+{
+       rtnl_rtdsfield_initialize();
+       return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
+}
+
+
+#if ENABLE_FEATURE_IP_RULE
+static rtnl_tab_t *rtnl_rttable_tab;
 
-int rtnl_dsfield_a2n(uint32_t *id, char *arg)
+static void rtnl_rttable_initialize(void)
 {
-       static char *cache = NULL;
-       static unsigned long res;
-       char *end;
-       int i;
+       if (rtnl_rtdsfield_tab) return;
+       rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
+       rtnl_rttable_tab->tab[0] = "unspec";
+       rtnl_rttable_tab->tab[255] = "local";
+       rtnl_rttable_tab->tab[254] = "main";
+       rtnl_rttable_tab->tab[253] = "default";
+       rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
+}
 
-       if (cache && strcmp(cache, arg) == 0) {
-               *id = res;
-               return 0;
+const char* FAST_FUNC rtnl_rttable_n2a(int id, char *buf)
+{
+       if (id < 0 || id >= 256) {
+               sprintf(buf, "%d", id);
+               return buf;
        }
 
-       if (!rtnl_rtdsfield_init)
-               rtnl_rtdsfield_initialize();
+       rtnl_rttable_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;
-               }
-       }
+       if (rtnl_rttable_tab->tab[id])
+               return rtnl_rttable_tab->tab[id];
+       /* buf is SPRINT_BSIZE big */
+       sprintf(buf, "%d", id);
+       return buf;
+}
 
-       res = strtoul(arg, &end, 16);
-       if (!end || end == arg || *end || res > 255)
-               return -1;
-       *id = res;
-       return 0;
+int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
+{
+       rtnl_rttable_initialize();
+       return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
 }
 
+#endif