route_utils.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * lib/route/route_utils.c Routing Utilities
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup route
  13. * @defgroup route_utils Utilities
  14. * @brief Routing Utility Functions
  15. *
  16. *
  17. * @par 1) Translating Routing Table Names
  18. * @code
  19. * // libnl is only aware of the de facto standard routing table names.
  20. * // Additional name <-> identifier associations have to be read in via
  21. * // a configuration file, f.e. /etc/iproute2/rt_tables
  22. * err = rtnl_route_read_table_names("/etc/iproute2/rt_tables");
  23. *
  24. * // Translating a table name to its idenfier
  25. * int table = rtnl_route_str2table("main");
  26. *
  27. * // ... and the other way around.
  28. * char buf[32];
  29. * printf("Name: %s\n",
  30. * rtnl_route_table2str(table, buf, sizeof(buf)));
  31. * @endcode
  32. *
  33. *
  34. *
  35. *
  36. * @{
  37. */
  38. #include <netlink-private/netlink.h>
  39. #include <netlink/netlink.h>
  40. #include <netlink/utils.h>
  41. #include <netlink/route/rtnl.h>
  42. #include <netlink/route/route.h>
  43. /**
  44. * @name Routing Table Identifier Translations
  45. * @{
  46. */
  47. static NL_LIST_HEAD(table_names);
  48. static int add_routing_table_name(long id, const char *name)
  49. {
  50. return __trans_list_add(id, name, &table_names);
  51. }
  52. static void __init init_routing_table_names(void)
  53. {
  54. add_routing_table_name(RT_TABLE_UNSPEC, "unspec");
  55. add_routing_table_name(RT_TABLE_COMPAT, "compat");
  56. add_routing_table_name(RT_TABLE_DEFAULT, "default");
  57. add_routing_table_name(RT_TABLE_MAIN, "main");
  58. add_routing_table_name(RT_TABLE_LOCAL, "local");
  59. };
  60. static void __exit release_routing_table_names(void)
  61. {
  62. __trans_list_clear(&table_names);
  63. }
  64. int rtnl_route_read_table_names(const char *path)
  65. {
  66. __trans_list_clear(&table_names);
  67. return __nl_read_num_str_file(path, &add_routing_table_name);
  68. }
  69. char *rtnl_route_table2str(int table, char *buf, size_t size)
  70. {
  71. return __list_type2str(table, buf, size, &table_names);
  72. }
  73. int rtnl_route_str2table(const char *name)
  74. {
  75. return __list_str2type(name, &table_names);
  76. }
  77. /** @} */
  78. /**
  79. * @name Routing Protocol Translations
  80. * @{
  81. */
  82. static NL_LIST_HEAD(proto_names);
  83. static int add_proto_name(long id, const char *name)
  84. {
  85. return __trans_list_add(id, name, &proto_names);
  86. }
  87. static void __init init_proto_names(void)
  88. {
  89. add_proto_name(RTPROT_UNSPEC, "unspec");
  90. add_proto_name(RTPROT_REDIRECT, "redirect");
  91. add_proto_name(RTPROT_KERNEL, "kernel");
  92. add_proto_name(RTPROT_BOOT, "boot");
  93. add_proto_name(RTPROT_STATIC, "static");
  94. };
  95. static void __exit release_proto_names(void)
  96. {
  97. __trans_list_clear(&proto_names);
  98. }
  99. int rtnl_route_read_protocol_names(const char *path)
  100. {
  101. __trans_list_clear(&proto_names);
  102. return __nl_read_num_str_file(path, &add_proto_name);
  103. }
  104. char *rtnl_route_proto2str(int proto, char *buf, size_t size)
  105. {
  106. return __list_type2str(proto, buf, size, &proto_names);
  107. }
  108. int rtnl_route_str2proto(const char *name)
  109. {
  110. return __list_str2type(name, &proto_names);
  111. }
  112. /** @} */
  113. /**
  114. * @name Routing Metrices Translations
  115. * @{
  116. */
  117. static const struct trans_tbl route_metrices[] = {
  118. __ADD(RTAX_UNSPEC, unspec)
  119. __ADD(RTAX_LOCK, lock)
  120. __ADD(RTAX_MTU, mtu)
  121. __ADD(RTAX_WINDOW, window)
  122. __ADD(RTAX_RTT, rtt)
  123. __ADD(RTAX_RTTVAR, rttvar)
  124. __ADD(RTAX_SSTHRESH, ssthresh)
  125. __ADD(RTAX_CWND, cwnd)
  126. __ADD(RTAX_ADVMSS, advmss)
  127. __ADD(RTAX_REORDERING, reordering)
  128. __ADD(RTAX_HOPLIMIT, hoplimit)
  129. __ADD(RTAX_INITCWND, initcwnd)
  130. __ADD(RTAX_FEATURES, features)
  131. };
  132. char *rtnl_route_metric2str(int metric, char *buf, size_t size)
  133. {
  134. return __type2str(metric, buf, size, route_metrices,
  135. ARRAY_SIZE(route_metrices));
  136. }
  137. int rtnl_route_str2metric(const char *name)
  138. {
  139. return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
  140. }
  141. /** @} */
  142. /** @} */