rtm_map.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, char *buf)
  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. /* buf is SPRINT_BSIZE big */
  42. sprintf(buf, "%d", id);
  43. return buf;
  44. }
  45. }
  46. int FAST_FUNC rtnl_rtntype_a2n(int *id, char *arg)
  47. {
  48. static const char keywords[] ALIGN1 =
  49. "local\0""nat\0""broadcast\0""brd\0""anycast\0"
  50. "multicast\0""prohibit\0""unreachable\0""blackhole\0"
  51. "xresolve\0""unicast\0""throw\0";
  52. enum {
  53. ARG_local = 1, ARG_nat, ARG_broadcast, ARG_brd, ARG_anycast,
  54. ARG_multicast, ARG_prohibit, ARG_unreachable, ARG_blackhole,
  55. ARG_xresolve, ARG_unicast, ARG_throw
  56. };
  57. const smalluint key = index_in_substrings(keywords, arg) + 1;
  58. char *end;
  59. unsigned long res;
  60. if (key == ARG_local)
  61. res = RTN_LOCAL;
  62. else if (key == ARG_nat)
  63. res = RTN_NAT;
  64. else if (key == ARG_broadcast || key == ARG_brd)
  65. res = RTN_BROADCAST;
  66. else if (key == ARG_anycast)
  67. res = RTN_ANYCAST;
  68. else if (key == ARG_multicast)
  69. res = RTN_MULTICAST;
  70. else if (key == ARG_prohibit)
  71. res = RTN_PROHIBIT;
  72. else if (key == ARG_unreachable)
  73. res = RTN_UNREACHABLE;
  74. else if (key == ARG_blackhole)
  75. res = RTN_BLACKHOLE;
  76. else if (key == ARG_xresolve)
  77. res = RTN_XRESOLVE;
  78. else if (key == ARG_unicast)
  79. res = RTN_UNICAST;
  80. else if (key == ARG_throw)
  81. res = RTN_THROW;
  82. else {
  83. res = strtoul(arg, &end, 0);
  84. if (end == arg || *end || res > 255)
  85. return -1;
  86. }
  87. *id = res;
  88. return 0;
  89. }
  90. int FAST_FUNC get_rt_realms(uint32_t *realms, char *arg)
  91. {
  92. uint32_t realm = 0;
  93. char *p = strchr(arg, '/');
  94. *realms = 0;
  95. if (p) {
  96. *p = 0;
  97. if (rtnl_rtrealm_a2n(realms, arg)) {
  98. *p = '/';
  99. return -1;
  100. }
  101. *realms <<= 16;
  102. *p = '/';
  103. arg = p+1;
  104. }
  105. if (*arg && rtnl_rtrealm_a2n(&realm, arg))
  106. return -1;
  107. *realms |= realm;
  108. return 0;
  109. }