oops
[libnl.git] / lib / route / 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
14  * @defgroup nexthop Nexthop
15  * @brief
16  * @{
17  */
18
19 #include <netlink-local.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink/route/rtnl.h>
23 #include <netlink/route/route.h>
24
25 /** @cond SKIP */
26 #define NEXTHOP_HAS_FLAGS   0x000001
27 #define NEXTHOP_HAS_WEIGHT  0x000002
28 #define NEXTHOP_HAS_IFINDEX 0x000004
29 #define NEXTHOP_HAS_GATEWAY 0x000008
30 /** @endcond */
31
32 /**
33  * @name Nexthop Allocation/Freeage
34  * @{
35  */
36
37 /**
38  * Allocate a routing nexthop.
39  * @return Newly allocated routing nexthop object.
40  */
41 struct rtnl_nexthop *rtnl_route_nh_alloc(void)
42 {
43         struct rtnl_nexthop *nh;
44
45         nh = calloc(1, sizeof(*nh));
46         if (!nh) {
47                 nl_errno(ENOMEM);
48                 return NULL;
49         }
50
51         nl_init_list_head(&nh->rtnh_list);
52
53         return nh;
54 }
55
56 /**
57  * Free a routing nexthop.
58  * @arg nh              Routing nexthop to be freed.
59  */
60 void rtnl_route_nh_free(struct rtnl_nexthop *nh)
61 {
62         nl_addr_put(nh->rtnh_gateway);
63         free(nh);
64 }
65
66 /** @} */
67 /**
68  * @name Attribute: Weight
69  */
70
71 /**
72  * Set weight of routing nexthop.
73  * @arg nh              Routing nexthop.
74  * @arg weight          New weight value.
75  */
76 void rtnl_route_nh_set_weight(struct rtnl_nexthop *nh, int weight)
77 {
78         nh->rtnh_weight = weight;
79         nh->rtnh_mask |= NEXTHOP_HAS_WEIGHT;
80 }
81
82 /**
83  * Get weight of routing nexthop.
84  * @arg nh              Routing nexthop.
85  * @return Weight value or 0 if not available.
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 /** @} */
96 /**
97  * @name Attribute: Interface Index
98  * @{
99  */
100
101 /**
102  * Set interface index for outgoing interface of routing nexthop.
103  * @arg nh              Routing nexthop.
104  * @arg ifindex         New interface index.
105  */
106 void rtnl_route_nh_set_ifindex(struct rtnl_nexthop *nh, int ifindex)
107 {
108         nh->rtnh_ifindex = ifindex;
109         nh->rtnh_mask |= NEXTHOP_HAS_IFINDEX;
110 }
111
112 /**
113  * Get interface index of outgoing index of routing nexthop.
114  * @arg nh              Routing nexthop.
115  * @return Interface index or -1 if not available.
116  */
117 int rtnl_route_nh_get_ifindex(struct rtnl_nexthop *nh)
118 {
119         if (nh->rtnh_mask & NEXTHOP_HAS_IFINDEX)
120                 return nh->rtnh_ifindex;
121         else
122                 return -1;
123 }       
124
125 /** @} */
126 /**
127  * @name Attribute: Gateway Address
128  * @{
129  */
130
131 /**
132  * Set gateway address of routing nexthop.
133  * @arg nh              Routing nexthop.
134  * @arg addr            New gateway address.
135  *
136  * An eventual existing gateway address will be freed and a
137  * reference is acquried of the new address.
138  */
139 void rtnl_route_nh_set_gateway(struct rtnl_nexthop *nh, struct nl_addr *addr)
140 {
141         struct nl_addr *old = nh->rtnh_gateway;
142
143         nh->rtnh_gateway = nl_addr_get(addr);
144         if (old)
145                 nl_addr_put(old);
146
147         nh->rtnh_mask |= NEXTHOP_HAS_GATEWAY;
148 }
149
150 /**
151  * Get gateway address of routing nexthop.
152  * @arg nh              Routing nexthop.
153  * @return Gateway address or NULL if not available.
154  */
155 struct nl_addr *rtnl_route_nh_get_gateway(struct rtnl_nexthop *nh)
156 {
157         if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
158                 return nh->rtnh_gateway;
159         else
160                 return NULL;
161 }
162
163 /** @} */
164 /**
165  * @name Attribute: Flags
166  * @{
167  */
168
169 /**
170  * Set flags of routing nexthop.
171  * @arg nh              Routing nexthop.
172  * @arg flags           Flags to be set.
173  */
174 void rtnl_route_nh_set_flags(struct rtnl_nexthop *nh, unsigned int flags)
175 {
176         nh->rtnh_flag_mask |= flags;
177         nh->rtnh_flags |= flags;
178         nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
179 }
180
181 /**
182  * Unset flags of routing nexthop.
183  * @arg nh              Routing nexthop.
184  * @arg flags           Flags to be unset.
185  */
186 void rtnl_route_nh_unset_flags(struct rtnl_nexthop *nh, unsigned int flags)
187 {
188         nh->rtnh_flag_mask |= flags;
189         nh->rtnh_flags &= ~flags;
190         nh->rtnh_mask |= NEXTHOP_HAS_FLAGS;
191 }
192
193 /**
194  * Get flags of routing nexthop.
195  * @arg nh              Routing nexthop.
196  * @return Flags or 0 if not available.
197  */
198 unsigned int rtnl_route_nh_get_flags(struct rtnl_nexthop *nh)
199 {
200         if (nh->rtnh_mask & NEXTHOP_HAS_FLAGS)
201                 return nh->rtnh_flags;
202         else
203                 return 0;
204 }
205
206 /** @} */
207 /** @} */