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