3
0

rtm_map.c 2.5 KB

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