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