ip.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * "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. * Changes:
  10. * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  11. * Bernhard Reutner-Fischer rewrote to use index_in_substr_array
  12. */
  13. #include "libbb.h"
  14. #include "libiproute/utils.h"
  15. #include "libiproute/ip_common.h"
  16. #if ENABLE_FEATURE_IP_ADDRESS \
  17. || ENABLE_FEATURE_IP_ROUTE \
  18. || ENABLE_FEATURE_IP_LINK \
  19. || ENABLE_FEATURE_IP_TUNNEL \
  20. || ENABLE_FEATURE_IP_RULE
  21. static int ip_print_help(char **argv UNUSED_PARAM)
  22. {
  23. bb_show_usage();
  24. }
  25. typedef int (*ip_func_ptr_t)(char**);
  26. static int ip_do(ip_func_ptr_t ip_func, char **argv)
  27. {
  28. argv = ip_parse_common_args(argv + 1);
  29. return ip_func(argv);
  30. }
  31. #if ENABLE_FEATURE_IP_ADDRESS
  32. int ipaddr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  33. int ipaddr_main(int argc UNUSED_PARAM, char **argv)
  34. {
  35. return ip_do(do_ipaddr, argv);
  36. }
  37. #endif
  38. #if ENABLE_FEATURE_IP_LINK
  39. int iplink_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int iplink_main(int argc UNUSED_PARAM, char **argv)
  41. {
  42. return ip_do(do_iplink, argv);
  43. }
  44. #endif
  45. #if ENABLE_FEATURE_IP_ROUTE
  46. int iproute_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  47. int iproute_main(int argc UNUSED_PARAM, char **argv)
  48. {
  49. return ip_do(do_iproute, argv);
  50. }
  51. #endif
  52. #if ENABLE_FEATURE_IP_RULE
  53. int iprule_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  54. int iprule_main(int argc UNUSED_PARAM, char **argv)
  55. {
  56. return ip_do(do_iprule, argv);
  57. }
  58. #endif
  59. #if ENABLE_FEATURE_IP_TUNNEL
  60. int iptunnel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  61. int iptunnel_main(int argc UNUSED_PARAM, char **argv)
  62. {
  63. return ip_do(do_iptunnel, argv);
  64. }
  65. #endif
  66. int ip_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  67. int ip_main(int argc UNUSED_PARAM, char **argv)
  68. {
  69. static const char keywords[] ALIGN1 =
  70. IF_FEATURE_IP_ADDRESS("address\0")
  71. IF_FEATURE_IP_ROUTE("route\0")
  72. IF_FEATURE_IP_ROUTE("r\0")
  73. IF_FEATURE_IP_LINK("link\0")
  74. IF_FEATURE_IP_TUNNEL("tunnel\0")
  75. IF_FEATURE_IP_TUNNEL("tunl\0")
  76. IF_FEATURE_IP_RULE("rule\0")
  77. ;
  78. static const ip_func_ptr_t ip_func_ptrs[] = {
  79. ip_print_help,
  80. IF_FEATURE_IP_ADDRESS(do_ipaddr,)
  81. IF_FEATURE_IP_ROUTE(do_iproute,)
  82. IF_FEATURE_IP_ROUTE(do_iproute,)
  83. IF_FEATURE_IP_LINK(do_iplink,)
  84. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  85. IF_FEATURE_IP_TUNNEL(do_iptunnel,)
  86. IF_FEATURE_IP_RULE(do_iprule,)
  87. };
  88. ip_func_ptr_t ip_func;
  89. int key;
  90. argv = ip_parse_common_args(argv + 1);
  91. key = *argv ? index_in_substrings(keywords, *argv++) : -1;
  92. ip_func = ip_func_ptrs[key + 1];
  93. return ip_func(argv);
  94. }
  95. #endif /* any of ENABLE_FEATURE_IP_xxx is 1 */