"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 | /* |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU General Public License |
| 5 | * as published by the Free Software Foundation; either version |
| 6 | * 2 of the License, or (at your option) any later version. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 9 | */ |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 11 | #include "rt_names.h" |
| 12 | #include "utils.h" |
| 13 | |
Denys Vlasenko | 3d8d5e8 | 2015-10-08 13:02:28 +0200 | [diff] [blame] | 14 | const char* FAST_FUNC rtnl_rtntype_n2a(int id) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | { |
| 16 | switch (id) { |
| 17 | case RTN_UNSPEC: |
| 18 | return "none"; |
| 19 | case RTN_UNICAST: |
| 20 | return "unicast"; |
| 21 | case RTN_LOCAL: |
| 22 | return "local"; |
| 23 | case RTN_BROADCAST: |
| 24 | return "broadcast"; |
| 25 | case RTN_ANYCAST: |
| 26 | return "anycast"; |
| 27 | case RTN_MULTICAST: |
| 28 | return "multicast"; |
| 29 | case RTN_BLACKHOLE: |
| 30 | return "blackhole"; |
| 31 | case RTN_UNREACHABLE: |
| 32 | return "unreachable"; |
| 33 | case RTN_PROHIBIT: |
| 34 | return "prohibit"; |
| 35 | case RTN_THROW: |
| 36 | return "throw"; |
| 37 | case RTN_NAT: |
| 38 | return "nat"; |
| 39 | case RTN_XRESOLVE: |
| 40 | return "xresolve"; |
| 41 | default: |
Denys Vlasenko | 3d8d5e8 | 2015-10-08 13:02:28 +0200 | [diff] [blame] | 42 | return itoa(id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 46 | int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 47 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 48 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 49 | "local\0""nat\0""broadcast\0""brd\0""anycast\0" |
| 50 | "multicast\0""prohibit\0""unreachable\0""blackhole\0" |
| 51 | "xresolve\0""unicast\0""throw\0"; |
| 52 | enum { |
| 53 | ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast, |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 54 | ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole, |
| 55 | ARG_xresolve, ARG_unicast, ARG_throw |
| 56 | }; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 57 | const smalluint key = index_in_substrings(keywords, arg) + 1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 58 | char *end; |
| 59 | unsigned long res; |
| 60 | |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 61 | if (key == ARG_local) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 62 | res = RTN_LOCAL; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 63 | else if (key == ARG_nat) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 64 | res = RTN_NAT; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 65 | else if (key == ARG_broadcast || key == ARG_brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | res = RTN_BROADCAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 67 | else if (key == ARG_anycast) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 68 | res = RTN_ANYCAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 69 | else if (key == ARG_multicast) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 70 | res = RTN_MULTICAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 71 | else if (key == ARG_prohibit) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 72 | res = RTN_PROHIBIT; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 73 | else if (key == ARG_unreachable) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 74 | res = RTN_UNREACHABLE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 75 | else if (key == ARG_blackhole) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | res = RTN_BLACKHOLE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 77 | else if (key == ARG_xresolve) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 78 | res = RTN_XRESOLVE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 79 | else if (key == ARG_unicast) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 80 | res = RTN_UNICAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 81 | else if (key == ARG_throw) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 82 | res = RTN_THROW; |
| 83 | else { |
| 84 | res = strtoul(arg, &end, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 85 | if (end == arg || *end || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | *id = res; |
| 89 | return 0; |
| 90 | } |
| 91 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 92 | int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 94 | uint32_t realm = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | char *p = strchr(arg, '/'); |
| 96 | |
| 97 | *realms = 0; |
| 98 | if (p) { |
| 99 | *p = 0; |
| 100 | if (rtnl_rtrealm_a2n(realms, arg)) { |
| 101 | *p = '/'; |
| 102 | return -1; |
| 103 | } |
| 104 | *realms <<= 16; |
| 105 | *p = '/'; |
| 106 | arg = p+1; |
| 107 | } |
| 108 | if (*arg && rtnl_rtrealm_a2n(&realm, arg)) |
| 109 | return -1; |
| 110 | *realms |= realm; |
| 111 | return 0; |
| 112 | } |