2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 01:33:55 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
*/
|
2002-12-16 07:37:21 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
#include "libbb.h"
|
2005-10-08 20:47:15 +00:00
|
|
|
#include "rt_names.h"
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2008-07-24 23:38:04 +00:00
|
|
|
/* so far all callers have size == 256 */
|
|
|
|
#define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab)
|
|
|
|
#define size 256
|
2007-01-29 22:51:58 +00:00
|
|
|
static void rtnl_tab_initialize(const char *file, const char **tab, int size)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2008-07-24 23:38:04 +00:00
|
|
|
char *token[2];
|
|
|
|
parser_t *parser = config_open2(file, fopen_for_read);
|
2008-07-26 23:08:31 +00:00
|
|
|
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
|
2008-07-24 23:38:04 +00:00
|
|
|
int id = bb_strtou(token[0], NULL, 0);
|
|
|
|
if (id < 0 || id > size) {
|
|
|
|
bb_error_msg("database %s is corrupted at line %d",
|
|
|
|
file, parser->lineno);
|
|
|
|
break;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2008-07-24 23:38:04 +00:00
|
|
|
tab[id] = xstrdup(token[1]);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2008-07-24 23:38:04 +00:00
|
|
|
config_close(parser);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2008-07-24 23:38:04 +00:00
|
|
|
#undef size
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
static const char **rtnl_rtprot_tab; /* [256] */
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
static void rtnl_rtprot_initialize(void)
|
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
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));
|
2002-11-10 01:33:55 +00:00
|
|
|
rtnl_tab_initialize("/etc/iproute2/rt_protos",
|
|
|
|
rtnl_rtprot_tab, 256);
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
const char* rtnl_rtprot_n2a(int id, char *buf, int len)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
if (id < 0 || id >= 256) {
|
2002-11-10 01:33:55 +00:00
|
|
|
snprintf(buf, len, "%d", id);
|
|
|
|
return buf;
|
|
|
|
}
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
rtnl_rtprot_initialize();
|
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
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)
|
|
|
|
{
|
2006-09-28 22:36:23 +00:00
|
|
|
static const char *cache = NULL;
|
2002-11-10 01:33:55 +00:00
|
|
|
static unsigned long res;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (cache && strcmp(cache, arg) == 0) {
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
rtnl_rtprot_initialize();
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2002-11-10 01:33:55 +00:00
|
|
|
if (rtnl_rtprot_tab[i] &&
|
|
|
|
strcmp(rtnl_rtprot_tab[i], arg) == 0) {
|
|
|
|
cache = rtnl_rtprot_tab[i];
|
|
|
|
res = i;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
res = bb_strtoul(arg, NULL, 0);
|
|
|
|
if (errno || res > 255)
|
2002-11-10 01:33:55 +00:00
|
|
|
return -1;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
static const char **rtnl_rtscope_tab; /* [256] */
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
static void rtnl_rtscope_initialize(void)
|
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
if (rtnl_rtscope_tab) return;
|
|
|
|
rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
|
|
|
|
rtnl_rtscope_tab[0] = "global";
|
2002-11-10 01:33:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
const char* rtnl_rtscope_n2a(int id, char *buf, int len)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
if (id < 0 || id >= 256) {
|
2002-11-10 01:33:55 +00:00
|
|
|
snprintf(buf, len, "%d", id);
|
|
|
|
return buf;
|
|
|
|
}
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
rtnl_rtscope_initialize();
|
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
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)
|
|
|
|
{
|
2006-09-28 22:36:23 +00:00
|
|
|
static const char *cache = NULL;
|
2002-11-10 01:33:55 +00:00
|
|
|
static unsigned long res;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (cache && strcmp(cache, arg) == 0) {
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
rtnl_rtscope_initialize();
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2002-11-10 01:33:55 +00:00
|
|
|
if (rtnl_rtscope_tab[i] &&
|
|
|
|
strcmp(rtnl_rtscope_tab[i], arg) == 0) {
|
|
|
|
cache = rtnl_rtscope_tab[i];
|
|
|
|
res = i;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
res = bb_strtoul(arg, NULL, 0);
|
|
|
|
if (errno || res > 255)
|
2002-11-10 01:33:55 +00:00
|
|
|
return -1;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
static const char **rtnl_rtrealm_tab; /* [256] */
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
static void rtnl_rtrealm_initialize(void)
|
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
if (rtnl_rtrealm_tab) return;
|
|
|
|
rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
|
|
|
|
rtnl_rtrealm_tab[0] = "unknown";
|
2002-11-10 01:33:55 +00:00
|
|
|
rtnl_tab_initialize("/etc/iproute2/rt_realms",
|
|
|
|
rtnl_rtrealm_tab, 256);
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
|
|
|
|
{
|
2006-09-28 22:36:23 +00:00
|
|
|
static const char *cache = NULL;
|
2002-11-10 01:33:55 +00:00
|
|
|
static unsigned long res;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (cache && strcmp(cache, arg) == 0) {
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
rtnl_rtrealm_initialize();
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2002-11-10 01:33:55 +00:00
|
|
|
if (rtnl_rtrealm_tab[i] &&
|
|
|
|
strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
|
|
|
|
cache = rtnl_rtrealm_tab[i];
|
|
|
|
res = i;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
res = bb_strtoul(arg, NULL, 0);
|
|
|
|
if (errno || res > 255)
|
2002-11-10 01:33:55 +00:00
|
|
|
return -1;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
2006-12-31 20:40:20 +00:00
|
|
|
const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
|
2006-11-21 15:36:08 +00:00
|
|
|
{
|
2007-01-01 06:00:38 +00:00
|
|
|
if (id < 0 || id >= 256) {
|
2006-11-21 15:36:08 +00:00
|
|
|
snprintf(buf, len, "%d", id);
|
|
|
|
return buf;
|
|
|
|
}
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
rtnl_rtrealm_initialize();
|
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
if (rtnl_rtrealm_tab[id])
|
|
|
|
return rtnl_rtrealm_tab[id];
|
|
|
|
snprintf(buf, len, "%d", id);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
#endif
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
static const char **rtnl_rtdsfield_tab; /* [256] */
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
static void rtnl_rtdsfield_initialize(void)
|
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
if (rtnl_rtdsfield_tab) return;
|
|
|
|
rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
|
|
|
|
rtnl_rtdsfield_tab[0] = "0";
|
2002-11-10 01:33:55 +00:00
|
|
|
rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
|
|
|
|
rtnl_rtdsfield_tab, 256);
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
|
2005-04-16 19:39:00 +00:00
|
|
|
const char * rtnl_dsfield_n2a(int id, char *buf, int len)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-01-01 06:00:38 +00:00
|
|
|
if (id < 0 || id >= 256) {
|
2002-11-10 01:33:55 +00:00
|
|
|
snprintf(buf, len, "%d", id);
|
|
|
|
return buf;
|
|
|
|
}
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
rtnl_rtdsfield_initialize();
|
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
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)
|
|
|
|
{
|
2006-09-28 22:36:23 +00:00
|
|
|
static const char *cache = NULL;
|
2002-11-10 01:33:55 +00:00
|
|
|
static unsigned long res;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (cache && strcmp(cache, arg) == 0) {
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
rtnl_rtdsfield_initialize();
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
2002-11-10 01:33:55 +00:00
|
|
|
if (rtnl_rtdsfield_tab[i] &&
|
|
|
|
strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
|
|
|
|
cache = rtnl_rtdsfield_tab[i];
|
|
|
|
res = i;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
res = bb_strtoul(arg, NULL, 16);
|
|
|
|
if (errno || res > 255)
|
2002-11-10 01:33:55 +00:00
|
|
|
return -1;
|
|
|
|
*id = res;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-21 15:36:08 +00:00
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
2006-12-31 20:40:20 +00:00
|
|
|
static const char **rtnl_rttable_tab; /* [256] */
|
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
static void rtnl_rttable_initialize(void)
|
|
|
|
{
|
2006-12-31 20:40:20 +00:00
|
|
|
if (rtnl_rtdsfield_tab) return;
|
|
|
|
rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
|
|
|
|
rtnl_rttable_tab[0] = "unspec";
|
2006-11-21 15:36:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
const char *rtnl_rttable_n2a(int id, char *buf, int len)
|
|
|
|
{
|
|
|
|
if (id < 0 || id >= 256) {
|
|
|
|
snprintf(buf, len, "%d", id);
|
|
|
|
return buf;
|
|
|
|
}
|
2006-12-31 20:40:20 +00:00
|
|
|
|
|
|
|
rtnl_rttable_initialize();
|
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
rtnl_rttable_initialize();
|
2006-11-21 15:36:08 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-31 20:40:20 +00:00
|
|
|
i = bb_strtoul(arg, NULL, 0);
|
|
|
|
if (errno || i > 255)
|
2006-11-21 15:36:08 +00:00
|
|
|
return -1;
|
|
|
|
*id = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|