route_utils.c

00001 /*
00002  * lib/route/route_utils.c      Routing Utilities
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 /**
00013  * @ingroup route
00014  * @defgroup route_utils Utilities
00015  * @brief Routing Utility Functions
00016  *
00017  *
00018  * @par 1) Translating Routing Table Names
00019  * @code
00020  * // libnl is only aware of the de facto standard routing table names.
00021  * // Additional name <-> identifier associations have to be read in via
00022  * // a configuration file, f.e. /etc/iproute2/rt_tables
00023  * err = rtnl_route_read_table_names("/etc/iproute2/rt_tables");
00024  *
00025  * // Translating a table name to its idenfier
00026  * int table = rtnl_route_str2table("main");
00027  *
00028  * // ... and the other way around.
00029  * char buf[32];
00030  * printf("Name: %s\n",
00031  *        rtnl_route_table2str(table, buf, sizeof(buf)));
00032  * @endcode
00033  *
00034  *
00035  *
00036  *
00037  * @{
00038  */
00039 
00040 #include <netlink-local.h>
00041 #include <netlink/netlink.h>
00042 #include <netlink/utils.h>
00043 #include <netlink/route/rtnl.h>
00044 #include <netlink/route/route.h>
00045         
00046 /**
00047  * @name Routing Table Identifier Translations
00048  * @{
00049  */
00050 
00051 static NL_LIST_HEAD(table_names);
00052 
00053 static int add_routing_table_name(long id, const char *name)
00054 {
00055         return __trans_list_add(id, name, &table_names);
00056 }
00057 
00058 static void __init init_routing_table_names(void)
00059 {
00060         add_routing_table_name(RT_TABLE_UNSPEC, "unspec");
00061         add_routing_table_name(RT_TABLE_COMPAT, "compat");
00062         add_routing_table_name(RT_TABLE_DEFAULT, "default");
00063         add_routing_table_name(RT_TABLE_MAIN, "main");
00064         add_routing_table_name(RT_TABLE_LOCAL, "local");
00065 };
00066 
00067 static void __exit release_routing_table_names(void)
00068 {
00069         __trans_list_clear(&table_names);
00070 }
00071 
00072 int rtnl_route_read_table_names(const char *path)
00073 {
00074         __trans_list_clear(&table_names);
00075 
00076         return __nl_read_num_str_file(path, &add_routing_table_name);
00077 }
00078 
00079 char *rtnl_route_table2str(int table, char *buf, size_t size)
00080 {
00081         return __list_type2str(table, buf, size, &table_names);
00082 }
00083 
00084 int rtnl_route_str2table(const char *name)
00085 {
00086         return __list_str2type(name, &table_names);
00087 }
00088 
00089 
00090 /** @} */
00091 
00092 /**
00093  * @name Routing Protocol Translations
00094  * @{
00095  */
00096 
00097 static NL_LIST_HEAD(proto_names);
00098 
00099 static int add_proto_name(long id, const char *name)
00100 {
00101         return __trans_list_add(id, name, &proto_names);
00102 }
00103 
00104 static void __init init_proto_names(void)
00105 {
00106         add_proto_name(RTPROT_UNSPEC, "unspec");
00107         add_proto_name(RTPROT_REDIRECT, "redirect");
00108         add_proto_name(RTPROT_KERNEL, "kernel");
00109         add_proto_name(RTPROT_BOOT, "boot");
00110         add_proto_name(RTPROT_STATIC, "static");
00111 };
00112 
00113 static void __exit release_proto_names(void)
00114 {
00115         __trans_list_clear(&proto_names);
00116 }
00117 
00118 int rtnl_route_read_protocol_names(const char *path)
00119 {
00120         __trans_list_clear(&proto_names);
00121 
00122         return __nl_read_num_str_file(path, &add_proto_name);
00123 }
00124 
00125 char *rtnl_route_proto2str(int proto, char *buf, size_t size)
00126 {
00127         return __list_type2str(proto, buf, size, &proto_names);
00128 }
00129 
00130 int rtnl_route_str2proto(const char *name)
00131 {
00132         return __list_str2type(name, &proto_names);
00133 }
00134 
00135 /** @} */
00136 
00137 /**
00138  * @name Routing Metrices Translations
00139  * @{
00140  */
00141 
00142 static const struct trans_tbl route_metrices[] = {
00143         __ADD(RTAX_UNSPEC, unspec)
00144         __ADD(RTAX_LOCK, lock)
00145         __ADD(RTAX_MTU, mtu)
00146         __ADD(RTAX_WINDOW, window)
00147         __ADD(RTAX_RTT, rtt)
00148         __ADD(RTAX_RTTVAR, rttvar)
00149         __ADD(RTAX_SSTHRESH, ssthresh)
00150         __ADD(RTAX_CWND, cwnd)
00151         __ADD(RTAX_ADVMSS, advmss)
00152         __ADD(RTAX_REORDERING, reordering)
00153         __ADD(RTAX_HOPLIMIT, hoplimit)
00154         __ADD(RTAX_INITCWND, initcwnd)
00155         __ADD(RTAX_FEATURES, features)
00156 };
00157 
00158 char *rtnl_route_metric2str(int metric, char *buf, size_t size)
00159 {
00160         return __type2str(metric, buf, size, route_metrices,
00161                           ARRAY_SIZE(route_metrices));
00162 }
00163 
00164 int rtnl_route_str2metric(const char *name)
00165 {
00166         return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
00167 }
00168 
00169 /** @} */
00170 
00171 /** @} */