ip.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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(int ATTRIBUTE_UNUSED ac, char ATTRIBUTE_UNUSED **av)
  24. {
  25. bb_show_usage();
  26. }
  27. static int (*ip_func)(int argc, char **argv) = ip_print_help;
  28. static int ip_do(int argc, char **argv)
  29. {
  30. ip_parse_common_args(&argc, &argv);
  31. return ip_func(argc-1, argv+1);
  32. }
  33. #if ENABLE_FEATURE_IP_ADDRESS
  34. int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  35. int ipaddr_main(int argc, char **argv)
  36. {
  37. ip_func = do_ipaddr;
  38. return ip_do(argc, argv);
  39. }
  40. #endif
  41. #if ENABLE_FEATURE_IP_LINK
  42. int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  43. int iplink_main(int argc, char **argv)
  44. {
  45. ip_func = do_iplink;
  46. return ip_do(argc, argv);
  47. }
  48. #endif
  49. #if ENABLE_FEATURE_IP_ROUTE
  50. int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  51. int iproute_main(int argc, char **argv)
  52. {
  53. ip_func = do_iproute;
  54. return ip_do(argc, argv);
  55. }
  56. #endif
  57. #if ENABLE_FEATURE_IP_RULE
  58. int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  59. int iprule_main(int argc, char **argv)
  60. {
  61. ip_func = do_iprule;
  62. return ip_do(argc, argv);
  63. }
  64. #endif
  65. #if ENABLE_FEATURE_IP_TUNNEL
  66. int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  67. int iptunnel_main(int argc, char **argv)
  68. {
  69. ip_func = do_iptunnel;
  70. return ip_do(argc, argv);
  71. }
  72. #endif
  73. int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  74. int ip_main(int argc, char **argv)
  75. {
  76. static const char keywords[] ALIGN1 =
  77. USE_FEATURE_IP_ADDRESS("address\0")
  78. USE_FEATURE_IP_ROUTE("route\0")
  79. USE_FEATURE_IP_LINK("link\0")
  80. USE_FEATURE_IP_TUNNEL("tunnel\0" "tunl\0")
  81. USE_FEATURE_IP_RULE("rule\0")
  82. ;
  83. enum {
  84. USE_FEATURE_IP_ADDRESS(IP_addr,)
  85. USE_FEATURE_IP_ROUTE(IP_route,)
  86. USE_FEATURE_IP_LINK(IP_link,)
  87. USE_FEATURE_IP_TUNNEL(IP_tunnel, IP_tunl,)
  88. USE_FEATURE_IP_RULE(IP_rule,)
  89. IP_none
  90. };
  91. ip_parse_common_args(&argc, &argv);
  92. if (argc > 1) {
  93. int key = index_in_substrings(keywords, argv[1]);
  94. argc -= 2;
  95. argv += 2;
  96. #if ENABLE_FEATURE_IP_ADDRESS
  97. if (key == IP_addr)
  98. ip_func = do_ipaddr;
  99. #endif
  100. #if ENABLE_FEATURE_IP_ROUTE
  101. if (key == IP_route)
  102. ip_func = do_iproute;
  103. #endif
  104. #if ENABLE_FEATURE_IP_LINK
  105. if (key == IP_link)
  106. ip_func = do_iplink;
  107. #endif
  108. #if ENABLE_FEATURE_IP_TUNNEL
  109. if (key == IP_tunnel || key == IP_tunl)
  110. ip_func = do_iptunnel;
  111. #endif
  112. #if ENABLE_FEATURE_IP_RULE
  113. if (key == IP_rule)
  114. ip_func = do_iprule;
  115. #endif
  116. }
  117. return ip_func(argc, argv);
  118. }
  119. #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */