libimcv: Implemented IETF SW PA-TNC attributes
[strongswan.git] / src / libimcv / swima / swima_inventory.c
1 /*
2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "swima_inventory.h"
17 #include "swima_record.h"
18
19 #include <collections/linked_list.h>
20 #include <utils/debug.h>
21
22 typedef struct private_swima_inventory_t private_swima_inventory_t;
23
24 /**
25 * Private data of a swima_inventory_t object.
26 *
27 */
28 struct private_swima_inventory_t {
29
30 /**
31 * Public swima_inventory_t interface.
32 */
33 swima_inventory_t public;
34
35 /**
36 * Earliest or last event ID of the inventory
37 */
38 uint32_t eid;
39
40 /**
41 * Epoch of event IDs
42 */
43 uint32_t epoch;
44
45 /**
46 * List of SW records
47 */
48 linked_list_t *list;
49
50
51 /**
52 * Reference count
53 */
54 refcount_t ref;
55
56 };
57
58 METHOD(swima_inventory_t, add, void,
59 private_swima_inventory_t *this, swima_record_t *record)
60 {
61 this->list->insert_last(this->list, record);
62 }
63
64 METHOD(swima_inventory_t, get_count, int,
65 private_swima_inventory_t *this)
66 {
67 return this->list->get_count(this->list);
68 }
69
70 METHOD(swima_inventory_t, set_eid, void,
71 private_swima_inventory_t *this, uint32_t eid, uint32_t epoch)
72 {
73 this->eid = eid;
74 this->epoch = epoch;
75 }
76
77 METHOD(swima_inventory_t, get_eid, uint32_t,
78 private_swima_inventory_t *this, uint32_t *epoch)
79 {
80 if (epoch)
81 {
82 *epoch = this->epoch;
83 }
84 return this->eid;
85 }
86
87 METHOD(swima_inventory_t, create_enumerator, enumerator_t*,
88 private_swima_inventory_t *this)
89 {
90 return this->list->create_enumerator(this->list);
91 }
92
93 METHOD(swima_inventory_t, get_ref, swima_inventory_t*,
94 private_swima_inventory_t *this)
95 {
96 ref_get(&this->ref);
97 return &this->public;
98 }
99
100 METHOD(swima_inventory_t, clear, void,
101 private_swima_inventory_t *this)
102 {
103 this->list->destroy_offset(this->list, offsetof(swima_record_t, destroy));
104 this->list = linked_list_create();
105 }
106
107 METHOD(swima_inventory_t, destroy, void,
108 private_swima_inventory_t *this)
109 {
110 if (ref_put(&this->ref))
111 {
112 this->list->destroy_offset(this->list, offsetof(swima_record_t, destroy));
113 free(this);
114 }
115 }
116
117 /**
118 * See header
119 */
120 swima_inventory_t *swima_inventory_create(void)
121 {
122 private_swima_inventory_t *this;
123
124 INIT(this,
125 .public = {
126 .add = _add,
127 .get_count = _get_count,
128 .set_eid = _set_eid,
129 .get_eid = _get_eid,
130 .create_enumerator = _create_enumerator,
131 .get_ref = _get_ref,
132 .clear = _clear,
133 .destroy = _destroy,
134 },
135 .list = linked_list_create(),
136 .ref = 1,
137 );
138
139 return &this->public;
140 }