blob: 7a0f308f0caf5f3f43053954809e4d61c4a8dedf [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
3 * ip.c "ip" utility frontend.
4 *
Bernhard Reutner-Fischerab187822005-10-26 10:47:26 +00005 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
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-Fischer6c4dade2008-09-25 12:13:34 +000013 * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014 */
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
Bernhard Reutner-Fischerab187822005-10-26 10:47:26 +000018#include "libiproute/utils.h"
19#include "libiproute/ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020
Denis Vlasenkod46e6d12007-05-17 12:58:30 +000021#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 Vlasenkoa60f84e2008-07-05 09:18:54 +000027static int NORETURN ip_print_help(char **argv UNUSED_PARAM)
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +000028{
29 bb_show_usage();
30}
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000031
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +000032static int ip_do(int (*ip_func)(char **argv), char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000033{
Denis Vlasenko3246c0d2008-12-04 12:15:19 +000034 argv = ip_parse_common_args(argv + 1);
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +000035 return ip_func(argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000036}
37
38#if ENABLE_FEATURE_IP_ADDRESS
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000039int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000040int ipaddr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000041{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000042 return ip_do(do_ipaddr, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000043}
44#endif
45#if ENABLE_FEATURE_IP_LINK
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000046int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000047int iplink_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000048{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000049 return ip_do(do_iplink, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000050}
51#endif
52#if ENABLE_FEATURE_IP_ROUTE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000053int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000054int iproute_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000055{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000056 return ip_do(do_iproute, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000057}
58#endif
59#if ENABLE_FEATURE_IP_RULE
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061int iprule_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000062{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000063 return ip_do(do_iprule, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000064}
65#endif
66#if ENABLE_FEATURE_IP_TUNNEL
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000067int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000068int iptunnel_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000069{
Denis Vlasenko2570b2e2008-03-28 01:00:09 +000070 return ip_do(do_iptunnel, argv);
Bernhard Reutner-Fischer07c394e2007-04-10 20:11:12 +000071}
72#endif
73
74
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000075int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000076int ip_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf112daa2002-12-01 23:04:06 +000077{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000078 static const char keywords[] ALIGN1 =
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000079 IF_FEATURE_IP_ADDRESS("address\0")
80 IF_FEATURE_IP_ROUTE("route\0")
81 IF_FEATURE_IP_LINK("link\0")
82 IF_FEATURE_IP_TUNNEL("tunnel\0" "tunl\0")
83 IF_FEATURE_IP_RULE("rule\0")
Denis Vlasenko990d0f62007-07-24 15:54:42 +000084 ;
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +000085 enum {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000086 IF_FEATURE_IP_ADDRESS(IP_addr,)
87 IF_FEATURE_IP_ROUTE(IP_route,)
88 IF_FEATURE_IP_LINK(IP_link,)
89 IF_FEATURE_IP_TUNNEL(IP_tunnel, IP_tunl,)
90 IF_FEATURE_IP_RULE(IP_rule,)
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +000091 IP_none
92 };
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000093 int (*ip_func)(char**) = ip_print_help;
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +000094
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +000095 argv = ip_parse_common_args(argv + 1);
96 if (*argv) {
97 int key = index_in_substrings(keywords, *argv);
98 argv++;
Bernhard Reutner-Fischere87d7952007-04-10 18:43:27 +000099#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 McGrathf112daa2002-12-01 23:04:06 +0000119 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000120 return ip_func(argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121}
Denis Vlasenkod46e6d12007-05-17 12:58:30 +0000122
123#endif /* any of ENABLE_FEATURE_IP_xxx is 1 */