| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This software is licensed as described in the file COPYING, which |
| 11 |
* you should have received as part of this distribution. The terms |
| 12 |
* are also available at https://curl.haxx.se/docs/copyright.html. |
| 13 |
* |
| 14 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 |
* copies of the Software, and permit persons to whom the Software is |
| 16 |
* furnished to do so, under the terms of the COPYING file. |
| 17 |
* |
| 18 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 |
* KIND, either express or implied. |
| 20 |
* |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include "curl_setup.h" |
| 24 |
|
| 25 |
#ifdef USE_NGTCP2 |
| 26 |
#include <ngtcp2/ngtcp2.h> |
| 27 |
#include <ngtcp2/ngtcp2_crypto.h> |
| 28 |
#include <nghttp3/nghttp3.h> |
| 29 |
#include <openssl/err.h> |
| 30 |
#include "urldata.h" |
| 31 |
#include "sendf.h" |
| 32 |
#include "strdup.h" |
| 33 |
#include "rand.h" |
| 34 |
#include "ngtcp2.h" |
| 35 |
#include "multiif.h" |
| 36 |
#include "strcase.h" |
| 37 |
#include "connect.h" |
| 38 |
#include "strerror.h" |
| 39 |
|
| 40 |
/* The last 3 #include files should be in this order */ |
| 41 |
#include "curl_printf.h" |
| 42 |
#include "curl_memory.h" |
| 43 |
#include "memdebug.h" |
| 44 |
|
| 45 |
/* #define DEBUG_NGTCP2 */ |
| 46 |
#ifdef CURLDEBUG |
| 47 |
#define DEBUG_HTTP3 |
| 48 |
#endif |
| 49 |
#ifdef DEBUG_HTTP3 |
| 50 |
#define H3BUGF(x) x |
| 51 |
#else |
| 52 |
#define H3BUGF(x) do { } while(0) |
| 53 |
#endif |
| 54 |
|
| 55 |
/* |
| 56 |
* This holds outgoing HTTP/3 stream data that is used by nghttp3 until acked. |
| 57 |
* It is used as a circular buffer. Add new bytes at the end until it reaches |
| 58 |
* the far end, then start over at index 0 again. |
| 59 |
*/ |
| 60 |
|
| 61 |
#define H3_SEND_SIZE (20*1024) |
| 62 |
struct h3out { |
| 63 |
uint8_t buf[H3_SEND_SIZE]; |
| 64 |
size_t used; /* number of bytes used in the buffer */ |
| 65 |
size_t windex; /* index in the buffer where to start writing the next |
| 66 |
data block */ |
| 67 |
}; |
| 68 |
|
| 69 |
#define QUIC_MAX_STREAMS (256*1024) |
| 70 |
#define QUIC_MAX_DATA (1*1024*1024) |
| 71 |
#define QUIC_IDLE_TIMEOUT 60000 /* milliseconds */ |
| 72 |
#define QUIC_CIPHERS \ |
| 73 |
"TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_" \ |
| 74 |
"POLY1305_SHA256:TLS_AES_128_CCM_SHA256" |
| 75 |
#define QUIC_GROUPS "P-256:X25519:P-384:P-521" |
| 76 |
|
| 77 |
static CURLcode ng_process_ingress(struct connectdata *conn, |
| 78 |
curl_socket_t sockfd, |
| 79 |
struct quicsocket *qs); |
| 80 |
static CURLcode ng_flush_egress(struct connectdata *conn, int sockfd, |
| 81 |
struct quicsocket *qs); |
| 82 |
static int cb_h3_acked_stream_data(nghttp3_conn *conn, int64_t stream_id, |
| 83 |
size_t datalen, void *user_data, |
| 84 |
void *stream_user_data); |
| 85 |
|
| 86 |
static ngtcp2_tstamp timestamp(void) |
| 87 |
{ |
| 88 |
struct curltime ct = Curl_now(); |
| 89 |
return ct.tv_sec * NGTCP2_SECONDS + ct.tv_usec * NGTCP2_MICROSECONDS; |
| 90 |
} |
| 91 |
|
| 92 |
#ifdef DEBUG_NGTCP2 |
| 93 |
static void quic_printf(void *user_data, const char *fmt, ...) |
| 94 |
{ |
| 95 |
va_list ap; |
| 96 |
(void)user_data; /* TODO, use this to do infof() instead long-term */ |
| 97 |
va_start(ap, fmt); |
| 98 |
vfprintf(stderr, fmt, ap); |
| 99 |
va_end(ap); |
| 100 |
fprintf(stderr, "\n"); |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
static ngtcp2_crypto_level |
| 105 |
quic_from_ossl_level(OSSL_ENCRYPTION_LEVEL ossl_level) |
| 106 |
{ |
| 107 |
switch(ossl_level) { |
| 108 |
case ssl_encryption_initial: |
| 109 |
return NGTCP2_CRYPTO_LEVEL_INITIAL; |
| 110 |
case ssl_encryption_early_data: |
| 111 |
return NGTCP2_CRYPTO_LEVEL_EARLY; |
| 112 |
case ssl_encryption_handshake: |
| 113 |
return NGTCP2_CRYPTO_LEVEL_HANDSHAKE; |
| 114 |
case ssl_encryption_application: |
| 115 |
return NGTCP2_CRYPTO_LEVEL_APP; |
| 116 |
default: |
| 117 |
assert(0); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static int setup_initial_crypto_context(struct quicsocket *qs) |
| 122 |
{ |
| 123 |
const ngtcp2_cid *dcid = ngtcp2_conn_get_dcid(qs->qconn); |
| 124 |
|
| 125 |
if(ngtcp2_crypto_derive_and_install_initial_key( |
| 126 |
qs->qconn, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, dcid, |
| 127 |
NGTCP2_CRYPTO_SIDE_CLIENT) != 0) |
| 128 |
return -1; |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
static void quic_settings(ngtcp2_settings *s, |
| 134 |
uint64_t stream_buffer_size) |
| 135 |
{ |
| 136 |
ngtcp2_settings_default(s); |
| 137 |
#ifdef DEBUG_NGTCP2 |
| 138 |
s->log_printf = quic_printf; |
| 139 |
#else |
| 140 |
s->log_printf = NULL; |
| 141 |
#endif |
| 142 |
s->initial_ts = timestamp(); |
| 143 |
s->transport_params.initial_max_stream_data_bidi_local = stream_buffer_size; |
| 144 |
s->transport_params.initial_max_stream_data_bidi_remote = QUIC_MAX_STREAMS; |
| 145 |
s->transport_params.initial_max_stream_data_uni = QUIC_MAX_STREAMS; |
| 146 |
s->transport_params.initial_max_data = QUIC_MAX_DATA; |
| 147 |
s->transport_params.initial_max_streams_bidi = 1; |
| 148 |
s->transport_params.initial_max_streams_uni = 3; |
| 149 |
s->transport_params.max_idle_timeout = QUIC_IDLE_TIMEOUT; |
| 150 |
} |
| 151 |
|
| 152 |
static FILE *keylog_file; /* not thread-safe */ |
| 153 |
static void keylog_callback(const SSL *ssl, const char *line) |
| 154 |
{ |
| 155 |
(void)ssl; |
| 156 |
fputs(line, keylog_file); |
| 157 |
fputc('\n', keylog_file); |
| 158 |
fflush(keylog_file); |
| 159 |
} |
| 160 |
|
| 161 |
static int init_ngh3_conn(struct quicsocket *qs); |
| 162 |
|
| 163 |
static int quic_set_encryption_secrets(SSL *ssl, |
| 164 |
OSSL_ENCRYPTION_LEVEL ossl_level, |
| 165 |
const uint8_t *rx_secret, |
| 166 |
const uint8_t *tx_secret, |
| 167 |
size_t secretlen) |
| 168 |
{ |
| 169 |
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl); |
| 170 |
int level = quic_from_ossl_level(ossl_level); |
| 171 |
|
| 172 |
if(ngtcp2_crypto_derive_and_install_key( |
| 173 |
qs->qconn, ssl, NULL, NULL, NULL, NULL, NULL, NULL, level, rx_secret, |
| 174 |
tx_secret, secretlen, NGTCP2_CRYPTO_SIDE_CLIENT) != 0) |
| 175 |
return 0; |
| 176 |
|
| 177 |
if(level == NGTCP2_CRYPTO_LEVEL_APP) { |
| 178 |
if(init_ngh3_conn(qs) != CURLE_OK) |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
return 1; |
| 183 |
} |
| 184 |
|
| 185 |
static int quic_add_handshake_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level, |
| 186 |
const uint8_t *data, size_t len) |
| 187 |
{ |
| 188 |
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl); |
| 189 |
struct quic_handshake *crypto_data; |
| 190 |
ngtcp2_crypto_level level = quic_from_ossl_level(ossl_level); |
| 191 |
int rv; |
| 192 |
|
| 193 |
crypto_data = &qs->crypto_data[level]; |
| 194 |
if(crypto_data->buf == NULL) { |
| 195 |
crypto_data->buf = malloc(4096); |
| 196 |
if(!crypto_data->buf) |
| 197 |
return 0; |
| 198 |
crypto_data->alloclen = 4096; |
| 199 |
} |
| 200 |
|
| 201 |
/* TODO Just pretend that handshake does not grow more than 4KiB for |
| 202 |
now */ |
| 203 |
assert(crypto_data->len + len <= crypto_data->alloclen); |
| 204 |
|
| 205 |
memcpy(&crypto_data->buf[crypto_data->len], data, len); |
| 206 |
crypto_data->len += len; |
| 207 |
|
| 208 |
rv = ngtcp2_conn_submit_crypto_data( |
| 209 |
qs->qconn, level, (uint8_t *)(&crypto_data->buf[crypto_data->len] - len), |
| 210 |
len); |
| 211 |
if(rv) { |
| 212 |
H3BUGF(fprintf(stderr, "write_client_handshake failed\n")); |
| 213 |
} |
| 214 |
assert(0 == rv); |
| 215 |
|
| 216 |
return 1; |
| 217 |
} |
| 218 |
|
| 219 |
static int quic_flush_flight(SSL *ssl) |
| 220 |
{ |
| 221 |
(void)ssl; |
| 222 |
return 1; |
| 223 |
} |
| 224 |
|
| 225 |
static int quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level, |
| 226 |
uint8_t alert) |
| 227 |
{ |
| 228 |
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl); |
| 229 |
(void)level; |
| 230 |
|
| 231 |
qs->tls_alert = alert; |
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
static SSL_QUIC_METHOD quic_method = {quic_set_encryption_secrets, |
| 236 |
quic_add_handshake_data, |
| 237 |
quic_flush_flight, quic_send_alert}; |
| 238 |
|
| 239 |
static SSL_CTX *quic_ssl_ctx(struct Curl_easy *data) |
| 240 |
{ |
| 241 |
SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method()); |
| 242 |
const char *keylog_filename; |
| 243 |
|
| 244 |
SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_3_VERSION); |
| 245 |
SSL_CTX_set_max_proto_version(ssl_ctx, TLS1_3_VERSION); |
| 246 |
|
| 247 |
SSL_CTX_set_default_verify_paths(ssl_ctx); |
| 248 |
|
| 249 |
if(SSL_CTX_set_ciphersuites(ssl_ctx, QUIC_CIPHERS) != 1) { |
| 250 |
char error_buffer[256]; |
| 251 |
ERR_error_string_n(ERR_get_error(), error_buffer, sizeof(error_buffer)); |
| 252 |
failf(data, "SSL_CTX_set_ciphersuites: %s", error_buffer); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
if(SSL_CTX_set1_groups_list(ssl_ctx, QUIC_GROUPS) != 1) { |
| 257 |
failf(data, "SSL_CTX_set1_groups_list failed"); |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
|
| 261 |
SSL_CTX_set_quic_method(ssl_ctx, &quic_method); |
| 262 |
|
| 263 |
keylog_filename = getenv("SSLKEYLOGFILE"); |
| 264 |
if(keylog_filename) { |
| 265 |
keylog_file = fopen(keylog_filename, "wb"); |
| 266 |
if(keylog_file) { |
| 267 |
SSL_CTX_set_keylog_callback(ssl_ctx, keylog_callback); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
return ssl_ctx; |
| 272 |
} |
| 273 |
|
| 274 |
/** SSL callbacks ***/ |
| 275 |
|
| 276 |
static int quic_init_ssl(struct quicsocket *qs) |
| 277 |
{ |
| 278 |
const uint8_t *alpn = NULL; |
| 279 |
size_t alpnlen = 0; |
| 280 |
/* this will need some attention when HTTPS proxy over QUIC get fixed */ |
| 281 |
const char * const hostname = qs->conn->host.name; |
| 282 |
|
| 283 |
if(qs->ssl) |
| 284 |
SSL_free(qs->ssl); |
| 285 |
|
| 286 |
qs->ssl = SSL_new(qs->sslctx); |
| 287 |
|
| 288 |
SSL_set_app_data(qs->ssl, qs); |
| 289 |
SSL_set_connect_state(qs->ssl); |
| 290 |
|
| 291 |
switch(qs->version) { |
| 292 |
#ifdef NGTCP2_PROTO_VER |
| 293 |
case NGTCP2_PROTO_VER: |
| 294 |
alpn = (const uint8_t *)NGTCP2_ALPN_H3; |
| 295 |
alpnlen = sizeof(NGTCP2_ALPN_H3) - 1; |
| 296 |
break; |
| 297 |
#endif |
| 298 |
} |
| 299 |
if(alpn) |
| 300 |
SSL_set_alpn_protos(qs->ssl, alpn, (int)alpnlen); |
| 301 |
|
| 302 |
/* set SNI */ |
| 303 |
SSL_set_tlsext_host_name(qs->ssl, hostname); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
|
| 307 |
static int cb_initial(ngtcp2_conn *quic, void *user_data) |
| 308 |
{ |
| 309 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 310 |
|
| 311 |
if(ngtcp2_crypto_read_write_crypto_data( |
| 312 |
quic, qs->ssl, NGTCP2_CRYPTO_LEVEL_INITIAL, NULL, 0) != 0) |
| 313 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
static int |
| 319 |
cb_recv_crypto_data(ngtcp2_conn *tconn, ngtcp2_crypto_level crypto_level, |
| 320 |
uint64_t offset, |
| 321 |
const uint8_t *data, size_t datalen, |
| 322 |
void *user_data) |
| 323 |
{ |
| 324 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 325 |
(void)offset; |
| 326 |
|
| 327 |
if(ngtcp2_crypto_read_write_crypto_data(tconn, qs->ssl, crypto_level, data, |
| 328 |
datalen) != 0) |
| 329 |
return NGTCP2_ERR_CRYPTO; |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
static int cb_handshake_completed(ngtcp2_conn *tconn, void *user_data) |
| 335 |
{ |
| 336 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 337 |
(void)tconn; |
| 338 |
infof(qs->conn->data, "QUIC handshake is completed\n"); |
| 339 |
|
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
static void extend_stream_window(ngtcp2_conn *tconn, |
| 344 |
struct HTTP *stream) |
| 345 |
{ |
| 346 |
size_t thismuch = stream->unacked_window; |
| 347 |
ngtcp2_conn_extend_max_stream_offset(tconn, stream->stream3_id, thismuch); |
| 348 |
ngtcp2_conn_extend_max_offset(tconn, thismuch); |
| 349 |
stream->unacked_window = 0; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
static int cb_recv_stream_data(ngtcp2_conn *tconn, int64_t stream_id, |
| 354 |
int fin, uint64_t offset, |
| 355 |
const uint8_t *buf, size_t buflen, |
| 356 |
void *user_data, void *stream_user_data) |
| 357 |
{ |
| 358 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 359 |
ssize_t nconsumed; |
| 360 |
(void)offset; |
| 361 |
(void)stream_user_data; |
| 362 |
|
| 363 |
nconsumed = |
| 364 |
nghttp3_conn_read_stream(qs->h3conn, stream_id, buf, buflen, fin); |
| 365 |
if(nconsumed < 0) { |
| 366 |
failf(qs->conn->data, "nghttp3_conn_read_stream returned error: %s\n", |
| 367 |
nghttp3_strerror((int)nconsumed)); |
| 368 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 369 |
} |
| 370 |
|
| 371 |
/* number of bytes inside buflen which consists of framing overhead |
| 372 |
* including QPACK HEADERS. In other words, it does not consume payload of |
| 373 |
* DATA frame. */ |
| 374 |
ngtcp2_conn_extend_max_stream_offset(tconn, stream_id, nconsumed); |
| 375 |
ngtcp2_conn_extend_max_offset(tconn, nconsumed); |
| 376 |
|
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
static int |
| 381 |
cb_acked_stream_data_offset(ngtcp2_conn *tconn, int64_t stream_id, |
| 382 |
uint64_t offset, size_t datalen, void *user_data, |
| 383 |
void *stream_user_data) |
| 384 |
{ |
| 385 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 386 |
int rv; |
| 387 |
(void)stream_id; |
| 388 |
(void)tconn; |
| 389 |
(void)offset; |
| 390 |
(void)datalen; |
| 391 |
(void)stream_user_data; |
| 392 |
|
| 393 |
rv = nghttp3_conn_add_ack_offset(qs->h3conn, stream_id, datalen); |
| 394 |
if(rv != 0) { |
| 395 |
failf(qs->conn->data, "nghttp3_conn_add_ack_offset returned error: %s\n", |
| 396 |
nghttp3_strerror(rv)); |
| 397 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 398 |
} |
| 399 |
|
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
static int cb_stream_close(ngtcp2_conn *tconn, int64_t stream_id, |
| 404 |
uint64_t app_error_code, |
| 405 |
void *user_data, void *stream_user_data) |
| 406 |
{ |
| 407 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 408 |
int rv; |
| 409 |
(void)tconn; |
| 410 |
(void)stream_user_data; |
| 411 |
/* stream is closed... */ |
| 412 |
|
| 413 |
rv = nghttp3_conn_close_stream(qs->h3conn, stream_id, |
| 414 |
app_error_code); |
| 415 |
if(rv != 0) { |
| 416 |
failf(qs->conn->data, "nghttp3_conn_close_stream returned error: %s\n", |
| 417 |
nghttp3_strerror(rv)); |
| 418 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 419 |
} |
| 420 |
|
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
static int cb_stream_reset(ngtcp2_conn *tconn, int64_t stream_id, |
| 425 |
uint64_t final_size, uint64_t app_error_code, |
| 426 |
void *user_data, void *stream_user_data) |
| 427 |
{ |
| 428 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 429 |
int rv; |
| 430 |
(void)tconn; |
| 431 |
(void)final_size; |
| 432 |
(void)app_error_code; |
| 433 |
(void)stream_user_data; |
| 434 |
|
| 435 |
rv = nghttp3_conn_reset_stream(qs->h3conn, stream_id); |
| 436 |
if(rv != 0) { |
| 437 |
failf(qs->conn->data, "nghttp3_conn_reset_stream returned error: %s\n", |
| 438 |
nghttp3_strerror(rv)); |
| 439 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 440 |
} |
| 441 |
|
| 442 |
return 0; |
| 443 |
} |
| 444 |
|
| 445 |
static int cb_recv_retry(ngtcp2_conn *tconn, const ngtcp2_pkt_hd *hd, |
| 446 |
const ngtcp2_pkt_retry *retry, void *user_data) |
| 447 |
{ |
| 448 |
/* Re-generate handshake secrets here because connection ID might change. */ |
| 449 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 450 |
(void)tconn; |
| 451 |
(void)hd; |
| 452 |
(void)retry; |
| 453 |
|
| 454 |
setup_initial_crypto_context(qs); |
| 455 |
|
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static int cb_extend_max_local_streams_bidi(ngtcp2_conn *tconn, |
| 460 |
uint64_t max_streams, |
| 461 |
void *user_data) |
| 462 |
{ |
| 463 |
(void)tconn; |
| 464 |
(void)max_streams; |
| 465 |
(void)user_data; |
| 466 |
|
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
static int cb_extend_max_stream_data(ngtcp2_conn *tconn, int64_t stream_id, |
| 471 |
uint64_t max_data, void *user_data, |
| 472 |
void *stream_user_data) |
| 473 |
{ |
| 474 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 475 |
int rv; |
| 476 |
(void)tconn; |
| 477 |
(void)max_data; |
| 478 |
(void)stream_user_data; |
| 479 |
|
| 480 |
rv = nghttp3_conn_unblock_stream(qs->h3conn, stream_id); |
| 481 |
if(rv != 0) { |
| 482 |
failf(qs->conn->data, "nghttp3_conn_unblock_stream returned error: %s\n", |
| 483 |
nghttp3_strerror(rv)); |
| 484 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 485 |
} |
| 486 |
|
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
static int cb_get_new_connection_id(ngtcp2_conn *tconn, ngtcp2_cid *cid, |
| 491 |
uint8_t *token, size_t cidlen, |
| 492 |
void *user_data) |
| 493 |
{ |
| 494 |
struct quicsocket *qs = (struct quicsocket *)user_data; |
| 495 |
CURLcode result; |
| 496 |
(void)tconn; |
| 497 |
|
| 498 |
result = Curl_rand(qs->conn->data, cid->data, cidlen); |
| 499 |
if(result) |
| 500 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 501 |
cid->datalen = cidlen; |
| 502 |
|
| 503 |
result = Curl_rand(qs->conn->data, token, NGTCP2_STATELESS_RESET_TOKENLEN); |
| 504 |
if(result) |
| 505 |
return NGTCP2_ERR_CALLBACK_FAILURE; |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
static ngtcp2_conn_callbacks ng_callbacks = { |
| 511 |
cb_initial, |
| 512 |
NULL, /* recv_client_initial */ |
| 513 |
cb_recv_crypto_data, |
| 514 |
cb_handshake_completed, |
| 515 |
NULL, /* recv_version_negotiation */ |
| 516 |
ngtcp2_crypto_encrypt_cb, |
| 517 |
ngtcp2_crypto_decrypt_cb, |
| 518 |
ngtcp2_crypto_hp_mask_cb, |
| 519 |
cb_recv_stream_data, |
| 520 |
NULL, /* acked_crypto_offset */ |
| 521 |
cb_acked_stream_data_offset, |
| 522 |
NULL, /* stream_open */ |
| 523 |
cb_stream_close, |
| 524 |
NULL, /* recv_stateless_reset */ |
| 525 |
cb_recv_retry, |
| 526 |
cb_extend_max_local_streams_bidi, |
| 527 |
NULL, /* extend_max_local_streams_uni */ |
| 528 |
NULL, /* rand */ |
| 529 |
cb_get_new_connection_id, |
| 530 |
NULL, /* remove_connection_id */ |
| 531 |
ngtcp2_crypto_update_key_cb, /* update_key */ |
| 532 |
NULL, /* path_validation */ |
| 533 |
NULL, /* select_preferred_addr */ |
| 534 |
cb_stream_reset, |
| 535 |
NULL, /* extend_max_remote_streams_bidi */ |
| 536 |
NULL, /* extend_max_remote_streams_uni */ |
| 537 |
cb_extend_max_stream_data, |
| 538 |
NULL, /* dcid_status */ |
| 539 |
NULL /* handshake_confirmed */ |
| 540 |
}; |
| 541 |
|
| 542 |
/* |
| 543 |
* Might be called twice for happy eyeballs. |
| 544 |
*/ |
| 545 |
CURLcode Curl_quic_connect(struct connectdata *conn, |
| 546 |
curl_socket_t sockfd, |
| 547 |
int sockindex, |
| 548 |
const struct sockaddr *addr, |
| 549 |
socklen_t addrlen) |
| 550 |
{ |
| 551 |
int rc; |
| 552 |
int rv; |
| 553 |
CURLcode result; |
| 554 |
ngtcp2_path path; /* TODO: this must be initialized properly */ |
| 555 |
struct Curl_easy *data = conn->data; |
| 556 |
struct quicsocket *qs = &conn->hequic[sockindex]; |
| 557 |
char ipbuf[40]; |
| 558 |
long port; |
| 559 |
uint8_t paramsbuf[64]; |
| 560 |
ngtcp2_transport_params params; |
| 561 |
ssize_t nwrite; |
| 562 |
|
| 563 |
qs->conn = conn; |
| 564 |
|
| 565 |
/* extract the used address as a string */ |
| 566 |
if(!Curl_addr2string((struct sockaddr*)addr, addrlen, ipbuf, &port)) { |
| 567 |
char buffer[STRERROR_LEN]; |
| 568 |
failf(data, "ssrem inet_ntop() failed with errno %d: %s", |
| 569 |
SOCKERRNO, Curl_strerror(SOCKERRNO, buffer, sizeof(buffer))); |
| 570 |
return CURLE_BAD_FUNCTION_ARGUMENT; |
| 571 |
} |
| 572 |
|
| 573 |
infof(data, "Connect socket %d over QUIC to %s:%ld\n", |
| 574 |
sockfd, ipbuf, port); |
| 575 |
|
| 576 |
qs->version = NGTCP2_PROTO_VER; |
| 577 |
qs->sslctx = quic_ssl_ctx(data); |
| 578 |
if(!qs->sslctx) |
| 579 |
return CURLE_QUIC_CONNECT_ERROR; |
| 580 |
|
| 581 |
if(quic_init_ssl(qs)) |
| 582 |
return CURLE_QUIC_CONNECT_ERROR; |
| 583 |
|
| 584 |
qs->dcid.datalen = NGTCP2_MAX_CIDLEN; |
| 585 |
result = Curl_rand(data, qs->dcid.data, NGTCP2_MAX_CIDLEN); |
| 586 |
if(result) |
| 587 |
return result; |
| 588 |
|
| 589 |
qs->scid.datalen = NGTCP2_MAX_CIDLEN; |
| 590 |
result = Curl_rand(data, qs->scid.data, NGTCP2_MAX_CIDLEN); |
| 591 |
if(result) |
| 592 |
return result; |
| 593 |
|
| 594 |
quic_settings(&qs->settings, data->set.buffer_size); |
| 595 |
|
| 596 |
qs->local_addrlen = sizeof(qs->local_addr); |
| 597 |
rv = getsockname(sockfd, (struct sockaddr *)&qs->local_addr, |
| 598 |
&qs->local_addrlen); |
| 599 |
if(rv == -1) |
| 600 |
return CURLE_QUIC_CONNECT_ERROR; |
| 601 |
|
| 602 |
ngtcp2_addr_init(&path.local, (uint8_t *)&qs->local_addr, qs->local_addrlen, |
| 603 |
NULL); |
| 604 |
ngtcp2_addr_init(&path.remote, (uint8_t*)addr, addrlen, NULL); |
| 605 |
|
| 606 |
#ifdef NGTCP2_PROTO_VER |
| 607 |
#define QUICVER NGTCP2_PROTO_VER |
| 608 |
#else |
| 609 |
#error "unsupported ngtcp2 version" |
| 610 |
#endif |
| 611 |
rc = ngtcp2_conn_client_new(&qs->qconn, &qs->dcid, &qs->scid, &path, QUICVER, |
| 612 |
&ng_callbacks, &qs->settings, NULL, qs); |
| 613 |
if(rc) |
| 614 |
return CURLE_QUIC_CONNECT_ERROR; |
| 615 |
|
| 616 |
ngtcp2_conn_get_local_transport_params(qs->qconn, ¶ms); |
| 617 |
nwrite = ngtcp2_encode_transport_params( |
| 618 |
paramsbuf, sizeof(paramsbuf), NGTCP2_TRANSPORT_PARAMS_TYPE_CLIENT_HELLO, |
| 619 |
¶ms); |
| 620 |
if(nwrite < 0) { |
| 621 |
failf(data, "ngtcp2_encode_transport_params: %s\n", |
| 622 |
ngtcp2_strerror((int)nwrite)); |
| 623 |
return CURLE_QUIC_CONNECT_ERROR; |
| 624 |
} |
| 625 |
|
| 626 |
if(!SSL_set_quic_transport_params(qs->ssl, paramsbuf, nwrite)) |
| 627 |
return CURLE_QUIC_CONNECT_ERROR; |
| 628 |
|
| 629 |
rc = setup_initial_crypto_context(qs); |
| 630 |
if(rc) |
| 631 |
return CURLE_QUIC_CONNECT_ERROR; |
| 632 |
|
| 633 |
return CURLE_OK; |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
* Store ngtp2 version info in this buffer, Prefix with a space. Return total |
| 638 |
* length written. |
| 639 |
*/ |
| 640 |
int Curl_quic_ver(char *p, size_t len) |
| 641 |
{ |
| 642 |
ngtcp2_info *ng2 = ngtcp2_version(0); |
| 643 |
nghttp3_info *ht3 = nghttp3_version(0); |
| 644 |
return msnprintf(p, len, " ngtcp2/%s nghttp3/%s", |
| 645 |
ng2->version_str, ht3->version_str); |
| 646 |
} |
| 647 |
|
| 648 |
static int ng_getsock(struct connectdata *conn, curl_socket_t *socks) |
| 649 |
{ |
| 650 |
struct SingleRequest *k = &conn->data->req; |
| 651 |
int bitmap = GETSOCK_BLANK; |
| 652 |
|
| 653 |
socks[0] = conn->sock[FIRSTSOCKET]; |
| 654 |
|
| 655 |
/* in a HTTP/2 connection we can basically always get a frame so we should |
| 656 |
always be ready for one */ |
| 657 |
bitmap |= GETSOCK_READSOCK(FIRSTSOCKET); |
| 658 |
|
| 659 |
/* we're still uploading or the HTTP/2 layer wants to send data */ |
| 660 |
if((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND) |
| 661 |
bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET); |
| 662 |
|
| 663 |
return bitmap; |
| 664 |
} |
| 665 |
|
| 666 |
static int ng_perform_getsock(const struct connectdata *conn, |
| 667 |
curl_socket_t *socks) |
| 668 |
{ |
| 669 |
return ng_getsock((struct connectdata *)conn, socks); |
| 670 |
} |
| 671 |
|
| 672 |
static CURLcode ng_disconnect(struct connectdata *conn, |
| 673 |
bool dead_connection) |
| 674 |
{ |
| 675 |
int i; |
| 676 |
struct quicsocket *qs = &conn->hequic[0]; |
| 677 |
(void)dead_connection; |
| 678 |
if(qs->ssl) |
| 679 |
SSL_free(qs->ssl); |
| 680 |
for(i = 0; i < 3; i++) |
| 681 |
free(qs->crypto_data[i].buf); |
| 682 |
nghttp3_conn_del(qs->h3conn); |
| 683 |
ngtcp2_conn_del(qs->qconn); |
| 684 |
SSL_CTX_free(qs->sslctx); |
| 685 |
return CURLE_OK; |
| 686 |
} |
| 687 |
|
| 688 |
static unsigned int ng_conncheck(struct connectdata *conn, |
| 689 |
unsigned int checks_to_perform) |
| 690 |
{ |
| 691 |
(void)conn; |
| 692 |
(void)checks_to_perform; |
| 693 |
return CONNRESULT_NONE; |
| 694 |
} |
| 695 |
|
| 696 |
static const struct Curl_handler Curl_handler_http3 = { |
| 697 |
"HTTPS", /* scheme */ |
| 698 |
ZERO_NULL, /* setup_connection */ |
| 699 |
Curl_http, /* do_it */ |
| 700 |
Curl_http_done, /* done */ |
| 701 |
ZERO_NULL, /* do_more */ |
| 702 |
ZERO_NULL, /* connect_it */ |
| 703 |
ZERO_NULL, /* connecting */ |
| 704 |
ZERO_NULL, /* doing */ |
| 705 |
ng_getsock, /* proto_getsock */ |
| 706 |
ng_getsock, /* doing_getsock */ |
| 707 |
ZERO_NULL, /* domore_getsock */ |
| 708 |
ng_perform_getsock, /* perform_getsock */ |
| 709 |
ng_disconnect, /* disconnect */ |
| 710 |
ZERO_NULL, /* readwrite */ |
| 711 |
ng_conncheck, /* connection_check */ |
| 712 |
PORT_HTTP, /* defport */ |
| 713 |
CURLPROTO_HTTPS, /* protocol */ |
| 714 |
PROTOPT_SSL | PROTOPT_STREAM /* flags */ |
| 715 |
}; |
| 716 |
|
| 717 |
static int cb_h3_stream_close(nghttp3_conn *conn, int64_t stream_id, |
| 718 |
uint64_t app_error_code, void *user_data, |
| 719 |
void *stream_user_data) |
| 720 |
{ |
| 721 |
struct Curl_easy *data = stream_user_data; |
| 722 |
struct HTTP *stream = data->req.protop; |
| 723 |
(void)conn; |
| 724 |
(void)stream_id; |
| 725 |
(void)app_error_code; |
| 726 |
(void)user_data; |
| 727 |
H3BUGF(infof(data, "cb_h3_stream_close CALLED\n")); |
| 728 |
|
| 729 |
stream->closed = TRUE; |
| 730 |
Curl_expire(data, 0, EXPIRE_QUIC); |
| 731 |
/* make sure that ngh3_stream_recv is called again to complete the transfer |
| 732 |
even if there are no more packets to be received from the server. */ |
| 733 |
data->state.drain = 1; |
| 734 |
return 0; |
| 735 |
} |
| 736 |
|
| 737 |
/* Minimum size of the overflow buffer */ |
| 738 |
#define OVERFLOWSIZE 1024 |
| 739 |
|
| 740 |
/* |
| 741 |
* allocate_overflow() ensures that there is room for incoming data in the |
| 742 |
* overflow buffer, growing it to accommodate the new data if necessary. We |
| 743 |
* may need to use the overflow buffer because we can't precisely limit the |
| 744 |
* amount of HTTP/3 header data we receive using QUIC flow control mechanisms. |
| 745 |
*/ |
| 746 |
static CURLcode allocate_overflow(struct Curl_easy *data, |
| 747 |
struct HTTP *stream, |
| 748 |
size_t length) |
| 749 |
{ |
| 750 |
size_t maxleft; |
| 751 |
size_t newsize; |
| 752 |
/* length can be arbitrarily large, so take care not to overflow newsize */ |
| 753 |
maxleft = CURL_MAX_READ_SIZE - stream->overflow_buflen; |
| 754 |
if(length > maxleft) { |
| 755 |
/* The reason to have a max limit for this is to avoid the risk of a bad |
| 756 |
server feeding libcurl with a highly compressed list of headers that |
| 757 |
will cause our overflow buffer to grow too large */ |
| 758 |
failf(data, "Rejected %zu bytes of overflow data (max is %d)!", |
| 759 |
stream->overflow_buflen + length, CURL_MAX_READ_SIZE); |
| 760 |
return CURLE_OUT_OF_MEMORY; |
| 761 |
} |
| 762 |
newsize = stream->overflow_buflen + length; |
| 763 |
if(newsize > stream->overflow_bufsize) { |
| 764 |
/* We enlarge the overflow buffer as it is too small */ |
| 765 |
char *newbuff; |
| 766 |
newsize = CURLMAX(newsize * 3 / 2, stream->overflow_bufsize*2); |
| 767 |
newsize = CURLMIN(CURLMAX(OVERFLOWSIZE, newsize), CURL_MAX_READ_SIZE); |
| 768 |
newbuff = realloc(stream->overflow_buf, newsize); |
| 769 |
if(!newbuff) { |
| 770 |
failf(data, "Failed to alloc memory for overflow buffer!"); |
| 771 |
return CURLE_OUT_OF_MEMORY; |
| 772 |
} |
| 773 |
stream->overflow_buf = newbuff; |
| 774 |
stream->overflow_bufsize = newsize; |
| 775 |
infof(data, "Grew HTTP/3 overflow buffer to %zu bytes\n", newsize); |
| 776 |
} |
| 777 |
return CURLE_OK; |
| 778 |
} |
| 779 |
|
| 780 |
/* |
| 781 |
* write_data() copies data to the stream's receive buffer. If not enough |
| 782 |
* space is available in the receive buffer, it copies the rest to the |
| 783 |
* stream's overflow buffer. |
| 784 |
*/ |
| 785 |
static CURLcode write_data(struct Curl_easy *data, |
| 786 |
struct HTTP *stream, |
| 787 |
const void *mem, size_t memlen) |
| 788 |
{ |
| 789 |
CURLcode result = CURLE_OK; |
| 790 |
const char *buf = mem; |
| 791 |
size_t ncopy = memlen; |
| 792 |
/* copy as much as possible to the receive buffer */ |
| 793 |
if(stream->len) { |
| 794 |
size_t len = CURLMIN(ncopy, stream->len); |
| 795 |
#if 0 /* extra debugging of incoming h3 data */ |
| 796 |
fprintf(stderr, "!! Copies %zd bytes to %p (total %zd)\n", |
| 797 |
len, stream->mem, stream->memlen); |
| 798 |
#endif |
| 799 |
memcpy(stream->mem, buf, len); |
| 800 |
stream->len -= len; |
| 801 |
stream->memlen += len; |
| 802 |
stream->mem += len; |
| 803 |
buf += len; |
| 804 |
ncopy -= len; |
| 805 |
} |
| 806 |
/* copy the rest to the overflow buffer */ |
| 807 |
if(ncopy) { |
| 808 |
result = allocate_overflow(data, stream, ncopy); |
| 809 |
if(result) { |
| 810 |
return result; |
| 811 |
} |
| 812 |
#if 0 /* extra debugging of incoming h3 data */ |
| 813 |
fprintf(stderr, "!! Copies %zd overflow bytes to %p (total %zd)\n", |
| 814 |
ncopy, stream->overflow_buf, stream->overflow_buflen); |
| 815 |
#endif |
| 816 |
memcpy(stream->overflow_buf + stream->overflow_buflen, buf, ncopy); |
| 817 |
stream->overflow_buflen += ncopy; |
| 818 |
} |
| 819 |
#if 0 /* extra debugging of incoming h3 data */ |
| 820 |
{ |
| 821 |
size_t i; |
| 822 |
for(i = 0; i < memlen; i++) { |
| 823 |
fprintf(stderr, "!! data[%d]: %02x '%c'\n", i, buf[i], buf[i]); |
| 824 |
} |
| 825 |
} |
| 826 |
#endif |
| 827 |
return result; |
| 828 |
} |
| 829 |
|
| 830 |
static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream_id, |
| 831 |
const uint8_t *buf, size_t buflen, |
| 832 |
void *user_data, void *stream_user_data) |
| 833 |
{ |
| 834 |
struct Curl_easy *data = stream_user_data; |
| 835 |
struct HTTP *stream = data->req.protop; |
| 836 |
CURLcode result = CURLE_OK; |
| 837 |
(void)conn; |
| 838 |
|
| 839 |
result = write_data(data, stream, buf, buflen); |
| 840 |
if(result) { |
| 841 |
return -1; |
| 842 |
} |
| 843 |
stream->unacked_window += buflen; |
| 844 |
(void)stream_id; |
| 845 |
(void)user_data; |
| 846 |
return 0; |
| 847 |
} |
| 848 |
|
| 849 |
static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream_id, |
| 850 |
size_t consumed, void *user_data, |
| 851 |
void *stream_user_data) |
| 852 |
{ |
| 853 |
struct quicsocket *qs = user_data; |
| 854 |
(void)conn; |
| 855 |
(void)stream_user_data; |
| 856 |
(void)stream_id; |
| 857 |
|
| 858 |
ngtcp2_conn_extend_max_stream_offset(qs->qconn, stream_id, consumed); |
| 859 |
ngtcp2_conn_extend_max_offset(qs->qconn, consumed); |
| 860 |
return 0; |
| 861 |
} |
| 862 |
|
| 863 |
/* Decode HTTP status code. Returns -1 if no valid status code was |
| 864 |
decoded. (duplicate from http2.c) */ |
| 865 |
static int decode_status_code(const uint8_t *value, size_t len) |
| 866 |
{ |
| 867 |
int i; |
| 868 |
int res; |
| 869 |
|
| 870 |
if(len != 3) { |
| 871 |
return -1; |
| 872 |
} |
| 873 |
|
| 874 |
res = 0; |
| 875 |
|
| 876 |
for(i = 0; i < 3; ++i) { |
| 877 |
char c = value[i]; |
| 878 |
|
| 879 |
if(c < '0' || c > '9') { |
| 880 |
return -1; |
| 881 |
} |
| 882 |
|
| 883 |
res *= 10; |
| 884 |
res += c - '0'; |
| 885 |
} |
| 886 |
|
| 887 |
return res; |
| 888 |
} |
| 889 |
|
| 890 |
static int cb_h3_end_headers(nghttp3_conn *conn, int64_t stream_id, |
| 891 |
void *user_data, void *stream_user_data) |
| 892 |
{ |
| 893 |
struct Curl_easy *data = stream_user_data; |
| 894 |
struct HTTP *stream = data->req.protop; |
| 895 |
CURLcode result = CURLE_OK; |
| 896 |
(void)conn; |
| 897 |
(void)stream_id; |
| 898 |
(void)user_data; |
| 899 |
|
| 900 |
/* add a CRLF only if we've received some headers */ |
| 901 |
if(stream->firstheader) { |
| 902 |
result = write_data(data, stream, "\r\n", 2); |
| 903 |
if(result) { |
| 904 |
return -1; |
| 905 |
} |
| 906 |
} |
| 907 |
return 0; |
| 908 |
} |
| 909 |
|
| 910 |
static int cb_h3_recv_header(nghttp3_conn *conn, int64_t stream_id, |
| 911 |
int32_t token, nghttp3_rcbuf *name, |
| 912 |
nghttp3_rcbuf *value, uint8_t flags, |
| 913 |
void *user_data, void *stream_user_data) |
| 914 |
{ |
| 915 |
nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name); |
| 916 |
nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value); |
| 917 |
struct Curl_easy *data = stream_user_data; |
| 918 |
struct HTTP *stream = data->req.protop; |
| 919 |
CURLcode result = CURLE_OK; |
| 920 |
(void)conn; |
| 921 |
(void)stream_id; |
| 922 |
(void)token; |
| 923 |
(void)flags; |
| 924 |
(void)user_data; |
| 925 |
|
| 926 |
if(h3name.len == sizeof(":status") - 1 && |
| 927 |
!memcmp(":status", h3name.base, h3name.len)) { |
| 928 |
char line[14]; /* status line is always 13 characters long */ |
| 929 |
size_t ncopy; |
| 930 |
int status = decode_status_code(h3val.base, h3val.len); |
| 931 |
DEBUGASSERT(status != -1); |
| 932 |
ncopy = msnprintf(line, sizeof(line), "HTTP/3 %03d \r\n", status); |
| 933 |
result = write_data(data, stream, line, ncopy); |
| 934 |
if(result) { |
| 935 |
return -1; |
| 936 |
} |
| 937 |
} |
| 938 |
else { |
| 939 |
/* store as a HTTP1-style header */ |
| 940 |
result = write_data(data, stream, h3name.base, h3name.len); |
| 941 |
if(result) { |
| 942 |
return -1; |
| 943 |
} |
| 944 |
result = write_data(data, stream, ": ", 2); |
| 945 |
if(result) { |
| 946 |
return -1; |
| 947 |
} |
| 948 |
result = write_data(data, stream, h3val.base, h3val.len); |
| 949 |
if(result) { |
| 950 |
return -1; |
| 951 |
} |
| 952 |
result = write_data(data, stream, "\r\n", 2); |
| 953 |
if(result) { |
| 954 |
return -1; |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
stream->firstheader = TRUE; |
| 959 |
return 0; |
| 960 |
} |
| 961 |
|
| 962 |
static int cb_h3_send_stop_sending(nghttp3_conn *conn, int64_t stream_id, |
| 963 |
uint64_t app_error_code, |
| 964 |
void *user_data, |
| 965 |
void *stream_user_data) |
| 966 |
{ |
| 967 |
(void)conn; |
| 968 |
(void)stream_id; |
| 969 |
(void)app_error_code; |
| 970 |
(void)user_data; |
| 971 |
(void)stream_user_data; |
| 972 |
return 0; |
| 973 |
} |
| 974 |
|
| 975 |
static nghttp3_conn_callbacks ngh3_callbacks = { |
| 976 |
cb_h3_acked_stream_data, /* acked_stream_data */ |
| 977 |
cb_h3_stream_close, |
| 978 |
cb_h3_recv_data, |
| 979 |
cb_h3_deferred_consume, |
| 980 |
NULL, /* begin_headers */ |
| 981 |
cb_h3_recv_header, |
| 982 |
cb_h3_end_headers, |
| 983 |
NULL, /* begin_trailers */ |
| 984 |
cb_h3_recv_header, |
| 985 |
NULL, /* end_trailers */ |
| 986 |
NULL, /* http_begin_push_promise */ |
| 987 |
NULL, /* http_recv_push_promise */ |
| 988 |
NULL, /* http_end_push_promise */ |
| 989 |
NULL, /* http_cancel_push */ |
| 990 |
cb_h3_send_stop_sending, |
| 991 |
NULL, /* push_stream */ |
| 992 |
NULL, /* end_stream */ |
| 993 |
}; |
| 994 |
|
| 995 |
static int init_ngh3_conn(struct quicsocket *qs) |
| 996 |
{ |
| 997 |
CURLcode result; |
| 998 |
int rc; |
| 999 |
int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id; |
| 1000 |
|
| 1001 |
if(ngtcp2_conn_get_max_local_streams_uni(qs->qconn) < 3) { |
| 1002 |
failf(qs->conn->data, "too few available QUIC streams"); |
| 1003 |
return CURLE_QUIC_CONNECT_ERROR; |
| 1004 |
} |
| 1005 |
|
| 1006 |
nghttp3_conn_settings_default(&qs->h3settings); |
| 1007 |
|
| 1008 |
rc = nghttp3_conn_client_new(&qs->h3conn, |
| 1009 |
&ngh3_callbacks, |
| 1010 |
&qs->h3settings, |
| 1011 |
nghttp3_mem_default(), |
| 1012 |
qs); |
| 1013 |
if(rc) { |
| 1014 |
result = CURLE_OUT_OF_MEMORY; |
| 1015 |
goto fail; |
| 1016 |
} |
| 1017 |
|
| 1018 |
rc = ngtcp2_conn_open_uni_stream(qs->qconn, &ctrl_stream_id, NULL); |
| 1019 |
if(rc) { |
| 1020 |
result = CURLE_QUIC_CONNECT_ERROR; |
| 1021 |
goto fail; |
| 1022 |
} |
| 1023 |
|
| 1024 |
rc = nghttp3_conn_bind_control_stream(qs->h3conn, ctrl_stream_id); |
| 1025 |
if(rc) { |
| 1026 |
result = CURLE_QUIC_CONNECT_ERROR; |
| 1027 |
goto fail; |
| 1028 |
} |
| 1029 |
|
| 1030 |
rc = ngtcp2_conn_open_uni_stream(qs->qconn, &qpack_enc_stream_id, NULL); |
| 1031 |
if(rc) { |
| 1032 |
result = CURLE_QUIC_CONNECT_ERROR; |
| 1033 |
goto fail; |
| 1034 |
} |
| 1035 |
|
| 1036 |
rc = ngtcp2_conn_open_uni_stream(qs->qconn, &qpack_dec_stream_id, NULL); |
| 1037 |
if(rc) { |
| 1038 |
result = CURLE_QUIC_CONNECT_ERROR; |
| 1039 |
goto fail; |
| 1040 |
} |
| 1041 |
|
| 1042 |
rc = nghttp3_conn_bind_qpack_streams(qs->h3conn, qpack_enc_stream_id, |
| 1043 |
qpack_dec_stream_id); |
| 1044 |
if(rc) { |
| 1045 |
result = CURLE_QUIC_CONNECT_ERROR; |
| 1046 |
goto fail; |
| 1047 |
} |
| 1048 |
|
| 1049 |
return CURLE_OK; |
| 1050 |
fail: |
| 1051 |
|
| 1052 |
return result; |
| 1053 |
} |
| 1054 |
|
| 1055 |
static Curl_recv ngh3_stream_recv; |
| 1056 |
static Curl_send ngh3_stream_send; |
| 1057 |
|
| 1058 |
static size_t drain_overflow_buffer(struct HTTP *stream) |
| 1059 |
{ |
| 1060 |
size_t ncopy = CURLMIN(stream->overflow_buflen, stream->len); |
| 1061 |
if(ncopy > 0) { |
| 1062 |
memcpy(stream->mem, stream->overflow_buf, ncopy); |
| 1063 |
stream->len -= ncopy; |
| 1064 |
stream->mem += ncopy; |
| 1065 |
stream->memlen += ncopy; |
| 1066 |
stream->overflow_buflen -= ncopy; |
| 1067 |
memmove(stream->overflow_buf, stream->overflow_buf + ncopy, |
| 1068 |
stream->overflow_buflen); |
| 1069 |
} |
| 1070 |
return ncopy; |
| 1071 |
} |
| 1072 |
|
| 1073 |
/* incoming data frames on the h3 stream */ |
| 1074 |
static ssize_t ngh3_stream_recv(struct connectdata *conn, |
| 1075 |
int sockindex, |
| 1076 |
char *buf, |
| 1077 |
size_t buffersize, |
| 1078 |
CURLcode *curlcode) |
| 1079 |
{ |
| 1080 |
curl_socket_t sockfd = conn->sock[sockindex]; |
| 1081 |
struct HTTP *stream = conn->data->req.protop; |
| 1082 |
struct quicsocket *qs = conn->quic; |
| 1083 |
|
| 1084 |
if(!stream->memlen) { |
| 1085 |
/* remember where to store incoming data for this stream and how big the |
| 1086 |
buffer is */ |
| 1087 |
stream->mem = buf; |
| 1088 |
stream->len = buffersize; |
| 1089 |
} |
| 1090 |
/* else, there's data in the buffer already */ |
| 1091 |
|
| 1092 |
/* if there's data in the overflow buffer from a previous call, copy as much |
| 1093 |
as possible to the receive buffer before receiving more */ |
| 1094 |
drain_overflow_buffer(stream); |
| 1095 |
|
| 1096 |
if(ng_process_ingress(conn, sockfd, qs)) { |
| 1097 |
*curlcode = CURLE_RECV_ERROR; |
| 1098 |
return -1; |
| 1099 |
} |
| 1100 |
if(ng_flush_egress(conn, sockfd, qs)) { |
| 1101 |
*curlcode = CURLE_SEND_ERROR; |
| 1102 |
return -1; |
| 1103 |
} |
| 1104 |
|
| 1105 |
if(stream->memlen) { |
| 1106 |
ssize_t memlen = stream->memlen; |
| 1107 |
/* data arrived */ |
| 1108 |
*curlcode = CURLE_OK; |
| 1109 |
/* reset to allow more data to come */ |
| 1110 |
stream->memlen = 0; |
| 1111 |
stream->mem = buf; |
| 1112 |
stream->len = buffersize; |
| 1113 |
/* extend the stream window with the data we're consuming and send out |
| 1114 |
any additional packets to tell the server that we can receive more */ |
| 1115 |
extend_stream_window(qs->qconn, stream); |
| 1116 |
if(ng_flush_egress(conn, sockfd, qs)) { |
| 1117 |
*curlcode = CURLE_SEND_ERROR; |
| 1118 |
return -1; |
| 1119 |
} |
| 1120 |
return memlen; |
| 1121 |
} |
| 1122 |
|
| 1123 |
if(stream->closed) { |
| 1124 |
*curlcode = CURLE_OK; |
| 1125 |
return 0; |
| 1126 |
} |
| 1127 |
|
| 1128 |
infof(conn->data, "ngh3_stream_recv returns 0 bytes and EAGAIN\n"); |
| 1129 |
*curlcode = CURLE_AGAIN; |
| 1130 |
return -1; |
| 1131 |
} |
| 1132 |
|
| 1133 |
/* this amount of data has now been acked on this stream */ |
| 1134 |
static int cb_h3_acked_stream_data(nghttp3_conn *conn, int64_t stream_id, |
| 1135 |
size_t datalen, void *user_data, |
| 1136 |
void *stream_user_data) |
| 1137 |
{ |
| 1138 |
struct Curl_easy *data = stream_user_data; |
| 1139 |
struct HTTP *stream = data->req.protop; |
| 1140 |
(void)conn; |
| 1141 |
(void)stream_id; |
| 1142 |
(void)user_data; |
| 1143 |
|
| 1144 |
if(!data->set.postfields) { |
| 1145 |
stream->h3out->used -= datalen; |
| 1146 |
H3BUGF(infof(data, |
| 1147 |
"cb_h3_acked_stream_data, %zd bytes, %zd left unacked\n", |
| 1148 |
datalen, stream->h3out->used)); |
| 1149 |
DEBUGASSERT(stream->h3out->used < H3_SEND_SIZE); |
| 1150 |
} |
| 1151 |
return 0; |
| 1152 |
} |
| 1153 |
|
| 1154 |
static ssize_t cb_h3_readfunction(nghttp3_conn *conn, int64_t stream_id, |
| 1155 |
nghttp3_vec *vec, size_t veccnt, |
| 1156 |
uint32_t *pflags, void *user_data, |
| 1157 |
void *stream_user_data) |
| 1158 |
{ |
| 1159 |
struct Curl_easy *data = stream_user_data; |
| 1160 |
size_t nread; |
| 1161 |
struct HTTP *stream = data->req.protop; |
| 1162 |
(void)conn; |
| 1163 |
(void)stream_id; |
| 1164 |
(void)user_data; |
| 1165 |
(void)veccnt; |
| 1166 |
|
| 1167 |
if(data->set.postfields) { |
| 1168 |
vec[0].base = data->set.postfields; |
| 1169 |
vec[0].len = data->state.infilesize; |
| 1170 |
*pflags = NGHTTP3_DATA_FLAG_EOF; |
| 1171 |
return 1; |
| 1172 |
} |
| 1173 |
|
| 1174 |
nread = CURLMIN(stream->upload_len, H3_SEND_SIZE - stream->h3out->used); |
| 1175 |
if(nread > 0) { |
| 1176 |
/* nghttp3 wants us to hold on to the data until it tells us it is okay to |
| 1177 |
delete it. Append the data at the end of the h3out buffer. Since we can |
| 1178 |
only return consecutive data, copy the amount that fits and the next |
| 1179 |
part comes in next invoke. */ |
| 1180 |
struct h3out *out = stream->h3out; |
| 1181 |
if(nread + out->windex > H3_SEND_SIZE) |
| 1182 |
nread = H3_SEND_SIZE - out->windex; |
| 1183 |
|
| 1184 |
memcpy(&out->buf[out->windex], stream->upload_mem, nread); |
| 1185 |
out->windex += nread; |
| 1186 |
out->used += nread; |
| 1187 |
|
| 1188 |
/* that's the chunk we return to nghttp3 */ |
| 1189 |
vec[0].base = &out->buf[out->windex]; |
| 1190 |
vec[0].len = nread; |
| 1191 |
|
| 1192 |
if(out->windex == H3_SEND_SIZE) |
| 1193 |
out->windex = 0; /* wrap */ |
| 1194 |
stream->upload_mem += nread; |
| 1195 |
stream->upload_len -= nread; |
| 1196 |
if(data->state.infilesize != -1) { |
| 1197 |
stream->upload_left -= nread; |
| 1198 |
if(!stream->upload_left) |
| 1199 |
*pflags = NGHTTP3_DATA_FLAG_EOF; |
| 1200 |
} |
| 1201 |
H3BUGF(infof(data, "cb_h3_readfunction %zd bytes%s (at %zd unacked)\n", |
| 1202 |
nread, *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"", |
| 1203 |
out->used)); |
| 1204 |
} |
| 1205 |
if(stream->upload_done && !stream->upload_len && |
| 1206 |
(stream->upload_left <= 0)) { |
| 1207 |
H3BUGF(infof(data, "!!!!!!!!! cb_h3_readfunction sets EOF\n")); |
| 1208 |
*pflags = NGHTTP3_DATA_FLAG_EOF; |
| 1209 |
return 0; |
| 1210 |
} |
| 1211 |
else if(!nread) { |
| 1212 |
return NGHTTP3_ERR_WOULDBLOCK; |
| 1213 |
} |
| 1214 |
return 1; |
| 1215 |
} |
| 1216 |
|
| 1217 |
/* Index where :authority header field will appear in request header |
| 1218 |
field list. */ |
| 1219 |
#define AUTHORITY_DST_IDX 3 |
| 1220 |
|
| 1221 |
static CURLcode http_request(struct connectdata *conn, const void *mem, |
| 1222 |
size_t len) |
| 1223 |
{ |
| 1224 |
struct HTTP *stream = conn->data->req.protop; |
| 1225 |
size_t nheader; |
| 1226 |
size_t i; |
| 1227 |
size_t authority_idx; |
| 1228 |
char *hdbuf = (char *)mem; |
| 1229 |
char *end, *line_end; |
| 1230 |
struct quicsocket *qs = conn->quic; |
| 1231 |
CURLcode result = CURLE_OK; |
| 1232 |
struct Curl_easy *data = conn->data; |
| 1233 |
nghttp3_nv *nva = NULL; |
| 1234 |
int64_t stream3_id; |
| 1235 |
int rc; |
| 1236 |
struct h3out *h3out = NULL; |
| 1237 |
|
| 1238 |
rc = ngtcp2_conn_open_bidi_stream(qs->qconn, &stream3_id, NULL); |
| 1239 |
if(rc) { |
| 1240 |
failf(conn->data, "can get bidi streams"); |
| 1241 |
result = CURLE_SEND_ERROR; |
| 1242 |
goto fail; |
| 1243 |
} |
| 1244 |
|
| 1245 |
stream->stream3_id = stream3_id; |
| 1246 |
stream->h3req = TRUE; /* senf off! */ |
| 1247 |
|
| 1248 |
/* Calculate number of headers contained in [mem, mem + len). Assumes a |
| 1249 |
correctly generated HTTP header field block. */ |
| 1250 |
nheader = 0; |
| 1251 |
for(i = 1; i < len; ++i) { |
| 1252 |
if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') { |
| 1253 |
++nheader; |
| 1254 |
++i; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
if(nheader < 2) |
| 1258 |
goto fail; |
| 1259 |
|
| 1260 |
/* We counted additional 2 \r\n in the first and last line. We need 3 |
| 1261 |
new headers: :method, :path and :scheme. Therefore we need one |
| 1262 |
more space. */ |
| 1263 |
nheader += 1; |
| 1264 |
nva = malloc(sizeof(nghttp3_nv) * nheader); |
| 1265 |
if(!nva) { |
| 1266 |
result = CURLE_OUT_OF_MEMORY; |
| 1267 |
goto fail; |
| 1268 |
} |
| 1269 |
|
| 1270 |
/* Extract :method, :path from request line |
| 1271 |
We do line endings with CRLF so checking for CR is enough */ |
| 1272 |
line_end = memchr(hdbuf, '\r', len); |
| 1273 |
if(!line_end) { |
| 1274 |
result = CURLE_BAD_FUNCTION_ARGUMENT; /* internal error */ |
| 1275 |
goto fail; |
| 1276 |
} |
| 1277 |
|
| 1278 |
/* Method does not contain spaces */ |
| 1279 |
end = memchr(hdbuf, ' ', line_end - hdbuf); |
| 1280 |
if(!end || end == hdbuf) |
| 1281 |
goto fail; |
| 1282 |
nva[0].name = (unsigned char *)":method"; |
| 1283 |
nva[0].namelen = strlen((char *)nva[0].name); |
| 1284 |
nva[0].value = (unsigned char *)hdbuf; |
| 1285 |
nva[0].valuelen = (size_t)(end - hdbuf); |
| 1286 |
nva[0].flags = NGHTTP3_NV_FLAG_NONE; |
| 1287 |
|
| 1288 |
hdbuf = end + 1; |
| 1289 |
|
| 1290 |
/* Path may contain spaces so scan backwards */ |
| 1291 |
end = NULL; |
| 1292 |
for(i = (size_t)(line_end - hdbuf); i; --i) { |
| 1293 |
if(hdbuf[i - 1] == ' ') { |
| 1294 |
end = &hdbuf[i - 1]; |
| 1295 |
break; |
| 1296 |
} |
| 1297 |
} |
| 1298 |
if(!end || end == hdbuf) |
| 1299 |
goto fail; |
| 1300 |
nva[1].name = (unsigned char *)":path"; |
| 1301 |
nva[1].namelen = strlen((char *)nva[1].name); |
| 1302 |
nva[1].value = (unsigned char *)hdbuf; |
| 1303 |
nva[1].valuelen = (size_t)(end - hdbuf); |
| 1304 |
nva[1].flags = NGHTTP3_NV_FLAG_NONE; |
| 1305 |
|
| 1306 |
nva[2].name = (unsigned char *)":scheme"; |
| 1307 |
nva[2].namelen = strlen((char *)nva[2].name); |
| 1308 |
if(conn->handler->flags & PROTOPT_SSL) |
| 1309 |
nva[2].value = (unsigned char *)"https"; |
| 1310 |
else |
| 1311 |
nva[2].value = (unsigned char *)"http"; |
| 1312 |
nva[2].valuelen = strlen((char *)nva[2].value); |
| 1313 |
nva[2].flags = NGHTTP3_NV_FLAG_NONE; |
| 1314 |
|
| 1315 |
|
| 1316 |
authority_idx = 0; |
| 1317 |
i = 3; |
| 1318 |
while(i < nheader) { |
| 1319 |
size_t hlen; |
| 1320 |
|
| 1321 |
hdbuf = line_end + 2; |
| 1322 |
|
| 1323 |
/* check for next CR, but only within the piece of data left in the given |
| 1324 |
buffer */ |
| 1325 |
line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem)); |
| 1326 |
if(!line_end || (line_end == hdbuf)) |
| 1327 |
goto fail; |
| 1328 |
|
| 1329 |
/* header continuation lines are not supported */ |
| 1330 |
if(*hdbuf == ' ' || *hdbuf == '\t') |
| 1331 |
goto fail; |
| 1332 |
|
| 1333 |
for(end = hdbuf; end < line_end && *end != ':'; ++end) |
| 1334 |
; |
| 1335 |
if(end == hdbuf || end == line_end) |
| 1336 |
goto fail; |
| 1337 |
hlen = end - hdbuf; |
| 1338 |
|
| 1339 |
if(hlen == 4 && strncasecompare("host", hdbuf, 4)) { |
| 1340 |
authority_idx = i; |
| 1341 |
nva[i].name = (unsigned char *)":authority"; |
| 1342 |
nva[i].namelen = strlen((char *)nva[i].name); |
| 1343 |
} |
| 1344 |
else { |
| 1345 |
nva[i].namelen = (size_t)(end - hdbuf); |
| 1346 |
/* Lower case the header name for HTTP/3 */ |
| 1347 |
Curl_strntolower((char *)hdbuf, hdbuf, nva[i].namelen); |
| 1348 |
nva[i].name = (unsigned char *)hdbuf; |
| 1349 |
} |
| 1350 |
nva[i].flags = NGHTTP3_NV_FLAG_NONE; |
| 1351 |
hdbuf = end + 1; |
| 1352 |
while(*hdbuf == ' ' || *hdbuf == '\t') |
| 1353 |
++hdbuf; |
| 1354 |
end = line_end; |
| 1355 |
|
| 1356 |
#if 0 /* This should probably go in more or less like this */ |
| 1357 |
switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf, |
| 1358 |
end - hdbuf)) { |
| 1359 |
case HEADERINST_IGNORE: |
| 1360 |
/* skip header fields prohibited by HTTP/2 specification. */ |
| 1361 |
--nheader; |
| 1362 |
continue; |
| 1363 |
case HEADERINST_TE_TRAILERS: |
| 1364 |
nva[i].value = (uint8_t*)"trailers"; |
| 1365 |
nva[i].value_len = sizeof("trailers") - 1; |
| 1366 |
break; |
| 1367 |
default: |
| 1368 |
nva[i].value = (unsigned char *)hdbuf; |
| 1369 |
nva[i].value_len = (size_t)(end - hdbuf); |
| 1370 |
} |
| 1371 |
#endif |
| 1372 |
nva[i].value = (unsigned char *)hdbuf; |
| 1373 |
nva[i].valuelen = (size_t)(end - hdbuf); |
| 1374 |
nva[i].flags = NGHTTP3_NV_FLAG_NONE; |
| 1375 |
|
| 1376 |
++i; |
| 1377 |
} |
| 1378 |
|
| 1379 |
/* :authority must come before non-pseudo header fields */ |
| 1380 |
if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) { |
| 1381 |
nghttp3_nv authority = nva[authority_idx]; |
| 1382 |
for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) { |
| 1383 |
nva[i] = nva[i - 1]; |
| 1384 |
} |
| 1385 |
nva[i] = authority; |
| 1386 |
} |
| 1387 |
|
| 1388 |
/* Warn stream may be rejected if cumulative length of headers is too |
| 1389 |
large. */ |
| 1390 |
#define MAX_ACC 60000 /* <64KB to account for some overhead */ |
| 1391 |
{ |
| 1392 |
size_t acc = 0; |
| 1393 |
for(i = 0; i < nheader; ++i) |
| 1394 |
acc += nva[i].namelen + nva[i].valuelen; |
| 1395 |
|
| 1396 |
if(acc > MAX_ACC) { |
| 1397 |
infof(data, "http_request: Warning: The cumulative length of all " |
| 1398 |
"headers exceeds %zu bytes and that could cause the " |
| 1399 |
"stream to be rejected.\n", MAX_ACC); |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
switch(data->set.httpreq) { |
| 1404 |
case HTTPREQ_POST: |
| 1405 |
case HTTPREQ_POST_FORM: |
| 1406 |
case HTTPREQ_POST_MIME: |
| 1407 |
case HTTPREQ_PUT: { |
| 1408 |
nghttp3_data_reader data_reader; |
| 1409 |
if(data->state.infilesize != -1) |
| 1410 |
stream->upload_left = data->state.infilesize; |
| 1411 |
else |
| 1412 |
/* data sending without specifying the data amount up front */ |
| 1413 |
stream->upload_left = -1; /* unknown, but not zero */ |
| 1414 |
|
| 1415 |
data_reader.read_data = cb_h3_readfunction; |
| 1416 |
|
| 1417 |
h3out = calloc(sizeof(struct h3out), 1); |
| 1418 |
if(!h3out) { |
| 1419 |
result = CURLE_OUT_OF_MEMORY; |
| 1420 |
goto fail; |
| 1421 |
} |
| 1422 |
stream->h3out = h3out; |
| 1423 |
|
| 1424 |
rc = nghttp3_conn_submit_request(qs->h3conn, stream->stream3_id, |
| 1425 |
nva, nheader, &data_reader, |
| 1426 |
conn->data); |
| 1427 |
if(rc) { |
| 1428 |
result = CURLE_SEND_ERROR; |
| 1429 |
goto fail; |
| 1430 |
} |
| 1431 |
break; |
| 1432 |
} |
| 1433 |
default: |
| 1434 |
stream->upload_left = 0; /* nothing left to send */ |
| 1435 |
rc = nghttp3_conn_submit_request(qs->h3conn, stream->stream3_id, |
| 1436 |
nva, nheader, |
| 1437 |
NULL, /* no body! */ |
| 1438 |
conn->data); |
| 1439 |
if(rc) { |
| 1440 |
result = CURLE_SEND_ERROR; |
| 1441 |
goto fail; |
| 1442 |
} |
| 1443 |
break; |
| 1444 |
} |
| 1445 |
|
| 1446 |
Curl_safefree(nva); |
| 1447 |
|
| 1448 |
infof(data, "Using HTTP/3 Stream ID: %x (easy handle %p)\n", |
| 1449 |
stream3_id, (void *)data); |
| 1450 |
|
| 1451 |
return CURLE_OK; |
| 1452 |
|
| 1453 |
fail: |
| 1454 |
free(nva); |
| 1455 |
return result; |
| 1456 |
} |
| 1457 |
static ssize_t ngh3_stream_send(struct connectdata *conn, |
| 1458 |
int sockindex, |
| 1459 |
const void *mem, |
| 1460 |
size_t len, |
| 1461 |
CURLcode *curlcode) |
| 1462 |
{ |
| 1463 |
ssize_t sent; |
| 1464 |
struct quicsocket *qs = conn->quic; |
| 1465 |
curl_socket_t sockfd = conn->sock[sockindex]; |
| 1466 |
struct HTTP *stream = conn->data->req.protop; |
| 1467 |
|
| 1468 |
if(!stream->h3req) { |
| 1469 |
CURLcode result = http_request(conn, mem, len); |
| 1470 |
if(result) { |
| 1471 |
*curlcode = CURLE_SEND_ERROR; |
| 1472 |
return -1; |
| 1473 |
} |
| 1474 |
sent = len; |
| 1475 |
} |
| 1476 |
else { |
| 1477 |
H3BUGF(infof(conn->data, "ngh3_stream_send() wants to send %zd bytes\n", |
| 1478 |
len)); |
| 1479 |
if(!stream->upload_len) { |
| 1480 |
stream->upload_mem = mem; |
| 1481 |
stream->upload_len = len; |
| 1482 |
(void)nghttp3_conn_resume_stream(qs->h3conn, stream->stream3_id); |
| 1483 |
sent = len; |
| 1484 |
} |
| 1485 |
else { |
| 1486 |
*curlcode = CURLE_AGAIN; |
| 1487 |
return -1; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
|
| 1491 |
if(ng_flush_egress(conn, sockfd, qs)) { |
| 1492 |
*curlcode = CURLE_SEND_ERROR; |
| 1493 |
return -1; |
| 1494 |
} |
| 1495 |
|
| 1496 |
*curlcode = CURLE_OK; |
| 1497 |
return sent; |
| 1498 |
} |
| 1499 |
|
| 1500 |
static void ng_has_connected(struct connectdata *conn, int tempindex) |
| 1501 |
{ |
| 1502 |
conn->recv[FIRSTSOCKET] = ngh3_stream_recv; |
| 1503 |
conn->send[FIRSTSOCKET] = ngh3_stream_send; |
| 1504 |
conn->handler = &Curl_handler_http3; |
| 1505 |
conn->bits.multiplex = TRUE; /* at least potentially multiplexed */ |
| 1506 |
conn->httpversion = 30; |
| 1507 |
conn->bundle->multiuse = BUNDLE_MULTIPLEX; |
| 1508 |
conn->quic = &conn->hequic[tempindex]; |
| 1509 |
DEBUGF(infof(conn->data, "ngtcp2 established connection!\n")); |
| 1510 |
} |
| 1511 |
|
| 1512 |
/* |
| 1513 |
* There can be multiple connection attempts going on in parallel. |
| 1514 |
*/ |
| 1515 |
CURLcode Curl_quic_is_connected(struct connectdata *conn, |
| 1516 |
int sockindex, |
| 1517 |
bool *done) |
| 1518 |
{ |
| 1519 |
CURLcode result; |
| 1520 |
struct quicsocket *qs = &conn->hequic[sockindex]; |
| 1521 |
curl_socket_t sockfd = conn->tempsock[sockindex]; |
| 1522 |
|
| 1523 |
result = ng_process_ingress(conn, sockfd, qs); |
| 1524 |
if(result) |
| 1525 |
return result; |
| 1526 |
|
| 1527 |
result = ng_flush_egress(conn, sockfd, qs); |
| 1528 |
if(result) |
| 1529 |
return result; |
| 1530 |
|
| 1531 |
if(ngtcp2_conn_get_handshake_completed(qs->qconn)) { |
| 1532 |
*done = TRUE; |
| 1533 |
ng_has_connected(conn, sockindex); |
| 1534 |
} |
| 1535 |
|
| 1536 |
return result; |
| 1537 |
} |
| 1538 |
|
| 1539 |
static CURLcode ng_process_ingress(struct connectdata *conn, int sockfd, |
| 1540 |
struct quicsocket *qs) |
| 1541 |
{ |
| 1542 |
ssize_t recvd; |
| 1543 |
int rv; |
| 1544 |
uint8_t buf[65536]; |
| 1545 |
size_t bufsize = sizeof(buf); |
| 1546 |
struct sockaddr_storage remote_addr; |
| 1547 |
socklen_t remote_addrlen; |
| 1548 |
ngtcp2_path path; |
| 1549 |
ngtcp2_tstamp ts = timestamp(); |
| 1550 |
|
| 1551 |
for(;;) { |
| 1552 |
remote_addrlen = sizeof(remote_addr); |
| 1553 |
while((recvd = recvfrom(sockfd, buf, bufsize, 0, |
| 1554 |
(struct sockaddr *)&remote_addr, |
| 1555 |
&remote_addrlen)) == -1 && |
| 1556 |
SOCKERRNO == EINTR) |
| 1557 |
; |
| 1558 |
if(recvd == -1) { |
| 1559 |
if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) |
| 1560 |
break; |
| 1561 |
|
| 1562 |
failf(conn->data, "ngtcp2: recvfrom() unexpectedly returned %d", recvd); |
| 1563 |
return CURLE_RECV_ERROR; |
| 1564 |
} |
| 1565 |
|
| 1566 |
ngtcp2_addr_init(&path.local, (uint8_t *)&qs->local_addr, |
| 1567 |
qs->local_addrlen, NULL); |
| 1568 |
ngtcp2_addr_init(&path.remote, (uint8_t *)&remote_addr, remote_addrlen, |
| 1569 |
NULL); |
| 1570 |
|
| 1571 |
rv = ngtcp2_conn_read_pkt(qs->qconn, &path, buf, recvd, ts); |
| 1572 |
if(rv != 0) { |
| 1573 |
/* TODO Send CONNECTION_CLOSE if possible */ |
| 1574 |
return CURLE_RECV_ERROR; |
| 1575 |
} |
| 1576 |
} |
| 1577 |
|
| 1578 |
return CURLE_OK; |
| 1579 |
} |
| 1580 |
|
| 1581 |
static CURLcode ng_flush_egress(struct connectdata *conn, int sockfd, |
| 1582 |
struct quicsocket *qs) |
| 1583 |
{ |
| 1584 |
int rv; |
| 1585 |
ssize_t sent; |
| 1586 |
ssize_t outlen; |
| 1587 |
uint8_t out[NGTCP2_MAX_PKTLEN_IPV4]; |
| 1588 |
size_t pktlen; |
| 1589 |
ngtcp2_path_storage ps; |
| 1590 |
ngtcp2_tstamp ts = timestamp(); |
| 1591 |
struct sockaddr_storage remote_addr; |
| 1592 |
ngtcp2_tstamp expiry; |
| 1593 |
ngtcp2_duration timeout; |
| 1594 |
int64_t stream_id; |
| 1595 |
ssize_t veccnt; |
| 1596 |
int fin; |
| 1597 |
nghttp3_vec vec[16]; |
| 1598 |
ssize_t ndatalen; |
| 1599 |
|
| 1600 |
switch(qs->local_addr.ss_family) { |
| 1601 |
case AF_INET: |
| 1602 |
pktlen = NGTCP2_MAX_PKTLEN_IPV4; |
| 1603 |
break; |
| 1604 |
#ifdef ENABLE_IPV6 |
| 1605 |
case AF_INET6: |
| 1606 |
pktlen = NGTCP2_MAX_PKTLEN_IPV6; |
| 1607 |
break; |
| 1608 |
#endif |
| 1609 |
default: |
| 1610 |
assert(0); |
| 1611 |
} |
| 1612 |
|
| 1613 |
rv = ngtcp2_conn_handle_expiry(qs->qconn, ts); |
| 1614 |
if(rv != 0) { |
| 1615 |
failf(conn->data, "ngtcp2_conn_handle_expiry returned error: %s\n", |
| 1616 |
ngtcp2_strerror(rv)); |
| 1617 |
return CURLE_SEND_ERROR; |
| 1618 |
} |
| 1619 |
|
| 1620 |
ngtcp2_path_storage_zero(&ps); |
| 1621 |
|
| 1622 |
for(;;) { |
| 1623 |
outlen = -1; |
| 1624 |
if(qs->h3conn && ngtcp2_conn_get_max_data_left(qs->qconn)) { |
| 1625 |
veccnt = nghttp3_conn_writev_stream(qs->h3conn, &stream_id, &fin, vec, |
| 1626 |
sizeof(vec) / sizeof(vec[0])); |
| 1627 |
if(veccnt < 0) { |
| 1628 |
failf(conn->data, "nghttp3_conn_writev_stream returned error: %s\n", |
| 1629 |
nghttp3_strerror((int)veccnt)); |
| 1630 |
return CURLE_SEND_ERROR; |
| 1631 |
} |
| 1632 |
else if(veccnt > 0) { |
| 1633 |
outlen = |
| 1634 |
ngtcp2_conn_writev_stream(qs->qconn, &ps.path, |
| 1635 |
out, pktlen, &ndatalen, |
| 1636 |
NGTCP2_WRITE_STREAM_FLAG_MORE, |
| 1637 |
stream_id, fin, |
| 1638 |
(const ngtcp2_vec *)vec, veccnt, ts); |
| 1639 |
if(outlen == 0) { |
| 1640 |
break; |
| 1641 |
} |
| 1642 |
if(outlen < 0) { |
| 1643 |
if(outlen == NGTCP2_ERR_STREAM_DATA_BLOCKED || |
| 1644 |
outlen == NGTCP2_ERR_STREAM_SHUT_WR) { |
| 1645 |
rv = nghttp3_conn_block_stream(qs->h3conn, stream_id); |
| 1646 |
if(rv != 0) { |
| 1647 |
failf(conn->data, |
| 1648 |
"nghttp3_conn_block_stream returned error: %s\n", |
| 1649 |
nghttp3_strerror(rv)); |
| 1650 |
return CURLE_SEND_ERROR; |
| 1651 |
} |
| 1652 |
continue; |
| 1653 |
} |
| 1654 |
else if(outlen == NGTCP2_ERR_WRITE_STREAM_MORE) { |
| 1655 |
assert(ndatalen > 0); |
| 1656 |
rv = nghttp3_conn_add_write_offset(qs->h3conn, stream_id, |
| 1657 |
ndatalen); |
| 1658 |
if(rv != 0) { |
| 1659 |
failf(conn->data, |
| 1660 |
"nghttp3_conn_add_write_offset returned error: %s\n", |
| 1661 |
nghttp3_strerror(rv)); |
| 1662 |
return CURLE_SEND_ERROR; |
| 1663 |
} |
| 1664 |
continue; |
| 1665 |
} |
| 1666 |
else { |
| 1667 |
failf(conn->data, "ngtcp2_conn_writev_stream returned error: %s\n", |
| 1668 |
ngtcp2_strerror((int)outlen)); |
| 1669 |
return CURLE_SEND_ERROR; |
| 1670 |
} |
| 1671 |
} |
| 1672 |
else if(ndatalen >= 0) { |
| 1673 |
rv = nghttp3_conn_add_write_offset(qs->h3conn, stream_id, ndatalen); |
| 1674 |
if(rv != 0) { |
| 1675 |
failf(conn->data, |
| 1676 |
"nghttp3_conn_add_write_offset returned error: %s\n", |
| 1677 |
nghttp3_strerror(rv)); |
| 1678 |
return CURLE_SEND_ERROR; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
} |
| 1682 |
} |
| 1683 |
if(outlen < 0) { |
| 1684 |
outlen = ngtcp2_conn_write_pkt(qs->qconn, &ps.path, out, pktlen, ts); |
| 1685 |
if(outlen < 0) { |
| 1686 |
failf(conn->data, "ngtcp2_conn_write_pkt returned error: %s\n", |
| 1687 |
ngtcp2_strerror((int)outlen)); |
| 1688 |
return CURLE_SEND_ERROR; |
| 1689 |
} |
| 1690 |
if(outlen == 0) |
| 1691 |
break; |
| 1692 |
} |
| 1693 |
|
| 1694 |
memcpy(&remote_addr, ps.path.remote.addr, ps.path.remote.addrlen); |
| 1695 |
while((sent = send(sockfd, out, outlen, 0)) == -1 && |
| 1696 |
SOCKERRNO == EINTR) |
| 1697 |
; |
| 1698 |
|
| 1699 |
if(sent == -1) { |
| 1700 |
if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) { |
| 1701 |
/* TODO Cache packet */ |
| 1702 |
break; |
| 1703 |
} |
| 1704 |
else { |
| 1705 |
failf(conn->data, "send() returned %zd (errno %d)\n", sent, |
| 1706 |
SOCKERRNO); |
| 1707 |
return CURLE_SEND_ERROR; |
| 1708 |
} |
| 1709 |
} |
| 1710 |
} |
| 1711 |
|
| 1712 |
expiry = ngtcp2_conn_get_expiry(qs->qconn); |
| 1713 |
if(expiry != UINT64_MAX) { |
| 1714 |
if(expiry <= ts) { |
| 1715 |
timeout = NGTCP2_MILLISECONDS; |
| 1716 |
} |
| 1717 |
else { |
| 1718 |
timeout = expiry - ts; |
| 1719 |
} |
| 1720 |
Curl_expire(conn->data, timeout / NGTCP2_MILLISECONDS, EXPIRE_QUIC); |
| 1721 |
} |
| 1722 |
|
| 1723 |
return CURLE_OK; |
| 1724 |
} |
| 1725 |
|
| 1726 |
/* |
| 1727 |
* Called from transfer.c:done_sending when we stop HTTP/3 uploading. |
| 1728 |
*/ |
| 1729 |
CURLcode Curl_quic_done_sending(struct connectdata *conn) |
| 1730 |
{ |
| 1731 |
if(conn->handler == &Curl_handler_http3) { |
| 1732 |
/* only for HTTP/3 transfers */ |
| 1733 |
struct HTTP *stream = conn->data->req.protop; |
| 1734 |
struct quicsocket *qs = conn->quic; |
| 1735 |
stream->upload_done = TRUE; |
| 1736 |
(void)nghttp3_conn_resume_stream(qs->h3conn, stream->stream3_id); |
| 1737 |
} |
| 1738 |
|
| 1739 |
return CURLE_OK; |
| 1740 |
} |
| 1741 |
|
| 1742 |
/* |
| 1743 |
* Called from http.c:Curl_http_done when a request completes. |
| 1744 |
*/ |
| 1745 |
void Curl_quic_done(struct Curl_easy *data, bool premature) |
| 1746 |
{ |
| 1747 |
(void)premature; |
| 1748 |
if(data->conn->handler == &Curl_handler_http3) { |
| 1749 |
/* only for HTTP/3 transfers */ |
| 1750 |
struct HTTP *stream = data->req.protop; |
| 1751 |
Curl_safefree(stream->overflow_buf); |
| 1752 |
} |
| 1753 |
} |
| 1754 |
|
| 1755 |
/* |
| 1756 |
* Called from transfer.c:data_pending to know if we should keep looping |
| 1757 |
* to receive more data from the connection. |
| 1758 |
*/ |
| 1759 |
bool Curl_quic_data_pending(const struct Curl_easy *data) |
| 1760 |
{ |
| 1761 |
/* We may have received more data than we're able to hold in the receive |
| 1762 |
buffer and allocated an overflow buffer. Since it's possible that |
| 1763 |
there's no more data coming on the socket, we need to keep reading |
| 1764 |
until the overflow buffer is empty. */ |
| 1765 |
const struct HTTP *stream = data->req.protop; |
| 1766 |
return stream->overflow_buflen > 0; |
| 1767 |
} |
| 1768 |
|
| 1769 |
#endif |