rtm_map.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. */
  10. #include "libbb.h"
  11. #include "rt_names.h"
  12. #include "utils.h"
  13. static const char keywords[] ALIGN1 =
  14. "local\0""nat\0""broadcast\0""brd\0""anycast\0"
  15. "multicast\0""prohibit\0""unreachable\0""blackhole\0"
  16. "xresolve\0""unicast\0""throw\0";
  17. enum {
  18. ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
  19. ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
  20. ARG_xresolve, ARG_unicast, ARG_throw
  21. };
  22. #define str_local keywords
  23. #define str_nat (str_local + sizeof("local"))
  24. #define str_broadcast (str_nat + sizeof("nat"))
  25. #define str_brd (str_broadcast + sizeof("broadcast"))
  26. #define str_anycast (str_brd + sizeof("brd"))
  27. #define str_multicast (str_anycast + sizeof("anycast"))
  28. #define str_prohibit (str_multicast + sizeof("multicast"))
  29. #define str_unreachable (str_prohibit + sizeof("prohibit"))
  30. #define str_blackhole (str_unreachable + sizeof("unreachable"))
  31. #define str_xresolve (str_blackhole + sizeof("blackhole"))
  32. #define str_unicast (str_xresolve + sizeof("xresolve"))
  33. #define str_throw (str_unicast + sizeof("unicast"))
  34. const char* FAST_FUNC rtnl_rtntype_n2a(int id)
  35. {
  36. switch (id) {
  37. case RTN_UNSPEC:
  38. return "none";
  39. case RTN_UNICAST:
  40. return str_unicast;
  41. case RTN_LOCAL:
  42. return str_local;
  43. case RTN_BROADCAST:
  44. return str_broadcast;
  45. case RTN_ANYCAST:
  46. return str_anycast;
  47. case RTN_MULTICAST:
  48. return str_multicast;
  49. case RTN_BLACKHOLE:
  50. return str_blackhole;
  51. case RTN_UNREACHABLE:
  52. return str_unreachable;
  53. case RTN_PROHIBIT:
  54. return str_prohibit;
  55. case RTN_THROW:
  56. return str_throw;
  57. case RTN_NAT:
  58. return str_nat;
  59. case RTN_XRESOLVE:
  60. return str_xresolve;
  61. default:
  62. return itoa(id);
  63. }
  64. }
  65. int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
  66. {
  67. const smalluint key = index_in_substrings(keywords, arg) + 1;
  68. char *end;
  69. unsigned long res;
  70. if (key == ARG_local)
  71. res = RTN_LOCAL;
  72. else if (key == ARG_nat)
  73. res = RTN_NAT;
  74. else if (key == ARG_broadcast || key == ARG_brd)
  75. res = RTN_BROADCAST;
  76. else if (key == ARG_anycast)
  77. res = RTN_ANYCAST;
  78. else if (key == ARG_multicast)
  79. res = RTN_MULTICAST;
  80. else if (key == ARG_prohibit)
  81. res = RTN_PROHIBIT;
  82. else if (key == ARG_unreachable)
  83. res = RTN_UNREACHABLE;
  84. else if (key == ARG_blackhole)
  85. res = RTN_BLACKHOLE;
  86. else if (key == ARG_xresolve)
  87. res = RTN_XRESOLVE;
  88. else if (key == ARG_unicast)
  89. res = RTN_UNICAST;
  90. else if (key == ARG_throw)
  91. res = RTN_THROW;
  92. else {
  93. res = strtoul(arg, &end, 0);
  94. if (end == arg || *end || res > 255)
  95. return -1;
  96. }
  97. *id = res;
  98. return 0;
  99. }
  100. int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
  101. {
  102. uint32_t realm = 0;
  103. char *p = strchr(arg, '/');
  104. *realms = 0;
  105. if (p) {
  106. *p = 0;
  107. if (rtnl_rtrealm_a2n(realms, arg)) {
  108. *p = '/';
  109. return -1;
  110. }
  111. *realms <<= 16;
  112. *p = '/';
  113. arg = p+1;
  114. }
  115. if (*arg && rtnl_rtrealm_a2n(&realm, arg))
  116. return -1;
  117. *realms |= realm;
  118. return 0;
  119. }