ip.c 2.6 KB

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