blob: c763da049c619535e6fee3465d32acbba5f627dd
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
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. |
7 | * |
8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
9 | */ |
10 | |
11 | #include "libbb.h" |
12 | #include "rt_names.h" |
13 | #include "utils.h" |
14 | |
15 | const char* FAST_FUNC rtnl_rtntype_n2a(int id) |
16 | { |
17 | switch (id) { |
18 | case RTN_UNSPEC: |
19 | return "none"; |
20 | case RTN_UNICAST: |
21 | return "unicast"; |
22 | case RTN_LOCAL: |
23 | return "local"; |
24 | case RTN_BROADCAST: |
25 | return "broadcast"; |
26 | case RTN_ANYCAST: |
27 | return "anycast"; |
28 | case RTN_MULTICAST: |
29 | return "multicast"; |
30 | case RTN_BLACKHOLE: |
31 | return "blackhole"; |
32 | case RTN_UNREACHABLE: |
33 | return "unreachable"; |
34 | case RTN_PROHIBIT: |
35 | return "prohibit"; |
36 | case RTN_THROW: |
37 | return "throw"; |
38 | case RTN_NAT: |
39 | return "nat"; |
40 | case RTN_XRESOLVE: |
41 | return "xresolve"; |
42 | default: |
43 | return itoa(id); |
44 | } |
45 | } |
46 | |
47 | |
48 | int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg) |
49 | { |
50 | static const char keywords[] ALIGN1 = |
51 | "local\0""nat\0""broadcast\0""brd\0""anycast\0" |
52 | "multicast\0""prohibit\0""unreachable\0""blackhole\0" |
53 | "xresolve\0""unicast\0""throw\0"; |
54 | enum { |
55 | ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast, |
56 | ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole, |
57 | ARG_xresolve, ARG_unicast, ARG_throw |
58 | }; |
59 | const smalluint key = index_in_substrings(keywords, arg) + 1; |
60 | char *end; |
61 | unsigned long res; |
62 | |
63 | if (key == ARG_local) |
64 | res = RTN_LOCAL; |
65 | else if (key == ARG_nat) |
66 | res = RTN_NAT; |
67 | else if (key == ARG_broadcast || key == ARG_brd) |
68 | res = RTN_BROADCAST; |
69 | else if (key == ARG_anycast) |
70 | res = RTN_ANYCAST; |
71 | else if (key == ARG_multicast) |
72 | res = RTN_MULTICAST; |
73 | else if (key == ARG_prohibit) |
74 | res = RTN_PROHIBIT; |
75 | else if (key == ARG_unreachable) |
76 | res = RTN_UNREACHABLE; |
77 | else if (key == ARG_blackhole) |
78 | res = RTN_BLACKHOLE; |
79 | else if (key == ARG_xresolve) |
80 | res = RTN_XRESOLVE; |
81 | else if (key == ARG_unicast) |
82 | res = RTN_UNICAST; |
83 | else if (key == ARG_throw) |
84 | res = RTN_THROW; |
85 | else { |
86 | res = strtoul(arg, &end, 0); |
87 | if (end == arg || *end || res > 255) |
88 | return -1; |
89 | } |
90 | *id = res; |
91 | return 0; |
92 | } |
93 | |
94 | int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg) |
95 | { |
96 | uint32_t realm = 0; |
97 | char *p = strchr(arg, '/'); |
98 | |
99 | *realms = 0; |
100 | if (p) { |
101 | *p = 0; |
102 | if (rtnl_rtrealm_a2n(realms, arg)) { |
103 | *p = '/'; |
104 | return -1; |
105 | } |
106 | *realms <<= 16; |
107 | *p = '/'; |
108 | arg = p+1; |
109 | } |
110 | if (*arg && rtnl_rtrealm_a2n(&realm, arg)) |
111 | return -1; |
112 | *realms |= realm; |
113 | return 0; |
114 | } |
115 |