-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
2222 lines (1944 loc) · 55.3 KB
/
client.c
File metadata and controls
2222 lines (1944 loc) · 55.3 KB
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
/**
* http3 client by using ngtcp2 and nghttp3
* make http3_client && SSLKEYLOGFILE=keylog.txt ./build/http3_client --disable-early-data -k 142.251.42.238 443
* make http3_client && SSLKEYLOGFILE=keylog.txt ./build/http3_client --disable-early-data -k www.example.org 443
* make http3_client && SSLKEYLOGFILE=keylog.txt ./build/http3_client --disable-early-data -k $REMOTE_IP 443
* make run_http3
* 1. http3 stream data数据分成2个packet,后面packet只是发送了fin,没有任何数据,nghttp3不能正确处理
* 比如 make http3_client && SSLKEYLOGFILE=keylog.txt ./build/http3_client 142.251.42.238 443 ca_cert.pem
* fix: 主要是因为收到服务端数据后,qpack decoder stream blocked, 注释以下两行解决
// settings.qpack_max_dtable_capacity = 4000;
// settings.qpack_blocked_streams = 100;
* 2. connection migration DONE
* 3. early data 0-RTT DONE
* 4. getopts for keyupdate, ciphers, groups etc DONE
* 5. keyupdate DONE
*/
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <ngtcp2/ngtcp2.h>
#include <ngtcp2/ngtcp2_crypto.h>
#include <ngtcp2/ngtcp2_crypto_quictls.h>
#include <nghttp3/nghttp3.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <execinfo.h>
#include <getopt.h>
#define MAX_EVENTS 64
#define MAX_BUFFER 1280
#define PRINT_BUF 512
#define SOURCE_PORT 9000
#define MIGRATION_PORT 9001
#define LOCAL_MAX_IDLE_TIMEOUT 60
/**
* https://www.openssl.org/docs/manmaster/man3/SSL_set_alpn_protos.html
* NOTE关于protocol-lists format
*/
#define ALPN "\x2h3"
int ssl_userdata_idx;
const char LOWER_XDIGITS[] = "0123456789abcdef";
typedef enum
{
INFO,
WARNING,
ERROR
} Level;
void print_debug(int level, const char *fmt, ...)
{
char msgbuf[PRINT_BUF];
memset(msgbuf, 0, PRINT_BUF);
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(msgbuf, PRINT_BUF, fmt, ap);
va_end(ap);
if (n > PRINT_BUF)
n = PRINT_BUF;
msgbuf[n++] = '\n';
fprintf(stderr, "[%s] %s",
level == INFO ? "INFO" : (level == WARNING ? "WARNING" : "ERROR"),
msgbuf);
}
struct stream
{
int64_t stream_id;
char *data;
size_t datalen;
size_t nwrite;
};
typedef struct
{
int check_cert; // 是否检验server证书
int verbose; // 是否打印调试信息
int disable_early_data; // 是否禁用early data功能
char *ca_file;
char *private_key_file;
char *session_file; // 保存new session ticket信息的PEM文件,用于session resumption or 0-RTT
char *quic_transport_parameter_file; // save previous QUIC transprant parameters as PEM file, used for session resumption or 0-RTT
char *ciphers;
char *groups;
int show_secret;
} Config;
struct client
{
/* TLS1.3 related */
SSL_CTX *ssl_ctx;
SSL *ssl;
/* ngtcp2 related */
ngtcp2_crypto_conn_ref conn_ref;
struct sockaddr_storage local_addr;
socklen_t local_addrlen;
struct sockaddr_storage remote_addr;
socklen_t remote_addrlen;
ngtcp2_conn *conn;
ngtcp2_ccerr last_error;
/* nghttp3 related */
nghttp3_conn *httpconn;
/* key update counter */
int nkey_update;
/* Session Resumption or 0-RTT */
int early_data_enabled;
int ticket_received;
int handshake_confirmed; // 设置状态,暂时没有用到
Config config;
struct stream stream; // 暂时没有用到
/* server交互socket */
int sock_fd;
int timer_fd;
int epoll_fd;
int sig_fd;
};
int get_ip_port(struct sockaddr_storage *addr, char *ip, uint16_t *port)
{
if (ip == NULL && port == NULL)
return 0;
if (addr->ss_family == AF_INET)
{
struct sockaddr_in *addrV4 = (struct sockaddr_in *)addr;
if (port)
*port = ntohs(addrV4->sin_port);
if (ip)
inet_ntop(addrV4->sin_family, &(addrV4->sin_addr), ip, INET_ADDRSTRLEN);
return 0;
}
else if (addr->ss_family == AF_INET6)
{
struct sockaddr_in6 *addrV6 = (struct sockaddr_in6 *)addr;
if (ip)
inet_ntop(addrV6->sin6_family, &(addrV6->sin6_addr), ip, INET6_ADDRSTRLEN);
if (port)
*port = ntohs(addrV6->sin6_port);
return 0;
}
return -1;
}
void print_backtrace()
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace(array, 10);
strings = backtrace_symbols(array, size);
if (strings == NULL)
{
perror("backtrace_symbols");
exit(-1);
}
printf("Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
{
printf("%s\n", strings[i]);
}
free(strings);
}
uint8_t *hexdump_addr(uint8_t *dest, size_t addr)
{
// Lower 32 bits are displayed.
for (size_t i = 0; i < 4; ++i)
{
size_t a = (addr >> (3 - i) * 8) & 0xff;
*dest++ = LOWER_XDIGITS[a >> 4];
*dest++ = LOWER_XDIGITS[a & 0xf];
}
return dest;
}
uint8_t *hexdump_ascii(uint8_t *dest, const uint8_t *data, size_t datalen)
{
*dest++ = '|';
for (size_t i = 0; i < datalen; ++i)
{
/* ASCII 0x20-0x7e 为可见字符, 30 - 0, 41 - A, 61 - a */
if (0x20 <= data[i] && data[i] <= 0x7e)
{
*dest++ = data[i];
}
else /* 不可见字符使用 . 代替 */
{
*dest++ = '.';
}
}
*dest++ = '|';
return dest;
}
uint8_t *hexdump8(uint8_t *dest, const uint8_t *data, size_t datalen)
{
size_t i;
for (i = 0; i < datalen; ++i)
{
*dest++ = LOWER_XDIGITS[data[i] >> 4];
*dest++ = LOWER_XDIGITS[data[i] & 0xf];
*dest++ = ' ';
}
for (; i < 8; ++i)
{
*dest++ = ' ';
*dest++ = ' ';
*dest++ = ' ';
}
return dest;
}
uint8_t *hexdump16(uint8_t *dest, const uint8_t *data, size_t datalen)
{
if (datalen > 8)
{
dest = hexdump8(dest, data, 8);
*dest++ = ' ';
dest = hexdump8(dest, data + 8, datalen - 8);
*dest++ = ' ';
}
else
{
dest = hexdump8(dest, data, datalen);
*dest++ = ' ';
dest = hexdump8(dest, NULL, 0);
*dest++ = ' ';
}
return dest;
}
uint8_t *hexdump_line(uint8_t *dest, const uint8_t *data, size_t datalen,
size_t addr)
{
dest = hexdump_addr(dest, addr);
*dest++ = ' ';
*dest++ = ' ';
dest = hexdump16(dest, data, datalen);
return hexdump_ascii(dest, data, datalen);
}
int hexdump_write(int fd, const uint8_t *data, size_t datalen)
{
ssize_t nwrite;
for (; (nwrite = write(fd, data, datalen)) == -1 && errno == EINTR;)
;
if (nwrite == -1)
{
return -1;
}
return 0;
}
int hexdump(FILE *out, const void *data, size_t datalen)
{
if (datalen == 0)
{
return 0;
}
// min_space is the additional minimum space that the buffer must
// accept, which is the size of a single full line output + one
// repeat line marker ("*\n"). If the remaining buffer size is less
// than that, flush the buffer and reset.
const size_t min_space = 79 + 2;
int fd = fileno(out);
uint8_t buf[4096];
uint8_t *last = buf;
uint8_t *in = (uint8_t *)data;
int repeated = 0;
for (size_t offset = 0; offset < datalen; offset += 16)
{
size_t n = datalen - offset; /* 当前offset */
uint8_t *s = in + offset; /* 原始数据 */
if (n >= 16) /* 如果剩下的数据能打印一整行(16 bytes) */
{
n = 16;
if (offset > 0) /* 不是首行 */
{
if (!strncmp((char *)s - 16, (char *)s, 16)) /* 判断数据上一行和当前行是否一样 */
{
if (repeated)
{
continue;
}
repeated = 1;
*last++ = '*';
*last++ = '\n';
continue;
}
repeated = 0;
}
}
last = hexdump_line(last, s, n, offset);
*last++ = '\n';
size_t len = (size_t)(last - buf);
if (len + min_space > 4096)
{
if (hexdump_write(fd, buf, len) != 0)
{
return -1;
}
last = buf;
}
}
last = hexdump_addr(last, datalen);
*last++ = '\n';
size_t len = (size_t)(last - buf);
if (len)
{
return hexdump_write(fd, buf, len);
}
return 0;
}
uint64_t timestamp(void)
{
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) < 0)
return 0;
return (uint64_t)tp.tv_sec * NGTCP2_SECONDS + (uint64_t)tp.tv_nsec;
}
int send_packet(struct client *c, uint8_t *data, size_t datalen)
{
struct iovec iov = {data, datalen};
struct msghdr msg = {0};
ssize_t nwrite;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
do
{
nwrite = sendmsg(c->sock_fd, &msg, 0);
} while (nwrite == -1 && errno == EINTR);
if (nwrite == -1)
{
fprintf(stderr, "sendmsg: %s\n", strerror(errno));
return -1;
}
return 0;
}
int write_to_stream(struct client *c)
{
int res;
ngtcp2_path_storage ps;
ngtcp2_path_storage_zero(&ps);
uint8_t stream_buf[MAX_BUFFER];
uint64_t ts = timestamp();
ngtcp2_pkt_info pi;
nghttp3_vec http3_vec[16];
for (;;)
{
nghttp3_ssize sveccnt = 0;
int fin = 0;
int64_t stream_id = -1;
if (c->httpconn && ngtcp2_conn_get_max_data_left(c->conn))
{
sveccnt = nghttp3_conn_writev_stream(c->httpconn, &stream_id, &fin, http3_vec, 16);
if (sveccnt < 0)
{
fprintf(stderr, "nghttp3_conn_writev_stream failed: %s\n", nghttp3_strerror(sveccnt));
return -1;
}
}
ngtcp2_vec *datav;
datav = (ngtcp2_vec *)http3_vec;
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
if (fin)
flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
/**
* https://nghttp2.org/ngtcp2/ngtcp2_conn_writev_stream.html
* ndatalen 表示里面有多少stream frame数据, 注意如果只有fin的0 stream数据则值为0
* nwrite 表示总共写入stream_buf数据
*/
ngtcp2_ssize ndatalen, nwrite;
nwrite = ngtcp2_conn_writev_stream(c->conn,
&ps.path,
&pi,
stream_buf, sizeof(stream_buf),
&ndatalen, flags,
stream_id,
datav, sveccnt,
ts);
if (nwrite < 0)
{
switch (nwrite)
{
case NGTCP2_ERR_WRITE_MORE:
assert(ndatalen >= 0);
res = nghttp3_conn_add_write_offset(c->httpconn, stream_id, ndatalen);
if (res < 0)
{
fprintf(stderr, "nghttp3_conn_add_write_offset failed: %s\n", nghttp3_strerror(res));
return -1;
}
continue;
default:
fprintf(stderr, "ngtcp2_conn_writev_stream: %s\n", ngtcp2_strerror((int)nwrite));
ngtcp2_ccerr_set_liberr(&c->last_error, (int)nwrite, NULL, 0);
return -1;
}
return -1;
}
else if (ndatalen >= 0)
{
/* 告诉nghttp3,ngtcp2中的QUIC层接受了多少数据 */
res = nghttp3_conn_add_write_offset(c->httpconn, stream_id, ndatalen);
if (res < 0)
{
fprintf(stderr, "nghttp3_conn_add_write_offset failed: %s\n", nghttp3_strerror(res));
ngtcp2_ccerr_set_application_error(&c->last_error, (int)nwrite, NULL, 0);
return -1;
}
}
// 不能写入frame,可能buffer太小或者拥塞控制了, 只能继续读和等待
if (nwrite == 0)
{
ngtcp2_conn_update_pkt_tx_time(c->conn, ts);
return 0;
}
res = send_packet(c, (uint8_t *)stream_buf, nwrite);
if (res < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
fprintf(stderr, "send_packet失败\n");
return -1;
}
ngtcp2_conn_update_pkt_tx_time(c->conn, ts);
return 0;
}
return 0;
}
int connection_write(struct client *c)
{
write_to_stream(c);
ngtcp2_tstamp expiry = ngtcp2_conn_get_expiry(c->conn);
ngtcp2_tstamp now = timestamp();
struct itimerspec it;
memset(&it, 0, sizeof(it));
if (timerfd_settime(c->timer_fd, 0, &it, NULL) < 0)
{
perror("timerfd_settime发生错误");
return -1;
}
if (expiry < now) /* 已经过期了,立即触发调用 ngtcp2_conn_handle_expiry */
{
it.it_value.tv_sec = 0;
it.it_value.tv_nsec = 1;
}
else
{
it.it_value.tv_sec = (expiry - now) / NGTCP2_SECONDS;
it.it_value.tv_nsec = ((expiry - now) % NGTCP2_SECONDS) / NGTCP2_NANOSECONDS;
}
if (timerfd_settime(c->timer_fd, 0, &it, NULL) < 0)
{
perror("timerfd_settime发生错误");
return -1;
}
return 0;
}
void connection_free(struct client *c)
{
if (c->conn)
ngtcp2_conn_del(c->conn);
SSL_free(c->ssl);
SSL_CTX_free(c->ssl_ctx);
epoll_ctl(c->epoll_fd, EPOLL_CTL_DEL, c->sock_fd, NULL);
if (c->timer_fd > 0)
close(c->timer_fd);
if (c->sock_fd > 0)
close(c->sock_fd);
if (c->epoll_fd > 0)
close(c->epoll_fd);
}
void connection_close(struct client *c)
{
ngtcp2_ssize nwrite;
ngtcp2_pkt_info pi;
ngtcp2_path_storage ps;
uint8_t buf[MAX_BUFFER];
if (ngtcp2_conn_in_closing_period(c->conn) ||
ngtcp2_conn_in_draining_period(c->conn))
{
goto fin;
}
ngtcp2_path_storage_zero(&ps);
nwrite = ngtcp2_conn_write_connection_close(
c->conn, &ps.path, &pi, buf, sizeof(buf), &c->last_error, timestamp());
if (nwrite < 0)
{
fprintf(stderr, "ngtcp2_conn_write_connection_close: %s\n",
ngtcp2_strerror((int)nwrite));
goto fin;
}
send_packet(c, buf, (size_t)nwrite);
fin:
epoll_ctl(c->epoll_fd, EPOLL_CTL_DEL, c->timer_fd, NULL);
}
nghttp3_nv make_nv(char *name, char *value, size_t namelen, size_t valuelen, int flags)
{
nghttp3_nv nv = {
(uint8_t *)name,
(uint8_t *)value,
namelen,
valuelen,
flags,
};
return nv;
}
int submit_http_request(struct client *c)
{
assert(c->httpconn);
int64_t stream_id;
int res;
res = ngtcp2_conn_open_bidi_stream(c->conn, &stream_id, NULL);
if (res != 0)
{
fprintf(stderr, "ngtcp2_conn_open_bidi_stream failed: %s\n", ngtcp2_strerror(res));
return -1;
}
nghttp3_nv nva[] = {
make_nv(":method", "GET", 7, 3, NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE),
make_nv(":scheme", "https", 7, 5, NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE),
make_nv(":authority", "47.96.31.23", 10, 11, NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE),
make_nv(":path", "/", 5, 1, NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE),
make_nv("user-agent", "nghttp3/ngtcp2 client", 10, 21, NGHTTP3_NV_FLAG_NO_COPY_NAME | NGHTTP3_NV_FLAG_NO_COPY_VALUE),
};
size_t nvlen = sizeof(nva) / sizeof(nghttp3_nv);
res = nghttp3_conn_submit_request(c->httpconn, stream_id, nva, nvlen, NULL, c);
if (res != 0)
{
fprintf(stderr, "nghttp3_conn_submit_request failed: %s\n", nghttp3_strerror(res));
return -1;
}
return 0;
}
int handle_keyupdate(struct client *c)
{
int res;
if (c->config.verbose)
{
print_debug(INFO, "Initiate key update");
}
if ((res = ngtcp2_conn_initiate_key_update(c->conn, timestamp())) != 0)
{
print_debug(ERROR, "ngtcp2_conn_initiate_key_update: %s(The previous key update has not been confirmed yet; or key update is too frequent; or new keys are not available yet.)", ngtcp2_strerror(res));
return -1;
}
return 0;
}
int handle_migration(struct client *c)
{
int fd, res, oldfd;
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fd < 0)
{
perror("socket create error");
return -1;
}
struct sockaddr_in source;
source.sin_addr.s_addr = htonl(INADDR_ANY);
source.sin_family = AF_INET;
source.sin_port = htons(MIGRATION_PORT);
if (bind(fd, (struct sockaddr *)&source, sizeof(source)) == -1)
{
perror("udp socket bind error");
close(fd);
return -1;
}
if (connect(fd, (struct sockaddr *)(&c->remote_addr), c->remote_addrlen) == -1)
{
perror("migration connection failed");
close(fd);
return -1;
}
struct sockaddr_in local;
local.sin_family = AF_INET;
socklen_t local_len;
if (getsockname(fd, (struct sockaddr *)&local, &local_len) == -1)
{
print_debug(ERROR, "migration getsockname failed: %s", strerror(errno));
close(fd);
return -1;
}
oldfd = c->sock_fd;
c->sock_fd = fd;
memcpy(&c->local_addr, &local, local_len);
c->local_addrlen = local_len;
ngtcp2_addr addr;
ngtcp2_addr_init(&addr, (struct sockaddr *)&local, local_len);
/**
* https://nghttp2.org/ngtcp2/ngtcp2_conn_set_local_addr.html
* This function is provided for testing purpose only
*/
// config.nat_rebinding
if (0)
{
ngtcp2_conn_set_local_addr(c->conn, &addr);
ngtcp2_conn_set_path_user_data(c->conn, c);
}
else
{
ngtcp2_path path = {
addr,
{
(struct sockaddr *)&c->remote_addr,
c->remote_addrlen,
},
c,
};
/* ngtcp2_conn_initiate_migration 会发送path challenge frame, ngtcp2_conn_initiate_immediate_migration不会? */
if ((res = ngtcp2_conn_initiate_immediate_migration(c->conn, &path, timestamp())) != 0)
// if ((res = ngtcp2_conn_initiate_migration(c->conn, &path, timestamp())) != 0)
{
fprintf(stderr, "ngtcp2_conn_initiate_immediate_migration: %s\n", ngtcp2_strerror(res));
return -1;
}
}
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
ev.data.fd = c->sock_fd;
if (epoll_ctl(c->epoll_fd, EPOLL_CTL_ADD, c->sock_fd, &ev) == -1)
{
perror("epoll_ctl添加quic socket失败");
close(fd);
return -1;
}
if (epoll_ctl(c->epoll_fd, EPOLL_CTL_DEL, oldfd, NULL) == -1)
{
perror("migration epoll_ctl delete fd failed");
return -1;
}
close(oldfd);
return 0;
}
/**
* \q exit
* \m migration from port 9000 to 9001
* \k key update manually
*/
int handle_stdin(struct client *c)
{
int ret;
char buf[MAX_BUFFER];
size_t nread = 0;
memset(buf, 0, MAX_BUFFER);
while (nread < sizeof(buf))
{
ret = read(STDIN_FILENO, buf + nread, sizeof(buf) - nread);
if (ret == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
break;
perror("读取STDIN_FILENO错误");
return -1;
}
else if (ret == 0)
{
return 0;
}
else
nread += ret;
}
if (nread == sizeof(buf))
{
perror("读取STDIN_FILENO的buf满了");
return -1;
}
if (strncmp(buf, "\\q", 2) == 0)
{
connection_close(c);
connection_free(c);
exit(0);
}
if (strncmp(buf, "\\m", 2) == 0)
{
return handle_migration(c);
}
if (strncmp(buf, "\\k", 2) == 0)
{
return handle_keyupdate(c);
}
if (submit_http_request(c) == -1)
{
fprintf(stderr, "submit_http_request failed\n");
return -1;
}
connection_write(c);
return nread;
}
int handle_timer(struct client *c)
{
int ret;
ret = ngtcp2_conn_handle_expiry(c->conn, timestamp());
if (ret < 0)
{
fprintf(stderr, "ngtcp2_conn_handle_expiry: %s\n", ngtcp2_strerror((int)ret));
return -1;
}
ret = connection_write(c);
if (ret < 0)
{
fprintf(stderr, "connection_write出问题了\n");
return -1;
}
return 0;
}
int setup_stdin(int epoll_fd)
{
int flags;
struct epoll_event ev;
flags = fcntl(STDIN_FILENO, F_GETFL, 0);
if (flags < 0)
{
perror("获取STDIN_FILENO F_GETFL错误");
return -1;
}
flags = fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
if (flags < 0)
{
perror("设置STDIN_FILENO F_SETFL错误");
return -1;
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = STDIN_FILENO;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
{
perror("epoll_ctl添加STDIN_FILENO失败");
return -1;
}
return 0;
}
int setup_timer(int epoll_fd)
{
struct epoll_event ev;
int timer_fd = -1;
timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (timer_fd < 0)
{
perror("timerfd_create失败");
return -1;
}
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = timer_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, timer_fd, &ev) == -1)
{
perror("epoll_ctl添加timer_fd失败");
return -1;
}
return timer_fd;
}
int resolve_and_connect(const char *host, const char *port,
struct sockaddr *local_addr, size_t *local_addrlen,
struct sockaddr *remote_addr, size_t *remote_addrlen)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int ret, fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
ret = getaddrinfo(host, port, &hints, &result);
if (ret != 0)
return -1;
for (rp = result; rp != NULL; rp = rp->ai_next)
{
fd = socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK,
rp->ai_protocol);
if (fd == -1)
continue;
struct sockaddr_in source;
source.sin_addr.s_addr = htonl(INADDR_ANY);
source.sin_family = rp->ai_family;
source.sin_port = htons(SOURCE_PORT);
if (bind(fd, (struct sockaddr *)&source, sizeof(source)) == -1)
{
perror("udp socket bind error");
return -1;
}
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0)
{
*remote_addrlen = rp->ai_addrlen;
memcpy(remote_addr, rp->ai_addr, rp->ai_addrlen);
socklen_t len = (socklen_t)*local_addrlen;
if (getsockname(fd, local_addr, &len) == -1)
return -1;
*local_addrlen = len;
break;
}
close(fd);
}
freeaddrinfo(result);
if (rp == NULL)
return -1;
return fd;
}
int verify_callback(int preverify_ok, X509_STORE_CTX *x509_store_ctx)
{
char *issuer_name;
X509 *current_cert;
X509_NAME *current_cert_subject;
X509_NAME *current_cert_issuer;
int ssl_ex_data_idx = SSL_get_ex_data_X509_STORE_CTX_idx();
SSL *ssl = X509_STORE_CTX_get_ex_data(x509_store_ctx, ssl_ex_data_idx);
ngtcp2_crypto_conn_ref *conn_ref = (ngtcp2_crypto_conn_ref *)(SSL_get_app_data(ssl));
struct client *c = (struct client *)(conn_ref->user_data);
int depth = X509_STORE_CTX_get_error_depth(x509_store_ctx);
current_cert = X509_STORE_CTX_get_current_cert(x509_store_ctx);
char buf[256];
current_cert_subject = X509_get_subject_name(current_cert);
X509_NAME_oneline(current_cert_subject, buf, 256);
current_cert_issuer = X509_get_issuer_name(current_cert);
if (current_cert_issuer)
{
issuer_name = X509_NAME_oneline(current_cert_issuer, NULL, 0);
}
/* preverify_ok = 1 表示现在这个证书及前面证书链没有错误 */
int error_code = preverify_ok ? X509_V_OK : X509_STORE_CTX_get_error(x509_store_ctx);
if (error_code != X509_V_OK)
{
const char *error_string = X509_verify_cert_error_string(error_code);
print_debug(ERROR, "verify_callback失败: %s", error_string);
}
if (c->config.verbose)
{
print_debug(INFO, "depth: %d, preverify_ok: %d, error_code: %d, error_string: %s, \n\tcert subject: %s\n\tcert issuer: %s",
depth,
preverify_ok,
error_code,
X509_verify_cert_error_string(error_code),
buf,
issuer_name);
}
OPENSSL_free(issuer_name);
return preverify_ok;
}
int numeric_host_family(const char *hostname, int family)
{
uint8_t dst[sizeof(struct in6_addr)];
return inet_pton(family, hostname, dst) == 1;
}
int numeric_host(const char *hostname)
{
return numeric_host_family(hostname, AF_INET) ||
numeric_host_family(hostname, AF_INET6);
}
void keylog_callback(const SSL *ssl, const char *line)
{
int res;
char *keylogfile_path = (char *)SSL_get_ex_data(ssl, ssl_userdata_idx);
if (!keylogfile_path)
{
fprintf(stderr, "keylogfile_path为空\n");
return;
}
int keylogfile = open(keylogfile_path, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (keylogfile == -1)
{
perror("open keylogfile");
exit(-1);
}