+/* vi: set sw=4 ts=4: */
/*
- * ip.c "ip" utility frontend.
- *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
- *
- * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*
+ * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*
* Changes:
- *
- * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
+ * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
+ * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <syslog.h>
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <string.h>
+#include "libbb.h"
#include "libiproute/utils.h"
#include "libiproute/ip_common.h"
-#include "busybox.h"
-
-#if 0
-int preferred_family = AF_UNSPEC;
-int oneline = 0;
-char * _SL_ = NULL;
+#if ENABLE_FEATURE_IP_ADDRESS \
+ || ENABLE_FEATURE_IP_ROUTE \
+ || ENABLE_FEATURE_IP_LINK \
+ || ENABLE_FEATURE_IP_TUNNEL \
+ || ENABLE_FEATURE_IP_RULE
-void ip_parse_common_args(int *argcp, char ***argvp)
+static int FAST_FUNC ip_print_help(char **argv UNUSED_PARAM)
{
- int argc = *argcp;
- char **argv = *argvp;
-
- while (argc > 1) {
- char *opt = argv[1];
-
- if (strcmp(opt,"--") == 0) {
- argc--; argv++;
- break;
- }
+ bb_show_usage();
+}
- if (opt[0] != '-')
- break;
+typedef int FAST_FUNC (*ip_func_ptr_t)(char**);
- if (opt[1] == '-')
- opt++;
+static int ip_do(ip_func_ptr_t ip_func, char **argv)
+{
+ argv = ip_parse_common_args(argv + 1);
+ return ip_func(argv);
+}
- if (matches(opt, "-family") == 0) {
- argc--;
- argv++;
- if (strcmp(argv[1], "inet") == 0)
- preferred_family = AF_INET;
- else if (strcmp(argv[1], "inet6") == 0)
- preferred_family = AF_INET6;
- else if (strcmp(argv[1], "link") == 0)
- preferred_family = AF_PACKET;
- else
- invarg(bb_msg_invalid_arg, argv[1], "-family");
- } else if (strcmp(opt, "-4") == 0) {
- preferred_family = AF_INET;
- } else if (strcmp(opt, "-6") == 0) {
- preferred_family = AF_INET6;
- } else if (strcmp(opt, "-0") == 0) {
- preferred_family = AF_PACKET;
- } else if (matches(opt, "-oneline") == 0) {
- ++oneline;
- } else {
- bb_show_usage();
- }
- argc--; argv++;
- }
- _SL_ = oneline ? "\\" : "\n" ;
+#if ENABLE_FEATURE_IP_ADDRESS
+int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int ipaddr_main(int argc UNUSED_PARAM, char **argv)
+{
+ return ip_do(do_ipaddr, argv);
}
#endif
-
-int ip_main(int argc, char **argv)
+#if ENABLE_FEATURE_IP_LINK
+int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int iplink_main(int argc UNUSED_PARAM, char **argv)
{
- int ret = EXIT_FAILURE;
-
- ip_parse_common_args(&argc, &argv);
-
- if (argc > 1) {
-#ifdef CONFIG_FEATURE_IP_ADDRESS
- if (matches(argv[1], "address") == 0) {
- ret = do_ipaddr(argc-2, argv+2);
- }
+ return ip_do(do_iplink, argv);
+}
#endif
-#ifdef CONFIG_FEATURE_IP_ROUTE
- if (matches(argv[1], "route") == 0) {
- ret = do_iproute(argc-2, argv+2);
- }
+#if ENABLE_FEATURE_IP_ROUTE
+int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int iproute_main(int argc UNUSED_PARAM, char **argv)
+{
+ return ip_do(do_iproute, argv);
+}
#endif
-#ifdef CONFIG_FEATURE_IP_LINK
- if (matches(argv[1], "link") == 0) {
- ret = do_iplink(argc-2, argv+2);
- }
+#if ENABLE_FEATURE_IP_RULE
+int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int iprule_main(int argc UNUSED_PARAM, char **argv)
+{
+ return ip_do(do_iprule, argv);
+}
#endif
-#ifdef CONFIG_FEATURE_IP_TUNNEL
- if (matches(argv[1], "tunnel") == 0 || strcmp(argv[1], "tunl") == 0) {
- ret = do_iptunnel(argc-2, argv+2);
- }
+#if ENABLE_FEATURE_IP_TUNNEL
+int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int iptunnel_main(int argc UNUSED_PARAM, char **argv)
+{
+ return ip_do(do_iptunnel, argv);
+}
#endif
- }
- if (ret) {
- bb_show_usage();
- }
- return(EXIT_SUCCESS);
+
+
+int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int ip_main(int argc UNUSED_PARAM, char **argv)
+{
+ static const char keywords[] ALIGN1 =
+ IF_FEATURE_IP_ADDRESS("address\0")
+ IF_FEATURE_IP_ROUTE("route\0")
+ IF_FEATURE_IP_ROUTE("r\0")
+ IF_FEATURE_IP_LINK("link\0")
+ IF_FEATURE_IP_TUNNEL("tunnel\0")
+ IF_FEATURE_IP_TUNNEL("tunl\0")
+ IF_FEATURE_IP_RULE("rule\0")
+ ;
+ static const ip_func_ptr_t ip_func_ptrs[] = {
+ ip_print_help,
+ IF_FEATURE_IP_ADDRESS(do_ipaddr,)
+ IF_FEATURE_IP_ROUTE(do_iproute,)
+ IF_FEATURE_IP_ROUTE(do_iproute,)
+ IF_FEATURE_IP_LINK(do_iplink,)
+ IF_FEATURE_IP_TUNNEL(do_iptunnel,)
+ IF_FEATURE_IP_TUNNEL(do_iptunnel,)
+ IF_FEATURE_IP_RULE(do_iprule,)
+ };
+ ip_func_ptr_t ip_func;
+ int key;
+
+ argv = ip_parse_common_args(argv + 1);
+ key = *argv ? index_in_substrings(keywords, *argv++) : -1;
+ ip_func = ip_func_ptrs[key + 1];
+
+ return ip_func(argv);
}
+
+#endif /* any of ENABLE_FEATURE_IP_xxx is 1 */