route_utils.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-local.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_DEFAULT, "default");
  56. add_routing_table_name(RT_TABLE_MAIN, "main");
  57. add_routing_table_name(RT_TABLE_LOCAL, "local");
  58. };
  59. static void __exit release_routing_table_names(void)
  60. {
  61. __trans_list_clear(&table_names);
  62. }
  63. int rtnl_route_read_table_names(const char *path)
  64. {
  65. __trans_list_clear(&table_names);
  66. return __nl_read_num_str_file(path, &add_routing_table_name);
  67. }
  68. char *rtnl_route_table2str(int table, char *buf, size_t size)
  69. {
  70. return __list_type2str(table, buf, size, &table_names);
  71. }
  72. int rtnl_route_str2table(const char *name)
  73. {
  74. return __list_str2type(name, &table_names);
  75. }
  76. /** @} */
  77. /**
  78. * @name Routing Protocol Translations
  79. * @{
  80. */
  81. static NL_LIST_HEAD(proto_names);
  82. static int add_proto_name(long id, const char *name)
  83. {
  84. return __trans_list_add(id, name, &proto_names);
  85. }
  86. static void __init init_proto_names(void)
  87. {
  88. add_proto_name(RTPROT_UNSPEC, "unspec");
  89. add_proto_name(RTPROT_REDIRECT, "redirect");
  90. add_proto_name(RTPROT_KERNEL, "kernel");
  91. add_proto_name(RTPROT_BOOT, "boot");
  92. add_proto_name(RTPROT_STATIC, "static");
  93. };
  94. static void __exit release_proto_names(void)
  95. {
  96. __trans_list_clear(&proto_names);
  97. }
  98. int rtnl_route_read_protocol_names(const char *path)
  99. {
  100. __trans_list_clear(&proto_names);
  101. return __nl_read_num_str_file(path, &add_proto_name);
  102. }
  103. char *rtnl_route_proto2str(int proto, char *buf, size_t size)
  104. {
  105. return __list_type2str(proto, buf, size, &proto_names);
  106. }
  107. int rtnl_route_str2proto(const char *name)
  108. {
  109. return __list_str2type(name, &proto_names);
  110. }
  111. /** @} */
  112. /**
  113. * @name Routing Metrices Translations
  114. * @{
  115. */
  116. static struct trans_tbl route_metrices[] = {
  117. __ADD(RTAX_UNSPEC, unspec)
  118. __ADD(RTAX_LOCK, lock)
  119. __ADD(RTAX_MTU, mtu)
  120. __ADD(RTAX_WINDOW, window)
  121. __ADD(RTAX_RTT, rtt)
  122. __ADD(RTAX_RTTVAR, rttvar)
  123. __ADD(RTAX_SSTHRESH, ssthresh)
  124. __ADD(RTAX_CWND, cwnd)
  125. __ADD(RTAX_ADVMSS, advmss)
  126. __ADD(RTAX_REORDERING, reordering)
  127. __ADD(RTAX_HOPLIMIT, hoplimit)
  128. __ADD(RTAX_INITCWND, initcwnd)
  129. __ADD(RTAX_FEATURES, features)
  130. };
  131. char *rtnl_route_metric2str(int metric, char *buf, size_t size)
  132. {
  133. return __type2str(metric, buf, size, route_metrices,
  134. ARRAY_SIZE(route_metrices));
  135. }
  136. int rtnl_route_str2metric(const char *name)
  137. {
  138. return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
  139. }
  140. /** @} */
  141. /**
  142. * @name Nexthop Flags Translations
  143. * @{
  144. */
  145. static struct trans_tbl nh_flags[] = {
  146. __ADD(RTNH_F_DEAD, dead)
  147. __ADD(RTNH_F_PERVASIVE, pervasive)
  148. __ADD(RTNH_F_ONLINK, onlink)
  149. };
  150. char * rtnl_route_nh_flags2str(int flags, char *buf, size_t len)
  151. {
  152. return __flags2str(flags, buf, len, nh_flags, ARRAY_SIZE(nh_flags));
  153. }
  154. int rtnl_route_nh_str2flags(const char *name)
  155. {
  156. return __str2flags(name, nh_flags, ARRAY_SIZE(nh_flags));
  157. }
  158. /** @} */
  159. /** @} */