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