libnl 1.1

lib/route/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_DEFAULT, "default");
00062         add_routing_table_name(RT_TABLE_MAIN, "main");
00063         add_routing_table_name(RT_TABLE_LOCAL, "local");
00064 };
00065 
00066 int rtnl_route_read_table_names(const char *path)
00067 {
00068         __trans_list_clear(&table_names);
00069 
00070         return __nl_read_num_str_file(path, &add_routing_table_name);
00071 }
00072 
00073 char *rtnl_route_table2str(int table, char *buf, size_t size)
00074 {
00075         return __list_type2str(table, buf, size, &table_names);
00076 }
00077 
00078 int rtnl_route_str2table(const char *name)
00079 {
00080         return __list_str2type(name, &table_names);
00081 }
00082 
00083 
00084 /** @} */
00085 
00086 /**
00087  * @name Routing Protocol Translations
00088  * @{
00089  */
00090 
00091 static NL_LIST_HEAD(proto_names);
00092 
00093 static int add_proto_name(long id, const char *name)
00094 {
00095         return __trans_list_add(id, name, &proto_names);
00096 }
00097 
00098 static void __init init_proto_names(void)
00099 {
00100         add_proto_name(RTPROT_UNSPEC, "unspec");
00101         add_proto_name(RTPROT_REDIRECT, "redirect");
00102         add_proto_name(RTPROT_KERNEL, "kernel");
00103         add_proto_name(RTPROT_BOOT, "boot");
00104         add_proto_name(RTPROT_STATIC, "static");
00105 };
00106 
00107 int rtnl_route_read_protocol_names(const char *path)
00108 {
00109         __trans_list_clear(&proto_names);
00110 
00111         return __nl_read_num_str_file(path, &add_proto_name);
00112 }
00113 
00114 char *rtnl_route_proto2str(int proto, char *buf, size_t size)
00115 {
00116         return __list_type2str(proto, buf, size, &proto_names);
00117 }
00118 
00119 int rtnl_route_str2proto(const char *name)
00120 {
00121         return __list_str2type(name, &proto_names);
00122 }
00123 
00124 /** @} */
00125 
00126 /**
00127  * @name Routing Metrices Translations
00128  * @{
00129  */
00130 
00131 static struct trans_tbl route_metrices[] = {
00132         __ADD(RTAX_UNSPEC, unspec)
00133         __ADD(RTAX_LOCK, lock)
00134         __ADD(RTAX_MTU, mtu)
00135         __ADD(RTAX_WINDOW, window)
00136         __ADD(RTAX_RTT, rtt)
00137         __ADD(RTAX_RTTVAR, rttvar)
00138         __ADD(RTAX_SSTHRESH, ssthresh)
00139         __ADD(RTAX_CWND, cwnd)
00140         __ADD(RTAX_ADVMSS, advmss)
00141         __ADD(RTAX_REORDERING, reordering)
00142         __ADD(RTAX_HOPLIMIT, hoplimit)
00143         __ADD(RTAX_INITCWND, initcwnd)
00144         __ADD(RTAX_FEATURES, features)
00145 };
00146 
00147 char *rtnl_route_metric2str(int metric, char *buf, size_t size)
00148 {
00149         return __type2str(metric, buf, size, route_metrices,
00150                           ARRAY_SIZE(route_metrices));
00151 }
00152 
00153 int rtnl_route_str2metric(const char *name)
00154 {
00155         return __str2type(name, route_metrices, ARRAY_SIZE(route_metrices));
00156 }
00157 
00158 /** @} */
00159 
00160 /**
00161  * @name Nexthop Flags Translations
00162  * @{
00163  */
00164 
00165 static struct trans_tbl nh_flags[] = {
00166         __ADD(RTNH_F_DEAD, dead)
00167         __ADD(RTNH_F_PERVASIVE, pervasive)
00168         __ADD(RTNH_F_ONLINK, onlink)
00169 };
00170 
00171 char * rtnl_route_nh_flags2str(int flags, char *buf, size_t len)
00172 {
00173         return __flags2str(flags, buf, len, nh_flags, ARRAY_SIZE(nh_flags));
00174 }
00175 
00176 int rtnl_route_nh_str2flags(const char *name)
00177 {
00178         return __str2flags(name, nh_flags, ARRAY_SIZE(nh_flags));
00179 }
00180 
00181 /** @} */
00182 
00183 /** @} */