"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 | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 16 | 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] | 17 | { |
| 18 | char buf[512]; |
| 19 | FILE *fp; |
| 20 | |
| 21 | fp = fopen(file, "r"); |
| 22 | if (!fp) |
| 23 | return; |
| 24 | while (fgets(buf, sizeof(buf), fp)) { |
| 25 | char *p = buf; |
| 26 | int id; |
| 27 | char namebuf[512]; |
| 28 | |
| 29 | while (*p == ' ' || *p == '\t') |
| 30 | p++; |
| 31 | if (*p == '#' || *p == '\n' || *p == 0) |
| 32 | continue; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 33 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 |
| 34 | && sscanf(p, "0x%x %s #", &id, namebuf) != 2 |
| 35 | && sscanf(p, "%d %s\n", &id, namebuf) != 2 |
| 36 | && sscanf(p, "%d %s #", &id, namebuf) != 2 |
| 37 | ) { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 38 | bb_error_msg("database %s is corrupted at %s", |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 39 | file, p); |
| 40 | return; |
| 41 | } |
| 42 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 43 | if (id < 0 || id > size) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 44 | continue; |
| 45 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 46 | tab[id] = xstrdup(namebuf); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 47 | } |
| 48 | fclose(fp); |
| 49 | } |
| 50 | |
| 51 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 52 | static const char **rtnl_rtprot_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | |
| 54 | static void rtnl_rtprot_initialize(void) |
| 55 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 56 | static const char *const init_tab[] = { |
| 57 | "none", |
| 58 | "redirect", |
| 59 | "kernel", |
| 60 | "boot", |
| 61 | "static", |
| 62 | NULL, |
| 63 | NULL, |
| 64 | NULL, |
| 65 | "gated", |
| 66 | "ra", |
| 67 | "mrt", |
| 68 | "zebra", |
| 69 | "bird", |
| 70 | }; |
| 71 | if (rtnl_rtprot_tab) return; |
| 72 | rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0])); |
| 73 | memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 74 | rtnl_tab_initialize("/etc/iproute2/rt_protos", |
| 75 | rtnl_rtprot_tab, 256); |
| 76 | } |
| 77 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 78 | |
| 79 | const char* rtnl_rtprot_n2a(int id, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 80 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 81 | if (id < 0 || id >= 256) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 82 | snprintf(buf, len, "%d", id); |
| 83 | return buf; |
| 84 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 85 | |
| 86 | rtnl_rtprot_initialize(); |
| 87 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 88 | if (rtnl_rtprot_tab[id]) |
| 89 | return rtnl_rtprot_tab[id]; |
| 90 | snprintf(buf, len, "%d", id); |
| 91 | return buf; |
| 92 | } |
| 93 | |
| 94 | int rtnl_rtprot_a2n(uint32_t *id, char *arg) |
| 95 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 96 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 97 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 98 | int i; |
| 99 | |
| 100 | if (cache && strcmp(cache, arg) == 0) { |
| 101 | *id = res; |
| 102 | return 0; |
| 103 | } |
| 104 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 105 | rtnl_rtprot_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 107 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 108 | if (rtnl_rtprot_tab[i] && |
| 109 | strcmp(rtnl_rtprot_tab[i], arg) == 0) { |
| 110 | cache = rtnl_rtprot_tab[i]; |
| 111 | res = i; |
| 112 | *id = res; |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 117 | res = bb_strtoul(arg, NULL, 0); |
| 118 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | return -1; |
| 120 | *id = res; |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 125 | static const char **rtnl_rtscope_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | |
| 127 | static void rtnl_rtscope_initialize(void) |
| 128 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 129 | if (rtnl_rtscope_tab) return; |
| 130 | rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0])); |
| 131 | rtnl_rtscope_tab[0] = "global"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 132 | rtnl_rtscope_tab[255] = "nowhere"; |
| 133 | rtnl_rtscope_tab[254] = "host"; |
| 134 | rtnl_rtscope_tab[253] = "link"; |
| 135 | rtnl_rtscope_tab[200] = "site"; |
| 136 | rtnl_tab_initialize("/etc/iproute2/rt_scopes", |
| 137 | rtnl_rtscope_tab, 256); |
| 138 | } |
| 139 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 140 | |
| 141 | const char* rtnl_rtscope_n2a(int id, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 142 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 143 | if (id < 0 || id >= 256) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 144 | snprintf(buf, len, "%d", id); |
| 145 | return buf; |
| 146 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 147 | |
| 148 | rtnl_rtscope_initialize(); |
| 149 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 150 | if (rtnl_rtscope_tab[id]) |
| 151 | return rtnl_rtscope_tab[id]; |
| 152 | snprintf(buf, len, "%d", id); |
| 153 | return buf; |
| 154 | } |
| 155 | |
| 156 | int rtnl_rtscope_a2n(uint32_t *id, char *arg) |
| 157 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 158 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 159 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 160 | int i; |
| 161 | |
| 162 | if (cache && strcmp(cache, arg) == 0) { |
| 163 | *id = res; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 167 | rtnl_rtscope_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 169 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 170 | if (rtnl_rtscope_tab[i] && |
| 171 | strcmp(rtnl_rtscope_tab[i], arg) == 0) { |
| 172 | cache = rtnl_rtscope_tab[i]; |
| 173 | res = i; |
| 174 | *id = res; |
| 175 | return 0; |
| 176 | } |
| 177 | } |
| 178 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 179 | res = bb_strtoul(arg, NULL, 0); |
| 180 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 181 | return -1; |
| 182 | *id = res; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 187 | static const char **rtnl_rtrealm_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 188 | |
| 189 | static void rtnl_rtrealm_initialize(void) |
| 190 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 191 | if (rtnl_rtrealm_tab) return; |
| 192 | rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0])); |
| 193 | rtnl_rtrealm_tab[0] = "unknown"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | rtnl_tab_initialize("/etc/iproute2/rt_realms", |
| 195 | rtnl_rtrealm_tab, 256); |
| 196 | } |
| 197 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 198 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 199 | int rtnl_rtrealm_a2n(uint32_t *id, char *arg) |
| 200 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 201 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 203 | int i; |
| 204 | |
| 205 | if (cache && strcmp(cache, arg) == 0) { |
| 206 | *id = res; |
| 207 | return 0; |
| 208 | } |
| 209 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 210 | rtnl_rtrealm_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 211 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 212 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 213 | if (rtnl_rtrealm_tab[i] && |
| 214 | strcmp(rtnl_rtrealm_tab[i], arg) == 0) { |
| 215 | cache = rtnl_rtrealm_tab[i]; |
| 216 | res = i; |
| 217 | *id = res; |
| 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 222 | res = bb_strtoul(arg, NULL, 0); |
| 223 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 224 | return -1; |
| 225 | *id = res; |
| 226 | return 0; |
| 227 | } |
| 228 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 229 | #if ENABLE_FEATURE_IP_RULE |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 230 | const char* rtnl_rtrealm_n2a(int id, char *buf, int len) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 231 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 232 | if (id < 0 || id >= 256) { |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 233 | snprintf(buf, len, "%d", id); |
| 234 | return buf; |
| 235 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 236 | |
| 237 | rtnl_rtrealm_initialize(); |
| 238 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 239 | if (rtnl_rtrealm_tab[id]) |
| 240 | return rtnl_rtrealm_tab[id]; |
| 241 | snprintf(buf, len, "%d", id); |
| 242 | return buf; |
| 243 | } |
| 244 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 245 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 247 | static const char **rtnl_rtdsfield_tab; /* [256] */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 248 | |
| 249 | static void rtnl_rtdsfield_initialize(void) |
| 250 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 251 | if (rtnl_rtdsfield_tab) return; |
| 252 | rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0])); |
| 253 | rtnl_rtdsfield_tab[0] = "0"; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 254 | rtnl_tab_initialize("/etc/iproute2/rt_dsfield", |
| 255 | rtnl_rtdsfield_tab, 256); |
| 256 | } |
| 257 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 258 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 259 | const char * rtnl_dsfield_n2a(int id, char *buf, int len) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 260 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 261 | if (id < 0 || id >= 256) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 262 | snprintf(buf, len, "%d", id); |
| 263 | return buf; |
| 264 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 265 | |
| 266 | rtnl_rtdsfield_initialize(); |
| 267 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 268 | if (rtnl_rtdsfield_tab[id]) |
| 269 | return rtnl_rtdsfield_tab[id]; |
| 270 | snprintf(buf, len, "0x%02x", id); |
| 271 | return buf; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | int rtnl_dsfield_a2n(uint32_t *id, char *arg) |
| 276 | { |
Denis Vlasenko | 981b24d | 2006-09-28 22:36:23 +0000 | [diff] [blame] | 277 | static const char *cache = NULL; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | static unsigned long res; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 279 | int i; |
| 280 | |
| 281 | if (cache && strcmp(cache, arg) == 0) { |
| 282 | *id = res; |
| 283 | return 0; |
| 284 | } |
| 285 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 286 | rtnl_rtdsfield_initialize(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 288 | for (i = 0; i < 256; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 289 | if (rtnl_rtdsfield_tab[i] && |
| 290 | strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { |
| 291 | cache = rtnl_rtdsfield_tab[i]; |
| 292 | res = i; |
| 293 | *id = res; |
| 294 | return 0; |
| 295 | } |
| 296 | } |
| 297 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 298 | res = bb_strtoul(arg, NULL, 16); |
| 299 | if (errno || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 300 | return -1; |
| 301 | *id = res; |
| 302 | return 0; |
| 303 | } |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 304 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 305 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 306 | #if ENABLE_FEATURE_IP_RULE |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 307 | static const char **rtnl_rttable_tab; /* [256] */ |
| 308 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 309 | static void rtnl_rttable_initialize(void) |
| 310 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 311 | if (rtnl_rtdsfield_tab) return; |
| 312 | rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0])); |
| 313 | rtnl_rttable_tab[0] = "unspec"; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 314 | rtnl_rttable_tab[255] = "local"; |
| 315 | rtnl_rttable_tab[254] = "main"; |
| 316 | rtnl_rttable_tab[253] = "default"; |
| 317 | rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256); |
| 318 | } |
| 319 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 320 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 321 | const char *rtnl_rttable_n2a(int id, char *buf, int len) |
| 322 | { |
| 323 | if (id < 0 || id >= 256) { |
| 324 | snprintf(buf, len, "%d", id); |
| 325 | return buf; |
| 326 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 327 | |
| 328 | rtnl_rttable_initialize(); |
| 329 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 330 | if (rtnl_rttable_tab[id]) |
| 331 | return rtnl_rttable_tab[id]; |
| 332 | snprintf(buf, len, "%d", id); |
| 333 | return buf; |
| 334 | } |
| 335 | |
| 336 | int rtnl_rttable_a2n(uint32_t * id, char *arg) |
| 337 | { |
| 338 | static char *cache = NULL; |
| 339 | static unsigned long res; |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 340 | int i; |
| 341 | |
| 342 | if (cache && strcmp(cache, arg) == 0) { |
| 343 | *id = res; |
| 344 | return 0; |
| 345 | } |
| 346 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 347 | rtnl_rttable_initialize(); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 348 | |
| 349 | for (i = 0; i < 256; i++) { |
| 350 | if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) { |
| 351 | cache = (char*)rtnl_rttable_tab[i]; |
| 352 | res = i; |
| 353 | *id = res; |
| 354 | return 0; |
| 355 | } |
| 356 | } |
| 357 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 358 | i = bb_strtoul(arg, NULL, 0); |
| 359 | if (errno || i > 255) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 360 | return -1; |
| 361 | *id = i; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | #endif |