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