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

Generated on 14 Jun 2013 for libnl by  doxygen 1.4.7