]> git.proxmox.com Git - mirror_qemu.git/blob - block/curl.c
2698ae31e8d0bb0215d26059f60c4ef590eb86db
[mirror_qemu.git] / block / curl.c
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 */
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qapi/qmp/qbool.h"
27 #include <curl/curl.h>
28
29 // #define DEBUG
30 // #define DEBUG_VERBOSE
31
32 #ifdef DEBUG_CURL
33 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
34 #else
35 #define DPRINTF(fmt, ...) do { } while (0)
36 #endif
37
38 #if LIBCURL_VERSION_NUM >= 0x071000
39 /* The multi interface timer callback was introduced in 7.16.0 */
40 #define NEED_CURL_TIMER_CALLBACK
41 #define HAVE_SOCKET_ACTION
42 #endif
43
44 #ifndef HAVE_SOCKET_ACTION
45 /* If curl_multi_socket_action isn't available, define it statically here in
46 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
47 * less efficient but still safe. */
48 static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
49 curl_socket_t sockfd,
50 int ev_bitmask,
51 int *running_handles)
52 {
53 return curl_multi_socket(multi_handle, sockfd, running_handles);
54 }
55 #define curl_multi_socket_action __curl_multi_socket_action
56 #endif
57
58 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
59 CURLPROTO_FTP | CURLPROTO_FTPS | \
60 CURLPROTO_TFTP)
61
62 #define CURL_NUM_STATES 8
63 #define CURL_NUM_ACB 8
64 #define SECTOR_SIZE 512
65 #define READ_AHEAD_DEFAULT (256 * 1024)
66 #define CURL_TIMEOUT_DEFAULT 5
67
68 #define FIND_RET_NONE 0
69 #define FIND_RET_OK 1
70 #define FIND_RET_WAIT 2
71
72 #define CURL_BLOCK_OPT_URL "url"
73 #define CURL_BLOCK_OPT_READAHEAD "readahead"
74 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
75 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
76
77 struct BDRVCURLState;
78
79 typedef struct CURLAIOCB {
80 BlockDriverAIOCB common;
81 QEMUBH *bh;
82 QEMUIOVector *qiov;
83
84 int64_t sector_num;
85 int nb_sectors;
86
87 size_t start;
88 size_t end;
89 } CURLAIOCB;
90
91 typedef struct CURLState
92 {
93 struct BDRVCURLState *s;
94 CURLAIOCB *acb[CURL_NUM_ACB];
95 CURL *curl;
96 curl_socket_t sock_fd;
97 char *orig_buf;
98 size_t buf_start;
99 size_t buf_off;
100 size_t buf_len;
101 char range[128];
102 char errmsg[CURL_ERROR_SIZE];
103 char in_use;
104 } CURLState;
105
106 typedef struct BDRVCURLState {
107 CURLM *multi;
108 QEMUTimer timer;
109 size_t len;
110 CURLState states[CURL_NUM_STATES];
111 char *url;
112 size_t readahead_size;
113 bool sslverify;
114 int timeout;
115 bool accept_range;
116 AioContext *aio_context;
117 } BDRVCURLState;
118
119 static void curl_clean_state(CURLState *s);
120 static void curl_multi_do(void *arg);
121 static void curl_multi_read(void *arg);
122
123 #ifdef NEED_CURL_TIMER_CALLBACK
124 static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
125 {
126 BDRVCURLState *s = opaque;
127
128 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
129 if (timeout_ms == -1) {
130 timer_del(&s->timer);
131 } else {
132 int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
133 timer_mod(&s->timer,
134 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
135 }
136 return 0;
137 }
138 #endif
139
140 static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
141 void *userp, void *sp)
142 {
143 BDRVCURLState *s;
144 CURLState *state = NULL;
145 curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
146 state->sock_fd = fd;
147 s = state->s;
148
149 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
150 switch (action) {
151 case CURL_POLL_IN:
152 aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
153 NULL, state);
154 break;
155 case CURL_POLL_OUT:
156 aio_set_fd_handler(s->aio_context, fd, NULL, curl_multi_do, state);
157 break;
158 case CURL_POLL_INOUT:
159 aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
160 curl_multi_do, state);
161 break;
162 case CURL_POLL_REMOVE:
163 aio_set_fd_handler(s->aio_context, fd, NULL, NULL, NULL);
164 break;
165 }
166
167 return 0;
168 }
169
170 static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
171 {
172 BDRVCURLState *s = opaque;
173 size_t realsize = size * nmemb;
174 const char *accept_line = "Accept-Ranges: bytes";
175
176 if (realsize >= strlen(accept_line)
177 && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
178 s->accept_range = true;
179 }
180
181 return realsize;
182 }
183
184 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
185 {
186 CURLState *s = ((CURLState*)opaque);
187 size_t realsize = size * nmemb;
188 int i;
189
190 DPRINTF("CURL: Just reading %zd bytes\n", realsize);
191
192 if (!s || !s->orig_buf)
193 return 0;
194
195 if (s->buf_off >= s->buf_len) {
196 /* buffer full, read nothing */
197 return 0;
198 }
199 realsize = MIN(realsize, s->buf_len - s->buf_off);
200 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
201 s->buf_off += realsize;
202
203 for(i=0; i<CURL_NUM_ACB; i++) {
204 CURLAIOCB *acb = s->acb[i];
205
206 if (!acb)
207 continue;
208
209 if ((s->buf_off >= acb->end)) {
210 qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
211 acb->end - acb->start);
212 acb->common.cb(acb->common.opaque, 0);
213 qemu_aio_release(acb);
214 s->acb[i] = NULL;
215 }
216 }
217
218 return realsize;
219 }
220
221 static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
222 CURLAIOCB *acb)
223 {
224 int i;
225 size_t end = start + len;
226
227 for (i=0; i<CURL_NUM_STATES; i++) {
228 CURLState *state = &s->states[i];
229 size_t buf_end = (state->buf_start + state->buf_off);
230 size_t buf_fend = (state->buf_start + state->buf_len);
231
232 if (!state->orig_buf)
233 continue;
234 if (!state->buf_off)
235 continue;
236
237 // Does the existing buffer cover our section?
238 if ((start >= state->buf_start) &&
239 (start <= buf_end) &&
240 (end >= state->buf_start) &&
241 (end <= buf_end))
242 {
243 char *buf = state->orig_buf + (start - state->buf_start);
244
245 qemu_iovec_from_buf(acb->qiov, 0, buf, len);
246 acb->common.cb(acb->common.opaque, 0);
247
248 return FIND_RET_OK;
249 }
250
251 // Wait for unfinished chunks
252 if (state->in_use &&
253 (start >= state->buf_start) &&
254 (start <= buf_fend) &&
255 (end >= state->buf_start) &&
256 (end <= buf_fend))
257 {
258 int j;
259
260 acb->start = start - state->buf_start;
261 acb->end = acb->start + len;
262
263 for (j=0; j<CURL_NUM_ACB; j++) {
264 if (!state->acb[j]) {
265 state->acb[j] = acb;
266 return FIND_RET_WAIT;
267 }
268 }
269 }
270 }
271
272 return FIND_RET_NONE;
273 }
274
275 static void curl_multi_check_completion(BDRVCURLState *s)
276 {
277 int msgs_in_queue;
278
279 /* Try to find done transfers, so we can free the easy
280 * handle again. */
281 for (;;) {
282 CURLMsg *msg;
283 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
284
285 /* Quit when there are no more completions */
286 if (!msg)
287 break;
288
289 if (msg->msg == CURLMSG_DONE) {
290 CURLState *state = NULL;
291 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
292 (char **)&state);
293
294 /* ACBs for successful messages get completed in curl_read_cb */
295 if (msg->data.result != CURLE_OK) {
296 int i;
297 for (i = 0; i < CURL_NUM_ACB; i++) {
298 CURLAIOCB *acb = state->acb[i];
299
300 if (acb == NULL) {
301 continue;
302 }
303
304 acb->common.cb(acb->common.opaque, -EIO);
305 qemu_aio_release(acb);
306 state->acb[i] = NULL;
307 }
308 }
309
310 curl_clean_state(state);
311 break;
312 }
313 }
314 }
315
316 static void curl_multi_do(void *arg)
317 {
318 CURLState *s = (CURLState *)arg;
319 int running;
320 int r;
321
322 if (!s->s->multi) {
323 return;
324 }
325
326 do {
327 r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
328 } while(r == CURLM_CALL_MULTI_PERFORM);
329
330 }
331
332 static void curl_multi_read(void *arg)
333 {
334 CURLState *s = (CURLState *)arg;
335
336 curl_multi_do(arg);
337 curl_multi_check_completion(s->s);
338 }
339
340 static void curl_multi_timeout_do(void *arg)
341 {
342 #ifdef NEED_CURL_TIMER_CALLBACK
343 BDRVCURLState *s = (BDRVCURLState *)arg;
344 int running;
345
346 if (!s->multi) {
347 return;
348 }
349
350 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
351
352 curl_multi_check_completion(s);
353 #else
354 abort();
355 #endif
356 }
357
358 static CURLState *curl_init_state(BDRVCURLState *s)
359 {
360 CURLState *state = NULL;
361 int i, j;
362
363 do {
364 for (i=0; i<CURL_NUM_STATES; i++) {
365 for (j=0; j<CURL_NUM_ACB; j++)
366 if (s->states[i].acb[j])
367 continue;
368 if (s->states[i].in_use)
369 continue;
370
371 state = &s->states[i];
372 state->in_use = 1;
373 break;
374 }
375 if (!state) {
376 aio_poll(state->s->aio_context, true);
377 }
378 } while(!state);
379
380 if (!state->curl) {
381 state->curl = curl_easy_init();
382 if (!state->curl) {
383 return NULL;
384 }
385 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
386 curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
387 (long) s->sslverify);
388 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, s->timeout);
389 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
390 (void *)curl_read_cb);
391 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
392 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
393 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
394 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
395 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
396 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
397 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
398
399 /* Restrict supported protocols to avoid security issues in the more
400 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
401 * CVE-2013-0249.
402 *
403 * Restricting protocols is only supported from 7.19.4 upwards.
404 */
405 #if LIBCURL_VERSION_NUM >= 0x071304
406 curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
407 curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
408 #endif
409
410 #ifdef DEBUG_VERBOSE
411 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
412 #endif
413 }
414
415 state->s = s;
416
417 return state;
418 }
419
420 static void curl_clean_state(CURLState *s)
421 {
422 if (s->s->multi)
423 curl_multi_remove_handle(s->s->multi, s->curl);
424 s->in_use = 0;
425 }
426
427 static void curl_parse_filename(const char *filename, QDict *options,
428 Error **errp)
429 {
430 qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
431 }
432
433 static void curl_detach_aio_context(BlockDriverState *bs)
434 {
435 BDRVCURLState *s = bs->opaque;
436 int i;
437
438 for (i = 0; i < CURL_NUM_STATES; i++) {
439 if (s->states[i].in_use) {
440 curl_clean_state(&s->states[i]);
441 }
442 if (s->states[i].curl) {
443 curl_easy_cleanup(s->states[i].curl);
444 s->states[i].curl = NULL;
445 }
446 g_free(s->states[i].orig_buf);
447 s->states[i].orig_buf = NULL;
448 }
449 if (s->multi) {
450 curl_multi_cleanup(s->multi);
451 s->multi = NULL;
452 }
453
454 timer_del(&s->timer);
455 }
456
457 static void curl_attach_aio_context(BlockDriverState *bs,
458 AioContext *new_context)
459 {
460 BDRVCURLState *s = bs->opaque;
461
462 aio_timer_init(new_context, &s->timer,
463 QEMU_CLOCK_REALTIME, SCALE_NS,
464 curl_multi_timeout_do, s);
465
466 assert(!s->multi);
467 s->multi = curl_multi_init();
468 s->aio_context = new_context;
469 curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
470 #ifdef NEED_CURL_TIMER_CALLBACK
471 curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
472 curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
473 #endif
474 }
475
476 static QemuOptsList runtime_opts = {
477 .name = "curl",
478 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
479 .desc = {
480 {
481 .name = CURL_BLOCK_OPT_URL,
482 .type = QEMU_OPT_STRING,
483 .help = "URL to open",
484 },
485 {
486 .name = CURL_BLOCK_OPT_READAHEAD,
487 .type = QEMU_OPT_SIZE,
488 .help = "Readahead size",
489 },
490 {
491 .name = CURL_BLOCK_OPT_SSLVERIFY,
492 .type = QEMU_OPT_BOOL,
493 .help = "Verify SSL certificate"
494 },
495 {
496 .name = CURL_BLOCK_OPT_TIMEOUT,
497 .type = QEMU_OPT_NUMBER,
498 .help = "Curl timeout"
499 },
500 { /* end of list */ }
501 },
502 };
503
504 static int curl_open(BlockDriverState *bs, QDict *options, int flags,
505 Error **errp)
506 {
507 BDRVCURLState *s = bs->opaque;
508 CURLState *state = NULL;
509 QemuOpts *opts;
510 Error *local_err = NULL;
511 const char *file;
512 double d;
513
514 static int inited = 0;
515
516 if (flags & BDRV_O_RDWR) {
517 error_setg(errp, "curl block device does not support writes");
518 return -EROFS;
519 }
520
521 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
522 qemu_opts_absorb_qdict(opts, options, &local_err);
523 if (local_err) {
524 error_propagate(errp, local_err);
525 goto out_noclean;
526 }
527
528 s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
529 READ_AHEAD_DEFAULT);
530 if ((s->readahead_size & 0x1ff) != 0) {
531 error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
532 s->readahead_size);
533 goto out_noclean;
534 }
535
536 s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
537 CURL_TIMEOUT_DEFAULT);
538
539 s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
540
541 file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
542 if (file == NULL) {
543 error_setg(errp, "curl block driver requires an 'url' option");
544 goto out_noclean;
545 }
546
547 if (!inited) {
548 curl_global_init(CURL_GLOBAL_ALL);
549 inited = 1;
550 }
551
552 DPRINTF("CURL: Opening %s\n", file);
553 s->aio_context = bdrv_get_aio_context(bs);
554 s->url = g_strdup(file);
555 state = curl_init_state(s);
556 if (!state)
557 goto out_noclean;
558
559 // Get file size
560
561 s->accept_range = false;
562 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
563 curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
564 curl_header_cb);
565 curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
566 if (curl_easy_perform(state->curl))
567 goto out;
568 curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
569 if (d)
570 s->len = (size_t)d;
571 else if(!s->len)
572 goto out;
573 if ((!strncasecmp(s->url, "http://", strlen("http://"))
574 || !strncasecmp(s->url, "https://", strlen("https://")))
575 && !s->accept_range) {
576 pstrcpy(state->errmsg, CURL_ERROR_SIZE,
577 "Server does not support 'range' (byte ranges).");
578 goto out;
579 }
580 DPRINTF("CURL: Size = %zd\n", s->len);
581
582 curl_clean_state(state);
583 curl_easy_cleanup(state->curl);
584 state->curl = NULL;
585
586 curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
587
588 qemu_opts_del(opts);
589 return 0;
590
591 out:
592 error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
593 curl_easy_cleanup(state->curl);
594 state->curl = NULL;
595 out_noclean:
596 g_free(s->url);
597 qemu_opts_del(opts);
598 return -EINVAL;
599 }
600
601 static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
602 {
603 // Do we have to implement canceling? Seems to work without...
604 }
605
606 static const AIOCBInfo curl_aiocb_info = {
607 .aiocb_size = sizeof(CURLAIOCB),
608 .cancel = curl_aio_cancel,
609 };
610
611
612 static void curl_readv_bh_cb(void *p)
613 {
614 CURLState *state;
615 int running;
616
617 CURLAIOCB *acb = p;
618 BDRVCURLState *s = acb->common.bs->opaque;
619
620 qemu_bh_delete(acb->bh);
621 acb->bh = NULL;
622
623 size_t start = acb->sector_num * SECTOR_SIZE;
624 size_t end;
625
626 // In case we have the requested data already (e.g. read-ahead),
627 // we can just call the callback and be done.
628 switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
629 case FIND_RET_OK:
630 qemu_aio_release(acb);
631 // fall through
632 case FIND_RET_WAIT:
633 return;
634 default:
635 break;
636 }
637
638 // No cache found, so let's start a new request
639 state = curl_init_state(s);
640 if (!state) {
641 acb->common.cb(acb->common.opaque, -EIO);
642 qemu_aio_release(acb);
643 return;
644 }
645
646 acb->start = 0;
647 acb->end = (acb->nb_sectors * SECTOR_SIZE);
648
649 state->buf_off = 0;
650 g_free(state->orig_buf);
651 state->buf_start = start;
652 state->buf_len = acb->end + s->readahead_size;
653 end = MIN(start + state->buf_len, s->len) - 1;
654 state->orig_buf = g_try_malloc(state->buf_len);
655 if (state->buf_len && state->orig_buf == NULL) {
656 curl_clean_state(state);
657 acb->common.cb(acb->common.opaque, -ENOMEM);
658 qemu_aio_release(acb);
659 return;
660 }
661 state->acb[0] = acb;
662
663 snprintf(state->range, 127, "%zd-%zd", start, end);
664 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
665 (acb->nb_sectors * SECTOR_SIZE), start, state->range);
666 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
667
668 curl_multi_add_handle(s->multi, state->curl);
669
670 /* Tell curl it needs to kick things off */
671 curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
672 }
673
674 static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
675 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
676 BlockDriverCompletionFunc *cb, void *opaque)
677 {
678 CURLAIOCB *acb;
679
680 acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
681
682 acb->qiov = qiov;
683 acb->sector_num = sector_num;
684 acb->nb_sectors = nb_sectors;
685
686 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
687 qemu_bh_schedule(acb->bh);
688 return &acb->common;
689 }
690
691 static void curl_close(BlockDriverState *bs)
692 {
693 BDRVCURLState *s = bs->opaque;
694
695 DPRINTF("CURL: Close\n");
696 curl_detach_aio_context(bs);
697
698 g_free(s->url);
699 }
700
701 static int64_t curl_getlength(BlockDriverState *bs)
702 {
703 BDRVCURLState *s = bs->opaque;
704 return s->len;
705 }
706
707 static BlockDriver bdrv_http = {
708 .format_name = "http",
709 .protocol_name = "http",
710
711 .instance_size = sizeof(BDRVCURLState),
712 .bdrv_parse_filename = curl_parse_filename,
713 .bdrv_file_open = curl_open,
714 .bdrv_close = curl_close,
715 .bdrv_getlength = curl_getlength,
716
717 .bdrv_aio_readv = curl_aio_readv,
718
719 .bdrv_detach_aio_context = curl_detach_aio_context,
720 .bdrv_attach_aio_context = curl_attach_aio_context,
721 };
722
723 static BlockDriver bdrv_https = {
724 .format_name = "https",
725 .protocol_name = "https",
726
727 .instance_size = sizeof(BDRVCURLState),
728 .bdrv_parse_filename = curl_parse_filename,
729 .bdrv_file_open = curl_open,
730 .bdrv_close = curl_close,
731 .bdrv_getlength = curl_getlength,
732
733 .bdrv_aio_readv = curl_aio_readv,
734
735 .bdrv_detach_aio_context = curl_detach_aio_context,
736 .bdrv_attach_aio_context = curl_attach_aio_context,
737 };
738
739 static BlockDriver bdrv_ftp = {
740 .format_name = "ftp",
741 .protocol_name = "ftp",
742
743 .instance_size = sizeof(BDRVCURLState),
744 .bdrv_parse_filename = curl_parse_filename,
745 .bdrv_file_open = curl_open,
746 .bdrv_close = curl_close,
747 .bdrv_getlength = curl_getlength,
748
749 .bdrv_aio_readv = curl_aio_readv,
750
751 .bdrv_detach_aio_context = curl_detach_aio_context,
752 .bdrv_attach_aio_context = curl_attach_aio_context,
753 };
754
755 static BlockDriver bdrv_ftps = {
756 .format_name = "ftps",
757 .protocol_name = "ftps",
758
759 .instance_size = sizeof(BDRVCURLState),
760 .bdrv_parse_filename = curl_parse_filename,
761 .bdrv_file_open = curl_open,
762 .bdrv_close = curl_close,
763 .bdrv_getlength = curl_getlength,
764
765 .bdrv_aio_readv = curl_aio_readv,
766
767 .bdrv_detach_aio_context = curl_detach_aio_context,
768 .bdrv_attach_aio_context = curl_attach_aio_context,
769 };
770
771 static BlockDriver bdrv_tftp = {
772 .format_name = "tftp",
773 .protocol_name = "tftp",
774
775 .instance_size = sizeof(BDRVCURLState),
776 .bdrv_parse_filename = curl_parse_filename,
777 .bdrv_file_open = curl_open,
778 .bdrv_close = curl_close,
779 .bdrv_getlength = curl_getlength,
780
781 .bdrv_aio_readv = curl_aio_readv,
782
783 .bdrv_detach_aio_context = curl_detach_aio_context,
784 .bdrv_attach_aio_context = curl_attach_aio_context,
785 };
786
787 static void curl_block_init(void)
788 {
789 bdrv_register(&bdrv_http);
790 bdrv_register(&bdrv_https);
791 bdrv_register(&bdrv_ftp);
792 bdrv_register(&bdrv_ftps);
793 bdrv_register(&bdrv_tftp);
794 }
795
796 block_init(curl_block_init);