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