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