Moved data structures to new collections subfolder
[strongswan.git] / src / libimcv / pa_tnc / pa_tnc_attr_manager.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
3 *
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "pa_tnc_attr_manager.h"
18
19 #include <collections/linked_list.h>
20 #include <debug.h>
21
22 typedef struct private_pa_tnc_attr_manager_t private_pa_tnc_attr_manager_t;
23 typedef struct entry_t entry_t;
24
25 struct entry_t {
26 pen_t vendor_id;
27 enum_name_t *attr_names;
28 pa_tnc_attr_create_t attr_create;
29 };
30
31 /**
32 * Private data of a pa_tnc_attr_manager_t object.
33 *
34 */
35 struct private_pa_tnc_attr_manager_t {
36
37 /**
38 * Public pa_tnc_attr_manager_t interface.
39 */
40 pa_tnc_attr_manager_t public;
41
42 /**
43 * List of PA-TNC vendor attributes
44 */
45 linked_list_t *list;
46 };
47
48 METHOD(pa_tnc_attr_manager_t, add_vendor, void,
49 private_pa_tnc_attr_manager_t *this, pen_t vendor_id,
50 pa_tnc_attr_create_t attr_create, enum_name_t *attr_names)
51 {
52 entry_t *entry;
53
54 entry = malloc_thing(entry_t);
55 entry->vendor_id = vendor_id;
56 entry->attr_create = attr_create;
57 entry->attr_names = attr_names;
58
59 this->list->insert_last(this->list, entry);
60 DBG2(DBG_TNC, "added %N attributes", pen_names, vendor_id);
61 }
62
63 METHOD(pa_tnc_attr_manager_t, remove_vendor, void,
64 private_pa_tnc_attr_manager_t *this, pen_t vendor_id)
65 {
66 enumerator_t *enumerator;
67 entry_t *entry;
68
69 enumerator = this->list->create_enumerator(this->list);
70 while (enumerator->enumerate(enumerator, &entry))
71 {
72 if (entry->vendor_id == vendor_id)
73 {
74 this->list->remove_at(this->list, enumerator);
75 free(entry);
76 DBG2(DBG_TNC, "removed %N attributes", pen_names, vendor_id);
77 }
78 }
79 enumerator->destroy(enumerator);
80 }
81
82 METHOD(pa_tnc_attr_manager_t, get_names, enum_name_t*,
83 private_pa_tnc_attr_manager_t *this, pen_t vendor_id)
84 {
85 enumerator_t *enumerator;
86 entry_t *entry;
87 enum_name_t *attr_names = NULL;
88
89 enumerator = this->list->create_enumerator(this->list);
90 while (enumerator->enumerate(enumerator, &entry))
91 {
92 if (entry->vendor_id == vendor_id)
93 {
94 attr_names = entry->attr_names;
95 break;
96 }
97 }
98 enumerator->destroy(enumerator);
99
100 return attr_names;
101 }
102
103 METHOD(pa_tnc_attr_manager_t, create, pa_tnc_attr_t*,
104 private_pa_tnc_attr_manager_t *this, pen_t vendor_id, u_int32_t type,
105 chunk_t value)
106 {
107 enumerator_t *enumerator;
108 entry_t *entry;
109 pa_tnc_attr_t *attr = NULL;
110
111 enumerator = this->list->create_enumerator(this->list);
112 while (enumerator->enumerate(enumerator, &entry))
113 {
114 if (entry->vendor_id == vendor_id)
115 {
116 if (entry->attr_create)
117 {
118 attr = entry->attr_create(type, value);
119 }
120 break;
121 }
122 }
123 enumerator->destroy(enumerator);
124
125 return attr;
126 }
127
128 METHOD(pa_tnc_attr_manager_t, destroy, void,
129 private_pa_tnc_attr_manager_t *this)
130 {
131 this->list->destroy_function(this->list, free);
132 free(this);
133 }
134
135 /**
136 * See header
137 */
138 pa_tnc_attr_manager_t *pa_tnc_attr_manager_create(void)
139 {
140 private_pa_tnc_attr_manager_t *this;
141
142 INIT(this,
143 .public = {
144 .add_vendor = _add_vendor,
145 .remove_vendor = _remove_vendor,
146 .get_names = _get_names,
147 .create = _create,
148 .destroy = _destroy,
149 },
150 .list = linked_list_create(),
151 );
152
153 return &this->public;
154 }
155