rtm_map.c 2.6 KB

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