"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 | * ip.c "ip" utility frontend. |
| 4 | * |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 5 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
| 7 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 8 | * |
| 9 | * |
| 10 | * Changes: |
| 11 | * |
| 12 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 13 | * Bernhard Reutner-Fischer rewrote to use index_in_substr_array |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 18 | #include "libiproute/utils.h" |
| 19 | #include "libiproute/ip_common.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | d46e6d1 | 2007-05-17 12:58:30 +0000 | [diff] [blame] | 21 | #if ENABLE_FEATURE_IP_ADDRESS \ |
| 22 | || ENABLE_FEATURE_IP_ROUTE \ |
| 23 | || ENABLE_FEATURE_IP_LINK \ |
| 24 | || ENABLE_FEATURE_IP_TUNNEL \ |
| 25 | || ENABLE_FEATURE_IP_RULE |
| 26 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 27 | static int NORETURN ip_print_help(char **argv UNUSED_PARAM) |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 28 | { |
| 29 | bb_show_usage(); |
| 30 | } |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 32 | static int ip_do(int (*ip_func)(char **argv), char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 33 | { |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 34 | argv = ip_parse_common_args(argv); |
| 35 | return ip_func(argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | #if ENABLE_FEATURE_IP_ADDRESS |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 39 | int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 40 | int ipaddr_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 41 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 42 | return ip_do(do_ipaddr, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 43 | } |
| 44 | #endif |
| 45 | #if ENABLE_FEATURE_IP_LINK |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 46 | int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 47 | int iplink_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 49 | return ip_do(do_iplink, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 50 | } |
| 51 | #endif |
| 52 | #if ENABLE_FEATURE_IP_ROUTE |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 53 | int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 54 | int iproute_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 55 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 56 | return ip_do(do_iproute, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 57 | } |
| 58 | #endif |
| 59 | #if ENABLE_FEATURE_IP_RULE |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 60 | int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 61 | int iprule_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 62 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 63 | return ip_do(do_iprule, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 64 | } |
| 65 | #endif |
| 66 | #if ENABLE_FEATURE_IP_TUNNEL |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 67 | int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 68 | int iptunnel_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 69 | { |
Denis Vlasenko | 2570b2e | 2008-03-28 01:00:09 +0000 | [diff] [blame] | 70 | return ip_do(do_iptunnel, argv); |
Bernhard Reutner-Fischer | 07c394e | 2007-04-10 20:11:12 +0000 | [diff] [blame] | 71 | } |
| 72 | #endif |
| 73 | |
| 74 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 75 | int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 76 | int ip_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 77 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 78 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 79 | USE_FEATURE_IP_ADDRESS("address\0") |
| 80 | USE_FEATURE_IP_ROUTE("route\0") |
| 81 | USE_FEATURE_IP_LINK("link\0") |
| 82 | USE_FEATURE_IP_TUNNEL("tunnel\0" "tunl\0") |
| 83 | USE_FEATURE_IP_RULE("rule\0") |
| 84 | ; |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 85 | enum { |
| 86 | USE_FEATURE_IP_ADDRESS(IP_addr,) |
| 87 | USE_FEATURE_IP_ROUTE(IP_route,) |
| 88 | USE_FEATURE_IP_LINK(IP_link,) |
| 89 | USE_FEATURE_IP_TUNNEL(IP_tunnel, IP_tunl,) |
| 90 | USE_FEATURE_IP_RULE(IP_rule,) |
| 91 | IP_none |
| 92 | }; |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 93 | int (*ip_func)(char**) = ip_print_help; |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 95 | argv = ip_parse_common_args(argv + 1); |
| 96 | if (*argv) { |
| 97 | int key = index_in_substrings(keywords, *argv); |
| 98 | argv++; |
Bernhard Reutner-Fischer | e87d795 | 2007-04-10 18:43:27 +0000 | [diff] [blame] | 99 | #if ENABLE_FEATURE_IP_ADDRESS |
| 100 | if (key == IP_addr) |
| 101 | ip_func = do_ipaddr; |
| 102 | #endif |
| 103 | #if ENABLE_FEATURE_IP_ROUTE |
| 104 | if (key == IP_route) |
| 105 | ip_func = do_iproute; |
| 106 | #endif |
| 107 | #if ENABLE_FEATURE_IP_LINK |
| 108 | if (key == IP_link) |
| 109 | ip_func = do_iplink; |
| 110 | #endif |
| 111 | #if ENABLE_FEATURE_IP_TUNNEL |
| 112 | if (key == IP_tunnel || key == IP_tunl) |
| 113 | ip_func = do_iptunnel; |
| 114 | #endif |
| 115 | #if ENABLE_FEATURE_IP_RULE |
| 116 | if (key == IP_rule) |
| 117 | ip_func = do_iprule; |
| 118 | #endif |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 119 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 120 | return ip_func(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 121 | } |
Denis Vlasenko | d46e6d1 | 2007-05-17 12:58:30 +0000 | [diff] [blame] | 122 | |
| 123 | #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */ |