ip.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ip.c "ip" utility frontend.
  4. *
  5. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  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
  13. * Bernhard Fischer rewrote to use index_in_substr_array
  14. */
  15. #include "libbb.h"
  16. #include "libiproute/utils.h"
  17. #include "libiproute/ip_common.h"
  18. #if ENABLE_FEATURE_IP_ADDRESS \
  19. || ENABLE_FEATURE_IP_ROUTE \
  20. || ENABLE_FEATURE_IP_LINK \
  21. || ENABLE_FEATURE_IP_TUNNEL \
  22. || ENABLE_FEATURE_IP_RULE
  23. static int ATTRIBUTE_NORETURN ip_print_help(char ATTRIBUTE_UNUSED **argv)
  24. {
  25. bb_show_usage();
  26. }
  27. static int ip_do(int (*ip_func)(char **argv), char **argv)
  28. {
  29. argv = ip_parse_common_args(argv);
  30. return ip_func(argv);
  31. }
  32. #if ENABLE_FEATURE_IP_ADDRESS
  33. int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  34. int ipaddr_main(int argc ATTRIBUTE_UNUSED, char **argv)
  35. {
  36. return ip_do(do_ipaddr, argv);
  37. }
  38. #endif
  39. #if ENABLE_FEATURE_IP_LINK
  40. int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  41. int iplink_main(int argc ATTRIBUTE_UNUSED, char **argv)
  42. {
  43. return ip_do(do_iplink, argv);
  44. }
  45. #endif
  46. #if ENABLE_FEATURE_IP_ROUTE
  47. int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  48. int iproute_main(int argc ATTRIBUTE_UNUSED, char **argv)
  49. {
  50. return ip_do(do_iproute, argv);
  51. }
  52. #endif
  53. #if ENABLE_FEATURE_IP_RULE
  54. int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  55. int iprule_main(int argc ATTRIBUTE_UNUSED, char **argv)
  56. {
  57. return ip_do(do_iprule, argv);
  58. }
  59. #endif
  60. #if ENABLE_FEATURE_IP_TUNNEL
  61. int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  62. int iptunnel_main(int argc ATTRIBUTE_UNUSED, char **argv)
  63. {
  64. return ip_do(do_iptunnel, argv);
  65. }
  66. #endif
  67. int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  68. int ip_main(int argc ATTRIBUTE_UNUSED, char **argv)
  69. {
  70. static const char keywords[] ALIGN1 =
  71. USE_FEATURE_IP_ADDRESS("address\0")
  72. USE_FEATURE_IP_ROUTE("route\0")
  73. USE_FEATURE_IP_LINK("link\0")
  74. USE_FEATURE_IP_TUNNEL("tunnel\0" "tunl\0")
  75. USE_FEATURE_IP_RULE("rule\0")
  76. ;
  77. enum {
  78. USE_FEATURE_IP_ADDRESS(IP_addr,)
  79. USE_FEATURE_IP_ROUTE(IP_route,)
  80. USE_FEATURE_IP_LINK(IP_link,)
  81. USE_FEATURE_IP_TUNNEL(IP_tunnel, IP_tunl,)
  82. USE_FEATURE_IP_RULE(IP_rule,)
  83. IP_none
  84. };
  85. int (*ip_func)(char**) = ip_print_help;
  86. argv = ip_parse_common_args(argv + 1);
  87. if (*argv) {
  88. int key = index_in_substrings(keywords, *argv);
  89. argv++;
  90. #if ENABLE_FEATURE_IP_ADDRESS
  91. if (key == IP_addr)
  92. ip_func = do_ipaddr;
  93. #endif
  94. #if ENABLE_FEATURE_IP_ROUTE
  95. if (key == IP_route)
  96. ip_func = do_iproute;
  97. #endif
  98. #if ENABLE_FEATURE_IP_LINK
  99. if (key == IP_link)
  100. ip_func = do_iplink;
  101. #endif
  102. #if ENABLE_FEATURE_IP_TUNNEL
  103. if (key == IP_tunnel || key == IP_tunl)
  104. ip_func = do_iptunnel;
  105. #endif
  106. #if ENABLE_FEATURE_IP_RULE
  107. if (key == IP_rule)
  108. ip_func = do_iprule;
  109. #endif
  110. }
  111. return ip_func(argv);
  112. }
  113. #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */