fixed format string argument error, resulting in crash
[strongswan.git] / src / charon / config / attributes / attribute_manager.c
1 /*
2 * Copyright (C) 2008 Martin Willi
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 * $Id$
16 */
17
18 #include "attribute_manager.h"
19
20 #include <daemon.h>
21 #include <utils/linked_list.h>
22 #include <utils/mutex.h>
23
24 typedef struct private_attribute_manager_t private_attribute_manager_t;
25
26 /**
27 * private data of attribute_manager
28 */
29 struct private_attribute_manager_t {
30
31 /**
32 * public functions
33 */
34 attribute_manager_t public;
35
36 /**
37 * list of registered providers
38 */
39 linked_list_t *providers;
40
41 /**
42 * rwlock provider list
43 */
44 rwlock_t *lock;
45 };
46
47 /**
48 * Implementation of attribute_manager_t.acquire_address.
49 */
50 static host_t* acquire_address(private_attribute_manager_t *this,
51 char *pool, identification_t *id,
52 auth_info_t *auth, host_t *requested)
53 {
54 enumerator_t *enumerator;
55 attribute_provider_t *current;
56 host_t *host = NULL;
57
58 this->lock->read_lock(this->lock);
59 enumerator = this->providers->create_enumerator(this->providers);
60 while (enumerator->enumerate(enumerator, &current))
61 {
62 host = current->acquire_address(current, pool, id, auth, requested);
63 if (host)
64 {
65 break;
66 }
67 }
68 enumerator->destroy(enumerator);
69 this->lock->unlock(this->lock);
70
71 if (!host)
72 {
73 DBG1(DBG_CFG, "acquiring address from pool '%s' failed", pool);
74 }
75 return host;
76 }
77
78 /**
79 * Implementation of attribute_manager_t.release_address.
80 */
81 static void release_address(private_attribute_manager_t *this,
82 char *pool, host_t *address, identification_t *id)
83 {
84 enumerator_t *enumerator;
85 attribute_provider_t *current;
86 bool found = FALSE;
87
88 this->lock->read_lock(this->lock);
89 enumerator = this->providers->create_enumerator(this->providers);
90 while (enumerator->enumerate(enumerator, &current))
91 {
92 if (current->release_address(current, pool, address, id))
93 {
94 found = TRUE;
95 break;
96 }
97 }
98 enumerator->destroy(enumerator);
99 this->lock->unlock(this->lock);
100
101 if (!found)
102 {
103 DBG1(DBG_CFG, "releasing address to pool '%s' failed", pool);
104 }
105 }
106
107 /**
108 * Implementation of attribute_manager_t.add_provider.
109 */
110 static void add_provider(private_attribute_manager_t *this,
111 attribute_provider_t *provider)
112 {
113 this->lock->write_lock(this->lock);
114 this->providers->insert_last(this->providers, provider);
115 this->lock->unlock(this->lock);
116 }
117
118 /**
119 * Implementation of attribute_manager_t.remove_provider.
120 */
121 static void remove_provider(private_attribute_manager_t *this,
122 attribute_provider_t *provider)
123 {
124 this->lock->write_lock(this->lock);
125 this->providers->remove(this->providers, provider, NULL);
126 this->lock->unlock(this->lock);
127 }
128
129 /**
130 * Implementation of attribute_manager_t.destroy
131 */
132 static void destroy(private_attribute_manager_t *this)
133 {
134 this->providers->destroy(this->providers);
135 this->lock->destroy(this->lock);
136 free(this);
137 }
138
139 /*
140 * see header file
141 */
142 attribute_manager_t *attribute_manager_create()
143 {
144 private_attribute_manager_t *this = malloc_thing(private_attribute_manager_t);
145
146 this->public.acquire_address = (host_t*(*)(attribute_manager_t*, char*, identification_t*,auth_info_t*,host_t*))acquire_address;
147 this->public.release_address = (void(*)(attribute_manager_t*, char *, host_t*, identification_t*))release_address;
148 this->public.add_provider = (void(*)(attribute_manager_t*, attribute_provider_t *provider))add_provider;
149 this->public.remove_provider = (void(*)(attribute_manager_t*, attribute_provider_t *provider))remove_provider;
150 this->public.destroy = (void(*)(attribute_manager_t*))destroy;
151
152 this->providers = linked_list_create();
153 this->lock = rwlock_create(RWLOCK_DEFAULT);
154
155 return &this->public;
156 }
157