/build/buildd/libnl-1.0~pre6/lib/route/nexthop.c

00001 /*
00002  * lib/route/nexthop.c  Routing Nexthop
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 nexthop Nexthop
00015  * @brief
00016  * @{
00017  */
00018 
00019 #include <netlink-local.h>
00020 #include <netlink/netlink.h>
00021 #include <netlink/utils.h>
00022 #include <netlink/route/rtnl.h>
00023 #include <netlink/route/route.h>
00024 
00025 /** @cond SKIP */
00026 #define NEXTHOP_HAS_FLAGS   0x000001
00027 #define NEXTHOP_HAS_WEIGHT  0x000002
00028 #define NEXTHOP_HAS_IFINDEX 0x000004
00029 #define NEXTHOP_HAS_GATEWAY 0x000008
00030 /** @endcond */
00031 
00032 /**
00033  * @name Nexthop Allocation/Freeage
00034  * @{
00035  */
00036 
00037 /**
00038  * Allocate a routing nexthop.
00039  * @return Newly allocated routing nexthop object.
00040  */
00041 struct rtnl_nexthop *rtnl_route_nh_alloc(void)
00042 {
00043         struct rtnl_nexthop *nh;
00044 
00045         nh = calloc(1, sizeof(*nh));
00046         if (!nh) {
00047                 nl_errno(ENOMEM);
00048                 return NULL;
00049         }
00050 
00051         nl_init_list_head(&nh->rtnh_list);
00052 
00053         return nh;
00054 }
00055 
00056 /**
00057  * Free a routing nexthop.
00058  * @arg nh              Routing nexthop to be freed.
00059  */
00060 void rtnl_route_nh_free(struct rtnl_nexthop *nh)
00061 {
00062         nl_addr_put(nh->rtnh_gateway);
00063         free(nh);
00064 }
00065 
00066 /** @} */
00067 /**
00068  * @name Attribute: Weight
00069  */
00070 
00071 /**
00072  * Set weight of routing nexthop.
00073  * @arg nh              Routing nexthop.
00074  * @arg weight          New weight value.
00075  */
00076 void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
00077 {
00078         nh->rtnh_weight = weight;
00079         nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
00080 }
00081 
00082 /**
00083  * Get weight of routing nexthop.
00084  * @arg nh              Routing nexthop.
00085  * @return Weight value or 0 if not available.
00086  */
00087 int rtnl_route_nh_get_weight(struct rtnl_nexthop *nh)
00088 {
00089         if (nh->rtnh_mask & NEXTHOP_HAS_WEIGHT)
00090                 return nh->rtnh_weight;
00091         else
00092                 return 0;
00093 }
00094 
00095 /** @} */
00096 /**
00097  * @name Attribute: Interface Index
00098  * @{
00099  */
00100 
00101 /**
00102  * Set interface index for outgoing interface of routing nexthop.
00103  * @arg nh              Routing nexthop.
00104  * @arg ifindex         New interface index.
00105  */
00106 void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
00107 {
00108         nh->rtnh_ifindex = ifindex;
00109         nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
00110 }
00111 
00112 /**
00113  * Get interface index of outgoing index of routing nexthop.
00114  * @arg nh              Routing nexthop.
00115  * @return Interface index or -1 if not available.
00116  */
00117 int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
00118 {
00119         if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
00120                 return nh->rtnh_ifindex;
00121         else
00122                 return -1;
00123 }       
00124 
00125 /** @} */
00126 /**
00127  * @name Attribute: Gateway Address
00128  * @{
00129  */
00130 
00131 /**
00132  * Set gateway address of routing nexthop.
00133  * @arg nh              Routing nexthop.
00134  * @arg addr            New gateway address.
00135  *
00136  * An eventual existing gateway address will be freed and a
00137  * reference is acquried of the new address.
00138  */
00139 void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
00140 {
00141         struct nl_addr *old = nh->rtnh_gateway;
00142 
00143         nh->rtnh_gateway = nl_addr_get(addr);
00144         if (old)
00145                 nl_addr_put(old);
00146 
00147         nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
00148 }
00149 
00150 /**
00151  * Get gateway address of routing nexthop.
00152  * @arg nh              Routing nexthop.
00153  * @return Gateway address or NULL if not available.
00154  */
00155 struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
00156 {
00157         if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
00158                 return nh->rtnh_gateway;
00159         else
00160                 return NULL;
00161 }
00162 
00163 /** @} */
00164 /**
00165  * @name Attribute: Flags
00166  * @{
00167  */
00168 
00169 /**
00170  * Set flags of routing nexthop.
00171  * @arg nh              Routing nexthop.
00172  * @arg flags           Flags to be set.
00173  */
00174 void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
00175 {
00176         nh->rtnh_flag_mask |= flags;
00177         nh->rtnh_flags |= flags;
00178         nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
00179 }
00180 
00181 /**
00182  * Unset flags of routing nexthop.
00183  * @arg nh              Routing nexthop.
00184  * @arg flags           Flags to be unset.
00185  */
00186 void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
00187 {
00188         nh->rtnh_flag_mask |= flags;
00189         nh->rtnh_flags &= ~flags;
00190         nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
00191 }
00192 
00193 /**
00194  * Get flags of routing nexthop.
00195  * @arg nh              Routing nexthop.
00196  * @return Flags or 0 if not available.
00197  */
00198 unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
00199 {
00200         if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
00201                 return nh->rtnh_flags;
00202         else
00203                 return 0;
00204 }
00205 
00206 /** @} */
00207 /** @} */

Generated on Fri Apr 27 14:14:07 2007 for libnl by  doxygen 1.5.1