summaryrefslogtreecommitdiff
path: root/lib/route/route_utils.c
blob: 04e52b4cc1dd5f0002babcb24c26c81b05d25b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup route
 * @defgroup route_utils Utilities
 * @brief Routing Utility Functions
 *
 *
 * @par 1) Translating Routing Table Names
 * @code
 * // libnl is only aware of the de facto standard routing table names.
 * // Additional name <-> identifier associations have to be read in via
 * // a configuration file, f.e. /etc/iproute2/rt_tables
 * err = rtnl_route_read_table_names("/etc/iproute2/rt_tables");
 *
 * // Translating a table name to its idenfier
 * int table = rtnl_route_str2table("main");
 *
 * // ... and the other way around.
 * char buf[32];
 * printf("Name: %s\n",
 *        rtnl_route_table2str(table, buf, sizeof(buf)));
 * @endcode
 *
 *
 *
 *
 * @{
 */

#include "nl-default.h"

#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/route.h>

#include "nl-priv-dynamic-core/nl-core.h"

/**
 * @name Routing Table Identifier Translations
 * @{
 */

static NL_LIST_HEAD(table_names);

static int add_routing_table_name(long id, const char *name)
{
	return __trans_list_add(id, name, &table_names);
}

static void _nl_init init_routing_table_names(void)
{
	add_routing_table_name(RT_TABLE_UNSPEC, "unspec");
	add_routing_table_name(RT_TABLE_COMPAT, "compat");
	add_routing_table_name(RT_TABLE_DEFAULT, "default");
	add_routing_table_name(RT_TABLE_MAIN, "main");
	add_routing_table_name(RT_TABLE_LOCAL, "local");
}

static void _nl_exit release_routing_table_names(void)
{
	__trans_list_clear(&table_names);
}

int rtnl_route_read_table_names(const char *path)
{
	__trans_list_clear(&table_names);

	return __nl_read_num_str_file(path, &add_routing_table_name);
}

char *rtnl_route_table2str(int table, char *buf, size_t size)
{
	return __list_type2str(table, buf, size, &table_names);
}

int rtnl_route_str2table(const char *name)
{
	return __list_str2type(name, &table_names);
}


/** @} */

/**
 * @name Routing Protocol Translations
 * @{
 */

static NL_LIST_HEAD(proto_names);

static int add_proto_name(long id, const char *name)
{
	return __trans_list_add(id, name, &proto_names);
}

static void _nl_init init_proto_names(void)
{
	add_proto_name(RTPROT_UNSPEC, "unspec");
	add_proto_name(RTPROT_REDIRECT, "redirect");
	add_proto_name(RTPROT_KERNEL, "kernel");
	add_proto_name(RTPROT_BOOT, "boot");
	add_proto_name(RTPROT_STATIC, "static");
}

static void _nl_exit release_proto_names(void)
{
	__trans_list_clear(&proto_names);
}

int rtnl_route_read_protocol_names(const char *path)
{
	__trans_list_clear(&proto_names);

	return __nl_read_num_str_file(path, &add_proto_name);
}

char *rtnl_route_proto2str(int proto, char *buf, size_t size)
{
	return __list_type2str(proto, buf, size, &proto_names);
}

int rtnl_route_str2proto(const char *name)
{
	return __list_str2type(name, &proto_names);
}

/** @} */

/**
 * @name Routing Metrices Translations
 * @{
 */

static const struct trans_tbl route_metrices[] = {
	__ADD(RTAX_UNSPEC, unspec),
	__ADD(RTAX_LOCK, lock),
	__ADD(RTAX_MTU, mtu),
	__ADD(RTAX_WINDOW, window),
	__ADD(RTAX_RTT, rtt),
	__ADD(RTAX_RTTVAR, rttvar),
	__ADD(RTAX_SSTHRESH, ssthresh),
	__ADD(RTAX_CWND, cwnd),
	__ADD(RTAX_ADVMSS, advmss),
	__ADD(RTAX_REORDERING, reordering),
	__ADD(RTAX_HOPLIMIT, hoplimit),
	__ADD(RTAX_INITCWND, initcwnd),
	__ADD(RTAX_FEATURES, features),
};

char *rtnl_route_metric2str(int metric, char *buf, size_t size)
{
	return __type2str(metric, buf, size, route_metrices,
			  ARRAY_SIZE(route_metrices));
}

int rtnl_route_str2metric(const char *name)
{
	return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
}

/** @} */

/** @} */