forked from curl/curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngtcp2.c
1769 lines (1550 loc) · 50.8 KB
/
ngtcp2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al.
*
* 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>
#include <ngtcp2/ngtcp2_crypto.h>
#include <nghttp3/nghttp3.h>
#include <openssl/err.h>
#include "urldata.h"
#include "sendf.h"
#include "strdup.h"
#include "rand.h"
#include "ngtcp2.h"
#include "multiif.h"
#include "strcase.h"
#include "connect.h"
#include "strerror.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
/* #define DEBUG_NGTCP2 */
#ifdef CURLDEBUG
#define DEBUG_HTTP3
#endif
#ifdef DEBUG_HTTP3
#define H3BUGF(x) x
#else
#define H3BUGF(x) do { } while(0)
#endif
/*
* 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 */
};
#define QUIC_MAX_STREAMS (256*1024)
#define QUIC_MAX_DATA (1*1024*1024)
#define QUIC_IDLE_TIMEOUT 60000 /* milliseconds */
#define QUIC_CIPHERS \
"TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_" \
"POLY1305_SHA256:TLS_AES_128_CCM_SHA256"
#define QUIC_GROUPS "P-256:X25519:P-384:P-521"
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);
static int cb_h3_acked_stream_data(nghttp3_conn *conn, int64_t stream_id,
size_t datalen, void *user_data,
void *stream_user_data);
static ngtcp2_tstamp timestamp(void)
{
struct curltime ct = Curl_now();
return ct.tv_sec * NGTCP2_SECONDS + ct.tv_usec * NGTCP2_MICROSECONDS;
}
#ifdef DEBUG_NGTCP2
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);
fprintf(stderr, "\n");
}
#endif
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);
}
}
static int setup_initial_crypto_context(struct quicsocket *qs)
{
const ngtcp2_cid *dcid = ngtcp2_conn_get_dcid(qs->qconn);
if(ngtcp2_crypto_derive_and_install_initial_key(
qs->qconn, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, dcid,
NGTCP2_CRYPTO_SIDE_CLIENT) != 0)
return -1;
return 0;
}
static void quic_settings(ngtcp2_settings *s,
uint64_t stream_buffer_size)
{
ngtcp2_settings_default(s);
#ifdef DEBUG_NGTCP2
s->log_printf = quic_printf;
#else
s->log_printf = NULL;
#endif
s->initial_ts = timestamp();
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;
s->transport_params.max_idle_timeout = QUIC_IDLE_TIMEOUT;
}
static FILE *keylog_file; /* not thread-safe */
static void keylog_callback(const SSL *ssl, const char *line)
{
(void)ssl;
fputs(line, keylog_file);
fputc('\n', keylog_file);
fflush(keylog_file);
}
static int init_ngh3_conn(struct quicsocket *qs);
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);
if(ngtcp2_crypto_derive_and_install_key(
qs->qconn, ssl, NULL, NULL, NULL, NULL, NULL, NULL, level, rx_secret,
tx_secret, secretlen, NGTCP2_CRYPTO_SIDE_CLIENT) != 0)
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);
struct quic_handshake *crypto_data;
ngtcp2_crypto_level level = quic_from_ossl_level(ossl_level);
int rv;
crypto_data = &qs->crypto_data[level];
if(crypto_data->buf == NULL) {
crypto_data->buf = malloc(4096);
if(!crypto_data->buf)
return 0;
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);
memcpy(&crypto_data->buf[crypto_data->len], data, len);
crypto_data->len += len;
rv = ngtcp2_conn_submit_crypto_data(
qs->qconn, level, (uint8_t *)(&crypto_data->buf[crypto_data->len] - len),
len);
if(rv) {
H3BUGF(fprintf(stderr, "write_client_handshake failed\n"));
}
assert(0 == rv);
return 1;
}
static int quic_flush_flight(SSL *ssl)
{
(void)ssl;
return 1;
}
static int quic_send_alert(SSL *ssl, enum ssl_encryption_level_t level,
uint8_t alert)
{
struct quicsocket *qs = (struct quicsocket *)SSL_get_app_data(ssl);
(void)level;
qs->tls_alert = alert;
return 1;
}
static SSL_QUIC_METHOD quic_method = {quic_set_encryption_secrets,
quic_add_handshake_data,
quic_flush_flight, quic_send_alert};
static SSL_CTX *quic_ssl_ctx(struct Curl_easy *data)
{
SSL_CTX *ssl_ctx = SSL_CTX_new(TLS_method());
const char *keylog_filename;
SSL_CTX_set_min_proto_version(ssl_ctx, TLS1_3_VERSION);
SSL_CTX_set_max_proto_version(ssl_ctx, TLS1_3_VERSION);
SSL_CTX_set_default_verify_paths(ssl_ctx);
if(SSL_CTX_set_ciphersuites(ssl_ctx, QUIC_CIPHERS) != 1) {
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);
return NULL;
}
if(SSL_CTX_set1_groups_list(ssl_ctx, QUIC_GROUPS) != 1) {
failf(data, "SSL_CTX_set1_groups_list failed");
return NULL;
}
SSL_CTX_set_quic_method(ssl_ctx, &quic_method);
keylog_filename = getenv("SSLKEYLOGFILE");
if(keylog_filename) {
keylog_file = fopen(keylog_filename, "wb");
if(keylog_file) {
SSL_CTX_set_keylog_callback(ssl_ctx, keylog_callback);
}
}
return ssl_ctx;
}
/** SSL callbacks ***/
static int quic_init_ssl(struct quicsocket *qs)
{
const uint8_t *alpn = NULL;
size_t alpnlen = 0;
/* this will need some attention when HTTPS proxy over QUIC get fixed */
const char * const hostname = qs->conn->host.name;
if(qs->ssl)
SSL_free(qs->ssl);
qs->ssl = SSL_new(qs->sslctx);
SSL_set_app_data(qs->ssl, qs);
SSL_set_connect_state(qs->ssl);
switch(qs->version) {
#ifdef NGTCP2_PROTO_VER
case NGTCP2_PROTO_VER:
alpn = (const uint8_t *)NGTCP2_ALPN_H3;
alpnlen = sizeof(NGTCP2_ALPN_H3) - 1;
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;
}
static int cb_initial(ngtcp2_conn *quic, void *user_data)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
if(ngtcp2_crypto_read_write_crypto_data(
quic, qs->ssl, NGTCP2_CRYPTO_LEVEL_INITIAL, NULL, 0) != 0)
return NGTCP2_ERR_CALLBACK_FAILURE;
return 0;
}
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)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
(void)offset;
if(ngtcp2_crypto_read_write_crypto_data(tconn, qs->ssl, crypto_level, data,
datalen) != 0)
return NGTCP2_ERR_CRYPTO;
return 0;
}
static int cb_handshake_completed(ngtcp2_conn *tconn, void *user_data)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
(void)tconn;
infof(qs->conn->data, "QUIC handshake is completed\n");
return 0;
}
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;
}
static int cb_recv_stream_data(ngtcp2_conn *tconn, int64_t stream_id,
int fin, uint64_t offset,
const uint8_t *buf, size_t buflen,
void *user_data, void *stream_user_data)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
ssize_t nconsumed;
(void)offset;
(void)stream_user_data;
nconsumed =
nghttp3_conn_read_stream(qs->h3conn, stream_id, buf, buflen, fin);
if(nconsumed < 0) {
failf(qs->conn->data, "nghttp3_conn_read_stream returned error: %s\n",
nghttp3_strerror((int)nconsumed));
return NGTCP2_ERR_CALLBACK_FAILURE;
}
/* number of bytes inside buflen which consists of framing overhead
* including QPACK HEADERS. In other words, it does not consume payload of
* DATA frame. */
ngtcp2_conn_extend_max_stream_offset(tconn, stream_id, nconsumed);
ngtcp2_conn_extend_max_offset(tconn, nconsumed);
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)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
int rv;
(void)stream_id;
(void)tconn;
(void)offset;
(void)datalen;
(void)stream_user_data;
rv = nghttp3_conn_add_ack_offset(qs->h3conn, stream_id, datalen);
if(rv != 0) {
failf(qs->conn->data, "nghttp3_conn_add_ack_offset returned error: %s\n",
nghttp3_strerror(rv));
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
static int cb_stream_close(ngtcp2_conn *tconn, int64_t stream_id,
uint64_t app_error_code,
void *user_data, void *stream_user_data)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
int rv;
(void)tconn;
(void)stream_user_data;
/* stream is closed... */
rv = nghttp3_conn_close_stream(qs->h3conn, stream_id,
app_error_code);
if(rv != 0) {
failf(qs->conn->data, "nghttp3_conn_close_stream returned error: %s\n",
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)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
int rv;
(void)tconn;
(void)final_size;
(void)app_error_code;
(void)stream_user_data;
rv = nghttp3_conn_reset_stream(qs->h3conn, stream_id);
if(rv != 0) {
failf(qs->conn->data, "nghttp3_conn_reset_stream returned error: %s\n",
nghttp3_strerror(rv));
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
static int cb_recv_retry(ngtcp2_conn *tconn, const ngtcp2_pkt_hd *hd,
const ngtcp2_pkt_retry *retry, void *user_data)
{
/* Re-generate handshake secrets here because connection ID might change. */
struct quicsocket *qs = (struct quicsocket *)user_data;
(void)tconn;
(void)hd;
(void)retry;
setup_initial_crypto_context(qs);
return 0;
}
static int cb_extend_max_local_streams_bidi(ngtcp2_conn *tconn,
uint64_t max_streams,
void *user_data)
{
(void)tconn;
(void)max_streams;
(void)user_data;
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)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
int rv;
(void)tconn;
(void)max_data;
(void)stream_user_data;
rv = nghttp3_conn_unblock_stream(qs->h3conn, stream_id);
if(rv != 0) {
failf(qs->conn->data, "nghttp3_conn_unblock_stream returned error: %s\n",
nghttp3_strerror(rv));
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
static int cb_get_new_connection_id(ngtcp2_conn *tconn, ngtcp2_cid *cid,
uint8_t *token, size_t cidlen,
void *user_data)
{
struct quicsocket *qs = (struct quicsocket *)user_data;
CURLcode result;
(void)tconn;
result = Curl_rand(qs->conn->data, cid->data, cidlen);
if(result)
return NGTCP2_ERR_CALLBACK_FAILURE;
cid->datalen = cidlen;
result = Curl_rand(qs->conn->data, token, NGTCP2_STATELESS_RESET_TOKENLEN);
if(result)
return NGTCP2_ERR_CALLBACK_FAILURE;
return 0;
}
static ngtcp2_conn_callbacks ng_callbacks = {
cb_initial,
NULL, /* recv_client_initial */
cb_recv_crypto_data,
cb_handshake_completed,
NULL, /* recv_version_negotiation */
ngtcp2_crypto_encrypt_cb,
ngtcp2_crypto_decrypt_cb,
ngtcp2_crypto_hp_mask_cb,
cb_recv_stream_data,
NULL, /* acked_crypto_offset */
cb_acked_stream_data_offset,
NULL, /* stream_open */
cb_stream_close,
NULL, /* recv_stateless_reset */
cb_recv_retry,
cb_extend_max_local_streams_bidi,
NULL, /* extend_max_local_streams_uni */
NULL, /* rand */
cb_get_new_connection_id,
NULL, /* remove_connection_id */
ngtcp2_crypto_update_key_cb, /* update_key */
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,
NULL, /* dcid_status */
NULL /* handshake_confirmed */
};
/*
* Might be called twice for happy eyeballs.
*/
CURLcode Curl_quic_connect(struct connectdata *conn,
curl_socket_t sockfd,
int sockindex,
const struct sockaddr *addr,
socklen_t addrlen)
{
int rc;
int rv;
CURLcode result;
ngtcp2_path path; /* TODO: this must be initialized properly */
struct Curl_easy *data = conn->data;
struct quicsocket *qs = &conn->hequic[sockindex];
char ipbuf[40];
long port;
uint8_t paramsbuf[64];
ngtcp2_transport_params params;
ssize_t nwrite;
qs->conn = conn;
/* extract the used address as a string */
if(!Curl_addr2string((struct sockaddr*)addr, addrlen, ipbuf, &port)) {
char buffer[STRERROR_LEN];
failf(data, "ssrem inet_ntop() failed with errno %d: %s",
SOCKERRNO, Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
return CURLE_BAD_FUNCTION_ARGUMENT;
}
infof(data, "Connect socket %d over QUIC to %s:%ld\n",
sockfd, ipbuf, port);
qs->version = NGTCP2_PROTO_VER;
qs->sslctx = quic_ssl_ctx(data);
if(!qs->sslctx)
return CURLE_QUIC_CONNECT_ERROR;
if(quic_init_ssl(qs))
return CURLE_QUIC_CONNECT_ERROR;
qs->dcid.datalen = NGTCP2_MAX_CIDLEN;
result = Curl_rand(data, qs->dcid.data, NGTCP2_MAX_CIDLEN);
if(result)
return result;
qs->scid.datalen = NGTCP2_MAX_CIDLEN;
result = Curl_rand(data, qs->scid.data, NGTCP2_MAX_CIDLEN);
if(result)
return result;
quic_settings(&qs->settings, data->set.buffer_size);
qs->local_addrlen = sizeof(qs->local_addr);
rv = getsockname(sockfd, (struct sockaddr *)&qs->local_addr,
&qs->local_addrlen);
if(rv == -1)
return CURLE_QUIC_CONNECT_ERROR;
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
#else
#error "unsupported ngtcp2 version"
#endif
rc = ngtcp2_conn_client_new(&qs->qconn, &qs->dcid, &qs->scid, &path, QUICVER,
&ng_callbacks, &qs->settings, NULL, qs);
if(rc)
return CURLE_QUIC_CONNECT_ERROR;
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) {
failf(data, "ngtcp2_encode_transport_params: %s\n",
ngtcp2_strerror((int)nwrite));
return CURLE_QUIC_CONNECT_ERROR;
}
if(!SSL_set_quic_transport_params(qs->ssl, paramsbuf, nwrite))
return CURLE_QUIC_CONNECT_ERROR;
rc = setup_initial_crypto_context(qs);
if(rc)
return CURLE_QUIC_CONNECT_ERROR;
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)
{
ngtcp2_info *ng2 = ngtcp2_version(0);
nghttp3_info *ht3 = nghttp3_version(0);
return msnprintf(p, len, " ngtcp2/%s nghttp3/%s",
ng2->version_str, ht3->version_str);
}
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);
}
static CURLcode ng_disconnect(struct connectdata *conn,
bool dead_connection)
{
int i;
struct quicsocket *qs = &conn->hequic[0];
(void)dead_connection;
if(qs->ssl)
SSL_free(qs->ssl);
for(i = 0; i < 3; i++)
free(qs->crypto_data[i].buf);
nghttp3_conn_del(qs->h3conn);
ngtcp2_conn_del(qs->qconn);
SSL_CTX_free(qs->sslctx);
return CURLE_OK;
}
static unsigned int ng_conncheck(struct connectdata *conn,
unsigned int checks_to_perform)
{
(void)conn;
(void)checks_to_perform;
return CONNRESULT_NONE;
}
static const struct Curl_handler Curl_handler_http3 = {
"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)
{
struct Curl_easy *data = stream_user_data;
struct HTTP *stream = data->req.protop;
(void)conn;
(void)stream_id;
(void)app_error_code;
(void)user_data;
H3BUGF(infof(data, "cb_h3_stream_close CALLED\n"));
stream->closed = TRUE;
Curl_expire(data, 0, EXPIRE_QUIC);
/* 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;
return 0;
}
/* Minimum size of the overflow buffer */
#define OVERFLOWSIZE 1024
/*
* allocate_overflow() ensures that there is room for incoming data in the
* overflow buffer, growing it to accommodate the new data if necessary. We
* may need to use the overflow buffer because we can't precisely limit the
* amount of HTTP/3 header data we receive using QUIC flow control mechanisms.
*/
static CURLcode allocate_overflow(struct Curl_easy *data,
struct HTTP *stream,
size_t length)
{
size_t maxleft;
size_t newsize;
/* length can be arbitrarily large, so take care not to overflow newsize */
maxleft = CURL_MAX_READ_SIZE - stream->overflow_buflen;
if(length > maxleft) {
/* The reason to have a max limit for this is to avoid the risk of a bad
server feeding libcurl with a highly compressed list of headers that
will cause our overflow buffer to grow too large */
failf(data, "Rejected %zu bytes of overflow data (max is %d)!",
stream->overflow_buflen + length, CURL_MAX_READ_SIZE);
return CURLE_OUT_OF_MEMORY;
}
newsize = stream->overflow_buflen + length;
if(newsize > stream->overflow_bufsize) {
/* We enlarge the overflow buffer as it is too small */
char *newbuff;
newsize = CURLMAX(newsize * 3 / 2, stream->overflow_bufsize*2);
newsize = CURLMIN(CURLMAX(OVERFLOWSIZE, newsize), CURL_MAX_READ_SIZE);
newbuff = realloc(stream->overflow_buf, newsize);
if(!newbuff) {
failf(data, "Failed to alloc memory for overflow buffer!");
return CURLE_OUT_OF_MEMORY;
}
stream->overflow_buf = newbuff;
stream->overflow_bufsize = newsize;
infof(data, "Grew HTTP/3 overflow buffer to %zu bytes\n", newsize);
}
return CURLE_OK;
}
/*
* 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.
*/
static CURLcode write_data(struct Curl_easy *data,
struct HTTP *stream,
const void *mem, size_t memlen)
{
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);
#if 0 /* extra debugging of incoming h3 data */
fprintf(stderr, "!! Copies %zd bytes to %p (total %zd)\n",
len, stream->mem, stream->memlen);
#endif
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 */
if(ncopy) {
result = allocate_overflow(data, stream, ncopy);
if(result) {
return result;
}
#if 0 /* extra debugging of incoming h3 data */
fprintf(stderr, "!! Copies %zd overflow bytes to %p (total %zd)\n",
ncopy, stream->overflow_buf, stream->overflow_buflen);
#endif
memcpy(stream->overflow_buf + stream->overflow_buflen, buf, ncopy);
stream->overflow_buflen += ncopy;
}
#if 0 /* extra debugging of incoming h3 data */
{
size_t i;
for(i = 0; i < memlen; i++) {
fprintf(stderr, "!! data[%d]: %02x '%c'\n", i, buf[i], buf[i]);
}
}
#endif
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;
result = write_data(data, stream, buf, buflen);
if(result) {
return -1;
}
stream->unacked_window += buflen;
(void)stream_id;
(void)user_data;
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)
{
struct quicsocket *qs = user_data;
(void)conn;
(void)stream_user_data;
(void)stream_id;
ngtcp2_conn_extend_max_stream_offset(qs->qconn, stream_id, consumed);
ngtcp2_conn_extend_max_offset(qs->qconn, consumed);
return 0;
}
/* 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)
{
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;
CURLcode result = CURLE_OK;
(void)conn;
(void)stream_id;
(void)user_data;
/* add a CRLF only if we've received some headers */
if(stream->firstheader) {
result = write_data(data, stream, "\r\n", 2);
if(result) {
return -1;
}
}
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)
{
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;
CURLcode result = CURLE_OK;
(void)conn;
(void)stream_id;
(void)token;
(void)flags;
(void)user_data;
if(h3name.len == sizeof(":status") - 1 &&
!memcmp(":status", h3name.base, h3name.len)) {
char line[14]; /* status line is always 13 characters long */
size_t ncopy;
int status = decode_status_code(h3val.base, h3val.len);
DEBUGASSERT(status != -1);
ncopy = msnprintf(line, sizeof(line), "HTTP/3 %03d \r\n", status);
result = write_data(data, stream, line, ncopy);
if(result) {
return -1;
}
}
else {
/* store as a HTTP1-style header */
result = write_data(data, stream, h3name.base, h3name.len);
if(result) {
return -1;
}
result = write_data(data, stream, ": ", 2);
if(result) {
return -1;
}
result = write_data(data, stream, h3val.base, h3val.len);
if(result) {
return -1;
}
result = write_data(data, stream, "\r\n", 2);
if(result) {
return -1;
}
}
stream->firstheader = TRUE;
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 = {
cb_h3_acked_stream_data, /* acked_stream_data */
cb_h3_stream_close,
cb_h3_recv_data,
cb_h3_deferred_consume,
NULL, /* begin_headers */
cb_h3_recv_header,
cb_h3_end_headers,
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 */
NULL, /* end_stream */
};
static int init_ngh3_conn(struct quicsocket *qs)
{
CURLcode result;
int rc;
int64_t ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id;