1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
* Copyright (C) 2010 Martin Willi
* Copyright (C) 2010 revosec AG
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "dhcp_provider.h"
#include <utils/hashtable.h>
#include <threading/mutex.h>
typedef struct private_dhcp_provider_t private_dhcp_provider_t;
/**
* Private data of an dhcp_provider_t object.
*/
struct private_dhcp_provider_t {
/**
* Public dhcp_provider_t interface.
*/
dhcp_provider_t public;
/**
* Completed DHCP transactions
*/
hashtable_t *transactions;
/**
* Lock for transactions
*/
mutex_t *mutex;
/**
* DHCP communication socket
*/
dhcp_socket_t *socket;
};
/**
* Hashtable hash function
*/
static u_int hash(void *key)
{
return (uintptr_t)key;
}
/**
* Hashtable equals function
*/
static bool equals(void *a, void *b)
{
return a == b;
}
/**
* Hash ID and host to a key
*/
static uintptr_t hash_id_host(identification_t *id, host_t *host)
{
return chunk_hash_inc(id->get_encoding(id),
chunk_hash(host->get_address(host)));
}
/**
* Hash a DHCP transaction to a key, using address and id
*/
static uintptr_t hash_transaction(dhcp_transaction_t *transaction)
{
return hash_id_host(transaction->get_identity(transaction),
transaction->get_address(transaction));
}
METHOD(attribute_provider_t, acquire_address, host_t*,
private_dhcp_provider_t *this, char *pool,
identification_t *id, host_t *requested)
{
if (streq(pool, "dhcp"))
{
dhcp_transaction_t *transaction, *old;
host_t *vip;
transaction = this->socket->enroll(this->socket, id);
if (!transaction)
{
return NULL;
}
vip = transaction->get_address(transaction);
vip = vip->clone(vip);
this->mutex->lock(this->mutex);
old = this->transactions->put(this->transactions,
(void*)hash_transaction(transaction), transaction);
this->mutex->unlock(this->mutex);
DESTROY_IF(old);
return vip;
}
return NULL;
}
METHOD(attribute_provider_t, release_address, bool,
private_dhcp_provider_t *this, char *pool,
host_t *address, identification_t *id)
{
if (streq(pool, "dhcp"))
{
dhcp_transaction_t *transaction;
this->mutex->lock(this->mutex);
transaction = this->transactions->remove(this->transactions,
(void*)hash_id_host(id, address));
this->mutex->unlock(this->mutex);
if (transaction)
{
this->socket->release(this->socket, transaction);
transaction->destroy(transaction);
return TRUE;
}
}
return FALSE;
}
METHOD(attribute_provider_t, create_attribute_enumerator, enumerator_t*,
private_dhcp_provider_t *this, char *pool, identification_t *id,
host_t *vip)
{
dhcp_transaction_t *transaction;
if (!vip)
{
return NULL;
}
this->mutex->lock(this->mutex);
transaction = this->transactions->get(this->transactions,
(void*)hash_id_host(id, vip));
if (!transaction)
{
this->mutex->unlock(this->mutex);
return NULL;
}
return enumerator_create_cleaner(
transaction->create_attribute_enumerator(transaction),
(void*)this->mutex->unlock, this->mutex);
}
METHOD(dhcp_provider_t, destroy, void,
private_dhcp_provider_t *this)
{
enumerator_t *enumerator;
dhcp_transaction_t *value;
void *key;
enumerator = this->transactions->create_enumerator(this->transactions);
while (enumerator->enumerate(enumerator, &key, &value))
{
value->destroy(value);
}
enumerator->destroy(enumerator);
this->transactions->destroy(this->transactions);
this->mutex->destroy(this->mutex);
free(this);
}
/**
* See header
*/
dhcp_provider_t *dhcp_provider_create(dhcp_socket_t *socket)
{
private_dhcp_provider_t *this;
INIT(this,
.public = {
.provider = {
.acquire_address = _acquire_address,
.release_address = _release_address,
.create_attribute_enumerator = _create_attribute_enumerator,
},
.destroy = _destroy,
},
.socket = socket,
.mutex = mutex_create(MUTEX_TYPE_DEFAULT),
.transactions = hashtable_create(hash, equals, 8),
);
return &this->public;
}
|