-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.cpp
3275 lines (2844 loc) · 93.7 KB
/
client.cpp
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
#include "common.cpp"
using namespace ngtcp2;
namespace
{
auto randgen = util::make_mt19937();
}
struct Request {
std::string_view scheme;
std::string authority;
std::string path;
};
struct Config {
ngtcp2_cid dcid;
ngtcp2_cid scid;
bool scid_present;
// tx_loss_prob is probability of losing outgoing packet.
double tx_loss_prob;
// rx_loss_prob is probability of losing incoming packet.
double rx_loss_prob;
// fd is a file descriptor to read input for streams.
int fd;
// ciphers is the list of enabled ciphers.
const char *ciphers;
// groups is the list of supported groups.
const char *groups;
// nstreams is the number of streams to open.
size_t nstreams;
// data is the pointer to memory region which maps file denoted by
// fd.
uint8_t *data;
// datalen is the length of file denoted by fd.
size_t datalen;
// version is a QUIC version to use.
uint32_t version;
// quiet suppresses the output normally shown except for the error
// messages.
bool quiet;
// timeout is an idle timeout for QUIC connection.
ngtcp2_duration timeout;
// session_file is a path to a file to write, and read TLS session.
const char *session_file;
// tp_file is a path to a file to write, and read QUIC transport
// parameters.
const char *tp_file;
// show_secret is true if transport secrets should be printed out.
bool show_secret;
// change_local_addr is the duration after which client changes
// local address.
ngtcp2_duration change_local_addr;
// key_update is the duration after which client initiates key
// update.
ngtcp2_duration key_update;
// delay_stream is the duration after which client sends the first
// 1-RTT stream.
ngtcp2_duration delay_stream;
// nat_rebinding is true if simulated NAT rebinding is enabled.
bool nat_rebinding;
// no_preferred_addr is true if client do not follow preferred
// address offered by server.
bool no_preferred_addr;
std::string_view http_method;
// download is a path to a directory where a downloaded file is
// saved. If it is empty, no file is saved.
std::string_view download;
// requests contains URIs to request.
std::vector<Request> requests;
// no_quic_dump is true if hexdump of QUIC STREAM and CRYPTO data
// should be disabled.
bool no_quic_dump;
// no_http_dump is true if hexdump of HTTP response body should be
// disabled.
bool no_http_dump;
// qlog_file is the path to write qlog.
std::string_view qlog_file;
// qlog_dir is the path to directory where qlog is stored. qlog_dir
// and qlog_file are mutually exclusive.
std::string_view qlog_dir;
// max_data is the initial connection-level flow control window.
uint64_t max_data;
// max_stream_data_bidi_local is the initial stream-level flow
// control window for a bidirectional stream that the local endpoint
// initiates.
uint64_t max_stream_data_bidi_local;
// max_stream_data_bidi_remote is the initial stream-level flow
// control window for a bidirectional stream that the remote
// endpoint initiates.
uint64_t max_stream_data_bidi_remote;
// max_stream_data_uni is the initial stream-level flow control
// window for a unidirectional stream.
uint64_t max_stream_data_uni;
// max_streams_bidi is the number of the concurrent bidirectional
// streams.
uint64_t max_streams_bidi;
// max_streams_uni is the number of the concurrent unidirectional
// streams.
uint64_t max_streams_uni;
// max_window is the maximum connection-level flow control window
// size if auto-tuning is enabled.
uint64_t max_window;
// max_stream_window is the maximum stream-level flow control window
// size if auto-tuning is enabled.
uint64_t max_stream_window;
// exit_on_first_stream_close is the flag that if it is true, client
// exits when a first HTTP stream gets closed. It is not
// necessarily the same time when the underlying QUIC stream closes
// due to the QPACK synchronization.
bool exit_on_first_stream_close;
// exit_on_all_streams_close is the flag that if it is true, client
// exits when all HTTP streams get closed.
bool exit_on_all_streams_close;
// disable_early_data disables early data.
bool disable_early_data;
// static_secret is used to derive keying materials for Stateless
// Retry token.
std::array<uint8_t, 32> static_secret;
// cc is the congestion controller algorithm.
std::string_view cc;
// token_file is a path to file to read or write token from
// NEW_TOKEN frame.
std::string_view token_file;
// sni is the value sent in TLS SNI, overriding DNS name of the
// remote host.
std::string_view sni;
// initial_rtt is an initial RTT.
ngtcp2_duration initial_rtt;
};
namespace {
Config config{};
} // namespace
struct Buffer {
Buffer(const uint8_t *data, size_t datalen);
explicit Buffer(size_t datalen);
size_t size() const { return tail - buf.data(); }
size_t left() const { return buf.data() + buf.size() - tail; }
uint8_t *const wpos() { return tail; }
const uint8_t *rpos() const { return buf.data(); }
void push(size_t len) { tail += len; }
void reset() { tail = buf.data(); }
std::vector<uint8_t> buf;
// tail points to the position of the buffer where write should
// occur.
uint8_t *tail;
};
Buffer::Buffer(const uint8_t *data, size_t datalen)
: buf{data, data + datalen}, tail(buf.data() + datalen) {}
Buffer::Buffer(size_t datalen) : buf(datalen), tail(buf.data()) {}
struct Stream {
Stream(const Request &req, int64_t stream_id);
~Stream();
int open_file(const std::string_view &path);
Request req;
int64_t stream_id;
int fd;
};
Stream::Stream(const Request &req, int64_t stream_id)
: req(req), stream_id(stream_id), fd(-1) {}
Stream::~Stream() {
if (fd != -1) {
close(fd);
}
}
int Stream::open_file(const std::string_view &path) {
assert(fd == -1);
auto it = std::find(std::rbegin(path), std::rend(path), '/').base();
if (it == std::end(path)) {
std::cerr << "No file name found: " << path << std::endl;
return -1;
}
auto b = std::string_view{it, static_cast<size_t>(std::end(path) - it)};
if (b == ".." || b == ".") {
std::cerr << "Invalid file name: " << b << std::endl;
return -1;
}
auto fname = std::string{config.download};
fname += '/';
fname += b;
fd = open(fname.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1) {
std::cerr << "open: Could not open file " << fname << ": "
<< strerror(errno) << std::endl;
return -1;
}
return 0;
}
struct Crypto {
/* data is unacknowledged data. */
std::deque<Buffer> data;
/* acked_offset is the size of acknowledged crypto data removed from
|data| so far */
uint64_t acked_offset;
};
class Client {
public:
Client(struct ev_loop *loop, SSL_CTX *ssl_ctx);
~Client();
int init(int fd, const Address &local_addr, const Address &remote_addr,
const char *addr, const char *port, uint32_t version);
int init_ssl();
void disconnect();
void close();
void start_wev();
int on_read();
int on_write();
int write_streams();
int feed_data(const sockaddr *sa, socklen_t salen, const ngtcp2_pkt_info *pi,
uint8_t *data, size_t datalen);
int handle_expiry();
void schedule_retransmit();
int handshake_completed();
int handshake_confirmed();
void write_client_handshake(ngtcp2_crypto_level level, const uint8_t *data,
size_t datalen);
int recv_crypto_data(ngtcp2_crypto_level crypto_level, const uint8_t *data,
size_t datalen);
ngtcp2_conn *conn() const;
void update_remote_addr(const ngtcp2_addr *addr, const ngtcp2_pkt_info *pi);
int send_packet();
void remove_tx_crypto_data(ngtcp2_crypto_level crypto_level, uint64_t offset,
uint64_t datalen);
int on_stream_close(int64_t stream_id, uint64_t app_error_code);
int on_extend_max_streams();
int handle_error();
int make_stream_early();
int change_local_addr();
void start_change_local_addr_timer();
int update_key(uint8_t *rx_secret, uint8_t *tx_secret,
ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
const uint8_t *current_rx_secret,
const uint8_t *current_tx_secret, size_t secretlen);
int initiate_key_update();
void start_key_update_timer();
void start_delay_stream_timer();
int on_key(ngtcp2_crypto_level level, const uint8_t *rx_secret,
const uint8_t *tx_secret, size_t secretlen);
void set_tls_alert(uint8_t alert);
int select_preferred_address(Address &selected_addr,
const ngtcp2_preferred_addr *paddr);
int setup_httpconn();
int submit_http_request(const Stream *stream);
int recv_stream_data(uint32_t flags, int64_t stream_id, const uint8_t *data,
size_t datalen);
int acked_stream_data_offset(int64_t stream_id, uint64_t datalen);
int http_acked_stream_data(int64_t stream_id, size_t datalen);
void http_consume(int64_t stream_id, size_t nconsumed);
void http_write_data(int64_t stream_id, const uint8_t *data, size_t datalen);
int on_stream_reset(int64_t stream_id);
int extend_max_stream_data(int64_t stream_id, uint64_t max_data);
int send_stop_sending(int64_t stream_id, uint64_t app_error_code);
int http_stream_close(int64_t stream_id, uint64_t app_error_code);
void reset_idle_timer();
void write_qlog(const void *data, size_t datalen);
void idle_timeout();
private:
Address local_addr_;
Address remote_addr_;
unsigned int ecn_;
size_t max_pktlen_;
ev_io wev_;
ev_io rev_;
ev_timer timer_;
ev_timer rttimer_;
ev_timer change_local_addr_timer_;
ev_timer key_update_timer_;
ev_timer delay_stream_timer_;
ev_signal sigintev_;
struct ev_loop *loop_;
SSL_CTX *ssl_ctx_;
SSL *ssl_;
int fd_;
std::map<int64_t, std::unique_ptr<Stream>> streams_;
Crypto crypto_[3];
FILE *qlog_;
ngtcp2_conn *conn_;
nghttp3_conn *httpconn_;
// addr_ is the server host address.
const char *addr_;
// port_ is the server port.
const char *port_;
QUICError last_error_;
// common buffer used to store packet data before sending
Buffer sendbuf_;
// nstreams_done_ is the number of streams opened.
size_t nstreams_done_;
// nstreams_closed_ is the number of streams get closed.
size_t nstreams_closed_;
// nkey_update_ is the number of key update occurred.
size_t nkey_update_;
uint32_t version_;
// early_data_ is true if client attempts to do 0RTT data transfer.
bool early_data_;
// should_exit_ is true if client should exit rather than waiting
// for timeout.
bool should_exit_;
// handshake_confirmed_ gets true after handshake has been
// confirmed.
bool handshake_confirmed_;
};
namespace {
int write_transport_params(const char *path,
const ngtcp2_transport_params *params) {
auto f = std::ofstream(path);
if (!f) {
return -1;
}
f << "initial_max_streams_bidi=" << params->initial_max_streams_bidi << '\n'
<< "initial_max_streams_uni=" << params->initial_max_streams_uni << '\n'
<< "initial_max_stream_data_bidi_local="
<< params->initial_max_stream_data_bidi_local << '\n'
<< "initial_max_stream_data_bidi_remote="
<< params->initial_max_stream_data_bidi_remote << '\n'
<< "initial_max_stream_data_uni=" << params->initial_max_stream_data_uni
<< '\n'
<< "initial_max_data=" << params->initial_max_data << '\n';
f.close();
if (!f) {
return -1;
}
return 0;
}
} // namespace
namespace {
int read_transport_params(const char *path, ngtcp2_transport_params *params) {
auto f = std::ifstream(path);
if (!f) {
return -1;
}
for (std::string line; std::getline(f, line);) {
if (util::istarts_with_l(line, "initial_max_streams_bidi=")) {
params->initial_max_streams_bidi = strtoul(
line.c_str() + str_size("initial_max_streams_bidi="), nullptr, 10);
} else if (util::istarts_with_l(line, "initial_max_streams_uni=")) {
params->initial_max_streams_uni = strtoul(
line.c_str() + str_size("initial_max_streams_uni="), nullptr, 10);
} else if (util::istarts_with_l(line,
"initial_max_stream_data_bidi_local=")) {
params->initial_max_stream_data_bidi_local = strtoul(
line.c_str() + str_size("initial_max_stream_data_bidi_local="),
nullptr, 10);
} else if (util::istarts_with_l(line,
"initial_max_stream_data_bidi_remote=")) {
params->initial_max_stream_data_bidi_remote = strtoul(
line.c_str() + str_size("initial_max_stream_data_bidi_remote="),
nullptr, 10);
} else if (util::istarts_with_l(line, "initial_max_stream_data_uni=")) {
params->initial_max_stream_data_uni = strtoul(
line.c_str() + str_size("initial_max_stream_data_uni="), nullptr, 10);
} else if (util::istarts_with_l(line, "initial_max_data=")) {
params->initial_max_data =
strtoul(line.c_str() + str_size("initial_max_data="), nullptr, 10);
}
}
return 0;
}
} // namespace
int Client::on_key(ngtcp2_crypto_level level, const uint8_t *rx_secret,
const uint8_t *tx_secret, size_t secretlen) {
std::array<uint8_t, 64> rx_key, rx_iv, rx_hp_key, tx_key, tx_iv, tx_hp_key;
if (ngtcp2_crypto_derive_and_install_rx_key(
conn_, rx_key.data(), rx_iv.data(), rx_hp_key.data(), level,
rx_secret, secretlen) != 0) {
return -1;
}
if (ngtcp2_crypto_derive_and_install_tx_key(
conn_, tx_key.data(), tx_iv.data(), tx_hp_key.data(), level,
tx_secret, secretlen) != 0) {
return -1;
}
auto crypto_ctx = ngtcp2_conn_get_crypto_ctx(conn_);
auto aead = &crypto_ctx->aead;
auto keylen = ngtcp2_crypto_aead_keylen(aead);
auto ivlen = ngtcp2_crypto_packet_protection_ivlen(aead);
const char *title = nullptr;
switch (level) {
case NGTCP2_CRYPTO_LEVEL_EARLY:
title = "early_traffic";
keylog::log_secret(ssl_, keylog::QUIC_CLIENT_EARLY_TRAFFIC_SECRET,
tx_secret, secretlen);
break;
case NGTCP2_CRYPTO_LEVEL_HANDSHAKE:
title = "handshake_traffic";
keylog::log_secret(ssl_, keylog::QUIC_SERVER_HANDSHAKE_TRAFFIC_SECRET,
rx_secret, secretlen);
keylog::log_secret(ssl_, keylog::QUIC_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
tx_secret, secretlen);
break;
case NGTCP2_CRYPTO_LEVEL_APP:
title = "application_traffic";
keylog::log_secret(ssl_, keylog::QUIC_SERVER_TRAFFIC_SECRET_0, rx_secret,
secretlen);
keylog::log_secret(ssl_, keylog::QUIC_CLIENT_TRAFFIC_SECRET_0, tx_secret,
secretlen);
break;
default:
assert(0);
}
if (!config.quiet && config.show_secret) {
if (rx_secret) {
std::cerr << title << " rx secret" << std::endl;
debug::print_secrets(rx_secret, secretlen, rx_key.data(), keylen,
rx_iv.data(), ivlen, rx_hp_key.data(), keylen);
}
std::cerr << title << " tx secret" << std::endl;
debug::print_secrets(tx_secret, secretlen, tx_key.data(), keylen,
tx_iv.data(), ivlen, tx_hp_key.data(), keylen);
}
if (level == NGTCP2_CRYPTO_LEVEL_APP) {
if (config.tp_file) {
ngtcp2_transport_params params;
ngtcp2_conn_get_remote_transport_params(conn_, ¶ms);
if (write_transport_params(config.tp_file, ¶ms) != 0) {
std::cerr << "Could not write transport parameters in "
<< config.tp_file << std::endl;
}
}
if (setup_httpconn() != 0) {
return -1;
}
}
return 0;
}
namespace {
void writecb(struct ev_loop *loop, ev_io *w, int revents) {
ev_io_stop(loop, w);
auto c = static_cast<Client *>(w->data);
auto rv = c->on_write();
switch (rv) {
case 0:
return;
case NETWORK_ERR_SEND_BLOCKED:
c->start_wev();
return;
}
}
} // namespace
namespace {
void readcb(struct ev_loop *loop, ev_io *w, int revents) {
auto c = static_cast<Client *>(w->data);
if (c->on_read() != 0) {
return;
}
auto rv = c->on_write();
switch (rv) {
case 0:
return;
case NETWORK_ERR_SEND_BLOCKED:
c->start_wev();
return;
}
}
} // namespace
namespace {
void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
auto c = static_cast<Client *>(w->data);
if (!config.quiet) {
std::cerr << "Timeout" << std::endl;
}
c->idle_timeout();
}
} // namespace
void Client::idle_timeout() {
last_error_ = quic_err_idle_timeout();
disconnect();
}
namespace {
void retransmitcb(struct ev_loop *loop, ev_timer *w, int revents) {
int rv;
auto c = static_cast<Client *>(w->data);
rv = c->handle_expiry();
if (rv != 0) {
goto fail;
}
rv = c->on_write();
if (rv != 0) {
goto fail;
}
return;
fail:
switch (rv) {
case NETWORK_ERR_SEND_BLOCKED:
c->start_wev();
return;
default:
c->disconnect();
return;
}
}
} // namespace
namespace {
void change_local_addrcb(struct ev_loop *loop, ev_timer *w, int revents) {
auto c = static_cast<Client *>(w->data);
c->change_local_addr();
}
} // namespace
namespace {
void key_updatecb(struct ev_loop *loop, ev_timer *w, int revents) {
auto c = static_cast<Client *>(w->data);
if (c->initiate_key_update() != 0) {
c->disconnect();
}
}
} // namespace
namespace {
void delay_streamcb(struct ev_loop *loop, ev_timer *w, int revents) {
auto c = static_cast<Client *>(w->data);
ev_timer_stop(loop, w);
c->on_extend_max_streams();
auto rv = c->on_write();
switch (rv) {
case 0:
return;
case NETWORK_ERR_SEND_BLOCKED:
c->start_wev();
return;
}
}
} // namespace
namespace {
void siginthandler(struct ev_loop *loop, ev_signal *w, int revents) {
ev_break(loop, EVBREAK_ALL);
}
} // namespace
#define NGTCP2_MAX_PKTLEN_IPV4 1252
#define NGTCP2_MAX_PKTLEN_IPV6 1232
#define NGTCP2_SECONDS ((uint64_t)1000000000ULL)
Client::Client(struct ev_loop *loop, SSL_CTX *ssl_ctx)
: local_addr_{},
remote_addr_{},
ecn_(0),
max_pktlen_(0),
loop_(loop),
ssl_ctx_(ssl_ctx),
ssl_(nullptr),
fd_(-1),
crypto_{},
qlog_(nullptr),
conn_(nullptr),
httpconn_(nullptr),
addr_(nullptr),
port_(nullptr),
last_error_{QUICErrorType::Transport, 0},
sendbuf_{NGTCP2_MAX_PKTLEN_IPV4},
nstreams_done_(0),
nstreams_closed_(0),
nkey_update_(0),
version_(0),
early_data_(false),
should_exit_(false),
handshake_confirmed_(false) {
ev_io_init(&wev_, writecb, 0, EV_WRITE);
ev_io_init(&rev_, readcb, 0, EV_READ);
wev_.data = this;
rev_.data = this;
ev_timer_init(&timer_, timeoutcb, 0.,
static_cast<double>(config.timeout) / NGTCP2_SECONDS);
timer_.data = this;
ev_timer_init(&rttimer_, retransmitcb, 0., 0.);
rttimer_.data = this;
ev_timer_init(&change_local_addr_timer_, change_local_addrcb,
static_cast<double>(config.change_local_addr) / NGTCP2_SECONDS,
0.);
change_local_addr_timer_.data = this;
ev_timer_init(&key_update_timer_, key_updatecb,
static_cast<double>(config.key_update) / NGTCP2_SECONDS, 0.);
key_update_timer_.data = this;
ev_timer_init(&delay_stream_timer_, delay_streamcb,
static_cast<double>(config.delay_stream) / NGTCP2_SECONDS, 0.);
delay_stream_timer_.data = this;
ev_signal_init(&sigintev_, siginthandler, SIGINT);
}
Client::~Client() {
disconnect();
close();
}
void Client::disconnect() {
handle_error();
config.tx_loss_prob = 0;
ev_timer_stop(loop_, &delay_stream_timer_);
ev_timer_stop(loop_, &key_update_timer_);
ev_timer_stop(loop_, &change_local_addr_timer_);
ev_timer_stop(loop_, &rttimer_);
ev_timer_stop(loop_, &timer_);
ev_io_stop(loop_, &rev_);
ev_signal_stop(loop_, &sigintev_);
}
void Client::close() {
ev_io_stop(loop_, &wev_);
if (httpconn_) {
nghttp3_conn_del(httpconn_);
httpconn_ = nullptr;
}
if (conn_) {
ngtcp2_conn_del(conn_);
conn_ = nullptr;
}
if (ssl_) {
SSL_free(ssl_);
ssl_ = nullptr;
}
if (fd_ != -1) {
::close(fd_);
fd_ = -1;
}
if (qlog_) {
fclose(qlog_);
qlog_ = nullptr;
}
}
namespace {
int recv_crypto_data(ngtcp2_conn *conn, ngtcp2_crypto_level crypto_level,
uint64_t offset, const uint8_t *data, size_t datalen,
void *user_data) {
if (!config.quiet && !config.no_quic_dump) {
debug::print_crypto_data(crypto_level, data, datalen);
}
auto c = static_cast<Client *>(user_data);
if (c->recv_crypto_data(crypto_level, data, datalen) != 0) {
if (auto err = ngtcp2_conn_get_tls_error(conn); err) {
return err;
}
return NGTCP2_ERR_CRYPTO;
}
return 0;
}
} // namespace
namespace {
int recv_stream_data(ngtcp2_conn *conn, uint32_t flags, int64_t stream_id,
uint64_t offset, const uint8_t *data, size_t datalen,
void *user_data, void *stream_user_data) {
if (!config.quiet && !config.no_quic_dump) {
debug::print_stream_data(stream_id, data, datalen);
}
auto c = static_cast<Client *>(user_data);
if (c->recv_stream_data(flags, stream_id, data, datalen) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
namespace {
int acked_crypto_offset(ngtcp2_conn *conn, ngtcp2_crypto_level crypto_level,
uint64_t offset, uint64_t datalen, void *user_data) {
auto c = static_cast<Client *>(user_data);
c->remove_tx_crypto_data(crypto_level, offset, datalen);
return 0;
}
} // namespace
namespace {
int acked_stream_data_offset(ngtcp2_conn *conn, int64_t stream_id,
uint64_t offset, uint64_t datalen, void *user_data,
void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->acked_stream_data_offset(stream_id, datalen) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
namespace {
int handshake_completed(ngtcp2_conn *conn, void *user_data) {
auto c = static_cast<Client *>(user_data);
if (!config.quiet) {
debug::handshake_completed(conn, user_data);
}
if (c->handshake_completed() != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
# define SSL_EARLY_DATA_NOT_SENT 0
# define SSL_EARLY_DATA_REJECTED 1
# define SSL_EARLY_DATA_ACCEPTED 2
int Client::handshake_completed() {
if (!config.quiet) {
// SSL_get_early_data_status works after handshake completes.
if (early_data_ &&
SSL_get_early_data_status(ssl_) != SSL_EARLY_DATA_ACCEPTED) {
std::cerr << "Early data was rejected by server" << std::endl;
if (auto rv = ngtcp2_conn_early_data_rejected(conn_); rv != 0) {
std::cerr << "ngtcp2_conn_early_data_rejected: " << ngtcp2_strerror(rv)
<< std::endl;
return -1;
}
}
std::cerr << "Negotiated cipher suite is " << SSL_CIPHER_get_name(SSL_get_current_cipher(ssl_)) << std::endl;
const unsigned char *alpn = nullptr;
unsigned int alpnlen;
SSL_get0_alpn_selected(ssl_, &alpn, &alpnlen);
if (alpn) {
std::cerr << "Negotiated ALPN is ";
std::cerr.write(reinterpret_cast<const char *>(alpn), alpnlen);
std::cerr << std::endl;
}
}
return 0;
}
namespace {
int handshake_confirmed(ngtcp2_conn *conn, void *user_data) {
auto c = static_cast<Client *>(user_data);
if (!config.quiet) {
debug::handshake_confirmed(conn, user_data);
}
if (c->handshake_confirmed() != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
int Client::handshake_confirmed() {
handshake_confirmed_ = true;
if (config.change_local_addr) {
start_change_local_addr_timer();
}
if (config.key_update) {
start_key_update_timer();
}
if (config.delay_stream) {
start_delay_stream_timer();
}
return 0;
}
int stream_close(ngtcp2_conn *conn, int64_t stream_id, uint64_t app_error_code,
void *user_data, void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->on_stream_close(stream_id, app_error_code) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
int stream_reset(ngtcp2_conn *conn, int64_t stream_id, uint64_t final_size,
uint64_t app_error_code, void *user_data,
void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->on_stream_reset(stream_id) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
int extend_max_streams_bidi(ngtcp2_conn *conn, uint64_t max_streams,
void *user_data) {
auto c = static_cast<Client *>(user_data);
if (c->on_extend_max_streams() != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
int rand(uint8_t *dest, size_t destlen, const ngtcp2_rand_ctx *rand_ctx,
ngtcp2_rand_usage usage) {
auto dis = std::uniform_int_distribution<uint8_t>(0, 255);
std::generate(dest, dest + destlen, [&dis]() { return dis(randgen); });
return 0;
}
int get_new_connection_id(ngtcp2_conn *conn, ngtcp2_cid *cid, uint8_t *token,
size_t cidlen, void *user_data) {
auto dis = std::uniform_int_distribution<uint8_t>(0, 255);
auto f = [&dis]() { return dis(randgen); };
std::generate_n(cid->data, cidlen, f);
cid->datalen = cidlen;
auto md = ngtcp2_crypto_md{const_cast<EVP_MD *>(EVP_sha256())};
if (ngtcp2_crypto_generate_stateless_reset_token(
token, &md, config.static_secret.data(), config.static_secret.size(),
cid) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
namespace {
int remove_connection_id(ngtcp2_conn *conn, const ngtcp2_cid *cid,
void *user_data) {
return 0;
}
} // namespace
namespace
{
int do_hp_mask(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
const ngtcp2_crypto_cipher_ctx *hp_ctx, const uint8_t *sample)
{
if (ngtcp2_crypto_hp_mask(dest, hp, hp_ctx, sample) != 0)
{
return NGTCP2_ERR_CALLBACK_FAILURE;
}
if (!config.quiet && config.show_secret)
{
debug::print_hp_mask(dest, 5, sample, 16);
}
return 0;
}
} // namespace
namespace {
int update_key(ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
const uint8_t *current_rx_secret,
const uint8_t *current_tx_secret, size_t secretlen,
void *user_data) {
auto c = static_cast<Client *>(user_data);
if (c->update_key(rx_secret, tx_secret, rx_aead_ctx, rx_iv, tx_aead_ctx,
tx_iv, current_rx_secret, current_tx_secret,
secretlen) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
namespace {
int path_validation(ngtcp2_conn *conn, const ngtcp2_path *path,
ngtcp2_path_validation_result res, void *user_data) {
if (!config.quiet) {
debug::path_validation(path, res);
}
return 0;
}
} // namespace
namespace {
int select_preferred_address(ngtcp2_conn *conn, ngtcp2_addr *dest,
const ngtcp2_preferred_addr *paddr,
void *user_data) {
auto c = static_cast<Client *>(user_data);
Address addr;
if (config.no_preferred_addr) {
return 0;
}
if (c->select_preferred_address(addr, paddr) != 0) {
dest->addrlen = 0;
return 0;
}
dest->addrlen = addr.len;
memcpy(dest->addr, &addr.su, dest->addrlen);
return 0;
}
} // namespace
namespace {
int extend_max_stream_data(ngtcp2_conn *conn, int64_t stream_id,
uint64_t max_data, void *user_data,
void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->extend_max_stream_data(stream_id, max_data) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
int Client::extend_max_stream_data(int64_t stream_id, uint64_t max_data) {
if (auto rv = nghttp3_conn_unblock_stream(httpconn_, stream_id); rv != 0) {
std::cerr << "nghttp3_conn_unblock_stream: " << nghttp3_strerror(rv)
<< std::endl;
return -1;
}
return 0;
}
namespace {
int recv_new_token(ngtcp2_conn *conn, const ngtcp2_vec *token,
void *user_data) {
if (config.token_file.empty()) {
return 0;
}