libnl  3.2.28
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-private/netlink.h>
50 
51 static void ltime_cfg_destroy(struct xfrmnl_ltime_cfg* ltime)
52 {
53  if (!ltime)
54  return;
55 
56  if (ltime->refcnt != 1)
57  {
58  fprintf(stderr, "BUG: %s:%d\n", __FILE__, __LINE__);
59  assert(0);
60  }
61 
62  free(ltime);
63 }
64 
65 /**
66  * @name Creating Selector
67  * @{
68  */
69 
70 /**
71  * Allocate new lifetime config object.
72  * @return Newly allocated lifetime config object or NULL
73  */
74 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_alloc()
75 {
76  struct xfrmnl_ltime_cfg* ltime;
77 
78  ltime = calloc(1, sizeof(struct xfrmnl_ltime_cfg));
79  if (!ltime)
80  return NULL;
81 
82  ltime->refcnt = 1;
83 
84  return ltime;
85 }
86 
87 /**
88  * Clone existing lifetime config object.
89  * @arg ltime Selector object.
90  * @return Newly allocated lifetime config object being a duplicate of the
91  * specified lifetime config object or NULL if a failure occured.
92  */
93 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg* ltime)
94 {
95  struct xfrmnl_ltime_cfg* new;
96 
97  new = xfrmnl_ltime_cfg_alloc();
98  if (new)
99  memcpy ((void*)new, (void*)ltime, sizeof (struct xfrmnl_ltime_cfg));
100 
101  return new;
102 }
103 
104 /** @} */
105 
106 /**
107  * @name Managing Usage References
108  * @{
109  */
110 
111 struct xfrmnl_ltime_cfg* xfrmnl_ltime_cfg_get(struct xfrmnl_ltime_cfg* ltime)
112 {
113  ltime->refcnt++;
114 
115  return ltime;
116 }
117 
118 void xfrmnl_ltime_cfg_put(struct xfrmnl_ltime_cfg* ltime)
119 {
120  if (!ltime)
121  return;
122 
123  if (ltime->refcnt == 1)
124  ltime_cfg_destroy(ltime);
125  else
126  ltime->refcnt--;
127 }
128 
129 /**
130  * Check whether an lifetime config object is shared.
131  * @arg addr Selector object.
132  * @return Non-zero if the lifetime config object is shared, otherwise 0.
133  */
134 int xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg* ltime)
135 {
136  return ltime->refcnt > 1;
137 }
138 
139 /** @} */
140 
141 /**
142  * @name Miscellaneous
143  * @{
144  */
145 
146 /**
147  * Compares two lifetime config objects.
148  * @arg a A lifetime config object.
149  * @arg b Another lifetime config object.
150  *
151  * @return Non zero if difference is found, 0 otherwise if both
152  * the objects are identical.
153  */
154 int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg* a, struct xfrmnl_ltime_cfg* b)
155 {
156  /* Check for any differences */
157  if ((a->soft_byte_limit != b->soft_byte_limit) ||
158  (a->soft_packet_limit != b->soft_packet_limit) ||
159  (a->hard_byte_limit != b->hard_byte_limit) ||
160  (a->hard_packet_limit != b->hard_packet_limit) ||
161  (a->soft_add_expires_seconds != b->soft_add_expires_seconds) ||
162  (a->hard_add_expires_seconds != b->hard_add_expires_seconds) ||
163  (a->soft_use_expires_seconds != b->soft_use_expires_seconds) ||
164  (a->hard_use_expires_seconds != b->hard_use_expires_seconds))
165  return 1;
166 
167  /* The objects are identical */
168  return 0;
169 }
170 
171 /** @} */
172 
173 /**
174  * @name Attributes
175  * @{
176  */
177 unsigned long long xfrmnl_ltime_cfg_get_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime)
178 {
179  return ltime->soft_byte_limit;
180 }
181 
182 int xfrmnl_ltime_cfg_set_soft_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_byte_limit)
183 {
184  ltime->soft_byte_limit = soft_byte_limit;
185 
186  return 0;
187 }
188 
189 unsigned long long xfrmnl_ltime_cfg_get_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime)
190 {
191  return ltime->hard_byte_limit;
192 }
193 
194 int xfrmnl_ltime_cfg_set_hard_bytelimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_byte_limit)
195 {
196  ltime->hard_byte_limit = hard_byte_limit;
197 
198  return 0;
199 }
200 
201 unsigned long long xfrmnl_ltime_cfg_get_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime)
202 {
203  return ltime->soft_packet_limit;
204 }
205 
206 int xfrmnl_ltime_cfg_set_soft_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_packet_limit)
207 {
208  ltime->soft_packet_limit = soft_packet_limit;
209 
210  return 0;
211 }
212 
213 unsigned long long xfrmnl_ltime_cfg_get_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime)
214 {
215  return ltime->hard_packet_limit;
216 }
217 
218 int xfrmnl_ltime_cfg_set_hard_packetlimit (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_packet_limit)
219 {
220  ltime->hard_packet_limit = hard_packet_limit;
221 
222  return 0;
223 }
224 
225 unsigned long long xfrmnl_ltime_cfg_get_soft_addexpires (struct xfrmnl_ltime_cfg* ltime)
226 {
227  return ltime->soft_add_expires_seconds;
228 }
229 
230 int xfrmnl_ltime_cfg_set_soft_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_add_expires_seconds)
231 {
232  ltime->soft_add_expires_seconds = soft_add_expires_seconds;
233 
234  return 0;
235 }
236 
237 unsigned long long xfrmnl_ltime_cfg_get_hard_addexpires (struct xfrmnl_ltime_cfg* ltime)
238 {
239  return ltime->hard_add_expires_seconds;
240 }
241 
242 int xfrmnl_ltime_cfg_set_hard_addexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_add_expires_seconds)
243 {
244  ltime->hard_add_expires_seconds = hard_add_expires_seconds;
245 
246  return 0;
247 }
248 
249 unsigned long long xfrmnl_ltime_cfg_get_soft_useexpires (struct xfrmnl_ltime_cfg* ltime)
250 {
251  return ltime->soft_use_expires_seconds;
252 }
253 
254 int xfrmnl_ltime_cfg_set_soft_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long soft_use_expires_seconds)
255 {
256  ltime->soft_use_expires_seconds = soft_use_expires_seconds;
257 
258  return 0;
259 }
260 
261 unsigned long long xfrmnl_ltime_cfg_get_hard_useexpires (struct xfrmnl_ltime_cfg* ltime)
262 {
263  return ltime->hard_use_expires_seconds;
264 }
265 
266 int xfrmnl_ltime_cfg_set_hard_useexpires (struct xfrmnl_ltime_cfg* ltime, unsigned long long hard_use_expires_seconds)
267 {
268  ltime->hard_use_expires_seconds = hard_use_expires_seconds;
269 
270  return 0;
271 }
272 
273 /** @} */
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_clone(struct xfrmnl_ltime_cfg *ltime)
Clone existing lifetime config object.
Definition: lifetime.c:93
int xfrmnl_ltime_cfg_shared(struct xfrmnl_ltime_cfg *ltime)
Check whether an lifetime config object is shared.
Definition: lifetime.c:134
struct xfrmnl_ltime_cfg * xfrmnl_ltime_cfg_alloc()
Allocate new lifetime config object.
Definition: lifetime.c:74
int xfrmnl_ltime_cfg_cmp(struct xfrmnl_ltime_cfg *a, struct xfrmnl_ltime_cfg *b)
Compares two lifetime config objects.
Definition: lifetime.c:154