Moved data structures to new collections subfolder
[strongswan.git] / src / libcharon / plugins / dhcp / dhcp_provider.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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 "dhcp_provider.h"
17
18 #include <collections/hashtable.h>
19 #include <threading/mutex.h>
20
21 typedef struct private_dhcp_provider_t private_dhcp_provider_t;
22
23 /**
24 * Private data of an dhcp_provider_t object.
25 */
26 struct private_dhcp_provider_t {
27
28 /**
29 * Public dhcp_provider_t interface.
30 */
31 dhcp_provider_t public;
32
33 /**
34 * Completed DHCP transactions
35 */
36 hashtable_t *transactions;
37
38 /**
39 * Lock for transactions
40 */
41 mutex_t *mutex;
42
43 /**
44 * DHCP communication socket
45 */
46 dhcp_socket_t *socket;
47 };
48
49 /**
50 * Hashtable hash function
51 */
52 static u_int hash(void *key)
53 {
54 return (uintptr_t)key;
55 }
56
57 /**
58 * Hashtable equals function
59 */
60 static bool equals(void *a, void *b)
61 {
62 return a == b;
63 }
64
65 /**
66 * Hash ID and host to a key
67 */
68 static uintptr_t hash_id_host(identification_t *id, host_t *host)
69 {
70 return chunk_hash_inc(id->get_encoding(id),
71 chunk_hash(host->get_address(host)));
72 }
73
74 /**
75 * Hash a DHCP transaction to a key, using address and id
76 */
77 static uintptr_t hash_transaction(dhcp_transaction_t *transaction)
78 {
79 return hash_id_host(transaction->get_identity(transaction),
80 transaction->get_address(transaction));
81 }
82
83 METHOD(attribute_provider_t, acquire_address, host_t*,
84 private_dhcp_provider_t *this, linked_list_t *pools,
85 identification_t *id, host_t *requested)
86 {
87 dhcp_transaction_t *transaction, *old;
88 enumerator_t *enumerator;
89 char *pool;
90 host_t *vip = NULL;
91
92 if (requested->get_family(requested) != AF_INET)
93 {
94 return NULL;
95 }
96 enumerator = pools->create_enumerator(pools);
97 while (enumerator->enumerate(enumerator, &pool))
98 {
99 if (!streq(pool, "dhcp"))
100 {
101 continue;
102 }
103 transaction = this->socket->enroll(this->socket, id);
104 if (!transaction)
105 {
106 continue;
107 }
108 vip = transaction->get_address(transaction);
109 vip = vip->clone(vip);
110 this->mutex->lock(this->mutex);
111 old = this->transactions->put(this->transactions,
112 (void*)hash_transaction(transaction), transaction);
113 this->mutex->unlock(this->mutex);
114 DESTROY_IF(old);
115 break;
116 }
117 enumerator->destroy(enumerator);
118 return vip;
119 }
120
121 METHOD(attribute_provider_t, release_address, bool,
122 private_dhcp_provider_t *this, linked_list_t *pools,
123 host_t *address, identification_t *id)
124 {
125 dhcp_transaction_t *transaction;
126 enumerator_t *enumerator;
127 bool found = FALSE;
128 char *pool;
129
130 if (address->get_family(address) != AF_INET)
131 {
132 return FALSE;
133 }
134 enumerator = pools->create_enumerator(pools);
135 while (enumerator->enumerate(enumerator, &pool))
136 {
137 if (!streq(pool, "dhcp"))
138 {
139 continue;
140 }
141 this->mutex->lock(this->mutex);
142 transaction = this->transactions->remove(this->transactions,
143 (void*)hash_id_host(id, address));
144 this->mutex->unlock(this->mutex);
145 if (transaction)
146 {
147 this->socket->release(this->socket, transaction);
148 transaction->destroy(transaction);
149 found = TRUE;
150 break;
151 }
152 }
153 enumerator->destroy(enumerator);
154 return found;
155 }
156
157 METHOD(attribute_provider_t, create_attribute_enumerator, enumerator_t*,
158 private_dhcp_provider_t *this, linked_list_t *pools, identification_t *id,
159 linked_list_t *vips)
160 {
161 dhcp_transaction_t *transaction = NULL;
162 enumerator_t *enumerator;
163 host_t *vip;
164
165 this->mutex->lock(this->mutex);
166 enumerator = vips->create_enumerator(vips);
167 while (enumerator->enumerate(enumerator, &vip))
168 {
169 transaction = this->transactions->get(this->transactions,
170 (void*)hash_id_host(id, vip));
171 if (transaction)
172 {
173 break;
174 }
175 }
176 enumerator->destroy(enumerator);
177 if (!transaction)
178 {
179 this->mutex->unlock(this->mutex);
180 return NULL;
181 }
182 return enumerator_create_cleaner(
183 transaction->create_attribute_enumerator(transaction),
184 (void*)this->mutex->unlock, this->mutex);
185 }
186
187 METHOD(dhcp_provider_t, destroy, void,
188 private_dhcp_provider_t *this)
189 {
190 enumerator_t *enumerator;
191 dhcp_transaction_t *value;
192 void *key;
193
194 enumerator = this->transactions->create_enumerator(this->transactions);
195 while (enumerator->enumerate(enumerator, &key, &value))
196 {
197 value->destroy(value);
198 }
199 enumerator->destroy(enumerator);
200 this->transactions->destroy(this->transactions);
201 this->mutex->destroy(this->mutex);
202 free(this);
203 }
204
205 /**
206 * See header
207 */
208 dhcp_provider_t *dhcp_provider_create(dhcp_socket_t *socket)
209 {
210 private_dhcp_provider_t *this;
211
212 INIT(this,
213 .public = {
214 .provider = {
215 .acquire_address = _acquire_address,
216 .release_address = _release_address,
217 .create_attribute_enumerator = _create_attribute_enumerator,
218 },
219 .destroy = _destroy,
220 },
221 .socket = socket,
222 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
223 .transactions = hashtable_create(hash, equals, 8),
224 );
225
226 return &this->public;
227 }
228