rt_names.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * rt_names.c rtnetlink names DB.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <stdint.h>
  15. static void rtnl_tab_initialize(char *file, char **tab, int size)
  16. {
  17. char buf[512];
  18. FILE *fp;
  19. fp = fopen(file, "r");
  20. if (!fp)
  21. return;
  22. while (fgets(buf, sizeof(buf), fp)) {
  23. char *p = buf;
  24. int id;
  25. char namebuf[512];
  26. while (*p == ' ' || *p == '\t')
  27. p++;
  28. if (*p == '#' || *p == '\n' || *p == 0)
  29. continue;
  30. if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 &&
  31. sscanf(p, "0x%x %s #", &id, namebuf) != 2 &&
  32. sscanf(p, "%d %s\n", &id, namebuf) != 2 &&
  33. sscanf(p, "%d %s #", &id, namebuf) != 2) {
  34. fprintf(stderr, "Database %s is corrupted at %s\n",
  35. file, p);
  36. return;
  37. }
  38. if (id<0 || id>size)
  39. continue;
  40. tab[id] = strdup(namebuf);
  41. }
  42. fclose(fp);
  43. }
  44. static char * rtnl_rtprot_tab[256] = {
  45. "none",
  46. "redirect",
  47. "kernel",
  48. "boot",
  49. "static",
  50. NULL,
  51. NULL,
  52. NULL,
  53. "gated",
  54. "ra",
  55. "mrt",
  56. "zebra",
  57. "bird",
  58. };
  59. static int rtnl_rtprot_init;
  60. static void rtnl_rtprot_initialize(void)
  61. {
  62. rtnl_rtprot_init = 1;
  63. rtnl_tab_initialize("/etc/iproute2/rt_protos",
  64. rtnl_rtprot_tab, 256);
  65. }
  66. const char * rtnl_rtprot_n2a(int id, char *buf, int len)
  67. {
  68. if (id<0 || id>=256) {
  69. snprintf(buf, len, "%d", id);
  70. return buf;
  71. }
  72. if (!rtnl_rtprot_tab[id]) {
  73. if (!rtnl_rtprot_init)
  74. rtnl_rtprot_initialize();
  75. }
  76. if (rtnl_rtprot_tab[id])
  77. return rtnl_rtprot_tab[id];
  78. snprintf(buf, len, "%d", id);
  79. return buf;
  80. }
  81. int rtnl_rtprot_a2n(uint32_t *id, char *arg)
  82. {
  83. static char *cache = NULL;
  84. static unsigned long res;
  85. char *end;
  86. int i;
  87. if (cache && strcmp(cache, arg) == 0) {
  88. *id = res;
  89. return 0;
  90. }
  91. if (!rtnl_rtprot_init)
  92. rtnl_rtprot_initialize();
  93. for (i=0; i<256; i++) {
  94. if (rtnl_rtprot_tab[i] &&
  95. strcmp(rtnl_rtprot_tab[i], arg) == 0) {
  96. cache = rtnl_rtprot_tab[i];
  97. res = i;
  98. *id = res;
  99. return 0;
  100. }
  101. }
  102. res = strtoul(arg, &end, 0);
  103. if (!end || end == arg || *end || res > 255)
  104. return -1;
  105. *id = res;
  106. return 0;
  107. }
  108. static char * rtnl_rtscope_tab[256] = {
  109. "global",
  110. };
  111. static int rtnl_rtscope_init;
  112. static void rtnl_rtscope_initialize(void)
  113. {
  114. rtnl_rtscope_init = 1;
  115. rtnl_rtscope_tab[255] = "nowhere";
  116. rtnl_rtscope_tab[254] = "host";
  117. rtnl_rtscope_tab[253] = "link";
  118. rtnl_rtscope_tab[200] = "site";
  119. rtnl_tab_initialize("/etc/iproute2/rt_scopes",
  120. rtnl_rtscope_tab, 256);
  121. }
  122. const char * rtnl_rtscope_n2a(int id, char *buf, int len)
  123. {
  124. if (id<0 || id>=256) {
  125. snprintf(buf, len, "%d", id);
  126. return buf;
  127. }
  128. if (!rtnl_rtscope_tab[id]) {
  129. if (!rtnl_rtscope_init)
  130. rtnl_rtscope_initialize();
  131. }
  132. if (rtnl_rtscope_tab[id])
  133. return rtnl_rtscope_tab[id];
  134. snprintf(buf, len, "%d", id);
  135. return buf;
  136. }
  137. int rtnl_rtscope_a2n(uint32_t *id, char *arg)
  138. {
  139. static char *cache = NULL;
  140. static unsigned long res;
  141. char *end;
  142. int i;
  143. if (cache && strcmp(cache, arg) == 0) {
  144. *id = res;
  145. return 0;
  146. }
  147. if (!rtnl_rtscope_init)
  148. rtnl_rtscope_initialize();
  149. for (i=0; i<256; i++) {
  150. if (rtnl_rtscope_tab[i] &&
  151. strcmp(rtnl_rtscope_tab[i], arg) == 0) {
  152. cache = rtnl_rtscope_tab[i];
  153. res = i;
  154. *id = res;
  155. return 0;
  156. }
  157. }
  158. res = strtoul(arg, &end, 0);
  159. if (!end || end == arg || *end || res > 255)
  160. return -1;
  161. *id = res;
  162. return 0;
  163. }
  164. static char * rtnl_rtrealm_tab[256] = {
  165. "unknown",
  166. };
  167. static int rtnl_rtrealm_init;
  168. static void rtnl_rtrealm_initialize(void)
  169. {
  170. rtnl_rtrealm_init = 1;
  171. rtnl_tab_initialize("/etc/iproute2/rt_realms",
  172. rtnl_rtrealm_tab, 256);
  173. }
  174. const char * rtnl_rtrealm_n2a(int id, char *buf, int len)
  175. {
  176. if (id<0 || id>=256) {
  177. snprintf(buf, len, "%d", id);
  178. return buf;
  179. }
  180. if (!rtnl_rtrealm_tab[id]) {
  181. if (!rtnl_rtrealm_init)
  182. rtnl_rtrealm_initialize();
  183. }
  184. if (rtnl_rtrealm_tab[id])
  185. return rtnl_rtrealm_tab[id];
  186. snprintf(buf, len, "%d", id);
  187. return buf;
  188. }
  189. int rtnl_rtrealm_a2n(uint32_t *id, char *arg)
  190. {
  191. static char *cache = NULL;
  192. static unsigned long res;
  193. char *end;
  194. int i;
  195. if (cache && strcmp(cache, arg) == 0) {
  196. *id = res;
  197. return 0;
  198. }
  199. if (!rtnl_rtrealm_init)
  200. rtnl_rtrealm_initialize();
  201. for (i=0; i<256; i++) {
  202. if (rtnl_rtrealm_tab[i] &&
  203. strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
  204. cache = rtnl_rtrealm_tab[i];
  205. res = i;
  206. *id = res;
  207. return 0;
  208. }
  209. }
  210. res = strtoul(arg, &end, 0);
  211. if (!end || end == arg || *end || res > 255)
  212. return -1;
  213. *id = res;
  214. return 0;
  215. }
  216. static char * rtnl_rttable_tab[256] = {
  217. "unspec",
  218. };
  219. static int rtnl_rttable_init;
  220. static void rtnl_rttable_initialize(void)
  221. {
  222. rtnl_rttable_init = 1;
  223. rtnl_rttable_tab[255] = "local";
  224. rtnl_rttable_tab[254] = "main";
  225. rtnl_tab_initialize("/etc/iproute2/rt_tables",
  226. rtnl_rttable_tab, 256);
  227. }
  228. const char * rtnl_rttable_n2a(int id, char *buf, int len)
  229. {
  230. if (id<0 || id>=256) {
  231. snprintf(buf, len, "%d", id);
  232. return buf;
  233. }
  234. if (!rtnl_rttable_tab[id]) {
  235. if (!rtnl_rttable_init)
  236. rtnl_rttable_initialize();
  237. }
  238. if (rtnl_rttable_tab[id])
  239. return rtnl_rttable_tab[id];
  240. snprintf(buf, len, "%d", id);
  241. return buf;
  242. }
  243. int rtnl_rttable_a2n(uint32_t *id, char *arg)
  244. {
  245. static char *cache = NULL;
  246. static unsigned long res;
  247. char *end;
  248. int i;
  249. if (cache && strcmp(cache, arg) == 0) {
  250. *id = res;
  251. return 0;
  252. }
  253. if (!rtnl_rttable_init)
  254. rtnl_rttable_initialize();
  255. for (i=0; i<256; i++) {
  256. if (rtnl_rttable_tab[i] &&
  257. strcmp(rtnl_rttable_tab[i], arg) == 0) {
  258. cache = rtnl_rttable_tab[i];
  259. res = i;
  260. *id = res;
  261. return 0;
  262. }
  263. }
  264. i = strtoul(arg, &end, 0);
  265. if (!end || end == arg || *end || i > 255)
  266. return -1;
  267. *id = i;
  268. return 0;
  269. }
  270. static char * rtnl_rtdsfield_tab[256] = {
  271. "0",
  272. };
  273. static int rtnl_rtdsfield_init;
  274. static void rtnl_rtdsfield_initialize(void)
  275. {
  276. rtnl_rtdsfield_init = 1;
  277. rtnl_tab_initialize("/etc/iproute2/rt_dsfield",
  278. rtnl_rtdsfield_tab, 256);
  279. }
  280. const char * rtnl_dsfield_n2a(int id, char *buf, int len)
  281. {
  282. if (id<0 || id>=256) {
  283. snprintf(buf, len, "%d", id);
  284. return buf;
  285. }
  286. if (!rtnl_rtdsfield_tab[id]) {
  287. if (!rtnl_rtdsfield_init)
  288. rtnl_rtdsfield_initialize();
  289. }
  290. if (rtnl_rtdsfield_tab[id])
  291. return rtnl_rtdsfield_tab[id];
  292. snprintf(buf, len, "0x%02x", id);
  293. return buf;
  294. }
  295. int rtnl_dsfield_a2n(uint32_t *id, char *arg)
  296. {
  297. static char *cache = NULL;
  298. static unsigned long res;
  299. char *end;
  300. int i;
  301. if (cache && strcmp(cache, arg) == 0) {
  302. *id = res;
  303. return 0;
  304. }
  305. if (!rtnl_rtdsfield_init)
  306. rtnl_rtdsfield_initialize();
  307. for (i=0; i<256; i++) {
  308. if (rtnl_rtdsfield_tab[i] &&
  309. strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
  310. cache = rtnl_rtdsfield_tab[i];
  311. res = i;
  312. *id = res;
  313. return 0;
  314. }
  315. }
  316. res = strtoul(arg, &end, 16);
  317. if (!end || end == arg || *end || res > 255)
  318. return -1;
  319. *id = res;
  320. return 0;
  321. }