rt_names.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. typedef struct rtnl_tab_t {
  13. const char *cached_str;
  14. unsigned cached_result;
  15. const char *tab[256];
  16. } rtnl_tab_t;
  17. static void rtnl_tab_initialize(const char *file, const char **tab)
  18. {
  19. char *token[2];
  20. parser_t *parser = config_open2(file, fopen_for_read);
  21. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
  22. unsigned id = bb_strtou(token[0], NULL, 0);
  23. if (id > 256) {
  24. bb_error_msg("database %s is corrupted at line %d",
  25. file, parser->lineno);
  26. break;
  27. }
  28. tab[id] = xstrdup(token[1]);
  29. }
  30. config_close(parser);
  31. }
  32. static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
  33. {
  34. unsigned i;
  35. if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
  36. *id = tab->cached_result;
  37. return 0;
  38. }
  39. for (i = 0; i < 256; i++) {
  40. if (tab->tab[i]
  41. && strcmp(tab->tab[i], arg) == 0
  42. ) {
  43. tab->cached_str = tab->tab[i];
  44. tab->cached_result = i;
  45. *id = i;
  46. return 0;
  47. }
  48. }
  49. i = bb_strtou(arg, NULL, base);
  50. if (i > 255)
  51. return -1;
  52. *id = i;
  53. return 0;
  54. }
  55. static rtnl_tab_t *rtnl_rtprot_tab;
  56. static void rtnl_rtprot_initialize(void)
  57. {
  58. static const char *const init_tab[] = {
  59. "none",
  60. "redirect",
  61. "kernel",
  62. "boot",
  63. "static",
  64. NULL,
  65. NULL,
  66. NULL,
  67. "gated",
  68. "ra",
  69. "mrt",
  70. "zebra",
  71. "bird",
  72. };
  73. if (rtnl_rtprot_tab)
  74. return;
  75. rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
  76. memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
  77. rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
  78. }
  79. const char* FAST_FUNC rtnl_rtprot_n2a(int id, char *buf)
  80. {
  81. if (id < 0 || id >= 256) {
  82. sprintf(buf, "%d", id);
  83. return buf;
  84. }
  85. rtnl_rtprot_initialize();
  86. if (rtnl_rtprot_tab->tab[id])
  87. return rtnl_rtprot_tab->tab[id];
  88. /* buf is SPRINT_BSIZE big */
  89. sprintf(buf, "%d", id);
  90. return buf;
  91. }
  92. int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
  93. {
  94. rtnl_rtprot_initialize();
  95. return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
  96. }
  97. static rtnl_tab_t *rtnl_rtscope_tab;
  98. static void rtnl_rtscope_initialize(void)
  99. {
  100. if (rtnl_rtscope_tab)
  101. return;
  102. rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
  103. rtnl_rtscope_tab->tab[0] = "global";
  104. rtnl_rtscope_tab->tab[255] = "nowhere";
  105. rtnl_rtscope_tab->tab[254] = "host";
  106. rtnl_rtscope_tab->tab[253] = "link";
  107. rtnl_rtscope_tab->tab[200] = "site";
  108. rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
  109. }
  110. const char* FAST_FUNC rtnl_rtscope_n2a(int id, char *buf)
  111. {
  112. if (id < 0 || id >= 256) {
  113. sprintf(buf, "%d", id);
  114. return buf;
  115. }
  116. rtnl_rtscope_initialize();
  117. if (rtnl_rtscope_tab->tab[id])
  118. return rtnl_rtscope_tab->tab[id];
  119. /* buf is SPRINT_BSIZE big */
  120. sprintf(buf, "%d", id);
  121. return buf;
  122. }
  123. int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
  124. {
  125. rtnl_rtscope_initialize();
  126. return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
  127. }
  128. static rtnl_tab_t *rtnl_rtrealm_tab;
  129. static void rtnl_rtrealm_initialize(void)
  130. {
  131. if (rtnl_rtrealm_tab) return;
  132. rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
  133. rtnl_rtrealm_tab->tab[0] = "unknown";
  134. rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
  135. }
  136. int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
  137. {
  138. rtnl_rtrealm_initialize();
  139. return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
  140. }
  141. #if ENABLE_FEATURE_IP_RULE
  142. const char* FAST_FUNC rtnl_rtrealm_n2a(int id, char *buf)
  143. {
  144. if (id < 0 || id >= 256) {
  145. sprintf(buf, "%d", id);
  146. return buf;
  147. }
  148. rtnl_rtrealm_initialize();
  149. if (rtnl_rtrealm_tab->tab[id])
  150. return rtnl_rtrealm_tab->tab[id];
  151. /* buf is SPRINT_BSIZE big */
  152. sprintf(buf, "%d", id);
  153. return buf;
  154. }
  155. #endif
  156. static rtnl_tab_t *rtnl_rtdsfield_tab;
  157. static void rtnl_rtdsfield_initialize(void)
  158. {
  159. if (rtnl_rtdsfield_tab) return;
  160. rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
  161. rtnl_rtdsfield_tab->tab[0] = "0";
  162. rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
  163. }
  164. const char* FAST_FUNC rtnl_dsfield_n2a(int id, char *buf)
  165. {
  166. if (id < 0 || id >= 256) {
  167. sprintf(buf, "%d", id);
  168. return buf;
  169. }
  170. rtnl_rtdsfield_initialize();
  171. if (rtnl_rtdsfield_tab->tab[id])
  172. return rtnl_rtdsfield_tab->tab[id];
  173. /* buf is SPRINT_BSIZE big */
  174. sprintf(buf, "0x%02x", id);
  175. return buf;
  176. }
  177. int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
  178. {
  179. rtnl_rtdsfield_initialize();
  180. return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
  181. }
  182. #if ENABLE_FEATURE_IP_RULE
  183. static rtnl_tab_t *rtnl_rttable_tab;
  184. static void rtnl_rttable_initialize(void)
  185. {
  186. if (rtnl_rtdsfield_tab) return;
  187. rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
  188. rtnl_rttable_tab->tab[0] = "unspec";
  189. rtnl_rttable_tab->tab[255] = "local";
  190. rtnl_rttable_tab->tab[254] = "main";
  191. rtnl_rttable_tab->tab[253] = "default";
  192. rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
  193. }
  194. const char* FAST_FUNC rtnl_rttable_n2a(int id, char *buf)
  195. {
  196. if (id < 0 || id >= 256) {
  197. sprintf(buf, "%d", id);
  198. return buf;
  199. }
  200. rtnl_rttable_initialize();
  201. if (rtnl_rttable_tab->tab[id])
  202. return rtnl_rttable_tab->tab[id];
  203. /* buf is SPRINT_BSIZE big */
  204. sprintf(buf, "%d", id);
  205. return buf;
  206. }
  207. int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
  208. {
  209. rtnl_rttable_initialize();
  210. return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
  211. }
  212. #endif