2019-07-21 17:48:58 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-01-12 10:55:34 -05:00
|
|
|
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2019-07-21 17:48:58 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#ifdef USE_NGTCP2
|
|
|
|
#include <ngtcp2/ngtcp2.h>
|
2019-08-29 03:09:26 -04:00
|
|
|
#include <ngtcp2/ngtcp2_crypto.h>
|
2019-08-11 17:50:11 -04:00
|
|
|
#include <nghttp3/nghttp3.h>
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
2019-07-21 17:48:58 -04:00
|
|
|
#include <openssl/err.h>
|
2020-03-25 17:49:02 -04:00
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "strdup.h"
|
|
|
|
#include "rand.h"
|
|
|
|
#include "ngtcp2.h"
|
2019-08-10 04:06:50 -04:00
|
|
|
#include "multiif.h"
|
2019-08-11 17:50:11 -04:00
|
|
|
#include "strcase.h"
|
|
|
|
#include "connect.h"
|
|
|
|
#include "strerror.h"
|
2020-05-04 05:37:12 -04:00
|
|
|
#include "dynbuf.h"
|
2020-05-07 08:17:42 -04:00
|
|
|
#include "vquic.h"
|
2020-05-03 19:41:46 -04:00
|
|
|
#include "vtls/keylog.h"
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
/* #define DEBUG_NGTCP2 */
|
2019-09-25 06:27:26 -04:00
|
|
|
#ifdef CURLDEBUG
|
2019-08-11 17:50:11 -04:00
|
|
|
#define DEBUG_HTTP3
|
2019-09-25 06:27:26 -04:00
|
|
|
#endif
|
2019-08-11 17:50:11 -04:00
|
|
|
#ifdef DEBUG_HTTP3
|
|
|
|
#define H3BUGF(x) x
|
|
|
|
#else
|
2019-11-30 03:29:36 -05:00
|
|
|
#define H3BUGF(x) do { } while(0)
|
2019-08-11 17:50:11 -04:00
|
|
|
#endif
|
|
|
|
|
2019-08-22 08:08:18 -04:00
|
|
|
/*
|
|
|
|
* This holds outgoing HTTP/3 stream data that is used by nghttp3 until acked.
|
|
|
|
* It is used as a circular buffer. Add new bytes at the end until it reaches
|
|
|
|
* the far end, then start over at index 0 again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define H3_SEND_SIZE (20*1024)
|
|
|
|
struct h3out {
|
|
|
|
uint8_t buf[H3_SEND_SIZE];
|
|
|
|
size_t used; /* number of bytes used in the buffer */
|
|
|
|
size_t windex; /* index in the buffer where to start writing the next
|
|
|
|
data block */
|
|
|
|
};
|
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
#define QUIC_MAX_STREAMS (256*1024)
|
|
|
|
#define QUIC_MAX_DATA (1*1024*1024)
|
2019-08-10 04:06:50 -04:00
|
|
|
#define QUIC_IDLE_TIMEOUT 60000 /* milliseconds */
|
2020-03-25 17:49:02 -04:00
|
|
|
|
|
|
|
#ifdef USE_OPENSSL
|
2019-08-10 04:06:50 -04:00
|
|
|
#define QUIC_CIPHERS \
|
|
|
|
"TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_" \
|
|
|
|
"POLY1305_SHA256:TLS_AES_128_CCM_SHA256"
|
2019-07-21 17:48:58 -04:00
|
|
|
#define QUIC_GROUPS "P-256:X25519:P-384:P-521"
|
2020-03-25 17:49:02 -04:00
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
#define QUIC_PRIORITY \
|
|
|
|
"NORMAL:-VERS-ALL:+VERS-TLS1.3:-CIPHER-ALL:+AES-128-GCM:+AES-256-GCM:" \
|
|
|
|
"+CHACHA20-POLY1305:+AES-128-CCM:-GROUP-ALL:+GROUP-SECP256R1:" \
|
|
|
|
"+GROUP-X25519:+GROUP-SECP384R1:+GROUP-SECP521R1"
|
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
static CURLcode ng_process_ingress(struct connectdata *conn,
|
|
|
|
curl_socket_t sockfd,
|
|
|
|
struct quicsocket *qs);
|
|
|
|
static CURLcode ng_flush_egress(struct connectdata *conn, int sockfd,
|
|
|
|
struct quicsocket *qs);
|
2019-08-22 08:08:18 -04:00
|
|
|
static int cb_h3_acked_stream_data(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
size_t datalen, void *user_data,
|
|
|
|
void *stream_user_data);
|
2019-08-10 04:06:50 -04:00
|
|
|
|
|
|
|
static ngtcp2_tstamp timestamp(void)
|
|
|
|
{
|
|
|
|
struct curltime ct = Curl_now();
|
|
|
|
return ct.tv_sec * NGTCP2_SECONDS + ct.tv_usec * NGTCP2_MICROSECONDS;
|
|
|
|
}
|
|
|
|
|
2019-08-12 05:10:56 -04:00
|
|
|
#ifdef DEBUG_NGTCP2
|
2019-07-21 17:48:58 -04:00
|
|
|
static void quic_printf(void *user_data, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
(void)user_data; /* TODO, use this to do infof() instead long-term */
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
2019-08-10 04:06:50 -04:00
|
|
|
fprintf(stderr, "\n");
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
2019-08-12 05:10:56 -04:00
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
2019-08-27 09:00:34 -04:00
|
|
|
static ngtcp2_crypto_level
|
|
|
|
quic_from_ossl_level(OSSL_ENCRYPTION_LEVEL ossl_level)
|
|
|
|
{
|
|
|
|
switch(ossl_level) {
|
|
|
|
case ssl_encryption_initial:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_INITIAL;
|
|
|
|
case ssl_encryption_early_data:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_EARLY;
|
|
|
|
case ssl_encryption_handshake:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_HANDSHAKE;
|
|
|
|
case ssl_encryption_application:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_APP;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
2019-08-27 09:00:34 -04:00
|
|
|
}
|
2020-03-25 17:49:02 -04:00
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
static ngtcp2_crypto_level
|
|
|
|
quic_from_gtls_level(gnutls_record_encryption_level_t gtls_level)
|
|
|
|
{
|
|
|
|
switch(gtls_level) {
|
|
|
|
case GNUTLS_ENCRYPTION_LEVEL_INITIAL:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_INITIAL;
|
|
|
|
case GNUTLS_ENCRYPTION_LEVEL_EARLY:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_EARLY;
|
|
|
|
case GNUTLS_ENCRYPTION_LEVEL_HANDSHAKE:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_HANDSHAKE;
|
|
|
|
case GNUTLS_ENCRYPTION_LEVEL_APPLICATION:
|
|
|
|
return NGTCP2_CRYPTO_LEVEL_APP;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2020-05-07 08:17:42 -04:00
|
|
|
static void qlog_callback(void *user_data, const void *data, size_t datalen)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
2020-05-07 08:17:42 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
|
|
|
if(qs->qlogfd != -1) {
|
|
|
|
ssize_t rc = write(qs->qlogfd, data, datalen);
|
|
|
|
if(rc == -1) {
|
|
|
|
/* on write error, stop further write attempts */
|
|
|
|
close(qs->qlogfd);
|
|
|
|
qs->qlogfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void quic_settings(struct quicsocket *qs,
|
2020-05-23 09:41:25 -04:00
|
|
|
uint64_t stream_buffer_size)
|
2020-05-07 08:17:42 -04:00
|
|
|
{
|
|
|
|
ngtcp2_settings *s = &qs->settings;
|
2019-08-10 21:48:34 -04:00
|
|
|
ngtcp2_settings_default(s);
|
2019-08-12 05:10:56 -04:00
|
|
|
#ifdef DEBUG_NGTCP2
|
2019-07-21 17:48:58 -04:00
|
|
|
s->log_printf = quic_printf;
|
2019-08-12 05:10:56 -04:00
|
|
|
#else
|
|
|
|
s->log_printf = NULL;
|
|
|
|
#endif
|
2019-08-10 21:48:34 -04:00
|
|
|
s->initial_ts = timestamp();
|
2019-10-03 17:08:45 -04:00
|
|
|
s->transport_params.initial_max_stream_data_bidi_local = stream_buffer_size;
|
|
|
|
s->transport_params.initial_max_stream_data_bidi_remote = QUIC_MAX_STREAMS;
|
|
|
|
s->transport_params.initial_max_stream_data_uni = QUIC_MAX_STREAMS;
|
|
|
|
s->transport_params.initial_max_data = QUIC_MAX_DATA;
|
|
|
|
s->transport_params.initial_max_streams_bidi = 1;
|
|
|
|
s->transport_params.initial_max_streams_uni = 3;
|
2020-01-29 09:56:16 -05:00
|
|
|
s->transport_params.max_idle_timeout = QUIC_IDLE_TIMEOUT;
|
2020-05-07 08:17:42 -04:00
|
|
|
if(qs->qlogfd != -1) {
|
|
|
|
s->qlog.write = qlog_callback;
|
|
|
|
}
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
2019-08-24 13:11:25 -04:00
|
|
|
static void keylog_callback(const SSL *ssl, const char *line)
|
|
|
|
{
|
|
|
|
(void)ssl;
|
2020-05-03 19:41:46 -04:00
|
|
|
Curl_tls_keylog_write_line(line);
|
2019-08-24 13:11:25 -04:00
|
|
|
}
|
2020-03-25 17:49:02 -04:00
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
static int keylog_callback(gnutls_session_t session, const char *label,
|
|
|
|
const gnutls_datum_t *secret)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
2020-03-25 17:49:02 -04:00
|
|
|
gnutls_datum_t crandom;
|
|
|
|
gnutls_datum_t srandom;
|
|
|
|
|
|
|
|
gnutls_session_get_random(session, &crandom, &srandom);
|
|
|
|
if(crandom.size != 32) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-29 08:53:39 -04:00
|
|
|
|
2020-05-03 19:41:46 -04:00
|
|
|
Curl_tls_keylog_write(label, crandom.data, secret->data, secret->size);
|
|
|
|
return 0;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
2020-03-25 17:49:02 -04:00
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2020-03-25 17:49:02 -04:00
|
|
|
static int init_ngh3_conn(struct quicsocket *qs);
|
|
|
|
|
|
|
|
static int write_client_handshake(struct quicsocket *qs,
|
|
|
|
ngtcp2_crypto_level level,
|
|
|
|
const uint8_t *data, size_t len)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
2019-08-10 04:06:50 -04:00
|
|
|
struct quic_handshake *crypto_data;
|
2019-08-27 09:00:34 -04:00
|
|
|
int rv;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-11-18 05:27:30 -05:00
|
|
|
crypto_data = &qs->crypto_data[level];
|
2019-08-10 04:06:50 -04:00
|
|
|
if(crypto_data->buf == NULL) {
|
|
|
|
crypto_data->buf = malloc(4096);
|
2019-11-18 05:27:30 -05:00
|
|
|
if(!crypto_data->buf)
|
|
|
|
return 0;
|
2019-08-10 04:06:50 -04:00
|
|
|
crypto_data->alloclen = 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO Just pretend that handshake does not grow more than 4KiB for
|
|
|
|
now */
|
|
|
|
assert(crypto_data->len + len <= crypto_data->alloclen);
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
memcpy(&crypto_data->buf[crypto_data->len], data, len);
|
2019-08-10 04:06:50 -04:00
|
|
|
crypto_data->len += len;
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
rv = ngtcp2_conn_submit_crypto_data(
|
2019-11-18 05:27:30 -05:00
|
|
|
qs->qconn, level, (uint8_t *)(&crypto_data->buf[crypto_data->len] - len),
|
|
|
|
len);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(rv) {
|
2019-09-25 06:27:26 -04:00
|
|
|
H3BUGF(fprintf(stderr, "write_client_handshake failed\n"));
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
assert(0 == rv);
|
2019-08-27 09:00:34 -04:00
|
|
|
|
|
|
|
return 1;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
static int quic_set_encryption_secrets(SSL *ssl,
|
|
|
|
OSSL_ENCRYPTION_LEVEL ossl_level,
|
|
|
|
const uint8_t *rx_secret,
|
|
|
|
const uint8_t *tx_secret,
|
|
|
|
size_t secretlen)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl);
|
|
|
|
int level = quic_from_ossl_level(ossl_level);
|
|
|
|
|
2020-05-23 09:41:25 -04:00
|
|
|
if(ngtcp2_crypto_derive_and_install_rx_key(
|
|
|
|
qs->qconn, NULL, NULL, NULL, level, rx_secret, secretlen) != 0)
|
2020-03-25 17:49:02 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(ngtcp2_crypto_derive_and_install_tx_key(
|
2020-05-23 09:41:25 -04:00
|
|
|
qs->qconn, NULL, NULL, NULL, level, tx_secret, secretlen) != 0)
|
2020-03-25 17:49:02 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(level == NGTCP2_CRYPTO_LEVEL_APP) {
|
|
|
|
if(init_ngh3_conn(qs) != CURLE_OK)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int quic_add_handshake_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level,
|
|
|
|
const uint8_t *data, size_t len)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl);
|
|
|
|
ngtcp2_crypto_level level = quic_from_ossl_level(ossl_level);
|
|
|
|
|
|
|
|
return write_client_handshake(qs, level, data, len);
|
|
|
|
}
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
static int quic_flush_flight(SSL *ssl)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
|
|
|
(void)ssl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
static int quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level,
|
|
|
|
uint8_t alert)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
2019-08-27 09:00:34 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl);
|
|
|
|
(void)level;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
qs->tls_alert = alert;
|
|
|
|
return 1;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
static SSL_QUIC_METHOD quic_method = {quic_set_encryption_secrets,
|
|
|
|
quic_add_handshake_data,
|
|
|
|
quic_flush_flight, quic_send_alert};
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
static SSL_CTX *quic_ssl_ctx(struct Curl_easy *data)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
2019-08-27 09:00:34 -04:00
|
|
|
SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method());
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_3_VERSION);
|
|
|
|
SSL_CTX_set_max_proto_version(ssl_ctx, TLS1_3_VERSION);
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
SSL_CTX_set_default_verify_paths(ssl_ctx);
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
if(SSL_CTX_set_ciphersuites(ssl_ctx, QUIC_CIPHERS) != 1) {
|
2019-11-27 16:53:51 -05:00
|
|
|
char error_buffer[256];
|
|
|
|
ERR_error_string_n(ERR_get_error(), error_buffer, sizeof(error_buffer));
|
|
|
|
failf(data, "SSL_CTX_set_ciphersuites: %s", error_buffer);
|
2019-08-27 09:00:34 -04:00
|
|
|
return NULL;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
if(SSL_CTX_set1_groups_list(ssl_ctx, QUIC_GROUPS) != 1) {
|
|
|
|
failf(data, "SSL_CTX_set1_groups_list failed");
|
|
|
|
return NULL;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
SSL_CTX_set_quic_method(ssl_ctx, &quic_method);
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2020-05-03 19:41:46 -04:00
|
|
|
/* Open the file if a TLS or QUIC backend has not done this before. */
|
|
|
|
Curl_tls_keylog_open();
|
|
|
|
if(Curl_tls_keylog_enabled()) {
|
|
|
|
SSL_CTX_set_keylog_callback(ssl_ctx, keylog_callback);
|
2019-08-27 09:00:34 -04:00
|
|
|
}
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
return ssl_ctx;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
/** SSL callbacks ***/
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
static int quic_init_ssl(struct quicsocket *qs)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
|
|
|
const uint8_t *alpn = NULL;
|
|
|
|
size_t alpnlen = 0;
|
|
|
|
/* this will need some attention when HTTPS proxy over QUIC get fixed */
|
2019-08-12 10:28:28 -04:00
|
|
|
const char * const hostname = qs->conn->host.name;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
if(qs->ssl)
|
|
|
|
SSL_free(qs->ssl);
|
|
|
|
|
|
|
|
qs->ssl = SSL_new(qs->sslctx);
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
SSL_set_app_data(qs->ssl, qs);
|
2019-07-21 17:48:58 -04:00
|
|
|
SSL_set_connect_state(qs->ssl);
|
|
|
|
|
|
|
|
switch(qs->version) {
|
2019-08-10 21:49:03 -04:00
|
|
|
#ifdef NGTCP2_PROTO_VER
|
|
|
|
case NGTCP2_PROTO_VER:
|
2020-05-23 09:41:25 -04:00
|
|
|
alpn = (const uint8_t *)NGHTTP3_ALPN_H3;
|
|
|
|
alpnlen = sizeof(NGHTTP3_ALPN_H3) - 1;
|
2019-07-21 17:48:58 -04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if(alpn)
|
|
|
|
SSL_set_alpn_protos(qs->ssl, alpn, (int)alpnlen);
|
|
|
|
|
|
|
|
/* set SNI */
|
|
|
|
SSL_set_tlsext_host_name(qs->ssl, hostname);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-25 17:49:02 -04:00
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
static int secret_func(gnutls_session_t ssl,
|
|
|
|
gnutls_record_encryption_level_t gtls_level,
|
|
|
|
const void *rx_secret,
|
|
|
|
const void *tx_secret, size_t secretlen)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = gnutls_session_get_ptr(ssl);
|
|
|
|
int level = quic_from_gtls_level(gtls_level);
|
|
|
|
|
|
|
|
if(level != NGTCP2_CRYPTO_LEVEL_EARLY &&
|
|
|
|
ngtcp2_crypto_derive_and_install_rx_key(
|
2020-05-23 09:41:25 -04:00
|
|
|
qs->qconn, NULL, NULL, NULL, level, rx_secret, secretlen) != 0)
|
2020-03-25 17:49:02 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(ngtcp2_crypto_derive_and_install_tx_key(
|
2020-05-23 09:41:25 -04:00
|
|
|
qs->qconn, NULL, NULL, NULL, level, tx_secret, secretlen) != 0)
|
2020-03-25 17:49:02 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(level == NGTCP2_CRYPTO_LEVEL_APP) {
|
|
|
|
if(init_ngh3_conn(qs) != CURLE_OK)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_func(gnutls_session_t ssl,
|
|
|
|
gnutls_record_encryption_level_t gtls_level,
|
|
|
|
gnutls_handshake_description_t htype, const void *data,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = gnutls_session_get_ptr(ssl);
|
|
|
|
ngtcp2_crypto_level level = quic_from_gtls_level(gtls_level);
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if(htype == GNUTLS_HANDSHAKE_CHANGE_CIPHER_SPEC)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rv = write_client_handshake(qs, level, data, len);
|
|
|
|
if(rv == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int alert_read_func(gnutls_session_t ssl,
|
|
|
|
gnutls_record_encryption_level_t gtls_level,
|
|
|
|
gnutls_alert_level_t alert_level,
|
|
|
|
gnutls_alert_description_t alert_desc)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = gnutls_session_get_ptr(ssl);
|
|
|
|
(void)gtls_level;
|
|
|
|
(void)alert_level;
|
|
|
|
|
|
|
|
qs->tls_alert = alert_desc;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tp_recv_func(gnutls_session_t ssl, const uint8_t *data,
|
|
|
|
size_t data_size)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = gnutls_session_get_ptr(ssl);
|
|
|
|
ngtcp2_transport_params params;
|
|
|
|
|
|
|
|
if(ngtcp2_decode_transport_params(
|
|
|
|
¶ms, NGTCP2_TRANSPORT_PARAMS_TYPE_ENCRYPTED_EXTENSIONS,
|
|
|
|
data, data_size) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(ngtcp2_conn_set_remote_transport_params(qs->qconn, ¶ms) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tp_send_func(gnutls_session_t ssl, gnutls_buffer_t extdata)
|
|
|
|
{
|
|
|
|
struct quicsocket *qs = gnutls_session_get_ptr(ssl);
|
|
|
|
uint8_t paramsbuf[64];
|
|
|
|
ngtcp2_transport_params params;
|
|
|
|
ssize_t nwrite;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
ngtcp2_conn_get_local_transport_params(qs->qconn, ¶ms);
|
|
|
|
nwrite = ngtcp2_encode_transport_params(
|
|
|
|
paramsbuf, sizeof(paramsbuf), NGTCP2_TRANSPORT_PARAMS_TYPE_CLIENT_HELLO,
|
|
|
|
¶ms);
|
|
|
|
if(nwrite < 0) {
|
|
|
|
fprintf(stderr, "ngtcp2_encode_transport_params: %s\n",
|
|
|
|
ngtcp2_strerror((int)nwrite));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = gnutls_buffer_append_data(extdata, paramsbuf, nwrite);
|
|
|
|
if(rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return (int)nwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int quic_init_ssl(struct quicsocket *qs)
|
|
|
|
{
|
|
|
|
gnutls_datum_t alpn = {NULL, 0};
|
|
|
|
/* this will need some attention when HTTPS proxy over QUIC get fixed */
|
|
|
|
const char * const hostname = qs->conn->host.name;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if(qs->ssl)
|
|
|
|
gnutls_deinit(qs->ssl);
|
|
|
|
|
|
|
|
gnutls_init(&qs->ssl, GNUTLS_CLIENT);
|
|
|
|
gnutls_session_set_ptr(qs->ssl, qs);
|
|
|
|
|
|
|
|
rc = gnutls_priority_set_direct(qs->ssl, QUIC_PRIORITY, NULL);
|
|
|
|
if(rc < 0) {
|
|
|
|
fprintf(stderr, "gnutls_priority_set_direct failed: %s\n",
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gnutls_handshake_set_secret_function(qs->ssl, secret_func);
|
|
|
|
gnutls_handshake_set_read_function(qs->ssl, read_func);
|
|
|
|
gnutls_alert_set_read_function(qs->ssl, alert_read_func);
|
|
|
|
|
|
|
|
rc = gnutls_session_ext_register(qs->ssl, "QUIC Transport Parameters",
|
|
|
|
0xffa5, GNUTLS_EXT_TLS,
|
|
|
|
tp_recv_func, tp_send_func,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
GNUTLS_EXT_FLAG_TLS |
|
|
|
|
GNUTLS_EXT_FLAG_CLIENT_HELLO |
|
|
|
|
GNUTLS_EXT_FLAG_EE);
|
|
|
|
if(rc < 0) {
|
|
|
|
fprintf(stderr, "gnutls_session_ext_register failed: %s\n",
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:41:46 -04:00
|
|
|
/* Open the file if a TLS or QUIC backend has not done this before. */
|
|
|
|
Curl_tls_keylog_open();
|
|
|
|
if(Curl_tls_keylog_enabled()) {
|
|
|
|
gnutls_session_set_keylog_function(qs->ssl, keylog_callback);
|
2020-03-25 17:49:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(qs->cred)
|
|
|
|
gnutls_certificate_free_credentials(qs->cred);
|
|
|
|
|
|
|
|
rc = gnutls_certificate_allocate_credentials(&qs->cred);
|
|
|
|
if(rc < 0) {
|
|
|
|
fprintf(stderr, "gnutls_certificate_allocate_credentials failed: %s\n",
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = gnutls_certificate_set_x509_system_trust(qs->cred);
|
|
|
|
if(rc < 0) {
|
|
|
|
fprintf(stderr, "gnutls_certificate_set_x509_system_trust failed: %s\n",
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = gnutls_credentials_set(qs->ssl, GNUTLS_CRD_CERTIFICATE, qs->cred);
|
|
|
|
if(rc < 0) {
|
|
|
|
fprintf(stderr, "gnutls_credentials_set failed: %s\n",
|
|
|
|
gnutls_strerror(rc));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(qs->version) {
|
|
|
|
#ifdef NGTCP2_PROTO_VER
|
|
|
|
case NGTCP2_PROTO_VER:
|
2020-05-23 09:41:25 -04:00
|
|
|
/* strip the first byte (the length) from NGHTTP3_ALPN_H3 */
|
|
|
|
alpn.data = (unsigned char *)NGHTTP3_ALPN_H3 + 1;
|
|
|
|
alpn.size = sizeof(NGHTTP3_ALPN_H3) - 2;
|
2020-03-25 17:49:02 -04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if(alpn.data)
|
|
|
|
gnutls_alpn_set_protocols(qs->ssl, &alpn, 1, 0);
|
|
|
|
|
|
|
|
/* set SNI */
|
|
|
|
gnutls_server_name_set(qs->ssl, GNUTLS_NAME_DNS, hostname, strlen(hostname));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
static int
|
|
|
|
cb_recv_crypto_data(ngtcp2_conn *tconn, ngtcp2_crypto_level crypto_level,
|
|
|
|
uint64_t offset,
|
|
|
|
const uint8_t *data, size_t datalen,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
(void)offset;
|
2020-05-23 09:41:25 -04:00
|
|
|
(void)user_data;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2020-05-23 09:41:25 -04:00
|
|
|
if(ngtcp2_crypto_read_write_crypto_data(tconn, crypto_level, data,
|
2019-08-27 09:00:34 -04:00
|
|
|
datalen) != 0)
|
2019-07-21 17:48:58 -04:00
|
|
|
return NGTCP2_ERR_CRYPTO;
|
|
|
|
|
2019-08-27 09:00:34 -04:00
|
|
|
return 0;
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_handshake_completed(ngtcp2_conn *tconn, void *user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-07-21 17:48:58 -04:00
|
|
|
(void)tconn;
|
2019-08-12 10:28:28 -04:00
|
|
|
infof(qs->conn->data, "QUIC handshake is completed\n");
|
2019-08-12 00:09:44 -04:00
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-12 05:39:41 -05:00
|
|
|
static void extend_stream_window(ngtcp2_conn *tconn,
|
|
|
|
struct HTTP *stream)
|
|
|
|
{
|
|
|
|
size_t thismuch = stream->unacked_window;
|
|
|
|
ngtcp2_conn_extend_max_stream_offset(tconn, stream->stream3_id, thismuch);
|
|
|
|
ngtcp2_conn_extend_max_offset(tconn, thismuch);
|
|
|
|
stream->unacked_window = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 03:33:51 -04:00
|
|
|
static int cb_recv_stream_data(ngtcp2_conn *tconn, uint32_t flags,
|
|
|
|
int64_t stream_id, uint64_t offset,
|
2019-07-21 17:48:58 -04:00
|
|
|
const uint8_t *buf, size_t buflen,
|
|
|
|
void *user_data, void *stream_user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-08-12 00:09:44 -04:00
|
|
|
ssize_t nconsumed;
|
2020-06-08 03:33:51 -04:00
|
|
|
int fin = flags & NGTCP2_STREAM_DATA_FLAG_FIN ? 1 : 0;
|
2019-07-21 17:48:58 -04:00
|
|
|
(void)offset;
|
|
|
|
(void)stream_user_data;
|
2019-08-12 10:28:28 -04:00
|
|
|
|
2019-08-12 00:09:44 -04:00
|
|
|
nconsumed =
|
2019-08-12 10:28:28 -04:00
|
|
|
nghttp3_conn_read_stream(qs->h3conn, stream_id, buf, buflen, fin);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(nconsumed < 0) {
|
2019-08-12 10:28:28 -04:00
|
|
|
failf(qs->conn->data, "nghttp3_conn_read_stream returned error: %s\n",
|
2019-08-12 00:09:44 -04:00
|
|
|
nghttp3_strerror((int)nconsumed));
|
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-11-12 05:39:41 -05:00
|
|
|
/* number of bytes inside buflen which consists of framing overhead
|
|
|
|
* including QPACK HEADERS. In other words, it does not consume payload of
|
|
|
|
* DATA frame. */
|
2019-08-15 01:52:09 -04:00
|
|
|
ngtcp2_conn_extend_max_stream_offset(tconn, stream_id, nconsumed);
|
|
|
|
ngtcp2_conn_extend_max_offset(tconn, nconsumed);
|
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cb_acked_stream_data_offset(ngtcp2_conn *tconn, int64_t stream_id,
|
|
|
|
uint64_t offset, size_t datalen, void *user_data,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-08-12 00:09:44 -04:00
|
|
|
int rv;
|
2019-07-21 17:48:58 -04:00
|
|
|
(void)stream_id;
|
|
|
|
(void)tconn;
|
|
|
|
(void)offset;
|
|
|
|
(void)datalen;
|
|
|
|
(void)stream_user_data;
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rv = nghttp3_conn_add_ack_offset(qs->h3conn, stream_id, datalen);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rv != 0) {
|
2019-08-12 10:28:28 -04:00
|
|
|
failf(qs->conn->data, "nghttp3_conn_add_ack_offset returned error: %s\n",
|
2019-08-12 00:09:44 -04:00
|
|
|
nghttp3_strerror(rv));
|
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_stream_close(ngtcp2_conn *tconn, int64_t stream_id,
|
2019-08-10 04:06:50 -04:00
|
|
|
uint64_t app_error_code,
|
2019-07-21 17:48:58 -04:00
|
|
|
void *user_data, void *stream_user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-08-12 00:09:44 -04:00
|
|
|
int rv;
|
2019-07-21 17:48:58 -04:00
|
|
|
(void)tconn;
|
|
|
|
(void)stream_user_data;
|
|
|
|
/* stream is closed... */
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rv = nghttp3_conn_close_stream(qs->h3conn, stream_id,
|
|
|
|
app_error_code);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rv != 0) {
|
2019-08-12 10:28:28 -04:00
|
|
|
failf(qs->conn->data, "nghttp3_conn_close_stream returned error: %s\n",
|
2019-08-12 00:09:44 -04:00
|
|
|
nghttp3_strerror(rv));
|
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_stream_reset(ngtcp2_conn *tconn, int64_t stream_id,
|
|
|
|
uint64_t final_size, uint64_t app_error_code,
|
|
|
|
void *user_data, void *stream_user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-08-12 00:09:44 -04:00
|
|
|
int rv;
|
|
|
|
(void)tconn;
|
|
|
|
(void)final_size;
|
|
|
|
(void)app_error_code;
|
|
|
|
(void)stream_user_data;
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rv = nghttp3_conn_reset_stream(qs->h3conn, stream_id);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rv != 0) {
|
2019-08-12 10:28:28 -04:00
|
|
|
failf(qs->conn->data, "nghttp3_conn_reset_stream returned error: %s\n",
|
2019-08-12 00:09:44 -04:00
|
|
|
nghttp3_strerror(rv));
|
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-10 04:06:50 -04:00
|
|
|
static int cb_extend_max_local_streams_bidi(ngtcp2_conn *tconn,
|
|
|
|
uint64_t max_streams,
|
|
|
|
void *user_data)
|
2019-07-21 17:48:58 -04:00
|
|
|
{
|
|
|
|
(void)tconn;
|
|
|
|
(void)max_streams;
|
|
|
|
(void)user_data;
|
2019-08-12 00:09:44 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_extend_max_stream_data(ngtcp2_conn *tconn, int64_t stream_id,
|
|
|
|
uint64_t max_data, void *user_data,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-08-12 00:09:44 -04:00
|
|
|
int rv;
|
|
|
|
(void)tconn;
|
|
|
|
(void)max_data;
|
|
|
|
(void)stream_user_data;
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rv = nghttp3_conn_unblock_stream(qs->h3conn, stream_id);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rv != 0) {
|
2019-08-12 10:28:28 -04:00
|
|
|
failf(qs->conn->data, "nghttp3_conn_unblock_stream returned error: %s\n",
|
2019-08-12 00:09:44 -04:00
|
|
|
nghttp3_strerror(rv));
|
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_get_new_connection_id(ngtcp2_conn *tconn, ngtcp2_cid *cid,
|
|
|
|
uint8_t *token, size_t cidlen,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = (struct quicsocket *)user_data;
|
2019-07-21 17:48:58 -04:00
|
|
|
CURLcode result;
|
|
|
|
(void)tconn;
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
result = Curl_rand(qs->conn->data, cid->data, cidlen);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(result)
|
2019-08-10 04:06:50 -04:00
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
|
|
|
cid->datalen = cidlen;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
result = Curl_rand(qs->conn->data, token, NGTCP2_STATELESS_RESET_TOKENLEN);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(result)
|
2019-08-10 04:06:50 -04:00
|
|
|
return NGTCP2_ERR_CALLBACK_FAILURE;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-15 04:20:35 -04:00
|
|
|
static ngtcp2_conn_callbacks ng_callbacks = {
|
2020-05-23 09:41:25 -04:00
|
|
|
ngtcp2_crypto_client_initial_cb,
|
2019-08-15 04:20:35 -04:00
|
|
|
NULL, /* recv_client_initial */
|
|
|
|
cb_recv_crypto_data,
|
|
|
|
cb_handshake_completed,
|
|
|
|
NULL, /* recv_version_negotiation */
|
2019-08-29 03:09:26 -04:00
|
|
|
ngtcp2_crypto_encrypt_cb,
|
|
|
|
ngtcp2_crypto_decrypt_cb,
|
|
|
|
ngtcp2_crypto_hp_mask_cb,
|
2019-08-15 04:20:35 -04:00
|
|
|
cb_recv_stream_data,
|
|
|
|
NULL, /* acked_crypto_offset */
|
|
|
|
cb_acked_stream_data_offset,
|
|
|
|
NULL, /* stream_open */
|
|
|
|
cb_stream_close,
|
|
|
|
NULL, /* recv_stateless_reset */
|
2020-05-23 09:41:25 -04:00
|
|
|
ngtcp2_crypto_recv_retry_cb,
|
2019-08-15 04:20:35 -04:00
|
|
|
cb_extend_max_local_streams_bidi,
|
|
|
|
NULL, /* extend_max_local_streams_uni */
|
|
|
|
NULL, /* rand */
|
|
|
|
cb_get_new_connection_id,
|
|
|
|
NULL, /* remove_connection_id */
|
2019-12-18 13:56:16 -05:00
|
|
|
ngtcp2_crypto_update_key_cb, /* update_key */
|
2019-08-15 04:20:35 -04:00
|
|
|
NULL, /* path_validation */
|
|
|
|
NULL, /* select_preferred_addr */
|
|
|
|
cb_stream_reset,
|
|
|
|
NULL, /* extend_max_remote_streams_bidi */
|
|
|
|
NULL, /* extend_max_remote_streams_uni */
|
|
|
|
cb_extend_max_stream_data,
|
2020-01-29 09:56:16 -05:00
|
|
|
NULL, /* dcid_status */
|
2020-06-08 03:33:51 -04:00
|
|
|
NULL, /* handshake_confirmed */
|
|
|
|
NULL /* recv_new_token */
|
2019-08-15 04:20:35 -04:00
|
|
|
};
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
/*
|
|
|
|
* Might be called twice for happy eyeballs.
|
|
|
|
*/
|
2019-07-21 17:48:58 -04:00
|
|
|
CURLcode Curl_quic_connect(struct connectdata *conn,
|
|
|
|
curl_socket_t sockfd,
|
2019-08-12 10:28:28 -04:00
|
|
|
int sockindex,
|
2019-07-21 17:48:58 -04:00
|
|
|
const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen)
|
|
|
|
{
|
|
|
|
int rc;
|
2019-08-10 04:06:50 -04:00
|
|
|
int rv;
|
2019-07-21 17:48:58 -04:00
|
|
|
CURLcode result;
|
|
|
|
ngtcp2_path path; /* TODO: this must be initialized properly */
|
2019-08-12 10:28:28 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
struct quicsocket *qs = &conn->hequic[sockindex];
|
|
|
|
char ipbuf[40];
|
|
|
|
long port;
|
2020-05-07 08:17:42 -04:00
|
|
|
int qfd;
|
2019-08-12 10:28:28 -04:00
|
|
|
|
|
|
|
qs->conn = conn;
|
|
|
|
|
|
|
|
/* extract the used address as a string */
|
2019-08-30 05:29:55 -04:00
|
|
|
if(!Curl_addr2string((struct sockaddr*)addr, addrlen, ipbuf, &port)) {
|
2019-08-12 10:28:28 -04:00
|
|
|
char buffer[STRERROR_LEN];
|
|
|
|
failf(data, "ssrem inet_ntop() failed with errno %d: %s",
|
2019-10-27 18:48:43 -04:00
|
|
|
SOCKERRNO, Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
|
2019-08-12 10:28:28 -04:00
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, "Connect socket %d over QUIC to %s:%ld\n",
|
|
|
|
sockfd, ipbuf, port);
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-10 21:49:03 -04:00
|
|
|
qs->version = NGTCP2_PROTO_VER;
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
2019-08-12 10:28:28 -04:00
|
|
|
qs->sslctx = quic_ssl_ctx(data);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(!qs->sslctx)
|
2019-12-24 05:17:16 -05:00
|
|
|
return CURLE_QUIC_CONNECT_ERROR;
|
2020-03-25 17:49:02 -04:00
|
|
|
#endif
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
if(quic_init_ssl(qs))
|
2019-12-24 05:17:16 -05:00
|
|
|
return CURLE_QUIC_CONNECT_ERROR;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
qs->dcid.datalen = NGTCP2_MAX_CIDLEN;
|
2019-08-12 10:28:28 -04:00
|
|
|
result = Curl_rand(data, qs->dcid.data, NGTCP2_MAX_CIDLEN);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
qs->scid.datalen = NGTCP2_MAX_CIDLEN;
|
2019-08-12 10:28:28 -04:00
|
|
|
result = Curl_rand(data, qs->scid.data, NGTCP2_MAX_CIDLEN);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2020-05-07 08:17:42 -04:00
|
|
|
(void)Curl_qlogdir(data, qs->scid.data, NGTCP2_MAX_CIDLEN, &qfd);
|
|
|
|
qs->qlogfd = qfd; /* -1 if failure above */
|
2020-05-23 09:41:25 -04:00
|
|
|
quic_settings(qs, data->set.buffer_size);
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2019-08-10 04:06:50 -04:00
|
|
|
qs->local_addrlen = sizeof(qs->local_addr);
|
|
|
|
rv = getsockname(sockfd, (struct sockaddr *)&qs->local_addr,
|
|
|
|
&qs->local_addrlen);
|
2019-08-12 10:28:28 -04:00
|
|
|
if(rv == -1)
|
2019-12-24 05:17:16 -05:00
|
|
|
return CURLE_QUIC_CONNECT_ERROR;
|
2019-08-10 04:06:50 -04:00
|
|
|
|
|
|
|
ngtcp2_addr_init(&path.local, (uint8_t *)&qs->local_addr, qs->local_addrlen,
|
|
|
|
NULL);
|
|
|
|
ngtcp2_addr_init(&path.remote, (uint8_t*)addr, addrlen, NULL);
|
|
|
|
|
|
|
|
#ifdef NGTCP2_PROTO_VER
|
|
|
|
#define QUICVER NGTCP2_PROTO_VER
|
2019-07-21 17:48:58 -04:00
|
|
|
#else
|
|
|
|
#error "unsupported ngtcp2 version"
|
|
|
|
#endif
|
2019-08-12 10:28:28 -04:00
|
|
|
rc = ngtcp2_conn_client_new(&qs->qconn, &qs->dcid, &qs->scid, &path, QUICVER,
|
2019-08-15 04:20:35 -04:00
|
|
|
&ng_callbacks, &qs->settings, NULL, qs);
|
2019-07-21 17:48:58 -04:00
|
|
|
if(rc)
|
2019-12-24 05:17:16 -05:00
|
|
|
return CURLE_QUIC_CONNECT_ERROR;
|
2019-07-21 17:48:58 -04:00
|
|
|
|
2020-05-23 09:41:25 -04:00
|
|
|
ngtcp2_conn_set_tls_native_handle(qs->qconn, qs->ssl);
|
2019-07-21 17:48:58 -04:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store ngtp2 version info in this buffer, Prefix with a space. Return total
|
|
|
|
* length written.
|
|
|
|
*/
|
|
|
|
int Curl_quic_ver(char *p, size_t len)
|
|
|
|
{
|
2019-08-19 17:04:45 -04:00
|
|
|
ngtcp2_info *ng2 = ngtcp2_version(0);
|
2019-08-25 12:09:37 -04:00
|
|
|
nghttp3_info *ht3 = nghttp3_version(0);
|
2020-03-01 10:16:19 -05:00
|
|
|
return msnprintf(p, len, "ngtcp2/%s nghttp3/%s",
|
2019-08-25 12:09:37 -04:00
|
|
|
ng2->version_str, ht3->version_str);
|
2019-07-21 17:48:58 -04:00
|
|
|
}
|
|
|
|
|
2019-08-11 17:50:11 -04:00
|
|
|
static int ng_getsock(struct connectdata *conn, curl_socket_t *socks)
|
|
|
|
{
|
|
|
|
struct SingleRequest *k = &conn->data->req;
|
|
|
|
int bitmap = GETSOCK_BLANK;
|
|
|
|
|
|
|
|
socks[0] = conn->sock[FIRSTSOCKET];
|
|
|
|
|
|
|
|
/* in a HTTP/2 connection we can basically always get a frame so we should
|
|
|
|
always be ready for one */
|
|
|
|
bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
|
|
|
|
|
|
|
|
/* we're still uploading or the HTTP/2 layer wants to send data */
|
|
|
|
if((k->keepon & (KEEP_SEND|KEEP_SEND_PAUSE)) == KEEP_SEND)
|
|
|
|
bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ng_perform_getsock(const struct connectdata *conn,
|
|
|
|
curl_socket_t *socks)
|
|
|
|
{
|
|
|
|
return ng_getsock((struct connectdata *)conn, socks);
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:55:16 -04:00
|
|
|
static CURLcode qs_disconnect(struct quicsocket *qs)
|
2019-08-11 17:50:11 -04:00
|
|
|
{
|
2019-11-18 05:27:30 -05:00
|
|
|
int i;
|
2020-05-07 08:17:42 -04:00
|
|
|
if(qs->qlogfd != -1)
|
|
|
|
close(qs->qlogfd);
|
2019-11-18 05:27:30 -05:00
|
|
|
if(qs->ssl)
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
2019-11-18 05:27:30 -05:00
|
|
|
SSL_free(qs->ssl);
|
2020-03-25 17:49:02 -04:00
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
gnutls_deinit(qs->ssl);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_GNUTLS
|
|
|
|
if(qs->cred)
|
|
|
|
gnutls_certificate_free_credentials(qs->cred);
|
|
|
|
#endif
|
2019-11-18 05:27:30 -05:00
|
|
|
for(i = 0; i < 3; i++)
|
|
|
|
free(qs->crypto_data[i].buf);
|
|
|
|
nghttp3_conn_del(qs->h3conn);
|
|
|
|
ngtcp2_conn_del(qs->qconn);
|
2020-03-25 17:49:02 -04:00
|
|
|
#ifdef USE_OPENSSL
|
2019-11-18 05:27:30 -05:00
|
|
|
SSL_CTX_free(qs->sslctx);
|
2020-03-25 17:49:02 -04:00
|
|
|
#endif
|
2019-08-11 17:50:11 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-25 11:55:16 -04:00
|
|
|
static CURLcode ng_disconnect(struct connectdata *conn,
|
|
|
|
bool dead_connection)
|
|
|
|
{
|
|
|
|
(void)dead_connection;
|
|
|
|
return qs_disconnect(&conn->hequic[0]);
|
|
|
|
}
|
|
|
|
|
2019-08-11 17:50:11 -04:00
|
|
|
static unsigned int ng_conncheck(struct connectdata *conn,
|
|
|
|
unsigned int checks_to_perform)
|
|
|
|
{
|
|
|
|
(void)conn;
|
|
|
|
(void)checks_to_perform;
|
|
|
|
return CONNRESULT_NONE;
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:08:18 -04:00
|
|
|
static const struct Curl_handler Curl_handler_http3 = {
|
2019-08-11 17:50:11 -04:00
|
|
|
"HTTPS", /* scheme */
|
|
|
|
ZERO_NULL, /* setup_connection */
|
|
|
|
Curl_http, /* do_it */
|
|
|
|
Curl_http_done, /* done */
|
|
|
|
ZERO_NULL, /* do_more */
|
|
|
|
ZERO_NULL, /* connect_it */
|
|
|
|
ZERO_NULL, /* connecting */
|
|
|
|
ZERO_NULL, /* doing */
|
|
|
|
ng_getsock, /* proto_getsock */
|
|
|
|
ng_getsock, /* doing_getsock */
|
|
|
|
ZERO_NULL, /* domore_getsock */
|
|
|
|
ng_perform_getsock, /* perform_getsock */
|
|
|
|
ng_disconnect, /* disconnect */
|
|
|
|
ZERO_NULL, /* readwrite */
|
|
|
|
ng_conncheck, /* connection_check */
|
|
|
|
PORT_HTTP, /* defport */
|
|
|
|
CURLPROTO_HTTPS, /* protocol */
|
|
|
|
PROTOPT_SSL | PROTOPT_STREAM /* flags */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cb_h3_stream_close(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
uint64_t app_error_code, void *user_data,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2019-08-15 01:52:56 -04:00
|
|
|
struct Curl_easy *data = stream_user_data;
|
|
|
|
struct HTTP *stream = data->req.protop;
|
2019-08-11 17:50:11 -04:00
|
|
|
(void)conn;
|
|
|
|
(void)stream_id;
|
|
|
|
(void)app_error_code;
|
|
|
|
(void)user_data;
|
2019-09-25 06:27:26 -04:00
|
|
|
H3BUGF(infof(data, "cb_h3_stream_close CALLED\n"));
|
2019-08-15 01:52:56 -04:00
|
|
|
|
|
|
|
stream->closed = TRUE;
|
2019-08-28 08:39:07 -04:00
|
|
|
Curl_expire(data, 0, EXPIRE_QUIC);
|
2019-11-16 02:57:45 -05:00
|
|
|
/* make sure that ngh3_stream_recv is called again to complete the transfer
|
|
|
|
even if there are no more packets to be received from the server. */
|
|
|
|
data->state.drain = 1;
|
2019-08-11 17:50:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-16 02:57:45 -05:00
|
|
|
/*
|
|
|
|
* write_data() copies data to the stream's receive buffer. If not enough
|
|
|
|
* space is available in the receive buffer, it copies the rest to the
|
|
|
|
* stream's overflow buffer.
|
|
|
|
*/
|
2020-05-04 05:37:12 -04:00
|
|
|
static CURLcode write_data(struct HTTP *stream, const void *mem, size_t memlen)
|
2019-11-16 02:57:45 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
const char *buf = mem;
|
|
|
|
size_t ncopy = memlen;
|
|
|
|
/* copy as much as possible to the receive buffer */
|
|
|
|
if(stream->len) {
|
|
|
|
size_t len = CURLMIN(ncopy, stream->len);
|
|
|
|
memcpy(stream->mem, buf, len);
|
|
|
|
stream->len -= len;
|
|
|
|
stream->memlen += len;
|
|
|
|
stream->mem += len;
|
|
|
|
buf += len;
|
|
|
|
ncopy -= len;
|
|
|
|
}
|
|
|
|
/* copy the rest to the overflow buffer */
|
2020-05-04 05:37:12 -04:00
|
|
|
if(ncopy)
|
|
|
|
result = Curl_dyn_addn(&stream->overflow, buf, ncopy);
|
2019-11-16 02:57:45 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_h3_recv_data(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
const uint8_t *buf, size_t buflen,
|
|
|
|
void *user_data, void *stream_user_data)
|
|
|
|
{
|
|
|
|
struct Curl_easy *data = stream_user_data;
|
|
|
|
struct HTTP *stream = data->req.protop;
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
(void)conn;
|
|
|
|
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, buf, buflen);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stream->unacked_window += buflen;
|
2019-11-12 05:39:41 -05:00
|
|
|
(void)stream_id;
|
|
|
|
(void)user_data;
|
2019-08-11 17:50:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_h3_deferred_consume(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
size_t consumed, void *user_data,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
2019-08-15 01:52:09 -04:00
|
|
|
struct quicsocket *qs = user_data;
|
2019-08-11 17:50:11 -04:00
|
|
|
(void)conn;
|
|
|
|
(void)stream_user_data;
|
2019-11-12 05:39:41 -05:00
|
|
|
(void)stream_id;
|
2019-08-15 01:52:09 -04:00
|
|
|
|
|
|
|
ngtcp2_conn_extend_max_stream_offset(qs->qconn, stream_id, consumed);
|
|
|
|
ngtcp2_conn_extend_max_offset(qs->qconn, consumed);
|
2019-08-11 17:50:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-12 05:10:56 -04:00
|
|
|
/* Decode HTTP status code. Returns -1 if no valid status code was
|
|
|
|
decoded. (duplicate from http2.c) */
|
|
|
|
static int decode_status_code(const uint8_t *value, size_t len)
|
2019-08-11 17:50:11 -04:00
|
|
|
{
|
2019-08-12 05:10:56 -04:00
|
|
|
int i;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if(len != 3) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
for(i = 0; i < 3; ++i) {
|
|
|
|
char c = value[i];
|
|
|
|
|
|
|
|
if(c < '0' || c > '9') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res *= 10;
|
|
|
|
res += c - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_h3_end_headers(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
void *user_data, void *stream_user_data)
|
|
|
|
{
|
|
|
|
struct Curl_easy *data = stream_user_data;
|
|
|
|
struct HTTP *stream = data->req.protop;
|
2019-11-16 02:57:45 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2019-08-11 17:50:11 -04:00
|
|
|
(void)conn;
|
|
|
|
(void)stream_id;
|
|
|
|
(void)user_data;
|
2019-08-12 05:10:56 -04:00
|
|
|
|
2019-11-16 02:57:45 -05:00
|
|
|
/* add a CRLF only if we've received some headers */
|
|
|
|
if(stream->firstheader) {
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, "\r\n", 2);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-12 05:10:56 -04:00
|
|
|
}
|
2019-08-11 17:50:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_h3_recv_header(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
int32_t token, nghttp3_rcbuf *name,
|
|
|
|
nghttp3_rcbuf *value, uint8_t flags,
|
|
|
|
void *user_data, void *stream_user_data)
|
|
|
|
{
|
2019-08-12 05:10:56 -04:00
|
|
|
nghttp3_vec h3name = nghttp3_rcbuf_get_buf(name);
|
|
|
|
nghttp3_vec h3val = nghttp3_rcbuf_get_buf(value);
|
|
|
|
struct Curl_easy *data = stream_user_data;
|
|
|
|
struct HTTP *stream = data->req.protop;
|
2019-11-16 02:57:45 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2019-08-11 17:50:11 -04:00
|
|
|
(void)conn;
|
|
|
|
(void)stream_id;
|
|
|
|
(void)token;
|
|
|
|
(void)flags;
|
|
|
|
(void)user_data;
|
2019-08-12 05:10:56 -04:00
|
|
|
|
|
|
|
if(h3name.len == sizeof(":status") - 1 &&
|
|
|
|
!memcmp(":status", h3name.base, h3name.len)) {
|
2019-11-16 02:57:45 -05:00
|
|
|
char line[14]; /* status line is always 13 characters long */
|
|
|
|
size_t ncopy;
|
2019-08-12 05:10:56 -04:00
|
|
|
int status = decode_status_code(h3val.base, h3val.len);
|
|
|
|
DEBUGASSERT(status != -1);
|
2019-11-16 02:57:45 -05:00
|
|
|
ncopy = msnprintf(line, sizeof(line), "HTTP/3 %03d \r\n", status);
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, line, ncopy);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-12 05:10:56 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* store as a HTTP1-style header */
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, h3name.base, h3name.len);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, ": ", 2);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, h3val.base, h3val.len);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-04 05:37:12 -04:00
|
|
|
result = write_data(stream, "\r\n", 2);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(result) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-12 05:10:56 -04:00
|
|
|
}
|
|
|
|
|
2019-11-16 02:57:45 -05:00
|
|
|
stream->firstheader = TRUE;
|
2019-08-11 17:50:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cb_h3_send_stop_sending(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
uint64_t app_error_code,
|
|
|
|
void *user_data,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
|
|
|
(void)conn;
|
|
|
|
(void)stream_id;
|
|
|
|
(void)app_error_code;
|
|
|
|
(void)user_data;
|
|
|
|
(void)stream_user_data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nghttp3_conn_callbacks ngh3_callbacks = {
|
2019-08-22 08:08:18 -04:00
|
|
|
cb_h3_acked_stream_data, /* acked_stream_data */
|
2019-08-11 17:50:11 -04:00
|
|
|
cb_h3_stream_close,
|
|
|
|
cb_h3_recv_data,
|
|
|
|
cb_h3_deferred_consume,
|
2019-08-12 05:10:56 -04:00
|
|
|
NULL, /* begin_headers */
|
2019-08-11 17:50:11 -04:00
|
|
|
cb_h3_recv_header,
|
2019-08-12 05:10:56 -04:00
|
|
|
cb_h3_end_headers,
|
2019-08-11 17:50:11 -04:00
|
|
|
NULL, /* begin_trailers */
|
|
|
|
cb_h3_recv_header,
|
|
|
|
NULL, /* end_trailers */
|
|
|
|
NULL, /* http_begin_push_promise */
|
|
|
|
NULL, /* http_recv_push_promise */
|
|
|
|
NULL, /* http_end_push_promise */
|
|
|
|
NULL, /* http_cancel_push */
|
|
|
|
cb_h3_send_stop_sending,
|
|
|
|
NULL, /* push_stream */
|
2019-08-22 08:08:18 -04:00
|
|
|
NULL, /* end_stream */
|
2019-08-11 17:50:11 -04:00
|
|
|
};
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
static int init_ngh3_conn(struct quicsocket *qs)
|
2019-08-12 00:09:44 -04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
int rc;
|
|
|
|
int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id;
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
if(ngtcp2_conn_get_max_local_streams_uni(qs->qconn) < 3) {
|
|
|
|
failf(qs->conn->data, "too few available QUIC streams");
|
2019-12-24 05:17:16 -05:00
|
|
|
return CURLE_QUIC_CONNECT_ERROR;
|
2019-08-12 00:09:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
nghttp3_conn_settings_default(&qs->h3settings);
|
|
|
|
|
|
|
|
rc = nghttp3_conn_client_new(&qs->h3conn,
|
|
|
|
&ngh3_callbacks,
|
|
|
|
&qs->h3settings,
|
|
|
|
nghttp3_mem_default(),
|
2019-08-15 01:52:09 -04:00
|
|
|
qs);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rc) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rc = ngtcp2_conn_open_uni_stream(qs->qconn, &ctrl_stream_id, NULL);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rc) {
|
2019-12-24 05:17:16 -05:00
|
|
|
result = CURLE_QUIC_CONNECT_ERROR;
|
2019-08-12 00:09:44 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = nghttp3_conn_bind_control_stream(qs->h3conn, ctrl_stream_id);
|
|
|
|
if(rc) {
|
2019-12-24 05:17:16 -05:00
|
|
|
result = CURLE_QUIC_CONNECT_ERROR;
|
2019-08-12 00:09:44 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rc = ngtcp2_conn_open_uni_stream(qs->qconn, &qpack_enc_stream_id, NULL);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rc) {
|
2019-12-24 05:17:16 -05:00
|
|
|
result = CURLE_QUIC_CONNECT_ERROR;
|
2019-08-12 00:09:44 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rc = ngtcp2_conn_open_uni_stream(qs->qconn, &qpack_dec_stream_id, NULL);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(rc) {
|
2019-12-24 05:17:16 -05:00
|
|
|
result = CURLE_QUIC_CONNECT_ERROR;
|
2019-08-12 00:09:44 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = nghttp3_conn_bind_qpack_streams(qs->h3conn, qpack_enc_stream_id,
|
|
|
|
qpack_dec_stream_id);
|
|
|
|
if(rc) {
|
2019-12-24 05:17:16 -05:00
|
|
|
result = CURLE_QUIC_CONNECT_ERROR;
|
2019-08-12 00:09:44 -04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
fail:
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-11 17:50:11 -04:00
|
|
|
static Curl_recv ngh3_stream_recv;
|
|
|
|
static Curl_send ngh3_stream_send;
|
|
|
|
|
2019-11-16 02:57:45 -05:00
|
|
|
static size_t drain_overflow_buffer(struct HTTP *stream)
|
|
|
|
{
|
2020-05-04 05:37:12 -04:00
|
|
|
size_t overlen = Curl_dyn_len(&stream->overflow);
|
|
|
|
size_t ncopy = CURLMIN(overlen, stream->len);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(ncopy > 0) {
|
2020-05-04 05:37:12 -04:00
|
|
|
memcpy(stream->mem, Curl_dyn_ptr(&stream->overflow), ncopy);
|
2019-11-16 02:57:45 -05:00
|
|
|
stream->len -= ncopy;
|
|
|
|
stream->mem += ncopy;
|
|
|
|
stream->memlen += ncopy;
|
2020-05-04 05:37:12 -04:00
|
|
|
if(ncopy != overlen)
|
|
|
|
/* make the buffer only keep the tail */
|
|
|
|
(void)Curl_dyn_tail(&stream->overflow, overlen - ncopy);
|
2019-11-16 02:57:45 -05:00
|
|
|
}
|
|
|
|
return ncopy;
|
|
|
|
}
|
|
|
|
|
2019-08-22 08:08:18 -04:00
|
|
|
/* incoming data frames on the h3 stream */
|
2019-08-11 17:50:11 -04:00
|
|
|
static ssize_t ngh3_stream_recv(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
char *buf,
|
|
|
|
size_t buffersize,
|
|
|
|
CURLcode *curlcode)
|
|
|
|
{
|
2019-08-12 00:09:44 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
2019-08-12 05:10:56 -04:00
|
|
|
struct HTTP *stream = conn->data->req.protop;
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = conn->quic;
|
2019-08-12 05:10:56 -04:00
|
|
|
|
2019-08-24 12:43:31 -04:00
|
|
|
if(!stream->memlen) {
|
|
|
|
/* remember where to store incoming data for this stream and how big the
|
|
|
|
buffer is */
|
|
|
|
stream->mem = buf;
|
|
|
|
stream->len = buffersize;
|
|
|
|
}
|
|
|
|
/* else, there's data in the buffer already */
|
2019-08-12 00:09:44 -04:00
|
|
|
|
2019-11-16 02:57:45 -05:00
|
|
|
/* if there's data in the overflow buffer from a previous call, copy as much
|
|
|
|
as possible to the receive buffer before receiving more */
|
|
|
|
drain_overflow_buffer(stream);
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
if(ng_process_ingress(conn, sockfd, qs)) {
|
2019-08-12 00:09:44 -04:00
|
|
|
*curlcode = CURLE_RECV_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-12 10:28:28 -04:00
|
|
|
if(ng_flush_egress(conn, sockfd, qs)) {
|
2019-08-12 00:09:44 -04:00
|
|
|
*curlcode = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-12 05:10:56 -04:00
|
|
|
if(stream->memlen) {
|
2019-08-24 12:43:31 -04:00
|
|
|
ssize_t memlen = stream->memlen;
|
2019-08-12 05:10:56 -04:00
|
|
|
/* data arrived */
|
|
|
|
*curlcode = CURLE_OK;
|
2019-08-24 12:43:31 -04:00
|
|
|
/* reset to allow more data to come */
|
|
|
|
stream->memlen = 0;
|
|
|
|
stream->mem = buf;
|
|
|
|
stream->len = buffersize;
|
2019-11-16 02:57:45 -05:00
|
|
|
/* extend the stream window with the data we're consuming and send out
|
|
|
|
any additional packets to tell the server that we can receive more */
|
2019-11-12 05:39:41 -05:00
|
|
|
extend_stream_window(qs->qconn, stream);
|
2019-11-16 02:57:45 -05:00
|
|
|
if(ng_flush_egress(conn, sockfd, qs)) {
|
|
|
|
*curlcode = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-24 12:43:31 -04:00
|
|
|
return memlen;
|
2019-08-12 05:10:56 -04:00
|
|
|
}
|
2019-08-12 00:09:44 -04:00
|
|
|
|
2019-08-15 01:52:56 -04:00
|
|
|
if(stream->closed) {
|
|
|
|
*curlcode = CURLE_OK;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-12 05:10:56 -04:00
|
|
|
infof(conn->data, "ngh3_stream_recv returns 0 bytes and EAGAIN\n");
|
|
|
|
*curlcode = CURLE_AGAIN;
|
2019-08-12 00:09:44 -04:00
|
|
|
return -1;
|
2019-08-11 17:50:11 -04:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:08:18 -04:00
|
|
|
/* this amount of data has now been acked on this stream */
|
|
|
|
static int cb_h3_acked_stream_data(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
size_t datalen, void *user_data,
|
|
|
|
void *stream_user_data)
|
|
|
|
{
|
|
|
|
struct Curl_easy *data = stream_user_data;
|
|
|
|
struct HTTP *stream = data->req.protop;
|
|
|
|
(void)conn;
|
|
|
|
(void)stream_id;
|
|
|
|
(void)user_data;
|
|
|
|
|
|
|
|
if(!data->set.postfields) {
|
|
|
|
stream->h3out->used -= datalen;
|
2019-09-25 06:27:26 -04:00
|
|
|
H3BUGF(infof(data,
|
|
|
|
"cb_h3_acked_stream_data, %zd bytes, %zd left unacked\n",
|
|
|
|
datalen, stream->h3out->used));
|
2019-08-22 08:08:18 -04:00
|
|
|
DEBUGASSERT(stream->h3out->used < H3_SEND_SIZE);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-20 22:24:38 -04:00
|
|
|
static ssize_t cb_h3_readfunction(nghttp3_conn *conn, int64_t stream_id,
|
|
|
|
nghttp3_vec *vec, size_t veccnt,
|
|
|
|
uint32_t *pflags, void *user_data,
|
|
|
|
void *stream_user_data)
|
2019-08-20 05:01:36 -04:00
|
|
|
{
|
|
|
|
struct Curl_easy *data = stream_user_data;
|
2019-08-22 08:08:18 -04:00
|
|
|
size_t nread;
|
|
|
|
struct HTTP *stream = data->req.protop;
|
2019-08-20 05:01:36 -04:00
|
|
|
(void)conn;
|
|
|
|
(void)stream_id;
|
|
|
|
(void)user_data;
|
2019-09-20 22:24:38 -04:00
|
|
|
(void)veccnt;
|
2019-08-20 05:01:36 -04:00
|
|
|
|
|
|
|
if(data->set.postfields) {
|
2019-09-20 22:24:38 -04:00
|
|
|
vec[0].base = data->set.postfields;
|
|
|
|
vec[0].len = data->state.infilesize;
|
2019-08-20 05:01:36 -04:00
|
|
|
*pflags = NGHTTP3_DATA_FLAG_EOF;
|
2019-09-20 22:24:38 -04:00
|
|
|
return 1;
|
2019-08-20 05:01:36 -04:00
|
|
|
}
|
|
|
|
|
2019-08-22 08:08:18 -04:00
|
|
|
nread = CURLMIN(stream->upload_len, H3_SEND_SIZE - stream->h3out->used);
|
|
|
|
if(nread > 0) {
|
|
|
|
/* nghttp3 wants us to hold on to the data until it tells us it is okay to
|
|
|
|
delete it. Append the data at the end of the h3out buffer. Since we can
|
|
|
|
only return consecutive data, copy the amount that fits and the next
|
|
|
|
part comes in next invoke. */
|
|
|
|
struct h3out *out = stream->h3out;
|
|
|
|
if(nread + out->windex > H3_SEND_SIZE)
|
|
|
|
nread = H3_SEND_SIZE - out->windex;
|
|
|
|
|
|
|
|
memcpy(&out->buf[out->windex], stream->upload_mem, nread);
|
|
|
|
out->windex += nread;
|
|
|
|
out->used += nread;
|
|
|
|
|
|
|
|
/* that's the chunk we return to nghttp3 */
|
2019-09-20 22:24:38 -04:00
|
|
|
vec[0].base = &out->buf[out->windex];
|
|
|
|
vec[0].len = nread;
|
2019-08-22 08:08:18 -04:00
|
|
|
|
|
|
|
if(out->windex == H3_SEND_SIZE)
|
|
|
|
out->windex = 0; /* wrap */
|
|
|
|
stream->upload_mem += nread;
|
|
|
|
stream->upload_len -= nread;
|
|
|
|
if(data->state.infilesize != -1) {
|
|
|
|
stream->upload_left -= nread;
|
|
|
|
if(!stream->upload_left)
|
|
|
|
*pflags = NGHTTP3_DATA_FLAG_EOF;
|
|
|
|
}
|
2019-09-25 06:27:26 -04:00
|
|
|
H3BUGF(infof(data, "cb_h3_readfunction %zd bytes%s (at %zd unacked)\n",
|
|
|
|
nread, *pflags == NGHTTP3_DATA_FLAG_EOF?" EOF":"",
|
|
|
|
out->used));
|
2019-08-22 08:08:18 -04:00
|
|
|
}
|
|
|
|
if(stream->upload_done && !stream->upload_len &&
|
|
|
|
(stream->upload_left <= 0)) {
|
2019-08-24 12:43:31 -04:00
|
|
|
H3BUGF(infof(data, "!!!!!!!!! cb_h3_readfunction sets EOF\n"));
|
2019-08-22 08:08:18 -04:00
|
|
|
*pflags = NGHTTP3_DATA_FLAG_EOF;
|
2019-09-20 22:24:38 -04:00
|
|
|
return 0;
|
2019-08-22 08:08:18 -04:00
|
|
|
}
|
|
|
|
else if(!nread) {
|
|
|
|
return NGHTTP3_ERR_WOULDBLOCK;
|
|
|
|
}
|
2019-09-20 22:24:38 -04:00
|
|
|
return 1;
|
2019-08-20 05:01:36 -04:00
|
|
|
}
|
|
|
|
|
2019-08-11 17:50:11 -04:00
|
|
|
/* Index where :authority header field will appear in request header
|
|
|
|
field list. */
|
|
|
|
#define AUTHORITY_DST_IDX 3
|
|
|
|
|
|
|
|
static CURLcode http_request(struct connectdata *conn, const void *mem,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct HTTP *stream = conn->data->req.protop;
|
|
|
|
size_t nheader;
|
|
|
|
size_t i;
|
|
|
|
size_t authority_idx;
|
|
|
|
char *hdbuf = (char *)mem;
|
|
|
|
char *end, *line_end;
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = conn->quic;
|
2019-08-11 17:50:11 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
nghttp3_nv *nva = NULL;
|
|
|
|
int64_t stream3_id;
|
|
|
|
int rc;
|
2019-08-22 08:08:18 -04:00
|
|
|
struct h3out *h3out = NULL;
|
2019-08-11 17:50:11 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rc = ngtcp2_conn_open_bidi_stream(qs->qconn, &stream3_id, NULL);
|
2019-08-11 17:50:11 -04:00
|
|
|
if(rc) {
|
|
|
|
failf(conn->data, "can get bidi streams");
|
|
|
|
result = CURLE_SEND_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->stream3_id = stream3_id;
|
|
|
|
stream->h3req = TRUE; /* senf off! */
|
2020-05-04 05:37:12 -04:00
|
|
|
Curl_dyn_init(&stream->overflow, CURL_MAX_READ_SIZE);
|
2019-08-11 17:50:11 -04:00
|
|
|
|
|
|
|
/* Calculate number of headers contained in [mem, mem + len). Assumes a
|
|
|
|
correctly generated HTTP header field block. */
|
|
|
|
nheader = 0;
|
|
|
|
for(i = 1; i < len; ++i) {
|
|
|
|
if(hdbuf[i] == '\n' && hdbuf[i - 1] == '\r') {
|
|
|
|
++nheader;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(nheader < 2)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* We counted additional 2 \r\n in the first and last line. We need 3
|
|
|
|
new headers: :method, :path and :scheme. Therefore we need one
|
|
|
|
more space. */
|
|
|
|
nheader += 1;
|
|
|
|
nva = malloc(sizeof(nghttp3_nv) * nheader);
|
|
|
|
if(!nva) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract :method, :path from request line
|
|
|
|
We do line endings with CRLF so checking for CR is enough */
|
|
|
|
line_end = memchr(hdbuf, '\r', len);
|
|
|
|
if(!line_end) {
|
|
|
|
result = CURLE_BAD_FUNCTION_ARGUMENT; /* internal error */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Method does not contain spaces */
|
|
|
|
end = memchr(hdbuf, ' ', line_end - hdbuf);
|
|
|
|
if(!end || end == hdbuf)
|
|
|
|
goto fail;
|
|
|
|
nva[0].name = (unsigned char *)":method";
|
|
|
|
nva[0].namelen = strlen((char *)nva[0].name);
|
|
|
|
nva[0].value = (unsigned char *)hdbuf;
|
|
|
|
nva[0].valuelen = (size_t)(end - hdbuf);
|
2019-08-12 00:09:44 -04:00
|
|
|
nva[0].flags = NGHTTP3_NV_FLAG_NONE;
|
2019-08-11 17:50:11 -04:00
|
|
|
|
|
|
|
hdbuf = end + 1;
|
|
|
|
|
|
|
|
/* Path may contain spaces so scan backwards */
|
|
|
|
end = NULL;
|
|
|
|
for(i = (size_t)(line_end - hdbuf); i; --i) {
|
|
|
|
if(hdbuf[i - 1] == ' ') {
|
|
|
|
end = &hdbuf[i - 1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!end || end == hdbuf)
|
|
|
|
goto fail;
|
|
|
|
nva[1].name = (unsigned char *)":path";
|
|
|
|
nva[1].namelen = strlen((char *)nva[1].name);
|
|
|
|
nva[1].value = (unsigned char *)hdbuf;
|
|
|
|
nva[1].valuelen = (size_t)(end - hdbuf);
|
2019-08-12 00:09:44 -04:00
|
|
|
nva[1].flags = NGHTTP3_NV_FLAG_NONE;
|
2019-08-11 17:50:11 -04:00
|
|
|
|
|
|
|
nva[2].name = (unsigned char *)":scheme";
|
|
|
|
nva[2].namelen = strlen((char *)nva[2].name);
|
|
|
|
if(conn->handler->flags & PROTOPT_SSL)
|
|
|
|
nva[2].value = (unsigned char *)"https";
|
|
|
|
else
|
|
|
|
nva[2].value = (unsigned char *)"http";
|
|
|
|
nva[2].valuelen = strlen((char *)nva[2].value);
|
2019-08-12 00:09:44 -04:00
|
|
|
nva[2].flags = NGHTTP3_NV_FLAG_NONE;
|
2019-08-11 17:50:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
authority_idx = 0;
|
|
|
|
i = 3;
|
|
|
|
while(i < nheader) {
|
|
|
|
size_t hlen;
|
|
|
|
|
|
|
|
hdbuf = line_end + 2;
|
|
|
|
|
|
|
|
/* check for next CR, but only within the piece of data left in the given
|
|
|
|
buffer */
|
|
|
|
line_end = memchr(hdbuf, '\r', len - (hdbuf - (char *)mem));
|
|
|
|
if(!line_end || (line_end == hdbuf))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* header continuation lines are not supported */
|
|
|
|
if(*hdbuf == ' ' || *hdbuf == '\t')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for(end = hdbuf; end < line_end && *end != ':'; ++end)
|
|
|
|
;
|
|
|
|
if(end == hdbuf || end == line_end)
|
|
|
|
goto fail;
|
|
|
|
hlen = end - hdbuf;
|
|
|
|
|
|
|
|
if(hlen == 4 && strncasecompare("host", hdbuf, 4)) {
|
|
|
|
authority_idx = i;
|
|
|
|
nva[i].name = (unsigned char *)":authority";
|
|
|
|
nva[i].namelen = strlen((char *)nva[i].name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nva[i].namelen = (size_t)(end - hdbuf);
|
2019-09-22 16:17:12 -04:00
|
|
|
/* Lower case the header name for HTTP/3 */
|
|
|
|
Curl_strntolower((char *)hdbuf, hdbuf, nva[i].namelen);
|
|
|
|
nva[i].name = (unsigned char *)hdbuf;
|
2019-08-11 17:50:11 -04:00
|
|
|
}
|
2019-08-12 00:09:44 -04:00
|
|
|
nva[i].flags = NGHTTP3_NV_FLAG_NONE;
|
2019-08-11 17:50:11 -04:00
|
|
|
hdbuf = end + 1;
|
|
|
|
while(*hdbuf == ' ' || *hdbuf == '\t')
|
|
|
|
++hdbuf;
|
|
|
|
end = line_end;
|
|
|
|
|
|
|
|
#if 0 /* This should probably go in more or less like this */
|
|
|
|
switch(inspect_header((const char *)nva[i].name, nva[i].namelen, hdbuf,
|
|
|
|
end - hdbuf)) {
|
|
|
|
case HEADERINST_IGNORE:
|
|
|
|
/* skip header fields prohibited by HTTP/2 specification. */
|
|
|
|
--nheader;
|
|
|
|
continue;
|
|
|
|
case HEADERINST_TE_TRAILERS:
|
|
|
|
nva[i].value = (uint8_t*)"trailers";
|
|
|
|
nva[i].value_len = sizeof("trailers") - 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nva[i].value = (unsigned char *)hdbuf;
|
|
|
|
nva[i].value_len = (size_t)(end - hdbuf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
nva[i].value = (unsigned char *)hdbuf;
|
|
|
|
nva[i].valuelen = (size_t)(end - hdbuf);
|
2019-08-12 00:09:44 -04:00
|
|
|
nva[i].flags = NGHTTP3_NV_FLAG_NONE;
|
2019-08-11 17:50:11 -04:00
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :authority must come before non-pseudo header fields */
|
|
|
|
if(authority_idx != 0 && authority_idx != AUTHORITY_DST_IDX) {
|
|
|
|
nghttp3_nv authority = nva[authority_idx];
|
|
|
|
for(i = authority_idx; i > AUTHORITY_DST_IDX; --i) {
|
|
|
|
nva[i] = nva[i - 1];
|
|
|
|
}
|
|
|
|
nva[i] = authority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Warn stream may be rejected if cumulative length of headers is too
|
|
|
|
large. */
|
|
|
|
#define MAX_ACC 60000 /* <64KB to account for some overhead */
|
|
|
|
{
|
|
|
|
size_t acc = 0;
|
2019-08-12 10:28:28 -04:00
|
|
|
for(i = 0; i < nheader; ++i)
|
2019-08-11 17:50:11 -04:00
|
|
|
acc += nva[i].namelen + nva[i].valuelen;
|
|
|
|
|
|
|
|
if(acc > MAX_ACC) {
|
|
|
|
infof(data, "http_request: Warning: The cumulative length of all "
|
|
|
|
"headers exceeds %zu bytes and that could cause the "
|
|
|
|
"stream to be rejected.\n", MAX_ACC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 16:58:46 -04:00
|
|
|
switch(data->state.httpreq) {
|
2019-08-11 17:50:11 -04:00
|
|
|
case HTTPREQ_POST:
|
|
|
|
case HTTPREQ_POST_FORM:
|
|
|
|
case HTTPREQ_POST_MIME:
|
2019-08-20 05:01:36 -04:00
|
|
|
case HTTPREQ_PUT: {
|
|
|
|
nghttp3_data_reader data_reader;
|
2019-08-11 17:50:11 -04:00
|
|
|
if(data->state.infilesize != -1)
|
|
|
|
stream->upload_left = data->state.infilesize;
|
|
|
|
else
|
|
|
|
/* data sending without specifying the data amount up front */
|
|
|
|
stream->upload_left = -1; /* unknown, but not zero */
|
|
|
|
|
2019-08-20 05:01:36 -04:00
|
|
|
data_reader.read_data = cb_h3_readfunction;
|
|
|
|
|
2019-08-22 08:08:18 -04:00
|
|
|
h3out = calloc(sizeof(struct h3out), 1);
|
|
|
|
if(!h3out) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
stream->h3out = h3out;
|
|
|
|
|
2019-08-20 05:01:36 -04:00
|
|
|
rc = nghttp3_conn_submit_request(qs->h3conn, stream->stream3_id,
|
|
|
|
nva, nheader, &data_reader,
|
|
|
|
conn->data);
|
|
|
|
if(rc) {
|
|
|
|
result = CURLE_SEND_ERROR;
|
|
|
|
goto fail;
|
2019-08-11 17:50:11 -04:00
|
|
|
}
|
|
|
|
break;
|
2019-08-20 05:01:36 -04:00
|
|
|
}
|
2019-08-11 17:50:11 -04:00
|
|
|
default:
|
2019-08-12 10:28:28 -04:00
|
|
|
stream->upload_left = 0; /* nothing left to send */
|
2019-08-11 17:50:11 -04:00
|
|
|
rc = nghttp3_conn_submit_request(qs->h3conn, stream->stream3_id,
|
|
|
|
nva, nheader,
|
|
|
|
NULL, /* no body! */
|
|
|
|
conn->data);
|
|
|
|
if(rc) {
|
|
|
|
result = CURLE_SEND_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl_safefree(nva);
|
|
|
|
|
|
|
|
infof(data, "Using HTTP/3 Stream ID: %x (easy handle %p)\n",
|
|
|
|
stream3_id, (void *)data);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free(nva);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
static ssize_t ngh3_stream_send(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
const void *mem,
|
|
|
|
size_t len,
|
|
|
|
CURLcode *curlcode)
|
|
|
|
{
|
|
|
|
ssize_t sent;
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = conn->quic;
|
2019-08-11 17:50:11 -04:00
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
struct HTTP *stream = conn->data->req.protop;
|
|
|
|
|
|
|
|
if(!stream->h3req) {
|
|
|
|
CURLcode result = http_request(conn, mem, len);
|
|
|
|
if(result) {
|
|
|
|
*curlcode = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sent = len;
|
|
|
|
}
|
|
|
|
else {
|
2019-09-25 06:27:26 -04:00
|
|
|
H3BUGF(infof(conn->data, "ngh3_stream_send() wants to send %zd bytes\n",
|
|
|
|
len));
|
2019-08-22 08:08:18 -04:00
|
|
|
if(!stream->upload_len) {
|
|
|
|
stream->upload_mem = mem;
|
|
|
|
stream->upload_len = len;
|
|
|
|
(void)nghttp3_conn_resume_stream(qs->h3conn, stream->stream3_id);
|
|
|
|
sent = len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*curlcode = CURLE_AGAIN;
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-11 17:50:11 -04:00
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
if(ng_flush_egress(conn, sockfd, qs)) {
|
2019-08-11 17:50:11 -04:00
|
|
|
*curlcode = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*curlcode = CURLE_OK;
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
static void ng_has_connected(struct connectdata *conn, int tempindex)
|
2019-08-11 17:50:11 -04:00
|
|
|
{
|
2019-08-12 10:28:28 -04:00
|
|
|
conn->recv[FIRSTSOCKET] = ngh3_stream_recv;
|
|
|
|
conn->send[FIRSTSOCKET] = ngh3_stream_send;
|
2019-08-22 08:08:18 -04:00
|
|
|
conn->handler = &Curl_handler_http3;
|
2019-08-11 17:50:11 -04:00
|
|
|
conn->bits.multiplex = TRUE; /* at least potentially multiplexed */
|
|
|
|
conn->httpversion = 30;
|
|
|
|
conn->bundle->multiuse = BUNDLE_MULTIPLEX;
|
2019-08-12 10:28:28 -04:00
|
|
|
conn->quic = &conn->hequic[tempindex];
|
|
|
|
DEBUGF(infof(conn->data, "ngtcp2 established connection!\n"));
|
2019-08-11 17:50:11 -04:00
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
/*
|
|
|
|
* There can be multiple connection attempts going on in parallel.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_quic_is_connected(struct connectdata *conn,
|
|
|
|
int sockindex,
|
2019-07-21 17:48:58 -04:00
|
|
|
bool *done)
|
|
|
|
{
|
2019-08-10 04:06:50 -04:00
|
|
|
CURLcode result;
|
2019-08-12 10:28:28 -04:00
|
|
|
struct quicsocket *qs = &conn->hequic[sockindex];
|
|
|
|
curl_socket_t sockfd = conn->tempsock[sockindex];
|
2019-08-10 04:06:50 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
result = ng_process_ingress(conn, sockfd, qs);
|
2019-08-10 04:06:50 -04:00
|
|
|
if(result)
|
2020-05-25 11:55:16 -04:00
|
|
|
goto error;
|
2019-08-10 04:06:50 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
result = ng_flush_egress(conn, sockfd, qs);
|
2019-08-10 04:06:50 -04:00
|
|
|
if(result)
|
2020-05-25 11:55:16 -04:00
|
|
|
goto error;
|
2019-08-10 04:06:50 -04:00
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
if(ngtcp2_conn_get_handshake_completed(qs->qconn)) {
|
2019-08-10 04:06:50 -04:00
|
|
|
*done = TRUE;
|
2019-08-12 00:09:44 -04:00
|
|
|
ng_has_connected(conn, sockindex);
|
2019-08-10 04:06:50 -04:00
|
|
|
}
|
|
|
|
|
2019-08-11 17:50:11 -04:00
|
|
|
return result;
|
2020-05-25 11:55:16 -04:00
|
|
|
error:
|
|
|
|
(void)qs_disconnect(qs);
|
|
|
|
return result;
|
|
|
|
|
2019-08-10 04:06:50 -04:00
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
static CURLcode ng_process_ingress(struct connectdata *conn, int sockfd,
|
|
|
|
struct quicsocket *qs)
|
2019-08-10 04:06:50 -04:00
|
|
|
{
|
|
|
|
ssize_t recvd;
|
|
|
|
int rv;
|
2019-08-12 10:28:28 -04:00
|
|
|
uint8_t buf[65536];
|
|
|
|
size_t bufsize = sizeof(buf);
|
2019-08-10 04:06:50 -04:00
|
|
|
struct sockaddr_storage remote_addr;
|
|
|
|
socklen_t remote_addrlen;
|
|
|
|
ngtcp2_path path;
|
|
|
|
ngtcp2_tstamp ts = timestamp();
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
remote_addrlen = sizeof(remote_addr);
|
2019-10-27 18:48:43 -04:00
|
|
|
while((recvd = recvfrom(sockfd, buf, bufsize, 0,
|
2019-08-10 04:06:50 -04:00
|
|
|
(struct sockaddr *)&remote_addr,
|
|
|
|
&remote_addrlen)) == -1 &&
|
2019-10-27 18:48:43 -04:00
|
|
|
SOCKERRNO == EINTR)
|
2019-08-10 04:06:50 -04:00
|
|
|
;
|
|
|
|
if(recvd == -1) {
|
2019-10-27 18:48:43 -04:00
|
|
|
if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK)
|
2019-08-10 04:06:50 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
failf(conn->data, "ngtcp2: recvfrom() unexpectedly returned %d", recvd);
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngtcp2_addr_init(&path.local, (uint8_t *)&qs->local_addr,
|
|
|
|
qs->local_addrlen, NULL);
|
|
|
|
ngtcp2_addr_init(&path.remote, (uint8_t *)&remote_addr, remote_addrlen,
|
|
|
|
NULL);
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rv = ngtcp2_conn_read_pkt(qs->qconn, &path, buf, recvd, ts);
|
2019-08-10 04:06:50 -04:00
|
|
|
if(rv != 0) {
|
|
|
|
/* TODO Send CONNECTION_CLOSE if possible */
|
|
|
|
return CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
static CURLcode ng_flush_egress(struct connectdata *conn, int sockfd,
|
|
|
|
struct quicsocket *qs)
|
2019-08-10 04:06:50 -04:00
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
ssize_t sent;
|
|
|
|
ssize_t outlen;
|
|
|
|
uint8_t out[NGTCP2_MAX_PKTLEN_IPV4];
|
|
|
|
size_t pktlen;
|
|
|
|
ngtcp2_path_storage ps;
|
|
|
|
ngtcp2_tstamp ts = timestamp();
|
|
|
|
struct sockaddr_storage remote_addr;
|
|
|
|
ngtcp2_tstamp expiry;
|
|
|
|
ngtcp2_duration timeout;
|
2019-08-12 00:09:44 -04:00
|
|
|
int64_t stream_id;
|
|
|
|
ssize_t veccnt;
|
|
|
|
int fin;
|
|
|
|
nghttp3_vec vec[16];
|
|
|
|
ssize_t ndatalen;
|
2019-08-10 04:06:50 -04:00
|
|
|
|
|
|
|
switch(qs->local_addr.ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
pktlen = NGTCP2_MAX_PKTLEN_IPV4;
|
|
|
|
break;
|
2020-02-03 17:58:38 -05:00
|
|
|
#ifdef ENABLE_IPV6
|
2019-08-10 04:06:50 -04:00
|
|
|
case AF_INET6:
|
|
|
|
pktlen = NGTCP2_MAX_PKTLEN_IPV6;
|
|
|
|
break;
|
2020-02-03 17:58:38 -05:00
|
|
|
#endif
|
2019-08-10 04:06:50 -04:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
rv = ngtcp2_conn_handle_expiry(qs->qconn, ts);
|
2019-08-10 04:06:50 -04:00
|
|
|
if(rv != 0) {
|
|
|
|
failf(conn->data, "ngtcp2_conn_handle_expiry returned error: %s\n",
|
|
|
|
ngtcp2_strerror(rv));
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngtcp2_path_storage_zero(&ps);
|
|
|
|
|
|
|
|
for(;;) {
|
2019-08-12 00:09:44 -04:00
|
|
|
outlen = -1;
|
2019-08-12 10:28:28 -04:00
|
|
|
if(qs->h3conn && ngtcp2_conn_get_max_data_left(qs->qconn)) {
|
2019-08-12 00:09:44 -04:00
|
|
|
veccnt = nghttp3_conn_writev_stream(qs->h3conn, &stream_id, &fin, vec,
|
|
|
|
sizeof(vec) / sizeof(vec[0]));
|
|
|
|
if(veccnt < 0) {
|
|
|
|
failf(conn->data, "nghttp3_conn_writev_stream returned error: %s\n",
|
|
|
|
nghttp3_strerror((int)veccnt));
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
else if(veccnt > 0) {
|
2020-06-08 03:33:51 -04:00
|
|
|
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE |
|
|
|
|
(fin ? NGTCP2_WRITE_STREAM_FLAG_FIN : 0);
|
2019-08-12 10:28:28 -04:00
|
|
|
outlen =
|
|
|
|
ngtcp2_conn_writev_stream(qs->qconn, &ps.path,
|
|
|
|
out, pktlen, &ndatalen,
|
2020-06-08 03:33:51 -04:00
|
|
|
flags, stream_id,
|
2019-08-12 10:28:28 -04:00
|
|
|
(const ngtcp2_vec *)vec, veccnt, ts);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(outlen == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(outlen < 0) {
|
|
|
|
if(outlen == NGTCP2_ERR_STREAM_DATA_BLOCKED ||
|
|
|
|
outlen == NGTCP2_ERR_STREAM_SHUT_WR) {
|
2020-05-23 09:41:25 -04:00
|
|
|
assert(ndatalen == -1);
|
2019-08-12 00:09:44 -04:00
|
|
|
rv = nghttp3_conn_block_stream(qs->h3conn, stream_id);
|
|
|
|
if(rv != 0) {
|
|
|
|
failf(conn->data,
|
|
|
|
"nghttp3_conn_block_stream returned error: %s\n",
|
|
|
|
nghttp3_strerror(rv));
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(outlen == NGTCP2_ERR_WRITE_STREAM_MORE) {
|
|
|
|
assert(ndatalen > 0);
|
|
|
|
rv = nghttp3_conn_add_write_offset(qs->h3conn, stream_id,
|
|
|
|
ndatalen);
|
|
|
|
if(rv != 0) {
|
|
|
|
failf(conn->data,
|
|
|
|
"nghttp3_conn_add_write_offset returned error: %s\n",
|
|
|
|
nghttp3_strerror(rv));
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
2020-05-23 09:41:25 -04:00
|
|
|
assert(ndatalen == -1);
|
2019-08-12 00:09:44 -04:00
|
|
|
failf(conn->data, "ngtcp2_conn_writev_stream returned error: %s\n",
|
|
|
|
ngtcp2_strerror((int)outlen));
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 09:41:25 -04:00
|
|
|
else {
|
|
|
|
assert(ndatalen == -1);
|
2019-08-15 01:53:24 -04:00
|
|
|
}
|
2019-08-12 00:09:44 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-10 04:06:50 -04:00
|
|
|
if(outlen < 0) {
|
2019-08-12 10:28:28 -04:00
|
|
|
outlen = ngtcp2_conn_write_pkt(qs->qconn, &ps.path, out, pktlen, ts);
|
2019-08-12 00:09:44 -04:00
|
|
|
if(outlen < 0) {
|
|
|
|
failf(conn->data, "ngtcp2_conn_write_pkt returned error: %s\n",
|
|
|
|
ngtcp2_strerror((int)outlen));
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
if(outlen == 0)
|
|
|
|
break;
|
2019-08-10 04:06:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&remote_addr, ps.path.remote.addr, ps.path.remote.addrlen);
|
2019-10-27 20:16:24 -04:00
|
|
|
while((sent = send(sockfd, out, outlen, 0)) == -1 &&
|
2019-10-27 18:48:43 -04:00
|
|
|
SOCKERRNO == EINTR)
|
2019-08-10 04:06:50 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
if(sent == -1) {
|
2019-10-27 18:48:43 -04:00
|
|
|
if(SOCKERRNO == EAGAIN || SOCKERRNO == EWOULDBLOCK) {
|
2019-08-10 04:06:50 -04:00
|
|
|
/* TODO Cache packet */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2019-10-27 20:16:24 -04:00
|
|
|
failf(conn->data, "send() returned %zd (errno %d)\n", sent,
|
2019-08-12 10:28:28 -04:00
|
|
|
SOCKERRNO);
|
2019-08-10 04:06:50 -04:00
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:28:28 -04:00
|
|
|
expiry = ngtcp2_conn_get_expiry(qs->qconn);
|
2019-08-10 04:06:50 -04:00
|
|
|
if(expiry != UINT64_MAX) {
|
|
|
|
if(expiry <= ts) {
|
|
|
|
timeout = NGTCP2_MILLISECONDS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
timeout = expiry - ts;
|
|
|
|
}
|
|
|
|
Curl_expire(conn->data, timeout / NGTCP2_MILLISECONDS, EXPIRE_QUIC);
|
|
|
|
}
|
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2019-08-22 08:08:18 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from transfer.c:done_sending when we stop HTTP/3 uploading.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_quic_done_sending(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
if(conn->handler == &Curl_handler_http3) {
|
|
|
|
/* only for HTTP/3 transfers */
|
|
|
|
struct HTTP *stream = conn->data->req.protop;
|
|
|
|
struct quicsocket *qs = conn->quic;
|
|
|
|
stream->upload_done = TRUE;
|
|
|
|
(void)nghttp3_conn_resume_stream(qs->h3conn, stream->stream3_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2019-11-16 02:57:45 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from http.c:Curl_http_done when a request completes.
|
|
|
|
*/
|
|
|
|
void Curl_quic_done(struct Curl_easy *data, bool premature)
|
|
|
|
{
|
|
|
|
(void)premature;
|
|
|
|
if(data->conn->handler == &Curl_handler_http3) {
|
|
|
|
/* only for HTTP/3 transfers */
|
|
|
|
struct HTTP *stream = data->req.protop;
|
2020-05-04 05:37:12 -04:00
|
|
|
Curl_dyn_free(&stream->overflow);
|
2019-11-16 02:57:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from transfer.c:data_pending to know if we should keep looping
|
|
|
|
* to receive more data from the connection.
|
|
|
|
*/
|
|
|
|
bool Curl_quic_data_pending(const struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
/* We may have received more data than we're able to hold in the receive
|
|
|
|
buffer and allocated an overflow buffer. Since it's possible that
|
|
|
|
there's no more data coming on the socket, we need to keep reading
|
|
|
|
until the overflow buffer is empty. */
|
|
|
|
const struct HTTP *stream = data->req.protop;
|
2020-05-04 05:37:12 -04:00
|
|
|
return Curl_dyn_len(&stream->overflow) > 0;
|
2019-11-16 02:57:45 -05:00
|
|
|
}
|
|
|
|
|
2019-07-21 17:48:58 -04:00
|
|
|
#endif
|