]> git.proxmox.com Git - mirror_qemu.git/blame - block/curl.c
block/curl: Drop TFTP "support"
[mirror_qemu.git] / block / curl.c
CommitLineData
769ce76d
AG
1/*
2 * QEMU Block driver for CURL images
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
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. */
60static 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
75#define SECTOR_SIZE 512
e3542c67 76#define READ_AHEAD_DEFAULT (256 * 1024)
212aefaa 77#define CURL_TIMEOUT_DEFAULT 5
f76faeda 78#define CURL_TIMEOUT_MAX 10000
769ce76d
AG
79
80#define FIND_RET_NONE 0
81#define FIND_RET_OK 1
82#define FIND_RET_WAIT 2
83
e3542c67
MB
84#define CURL_BLOCK_OPT_URL "url"
85#define CURL_BLOCK_OPT_READAHEAD "readahead"
97a3ea57 86#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
212aefaa 87#define CURL_BLOCK_OPT_TIMEOUT "timeout"
a94f83d9 88#define CURL_BLOCK_OPT_COOKIE "cookie"
1bff9606
DB
89#define CURL_BLOCK_OPT_USERNAME "username"
90#define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
91#define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
92#define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
e3542c67 93
769ce76d
AG
94struct BDRVCURLState;
95
96typedef struct CURLAIOCB {
7c84b1b8 97 BlockAIOCB common;
769ce76d 98 QEMUIOVector *qiov;
363c3c85
NT
99
100 int64_t sector_num;
101 int nb_sectors;
102
769ce76d
AG
103 size_t start;
104 size_t end;
105} CURLAIOCB;
106
107typedef struct CURLState
108{
109 struct BDRVCURLState *s;
110 CURLAIOCB *acb[CURL_NUM_ACB];
111 CURL *curl;
838ef602 112 curl_socket_t sock_fd;
769ce76d
AG
113 char *orig_buf;
114 size_t buf_start;
115 size_t buf_off;
116 size_t buf_len;
117 char range[128];
118 char errmsg[CURL_ERROR_SIZE];
119 char in_use;
120} CURLState;
121
122typedef struct BDRVCURLState {
123 CURLM *multi;
031fd1be 124 QEMUTimer timer;
769ce76d
AG
125 size_t len;
126 CURLState states[CURL_NUM_STATES];
127 char *url;
c76f4952 128 size_t readahead_size;
97a3ea57 129 bool sslverify;
f76faeda 130 uint64_t timeout;
a94f83d9 131 char *cookie;
3494d650 132 bool accept_range;
63f0f45f 133 AioContext *aio_context;
1bff9606
DB
134 char *username;
135 char *password;
136 char *proxyusername;
137 char *proxypassword;
769ce76d
AG
138} BDRVCURLState;
139
140static void curl_clean_state(CURLState *s);
141static void curl_multi_do(void *arg);
838ef602 142static void curl_multi_read(void *arg);
769ce76d 143
031fd1be
PM
144#ifdef NEED_CURL_TIMER_CALLBACK
145static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
146{
147 BDRVCURLState *s = opaque;
148
149 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
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
769ce76d 161static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
63f0f45f 162 void *userp, void *sp)
769ce76d 163{
63f0f45f 164 BDRVCURLState *s;
838ef602
MB
165 CURLState *state = NULL;
166 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
167 state->sock_fd = fd;
63f0f45f 168 s = state->s;
838ef602 169
92b6a160 170 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, (int)fd);
769ce76d
AG
171 switch (action) {
172 case CURL_POLL_IN:
dca21ef2
FZ
173 aio_set_fd_handler(s->aio_context, fd, false,
174 curl_multi_read, NULL, state);
769ce76d
AG
175 break;
176 case CURL_POLL_OUT:
dca21ef2
FZ
177 aio_set_fd_handler(s->aio_context, fd, false,
178 NULL, curl_multi_do, state);
769ce76d
AG
179 break;
180 case CURL_POLL_INOUT:
dca21ef2
FZ
181 aio_set_fd_handler(s->aio_context, fd, false,
182 curl_multi_read, curl_multi_do, state);
769ce76d
AG
183 break;
184 case CURL_POLL_REMOVE:
dca21ef2
FZ
185 aio_set_fd_handler(s->aio_context, fd, false,
186 NULL, NULL, NULL);
769ce76d
AG
187 break;
188 }
189
190 return 0;
191}
192
3494d650 193static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
769ce76d 194{
3494d650 195 BDRVCURLState *s = opaque;
769ce76d 196 size_t realsize = size * nmemb;
3494d650 197 const char *accept_line = "Accept-Ranges: bytes";
769ce76d 198
3494d650
FZ
199 if (realsize >= strlen(accept_line)
200 && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
201 s->accept_range = true;
0bfcd599 202 }
769ce76d
AG
203
204 return realsize;
205}
206
207static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
208{
209 CURLState *s = ((CURLState*)opaque);
210 size_t realsize = size * nmemb;
211 int i;
212
0bfcd599 213 DPRINTF("CURL: Just reading %zd bytes\n", realsize);
769ce76d
AG
214
215 if (!s || !s->orig_buf)
38bbc0a5 216 return 0;
769ce76d 217
6d4b9e55
FZ
218 if (s->buf_off >= s->buf_len) {
219 /* buffer full, read nothing */
220 return 0;
221 }
222 realsize = MIN(realsize, s->buf_len - s->buf_off);
769ce76d
AG
223 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
224 s->buf_off += realsize;
225
226 for(i=0; i<CURL_NUM_ACB; i++) {
227 CURLAIOCB *acb = s->acb[i];
228
229 if (!acb)
230 continue;
231
232 if ((s->buf_off >= acb->end)) {
03396148
MT
233 qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
234 acb->end - acb->start);
769ce76d 235 acb->common.cb(acb->common.opaque, 0);
8007429a 236 qemu_aio_unref(acb);
769ce76d
AG
237 s->acb[i] = NULL;
238 }
239 }
240
769ce76d
AG
241 return realsize;
242}
243
244static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
245 CURLAIOCB *acb)
246{
247 int i;
248 size_t end = start + len;
249
250 for (i=0; i<CURL_NUM_STATES; i++) {
251 CURLState *state = &s->states[i];
252 size_t buf_end = (state->buf_start + state->buf_off);
253 size_t buf_fend = (state->buf_start + state->buf_len);
254
255 if (!state->orig_buf)
256 continue;
257 if (!state->buf_off)
258 continue;
259
260 // Does the existing buffer cover our section?
261 if ((start >= state->buf_start) &&
262 (start <= buf_end) &&
263 (end >= state->buf_start) &&
264 (end <= buf_end))
265 {
266 char *buf = state->orig_buf + (start - state->buf_start);
267
03396148 268 qemu_iovec_from_buf(acb->qiov, 0, buf, len);
769ce76d
AG
269 acb->common.cb(acb->common.opaque, 0);
270
271 return FIND_RET_OK;
272 }
273
274 // Wait for unfinished chunks
b7079df4
MB
275 if (state->in_use &&
276 (start >= state->buf_start) &&
769ce76d
AG
277 (start <= buf_fend) &&
278 (end >= state->buf_start) &&
279 (end <= buf_fend))
280 {
281 int j;
282
283 acb->start = start - state->buf_start;
284 acb->end = acb->start + len;
285
286 for (j=0; j<CURL_NUM_ACB; j++) {
287 if (!state->acb[j]) {
288 state->acb[j] = acb;
289 return FIND_RET_WAIT;
290 }
291 }
292 }
293 }
294
295 return FIND_RET_NONE;
296}
297
838ef602 298static void curl_multi_check_completion(BDRVCURLState *s)
769ce76d 299{
769ce76d
AG
300 int msgs_in_queue;
301
769ce76d
AG
302 /* Try to find done transfers, so we can free the easy
303 * handle again. */
1f2cead3 304 for (;;) {
769ce76d
AG
305 CURLMsg *msg;
306 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
307
1f2cead3 308 /* Quit when there are no more completions */
769ce76d
AG
309 if (!msg)
310 break;
769ce76d 311
1f2cead3
MB
312 if (msg->msg == CURLMSG_DONE) {
313 CURLState *state = NULL;
314 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
315 (char **)&state);
316
317 /* ACBs for successful messages get completed in curl_read_cb */
318 if (msg->data.result != CURLE_OK) {
319 int i;
796a060b
RJ
320 static int errcount = 100;
321
322 /* Don't lose the original error message from curl, since
323 * it contains extra data.
324 */
325 if (errcount > 0) {
326 error_report("curl: %s", state->errmsg);
327 if (--errcount == 0) {
328 error_report("curl: further errors suppressed");
329 }
330 }
331
1f2cead3
MB
332 for (i = 0; i < CURL_NUM_ACB; i++) {
333 CURLAIOCB *acb = state->acb[i];
334
335 if (acb == NULL) {
336 continue;
f785a5ae 337 }
f785a5ae 338
796a060b 339 acb->common.cb(acb->common.opaque, -EPROTO);
8007429a 340 qemu_aio_unref(acb);
1f2cead3
MB
341 state->acb[i] = NULL;
342 }
769ce76d 343 }
1f2cead3
MB
344
345 curl_clean_state(state);
346 break;
769ce76d 347 }
1f2cead3 348 }
769ce76d
AG
349}
350
031fd1be
PM
351static void curl_multi_do(void *arg)
352{
838ef602 353 CURLState *s = (CURLState *)arg;
031fd1be
PM
354 int running;
355 int r;
356
838ef602 357 if (!s->s->multi) {
031fd1be
PM
358 return;
359 }
360
361 do {
838ef602 362 r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
031fd1be
PM
363 } while(r == CURLM_CALL_MULTI_PERFORM);
364
838ef602
MB
365}
366
367static void curl_multi_read(void *arg)
368{
369 CURLState *s = (CURLState *)arg;
370
371 curl_multi_do(arg);
372 curl_multi_check_completion(s->s);
031fd1be
PM
373}
374
375static void curl_multi_timeout_do(void *arg)
376{
377#ifdef NEED_CURL_TIMER_CALLBACK
378 BDRVCURLState *s = (BDRVCURLState *)arg;
379 int running;
380
381 if (!s->multi) {
382 return;
383 }
384
385 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
386
838ef602 387 curl_multi_check_completion(s);
031fd1be
PM
388#else
389 abort();
390#endif
391}
392
a2f468e4 393static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
769ce76d
AG
394{
395 CURLState *state = NULL;
396 int i, j;
397
398 do {
399 for (i=0; i<CURL_NUM_STATES; i++) {
400 for (j=0; j<CURL_NUM_ACB; j++)
401 if (s->states[i].acb[j])
402 continue;
403 if (s->states[i].in_use)
404 continue;
405
406 state = &s->states[i];
407 state->in_use = 1;
408 break;
409 }
410 if (!state) {
a2f468e4 411 aio_poll(bdrv_get_aio_context(bs), true);
769ce76d
AG
412 }
413 } while(!state);
414
9e550b32
MB
415 if (!state->curl) {
416 state->curl = curl_easy_init();
417 if (!state->curl) {
418 return NULL;
419 }
420 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
97a3ea57
MB
421 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
422 (long) s->sslverify);
a94f83d9
RJ
423 if (s->cookie) {
424 curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
425 }
f76faeda 426 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
9e550b32
MB
427 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
428 (void *)curl_read_cb);
429 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
430 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
431 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
432 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
433 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
434 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
435 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
436
1bff9606
DB
437 if (s->username) {
438 curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username);
439 }
440 if (s->password) {
441 curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password);
442 }
443 if (s->proxyusername) {
444 curl_easy_setopt(state->curl,
445 CURLOPT_PROXYUSERNAME, s->proxyusername);
446 }
447 if (s->proxypassword) {
448 curl_easy_setopt(state->curl,
449 CURLOPT_PROXYPASSWORD, s->proxypassword);
450 }
451
9e550b32
MB
452 /* Restrict supported protocols to avoid security issues in the more
453 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
454 * CVE-2013-0249.
455 *
456 * Restricting protocols is only supported from 7.19.4 upwards.
457 */
8a8f5840 458#if LIBCURL_VERSION_NUM >= 0x071304
9e550b32
MB
459 curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
460 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
8a8f5840 461#endif
fb6d1bbd 462
769ce76d 463#ifdef DEBUG_VERBOSE
9e550b32 464 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
769ce76d 465#endif
9e550b32 466 }
769ce76d
AG
467
468 state->s = s;
469
470 return state;
471}
472
473static void curl_clean_state(CURLState *s)
474{
475 if (s->s->multi)
476 curl_multi_remove_handle(s->s->multi, s->curl);
477 s->in_use = 0;
478}
479
8e6d58cd
KW
480static void curl_parse_filename(const char *filename, QDict *options,
481 Error **errp)
769ce76d 482{
e3542c67 483 qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
8e6d58cd
KW
484}
485
63f0f45f
SH
486static void curl_detach_aio_context(BlockDriverState *bs)
487{
488 BDRVCURLState *s = bs->opaque;
489 int i;
490
491 for (i = 0; i < CURL_NUM_STATES; i++) {
492 if (s->states[i].in_use) {
493 curl_clean_state(&s->states[i]);
494 }
495 if (s->states[i].curl) {
496 curl_easy_cleanup(s->states[i].curl);
497 s->states[i].curl = NULL;
498 }
f7047c2d
MA
499 g_free(s->states[i].orig_buf);
500 s->states[i].orig_buf = NULL;
63f0f45f
SH
501 }
502 if (s->multi) {
503 curl_multi_cleanup(s->multi);
504 s->multi = NULL;
505 }
506
507 timer_del(&s->timer);
508}
509
510static void curl_attach_aio_context(BlockDriverState *bs,
511 AioContext *new_context)
512{
513 BDRVCURLState *s = bs->opaque;
514
515 aio_timer_init(new_context, &s->timer,
516 QEMU_CLOCK_REALTIME, SCALE_NS,
517 curl_multi_timeout_do, s);
518
519 assert(!s->multi);
520 s->multi = curl_multi_init();
521 s->aio_context = new_context;
522 curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
523#ifdef NEED_CURL_TIMER_CALLBACK
524 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
525 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
526#endif
527}
528
8e6d58cd
KW
529static QemuOptsList runtime_opts = {
530 .name = "curl",
531 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
532 .desc = {
533 {
e3542c67 534 .name = CURL_BLOCK_OPT_URL,
8e6d58cd
KW
535 .type = QEMU_OPT_STRING,
536 .help = "URL to open",
537 },
538 {
e3542c67 539 .name = CURL_BLOCK_OPT_READAHEAD,
8e6d58cd
KW
540 .type = QEMU_OPT_SIZE,
541 .help = "Readahead size",
542 },
97a3ea57
MB
543 {
544 .name = CURL_BLOCK_OPT_SSLVERIFY,
545 .type = QEMU_OPT_BOOL,
546 .help = "Verify SSL certificate"
547 },
212aefaa
DHB
548 {
549 .name = CURL_BLOCK_OPT_TIMEOUT,
550 .type = QEMU_OPT_NUMBER,
551 .help = "Curl timeout"
552 },
a94f83d9
RJ
553 {
554 .name = CURL_BLOCK_OPT_COOKIE,
555 .type = QEMU_OPT_STRING,
556 .help = "Pass the cookie or list of cookies with each request"
557 },
1bff9606
DB
558 {
559 .name = CURL_BLOCK_OPT_USERNAME,
560 .type = QEMU_OPT_STRING,
561 .help = "Username for HTTP auth"
562 },
563 {
564 .name = CURL_BLOCK_OPT_PASSWORD_SECRET,
565 .type = QEMU_OPT_STRING,
566 .help = "ID of secret used as password for HTTP auth",
567 },
568 {
569 .name = CURL_BLOCK_OPT_PROXY_USERNAME,
570 .type = QEMU_OPT_STRING,
571 .help = "Username for HTTP proxy auth"
572 },
573 {
574 .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET,
575 .type = QEMU_OPT_STRING,
576 .help = "ID of secret used as password for HTTP proxy auth",
577 },
8e6d58cd
KW
578 { /* end of list */ }
579 },
580};
581
1bff9606 582
015a1036
HR
583static int curl_open(BlockDriverState *bs, QDict *options, int flags,
584 Error **errp)
8e6d58cd
KW
585{
586 BDRVCURLState *s = bs->opaque;
587 CURLState *state = NULL;
588 QemuOpts *opts;
589 Error *local_err = NULL;
590 const char *file;
a94f83d9 591 const char *cookie;
8e6d58cd 592 double d;
1bff9606 593 const char *secretid;
8e6d58cd
KW
594
595 static int inited = 0;
596
a7cea2ba 597 if (flags & BDRV_O_RDWR) {
2a94fee3 598 error_setg(errp, "curl block device does not support writes");
a7cea2ba
RJ
599 return -EROFS;
600 }
601
87ea75d5 602 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
8e6d58cd 603 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 604 if (local_err) {
2a94fee3 605 error_propagate(errp, local_err);
8e6d58cd
KW
606 goto out_noclean;
607 }
608
e3542c67
MB
609 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
610 READ_AHEAD_DEFAULT);
c76f4952 611 if ((s->readahead_size & 0x1ff) != 0) {
2a94fee3
PB
612 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
613 s->readahead_size);
c76f4952
N
614 goto out_noclean;
615 }
616
212aefaa
DHB
617 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
618 CURL_TIMEOUT_DEFAULT);
f76faeda
RJ
619 if (s->timeout > CURL_TIMEOUT_MAX) {
620 error_setg(errp, "timeout parameter is too large or negative");
621 goto out_noclean;
622 }
212aefaa 623
97a3ea57
MB
624 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
625
a94f83d9
RJ
626 cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
627 s->cookie = g_strdup(cookie);
628
e3542c67 629 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
8e6d58cd 630 if (file == NULL) {
2a94fee3 631 error_setg(errp, "curl block driver requires an 'url' option");
8e6d58cd
KW
632 goto out_noclean;
633 }
634
1bff9606
DB
635 s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME));
636 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET);
637
638 if (secretid) {
639 s->password = qcrypto_secret_lookup_as_utf8(secretid, errp);
640 if (!s->password) {
641 goto out_noclean;
642 }
643 }
644
645 s->proxyusername = g_strdup(
646 qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME));
647 secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET);
648 if (secretid) {
649 s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp);
650 if (!s->proxypassword) {
651 goto out_noclean;
652 }
653 }
654
769ce76d
AG
655 if (!inited) {
656 curl_global_init(CURL_GLOBAL_ALL);
657 inited = 1;
658 }
659
d0f2c4c6 660 DPRINTF("CURL: Opening %s\n", file);
63f0f45f 661 s->aio_context = bdrv_get_aio_context(bs);
8e6d58cd 662 s->url = g_strdup(file);
a2f468e4 663 state = curl_init_state(bs, s);
769ce76d
AG
664 if (!state)
665 goto out_noclean;
666
667 // Get file size
668
3494d650 669 s->accept_range = false;
769ce76d 670 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
3494d650
FZ
671 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
672 curl_header_cb);
673 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
769ce76d
AG
674 if (curl_easy_perform(state->curl))
675 goto out;
a41c4578 676 if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
769ce76d 677 goto out;
a41c4578
TG
678 }
679 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
680 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
681 * known and zero if it is realy zero-length file. */
682#if LIBCURL_VERSION_NUM >= 0x071304
683 if (d < 0) {
684 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
685 "Server didn't report file size.");
686 goto out;
687 }
688#else
689 if (d <= 0) {
690 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
691 "Unknown file size or zero-length file.");
692 goto out;
693 }
694#endif
695
696 s->len = (size_t)d;
697
3494d650
FZ
698 if ((!strncasecmp(s->url, "http://", strlen("http://"))
699 || !strncasecmp(s->url, "https://", strlen("https://")))
700 && !s->accept_range) {
701 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
702 "Server does not support 'range' (byte ranges).");
703 goto out;
704 }
0bfcd599 705 DPRINTF("CURL: Size = %zd\n", s->len);
769ce76d
AG
706
707 curl_clean_state(state);
708 curl_easy_cleanup(state->curl);
709 state->curl = NULL;
710
63f0f45f 711 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
769ce76d 712
8e6d58cd 713 qemu_opts_del(opts);
769ce76d
AG
714 return 0;
715
716out:
acd7fdc6 717 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
769ce76d
AG
718 curl_easy_cleanup(state->curl);
719 state->curl = NULL;
720out_noclean:
a94f83d9 721 g_free(s->cookie);
8e6d58cd
KW
722 g_free(s->url);
723 qemu_opts_del(opts);
769ce76d
AG
724 return -EINVAL;
725}
726
d7331bed 727static const AIOCBInfo curl_aiocb_info = {
c16b5a2c 728 .aiocb_size = sizeof(CURLAIOCB),
c16b5a2c
CH
729};
730
363c3c85
NT
731
732static void curl_readv_bh_cb(void *p)
769ce76d 733{
769ce76d 734 CURLState *state;
b69cdef8 735 int running;
769ce76d 736
363c3c85
NT
737 CURLAIOCB *acb = p;
738 BDRVCURLState *s = acb->common.bs->opaque;
769ce76d 739
363c3c85
NT
740 size_t start = acb->sector_num * SECTOR_SIZE;
741 size_t end;
769ce76d
AG
742
743 // In case we have the requested data already (e.g. read-ahead),
744 // we can just call the callback and be done.
363c3c85 745 switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
769ce76d 746 case FIND_RET_OK:
8007429a 747 qemu_aio_unref(acb);
769ce76d
AG
748 // fall through
749 case FIND_RET_WAIT:
363c3c85 750 return;
769ce76d
AG
751 default:
752 break;
753 }
754
755 // No cache found, so let's start a new request
a2f468e4 756 state = curl_init_state(acb->common.bs, s);
363c3c85
NT
757 if (!state) {
758 acb->common.cb(acb->common.opaque, -EIO);
8007429a 759 qemu_aio_unref(acb);
363c3c85
NT
760 return;
761 }
769ce76d
AG
762
763 acb->start = 0;
363c3c85 764 acb->end = (acb->nb_sectors * SECTOR_SIZE);
769ce76d
AG
765
766 state->buf_off = 0;
f7047c2d 767 g_free(state->orig_buf);
769ce76d 768 state->buf_start = start;
c76f4952 769 state->buf_len = acb->end + s->readahead_size;
769ce76d 770 end = MIN(start + state->buf_len, s->len) - 1;
8dc7a772
KW
771 state->orig_buf = g_try_malloc(state->buf_len);
772 if (state->buf_len && state->orig_buf == NULL) {
773 curl_clean_state(state);
774 acb->common.cb(acb->common.opaque, -ENOMEM);
8007429a 775 qemu_aio_unref(acb);
8dc7a772
KW
776 return;
777 }
769ce76d
AG
778 state->acb[0] = acb;
779
0bfcd599
BS
780 snprintf(state->range, 127, "%zd-%zd", start, end);
781 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
363c3c85 782 (acb->nb_sectors * SECTOR_SIZE), start, state->range);
769ce76d
AG
783 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
784
785 curl_multi_add_handle(s->multi, state->curl);
769ce76d 786
b69cdef8
MB
787 /* Tell curl it needs to kick things off */
788 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
363c3c85
NT
789}
790
7c84b1b8 791static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
363c3c85 792 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
097310b5 793 BlockCompletionFunc *cb, void *opaque)
363c3c85
NT
794{
795 CURLAIOCB *acb;
796
d7331bed 797 acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
363c3c85 798
363c3c85
NT
799 acb->qiov = qiov;
800 acb->sector_num = sector_num;
801 acb->nb_sectors = nb_sectors;
802
fffb6e12 803 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
769ce76d
AG
804 return &acb->common;
805}
806
769ce76d
AG
807static void curl_close(BlockDriverState *bs)
808{
809 BDRVCURLState *s = bs->opaque;
769ce76d 810
d0f2c4c6 811 DPRINTF("CURL: Close\n");
63f0f45f 812 curl_detach_aio_context(bs);
031fd1be 813
a94f83d9 814 g_free(s->cookie);
45724d6d 815 g_free(s->url);
769ce76d
AG
816}
817
818static int64_t curl_getlength(BlockDriverState *bs)
819{
820 BDRVCURLState *s = bs->opaque;
821 return s->len;
822}
823
824static BlockDriver bdrv_http = {
63f0f45f
SH
825 .format_name = "http",
826 .protocol_name = "http",
827
828 .instance_size = sizeof(BDRVCURLState),
829 .bdrv_parse_filename = curl_parse_filename,
830 .bdrv_file_open = curl_open,
831 .bdrv_close = curl_close,
832 .bdrv_getlength = curl_getlength,
769ce76d 833
63f0f45f 834 .bdrv_aio_readv = curl_aio_readv,
769ce76d 835
63f0f45f
SH
836 .bdrv_detach_aio_context = curl_detach_aio_context,
837 .bdrv_attach_aio_context = curl_attach_aio_context,
769ce76d
AG
838};
839
840static BlockDriver bdrv_https = {
63f0f45f
SH
841 .format_name = "https",
842 .protocol_name = "https",
769ce76d 843
63f0f45f
SH
844 .instance_size = sizeof(BDRVCURLState),
845 .bdrv_parse_filename = curl_parse_filename,
846 .bdrv_file_open = curl_open,
847 .bdrv_close = curl_close,
848 .bdrv_getlength = curl_getlength,
769ce76d 849
63f0f45f
SH
850 .bdrv_aio_readv = curl_aio_readv,
851
852 .bdrv_detach_aio_context = curl_detach_aio_context,
853 .bdrv_attach_aio_context = curl_attach_aio_context,
769ce76d
AG
854};
855
856static BlockDriver bdrv_ftp = {
63f0f45f
SH
857 .format_name = "ftp",
858 .protocol_name = "ftp",
859
860 .instance_size = sizeof(BDRVCURLState),
861 .bdrv_parse_filename = curl_parse_filename,
862 .bdrv_file_open = curl_open,
863 .bdrv_close = curl_close,
864 .bdrv_getlength = curl_getlength,
769ce76d 865
63f0f45f 866 .bdrv_aio_readv = curl_aio_readv,
769ce76d 867
63f0f45f
SH
868 .bdrv_detach_aio_context = curl_detach_aio_context,
869 .bdrv_attach_aio_context = curl_attach_aio_context,
769ce76d
AG
870};
871
872static BlockDriver bdrv_ftps = {
63f0f45f
SH
873 .format_name = "ftps",
874 .protocol_name = "ftps",
769ce76d 875
63f0f45f
SH
876 .instance_size = sizeof(BDRVCURLState),
877 .bdrv_parse_filename = curl_parse_filename,
878 .bdrv_file_open = curl_open,
879 .bdrv_close = curl_close,
880 .bdrv_getlength = curl_getlength,
769ce76d 881
63f0f45f
SH
882 .bdrv_aio_readv = curl_aio_readv,
883
884 .bdrv_detach_aio_context = curl_detach_aio_context,
885 .bdrv_attach_aio_context = curl_attach_aio_context,
769ce76d
AG
886};
887
769ce76d
AG
888static void curl_block_init(void)
889{
890 bdrv_register(&bdrv_http);
891 bdrv_register(&bdrv_https);
892 bdrv_register(&bdrv_ftp);
893 bdrv_register(&bdrv_ftps);
769ce76d
AG
894}
895
896block_init(curl_block_init);