libnl 1.1

lib/route/rtnl.c

00001 /*
00002  * lib/route/rtnl.c             Routing Netlink
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 nlfam
00014  * @defgroup rtnl Routing Netlink
00015  * @{
00016  */
00017 
00018 #include <netlink-local.h>
00019 #include <netlink/netlink.h>
00020 #include <netlink/utils.h>
00021 #include <netlink/route/rtnl.h>
00022 
00023 /**
00024  * @name Sending
00025  * @{
00026  */
00027 
00028 /**
00029  * Send routing netlink request message
00030  * @arg handle          Netlink handle.
00031  * @arg type            Netlink message type.
00032  * @arg family          Address family.
00033  * @arg flags           Additional netlink message flags.
00034  *
00035  * Fills out a routing netlink request message and sends it out
00036  * using nl_send_simple().
00037  *
00038  * @return 0 on success or a negative error code.
00039  */
00040 int nl_rtgen_request(struct nl_handle *handle, int type, int family, int flags)
00041 {
00042         struct rtgenmsg gmsg = {
00043                 .rtgen_family = family,
00044         };
00045 
00046         return nl_send_simple(handle, type, flags, &gmsg, sizeof(gmsg));
00047 }
00048 
00049 /** @} */
00050 
00051 /**
00052  * @name Routing Type Translations
00053  * @{
00054  */
00055 
00056 static struct trans_tbl rtntypes[] = {
00057         __ADD(RTN_UNSPEC,unspec)
00058         __ADD(RTN_UNICAST,unicast)
00059         __ADD(RTN_LOCAL,local)
00060         __ADD(RTN_BROADCAST,broadcast)
00061         __ADD(RTN_ANYCAST,anycast)
00062         __ADD(RTN_MULTICAST,multicast)
00063         __ADD(RTN_BLACKHOLE,blackhole)
00064         __ADD(RTN_UNREACHABLE,unreachable)
00065         __ADD(RTN_PROHIBIT,prohibit)
00066         __ADD(RTN_THROW,throw)
00067         __ADD(RTN_NAT,nat)
00068         __ADD(RTN_XRESOLVE,xresolve)
00069 };
00070 
00071 char *nl_rtntype2str(int type, char *buf, size_t size)
00072 {
00073         return __type2str(type, buf, size, rtntypes, ARRAY_SIZE(rtntypes));
00074 }
00075 
00076 int nl_str2rtntype(const char *name)
00077 {
00078         return __str2type(name, rtntypes, ARRAY_SIZE(rtntypes));
00079 }
00080 
00081 /** @} */
00082 
00083 /**
00084  * @name Scope Translations
00085  * @{
00086  */
00087 
00088 static struct trans_tbl scopes[] = {
00089         __ADD(255,nowhere)
00090         __ADD(254,host)
00091         __ADD(253,link)
00092         __ADD(200,site)
00093         __ADD(0,universe)
00094 };
00095 
00096 char *rtnl_scope2str(int scope, char *buf, size_t size)
00097 {
00098         return __type2str(scope, buf, size, scopes, ARRAY_SIZE(scopes));
00099 }
00100 
00101 int rtnl_str2scope(const char *name)
00102 {
00103         return __str2type(name, scopes, ARRAY_SIZE(scopes));
00104 }
00105 
00106 /** @} */
00107 
00108 /**
00109  * @name Realms Translations
00110  * @{
00111  */
00112 
00113 char * rtnl_realms2str(uint32_t realms, char *buf, size_t len)
00114 {
00115         int from = RTNL_REALM_FROM(realms);
00116         int to = RTNL_REALM_TO(realms);
00117 
00118         snprintf(buf, len, "%d/%d", from, to);
00119 
00120         return buf;
00121 }
00122 
00123 /** @} */
00124 
00125 /** @} */