rt_names.c 5.5 KB

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