libnl 1.1

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_obj
00014  * @defgroup nexthop Nexthop
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 #include <netlink/route/route.h>
00023 
00024 /**
00025  * @name Allocation/Freeing
00026  * @{
00027  */
00028 
00029 struct rtnl_nexthop *rtnl_route_nh_alloc(void)
00030 {
00031         struct rtnl_nexthop *nh;
00032 
00033         nh = calloc(1, sizeof(*nh));
00034         if (!nh) {
00035                 nl_errno(ENOMEM);
00036                 return NULL;
00037         }
00038 
00039         nl_init_list_head(&nh->rtnh_list);
00040 
00041         return nh;
00042 }
00043 
00044 struct rtnl_nexthop *rtnl_route_nh_clone(struct rtnl_nexthop *src)
00045 {
00046         struct rtnl_nexthop *nh;
00047 
00048         nh = rtnl_route_nh_alloc();
00049         if (!nh)
00050                 return NULL;
00051 
00052         nh->rtnh_flags = src->rtnh_flags;
00053         nh->rtnh_flag_mask = src->rtnh_flag_mask;
00054         nh->rtnh_weight = src->rtnh_weight;
00055         nh->rtnh_ifindex = src->rtnh_ifindex;
00056         nh->rtnh_mask = src->rtnh_mask;
00057 
00058         if (src->rtnh_gateway) {
00059                 nh->rtnh_gateway = nl_addr_clone(src->rtnh_gateway);
00060                 if (!nh->rtnh_gateway) {
00061                         free(nh);
00062                         return NULL;
00063                 }
00064         }
00065 
00066         return nh;
00067 }
00068 
00069 void rtnl_route_nh_free(struct rtnl_nexthop *nh)
00070 {
00071         nl_addr_put(nh->rtnh_gateway);
00072         free(nh);
00073 }
00074 
00075 /** @} */
00076 
00077 /**
00078  * @name Attributes
00079  */
00080 
00081 void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
00082 {
00083         nh->rtnh_weight = weight;
00084         nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
00085 }
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 void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
00096 {
00097         nh->rtnh_ifindex = ifindex;
00098         nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
00099 }
00100 
00101 int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
00102 {
00103         if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
00104                 return nh->rtnh_ifindex;
00105         else
00106                 return -1;
00107 }       
00108 
00109 void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
00110 {
00111         struct nl_addr *old = nh->rtnh_gateway;
00112 
00113         nh->rtnh_gateway = nl_addr_get(addr);
00114         if (old)
00115                 nl_addr_put(old);
00116 
00117         nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
00118 }
00119 
00120 struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
00121 {
00122         if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
00123                 return nh->rtnh_gateway;
00124         else
00125                 return NULL;
00126 }
00127 
00128 void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
00129 {
00130         nh->rtnh_flag_mask |= flags;
00131         nh->rtnh_flags |= flags;
00132         nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
00133 }
00134 
00135 void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
00136 {
00137         nh->rtnh_flag_mask |= flags;
00138         nh->rtnh_flags &= ~flags;
00139         nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
00140 }
00141 
00142 unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
00143 {
00144         if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
00145                 return nh->rtnh_flags;
00146         else
00147                 return 0;
00148 }
00149 
00150 /** @} */
00151 /** @} */