3
0

rt_names.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. static void rtnl_tab_initialize(char *file, const char **tab, int size)
  15. {
  16. char buf[512];
  17. FILE *fp;
  18. fp = fopen(file, "r");
  19. if (!fp)
  20. return;
  21. while (fgets(buf, sizeof(buf), fp)) {
  22. char *p = buf;
  23. int id;
  24. char namebuf[512];
  25. while (*p == ' ' || *p == '\t')
  26. p++;
  27. if (*p == '#' || *p == '\n' || *p == 0)
  28. continue;
  29. if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
  30. sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
  31. sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
  32. sscanf(p, "%d %s #", &id, namebuf) != 2) {
  33. bb_error_msg("database %s is corrupted at %s",
  34. file, p);
  35. return;
  36. }
  37. if (id < 0 || id > size)
  38. continue;
  39. tab[id] = xstrdup(namebuf);
  40. }
  41. fclose(fp);
  42. }
  43. static const char **rtnl_rtprot_tab; /* [256] */
  44. static void rtnl_rtprot_initialize(void)
  45. {
  46. static const char *const init_tab[] = {
  47. "none",
  48. "redirect",
  49. "kernel",
  50. "boot",
  51. "static",
  52. NULL,
  53. NULL,
  54. NULL,
  55. "gated",
  56. "ra",
  57. "mrt",
  58. "zebra",
  59. "bird",
  60. };
  61. if (rtnl_rtprot_tab) return;
  62. rtnl_rtprot_tab = xzalloc(256 * sizeof(rtnl_rtprot_tab[0]));
  63. memcpy(rtnl_rtprot_tab, init_tab, sizeof(init_tab));
  64. rtnl_tab_initialize("/etc/iproute2/rt_protos",
  65. rtnl_rtprot_tab, 256);
  66. }
  67. const char* rtnl_rtprot_n2a(int id, char *buf, int len)
  68. {
  69. if (id < 0 || id >= 256) {
  70. snprintf(buf, len, "%d", id);
  71. return buf;
  72. }
  73. rtnl_rtprot_initialize();
  74. if (rtnl_rtprot_tab[id])
  75. return rtnl_rtprot_tab[id];
  76. snprintf(buf, len, "%d", id);
  77. return buf;
  78. }
  79. int rtnl_rtprot_a2n(uint32_t *id, char *arg)
  80. {
  81. static const char *cache = NULL;
  82. static unsigned long res;
  83. int i;
  84. if (cache && strcmp(cache, arg) == 0) {
  85. *id = res;
  86. return 0;
  87. }
  88. rtnl_rtprot_initialize();
  89. for (i = 0; i < 256; i++) {
  90. if (rtnl_rtprot_tab[i] &&
  91. strcmp(rtnl_rtprot_tab[i], arg) == 0) {
  92. cache = rtnl_rtprot_tab[i];
  93. res = i;
  94. *id = res;
  95. return 0;
  96. }
  97. }
  98. res = bb_strtoul(arg, NULL, 0);
  99. if (errno || res > 255)
  100. return -1;
  101. *id = res;
  102. return 0;
  103. }
  104. static const char **rtnl_rtscope_tab; /* [256] */
  105. static void rtnl_rtscope_initialize(void)
  106. {
  107. if (rtnl_rtscope_tab) return;
  108. rtnl_rtscope_tab = xzalloc(256 * sizeof(rtnl_rtscope_tab[0]));
  109. rtnl_rtscope_tab[0] = "global";
  110. rtnl_rtscope_tab[255] = "nowhere";
  111. rtnl_rtscope_tab[254] = "host";
  112. rtnl_rtscope_tab[253] = "link";
  113. rtnl_rtscope_tab[200] = "site";
  114. rtnl_tab_initialize("/etc/iproute2/rt_scopes",
  115. rtnl_rtscope_tab, 256);
  116. }
  117. const char* rtnl_rtscope_n2a(int id, char *buf, int len)
  118. {
  119. if (id < 0 || id >= 256) {
  120. snprintf(buf, len, "%d", id);
  121. return buf;
  122. }
  123. rtnl_rtscope_initialize();
  124. if (rtnl_rtscope_tab[id])
  125. return rtnl_rtscope_tab[id];
  126. snprintf(buf, len, "%d", id);
  127. return buf;
  128. }
  129. int rtnl_rtscope_a2n(uint32_t *id, char *arg)
  130. {
  131. static const char *cache = NULL;
  132. static unsigned long res;
  133. int i;
  134. if (cache && strcmp(cache, arg) == 0) {
  135. *id = res;
  136. return 0;
  137. }
  138. rtnl_rtscope_initialize();
  139. for (i = 0; i < 256; i++) {
  140. if (rtnl_rtscope_tab[i] &&
  141. strcmp(rtnl_rtscope_tab[i], arg) == 0) {
  142. cache = rtnl_rtscope_tab[i];
  143. res = i;
  144. *id = res;
  145. return 0;
  146. }
  147. }
  148. res = bb_strtoul(arg, NULL, 0);
  149. if (errno || res > 255)
  150. return -1;
  151. *id = res;
  152. return 0;
  153. }
  154. static const char **rtnl_rtrealm_tab; /* [256] */
  155. static void rtnl_rtrealm_initialize(void)
  156. {
  157. if (rtnl_rtrealm_tab) return;
  158. rtnl_rtrealm_tab = xzalloc(256 * sizeof(rtnl_rtrealm_tab[0]));
  159. rtnl_rtrealm_tab[0] = "unknown";
  160. rtnl_tab_initialize("/etc/iproute2/rt_realms",
  161. rtnl_rtrealm_tab, 256);
  162. }
  163. int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
  164. {
  165. static const char *cache = NULL;
  166. static unsigned long res;
  167. int i;
  168. if (cache && strcmp(cache, arg) == 0) {
  169. *id = res;
  170. return 0;
  171. }
  172. rtnl_rtrealm_initialize();
  173. for (i = 0; i < 256; i++) {
  174. if (rtnl_rtrealm_tab[i] &&
  175. strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
  176. cache = rtnl_rtrealm_tab[i];
  177. res = i;
  178. *id = res;
  179. return 0;
  180. }
  181. }
  182. res = bb_strtoul(arg, NULL, 0);
  183. if (errno || res > 255)
  184. return -1;
  185. *id = res;
  186. return 0;
  187. }
  188. #if ENABLE_FEATURE_IP_RULE
  189. const char* rtnl_rtrealm_n2a(int id, char *buf, int len)
  190. {
  191. if (id < 0 || id >= 256) {
  192. snprintf(buf, len, "%d", id);
  193. return buf;
  194. }
  195. rtnl_rtrealm_initialize();
  196. if (rtnl_rtrealm_tab[id])
  197. return rtnl_rtrealm_tab[id];
  198. snprintf(buf, len, "%d", id);
  199. return buf;
  200. }
  201. #endif
  202. static const char **rtnl_rtdsfield_tab; /* [256] */
  203. static void rtnl_rtdsfield_initialize(void)
  204. {
  205. if (rtnl_rtdsfield_tab) return;
  206. rtnl_rtdsfield_tab = xzalloc(256 * sizeof(rtnl_rtdsfield_tab[0]));
  207. rtnl_rtdsfield_tab[0] = "0";
  208. rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
  209. rtnl_rtdsfield_tab, 256);
  210. }
  211. const char * rtnl_dsfield_n2a(int id, char *buf, int len)
  212. {
  213. if (id < 0 || id >= 256) {
  214. snprintf(buf, len, "%d", id);
  215. return buf;
  216. }
  217. rtnl_rtdsfield_initialize();
  218. if (rtnl_rtdsfield_tab[id])
  219. return rtnl_rtdsfield_tab[id];
  220. snprintf(buf, len, "0x%02x", id);
  221. return buf;
  222. }
  223. int rtnl_dsfield_a2n(uint32_t *id, char *arg)
  224. {
  225. static const char *cache = NULL;
  226. static unsigned long res;
  227. int i;
  228. if (cache && strcmp(cache, arg) == 0) {
  229. *id = res;
  230. return 0;
  231. }
  232. rtnl_rtdsfield_initialize();
  233. for (i = 0; i < 256; i++) {
  234. if (rtnl_rtdsfield_tab[i] &&
  235. strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
  236. cache = rtnl_rtdsfield_tab[i];
  237. res = i;
  238. *id = res;
  239. return 0;
  240. }
  241. }
  242. res = bb_strtoul(arg, NULL, 16);
  243. if (errno || res > 255)
  244. return -1;
  245. *id = res;
  246. return 0;
  247. }
  248. #if ENABLE_FEATURE_IP_RULE
  249. static const char **rtnl_rttable_tab; /* [256] */
  250. static void rtnl_rttable_initialize(void)
  251. {
  252. if (rtnl_rtdsfield_tab) return;
  253. rtnl_rttable_tab = xzalloc(256 * sizeof(rtnl_rttable_tab[0]));
  254. rtnl_rttable_tab[0] = "unspec";
  255. rtnl_rttable_tab[255] = "local";
  256. rtnl_rttable_tab[254] = "main";
  257. rtnl_rttable_tab[253] = "default";
  258. rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab, 256);
  259. }
  260. const char *rtnl_rttable_n2a(int id, char *buf, int len)
  261. {
  262. if (id < 0 || id >= 256) {
  263. snprintf(buf, len, "%d", id);
  264. return buf;
  265. }
  266. rtnl_rttable_initialize();
  267. if (rtnl_rttable_tab[id])
  268. return rtnl_rttable_tab[id];
  269. snprintf(buf, len, "%d", id);
  270. return buf;
  271. }
  272. int rtnl_rttable_a2n(uint32_t * id, char *arg)
  273. {
  274. static char *cache = NULL;
  275. static unsigned long res;
  276. int i;
  277. if (cache && strcmp(cache, arg) == 0) {
  278. *id = res;
  279. return 0;
  280. }
  281. rtnl_rttable_initialize();
  282. for (i = 0; i < 256; i++) {
  283. if (rtnl_rttable_tab[i] && strcmp(rtnl_rttable_tab[i], arg) == 0) {
  284. cache = (char*)rtnl_rttable_tab[i];
  285. res = i;
  286. *id = res;
  287. return 0;
  288. }
  289. }
  290. i = bb_strtoul(arg, NULL, 0);
  291. if (errno || i > 255)
  292. return -1;
  293. *id = i;
  294. return 0;
  295. }
  296. #endif