osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rt_names.c rtnetlink names DB. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <string.h> |
| 18 | #include <sys/time.h> |
osdl.org!shemminger | b88fd9f | 2004-06-08 23:50:43 +0000 | [diff] [blame] | 19 | #include <sys/socket.h> |
| 20 | |
| 21 | #include <asm/types.h> |
| 22 | #include <linux/rtnetlink.h> |
| 23 | |
| 24 | #include "rt_names.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 25 | |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 26 | #ifndef CONFDIR |
| 27 | #define CONFDIR "/etc/iproute2" |
| 28 | #endif |
| 29 | |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 30 | struct rtnl_hash_entry { |
| 31 | struct rtnl_hash_entry *next; |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 32 | const char * name; |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 33 | unsigned int id; |
| 34 | }; |
| 35 | |
| 36 | static void |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 37 | rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size) |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 38 | { |
| 39 | struct rtnl_hash_entry *entry; |
| 40 | char buf[512]; |
| 41 | FILE *fp; |
| 42 | |
| 43 | fp = fopen(file, "r"); |
| 44 | if (!fp) |
| 45 | return; |
| 46 | while (fgets(buf, sizeof(buf), fp)) { |
| 47 | char *p = buf; |
| 48 | int id; |
| 49 | char namebuf[512]; |
| 50 | |
| 51 | while (*p == ' ' || *p == '\t') |
| 52 | p++; |
| 53 | if (*p == '#' || *p == '\n' || *p == 0) |
| 54 | continue; |
| 55 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 && |
| 56 | sscanf(p, "0x%x %s #", &id, namebuf) != 2 && |
| 57 | sscanf(p, "%d %s\n", &id, namebuf) != 2 && |
| 58 | sscanf(p, "%d %s #", &id, namebuf) != 2) { |
| 59 | fprintf(stderr, "Database %s is corrupted at %s\n", |
| 60 | file, p); |
Thomas Jarosch | 97c1358 | 2011-10-03 05:22:42 +0000 | [diff] [blame] | 61 | fclose(fp); |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (id<0) |
| 66 | continue; |
| 67 | entry = malloc(sizeof(*entry)); |
| 68 | entry->id = id; |
| 69 | entry->name = strdup(namebuf); |
| 70 | entry->next = hash[id & (size - 1)]; |
| 71 | hash[id & (size - 1)] = entry; |
| 72 | } |
| 73 | fclose(fp); |
| 74 | } |
| 75 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 76 | static void rtnl_tab_initialize(const char *file, char **tab, int size) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | { |
| 78 | char buf[512]; |
| 79 | FILE *fp; |
| 80 | |
| 81 | fp = fopen(file, "r"); |
| 82 | if (!fp) |
| 83 | return; |
| 84 | while (fgets(buf, sizeof(buf), fp)) { |
| 85 | char *p = buf; |
| 86 | int id; |
| 87 | char namebuf[512]; |
| 88 | |
| 89 | while (*p == ' ' || *p == '\t') |
| 90 | p++; |
| 91 | if (*p == '#' || *p == '\n' || *p == 0) |
| 92 | continue; |
| 93 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 && |
| 94 | sscanf(p, "0x%x %s #", &id, namebuf) != 2 && |
| 95 | sscanf(p, "%d %s\n", &id, namebuf) != 2 && |
| 96 | sscanf(p, "%d %s #", &id, namebuf) != 2) { |
| 97 | fprintf(stderr, "Database %s is corrupted at %s\n", |
| 98 | file, p); |
Thomas Jarosch | 97c1358 | 2011-10-03 05:22:42 +0000 | [diff] [blame] | 99 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (id<0 || id>size) |
| 104 | continue; |
| 105 | |
| 106 | tab[id] = strdup(namebuf); |
| 107 | } |
| 108 | fclose(fp); |
| 109 | } |
| 110 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 111 | static char * rtnl_rtprot_tab[256] = { |
osdl.org!shemminger | b88fd9f | 2004-06-08 23:50:43 +0000 | [diff] [blame] | 112 | [RTPROT_UNSPEC] = "none", |
| 113 | [RTPROT_REDIRECT] ="redirect", |
| 114 | [RTPROT_KERNEL] = "kernel", |
| 115 | [RTPROT_BOOT] = "boot", |
| 116 | [RTPROT_STATIC] = "static", |
| 117 | |
| 118 | [RTPROT_GATED] = "gated", |
| 119 | [RTPROT_RA] = "ra", |
| 120 | [RTPROT_MRT] = "mrt", |
| 121 | [RTPROT_ZEBRA] ="zebra", |
| 122 | [RTPROT_BIRD] = "bird", |
| 123 | [RTPROT_DNROUTED] = "dnrouted", |
| 124 | [RTPROT_XORP] = "xorp", |
shemminger | 40b6c62 | 2006-03-10 23:44:04 +0000 | [diff] [blame] | 125 | [RTPROT_NTK] = "ntk", |
Stephen Hemminger | 1e659af | 2009-03-18 13:33:12 -0700 | [diff] [blame] | 126 | [RTPROT_DHCP] = "dhcp", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 130 | static int rtnl_rtprot_init; |
| 131 | |
| 132 | static void rtnl_rtprot_initialize(void) |
| 133 | { |
| 134 | rtnl_rtprot_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 135 | rtnl_tab_initialize(CONFDIR "/rt_protos", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 136 | rtnl_rtprot_tab, 256); |
| 137 | } |
| 138 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 139 | const char * rtnl_rtprot_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 140 | { |
| 141 | if (id<0 || id>=256) { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 142 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | return buf; |
| 144 | } |
| 145 | if (!rtnl_rtprot_tab[id]) { |
| 146 | if (!rtnl_rtprot_init) |
| 147 | rtnl_rtprot_initialize(); |
| 148 | } |
| 149 | if (rtnl_rtprot_tab[id]) |
| 150 | return rtnl_rtprot_tab[id]; |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 151 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 152 | return buf; |
| 153 | } |
| 154 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 155 | int rtnl_rtprot_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 156 | { |
| 157 | static char *cache = NULL; |
| 158 | static unsigned long res; |
| 159 | char *end; |
| 160 | int i; |
| 161 | |
| 162 | if (cache && strcmp(cache, arg) == 0) { |
| 163 | *id = res; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | if (!rtnl_rtprot_init) |
| 168 | rtnl_rtprot_initialize(); |
| 169 | |
| 170 | for (i=0; i<256; i++) { |
| 171 | if (rtnl_rtprot_tab[i] && |
| 172 | strcmp(rtnl_rtprot_tab[i], arg) == 0) { |
| 173 | cache = rtnl_rtprot_tab[i]; |
| 174 | res = i; |
| 175 | *id = res; |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | res = strtoul(arg, &end, 0); |
| 181 | if (!end || end == arg || *end || res > 255) |
| 182 | return -1; |
| 183 | *id = res; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 188 | static char * rtnl_rtscope_tab[256] = { |
| 189 | "global", |
| 190 | }; |
| 191 | |
| 192 | static int rtnl_rtscope_init; |
| 193 | |
| 194 | static void rtnl_rtscope_initialize(void) |
| 195 | { |
| 196 | rtnl_rtscope_init = 1; |
| 197 | rtnl_rtscope_tab[255] = "nowhere"; |
| 198 | rtnl_rtscope_tab[254] = "host"; |
| 199 | rtnl_rtscope_tab[253] = "link"; |
| 200 | rtnl_rtscope_tab[200] = "site"; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 201 | rtnl_tab_initialize(CONFDIR "/rt_scopes", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 202 | rtnl_rtscope_tab, 256); |
| 203 | } |
| 204 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 205 | const char *rtnl_rtscope_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 206 | { |
| 207 | if (id<0 || id>=256) { |
| 208 | snprintf(buf, len, "%d", id); |
| 209 | return buf; |
| 210 | } |
| 211 | if (!rtnl_rtscope_tab[id]) { |
| 212 | if (!rtnl_rtscope_init) |
| 213 | rtnl_rtscope_initialize(); |
| 214 | } |
| 215 | if (rtnl_rtscope_tab[id]) |
| 216 | return rtnl_rtscope_tab[id]; |
| 217 | snprintf(buf, len, "%d", id); |
| 218 | return buf; |
| 219 | } |
| 220 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 221 | int rtnl_rtscope_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 222 | { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 223 | static const char *cache = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 224 | static unsigned long res; |
| 225 | char *end; |
| 226 | int i; |
| 227 | |
| 228 | if (cache && strcmp(cache, arg) == 0) { |
| 229 | *id = res; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | if (!rtnl_rtscope_init) |
| 234 | rtnl_rtscope_initialize(); |
| 235 | |
| 236 | for (i=0; i<256; i++) { |
| 237 | if (rtnl_rtscope_tab[i] && |
| 238 | strcmp(rtnl_rtscope_tab[i], arg) == 0) { |
| 239 | cache = rtnl_rtscope_tab[i]; |
| 240 | res = i; |
| 241 | *id = res; |
| 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | res = strtoul(arg, &end, 0); |
| 247 | if (!end || end == arg || *end || res > 255) |
| 248 | return -1; |
| 249 | *id = res; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | static char * rtnl_rtrealm_tab[256] = { |
| 255 | "unknown", |
| 256 | }; |
| 257 | |
| 258 | static int rtnl_rtrealm_init; |
| 259 | |
| 260 | static void rtnl_rtrealm_initialize(void) |
| 261 | { |
| 262 | rtnl_rtrealm_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 263 | rtnl_tab_initialize(CONFDIR "/rt_realms", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 264 | rtnl_rtrealm_tab, 256); |
| 265 | } |
| 266 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 267 | const char *rtnl_rtrealm_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 268 | { |
| 269 | if (id<0 || id>=256) { |
| 270 | snprintf(buf, len, "%d", id); |
| 271 | return buf; |
| 272 | } |
| 273 | if (!rtnl_rtrealm_tab[id]) { |
| 274 | if (!rtnl_rtrealm_init) |
| 275 | rtnl_rtrealm_initialize(); |
| 276 | } |
| 277 | if (rtnl_rtrealm_tab[id]) |
| 278 | return rtnl_rtrealm_tab[id]; |
| 279 | snprintf(buf, len, "%d", id); |
| 280 | return buf; |
| 281 | } |
| 282 | |
| 283 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 284 | int rtnl_rtrealm_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 285 | { |
| 286 | static char *cache = NULL; |
| 287 | static unsigned long res; |
| 288 | char *end; |
| 289 | int i; |
| 290 | |
| 291 | if (cache && strcmp(cache, arg) == 0) { |
| 292 | *id = res; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | if (!rtnl_rtrealm_init) |
| 297 | rtnl_rtrealm_initialize(); |
| 298 | |
| 299 | for (i=0; i<256; i++) { |
| 300 | if (rtnl_rtrealm_tab[i] && |
| 301 | strcmp(rtnl_rtrealm_tab[i], arg) == 0) { |
| 302 | cache = rtnl_rtrealm_tab[i]; |
| 303 | res = i; |
| 304 | *id = res; |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | res = strtoul(arg, &end, 0); |
| 310 | if (!end || end == arg || *end || res > 255) |
| 311 | return -1; |
| 312 | *id = res; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 317 | static struct rtnl_hash_entry dflt_table_entry = { .id = 253, .name = "default" }; |
| 318 | static struct rtnl_hash_entry main_table_entry = { .id = 254, .name = "main" }; |
| 319 | static struct rtnl_hash_entry local_table_entry = { .id = 255, .name = "local" }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 320 | |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 321 | static struct rtnl_hash_entry * rtnl_rttable_hash[256] = { |
| 322 | [253] = &dflt_table_entry, |
| 323 | [254] = &main_table_entry, |
| 324 | [255] = &local_table_entry, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | static int rtnl_rttable_init; |
| 328 | |
| 329 | static void rtnl_rttable_initialize(void) |
| 330 | { |
| 331 | rtnl_rttable_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 332 | rtnl_hash_initialize(CONFDIR "/rt_tables", |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 333 | rtnl_rttable_hash, 256); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 336 | const char * rtnl_rttable_n2a(__u32 id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 337 | { |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 338 | struct rtnl_hash_entry *entry; |
| 339 | |
Boian Bonev | 887a5d0 | 2006-11-27 14:12:31 -0800 | [diff] [blame] | 340 | if (id > RT_TABLE_MAX) { |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 341 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 342 | return buf; |
| 343 | } |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 344 | if (!rtnl_rttable_init) |
| 345 | rtnl_rttable_initialize(); |
| 346 | entry = rtnl_rttable_hash[id & 255]; |
| 347 | while (entry && entry->id != id) |
| 348 | entry = entry->next; |
| 349 | if (entry) |
| 350 | return entry->name; |
| 351 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 352 | return buf; |
| 353 | } |
| 354 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 355 | int rtnl_rttable_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 356 | { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 357 | static const char *cache = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 358 | static unsigned long res; |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 359 | struct rtnl_hash_entry *entry; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 360 | char *end; |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 361 | __u32 i; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 362 | |
| 363 | if (cache && strcmp(cache, arg) == 0) { |
| 364 | *id = res; |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | if (!rtnl_rttable_init) |
| 369 | rtnl_rttable_initialize(); |
| 370 | |
| 371 | for (i=0; i<256; i++) { |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 372 | entry = rtnl_rttable_hash[i]; |
| 373 | while (entry && strcmp(entry->name, arg)) |
| 374 | entry = entry->next; |
| 375 | if (entry) { |
| 376 | cache = entry->name; |
| 377 | res = entry->id; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 378 | *id = res; |
| 379 | return 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | i = strtoul(arg, &end, 0); |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 384 | if (!end || end == arg || *end || i > RT_TABLE_MAX) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 385 | return -1; |
| 386 | *id = i; |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | static char * rtnl_rtdsfield_tab[256] = { |
| 392 | "0", |
| 393 | }; |
| 394 | |
| 395 | static int rtnl_rtdsfield_init; |
| 396 | |
| 397 | static void rtnl_rtdsfield_initialize(void) |
| 398 | { |
| 399 | rtnl_rtdsfield_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 400 | rtnl_tab_initialize(CONFDIR "/rt_dsfield", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 401 | rtnl_rtdsfield_tab, 256); |
| 402 | } |
| 403 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 404 | const char *rtnl_dsfield_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 405 | { |
| 406 | if (id<0 || id>=256) { |
| 407 | snprintf(buf, len, "%d", id); |
| 408 | return buf; |
| 409 | } |
| 410 | if (!rtnl_rtdsfield_tab[id]) { |
| 411 | if (!rtnl_rtdsfield_init) |
| 412 | rtnl_rtdsfield_initialize(); |
| 413 | } |
| 414 | if (rtnl_rtdsfield_tab[id]) |
| 415 | return rtnl_rtdsfield_tab[id]; |
| 416 | snprintf(buf, len, "0x%02x", id); |
| 417 | return buf; |
| 418 | } |
| 419 | |
| 420 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 421 | int rtnl_dsfield_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 422 | { |
| 423 | static char *cache = NULL; |
| 424 | static unsigned long res; |
| 425 | char *end; |
| 426 | int i; |
| 427 | |
| 428 | if (cache && strcmp(cache, arg) == 0) { |
| 429 | *id = res; |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | if (!rtnl_rtdsfield_init) |
| 434 | rtnl_rtdsfield_initialize(); |
| 435 | |
| 436 | for (i=0; i<256; i++) { |
| 437 | if (rtnl_rtdsfield_tab[i] && |
| 438 | strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { |
| 439 | cache = rtnl_rtdsfield_tab[i]; |
| 440 | res = i; |
| 441 | *id = res; |
| 442 | return 0; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | res = strtoul(arg, &end, 16); |
| 447 | if (!end || end == arg || *end || res > 255) |
| 448 | return -1; |
| 449 | *id = res; |
| 450 | return 0; |
| 451 | } |
| 452 | |
Vlad Dogaru | ac694c3 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 453 | |
| 454 | static struct rtnl_hash_entry dflt_group_entry = { .id = 0, .name = "default" }; |
| 455 | |
| 456 | static struct rtnl_hash_entry * rtnl_group_hash[256] = { |
| 457 | [0] = &dflt_group_entry, |
| 458 | }; |
| 459 | |
| 460 | static int rtnl_group_init; |
| 461 | |
| 462 | static void rtnl_group_initialize(void) |
| 463 | { |
| 464 | rtnl_group_init = 1; |
| 465 | rtnl_hash_initialize("/etc/iproute2/group", |
| 466 | rtnl_group_hash, 256); |
| 467 | } |
| 468 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 469 | int rtnl_group_a2n(int *id, const char *arg) |
Vlad Dogaru | ac694c3 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 470 | { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 471 | static const char *cache = NULL; |
Vlad Dogaru | ac694c3 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 472 | static unsigned long res; |
| 473 | struct rtnl_hash_entry *entry; |
| 474 | char *end; |
| 475 | int i; |
| 476 | |
| 477 | if (cache && strcmp(cache, arg) == 0) { |
| 478 | *id = res; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | if (!rtnl_group_init) |
| 483 | rtnl_group_initialize(); |
| 484 | |
| 485 | for (i=0; i<256; i++) { |
| 486 | entry = rtnl_group_hash[i]; |
| 487 | while (entry && strcmp(entry->name, arg)) |
| 488 | entry = entry->next; |
| 489 | if (entry) { |
| 490 | cache = entry->name; |
| 491 | res = entry->id; |
| 492 | *id = res; |
| 493 | return 0; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | i = strtol(arg, &end, 0); |
| 498 | if (!end || end == arg || *end || i < 0) |
| 499 | return -1; |
| 500 | *id = i; |
| 501 | return 0; |
| 502 | } |
Stefan Tomanek | c4fdf75 | 2013-08-03 14:20:53 +0200 | [diff] [blame^] | 503 | |
| 504 | const char *rtnl_group_n2a(int id, char *buf, int len) |
| 505 | { |
| 506 | struct rtnl_hash_entry *entry; |
| 507 | int i; |
| 508 | |
| 509 | if (!rtnl_group_init) |
| 510 | rtnl_group_initialize(); |
| 511 | |
| 512 | for (i=0; i<256; i++) { |
| 513 | entry = rtnl_group_hash[i]; |
| 514 | if (entry && entry->id == id) { |
| 515 | return entry->name; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | snprintf(buf, len, "%d", id); |
| 520 | return buf; |
| 521 | } |