"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * rt_names.c rtnetlink names DB. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 11 | */ |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Bernhard Reutner-Fischer | 1d62d3b | 2005-10-08 20:47:15 +0000 | [diff] [blame] | 14 | #include "rt_names.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 16 | /* so far all callers have size == 256 */ |
| 17 | #define rtnl_tab_initialize(file, tab, size) rtnl_tab_initialize(file, tab) |
| 18 | #define size 256 |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 19 | static void rtnl_tab_initialize(const char *file, const char **tab, int size) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | { |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 21 | char *token[2]; |
| 22 | parser_t *parser = config_open2(file, fopen_for_read); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 23 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 24 | int id = bb_strtou(token[0], NULL, 0); |
| 25 | if (id < 0 || id > size) { |
| 26 | bb_error_msg("database %s is corrupted at line %d", |
| 27 | file, parser->lineno); |
| 28 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 29 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 30 | tab[id] = xstrdup(token[1]); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 31 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 32 | config_close(parser); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 33 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 34 | #undef size |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 36 | static const char **rtnl_rtprot_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 37 | |
| 38 | static void rtnl_rtprot_initialize(void) |
| 39 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 40 | static const char *const init_tab[] = { |
| 41 | "none", |
| 42 | "redirect", |
| 43 | "kernel", |
| 44 | "boot", |
| 45 | "static", |
| 46 | NULL, |
| 47 | NULL, |
| 48 | NULL, |
| 49 | "gated", |
| 50 | "ra", |
| 51 | "mrt", |
| 52 | "zebra", |
| 53 | "bird", |
| 54 | }; |
| 55 | if (rtnl_rtprot_tab) return; |
| 56 | rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0])); |
| 57 | memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 58 | rtnl_tab_initialize("/etc/iproute2/rt_protos", |
| 59 | rtnl_rtprot_tab, 256); |
| 60 | } |
| 61 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 62 | |
| 63 | const char* rtnl_rtprot_n2a(int id, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 64 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 65 | if (id < 0 || id >= 256) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | snprintf(buf, len, "%d", id); |
| 67 | return buf; |
| 68 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 69 | |
| 70 | rtnl_rtprot_initialize(); |
| 71 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 72 | if (rtnl_rtprot_tab[id]) |
| 73 | return rtnl_rtprot_tab[id]; |
| 74 | snprintf(buf, len, "%d", id); |
| 75 | return buf; |
| 76 | } |
| 77 | |
| 78 | int rtnl_rtprot_a2n(uint32_t *id, char *arg) |
| 79 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 80 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 81 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 82 | int i; |
| 83 | |
| 84 | if (cache && strcmp(cache, arg) == 0) { |
| 85 | *id = res; |
| 86 | return 0; |
| 87 | } |
| 88 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 89 | rtnl_rtprot_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 91 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 92 | if (rtnl_rtprot_tab[i] && |
| 93 | strcmp(rtnl_rtprot_tab[i], arg) == 0) { |
| 94 | cache = rtnl_rtprot_tab[i]; |
| 95 | res = i; |
| 96 | *id = res; |
| 97 | return 0; |
| 98 | } |
| 99 | } |
| 100 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 101 | res = bb_strtoul(arg, NULL, 0); |
| 102 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 103 | return -1; |
| 104 | *id = res; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 109 | static const char **rtnl_rtscope_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | |
| 111 | static void rtnl_rtscope_initialize(void) |
| 112 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 113 | if (rtnl_rtscope_tab) return; |
| 114 | rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0])); |
| 115 | rtnl_rtscope_tab[0] = "global"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 116 | rtnl_rtscope_tab[255] = "nowhere"; |
| 117 | rtnl_rtscope_tab[254] = "host"; |
| 118 | rtnl_rtscope_tab[253] = "link"; |
| 119 | rtnl_rtscope_tab[200] = "site"; |
| 120 | rtnl_tab_initialize("/etc/iproute2/rt_scopes", |
| 121 | rtnl_rtscope_tab, 256); |
| 122 | } |
| 123 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 124 | |
| 125 | const char* rtnl_rtscope_n2a(int id, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 127 | if (id < 0 || id >= 256) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 128 | snprintf(buf, len, "%d", id); |
| 129 | return buf; |
| 130 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 131 | |
| 132 | rtnl_rtscope_initialize(); |
| 133 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 134 | if (rtnl_rtscope_tab[id]) |
| 135 | return rtnl_rtscope_tab[id]; |
| 136 | snprintf(buf, len, "%d", id); |
| 137 | return buf; |
| 138 | } |
| 139 | |
| 140 | int rtnl_rtscope_a2n(uint32_t *id, char *arg) |
| 141 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 142 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 143 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 144 | int i; |
| 145 | |
| 146 | if (cache && strcmp(cache, arg) == 0) { |
| 147 | *id = res; |
| 148 | return 0; |
| 149 | } |
| 150 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 151 | rtnl_rtscope_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 152 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 153 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 154 | if (rtnl_rtscope_tab[i] && |
| 155 | strcmp(rtnl_rtscope_tab[i], arg) == 0) { |
| 156 | cache = rtnl_rtscope_tab[i]; |
| 157 | res = i; |
| 158 | *id = res; |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 163 | res = bb_strtoul(arg, NULL, 0); |
| 164 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | return -1; |
| 166 | *id = res; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 171 | static const char **rtnl_rtrealm_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 172 | |
| 173 | static void rtnl_rtrealm_initialize(void) |
| 174 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 175 | if (rtnl_rtrealm_tab) return; |
| 176 | rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0])); |
| 177 | rtnl_rtrealm_tab[0] = "unknown"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 178 | rtnl_tab_initialize("/etc/iproute2/rt_realms", |
| 179 | rtnl_rtrealm_tab, 256); |
| 180 | } |
| 181 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 182 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 183 | int rtnl_rtrealm_a2n(uint32_t *id, char *arg) |
| 184 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 185 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 187 | int i; |
| 188 | |
| 189 | if (cache && strcmp(cache, arg) == 0) { |
| 190 | *id = res; |
| 191 | return 0; |
| 192 | } |
| 193 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 194 | rtnl_rtrealm_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 195 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 196 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 197 | if (rtnl_rtrealm_tab[i] && |
| 198 | strcmp(rtnl_rtrealm_tab[i], arg) == 0) { |
| 199 | cache = rtnl_rtrealm_tab[i]; |
| 200 | res = i; |
| 201 | *id = res; |
| 202 | return 0; |
| 203 | } |
| 204 | } |
| 205 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 206 | res = bb_strtoul(arg, NULL, 0); |
| 207 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 208 | return -1; |
| 209 | *id = res; |
| 210 | return 0; |
| 211 | } |
| 212 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 213 | #if ENABLE_FEATURE_IP_RULE |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 214 | const char* rtnl_rtrealm_n2a(int id, char *buf, int len) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 215 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 216 | if (id < 0 || id >= 256) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 217 | snprintf(buf, len, "%d", id); |
| 218 | return buf; |
| 219 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 220 | |
| 221 | rtnl_rtrealm_initialize(); |
| 222 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 223 | if (rtnl_rtrealm_tab[id]) |
| 224 | return rtnl_rtrealm_tab[id]; |
| 225 | snprintf(buf, len, "%d", id); |
| 226 | return buf; |
| 227 | } |
| 228 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 229 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 230 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 231 | static const char **rtnl_rtdsfield_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 232 | |
| 233 | static void rtnl_rtdsfield_initialize(void) |
| 234 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 235 | if (rtnl_rtdsfield_tab) return; |
| 236 | rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0])); |
| 237 | rtnl_rtdsfield_tab[0] = "0"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 238 | rtnl_tab_initialize("/etc/iproute2/rt_dsfield", |
| 239 | rtnl_rtdsfield_tab, 256); |
| 240 | } |
| 241 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 242 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 243 | const char * rtnl_dsfield_n2a(int id, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 244 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 245 | if (id < 0 || id >= 256) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 246 | snprintf(buf, len, "%d", id); |
| 247 | return buf; |
| 248 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 249 | |
| 250 | rtnl_rtdsfield_initialize(); |
| 251 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 252 | if (rtnl_rtdsfield_tab[id]) |
| 253 | return rtnl_rtdsfield_tab[id]; |
| 254 | snprintf(buf, len, "0x%02x", id); |
| 255 | return buf; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | int rtnl_dsfield_a2n(uint32_t *id, char *arg) |
| 260 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 261 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 262 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 263 | int i; |
| 264 | |
| 265 | if (cache && strcmp(cache, arg) == 0) { |
| 266 | *id = res; |
| 267 | return 0; |
| 268 | } |
| 269 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 270 | rtnl_rtdsfield_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 271 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 272 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 273 | if (rtnl_rtdsfield_tab[i] && |
| 274 | strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { |
| 275 | cache = rtnl_rtdsfield_tab[i]; |
| 276 | res = i; |
| 277 | *id = res; |
| 278 | return 0; |
| 279 | } |
| 280 | } |
| 281 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 282 | res = bb_strtoul(arg, NULL, 16); |
| 283 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 284 | return -1; |
| 285 | *id = res; |
| 286 | return 0; |
| 287 | } |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 289 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 290 | #if ENABLE_FEATURE_IP_RULE |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 291 | static const char **rtnl_rttable_tab; /* [256] */ |
| 292 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 293 | static void rtnl_rttable_initialize(void) |
| 294 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 295 | if (rtnl_rtdsfield_tab) return; |
| 296 | rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0])); |
| 297 | rtnl_rttable_tab[0] = "unspec"; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 298 | rtnl_rttable_tab[255] = "local"; |
| 299 | rtnl_rttable_tab[254] = "main"; |
| 300 | rtnl_rttable_tab[253] = "default"; |
| 301 | rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256); |
| 302 | } |
| 303 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 304 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 305 | const char *rtnl_rttable_n2a(int id, char *buf, int len) |
| 306 | { |
| 307 | if (id < 0 || id >= 256) { |
| 308 | snprintf(buf, len, "%d", id); |
| 309 | return buf; |
| 310 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 311 | |
| 312 | rtnl_rttable_initialize(); |
| 313 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 314 | if (rtnl_rttable_tab[id]) |
| 315 | return rtnl_rttable_tab[id]; |
| 316 | snprintf(buf, len, "%d", id); |
| 317 | return buf; |
| 318 | } |
| 319 | |
| 320 | int rtnl_rttable_a2n(uint32_t * id, char *arg) |
| 321 | { |
| 322 | static char *cache = NULL; |
| 323 | static unsigned long res; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 324 | int i; |
| 325 | |
| 326 | if (cache && strcmp(cache, arg) == 0) { |
| 327 | *id = res; |
| 328 | return 0; |
| 329 | } |
| 330 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 331 | rtnl_rttable_initialize(); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 332 | |
| 333 | for (i = 0; i < 256; i++) { |
| 334 | if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) { |
| 335 | cache = (char*)rtnl_rttable_tab[i]; |
| 336 | res = i; |
| 337 | *id = res; |
| 338 | return 0; |
| 339 | } |
| 340 | } |
| 341 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 342 | i = bb_strtoul(arg, NULL, 0); |
| 343 | if (errno || i > 255) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 344 | return -1; |
| 345 | *id = i; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | #endif |