"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 | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 14 | static const char keywords[] ALIGN1 = |
| 15 | "local\0""nat\0""broadcast\0""brd\0""anycast\0" |
| 16 | "multicast\0""prohibit\0""unreachable\0""blackhole\0" |
| 17 | "xresolve\0""unicast\0""throw\0"; |
| 18 | enum { |
| 19 | ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast, |
| 20 | ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole, |
| 21 | ARG_xresolve, ARG_unicast, ARG_throw |
| 22 | }; |
| 23 | #define str_local keywords |
| 24 | #define str_nat (str_local + sizeof("local")) |
| 25 | #define str_broadcast (str_nat + sizeof("nat")) |
| 26 | #define str_brd (str_broadcast + sizeof("broadcast")) |
| 27 | #define str_anycast (str_brd + sizeof("brd")) |
| 28 | #define str_multicast (str_anycast + sizeof("anycast")) |
| 29 | #define str_prohibit (str_multicast + sizeof("multicast")) |
| 30 | #define str_unreachable (str_prohibit + sizeof("prohibit")) |
| 31 | #define str_blackhole (str_unreachable + sizeof("unreachable")) |
| 32 | #define str_xresolve (str_blackhole + sizeof("blackhole")) |
| 33 | #define str_unicast (str_xresolve + sizeof("xresolve")) |
| 34 | #define str_throw (str_unicast + sizeof("unicast")) |
| 35 | |
Denys Vlasenko | 3d8d5e8 | 2015-10-08 13:02:28 +0200 | [diff] [blame] | 36 | const char* FAST_FUNC rtnl_rtntype_n2a(int id) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 37 | { |
| 38 | switch (id) { |
| 39 | case RTN_UNSPEC: |
| 40 | return "none"; |
| 41 | case RTN_UNICAST: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 42 | return str_unicast; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 43 | case RTN_LOCAL: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 44 | return str_local; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 45 | case RTN_BROADCAST: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 46 | return str_broadcast; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 47 | case RTN_ANYCAST: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 48 | return str_anycast; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 49 | case RTN_MULTICAST: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 50 | return str_multicast; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 51 | case RTN_BLACKHOLE: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 52 | return str_blackhole; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | case RTN_UNREACHABLE: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 54 | return str_unreachable; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 55 | case RTN_PROHIBIT: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 56 | return str_prohibit; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 57 | case RTN_THROW: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 58 | return str_throw; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 59 | case RTN_NAT: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 60 | return str_nat; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | case RTN_XRESOLVE: |
Denys Vlasenko | 4323ac8 | 2020-11-21 17:23:13 +0100 | [diff] [blame] | 62 | return str_xresolve; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 63 | default: |
Denys Vlasenko | 3d8d5e8 | 2015-10-08 13:02:28 +0200 | [diff] [blame] | 64 | return itoa(id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 68 | int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 69 | { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 70 | const smalluint key = index_in_substrings(keywords, arg) + 1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 71 | char *end; |
| 72 | unsigned long res; |
| 73 | |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 74 | if (key == ARG_local) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | res = RTN_LOCAL; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 76 | else if (key == ARG_nat) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 77 | res = RTN_NAT; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 78 | else if (key == ARG_broadcast || key == ARG_brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | res = RTN_BROADCAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 80 | else if (key == ARG_anycast) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 81 | res = RTN_ANYCAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 82 | else if (key == ARG_multicast) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 83 | res = RTN_MULTICAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 84 | else if (key == ARG_prohibit) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 85 | res = RTN_PROHIBIT; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 86 | else if (key == ARG_unreachable) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 87 | res = RTN_UNREACHABLE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 88 | else if (key == ARG_blackhole) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 89 | res = RTN_BLACKHOLE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 90 | else if (key == ARG_xresolve) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | res = RTN_XRESOLVE; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 92 | else if (key == ARG_unicast) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | res = RTN_UNICAST; |
Bernhard Reutner-Fischer | 789b87e | 2007-06-21 10:20:13 +0000 | [diff] [blame] | 94 | else if (key == ARG_throw) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | res = RTN_THROW; |
| 96 | else { |
| 97 | res = strtoul(arg, &end, 0); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 98 | if (end == arg || *end || res > 255) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | *id = res; |
| 102 | return 0; |
| 103 | } |
| 104 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 105 | int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 106 | { |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 107 | uint32_t realm = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 108 | char *p = strchr(arg, '/'); |
| 109 | |
| 110 | *realms = 0; |
| 111 | if (p) { |
| 112 | *p = 0; |
| 113 | if (rtnl_rtrealm_a2n(realms, arg)) { |
| 114 | *p = '/'; |
| 115 | return -1; |
| 116 | } |
| 117 | *realms <<= 16; |
| 118 | *p = '/'; |
| 119 | arg = p+1; |
| 120 | } |
| 121 | if (*arg && rtnl_rtrealm_a2n(&realm, arg)) |
| 122 | return -1; |
| 123 | *realms |= realm; |
| 124 | return 0; |
| 125 | } |