e46cc4d90fc7fb6691c767a5bae79084eadeb6a4
[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 <utils/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, char *pool,
85 identification_t *id, host_t *requested)
86 {
87 if (streq(pool, "dhcp") && requested->get_family(requested) == AF_INET)
88 {
89 dhcp_transaction_t *transaction, *old;
90 host_t *vip;
91
92 transaction = this->socket->enroll(this->socket, id);
93 if (!transaction)
94 {
95 return NULL;
96 }
97 vip = transaction->get_address(transaction);
98 vip = vip->clone(vip);
99 this->mutex->lock(this->mutex);
100 old = this->transactions->put(this->transactions,
101 (void*)hash_transaction(transaction), transaction);
102 this->mutex->unlock(this->mutex);
103 DESTROY_IF(old);
104 return vip;
105 }
106 return NULL;
107 }
108
109 METHOD(attribute_provider_t, release_address, bool,
110 private_dhcp_provider_t *this, char *pool,
111 host_t *address, identification_t *id)
112 {
113 if (streq(pool, "dhcp") && address->get_family(address) == AF_INET)
114 {
115 dhcp_transaction_t *transaction;
116
117 this->mutex->lock(this->mutex);
118 transaction = this->transactions->remove(this->transactions,
119 (void*)hash_id_host(id, address));
120 this->mutex->unlock(this->mutex);
121 if (transaction)
122 {
123 this->socket->release(this->socket, transaction);
124 transaction->destroy(transaction);
125 return TRUE;
126 }
127 }
128 return FALSE;
129 }
130
131 METHOD(attribute_provider_t, create_attribute_enumerator, enumerator_t*,
132 private_dhcp_provider_t *this, linked_list_t *pools, identification_t *id,
133 linked_list_t *vips)
134 {
135 dhcp_transaction_t *transaction = NULL;
136 enumerator_t *enumerator;
137 host_t *vip;
138
139 this->mutex->lock(this->mutex);
140 enumerator = vips->create_enumerator(vips);
141 while (enumerator->enumerate(enumerator, &vip))
142 {
143 transaction = this->transactions->get(this->transactions,
144 (void*)hash_id_host(id, vip));
145 if (transaction)
146 {
147 break;
148 }
149 }
150 enumerator->destroy(enumerator);
151 if (!transaction)
152 {
153 this->mutex->unlock(this->mutex);
154 return NULL;
155 }
156 return enumerator_create_cleaner(
157 transaction->create_attribute_enumerator(transaction),
158 (void*)this->mutex->unlock, this->mutex);
159 }
160
161 METHOD(dhcp_provider_t, destroy, void,
162 private_dhcp_provider_t *this)
163 {
164 enumerator_t *enumerator;
165 dhcp_transaction_t *value;
166 void *key;
167
168 enumerator = this->transactions->create_enumerator(this->transactions);
169 while (enumerator->enumerate(enumerator, &key, &value))
170 {
171 value->destroy(value);
172 }
173 enumerator->destroy(enumerator);
174 this->transactions->destroy(this->transactions);
175 this->mutex->destroy(this->mutex);
176 free(this);
177 }
178
179 /**
180 * See header
181 */
182 dhcp_provider_t *dhcp_provider_create(dhcp_socket_t *socket)
183 {
184 private_dhcp_provider_t *this;
185
186 INIT(this,
187 .public = {
188 .provider = {
189 .acquire_address = _acquire_address,
190 .release_address = _release_address,
191 .create_attribute_enumerator = _create_attribute_enumerator,
192 },
193 .destroy = _destroy,
194 },
195 .socket = socket,
196 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
197 .transactions = hashtable_create(hash, equals, 8),
198 );
199
200 return &this->public;
201 }
202