rtm_map.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <stdlib.h>
  14. #include <string.h>
  15. #include "rt_names.h"
  16. #include "utils.h"
  17. char *rtnl_rtntype_n2a(int id, char *buf, int len)
  18. {
  19. switch (id) {
  20. case RTN_UNSPEC:
  21. return "none";
  22. case RTN_UNICAST:
  23. return "unicast";
  24. case RTN_LOCAL:
  25. return "local";
  26. case RTN_BROADCAST:
  27. return "broadcast";
  28. case RTN_ANYCAST:
  29. return "anycast";
  30. case RTN_MULTICAST:
  31. return "multicast";
  32. case RTN_BLACKHOLE:
  33. return "blackhole";
  34. case RTN_UNREACHABLE:
  35. return "unreachable";
  36. case RTN_PROHIBIT:
  37. return "prohibit";
  38. case RTN_THROW:
  39. return "throw";
  40. case RTN_NAT:
  41. return "nat";
  42. case RTN_XRESOLVE:
  43. return "xresolve";
  44. default:
  45. snprintf(buf, len, "%d", id);
  46. return buf;
  47. }
  48. }
  49. int rtnl_rtntype_a2n(int *id, char *arg)
  50. {
  51. char *end;
  52. unsigned long res;
  53. if (strcmp(arg, "local") == 0)
  54. res = RTN_LOCAL;
  55. else if (strcmp(arg, "nat") == 0)
  56. res = RTN_NAT;
  57. else if (matches(arg, "broadcast") == 0 ||
  58. strcmp(arg, "brd") == 0)
  59. res = RTN_BROADCAST;
  60. else if (matches(arg, "anycast") == 0)
  61. res = RTN_ANYCAST;
  62. else if (matches(arg, "multicast") == 0)
  63. res = RTN_MULTICAST;
  64. else if (matches(arg, "prohibit") == 0)
  65. res = RTN_PROHIBIT;
  66. else if (matches(arg, "unreachable") == 0)
  67. res = RTN_UNREACHABLE;
  68. else if (matches(arg, "blackhole") == 0)
  69. res = RTN_BLACKHOLE;
  70. else if (matches(arg, "xresolve") == 0)
  71. res = RTN_XRESOLVE;
  72. else if (matches(arg, "unicast") == 0)
  73. res = RTN_UNICAST;
  74. else if (strcmp(arg, "throw") == 0)
  75. res = RTN_THROW;
  76. else {
  77. res = strtoul(arg, &end, 0);
  78. if (!end || end == arg || *end || res > 255)
  79. return -1;
  80. }
  81. *id = res;
  82. return 0;
  83. }
  84. int get_rt_realms(uint32_t *realms, char *arg)
  85. {
  86. uint32_t realm = 0;
  87. char *p = strchr(arg, '/');
  88. *realms = 0;
  89. if (p) {
  90. *p = 0;
  91. if (rtnl_rtrealm_a2n(realms, arg)) {
  92. *p = '/';
  93. return -1;
  94. }
  95. *realms <<= 16;
  96. *p = '/';
  97. arg = p+1;
  98. }
  99. if (*arg && rtnl_rtrealm_a2n(&realm, arg))
  100. return -1;
  101. *realms |= realm;
  102. return 0;
  103. }