rtm_map.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. const char* FAST_FUNC rtnl_rtntype_n2a(int id)
  14. {
  15. switch (id) {
  16. case RTN_UNSPEC:
  17. return "none";
  18. case RTN_UNICAST:
  19. return "unicast";
  20. case RTN_LOCAL:
  21. return "local";
  22. case RTN_BROADCAST:
  23. return "broadcast";
  24. case RTN_ANYCAST:
  25. return "anycast";
  26. case RTN_MULTICAST:
  27. return "multicast";
  28. case RTN_BLACKHOLE:
  29. return "blackhole";
  30. case RTN_UNREACHABLE:
  31. return "unreachable";
  32. case RTN_PROHIBIT:
  33. return "prohibit";
  34. case RTN_THROW:
  35. return "throw";
  36. case RTN_NAT:
  37. return "nat";
  38. case RTN_XRESOLVE:
  39. return "xresolve";
  40. default:
  41. return itoa(id);
  42. }
  43. }
  44. int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
  45. {
  46. static const char keywords[] ALIGN1 =
  47. "local\0""nat\0""broadcast\0""brd\0""anycast\0"
  48. "multicast\0""prohibit\0""unreachable\0""blackhole\0"
  49. "xresolve\0""unicast\0""throw\0";
  50. enum {
  51. ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
  52. ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
  53. ARG_xresolve, ARG_unicast, ARG_throw
  54. };
  55. const smalluint key = index_in_substrings(keywords, arg) + 1;
  56. char *end;
  57. unsigned long res;
  58. if (key == ARG_local)
  59. res = RTN_LOCAL;
  60. else if (key == ARG_nat)
  61. res = RTN_NAT;
  62. else if (key == ARG_broadcast || key == ARG_brd)
  63. res = RTN_BROADCAST;
  64. else if (key == ARG_anycast)
  65. res = RTN_ANYCAST;
  66. else if (key == ARG_multicast)
  67. res = RTN_MULTICAST;
  68. else if (key == ARG_prohibit)
  69. res = RTN_PROHIBIT;
  70. else if (key == ARG_unreachable)
  71. res = RTN_UNREACHABLE;
  72. else if (key == ARG_blackhole)
  73. res = RTN_BLACKHOLE;
  74. else if (key == ARG_xresolve)
  75. res = RTN_XRESOLVE;
  76. else if (key == ARG_unicast)
  77. res = RTN_UNICAST;
  78. else if (key == ARG_throw)
  79. res = RTN_THROW;
  80. else {
  81. res = strtoul(arg, &end, 0);
  82. if (end == arg || *end || res > 255)
  83. return -1;
  84. }
  85. *id = res;
  86. return 0;
  87. }
  88. int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
  89. {
  90. uint32_t realm = 0;
  91. char *p = strchr(arg, '/');
  92. *realms = 0;
  93. if (p) {
  94. *p = 0;
  95. if (rtnl_rtrealm_a2n(realms, arg)) {
  96. *p = '/';
  97. return -1;
  98. }
  99. *realms <<= 16;
  100. *p = '/';
  101. arg = p+1;
  102. }
  103. if (*arg && rtnl_rtrealm_a2n(&realm, arg))
  104. return -1;
  105. *realms |= realm;
  106. return 0;
  107. }