Moved data structures to new collections subfolder
[strongswan.git] / src / libstrongswan / crypto / proposal / proposal_keywords.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * 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 /*
17 * Copyright (c) 2012 Nanoteq Pty Ltd
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to deal
21 * in the Software without restriction, including without limitation the rights
22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23 * copies of the Software, and to permit persons to whom the Software is
24 * furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35 * THE SOFTWARE.
36 */
37
38 #include "proposal_keywords.h"
39 #include "proposal_keywords_static.h"
40
41 #include <collections/linked_list.h>
42 #include <threading/rwlock.h>
43
44 typedef struct private_proposal_keywords_t private_proposal_keywords_t;
45
46 struct private_proposal_keywords_t {
47
48 /**
49 * public interface
50 */
51 proposal_keywords_t public;
52
53 /**
54 * registered tokens, as proposal_token_t
55 */
56 linked_list_t * tokens;
57
58 /**
59 * rwlock to lock access to modules
60 */
61 rwlock_t *lock;
62 };
63
64 /**
65 * Find the token object for the algorithm specified.
66 */
67 static const proposal_token_t* find_token(private_proposal_keywords_t *this,
68 const char *str)
69 {
70 proposal_token_t *token, *found = NULL;
71 enumerator_t *enumerator;
72
73 this->lock->read_lock(this->lock);
74 enumerator = this->tokens->create_enumerator(this->tokens);
75 while (enumerator->enumerate(enumerator, &token))
76 {
77 if (streq(token->name, str))
78 {
79 found = token;
80 break;
81 }
82 }
83 enumerator->destroy(enumerator);
84 this->lock->unlock(this->lock);
85 return found;
86 }
87
88 METHOD(proposal_keywords_t, get_token, const proposal_token_t*,
89 private_proposal_keywords_t *this, const char *str)
90 {
91 const proposal_token_t *token = proposal_get_token_static(str, strlen(str));
92 return token ?: find_token(this, str);
93 }
94
95 METHOD(proposal_keywords_t, register_token, void,
96 private_proposal_keywords_t *this, const char *name, transform_type_t type,
97 u_int16_t algorithm, u_int16_t keysize)
98 {
99 proposal_token_t *token;
100
101 INIT(token,
102 .name = strdup(name),
103 .type = type,
104 .algorithm = algorithm,
105 .keysize = keysize,
106 );
107
108 this->lock->write_lock(this->lock);
109 this->tokens->insert_first(this->tokens, token);
110 this->lock->unlock(this->lock);
111 }
112
113 METHOD(proposal_keywords_t, destroy, void,
114 private_proposal_keywords_t *this)
115 {
116 proposal_token_t *token;
117
118 while (this->tokens->remove_first(this->tokens, (void**)&token) == SUCCESS)
119 {
120 free(token->name);
121 free(token);
122 }
123 this->tokens->destroy(this->tokens);
124 this->lock->destroy(this->lock);
125 free(this);
126 }
127
128 /*
129 * Described in header.
130 */
131 proposal_keywords_t *proposal_keywords_create()
132 {
133 private_proposal_keywords_t *this;
134
135 INIT(this,
136 .public = {
137 .get_token = _get_token,
138 .register_token = _register_token,
139 .destroy = _destroy,
140 },
141 .tokens = linked_list_create(),
142 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
143 );
144
145 return &this->public;
146 }