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