Merge branch 'scep-bind'
[strongswan.git] / src / libstrongswan / fetcher / fetcher_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
16 #include "fetcher_manager.h"
17
18 #include <utils/debug.h>
19 #include <threading/rwlock.h>
20 #include <collections/linked_list.h>
21
22 typedef struct private_fetcher_manager_t private_fetcher_manager_t;
23
24 /**
25 * private data of fetcher_manager
26 */
27 struct private_fetcher_manager_t {
28
29 /**
30 * public functions
31 */
32 fetcher_manager_t public;
33
34 /**
35 * list of registered fetchers, as entry_t
36 */
37 linked_list_t *fetchers;
38
39 /**
40 * read write lock to list
41 */
42 rwlock_t *lock;
43 };
44
45 typedef struct {
46 /** assocaited fetcher construction function */
47 fetcher_constructor_t create;
48 /** URL this fetcher support */
49 char *url;
50 } entry_t;
51
52 /**
53 * destroy an entry_t
54 */
55 static void entry_destroy(entry_t *entry)
56 {
57 free(entry->url);
58 free(entry);
59 }
60
61 METHOD(fetcher_manager_t, fetch, status_t,
62 private_fetcher_manager_t *this, char *url, void *userdata, ...)
63 {
64 enumerator_t *enumerator;
65 status_t status = NOT_SUPPORTED;
66 entry_t *entry;
67 bool capable = FALSE;
68
69 this->lock->read_lock(this->lock);
70 enumerator = this->fetchers->create_enumerator(this->fetchers);
71 while (enumerator->enumerate(enumerator, &entry))
72 {
73 fetcher_option_t opt;
74 fetcher_t *fetcher;
75 bool good = TRUE;
76 host_t *host;
77 va_list args;
78
79 /* check URL support of fetcher */
80 if (strncasecmp(entry->url, url, strlen(entry->url)))
81 {
82 continue;
83 }
84 /* create fetcher instance and set options */
85 fetcher = entry->create();
86 if (!fetcher)
87 {
88 continue;
89 }
90 va_start(args, userdata);
91 while (good)
92 {
93 opt = va_arg(args, int);
94 switch (opt)
95 {
96 case FETCH_REQUEST_DATA:
97 good = fetcher->set_option(fetcher, opt,
98 va_arg(args, chunk_t));
99 continue;
100 case FETCH_REQUEST_TYPE:
101 case FETCH_REQUEST_HEADER:
102 good = fetcher->set_option(fetcher, opt,
103 va_arg(args, char*));
104 continue;
105 case FETCH_HTTP_VERSION_1_0:
106 good = fetcher->set_option(fetcher, opt);
107 continue;
108 case FETCH_TIMEOUT:
109 good = fetcher->set_option(fetcher, opt,
110 va_arg(args, u_int));
111 continue;
112 case FETCH_CALLBACK:
113 good = fetcher->set_option(fetcher, opt,
114 va_arg(args, fetcher_callback_t));
115 continue;
116 case FETCH_SOURCEIP:
117 host = va_arg(args, host_t*);
118 if (host && !host->is_anyaddr(host))
119 {
120 good = fetcher->set_option(fetcher, opt, host);
121 }
122 continue;
123 case FETCH_END:
124 break;
125 }
126 break;
127 }
128 va_end(args);
129 if (!good)
130 { /* fetcher does not support supplied options, try another */
131 fetcher->destroy(fetcher);
132 continue;
133 }
134
135 status = fetcher->fetch(fetcher, url, userdata);
136 fetcher->destroy(fetcher);
137 /* try another fetcher only if this one does not support that URL */
138 if (status == NOT_SUPPORTED)
139 {
140 continue;
141 }
142 capable = TRUE;
143 break;
144 }
145 enumerator->destroy(enumerator);
146 this->lock->unlock(this->lock);
147 if (!capable)
148 {
149 DBG1(DBG_LIB, "unable to fetch from %s, no capable fetcher found", url);
150 }
151 return status;
152 }
153
154 METHOD(fetcher_manager_t, add_fetcher, void,
155 private_fetcher_manager_t *this, fetcher_constructor_t create, char *url)
156 {
157 entry_t *entry;
158
159 INIT(entry,
160 .url = strdup(url),
161 .create = create,
162 );
163 this->lock->write_lock(this->lock);
164 this->fetchers->insert_last(this->fetchers, entry);
165 this->lock->unlock(this->lock);
166 }
167
168 METHOD(fetcher_manager_t, remove_fetcher, void,
169 private_fetcher_manager_t *this, fetcher_constructor_t create)
170 {
171 enumerator_t *enumerator;
172 entry_t *entry;
173
174 this->lock->write_lock(this->lock);
175 enumerator = this->fetchers->create_enumerator(this->fetchers);
176 while (enumerator->enumerate(enumerator, &entry))
177 {
178 if (entry->create == create)
179 {
180 this->fetchers->remove_at(this->fetchers, enumerator);
181 entry_destroy(entry);
182 }
183 }
184 enumerator->destroy(enumerator);
185 this->lock->unlock(this->lock);
186 }
187
188 METHOD(fetcher_manager_t, destroy, void,
189 private_fetcher_manager_t *this)
190 {
191 this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
192 this->lock->destroy(this->lock);
193 free(this);
194 }
195
196 /*
197 * see header file
198 */
199 fetcher_manager_t *fetcher_manager_create()
200 {
201 private_fetcher_manager_t *this;
202
203 INIT(this,
204 .public = {
205 .fetch = _fetch,
206 .add_fetcher = _add_fetcher,
207 .remove_fetcher = _remove_fetcher,
208 .destroy = _destroy,
209 },
210 .fetchers = linked_list_create(),
211 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
212 );
213
214 return &this->public;
215 }