Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 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 | */ |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 27 | #include "qemu/error-report.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 28 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 29 | #include "qemu/option.h" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 30 | #include "block/block-io.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 31 | #include "block/block_int.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 32 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 33 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 34 | #include "crypto/secret.h" |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 35 | #include <curl/curl.h> |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 36 | #include "qemu/cutils.h" |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 37 | #include "trace.h" |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 38 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 39 | // #define DEBUG_VERBOSE |
| 40 | |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 41 | /* CURL 7.85.0 switches to a string based API for specifying |
| 42 | * the desired protocols. |
| 43 | */ |
| 44 | #if LIBCURL_VERSION_NUM >= 0x075500 |
| 45 | #define PROTOCOLS "HTTP,HTTPS,FTP,FTPS" |
| 46 | #else |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 47 | #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \ |
Max Reitz | 23dce38 | 2016-11-02 18:55:37 +0100 | [diff] [blame] | 48 | CURLPROTO_FTP | CURLPROTO_FTPS) |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 49 | #endif |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 50 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 51 | #define CURL_NUM_STATES 8 |
| 52 | #define CURL_NUM_ACB 8 |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 53 | #define CURL_TIMEOUT_MAX 10000 |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 54 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 55 | #define CURL_BLOCK_OPT_URL "url" |
| 56 | #define CURL_BLOCK_OPT_READAHEAD "readahead" |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 57 | #define CURL_BLOCK_OPT_SSLVERIFY "sslverify" |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 58 | #define CURL_BLOCK_OPT_TIMEOUT "timeout" |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 59 | #define CURL_BLOCK_OPT_COOKIE "cookie" |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 60 | #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret" |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 61 | #define CURL_BLOCK_OPT_USERNAME "username" |
| 62 | #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret" |
| 63 | #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username" |
| 64 | #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret" |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 65 | |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 66 | #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024) |
| 67 | #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true |
| 68 | #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5 |
| 69 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 70 | struct BDRVCURLState; |
Max Reitz | 0487861 | 2019-09-10 14:41:30 +0200 | [diff] [blame] | 71 | struct CURLState; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 72 | |
Jeff Cody | 2d25964 | 2017-11-07 17:27:22 -0500 | [diff] [blame] | 73 | static bool libcurl_initialized; |
| 74 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 75 | typedef struct CURLAIOCB { |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 76 | Coroutine *co; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 77 | QEMUIOVector *qiov; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 78 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 79 | uint64_t offset; |
| 80 | uint64_t bytes; |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 81 | int ret; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 82 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 83 | size_t start; |
| 84 | size_t end; |
| 85 | } CURLAIOCB; |
| 86 | |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 87 | typedef struct CURLSocket { |
| 88 | int fd; |
Max Reitz | 3663dca | 2021-03-09 14:05:40 +0100 | [diff] [blame] | 89 | struct BDRVCURLState *s; |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 90 | } CURLSocket; |
| 91 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 92 | typedef struct CURLState |
| 93 | { |
| 94 | struct BDRVCURLState *s; |
| 95 | CURLAIOCB *acb[CURL_NUM_ACB]; |
| 96 | CURL *curl; |
| 97 | char *orig_buf; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 98 | uint64_t buf_start; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 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; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 108 | QEMUTimer timer; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 109 | uint64_t len; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 110 | CURLState states[CURL_NUM_STATES]; |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 111 | GHashTable *sockets; /* GINT_TO_POINTER(fd) -> socket */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 112 | char *url; |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 113 | size_t readahead_size; |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 114 | bool sslverify; |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 115 | uint64_t timeout; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 116 | char *cookie; |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 117 | bool accept_range; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 118 | AioContext *aio_context; |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 119 | QemuMutex mutex; |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 120 | CoQueue free_state_waitq; |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 121 | char *username; |
| 122 | char *password; |
| 123 | char *proxyusername; |
| 124 | char *proxypassword; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 125 | } BDRVCURLState; |
| 126 | |
| 127 | static void curl_clean_state(CURLState *s); |
| 128 | static void curl_multi_do(void *arg); |
| 129 | |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 130 | static gboolean curl_drop_socket(void *key, void *value, void *opaque) |
| 131 | { |
| 132 | CURLSocket *socket = value; |
| 133 | BDRVCURLState *s = socket->s; |
| 134 | |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 135 | aio_set_fd_handler(s->aio_context, socket->fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 136 | NULL, NULL, NULL, NULL, NULL); |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 137 | return true; |
| 138 | } |
| 139 | |
| 140 | static void curl_drop_all_sockets(GHashTable *sockets) |
| 141 | { |
| 142 | g_hash_table_foreach_remove(sockets, curl_drop_socket, NULL); |
| 143 | } |
| 144 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 145 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 146 | static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque) |
| 147 | { |
| 148 | BDRVCURLState *s = opaque; |
| 149 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 150 | trace_curl_timer_cb(timeout_ms); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 151 | if (timeout_ms == -1) { |
| 152 | timer_del(&s->timer); |
| 153 | } else { |
| 154 | int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000; |
| 155 | timer_mod(&s->timer, |
| 156 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 160 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 161 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 162 | static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 163 | void *userp, void *sp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 164 | { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 165 | BDRVCURLState *s; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 166 | CURLState *state = NULL; |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 167 | CURLSocket *socket; |
| 168 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 169 | curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 170 | s = state->s; |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 171 | |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 172 | socket = g_hash_table_lookup(s->sockets, GINT_TO_POINTER(fd)); |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 173 | if (!socket) { |
| 174 | socket = g_new0(CURLSocket, 1); |
| 175 | socket->fd = fd; |
Max Reitz | 3663dca | 2021-03-09 14:05:40 +0100 | [diff] [blame] | 176 | socket->s = s; |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 177 | g_hash_table_insert(s->sockets, GINT_TO_POINTER(fd), socket); |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 178 | } |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 179 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 180 | trace_curl_sock_cb(action, (int)fd); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 181 | switch (action) { |
| 182 | case CURL_POLL_IN: |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 183 | aio_set_fd_handler(s->aio_context, fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 184 | curl_multi_do, NULL, NULL, NULL, socket); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 185 | break; |
| 186 | case CURL_POLL_OUT: |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 187 | aio_set_fd_handler(s->aio_context, fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 188 | NULL, curl_multi_do, NULL, NULL, socket); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 189 | break; |
| 190 | case CURL_POLL_INOUT: |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 191 | aio_set_fd_handler(s->aio_context, fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 192 | curl_multi_do, curl_multi_do, |
| 193 | NULL, NULL, socket); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 194 | break; |
| 195 | case CURL_POLL_REMOVE: |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 196 | aio_set_fd_handler(s->aio_context, fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 197 | NULL, NULL, NULL, NULL, NULL); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | |
Max Reitz | 007f339 | 2019-09-10 14:41:31 +0200 | [diff] [blame] | 201 | if (action == CURL_POLL_REMOVE) { |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 202 | g_hash_table_remove(s->sockets, GINT_TO_POINTER(fd)); |
Max Reitz | 007f339 | 2019-09-10 14:41:31 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 208 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 209 | static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 210 | { |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 211 | BDRVCURLState *s = opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 212 | size_t realsize = size * nmemb; |
Michael Tokarev | 2abc22e | 2024-06-29 16:27:00 +0300 | [diff] [blame] | 213 | const char *p = ptr; |
| 214 | const char *end = p + realsize; |
| 215 | const char *t = "accept-ranges : bytes "; /* A lowercase template */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 216 | |
Michael Tokarev | 2abc22e | 2024-06-29 16:27:00 +0300 | [diff] [blame] | 217 | /* check if header matches the "t" template */ |
| 218 | for (;;) { |
| 219 | if (*t == ' ') { /* space in t matches any amount of isspace in p */ |
| 220 | if (p < end && g_ascii_isspace(*p)) { |
| 221 | ++p; |
| 222 | } else { |
| 223 | ++t; |
David Edmondson | 7788a31 | 2020-02-24 10:13:09 +0000 | [diff] [blame] | 224 | } |
Michael Tokarev | 2abc22e | 2024-06-29 16:27:00 +0300 | [diff] [blame] | 225 | } else if (*t && p < end && *t == g_ascii_tolower(*p)) { |
| 226 | ++p, ++t; |
| 227 | } else { |
| 228 | break; |
David Edmondson | 7788a31 | 2020-02-24 10:13:09 +0000 | [diff] [blame] | 229 | } |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 230 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 231 | |
Michael Tokarev | 2abc22e | 2024-06-29 16:27:00 +0300 | [diff] [blame] | 232 | if (!*t && p == end) { /* if we managed to reach ends of both strings */ |
| 233 | s->accept_range = true; |
| 234 | } |
| 235 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 236 | return realsize; |
| 237 | } |
| 238 | |
Paolo Bonzini | 34db05e | 2017-05-15 12:00:54 +0200 | [diff] [blame] | 239 | /* Called from curl_multi_do_locked, with s->mutex held. */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 240 | static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque) |
| 241 | { |
| 242 | CURLState *s = ((CURLState*)opaque); |
| 243 | size_t realsize = size * nmemb; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 244 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 245 | trace_curl_read_cb(realsize); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 246 | |
Max Reitz | 4e76765 | 2016-10-25 04:54:29 +0200 | [diff] [blame] | 247 | if (!s || !s->orig_buf) { |
| 248 | goto read_end; |
| 249 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 250 | |
Fam Zheng | 6d4b9e5 | 2014-03-26 13:05:40 +0100 | [diff] [blame] | 251 | if (s->buf_off >= s->buf_len) { |
| 252 | /* buffer full, read nothing */ |
Max Reitz | 4e76765 | 2016-10-25 04:54:29 +0200 | [diff] [blame] | 253 | goto read_end; |
Fam Zheng | 6d4b9e5 | 2014-03-26 13:05:40 +0100 | [diff] [blame] | 254 | } |
| 255 | realsize = MIN(realsize, s->buf_len - s->buf_off); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 256 | memcpy(s->orig_buf + s->buf_off, ptr, realsize); |
| 257 | s->buf_off += realsize; |
| 258 | |
Max Reitz | 4e76765 | 2016-10-25 04:54:29 +0200 | [diff] [blame] | 259 | read_end: |
| 260 | /* curl will error out if we do not return this value */ |
| 261 | return size * nmemb; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 264 | /* Called with s->mutex held. */ |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 265 | static bool curl_find_buf(BDRVCURLState *s, uint64_t start, uint64_t len, |
| 266 | CURLAIOCB *acb) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 267 | { |
| 268 | int i; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 269 | uint64_t end = start + len; |
| 270 | uint64_t clamped_end = MIN(end, s->len); |
| 271 | uint64_t clamped_len = clamped_end - start; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 272 | |
| 273 | for (i=0; i<CURL_NUM_STATES; i++) { |
| 274 | CURLState *state = &s->states[i]; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 275 | uint64_t buf_end = (state->buf_start + state->buf_off); |
| 276 | uint64_t buf_fend = (state->buf_start + state->buf_len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 277 | |
| 278 | if (!state->orig_buf) |
| 279 | continue; |
| 280 | if (!state->buf_off) |
| 281 | continue; |
| 282 | |
| 283 | // Does the existing buffer cover our section? |
| 284 | if ((start >= state->buf_start) && |
| 285 | (start <= buf_end) && |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 286 | (clamped_end >= state->buf_start) && |
| 287 | (clamped_end <= buf_end)) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 288 | { |
| 289 | char *buf = state->orig_buf + (start - state->buf_start); |
| 290 | |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 291 | qemu_iovec_from_buf(acb->qiov, 0, buf, clamped_len); |
| 292 | if (clamped_len < len) { |
| 293 | qemu_iovec_memset(acb->qiov, clamped_len, 0, len - clamped_len); |
| 294 | } |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 295 | acb->ret = 0; |
| 296 | return true; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Wait for unfinished chunks |
Matthew Booth | b7079df | 2014-04-29 16:03:32 +0100 | [diff] [blame] | 300 | if (state->in_use && |
| 301 | (start >= state->buf_start) && |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 302 | (start <= buf_fend) && |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 303 | (clamped_end >= state->buf_start) && |
| 304 | (clamped_end <= buf_fend)) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 305 | { |
| 306 | int j; |
| 307 | |
| 308 | acb->start = start - state->buf_start; |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 309 | acb->end = acb->start + clamped_len; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 310 | |
| 311 | for (j=0; j<CURL_NUM_ACB; j++) { |
| 312 | if (!state->acb[j]) { |
| 313 | state->acb[j] = acb; |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 314 | return true; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 320 | return false; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 323 | /* Called with s->mutex held. */ |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 324 | static void curl_multi_check_completion(BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 325 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 326 | int msgs_in_queue; |
| 327 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 328 | /* Try to find done transfers, so we can free the easy |
| 329 | * handle again. */ |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 330 | for (;;) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 331 | CURLMsg *msg; |
| 332 | msg = curl_multi_info_read(s->multi, &msgs_in_queue); |
| 333 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 334 | /* Quit when there are no more completions */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 335 | if (!msg) |
| 336 | break; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 337 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 338 | if (msg->msg == CURLMSG_DONE) { |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 339 | int i; |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 340 | CURLState *state = NULL; |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 341 | bool error = msg->data.result != CURLE_OK; |
| 342 | |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 343 | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, |
| 344 | (char **)&state); |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 345 | |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 346 | if (error) { |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 347 | static int errcount = 100; |
| 348 | |
| 349 | /* Don't lose the original error message from curl, since |
| 350 | * it contains extra data. |
| 351 | */ |
| 352 | if (errcount > 0) { |
| 353 | error_report("curl: %s", state->errmsg); |
| 354 | if (--errcount == 0) { |
| 355 | error_report("curl: further errors suppressed"); |
| 356 | } |
| 357 | } |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 358 | } |
Richard W.M. Jones | 796a060 | 2015-07-08 14:37:48 +0100 | [diff] [blame] | 359 | |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 360 | for (i = 0; i < CURL_NUM_ACB; i++) { |
| 361 | CURLAIOCB *acb = state->acb[i]; |
Nicholas Thomas | f785a5a | 2011-08-15 10:00:34 +0100 | [diff] [blame] | 362 | |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 363 | if (acb == NULL) { |
| 364 | continue; |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 365 | } |
Max Reitz | bfb23b4 | 2019-09-10 14:41:35 +0200 | [diff] [blame] | 366 | |
| 367 | if (!error) { |
| 368 | /* Assert that we have read all data */ |
| 369 | assert(state->buf_off >= acb->end); |
| 370 | |
| 371 | qemu_iovec_from_buf(acb->qiov, 0, |
| 372 | state->orig_buf + acb->start, |
| 373 | acb->end - acb->start); |
| 374 | |
| 375 | if (acb->end - acb->start < acb->bytes) { |
| 376 | size_t offset = acb->end - acb->start; |
| 377 | qemu_iovec_memset(acb->qiov, offset, 0, |
| 378 | acb->bytes - offset); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | acb->ret = error ? -EIO : 0; |
| 383 | state->acb[i] = NULL; |
| 384 | qemu_mutex_unlock(&s->mutex); |
| 385 | aio_co_wake(acb->co); |
| 386 | qemu_mutex_lock(&s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 387 | } |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 388 | |
| 389 | curl_clean_state(state); |
| 390 | break; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 391 | } |
Matthew Booth | 1f2cead | 2014-04-29 16:03:31 +0100 | [diff] [blame] | 392 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 395 | /* Called with s->mutex held. */ |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 396 | static void curl_multi_do_locked(CURLSocket *socket) |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 397 | { |
Max Reitz | 3663dca | 2021-03-09 14:05:40 +0100 | [diff] [blame] | 398 | BDRVCURLState *s = socket->s; |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 399 | int running; |
| 400 | int r; |
| 401 | |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 402 | if (!s->multi) { |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
Max Reitz | 9abaf9f | 2019-09-10 14:41:34 +0200 | [diff] [blame] | 406 | do { |
| 407 | r = curl_multi_socket_action(s->multi, socket->fd, 0, &running); |
| 408 | } while (r == CURLM_CALL_MULTI_PERFORM); |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 411 | static void curl_multi_do(void *arg) |
| 412 | { |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 413 | CURLSocket *socket = arg; |
Max Reitz | 3663dca | 2021-03-09 14:05:40 +0100 | [diff] [blame] | 414 | BDRVCURLState *s = socket->s; |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 415 | |
Max Reitz | 9dbad87 | 2019-09-10 14:41:33 +0200 | [diff] [blame] | 416 | qemu_mutex_lock(&s->mutex); |
| 417 | curl_multi_do_locked(socket); |
| 418 | curl_multi_check_completion(s); |
| 419 | qemu_mutex_unlock(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void curl_multi_timeout_do(void *arg) |
| 423 | { |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 424 | BDRVCURLState *s = (BDRVCURLState *)arg; |
| 425 | int running; |
| 426 | |
| 427 | if (!s->multi) { |
| 428 | return; |
| 429 | } |
| 430 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 431 | qemu_mutex_lock(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 432 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
| 433 | |
Matthew Booth | 838ef60 | 2014-04-29 16:03:30 +0100 | [diff] [blame] | 434 | curl_multi_check_completion(s); |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 435 | qemu_mutex_unlock(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 438 | /* Called with s->mutex held. */ |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 439 | static CURLState *curl_find_state(BDRVCURLState *s) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 440 | { |
| 441 | CURLState *state = NULL; |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 442 | int i; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 443 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 444 | for (i = 0; i < CURL_NUM_STATES; i++) { |
| 445 | if (!s->states[i].in_use) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 446 | state = &s->states[i]; |
| 447 | state->in_use = 1; |
| 448 | break; |
| 449 | } |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 450 | } |
| 451 | return state; |
| 452 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 453 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 454 | static int curl_init_state(BDRVCURLState *s, CURLState *state) |
| 455 | { |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 456 | if (!state->curl) { |
| 457 | state->curl = curl_easy_init(); |
| 458 | if (!state->curl) { |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 459 | return -EIO; |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 460 | } |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 461 | if (curl_easy_setopt(state->curl, CURLOPT_URL, s->url) || |
| 462 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER, |
| 463 | (long) s->sslverify) || |
| 464 | curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYHOST, |
| 465 | s->sslverify ? 2L : 0L)) { |
| 466 | goto err; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 467 | } |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 468 | if (s->cookie) { |
| 469 | if (curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie)) { |
| 470 | goto err; |
| 471 | } |
| 472 | } |
| 473 | if (curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout) || |
| 474 | curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, |
| 475 | (void *)curl_read_cb) || |
| 476 | curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state) || |
| 477 | curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state) || |
| 478 | curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1) || |
| 479 | curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1) || |
| 480 | curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1) || |
| 481 | curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg) || |
| 482 | curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1)) { |
| 483 | goto err; |
| 484 | } |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 485 | if (s->username) { |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 486 | if (curl_easy_setopt(state->curl, CURLOPT_USERNAME, s->username)) { |
| 487 | goto err; |
| 488 | } |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 489 | } |
| 490 | if (s->password) { |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 491 | if (curl_easy_setopt(state->curl, CURLOPT_PASSWORD, s->password)) { |
| 492 | goto err; |
| 493 | } |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 494 | } |
| 495 | if (s->proxyusername) { |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 496 | if (curl_easy_setopt(state->curl, |
| 497 | CURLOPT_PROXYUSERNAME, s->proxyusername)) { |
| 498 | goto err; |
| 499 | } |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 500 | } |
| 501 | if (s->proxypassword) { |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 502 | if (curl_easy_setopt(state->curl, |
| 503 | CURLOPT_PROXYPASSWORD, s->proxypassword)) { |
| 504 | goto err; |
| 505 | } |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 508 | /* Restrict supported protocols to avoid security issues in the more |
| 509 | * obscure protocols. For example, do not allow POP3/SMTP/IMAP see |
| 510 | * CVE-2013-0249. |
| 511 | * |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 512 | * Restricting protocols is only supported from 7.19.4 upwards. Note: |
| 513 | * version 7.85.0 deprecates CURLOPT_*PROTOCOLS in favour of a string |
| 514 | * based CURLOPT_*PROTOCOLS_STR API. |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 515 | */ |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 516 | #if LIBCURL_VERSION_NUM >= 0x075500 |
| 517 | if (curl_easy_setopt(state->curl, |
| 518 | CURLOPT_PROTOCOLS_STR, PROTOCOLS) || |
| 519 | curl_easy_setopt(state->curl, |
| 520 | CURLOPT_REDIR_PROTOCOLS_STR, PROTOCOLS)) { |
| 521 | goto err; |
| 522 | } |
| 523 | #elif LIBCURL_VERSION_NUM >= 0x071304 |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 524 | if (curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS) || |
| 525 | curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS)) { |
| 526 | goto err; |
| 527 | } |
Stefan Hajnoczi | 8a8f584 | 2013-02-13 09:25:34 +0100 | [diff] [blame] | 528 | #endif |
Stefan Hajnoczi | fb6d1bb | 2013-02-08 08:49:10 +0100 | [diff] [blame] | 529 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 530 | #ifdef DEBUG_VERBOSE |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 531 | if (curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1)) { |
| 532 | goto err; |
| 533 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 534 | #endif |
Matthew Booth | 9e550b3 | 2014-04-29 16:03:26 +0100 | [diff] [blame] | 535 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 536 | |
| 537 | state->s = s; |
| 538 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 539 | return 0; |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 540 | |
| 541 | err: |
| 542 | curl_easy_cleanup(state->curl); |
| 543 | state->curl = NULL; |
| 544 | return -EIO; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 547 | /* Called with s->mutex held. */ |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 548 | static void curl_clean_state(CURLState *s) |
| 549 | { |
Paolo Bonzini | 675a775 | 2017-05-15 12:00:53 +0200 | [diff] [blame] | 550 | int j; |
| 551 | for (j = 0; j < CURL_NUM_ACB; j++) { |
| 552 | assert(!s->acb[j]); |
| 553 | } |
| 554 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 555 | if (s->s->multi) |
| 556 | curl_multi_remove_handle(s->s->multi, s->curl); |
Max Reitz | ff5ca16 | 2016-10-25 04:54:30 +0200 | [diff] [blame] | 557 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 558 | s->in_use = 0; |
Paolo Bonzini | 2bb5c93 | 2017-05-15 12:00:59 +0200 | [diff] [blame] | 559 | |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 560 | qemu_co_enter_next(&s->s->free_state_waitq, &s->s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 563 | static void curl_parse_filename(const char *filename, QDict *options, |
| 564 | Error **errp) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 565 | { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 566 | qdict_put_str(options, CURL_BLOCK_OPT_URL, filename); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 569 | static void curl_detach_aio_context(BlockDriverState *bs) |
| 570 | { |
| 571 | BDRVCURLState *s = bs->opaque; |
| 572 | int i; |
| 573 | |
Gan Qixin | f5056b7 | 2020-12-03 15:50:53 +0800 | [diff] [blame] | 574 | WITH_QEMU_LOCK_GUARD(&s->mutex) { |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 575 | curl_drop_all_sockets(s->sockets); |
Gan Qixin | f5056b7 | 2020-12-03 15:50:53 +0800 | [diff] [blame] | 576 | for (i = 0; i < CURL_NUM_STATES; i++) { |
| 577 | if (s->states[i].in_use) { |
| 578 | curl_clean_state(&s->states[i]); |
| 579 | } |
| 580 | if (s->states[i].curl) { |
| 581 | curl_easy_cleanup(s->states[i].curl); |
| 582 | s->states[i].curl = NULL; |
| 583 | } |
| 584 | g_free(s->states[i].orig_buf); |
| 585 | s->states[i].orig_buf = NULL; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 586 | } |
Gan Qixin | f5056b7 | 2020-12-03 15:50:53 +0800 | [diff] [blame] | 587 | if (s->multi) { |
| 588 | curl_multi_cleanup(s->multi); |
| 589 | s->multi = NULL; |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 590 | } |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 591 | } |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 592 | |
| 593 | timer_del(&s->timer); |
| 594 | } |
| 595 | |
| 596 | static void curl_attach_aio_context(BlockDriverState *bs, |
| 597 | AioContext *new_context) |
| 598 | { |
| 599 | BDRVCURLState *s = bs->opaque; |
| 600 | |
| 601 | aio_timer_init(new_context, &s->timer, |
| 602 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 603 | curl_multi_timeout_do, s); |
| 604 | |
| 605 | assert(!s->multi); |
| 606 | s->multi = curl_multi_init(); |
| 607 | s->aio_context = new_context; |
| 608 | curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 609 | curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s); |
| 610 | curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 613 | static QemuOptsList runtime_opts = { |
| 614 | .name = "curl", |
| 615 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 616 | .desc = { |
| 617 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 618 | .name = CURL_BLOCK_OPT_URL, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 619 | .type = QEMU_OPT_STRING, |
| 620 | .help = "URL to open", |
| 621 | }, |
| 622 | { |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 623 | .name = CURL_BLOCK_OPT_READAHEAD, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 624 | .type = QEMU_OPT_SIZE, |
| 625 | .help = "Readahead size", |
| 626 | }, |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 627 | { |
| 628 | .name = CURL_BLOCK_OPT_SSLVERIFY, |
| 629 | .type = QEMU_OPT_BOOL, |
| 630 | .help = "Verify SSL certificate" |
| 631 | }, |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 632 | { |
| 633 | .name = CURL_BLOCK_OPT_TIMEOUT, |
| 634 | .type = QEMU_OPT_NUMBER, |
| 635 | .help = "Curl timeout" |
| 636 | }, |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 637 | { |
| 638 | .name = CURL_BLOCK_OPT_COOKIE, |
| 639 | .type = QEMU_OPT_STRING, |
| 640 | .help = "Pass the cookie or list of cookies with each request" |
| 641 | }, |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 642 | { |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 643 | .name = CURL_BLOCK_OPT_COOKIE_SECRET, |
| 644 | .type = QEMU_OPT_STRING, |
| 645 | .help = "ID of secret used as cookie passed with each request" |
| 646 | }, |
| 647 | { |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 648 | .name = CURL_BLOCK_OPT_USERNAME, |
| 649 | .type = QEMU_OPT_STRING, |
| 650 | .help = "Username for HTTP auth" |
| 651 | }, |
| 652 | { |
| 653 | .name = CURL_BLOCK_OPT_PASSWORD_SECRET, |
| 654 | .type = QEMU_OPT_STRING, |
| 655 | .help = "ID of secret used as password for HTTP auth", |
| 656 | }, |
| 657 | { |
| 658 | .name = CURL_BLOCK_OPT_PROXY_USERNAME, |
| 659 | .type = QEMU_OPT_STRING, |
| 660 | .help = "Username for HTTP proxy auth" |
| 661 | }, |
| 662 | { |
| 663 | .name = CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, |
| 664 | .type = QEMU_OPT_STRING, |
| 665 | .help = "ID of secret used as password for HTTP proxy auth", |
| 666 | }, |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 667 | { /* end of list */ } |
| 668 | }, |
| 669 | }; |
| 670 | |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 671 | |
Max Reitz | 015a103 | 2013-09-05 14:22:29 +0200 | [diff] [blame] | 672 | static int curl_open(BlockDriverState *bs, QDict *options, int flags, |
| 673 | Error **errp) |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 674 | { |
| 675 | BDRVCURLState *s = bs->opaque; |
| 676 | CURLState *state = NULL; |
| 677 | QemuOpts *opts; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 678 | const char *file; |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 679 | const char *cookie; |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 680 | const char *cookie_secret; |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 681 | /* CURL >= 7.55.0 uses curl_off_t for content length instead of a double */ |
| 682 | #if LIBCURL_VERSION_NUM >= 0x073700 |
| 683 | curl_off_t cl; |
| 684 | #else |
| 685 | double cl; |
| 686 | #endif |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 687 | const char *secretid; |
Max Reitz | 34634ca | 2017-03-31 14:04:31 +0200 | [diff] [blame] | 688 | const char *protocol_delimiter; |
Jeff Cody | 2d25964 | 2017-11-07 17:27:22 -0500 | [diff] [blame] | 689 | int ret; |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 690 | |
Kevin Wolf | 018f9de | 2023-09-29 16:51:53 +0200 | [diff] [blame] | 691 | bdrv_graph_rdlock_main_loop(); |
Kevin Wolf | 6ceef36 | 2018-10-08 17:27:18 +0200 | [diff] [blame] | 692 | ret = bdrv_apply_auto_read_only(bs, "curl driver does not support writes", |
| 693 | errp); |
Kevin Wolf | 018f9de | 2023-09-29 16:51:53 +0200 | [diff] [blame] | 694 | bdrv_graph_rdunlock_main_loop(); |
Kevin Wolf | 6ceef36 | 2018-10-08 17:27:18 +0200 | [diff] [blame] | 695 | if (ret < 0) { |
| 696 | return ret; |
Richard W.M. Jones | a7cea2b | 2013-06-10 12:38:43 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Jeff Cody | 2d25964 | 2017-11-07 17:27:22 -0500 | [diff] [blame] | 699 | if (!libcurl_initialized) { |
| 700 | ret = curl_global_init(CURL_GLOBAL_ALL); |
| 701 | if (ret) { |
| 702 | error_setg(errp, "libcurl initialization failed with %d", ret); |
| 703 | return -EIO; |
| 704 | } |
| 705 | libcurl_initialized = true; |
| 706 | } |
| 707 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 708 | qemu_mutex_init(&s->mutex); |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 709 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
Markus Armbruster | 668f62e | 2020-07-07 18:06:02 +0200 | [diff] [blame] | 710 | if (!qemu_opts_absorb_qdict(opts, options, errp)) { |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 711 | goto out_noclean; |
| 712 | } |
| 713 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 714 | s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD, |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 715 | CURL_BLOCK_OPT_READAHEAD_DEFAULT); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 716 | if ((s->readahead_size & 0x1ff) != 0) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 717 | error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512", |
| 718 | s->readahead_size); |
Nolan | c76f495 | 2009-07-01 17:16:52 -0700 | [diff] [blame] | 719 | goto out_noclean; |
| 720 | } |
| 721 | |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 722 | s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT, |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 723 | CURL_BLOCK_OPT_TIMEOUT_DEFAULT); |
Richard W.M. Jones | f76faed | 2014-10-26 11:05:27 +0000 | [diff] [blame] | 724 | if (s->timeout > CURL_TIMEOUT_MAX) { |
| 725 | error_setg(errp, "timeout parameter is too large or negative"); |
| 726 | goto out_noclean; |
| 727 | } |
Daniel Henrique Barboza | 212aefa | 2014-08-13 12:44:27 -0300 | [diff] [blame] | 728 | |
Max Reitz | 712b64e | 2019-02-01 20:29:31 +0100 | [diff] [blame] | 729 | s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, |
| 730 | CURL_BLOCK_OPT_SSLVERIFY_DEFAULT); |
Matthew Booth | 97a3ea5 | 2014-05-14 19:28:42 -0400 | [diff] [blame] | 731 | |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 732 | cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE); |
Peter Krempa | 327c8eb | 2017-05-04 16:00:06 +0200 | [diff] [blame] | 733 | cookie_secret = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE_SECRET); |
| 734 | |
| 735 | if (cookie && cookie_secret) { |
| 736 | error_setg(errp, |
| 737 | "curl driver cannot handle both cookie and cookie secret"); |
| 738 | goto out_noclean; |
| 739 | } |
| 740 | |
| 741 | if (cookie_secret) { |
| 742 | s->cookie = qcrypto_secret_lookup_as_utf8(cookie_secret, errp); |
| 743 | if (!s->cookie) { |
| 744 | goto out_noclean; |
| 745 | } |
| 746 | } else { |
| 747 | s->cookie = g_strdup(cookie); |
| 748 | } |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 749 | |
Matthew Booth | e3542c6 | 2014-05-14 19:28:41 -0400 | [diff] [blame] | 750 | file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 751 | if (file == NULL) { |
Paolo Bonzini | 2a94fee | 2014-02-17 14:43:57 +0100 | [diff] [blame] | 752 | error_setg(errp, "curl block driver requires an 'url' option"); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 753 | goto out_noclean; |
| 754 | } |
| 755 | |
Max Reitz | 34634ca | 2017-03-31 14:04:31 +0200 | [diff] [blame] | 756 | if (!strstart(file, bs->drv->protocol_name, &protocol_delimiter) || |
| 757 | !strstart(protocol_delimiter, "://", NULL)) |
| 758 | { |
| 759 | error_setg(errp, "%s curl driver cannot handle the URL '%s' (does not " |
| 760 | "start with '%s://')", bs->drv->protocol_name, file, |
| 761 | bs->drv->protocol_name); |
| 762 | goto out_noclean; |
| 763 | } |
| 764 | |
Daniel P. Berrange | 1bff960 | 2016-01-21 14:19:20 +0000 | [diff] [blame] | 765 | s->username = g_strdup(qemu_opt_get(opts, CURL_BLOCK_OPT_USERNAME)); |
| 766 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PASSWORD_SECRET); |
| 767 | |
| 768 | if (secretid) { |
| 769 | s->password = qcrypto_secret_lookup_as_utf8(secretid, errp); |
| 770 | if (!s->password) { |
| 771 | goto out_noclean; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | s->proxyusername = g_strdup( |
| 776 | qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_USERNAME)); |
| 777 | secretid = qemu_opt_get(opts, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET); |
| 778 | if (secretid) { |
| 779 | s->proxypassword = qcrypto_secret_lookup_as_utf8(secretid, errp); |
| 780 | if (!s->proxypassword) { |
| 781 | goto out_noclean; |
| 782 | } |
| 783 | } |
| 784 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 785 | trace_curl_open(file); |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 786 | qemu_co_queue_init(&s->free_state_waitq); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 787 | s->aio_context = bdrv_get_aio_context(bs); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 788 | s->url = g_strdup(file); |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 789 | s->sockets = g_hash_table_new_full(NULL, NULL, NULL, g_free); |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 790 | qemu_mutex_lock(&s->mutex); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 791 | state = curl_find_state(s); |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 792 | qemu_mutex_unlock(&s->mutex); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 793 | if (!state) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 794 | goto out_noclean; |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 795 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 796 | |
| 797 | // Get file size |
| 798 | |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 799 | if (curl_init_state(s, state) < 0) { |
Peter Maydell | 2ea7dfc | 2022-02-22 15:23:40 +0000 | [diff] [blame] | 800 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 801 | "curl library initialization failed."); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 802 | goto out; |
| 803 | } |
| 804 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 805 | s->accept_range = false; |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 806 | if (curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1) || |
| 807 | curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION, curl_header_cb) || |
| 808 | curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s)) { |
| 809 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 810 | "curl library initialization failed."); |
| 811 | goto out; |
| 812 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 813 | if (curl_easy_perform(state->curl)) |
| 814 | goto out; |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 815 | /* CURL 7.55.0 deprecates CURLINFO_CONTENT_LENGTH_DOWNLOAD in favour of |
| 816 | * the *_T version which returns a more sensible type for content length. |
| 817 | */ |
| 818 | #if LIBCURL_VERSION_NUM >= 0x073700 |
| 819 | if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl)) { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 820 | goto out; |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 821 | } |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 822 | #else |
| 823 | if (curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl)) { |
| 824 | goto out; |
| 825 | } |
| 826 | #endif |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 827 | /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not |
| 828 | * know or the size is zero. From 7.19.4 CURL returns -1 if size is not |
Stefan Weil | 50d6a8a | 2018-07-12 21:51:20 +0200 | [diff] [blame] | 829 | * known and zero if it is really zero-length file. */ |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 830 | #if LIBCURL_VERSION_NUM >= 0x071304 |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 831 | if (cl < 0) { |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 832 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 833 | "Server didn't report file size."); |
| 834 | goto out; |
| 835 | } |
| 836 | #else |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 837 | if (cl <= 0) { |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 838 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 839 | "Unknown file size or zero-length file."); |
| 840 | goto out; |
| 841 | } |
| 842 | #endif |
| 843 | |
Anton Johansson | e7b8d9d | 2023-01-23 21:14:31 +0100 | [diff] [blame] | 844 | s->len = cl; |
Tomáš Golembiovský | a41c457 | 2016-07-08 13:18:09 +0200 | [diff] [blame] | 845 | |
Fam Zheng | 3494d65 | 2013-07-02 15:19:21 +0800 | [diff] [blame] | 846 | if ((!strncasecmp(s->url, "http://", strlen("http://")) |
| 847 | || !strncasecmp(s->url, "https://", strlen("https://"))) |
| 848 | && !s->accept_range) { |
| 849 | pstrcpy(state->errmsg, CURL_ERROR_SIZE, |
| 850 | "Server does not support 'range' (byte ranges)."); |
| 851 | goto out; |
| 852 | } |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 853 | trace_curl_open_size(s->len); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 854 | |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 855 | qemu_mutex_lock(&s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 856 | curl_clean_state(state); |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 857 | qemu_mutex_unlock(&s->mutex); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 858 | curl_easy_cleanup(state->curl); |
| 859 | state->curl = NULL; |
| 860 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 861 | curl_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 862 | |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 863 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 864 | return 0; |
| 865 | |
| 866 | out: |
Maria Kustova | acd7fdc | 2014-03-18 09:59:18 +0400 | [diff] [blame] | 867 | error_setg(errp, "CURL: Error opening file: %s", state->errmsg); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 868 | curl_easy_cleanup(state->curl); |
| 869 | state->curl = NULL; |
| 870 | out_noclean: |
Paolo Bonzini | 456af34 | 2017-05-15 12:00:55 +0200 | [diff] [blame] | 871 | qemu_mutex_destroy(&s->mutex); |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 872 | g_free(s->cookie); |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 873 | g_free(s->url); |
Jeff Cody | 996922d | 2017-11-07 17:27:23 -0500 | [diff] [blame] | 874 | g_free(s->username); |
| 875 | g_free(s->proxyusername); |
| 876 | g_free(s->proxypassword); |
Hanna Czenczek | 1e84cf7 | 2023-02-06 14:29:49 +0100 | [diff] [blame] | 877 | if (s->sockets) { |
| 878 | curl_drop_all_sockets(s->sockets); |
| 879 | g_hash_table_destroy(s->sockets); |
| 880 | } |
Kevin Wolf | 8e6d58c | 2013-04-10 15:31:33 +0200 | [diff] [blame] | 881 | qemu_opts_del(opts); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | } |
| 884 | |
Paolo Bonzini | 9bae2ac | 2022-09-22 10:49:14 +0200 | [diff] [blame] | 885 | static void coroutine_fn curl_setup_preadv(BlockDriverState *bs, CURLAIOCB *acb) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 886 | { |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 887 | CURLState *state; |
Matthew Booth | b69cdef | 2014-04-29 16:03:29 +0100 | [diff] [blame] | 888 | int running; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 889 | |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 890 | BDRVCURLState *s = bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 891 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 892 | uint64_t start = acb->offset; |
| 893 | uint64_t end; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 894 | |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 895 | qemu_mutex_lock(&s->mutex); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 896 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 897 | // In case we have the requested data already (e.g. read-ahead), |
| 898 | // we can just call the callback and be done. |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 899 | if (curl_find_buf(s, start, acb->bytes, acb)) { |
| 900 | goto out; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | // No cache found, so let's start a new request |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 904 | for (;;) { |
| 905 | state = curl_find_state(s); |
| 906 | if (state) { |
| 907 | break; |
| 908 | } |
Paolo Bonzini | 709f213 | 2018-02-03 10:39:35 -0500 | [diff] [blame] | 909 | qemu_co_queue_wait(&s->free_state_waitq, &s->mutex); |
Paolo Bonzini | 3ce6a72 | 2017-05-15 12:00:56 +0200 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | if (curl_init_state(s, state) < 0) { |
| 913 | curl_clean_state(state); |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 914 | acb->ret = -EIO; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 915 | goto out; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 916 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 917 | |
| 918 | acb->start = 0; |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 919 | acb->end = MIN(acb->bytes, s->len - start); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 920 | |
| 921 | state->buf_off = 0; |
Markus Armbruster | f7047c2 | 2014-06-06 18:25:12 +0200 | [diff] [blame] | 922 | g_free(state->orig_buf); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 923 | state->buf_start = start; |
Max Reitz | 4e50453 | 2016-10-25 04:54:31 +0200 | [diff] [blame] | 924 | state->buf_len = MIN(acb->end + s->readahead_size, s->len - start); |
| 925 | end = start + state->buf_len - 1; |
Kevin Wolf | 8dc7a77 | 2014-05-20 13:26:40 +0200 | [diff] [blame] | 926 | state->orig_buf = g_try_malloc(state->buf_len); |
| 927 | if (state->buf_len && state->orig_buf == NULL) { |
| 928 | curl_clean_state(state); |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 929 | acb->ret = -ENOMEM; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 930 | goto out; |
Kevin Wolf | 8dc7a77 | 2014-05-20 13:26:40 +0200 | [diff] [blame] | 931 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 932 | state->acb[0] = acb; |
| 933 | |
Paolo Bonzini | 2125e5e | 2017-05-15 12:00:57 +0200 | [diff] [blame] | 934 | snprintf(state->range, 127, "%" PRIu64 "-%" PRIu64, start, end); |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 935 | trace_curl_setup_preadv(acb->bytes, start, state->range); |
Peter Maydell | b0ea6c9 | 2022-02-22 15:23:41 +0000 | [diff] [blame] | 936 | if (curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range) || |
| 937 | curl_multi_add_handle(s->multi, state->curl) != CURLM_OK) { |
Max Reitz | c34dc07 | 2019-09-10 14:41:36 +0200 | [diff] [blame] | 938 | state->acb[0] = NULL; |
| 939 | acb->ret = -EIO; |
| 940 | |
| 941 | curl_clean_state(state); |
| 942 | goto out; |
| 943 | } |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 944 | |
Matthew Booth | b69cdef | 2014-04-29 16:03:29 +0100 | [diff] [blame] | 945 | /* Tell curl it needs to kick things off */ |
| 946 | curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running); |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 947 | |
| 948 | out: |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 949 | qemu_mutex_unlock(&s->mutex); |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 950 | } |
| 951 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 952 | static int coroutine_fn curl_co_preadv(BlockDriverState *bs, |
Vladimir Sementsov-Ogievskiy | f7ef38d | 2021-09-03 13:27:59 +0300 | [diff] [blame] | 953 | int64_t offset, int64_t bytes, QEMUIOVector *qiov, |
| 954 | BdrvRequestFlags flags) |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 955 | { |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 956 | CURLAIOCB acb = { |
| 957 | .co = qemu_coroutine_self(), |
| 958 | .ret = -EINPROGRESS, |
| 959 | .qiov = qiov, |
| 960 | .offset = offset, |
| 961 | .bytes = bytes |
| 962 | }; |
Nick Thomas | 363c3c8 | 2011-09-21 11:55:50 +0100 | [diff] [blame] | 963 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 964 | curl_setup_preadv(bs, &acb); |
| 965 | while (acb.ret == -EINPROGRESS) { |
| 966 | qemu_coroutine_yield(); |
| 967 | } |
| 968 | return acb.ret; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 971 | static void curl_close(BlockDriverState *bs) |
| 972 | { |
| 973 | BDRVCURLState *s = bs->opaque; |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 974 | |
Laurent Vivier | ed2a66d | 2018-12-13 17:27:25 +0100 | [diff] [blame] | 975 | trace_curl_close(); |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 976 | curl_detach_aio_context(bs); |
Paolo Bonzini | ba3186c | 2017-02-22 19:07:23 +0100 | [diff] [blame] | 977 | qemu_mutex_destroy(&s->mutex); |
Peter Maydell | 031fd1b | 2014-01-24 14:56:17 +0100 | [diff] [blame] | 978 | |
Max Reitz | 0f418a2 | 2021-03-09 14:05:41 +0100 | [diff] [blame] | 979 | g_hash_table_destroy(s->sockets); |
Richard W.M. Jones | a94f83d | 2014-08-29 16:03:12 +0100 | [diff] [blame] | 980 | g_free(s->cookie); |
Stefan Weil | 45724d6 | 2012-09-01 11:06:45 +0200 | [diff] [blame] | 981 | g_free(s->url); |
Jeff Cody | 996922d | 2017-11-07 17:27:23 -0500 | [diff] [blame] | 982 | g_free(s->username); |
| 983 | g_free(s->proxyusername); |
| 984 | g_free(s->proxypassword); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 987 | static int64_t coroutine_fn curl_co_getlength(BlockDriverState *bs) |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 988 | { |
| 989 | BDRVCURLState *s = bs->opaque; |
| 990 | return s->len; |
| 991 | } |
| 992 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 993 | static void curl_refresh_filename(BlockDriverState *bs) |
| 994 | { |
| 995 | BDRVCURLState *s = bs->opaque; |
| 996 | |
| 997 | /* "readahead" and "timeout" do not change the guest-visible data, |
| 998 | * so ignore them */ |
| 999 | if (s->sslverify != CURL_BLOCK_OPT_SSLVERIFY_DEFAULT || |
| 1000 | s->cookie || s->username || s->password || s->proxyusername || |
| 1001 | s->proxypassword) |
| 1002 | { |
| 1003 | return; |
| 1004 | } |
| 1005 | |
| 1006 | pstrcpy(bs->exact_filename, sizeof(bs->exact_filename), s->url); |
| 1007 | } |
| 1008 | |
| 1009 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1010 | static const char *const curl_strong_runtime_opts[] = { |
| 1011 | CURL_BLOCK_OPT_URL, |
| 1012 | CURL_BLOCK_OPT_SSLVERIFY, |
| 1013 | CURL_BLOCK_OPT_COOKIE, |
| 1014 | CURL_BLOCK_OPT_COOKIE_SECRET, |
| 1015 | CURL_BLOCK_OPT_USERNAME, |
| 1016 | CURL_BLOCK_OPT_PASSWORD_SECRET, |
| 1017 | CURL_BLOCK_OPT_PROXY_USERNAME, |
| 1018 | CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET, |
| 1019 | |
| 1020 | NULL |
| 1021 | }; |
| 1022 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1023 | static BlockDriver bdrv_http = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1024 | .format_name = "http", |
| 1025 | .protocol_name = "http", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1026 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1027 | .instance_size = sizeof(BDRVCURLState), |
| 1028 | .bdrv_parse_filename = curl_parse_filename, |
Paolo Bonzini | 44b424d | 2022-11-24 16:22:22 +0100 | [diff] [blame] | 1029 | .bdrv_open = curl_open, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1030 | .bdrv_close = curl_close, |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 1031 | .bdrv_co_getlength = curl_co_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1032 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 1033 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1034 | |
| 1035 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 1036 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1037 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 1038 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1039 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1040 | }; |
| 1041 | |
| 1042 | static BlockDriver bdrv_https = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1043 | .format_name = "https", |
| 1044 | .protocol_name = "https", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1045 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1046 | .instance_size = sizeof(BDRVCURLState), |
| 1047 | .bdrv_parse_filename = curl_parse_filename, |
Paolo Bonzini | 44b424d | 2022-11-24 16:22:22 +0100 | [diff] [blame] | 1048 | .bdrv_open = curl_open, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1049 | .bdrv_close = curl_close, |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 1050 | .bdrv_co_getlength = curl_co_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1051 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 1052 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1053 | |
| 1054 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 1055 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1056 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 1057 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1058 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1059 | }; |
| 1060 | |
| 1061 | static BlockDriver bdrv_ftp = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1062 | .format_name = "ftp", |
| 1063 | .protocol_name = "ftp", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1064 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1065 | .instance_size = sizeof(BDRVCURLState), |
| 1066 | .bdrv_parse_filename = curl_parse_filename, |
Paolo Bonzini | 44b424d | 2022-11-24 16:22:22 +0100 | [diff] [blame] | 1067 | .bdrv_open = curl_open, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1068 | .bdrv_close = curl_close, |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 1069 | .bdrv_co_getlength = curl_co_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1070 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 1071 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1072 | |
| 1073 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 1074 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1075 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 1076 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1077 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1078 | }; |
| 1079 | |
| 1080 | static BlockDriver bdrv_ftps = { |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1081 | .format_name = "ftps", |
| 1082 | .protocol_name = "ftps", |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1083 | |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1084 | .instance_size = sizeof(BDRVCURLState), |
| 1085 | .bdrv_parse_filename = curl_parse_filename, |
Paolo Bonzini | 44b424d | 2022-11-24 16:22:22 +0100 | [diff] [blame] | 1086 | .bdrv_open = curl_open, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1087 | .bdrv_close = curl_close, |
Emanuele Giuseppe Esposito | c86422c | 2023-01-13 21:42:04 +0100 | [diff] [blame] | 1088 | .bdrv_co_getlength = curl_co_getlength, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1089 | |
Paolo Bonzini | 28256d8 | 2017-05-15 12:00:58 +0200 | [diff] [blame] | 1090 | .bdrv_co_preadv = curl_co_preadv, |
Stefan Hajnoczi | 63f0f45 | 2014-05-08 16:34:40 +0200 | [diff] [blame] | 1091 | |
| 1092 | .bdrv_detach_aio_context = curl_detach_aio_context, |
| 1093 | .bdrv_attach_aio_context = curl_attach_aio_context, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1094 | |
Max Reitz | 937c007 | 2019-02-01 20:29:32 +0100 | [diff] [blame] | 1095 | .bdrv_refresh_filename = curl_refresh_filename, |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 1096 | .strong_runtime_opts = curl_strong_runtime_opts, |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1097 | }; |
| 1098 | |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1099 | static void curl_block_init(void) |
| 1100 | { |
| 1101 | bdrv_register(&bdrv_http); |
| 1102 | bdrv_register(&bdrv_https); |
| 1103 | bdrv_register(&bdrv_ftp); |
| 1104 | bdrv_register(&bdrv_ftps); |
Alexander Graf | 769ce76 | 2009-05-11 17:41:42 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | block_init(curl_block_init); |