proposal: Add possibility to register custom proposal keyword parser
[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 * registered algname parsers, as proposal_algname_parser_t
60 */
61 linked_list_t *parsers;
62
63 /**
64 * rwlock to lock access to modules
65 */
66 rwlock_t *lock;
67 };
68
69 /**
70 * Find the token object for the algorithm specified.
71 */
72 static const proposal_token_t* find_token(private_proposal_keywords_t *this,
73 const char *str)
74 {
75 proposal_token_t *token, *found = NULL;
76 enumerator_t *enumerator;
77
78 this->lock->read_lock(this->lock);
79 enumerator = this->tokens->create_enumerator(this->tokens);
80 while (enumerator->enumerate(enumerator, &token))
81 {
82 if (streq(token->name, str))
83 {
84 found = token;
85 break;
86 }
87 }
88 enumerator->destroy(enumerator);
89 this->lock->unlock(this->lock);
90 return found;
91 }
92
93 /**
94 * Parse the given algorithm into a token with user defined parser functions.
95 */
96 static const proposal_token_t* parse_token(private_proposal_keywords_t *this,
97 const char *str)
98 {
99 proposal_algname_parser_t parser;
100 enumerator_t *enumerator;
101 proposal_token_t *found = NULL;
102
103 this->lock->read_lock(this->lock);
104 enumerator = this->parsers->create_enumerator(this->parsers);
105 while (enumerator->enumerate(enumerator, &parser))
106 {
107 found = parser(str);
108 if (found)
109 {
110 break;
111 }
112 }
113 enumerator->destroy(enumerator);
114 this->lock->unlock(this->lock);
115 return found;
116 }
117
118 METHOD(proposal_keywords_t, get_token, const proposal_token_t*,
119 private_proposal_keywords_t *this, const char *str)
120 {
121 const proposal_token_t *token;
122
123 token = proposal_get_token_static(str, strlen(str));
124 if (!token)
125 {
126 token = find_token(this, str);
127 }
128 if (!token)
129 {
130 token = parse_token(this, str);
131 }
132 return token;
133 }
134
135 METHOD(proposal_keywords_t, register_token, void,
136 private_proposal_keywords_t *this, const char *name, transform_type_t type,
137 u_int16_t algorithm, u_int16_t keysize)
138 {
139 proposal_token_t *token;
140
141 INIT(token,
142 .name = strdup(name),
143 .type = type,
144 .algorithm = algorithm,
145 .keysize = keysize,
146 );
147
148 this->lock->write_lock(this->lock);
149 this->tokens->insert_first(this->tokens, token);
150 this->lock->unlock(this->lock);
151 }
152
153 METHOD(proposal_keywords_t, register_algname_parser, void,
154 private_proposal_keywords_t *this, proposal_algname_parser_t parser)
155 {
156 this->lock->write_lock(this->lock);
157 this->tokens->insert_first(this->parsers, parser);
158 this->lock->unlock(this->lock);
159 }
160
161 METHOD(proposal_keywords_t, destroy, void,
162 private_proposal_keywords_t *this)
163 {
164 proposal_token_t *token;
165
166 while (this->tokens->remove_first(this->tokens, (void**)&token) == SUCCESS)
167 {
168 free(token->name);
169 free(token);
170 }
171 this->tokens->destroy(this->tokens);
172 this->parsers->destroy(this->parsers);
173 this->lock->destroy(this->lock);
174 free(this);
175 }
176
177 /*
178 * Described in header.
179 */
180 proposal_keywords_t *proposal_keywords_create()
181 {
182 private_proposal_keywords_t *this;
183
184 INIT(this,
185 .public = {
186 .get_token = _get_token,
187 .register_token = _register_token,
188 .register_algname_parser = _register_algname_parser,
189 .destroy = _destroy,
190 },
191 .tokens = linked_list_create(),
192 .parsers = linked_list_create(),
193 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
194 );
195
196 return &this->public;
197 }