libnl  3.4.0
lifetime.c
1 /*
2  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the
15  * distribution.
16  *
17  * Neither the name of Texas Instruments Incorporated nor the names of
18  * its contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 /**
35  * @ingroup xfrmnl
36  * @defgroup XFRM Lifetime Configuration Object
37  *
38  * Abstract data type representing XFRM SA lifetime properties
39  *
40  * @{
41  *
42  * Header
43  * ------
44  * ~~~~{.c}
45  * #include <netlink/xfrm/lifetime.h>
46  * ~~~~
47  */
48 
49 #include <netlink/xfrm/lifetime.h>
50 #include <netlink-private/netlink.h>
51 
52 static void ltime_cfg_destroy(struct xfrmnl_ltime_cfg* ltime)
53 {
54  if (!ltime)
55  return;
56 
57  if (ltime->refcnt != 1)
58  {
59  fprintf(stderr, "BUG: %s:%d\n", __FILE__, __LINE__);
60  assert(0);
61  }
62 
63  free(ltime);
64 }
65 
66 /**
67  * @name Creating Selector
68  * @{
69  */
70 
71 /**
72  * Allocate new lifetime config object.
73  * @return Newly allocated lifetime config object or NULL
74  */
75 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_alloc()
76 {
77  struct xfrmnl_ltime_cfg* ltime;
78 
79  ltime = calloc(1, sizeof(struct xfrmnl_ltime_cfg));
80  if (!ltime)
81  return NULL;
82 
83  ltime->refcnt = 1;
84 
85  return ltime;
86 }
87 
88 /**
89  * Clone existing lifetime config object.
90  * @arg ltime Selector object.
91  * @return Newly allocated lifetime config object being a duplicate of the
92  * specified lifetime config object or NULL if a failure occured.
93  */
94 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg* ltime)
95 {
96  struct xfrmnl_ltime_cfg* new;
97 
98  new = xfrmnl_ltime_cfg_alloc();
99  if (new)
100  memcpy ((void*)new, (void*)ltime, sizeof (struct xfrmnl_ltime_cfg));
101 
102  return new;
103 }
104 
105 /** @} */
106 
107 /**
108  * @name Managing Usage References
109  * @{
110  */
111 
112 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_get(struct xfrmnl_ltime_cfg* ltime)
113 {
114  ltime->refcnt++;
115 
116  return ltime;
117 }
118 
119 void xfrmnl_ltime_cfg_put(struct xfrmnl_ltime_cfg* ltime)
120 {
121  if (!ltime)
122  return;
123 
124  if (ltime->refcnt == 1)
125  ltime_cfg_destroy(ltime);
126  else
127  ltime->refcnt--;
128 }
129 
130 /**
131  * Check whether an lifetime config object is shared.
132  * @arg addr Selector object.
133  * @return Non-zero if the lifetime config object is shared, otherwise 0.
134  */
135 int xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg* ltime)
136 {
137  return ltime->refcnt > 1;
138 }
139 
140 /** @} */
141 
142 /**
143  * @name Miscellaneous
144  * @{
145  */
146 
147 /**
148  * Compares two lifetime config objects.
149  * @arg a A lifetime config object.
150  * @arg b Another lifetime config object.
151  *
152  * @return Non zero if difference is found, 0 otherwise if both
153  * the objects are identical.
154  */
155 int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg* a, struct xfrmnl_ltime_cfg* b)
156 {
157  /* Check for any differences */
158  if ((a->soft_byte_limit != b->soft_byte_limit) ||
159  (a->soft_packet_limit != b->soft_packet_limit) ||
160  (a->hard_byte_limit != b->hard_byte_limit) ||
161  (a->hard_packet_limit != b->hard_packet_limit) ||
162  (a->soft_add_expires_seconds != b->soft_add_expires_seconds) ||
163  (a->hard_add_expires_seconds != b->hard_add_expires_seconds) ||
164  (a->soft_use_expires_seconds != b->soft_use_expires_seconds) ||
165  (a->hard_use_expires_seconds != b->hard_use_expires_seconds))
166  return 1;
167 
168  /* The objects are identical */
169  return 0;
170 }
171 
172 /** @} */
173 
174 /**
175  * @name Attributes
176  * @{
177  */
178 unsigned long long xfrmnl_ltime_cfg_get_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime)
179 {
180  return ltime->soft_byte_limit;
181 }
182 
183 int xfrmnl_ltime_cfg_set_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_byte_limit)
184 {
185  ltime->soft_byte_limit = soft_byte_limit;
186 
187  return 0;
188 }
189 
190 unsigned long long xfrmnl_ltime_cfg_get_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime)
191 {
192  return ltime->hard_byte_limit;
193 }
194 
195 int xfrmnl_ltime_cfg_set_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_byte_limit)
196 {
197  ltime->hard_byte_limit = hard_byte_limit;
198 
199  return 0;
200 }
201 
202 unsigned long long xfrmnl_ltime_cfg_get_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime)
203 {
204  return ltime->soft_packet_limit;
205 }
206 
207 int xfrmnl_ltime_cfg_set_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_packet_limit)
208 {
209  ltime->soft_packet_limit = soft_packet_limit;
210 
211  return 0;
212 }
213 
214 unsigned long long xfrmnl_ltime_cfg_get_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime)
215 {
216  return ltime->hard_packet_limit;
217 }
218 
219 int xfrmnl_ltime_cfg_set_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_packet_limit)
220 {
221  ltime->hard_packet_limit = hard_packet_limit;
222 
223  return 0;
224 }
225 
226 unsigned long long xfrmnl_ltime_cfg_get_soft_addexpires (struct xfrmnl_ltime_cfg* ltime)
227 {
228  return ltime->soft_add_expires_seconds;
229 }
230 
231 int xfrmnl_ltime_cfg_set_soft_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_add_expires_seconds)
232 {
233  ltime->soft_add_expires_seconds = soft_add_expires_seconds;
234 
235  return 0;
236 }
237 
238 unsigned long long xfrmnl_ltime_cfg_get_hard_addexpires (struct xfrmnl_ltime_cfg* ltime)
239 {
240  return ltime->hard_add_expires_seconds;
241 }
242 
243 int xfrmnl_ltime_cfg_set_hard_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_add_expires_seconds)
244 {
245  ltime->hard_add_expires_seconds = hard_add_expires_seconds;
246 
247  return 0;
248 }
249 
250 unsigned long long xfrmnl_ltime_cfg_get_soft_useexpires (struct xfrmnl_ltime_cfg* ltime)
251 {
252  return ltime->soft_use_expires_seconds;
253 }
254 
255 int xfrmnl_ltime_cfg_set_soft_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_use_expires_seconds)
256 {
257  ltime->soft_use_expires_seconds = soft_use_expires_seconds;
258 
259  return 0;
260 }
261 
262 unsigned long long xfrmnl_ltime_cfg_get_hard_useexpires (struct xfrmnl_ltime_cfg* ltime)
263 {
264  return ltime->hard_use_expires_seconds;
265 }
266 
267 int xfrmnl_ltime_cfg_set_hard_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_use_expires_seconds)
268 {
269  ltime->hard_use_expires_seconds = hard_use_expires_seconds;
270 
271  return 0;
272 }
273 
274 /** @} */
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg *ltime)
Clone existing lifetime config object.
Definition: lifetime.c:94
int xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg *ltime)
Check whether an lifetime config object is shared.
Definition: lifetime.c:135
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_alloc()
Allocate new lifetime config object.
Definition: lifetime.c:75
int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg *a, struct xfrmnl_ltime_cfg *b)
Compares two lifetime config objects.
Definition: lifetime.c:155