libnl  1.1
nexthop.c
1 /*
2  * lib/route/nexthop.c Routing Nexthop
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@suug.ch>
10  */
11 
12 /**
13  * @ingroup route_obj
14  * @defgroup nexthop Nexthop
15  * @{
16  */
17 
18 #include <netlink-local.h>
19 #include <netlink/netlink.h>
20 #include <netlink/utils.h>
21 #include <netlink/route/rtnl.h>
22 #include <netlink/route/route.h>
23 
24 /**
25  * @name Allocation/Freeing
26  * @{
27  */
28 
29 struct rtnl_nexthop *rtnl_route_nh_alloc(void)
30 {
31  struct rtnl_nexthop *nh;
32 
33  nh = calloc(1, sizeof(*nh));
34  if (!nh) {
35  nl_errno(ENOMEM);
36  return NULL;
37  }
38 
39  nl_init_list_head(&nh->rtnh_list);
40 
41  return nh;
42 }
43 
44 struct rtnl_nexthop *rtnl_route_nh_clone(struct rtnl_nexthop *src)
45 {
46  struct rtnl_nexthop *nh;
47 
48  nh = rtnl_route_nh_alloc();
49  if (!nh)
50  return NULL;
51 
52  nh->rtnh_flags = src->rtnh_flags;
53  nh->rtnh_flag_mask = src->rtnh_flag_mask;
54  nh->rtnh_weight = src->rtnh_weight;
55  nh->rtnh_ifindex = src->rtnh_ifindex;
56  nh->rtnh_mask = src->rtnh_mask;
57 
58  if (src->rtnh_gateway) {
59  nh->rtnh_gateway = nl_addr_clone(src->rtnh_gateway);
60  if (!nh->rtnh_gateway) {
61  free(nh);
62  return NULL;
63  }
64  }
65 
66  return nh;
67 }
68 
69 void rtnl_route_nh_free(struct rtnl_nexthop *nh)
70 {
71  nl_addr_put(nh->rtnh_gateway);
72  free(nh);
73 }
74 
75 /** @} */
76 
77 /**
78  * @name Attributes
79  */
80 
81 void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
82 {
83  nh->rtnh_weight = weight;
84  nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
85 }
86 
87 int rtnl_route_nh_get_weight(struct rtnl_nexthop *nh)
88 {
89  if (nh->rtnh_mask & NEXTHOP_HAS_WEIGHT)
90  return nh->rtnh_weight;
91  else
92  return 0;
93 }
94 
95 void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
96 {
97  nh->rtnh_ifindex = ifindex;
98  nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
99 }
100 
101 int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
102 {
103  if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
104  return nh->rtnh_ifindex;
105  else
106  return -1;
107 }
108 
109 void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
110 {
111  struct nl_addr *old = nh->rtnh_gateway;
112 
113  nh->rtnh_gateway = nl_addr_get(addr);
114  if (old)
115  nl_addr_put(old);
116 
117  nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
118 }
119 
120 struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
121 {
122  if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
123  return nh->rtnh_gateway;
124  else
125  return NULL;
126 }
127 
128 void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
129 {
130  nh->rtnh_flag_mask |= flags;
131  nh->rtnh_flags |= flags;
132  nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
133 }
134 
135 void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
136 {
137  nh->rtnh_flag_mask |= flags;
138  nh->rtnh_flags &= ~flags;
139  nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
140 }
141 
142 unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
143 {
144  if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
145  return nh->rtnh_flags;
146  else
147  return 0;
148 }
149 
150 /** @} */
151 /** @} */
struct nl_addr * nl_addr_clone(struct nl_addr *addr)
Clone existing abstract address object.
Definition: addr.c:406