]>
Commit | Line | Data |
---|---|---|
769ce76d AG |
1 | /* |
2 | * QEMU Block driver for CURL images | |
3 | * | |
4 | * Copyright (c) 2009 Alexander Graf <[email protected]> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
452fcdbc | 24 | |
80c71a24 | 25 | #include "qemu/osdep.h" |
da34e65c | 26 | #include "qapi/error.h" |
796a060b | 27 | #include "qemu/error-report.h" |
0b8fa32f | 28 | #include "qemu/module.h" |
922a01a0 | 29 | #include "qemu/option.h" |
737e150e | 30 | #include "block/block_int.h" |
452fcdbc | 31 | #include "qapi/qmp/qdict.h" |
d49b6836 | 32 | #include "qapi/qmp/qstring.h" |
1bff9606 | 33 | #include "crypto/secret.h" |
769ce76d | 34 | #include <curl/curl.h> |
f348b6d1 | 35 | #include "qemu/cutils.h" |
ed2a66de | 36 | #include "trace.h" |
769ce76d | 37 | |
769ce76d AG |
38 | // #define DEBUG_VERBOSE |
39 | ||
fb6d1bbd | 40 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
23dce387 | 41 | CURLPROTO_FTP | CURLPROTO_FTPS) |
fb6d1bbd | 42 | |
769ce76d AG |
43 | #define CURL_NUM_STATES 8 |
44 | #define CURL_NUM_ACB 8 | |
f76faeda | 45 | #define CURL_TIMEOUT_MAX 10000 |
769ce76d | 46 | |
e3542c67 MB |
47 | #define CURL_BLOCK_OPT_URL "url" |
48 | #define CURL_BLOCK_OPT_READAHEAD "readahead" | |
97a3ea57 | 49 | #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" |
212aefaa | 50 | #define CURL_BLOCK_OPT_TIMEOUT "timeout" |
a94f83d9 | 51 | #define CURL_BLOCK_OPT_COOKIE "cookie" |
327c8ebd | 52 | #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" |
1bff9606 DB |
53 | #define CURL_BLOCK_OPT_USERNAME "username" |
54 | #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" | |
55 | #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" | |
56 | #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" | |
e3542c67 | 57 | |
712b64e8 HR |
58 | #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024) |
59 | #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true | |
60 | #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5 | |
61 | ||
769ce76d | 62 | struct BDRVCURLState; |
04878616 | 63 | struct CURLState; |
769ce76d | 64 | |
2d25964d JC |
65 | static bool libcurl_initialized; |
66 | ||
769ce76d | 67 | typedef struct CURLAIOCB { |
28256d82 | 68 | Coroutine *co; |
769ce76d | 69 | QEMUIOVector *qiov; |
363c3c85 | 70 | |
2125e5ea PB |
71 | uint64_t offset; |
72 | uint64_t bytes; | |
28256d82 | 73 | int ret; |
363c3c85 | 74 | |
769ce76d AG |
75 | size_t start; |
76 | size_t end; | |
77 | } CURLAIOCB; | |
78 | ||
ff5ca166 HR |
79 | typedef struct CURLSocket { |
80 | int fd; | |
3663dca4 | 81 | struct BDRVCURLState *s; |
ff5ca166 HR |
82 | } CURLSocket; |
83 | ||
769ce76d AG |
84 | typedef struct CURLState |
85 | { | |
86 | struct BDRVCURLState *s; | |
87 | CURLAIOCB *acb[CURL_NUM_ACB]; | |
88 | CURL *curl; | |
89 | char *orig_buf; | |
2125e5ea | 90 | uint64_t buf_start; |
769ce76d AG |
91 | size_t buf_off; |
92 | size_t buf_len; | |
93 | char range[128]; | |
94 | char errmsg[CURL_ERROR_SIZE]; | |
95 | char in_use; | |
96 | } CURLState; | |
97 | ||
98 | typedef struct BDRVCURLState { | |
99 | CURLM *multi; | |
031fd1be | 100 | QEMUTimer timer; |
2125e5ea | 101 | uint64_t len; |
769ce76d | 102 | CURLState states[CURL_NUM_STATES]; |
0f418a20 | 103 | GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */ |
769ce76d | 104 | char *url; |
c76f4952 | 105 | size_t readahead_size; |
97a3ea57 | 106 | bool sslverify; |
f76faeda | 107 | uint64_t timeout; |
a94f83d9 | 108 | char *cookie; |
3494d650 | 109 | bool accept_range; |
63f0f45f | 110 | AioContext *aio_context; |
ba3186c4 | 111 | QemuMutex mutex; |
709f2132 | 112 | CoQueue free_state_waitq; |
1bff9606 DB |
113 | char *username; |
114 | char *password; | |
115 | char *proxyusername; | |
116 | char *proxypassword; | |
769ce76d AG |
117 | } BDRVCURLState; |
118 | ||
119 | static void curl_clean_state(CURLState *s); | |
120 | static void curl_multi_do(void *arg); | |
121 | ||
0f418a20 HR |
122 | static gboolean curl_drop_socket(void *key, void *value, void *opaque) |
123 | { | |
124 | CURLSocket *socket = value; | |
125 | BDRVCURLState *s = socket->s; | |
126 | ||
127 | aio_set_fd_handler(s->aio_context, socket->fd, false, | |
826cc324 | 128 | NULL, NULL, NULL, NULL, NULL); |
0f418a20 HR |
129 | return true; |
130 | } | |
131 | ||
132 | static void curl_drop_all_sockets(GHashTable *sockets) | |
133 | { | |
134 | g_hash_table_foreach_remove(sockets, curl_drop_socket, NULL); | |
135 | } | |
136 | ||
34db05e7 | 137 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
031fd1be PM |
138 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) |
139 | { | |
140 | BDRVCURLState *s = opaque; | |
141 | ||
ed2a66de | 142 | trace_curl_timer_cb(timeout_ms); |
031fd1be PM |
143 | if (timeout_ms == -1) { |
144 | timer_del(&s->timer); | |
145 | } else { | |
146 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; | |
147 | timer_mod(&s->timer, | |
148 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); | |
149 | } | |
150 | return 0; | |
151 | } | |
031fd1be | 152 | |
34db05e7 | 153 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
769ce76d | 154 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
63f0f45f | 155 | void *userp, void *sp) |
769ce76d | 156 | { |
63f0f45f | 157 | BDRVCURLState *s; |
838ef602 | 158 | CURLState *state = NULL; |
ff5ca166 HR |
159 | CURLSocket *socket; |
160 | ||
838ef602 | 161 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); |
63f0f45f | 162 | s = state->s; |
838ef602 | 163 | |
0f418a20 | 164 | socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd)); |
ff5ca166 HR |
165 | if (!socket) { |
166 | socket = g_new0(CURLSocket, 1); | |
167 | socket->fd = fd; | |
3663dca4 | 168 | socket->s = s; |
0f418a20 | 169 | g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket); |
ff5ca166 | 170 | } |
ff5ca166 | 171 | |
ed2a66de | 172 | trace_curl_sock_cb(action, (int)fd); |
769ce76d AG |
173 | switch (action) { |
174 | case CURL_POLL_IN: | |
dca21ef2 | 175 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 | 176 | curl_multi_do, NULL, NULL, NULL, socket); |
769ce76d AG |
177 | break; |
178 | case CURL_POLL_OUT: | |
dca21ef2 | 179 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 | 180 | NULL, curl_multi_do, NULL, NULL, socket); |
769ce76d AG |
181 | break; |
182 | case CURL_POLL_INOUT: | |
dca21ef2 | 183 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 SH |
184 | curl_multi_do, curl_multi_do, |
185 | NULL, NULL, socket); | |
769ce76d AG |
186 | break; |
187 | case CURL_POLL_REMOVE: | |
dca21ef2 | 188 | aio_set_fd_handler(s->aio_context, fd, false, |
826cc324 | 189 | NULL, NULL, NULL, NULL, NULL); |
769ce76d AG |
190 | break; |
191 | } | |
192 | ||
007f339b | 193 | if (action == CURL_POLL_REMOVE) { |
0f418a20 | 194 | g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd)); |
007f339b HR |
195 | } |
196 | ||
769ce76d AG |
197 | return 0; |
198 | } | |
199 | ||
34db05e7 | 200 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
3494d650 | 201 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
769ce76d | 202 | { |
3494d650 | 203 | BDRVCURLState *s = opaque; |
769ce76d | 204 | size_t realsize = size * nmemb; |
7788a319 DE |
205 | const char *header = (char *)ptr; |
206 | const char *end = header + realsize; | |
69032253 | 207 | const char *accept_ranges = "accept-ranges:"; |
7788a319 | 208 | const char *bytes = "bytes"; |
769ce76d | 209 | |
7788a319 | 210 | if (realsize >= strlen(accept_ranges) |
69032253 DE |
211 | && g_ascii_strncasecmp(header, accept_ranges, |
212 | strlen(accept_ranges)) == 0) { | |
7788a319 DE |
213 | |
214 | char *p = strchr(header, ':') + 1; | |
215 | ||
216 | /* Skip whitespace between the header name and value. */ | |
217 | while (p < end && *p && g_ascii_isspace(*p)) { | |
218 | p++; | |
219 | } | |
220 | ||
221 | if (end - p >= strlen(bytes) | |
222 | && strncmp(p, bytes, strlen(bytes)) == 0) { | |
223 | ||
224 | /* Check that there is nothing but whitespace after the value. */ | |
225 | p += strlen(bytes); | |
226 | while (p < end && *p && g_ascii_isspace(*p)) { | |
227 | p++; | |
228 | } | |
229 | ||
230 | if (p == end || !*p) { | |
231 | s->accept_range = true; | |
232 | } | |
233 | } | |
0bfcd599 | 234 | } |
769ce76d AG |
235 | |
236 | return realsize; | |
237 | } | |
238 | ||
34db05e7 | 239 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
769ce76d AG |
240 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
241 | { | |
242 | CURLState *s = ((CURLState*)opaque); | |
243 | size_t realsize = size * nmemb; | |
769ce76d | 244 | |
ed2a66de | 245 | trace_curl_read_cb(realsize); |
769ce76d | 246 | |
4e767657 HR |
247 | if (!s || !s->orig_buf) { |
248 | goto read_end; | |
249 | } | |
769ce76d | 250 | |
6d4b9e55 FZ |
251 | if (s->buf_off >= s->buf_len) { |
252 | /* buffer full, read nothing */ | |
4e767657 | 253 | goto read_end; |
6d4b9e55 FZ |
254 | } |
255 | realsize = MIN(realsize, s->buf_len - s->buf_off); | |
769ce76d AG |
256 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
257 | s->buf_off += realsize; | |
258 | ||
4e767657 HR |
259 | read_end: |
260 | /* curl will error out if we do not return this value */ | |
261 | return size * nmemb; | |
769ce76d AG |
262 | } |
263 | ||
456af346 | 264 | /* Called with s->mutex held. */ |
28256d82 PB |
265 | static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, |
266 | CURLAIOCB *acb) | |
769ce76d AG |
267 | { |
268 | int i; | |
2125e5ea PB |
269 | uint64_t end = start + len; |
270 | uint64_t clamped_end = MIN(end, s->len); | |
271 | uint64_t clamped_len = clamped_end - start; | |
769ce76d AG |
272 | |
273 | for (i=0; i<CURL_NUM_STATES; i++) { | |
274 | CURLState *state = &s->states[i]; | |
2125e5ea PB |
275 | uint64_t buf_end = (state->buf_start + state->buf_off); |
276 | uint64_t buf_fend = (state->buf_start + state->buf_len); | |
769ce76d AG |
277 | |
278 | if (!state->orig_buf) | |
279 | continue; | |
280 | if (!state->buf_off) | |
281 | continue; | |
282 | ||
283 | // Does the existing buffer cover our section? | |
284 | if ((start >= state->buf_start) && | |
285 | (start <= buf_end) && | |
4e504535 HR |
286 | (clamped_end >= state->buf_start) && |
287 | (clamped_end <= buf_end)) | |
769ce76d AG |
288 | { |
289 | char *buf = state->orig_buf + (start - state->buf_start); | |
290 | ||
4e504535 HR |
291 | qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); |
292 | if (clamped_len < len) { | |
293 | qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); | |
294 | } | |
28256d82 PB |
295 | acb->ret = 0; |
296 | return true; | |
769ce76d AG |
297 | } |
298 | ||
299 | // Wait for unfinished chunks | |
b7079df4 MB |
300 | if (state->in_use && |
301 | (start >= state->buf_start) && | |
769ce76d | 302 | (start <= buf_fend) && |
4e504535 HR |
303 | (clamped_end >= state->buf_start) && |
304 | (clamped_end <= buf_fend)) | |
769ce76d AG |
305 | { |
306 | int j; | |
307 | ||
308 | acb->start = start - state->buf_start; | |
4e504535 | 309 | acb->end = acb->start + clamped_len; |
769ce76d AG |
310 | |
311 | for (j=0; j<CURL_NUM_ACB; j++) { | |
312 | if (!state->acb[j]) { | |
313 | state->acb[j] = acb; | |
28256d82 | 314 | return true; |
769ce76d AG |
315 | } |
316 | } | |
317 | } | |
318 | } | |
319 | ||
28256d82 | 320 | return false; |
769ce76d AG |
321 | } |
322 | ||
ba3186c4 | 323 | /* Called with s->mutex held. */ |
838ef602 | 324 | static void curl_multi_check_completion(BDRVCURLState *s) |
769ce76d | 325 | { |
769ce76d AG |
326 | int msgs_in_queue; |
327 | ||
769ce76d AG |
328 | /* Try to find done transfers, so we can free the easy |
329 | * handle again. */ | |
1f2cead3 | 330 | for (;;) { |
769ce76d AG |
331 | CURLMsg *msg; |
332 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); | |
333 | ||
1f2cead3 | 334 | /* Quit when there are no more completions */ |
769ce76d AG |
335 | if (!msg) |
336 | break; | |
769ce76d | 337 | |
1f2cead3 | 338 | if (msg->msg == CURLMSG_DONE) { |
bfb23b48 | 339 | int i; |
1f2cead3 | 340 | CURLState *state = NULL; |
bfb23b48 HR |
341 | bool error = msg->data.result != CURLE_OK; |
342 | ||
1f2cead3 MB |
343 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, |
344 | (char **)&state); | |
345 | ||
bfb23b48 | 346 | if (error) { |
796a060b RJ |
347 | static int errcount = 100; |
348 | ||
349 | /* Don't lose the original error message from curl, since | |
350 | * it contains extra data. | |
351 | */ | |
352 | if (errcount > 0) { | |
353 | error_report("curl: %s", state->errmsg); | |
354 | if (--errcount == 0) { | |
355 | error_report("curl: further errors suppressed"); | |
356 | } | |
357 | } | |
bfb23b48 | 358 | } |
796a060b | 359 | |
bfb23b48 HR |
360 | for (i = 0; i < CURL_NUM_ACB; i++) { |
361 | CURLAIOCB *acb = state->acb[i]; | |
1f2cead3 | 362 | |
bfb23b48 HR |
363 | if (acb == NULL) { |
364 | continue; | |
365 | } | |
366 | ||
367 | if (!error) { | |
368 | /* Assert that we have read all data */ | |
369 | assert(state->buf_off >= acb->end); | |
370 | ||
371 | qemu_iovec_from_buf(acb->qiov, 0, | |
372 | state->orig_buf + acb->start, | |
373 | acb->end - acb->start); | |
f785a5ae | 374 | |
bfb23b48 HR |
375 | if (acb->end - acb->start < acb->bytes) { |
376 | size_t offset = acb->end - acb->start; | |
377 | qemu_iovec_memset(acb->qiov, offset, 0, | |
378 | acb->bytes - offset); | |
379 | } | |
1f2cead3 | 380 | } |
bfb23b48 HR |
381 | |
382 | acb->ret = error ? -EIO : 0; | |
383 | state->acb[i] = NULL; | |
384 | qemu_mutex_unlock(&s->mutex); | |
385 | aio_co_wake(acb->co); | |
386 | qemu_mutex_lock(&s->mutex); | |
769ce76d | 387 | } |
1f2cead3 MB |
388 | |
389 | curl_clean_state(state); | |
390 | break; | |
769ce76d | 391 | } |
1f2cead3 | 392 | } |
769ce76d AG |
393 | } |
394 | ||
ba3186c4 | 395 | /* Called with s->mutex held. */ |
9abaf9fc | 396 | static void curl_multi_do_locked(CURLSocket *socket) |
031fd1be | 397 | { |
3663dca4 | 398 | BDRVCURLState *s = socket->s; |
031fd1be PM |
399 | int running; |
400 | int r; | |
401 | ||
9abaf9fc | 402 | if (!s->multi) { |
031fd1be PM |
403 | return; |
404 | } | |
405 | ||
9abaf9fc HR |
406 | do { |
407 | r = curl_multi_socket_action(s->multi, socket->fd, 0, &running); | |
408 | } while (r == CURLM_CALL_MULTI_PERFORM); | |
838ef602 MB |
409 | } |
410 | ||
9d456654 PB |
411 | static void curl_multi_do(void *arg) |
412 | { | |
9dbad87d | 413 | CURLSocket *socket = arg; |
3663dca4 | 414 | BDRVCURLState *s = socket->s; |
9d456654 | 415 | |
9dbad87d HR |
416 | qemu_mutex_lock(&s->mutex); |
417 | curl_multi_do_locked(socket); | |
418 | curl_multi_check_completion(s); | |
419 | qemu_mutex_unlock(&s->mutex); | |
031fd1be PM |
420 | } |
421 | ||
422 | static void curl_multi_timeout_do(void *arg) | |
423 | { | |
031fd1be PM |
424 | BDRVCURLState *s = (BDRVCURLState *)arg; |
425 | int running; | |
426 | ||
427 | if (!s->multi) { | |
428 | return; | |
429 | } | |
430 | ||
ba3186c4 | 431 | qemu_mutex_lock(&s->mutex); |
031fd1be PM |
432 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
433 | ||
838ef602 | 434 | curl_multi_check_completion(s); |
ba3186c4 | 435 | qemu_mutex_unlock(&s->mutex); |
031fd1be PM |
436 | } |
437 | ||
456af346 | 438 | /* Called with s->mutex held. */ |
3ce6a729 | 439 | static CURLState *curl_find_state(BDRVCURLState *s) |
769ce76d AG |
440 | { |
441 | CURLState *state = NULL; | |
3ce6a729 | 442 | int i; |
769ce76d | 443 | |
3ce6a729 PB |
444 | for (i = 0; i < CURL_NUM_STATES; i++) { |
445 | if (!s->states[i].in_use) { | |
769ce76d AG |
446 | state = &s->states[i]; |
447 | state->in_use = 1; | |
448 | break; | |
449 | } | |
3ce6a729 PB |
450 | } |
451 | return state; | |
452 | } | |
769ce76d | 453 | |
3ce6a729 PB |
454 | static int curl_init_state(BDRVCURLState *s, CURLState *state) |
455 | { | |
9e550b32 MB |
456 | if (!state->curl) { |
457 | state->curl = curl_easy_init(); | |
458 | if (!state->curl) { | |
3ce6a729 | 459 | return -EIO; |
9e550b32 | 460 | } |
b0ea6c98 PM |
461 | if (curl_easy_setopt(state->curl, CURLOPT_URL, s->url) || |
462 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, | |
463 | (long) s->sslverify) || | |
464 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST, | |
465 | s->sslverify ? 2L : 0L)) { | |
466 | goto err; | |
467 | } | |
a94f83d9 | 468 | if (s->cookie) { |
b0ea6c98 PM |
469 | if (curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie)) { |
470 | goto err; | |
471 | } | |
472 | } | |
473 | if (curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout) || | |
474 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, | |
475 | (void *)curl_read_cb) || | |
476 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state) || | |
477 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state) || | |
478 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1) || | |
479 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1) || | |
480 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1) || | |
481 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg) || | |
482 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1)) { | |
483 | goto err; | |
a94f83d9 | 484 | } |
1bff9606 | 485 | if (s->username) { |
b0ea6c98 PM |
486 | if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) { |
487 | goto err; | |
488 | } | |
1bff9606 DB |
489 | } |
490 | if (s->password) { | |
b0ea6c98 PM |
491 | if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) { |
492 | goto err; | |
493 | } | |
1bff9606 DB |
494 | } |
495 | if (s->proxyusername) { | |
b0ea6c98 PM |
496 | if (curl_easy_setopt(state->curl, |
497 | CURLOPT_PROXYUSERNAME, s->proxyusername)) { | |
498 | goto err; | |
499 | } | |
1bff9606 DB |
500 | } |
501 | if (s->proxypassword) { | |
b0ea6c98 PM |
502 | if (curl_easy_setopt(state->curl, |
503 | CURLOPT_PROXYPASSWORD, s->proxypassword)) { | |
504 | goto err; | |
505 | } | |
1bff9606 DB |
506 | } |
507 | ||
9e550b32 MB |
508 | /* Restrict supported protocols to avoid security issues in the more |
509 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see | |
510 | * CVE-2013-0249. | |
511 | * | |
512 | * Restricting protocols is only supported from 7.19.4 upwards. | |
513 | */ | |
8a8f5840 | 514 | #if LIBCURL_VERSION_NUM >= 0x071304 |
b0ea6c98 PM |
515 | if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) || |
516 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) { | |
517 | goto err; | |
518 | } | |
8a8f5840 | 519 | #endif |
fb6d1bbd | 520 | |
769ce76d | 521 | #ifdef DEBUG_VERBOSE |
b0ea6c98 PM |
522 | if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) { |
523 | goto err; | |
524 | } | |
769ce76d | 525 | #endif |
9e550b32 | 526 | } |
769ce76d AG |
527 | |
528 | state->s = s; | |
529 | ||
3ce6a729 | 530 | return 0; |
b0ea6c98 PM |
531 | |
532 | err: | |
533 | curl_easy_cleanup(state->curl); | |
534 | state->curl = NULL; | |
535 | return -EIO; | |
769ce76d AG |
536 | } |
537 | ||
456af346 | 538 | /* Called with s->mutex held. */ |
769ce76d AG |
539 | static void curl_clean_state(CURLState *s) |
540 | { | |
675a7756 PB |
541 | int j; |
542 | for (j = 0; j < CURL_NUM_ACB; j++) { | |
543 | assert(!s->acb[j]); | |
544 | } | |
545 | ||
769ce76d AG |
546 | if (s->s->multi) |
547 | curl_multi_remove_handle(s->s->multi, s->curl); | |
ff5ca166 | 548 | |
769ce76d | 549 | s->in_use = 0; |
2bb5c936 | 550 | |
709f2132 | 551 | qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); |
769ce76d AG |
552 | } |
553 | ||
8e6d58cd KW |
554 | static void curl_parse_filename(const char *filename, QDict *options, |
555 | Error **errp) | |
769ce76d | 556 | { |
46f5ac20 | 557 | qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); |
8e6d58cd KW |
558 | } |
559 | ||
63f0f45f SH |
560 | static void curl_detach_aio_context(BlockDriverState *bs) |
561 | { | |
562 | BDRVCURLState *s = bs->opaque; | |
563 | int i; | |
564 | ||
f5056b70 | 565 | WITH_QEMU_LOCK_GUARD(&s->mutex) { |
0f418a20 | 566 | curl_drop_all_sockets(s->sockets); |
f5056b70 GQ |
567 | for (i = 0; i < CURL_NUM_STATES; i++) { |
568 | if (s->states[i].in_use) { | |
569 | curl_clean_state(&s->states[i]); | |
570 | } | |
571 | if (s->states[i].curl) { | |
572 | curl_easy_cleanup(s->states[i].curl); | |
573 | s->states[i].curl = NULL; | |
574 | } | |
575 | g_free(s->states[i].orig_buf); | |
576 | s->states[i].orig_buf = NULL; | |
63f0f45f | 577 | } |
f5056b70 GQ |
578 | if (s->multi) { |
579 | curl_multi_cleanup(s->multi); | |
580 | s->multi = NULL; | |
63f0f45f | 581 | } |
63f0f45f SH |
582 | } |
583 | ||
584 | timer_del(&s->timer); | |
585 | } | |
586 | ||
587 | static void curl_attach_aio_context(BlockDriverState *bs, | |
588 | AioContext *new_context) | |
589 | { | |
590 | BDRVCURLState *s = bs->opaque; | |
591 | ||
592 | aio_timer_init(new_context, &s->timer, | |
593 | QEMU_CLOCK_REALTIME, SCALE_NS, | |
594 | curl_multi_timeout_do, s); | |
595 | ||
596 | assert(!s->multi); | |
597 | s->multi = curl_multi_init(); | |
598 | s->aio_context = new_context; | |
599 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); | |
63f0f45f SH |
600 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); |
601 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); | |
63f0f45f SH |
602 | } |
603 | ||
8e6d58cd KW |
604 | static QemuOptsList runtime_opts = { |
605 | .name = "curl", | |
606 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
607 | .desc = { | |
608 | { | |
e3542c67 | 609 | .name = CURL_BLOCK_OPT_URL, |
8e6d58cd KW |
610 | .type = QEMU_OPT_STRING, |
611 | .help = "URL to open", | |
612 | }, | |
613 | { | |
e3542c67 | 614 | .name = CURL_BLOCK_OPT_READAHEAD, |
8e6d58cd KW |
615 | .type = QEMU_OPT_SIZE, |
616 | .help = "Readahead size", | |
617 | }, | |
97a3ea57 MB |
618 | { |
619 | .name = CURL_BLOCK_OPT_SSLVERIFY, | |
620 | .type = QEMU_OPT_BOOL, | |
621 | .help = "Verify SSL certificate" | |
622 | }, | |
212aefaa DHB |
623 | { |
624 | .name = CURL_BLOCK_OPT_TIMEOUT, | |
625 | .type = QEMU_OPT_NUMBER, | |
626 | .help = "Curl timeout" | |
627 | }, | |
a94f83d9 RJ |
628 | { |
629 | .name = CURL_BLOCK_OPT_COOKIE, | |
630 | .type = QEMU_OPT_STRING, | |
631 | .help = "Pass the cookie or list of cookies with each request" | |
632 | }, | |
327c8ebd PK |
633 | { |
634 | .name = CURL_BLOCK_OPT_COOKIE_SECRET, | |
635 | .type = QEMU_OPT_STRING, | |
636 | .help = "ID of secret used as cookie passed with each request" | |
637 | }, | |
1bff9606 DB |
638 | { |
639 | .name = CURL_BLOCK_OPT_USERNAME, | |
640 | .type = QEMU_OPT_STRING, | |
641 | .help = "Username for HTTP auth" | |
642 | }, | |
643 | { | |
644 | .name = CURL_BLOCK_OPT_PASSWORD_SECRET, | |
645 | .type = QEMU_OPT_STRING, | |
646 | .help = "ID of secret used as password for HTTP auth", | |
647 | }, | |
648 | { | |
649 | .name = CURL_BLOCK_OPT_PROXY_USERNAME, | |
650 | .type = QEMU_OPT_STRING, | |
651 | .help = "Username for HTTP proxy auth" | |
652 | }, | |
653 | { | |
654 | .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, | |
655 | .type = QEMU_OPT_STRING, | |
656 | .help = "ID of secret used as password for HTTP proxy auth", | |
657 | }, | |
8e6d58cd KW |
658 | { /* end of list */ } |
659 | }, | |
660 | }; | |
661 | ||
1bff9606 | 662 | |
015a1036 HR |
663 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
664 | Error **errp) | |
8e6d58cd KW |
665 | { |
666 | BDRVCURLState *s = bs->opaque; | |
667 | CURLState *state = NULL; | |
668 | QemuOpts *opts; | |
8e6d58cd | 669 | const char *file; |
a94f83d9 | 670 | const char *cookie; |
327c8ebd | 671 | const char *cookie_secret; |
8e6d58cd | 672 | double d; |
1bff9606 | 673 | const char *secretid; |
34634ca2 | 674 | const char *protocol_delimiter; |
2d25964d | 675 | int ret; |
8e6d58cd | 676 | |
6ceef36a KW |
677 | ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes", |
678 | errp); | |
679 | if (ret < 0) { | |
680 | return ret; | |
a7cea2ba RJ |
681 | } |
682 | ||
2d25964d JC |
683 | if (!libcurl_initialized) { |
684 | ret = curl_global_init(CURL_GLOBAL_ALL); | |
685 | if (ret) { | |
686 | error_setg(errp, "libcurl initialization failed with %d", ret); | |
687 | return -EIO; | |
688 | } | |
689 | libcurl_initialized = true; | |
690 | } | |
691 | ||
456af346 | 692 | qemu_mutex_init(&s->mutex); |
87ea75d5 | 693 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
668f62ec | 694 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
8e6d58cd KW |
695 | goto out_noclean; |
696 | } | |
697 | ||
e3542c67 | 698 | s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, |
712b64e8 | 699 | CURL_BLOCK_OPT_READAHEAD_DEFAULT); |
c76f4952 | 700 | if ((s->readahead_size & 0x1ff) != 0) { |
2a94fee3 PB |
701 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
702 | s->readahead_size); | |
c76f4952 N |
703 | goto out_noclean; |
704 | } | |
705 | ||
212aefaa | 706 | s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, |
712b64e8 | 707 | CURL_BLOCK_OPT_TIMEOUT_DEFAULT); |
f76faeda RJ |
708 | if (s->timeout > CURL_TIMEOUT_MAX) { |
709 | error_setg(errp, "timeout parameter is too large or negative"); | |
710 | goto out_noclean; | |
711 | } | |
212aefaa | 712 | |
712b64e8 HR |
713 | s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, |
714 | CURL_BLOCK_OPT_SSLVERIFY_DEFAULT); | |
97a3ea57 | 715 | |
a94f83d9 | 716 | cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); |
327c8ebd PK |
717 | cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); |
718 | ||
719 | if (cookie && cookie_secret) { | |
720 | error_setg(errp, | |
721 | "curl driver cannot handle both cookie and cookie secret"); | |
722 | goto out_noclean; | |
723 | } | |
724 | ||
725 | if (cookie_secret) { | |
726 | s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); | |
727 | if (!s->cookie) { | |
728 | goto out_noclean; | |
729 | } | |
730 | } else { | |
731 | s->cookie = g_strdup(cookie); | |
732 | } | |
a94f83d9 | 733 | |
e3542c67 | 734 | file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); |
8e6d58cd | 735 | if (file == NULL) { |
2a94fee3 | 736 | error_setg(errp, "curl block driver requires an 'url' option"); |
8e6d58cd KW |
737 | goto out_noclean; |
738 | } | |
739 | ||
34634ca2 HR |
740 | if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || |
741 | !strstart(protocol_delimiter, "://", NULL)) | |
742 | { | |
743 | error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " | |
744 | "start with '%s://')", bs->drv->protocol_name, file, | |
745 | bs->drv->protocol_name); | |
746 | goto out_noclean; | |
747 | } | |
748 | ||
1bff9606 DB |
749 | s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); |
750 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); | |
751 | ||
752 | if (secretid) { | |
753 | s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
754 | if (!s->password) { | |
755 | goto out_noclean; | |
756 | } | |
757 | } | |
758 | ||
759 | s->proxyusername = g_strdup( | |
760 | qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); | |
761 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); | |
762 | if (secretid) { | |
763 | s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); | |
764 | if (!s->proxypassword) { | |
765 | goto out_noclean; | |
766 | } | |
767 | } | |
768 | ||
ed2a66de | 769 | trace_curl_open(file); |
709f2132 | 770 | qemu_co_queue_init(&s->free_state_waitq); |
63f0f45f | 771 | s->aio_context = bdrv_get_aio_context(bs); |
8e6d58cd | 772 | s->url = g_strdup(file); |
0f418a20 | 773 | s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free); |
456af346 | 774 | qemu_mutex_lock(&s->mutex); |
3ce6a729 | 775 | state = curl_find_state(s); |
456af346 | 776 | qemu_mutex_unlock(&s->mutex); |
3ce6a729 | 777 | if (!state) { |
769ce76d | 778 | goto out_noclean; |
3ce6a729 | 779 | } |
769ce76d AG |
780 | |
781 | // Get file size | |
782 | ||
3ce6a729 | 783 | if (curl_init_state(s, state) < 0) { |
2ea7dfcd PM |
784 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
785 | "curl library initialization failed."); | |
3ce6a729 PB |
786 | goto out; |
787 | } | |
788 | ||
3494d650 | 789 | s->accept_range = false; |
b0ea6c98 PM |
790 | if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) || |
791 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) || | |
792 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) { | |
793 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
794 | "curl library initialization failed."); | |
795 | goto out; | |
796 | } | |
769ce76d AG |
797 | if (curl_easy_perform(state->curl)) |
798 | goto out; | |
a41c4578 | 799 | if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { |
769ce76d | 800 | goto out; |
a41c4578 TG |
801 | } |
802 | /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not | |
803 | * know or the size is zero. From 7.19.4 CURL returns -1 if size is not | |
50d6a8a3 | 804 | * known and zero if it is really zero-length file. */ |
a41c4578 TG |
805 | #if LIBCURL_VERSION_NUM >= 0x071304 |
806 | if (d < 0) { | |
807 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
808 | "Server didn't report file size."); | |
809 | goto out; | |
810 | } | |
811 | #else | |
812 | if (d <= 0) { | |
813 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
814 | "Unknown file size or zero-length file."); | |
815 | goto out; | |
816 | } | |
817 | #endif | |
818 | ||
2125e5ea | 819 | s->len = d; |
a41c4578 | 820 | |
3494d650 FZ |
821 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
822 | || !strncasecmp(s->url, "https://", strlen("https://"))) | |
823 | && !s->accept_range) { | |
824 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, | |
825 | "Server does not support 'range' (byte ranges)."); | |
826 | goto out; | |
827 | } | |
ed2a66de | 828 | trace_curl_open_size(s->len); |
769ce76d | 829 | |
456af346 | 830 | qemu_mutex_lock(&s->mutex); |
769ce76d | 831 | curl_clean_state(state); |
456af346 | 832 | qemu_mutex_unlock(&s->mutex); |
769ce76d AG |
833 | curl_easy_cleanup(state->curl); |
834 | state->curl = NULL; | |
835 | ||
63f0f45f | 836 | curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
769ce76d | 837 | |
8e6d58cd | 838 | qemu_opts_del(opts); |
769ce76d AG |
839 | return 0; |
840 | ||
841 | out: | |
acd7fdc6 | 842 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
769ce76d AG |
843 | curl_easy_cleanup(state->curl); |
844 | state->curl = NULL; | |
845 | out_noclean: | |
456af346 | 846 | qemu_mutex_destroy(&s->mutex); |
a94f83d9 | 847 | g_free(s->cookie); |
8e6d58cd | 848 | g_free(s->url); |
996922de JC |
849 | g_free(s->username); |
850 | g_free(s->proxyusername); | |
851 | g_free(s->proxypassword); | |
0f418a20 HR |
852 | curl_drop_all_sockets(s->sockets); |
853 | g_hash_table_destroy(s->sockets); | |
8e6d58cd | 854 | qemu_opts_del(opts); |
769ce76d AG |
855 | return -EINVAL; |
856 | } | |
857 | ||
28256d82 | 858 | static void curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) |
769ce76d | 859 | { |
769ce76d | 860 | CURLState *state; |
b69cdef8 | 861 | int running; |
769ce76d | 862 | |
1919631e | 863 | BDRVCURLState *s = bs->opaque; |
769ce76d | 864 | |
2125e5ea PB |
865 | uint64_t start = acb->offset; |
866 | uint64_t end; | |
769ce76d | 867 | |
ba3186c4 | 868 | qemu_mutex_lock(&s->mutex); |
1919631e | 869 | |
769ce76d AG |
870 | // In case we have the requested data already (e.g. read-ahead), |
871 | // we can just call the callback and be done. | |
28256d82 PB |
872 | if (curl_find_buf(s, start, acb->bytes, acb)) { |
873 | goto out; | |
769ce76d AG |
874 | } |
875 | ||
876 | // No cache found, so let's start a new request | |
3ce6a729 PB |
877 | for (;;) { |
878 | state = curl_find_state(s); | |
879 | if (state) { | |
880 | break; | |
881 | } | |
709f2132 | 882 | qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); |
3ce6a729 PB |
883 | } |
884 | ||
885 | if (curl_init_state(s, state) < 0) { | |
886 | curl_clean_state(state); | |
28256d82 | 887 | acb->ret = -EIO; |
1919631e | 888 | goto out; |
363c3c85 | 889 | } |
769ce76d AG |
890 | |
891 | acb->start = 0; | |
2125e5ea | 892 | acb->end = MIN(acb->bytes, s->len - start); |
769ce76d AG |
893 | |
894 | state->buf_off = 0; | |
f7047c2d | 895 | g_free(state->orig_buf); |
769ce76d | 896 | state->buf_start = start; |
4e504535 HR |
897 | state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); |
898 | end = start + state->buf_len - 1; | |
8dc7a772 KW |
899 | state->orig_buf = g_try_malloc(state->buf_len); |
900 | if (state->buf_len && state->orig_buf == NULL) { | |
901 | curl_clean_state(state); | |
28256d82 | 902 | acb->ret = -ENOMEM; |
1919631e | 903 | goto out; |
8dc7a772 | 904 | } |
769ce76d AG |
905 | state->acb[0] = acb; |
906 | ||
2125e5ea | 907 | snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); |
ed2a66de | 908 | trace_curl_setup_preadv(acb->bytes, start, state->range); |
b0ea6c98 PM |
909 | if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) || |
910 | curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) { | |
c34dc07f HR |
911 | state->acb[0] = NULL; |
912 | acb->ret = -EIO; | |
913 | ||
914 | curl_clean_state(state); | |
915 | goto out; | |
916 | } | |
769ce76d | 917 | |
b69cdef8 MB |
918 | /* Tell curl it needs to kick things off */ |
919 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); | |
1919631e PB |
920 | |
921 | out: | |
ba3186c4 | 922 | qemu_mutex_unlock(&s->mutex); |
363c3c85 NT |
923 | } |
924 | ||
28256d82 | 925 | static int coroutine_fn curl_co_preadv(BlockDriverState *bs, |
f7ef38dd VSO |
926 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, |
927 | BdrvRequestFlags flags) | |
363c3c85 | 928 | { |
28256d82 PB |
929 | CURLAIOCB acb = { |
930 | .co = qemu_coroutine_self(), | |
931 | .ret = -EINPROGRESS, | |
932 | .qiov = qiov, | |
933 | .offset = offset, | |
934 | .bytes = bytes | |
935 | }; | |
936 | ||
937 | curl_setup_preadv(bs, &acb); | |
938 | while (acb.ret == -EINPROGRESS) { | |
939 | qemu_coroutine_yield(); | |
940 | } | |
941 | return acb.ret; | |
769ce76d AG |
942 | } |
943 | ||
769ce76d AG |
944 | static void curl_close(BlockDriverState *bs) |
945 | { | |
946 | BDRVCURLState *s = bs->opaque; | |
769ce76d | 947 | |
ed2a66de | 948 | trace_curl_close(); |
63f0f45f | 949 | curl_detach_aio_context(bs); |
ba3186c4 | 950 | qemu_mutex_destroy(&s->mutex); |
031fd1be | 951 | |
0f418a20 | 952 | g_hash_table_destroy(s->sockets); |
a94f83d9 | 953 | g_free(s->cookie); |
45724d6d | 954 | g_free(s->url); |
996922de JC |
955 | g_free(s->username); |
956 | g_free(s->proxyusername); | |
957 | g_free(s->proxypassword); | |
769ce76d AG |
958 | } |
959 | ||
960 | static int64_t curl_getlength(BlockDriverState *bs) | |
961 | { | |
962 | BDRVCURLState *s = bs->opaque; | |
963 | return s->len; | |
964 | } | |
965 | ||
937c007b HR |
966 | static void curl_refresh_filename(BlockDriverState *bs) |
967 | { | |
968 | BDRVCURLState *s = bs->opaque; | |
969 | ||
970 | /* "readahead" and "timeout" do not change the guest-visible data, | |
971 | * so ignore them */ | |
972 | if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT || | |
973 | s->cookie || s->username || s->password || s->proxyusername || | |
974 | s->proxypassword) | |
975 | { | |
976 | return; | |
977 | } | |
978 | ||
979 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url); | |
980 | } | |
981 | ||
982 | ||
2654267c HR |
983 | static const char *const curl_strong_runtime_opts[] = { |
984 | CURL_BLOCK_OPT_URL, | |
985 | CURL_BLOCK_OPT_SSLVERIFY, | |
986 | CURL_BLOCK_OPT_COOKIE, | |
987 | CURL_BLOCK_OPT_COOKIE_SECRET, | |
988 | CURL_BLOCK_OPT_USERNAME, | |
989 | CURL_BLOCK_OPT_PASSWORD_SECRET, | |
990 | CURL_BLOCK_OPT_PROXY_USERNAME, | |
991 | CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, | |
992 | ||
993 | NULL | |
994 | }; | |
995 | ||
769ce76d | 996 | static BlockDriver bdrv_http = { |
63f0f45f SH |
997 | .format_name = "http", |
998 | .protocol_name = "http", | |
999 | ||
1000 | .instance_size = sizeof(BDRVCURLState), | |
1001 | .bdrv_parse_filename = curl_parse_filename, | |
1002 | .bdrv_file_open = curl_open, | |
1003 | .bdrv_close = curl_close, | |
1004 | .bdrv_getlength = curl_getlength, | |
769ce76d | 1005 | |
28256d82 | 1006 | .bdrv_co_preadv = curl_co_preadv, |
769ce76d | 1007 | |
63f0f45f SH |
1008 | .bdrv_detach_aio_context = curl_detach_aio_context, |
1009 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1010 | |
937c007b | 1011 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1012 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1013 | }; |
1014 | ||
1015 | static BlockDriver bdrv_https = { | |
63f0f45f SH |
1016 | .format_name = "https", |
1017 | .protocol_name = "https", | |
769ce76d | 1018 | |
63f0f45f SH |
1019 | .instance_size = sizeof(BDRVCURLState), |
1020 | .bdrv_parse_filename = curl_parse_filename, | |
1021 | .bdrv_file_open = curl_open, | |
1022 | .bdrv_close = curl_close, | |
1023 | .bdrv_getlength = curl_getlength, | |
769ce76d | 1024 | |
28256d82 | 1025 | .bdrv_co_preadv = curl_co_preadv, |
63f0f45f SH |
1026 | |
1027 | .bdrv_detach_aio_context = curl_detach_aio_context, | |
1028 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1029 | |
937c007b | 1030 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1031 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1032 | }; |
1033 | ||
1034 | static BlockDriver bdrv_ftp = { | |
63f0f45f SH |
1035 | .format_name = "ftp", |
1036 | .protocol_name = "ftp", | |
1037 | ||
1038 | .instance_size = sizeof(BDRVCURLState), | |
1039 | .bdrv_parse_filename = curl_parse_filename, | |
1040 | .bdrv_file_open = curl_open, | |
1041 | .bdrv_close = curl_close, | |
1042 | .bdrv_getlength = curl_getlength, | |
769ce76d | 1043 | |
28256d82 | 1044 | .bdrv_co_preadv = curl_co_preadv, |
769ce76d | 1045 | |
63f0f45f SH |
1046 | .bdrv_detach_aio_context = curl_detach_aio_context, |
1047 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1048 | |
937c007b | 1049 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1050 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1051 | }; |
1052 | ||
1053 | static BlockDriver bdrv_ftps = { | |
63f0f45f SH |
1054 | .format_name = "ftps", |
1055 | .protocol_name = "ftps", | |
769ce76d | 1056 | |
63f0f45f SH |
1057 | .instance_size = sizeof(BDRVCURLState), |
1058 | .bdrv_parse_filename = curl_parse_filename, | |
1059 | .bdrv_file_open = curl_open, | |
1060 | .bdrv_close = curl_close, | |
1061 | .bdrv_getlength = curl_getlength, | |
769ce76d | 1062 | |
28256d82 | 1063 | .bdrv_co_preadv = curl_co_preadv, |
63f0f45f SH |
1064 | |
1065 | .bdrv_detach_aio_context = curl_detach_aio_context, | |
1066 | .bdrv_attach_aio_context = curl_attach_aio_context, | |
2654267c | 1067 | |
937c007b | 1068 | .bdrv_refresh_filename = curl_refresh_filename, |
2654267c | 1069 | .strong_runtime_opts = curl_strong_runtime_opts, |
769ce76d AG |
1070 | }; |
1071 | ||
769ce76d AG |
1072 | static void curl_block_init(void) |
1073 | { | |
1074 | bdrv_register(&bdrv_http); | |
1075 | bdrv_register(&bdrv_https); | |
1076 | bdrv_register(&bdrv_ftp); | |
1077 | bdrv_register(&bdrv_ftps); | |
769ce76d AG |
1078 | } |
1079 | ||
1080 | block_init(curl_block_init); |