forked from alibaba/xquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_client.c
3068 lines (2524 loc) · 95.7 KB
/
demo_client.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
/**
* @copyright Copyright (c) 2022, Alibaba Group Holding Limited
*/
#define _GNU_SOURCE
#include <xquic/xquic.h>
#include <xquic/xquic_typedef.h>
#include <xquic/xqc_http3.h>
#include <stdio.h>
#include <memory.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
#include <string.h>
#include <event2/event.h>
#include "common.h"
#include "xqc_hq.h"
#include "../tests/platform.h"
#ifdef XQC_SYS_WINDOWS
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"event.lib")
#pragma comment(lib, "Iphlpapi.lib")
#pragma comment(lib, "Bcrypt.lib")
#pragma comment(lib, "crypt32")
#include "../tests/getopt.h"
#else
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netdb.h>
#endif
#define XQC_PACKET_TMP_BUF_LEN 1600
#define MAX_BUF_SIZE (100*1024*1024)
#define XQC_INTEROP_TLS_GROUPS "X25519:P-256:P-384:P-521"
#define MAX_PATH_CNT 2
typedef enum xqc_demo_cli_alpn_type_s {
ALPN_HQ,
ALPN_H3,
} xqc_demo_cli_alpn_type_t;
#define MAX_HEADER 100
typedef struct xqc_demo_cli_user_conn_s xqc_demo_cli_user_conn_t;
typedef struct xqc_demo_cli_user_stream_s {
xqc_demo_cli_user_conn_t *user_conn;
/* save file */
char file_name[RESOURCE_LEN];
FILE *recv_body_fp;
/* stat for IO */
size_t send_body_len;
size_t recv_body_len;
int recv_fin;
xqc_msec_t start_time;
/* hq request content */
xqc_hq_request_t *hq_request;
char *send_buf;
size_t send_len;
size_t send_offset;
/* h3 request content */
xqc_h3_request_t *h3_request;
xqc_http_headers_t h3_hdrs;
uint8_t hdr_sent;
} xqc_demo_cli_user_stream_t;
/**
* ============================================================================
* the network config definition section
* network config is those arguments about socket connection
* all configuration on network should be put under this section
* ============================================================================
*/
typedef enum xqc_demo_cli_task_mode_s {
/* send multi requests in single connection with multi streams */
MODE_SCMR,
/* serially send multi requests in multi connections, with one request each connection */
MODE_SCSR_SERIAL,
/* concurrently send multi requests in multi connections, with one request each connection */
MODE_SCSR_CONCURRENT,
} xqc_demo_cli_task_mode_t;
/* network arguments */
typedef struct xqc_demo_cli_net_config_s {
/* server addr info */
struct sockaddr_in6 addr;
int addr_len;
char server_addr[64];
short server_port;
char host[256];
/* ipv4 or ipv6 */
int ipv6;
/* congestion control algorithm */
CC_TYPE cc; /* congestion control algorithm */
int pacing; /* is pacing on */
/* idle persist timeout */
int conn_timeout;
xqc_demo_cli_task_mode_t mode;
char iflist[MAX_PATH_CNT][128]; /* list of interfaces */
int ifcnt;
int multipath;
uint8_t rebind_p0;
uint8_t rebind_p1;
} xqc_demo_cli_net_config_t;
/**
* ============================================================================
* the quic config definition section
* quic config is those arguments about quic connection
* all configuration on network should be put under this section
* ============================================================================
*/
/* definition for quic */
#define MAX_SESSION_TICKET_LEN 8192 /* session ticket len */
#define MAX_TRANSPORT_PARAMS_LEN 8192 /* transport parameter len */
#define XQC_MAX_TOKEN_LEN 8192 /* token len */
#define SESSION_TICKET_FILE "session_ticket"
#define TRANSPORT_PARAMS_FILE "transport_params"
#define TOKEN_FILE "token"
typedef struct xqc_demo_cli_quic_config_s {
/* alpn protocol of client */
xqc_demo_cli_alpn_type_t alpn_type;
char alpn[16];
/* 0-rtt config */
int st_len; /* session ticket len */
char st[MAX_SESSION_TICKET_LEN]; /* session ticket buf */
int tp_len; /* transport params len */
char tp[MAX_TRANSPORT_PARAMS_LEN]; /* transport params buf */
int token_len; /* token len */
char token[XQC_MAX_TOKEN_LEN]; /* token buf */
char *cipher_suites; /* cipher suites */
uint8_t use_0rtt; /* 0-rtt switch, default turned off */
uint64_t keyupdate_pkt_threshold; /* packet limit of a single 1-rtt key, 0 for unlimited */
uint8_t mp_ack_on_any_path;
char mp_sched[32];
uint8_t mp_backup;
uint64_t close_path;
uint8_t no_encryption;
uint64_t recv_rate;
uint32_t reinjection;
uint8_t mp_version;
uint64_t init_max_path_id;
/* support interop test */
int is_interop_mode;
uint8_t send_path_standby;
xqc_msec_t path_status_timer_threshold;
uint64_t least_available_cid_count;
uint64_t idle_timeout;
uint8_t remove_path_flag;
size_t max_pkt_sz;
char co_str[XQC_CO_STR_MAX_LEN];
} xqc_demo_cli_quic_config_t;
/**
* ============================================================================
* the environment config definition section
* environment config is those arguments about IO inputs and outputs
* all configuration on environment should be put under this section
* ============================================================================
*/
#define LOG_PATH "clog.log"
#define KEY_PATH "ckeys.log"
#define OUT_DIR "."
/* environment config */
typedef struct xqc_demo_cli_env_config_s {
/* log path */
char log_path[256];
int log_level;
/* out file */
char out_file_dir[256];
/* key export */
int key_output_flag;
char key_out_path[256];
/* life cycle */
int life;
} xqc_demo_cli_env_config_t;
/**
* ============================================================================
* the request config definition section
* all configuration on request should be put under this section
* ============================================================================
*/
#define MAX_REQUEST_CNT 20480 /* client might deal MAX_REQUEST_CNT requests once */
#define MAX_REQUEST_LEN 256 /* the max length of a request */
#define g_host ""
/* args of one single request */
typedef struct xqc_demo_cli_request_s {
char path[RESOURCE_LEN]; /* request path */
char scheme[8]; /* request scheme, http/https */
REQUEST_METHOD method;
char auth[AUTHORITY_LEN];
char url[URL_LEN]; /* original url */
// char headers[MAX_HEADER][256]; /* field line of h3 */
} xqc_demo_cli_request_t;
/* request bundle args */
typedef struct xqc_demo_cli_requests_s {
/* requests */
int request_cnt; /* requests cnt in urls */
xqc_demo_cli_request_t reqs[MAX_REQUEST_CNT];
/* do not save responses to files */
int dummy_mode;
/* delay X us to start reqs */
uint64_t req_start_delay;
uint64_t idle_gap;
/* serial requests */
uint8_t serial;
int throttled_req;
int ext_reqn;
int batch_cnt;
} xqc_demo_cli_requests_t;
/**
* ============================================================================
* the client args definition section
* client initial args
* ============================================================================
*/
typedef struct xqc_demo_cli_client_args_s {
/* network args */
xqc_demo_cli_net_config_t net_cfg;
/* quic args */
xqc_demo_cli_quic_config_t quic_cfg;
/* environment args */
xqc_demo_cli_env_config_t env_cfg;
/* request args */
xqc_demo_cli_requests_t req_cfg;
} xqc_demo_cli_client_args_t;
typedef enum xqc_demo_cli_task_status_s {
TASK_STATUS_WAITTING,
TASK_STATUS_RUNNING,
TASK_STATUS_FINISHED,
TASK_STATUS_FAILED,
} xqc_demo_cli_task_status_t;
typedef struct xqc_demo_cli_task_schedule_info_s {
xqc_demo_cli_task_status_t status; /* task status */
int req_create_cnt; /* streams created */
int req_sent_cnt;
int req_fin_cnt; /* the req cnt which have received FIN */
uint8_t fin_flag; /* all reqs finished, need close */
} xqc_demo_cli_task_schedule_info_t;
/*
* the task schedule info, used to mark the operation
* info of all requests, the client will exit when all
* tasks are finished or closed
*/
typedef struct xqc_demo_cli_task_schedule_s {
/* the cnt of tasks that been running or have been ran */
int idx;
/* the task status, 0: not executed; 1: suc; -1: failed */
xqc_demo_cli_task_schedule_info_t *schedule_info;
} xqc_demo_cli_task_schedule_t;
/*
* task info structure.
* a task is strongly correlate to a net connection
*/
typedef struct xqc_demo_cli_task_s {
int task_idx;
int req_cnt;
xqc_demo_cli_request_t *reqs; /* a task could contain multipule requests, which wil be sent */
xqc_demo_cli_user_conn_t *user_conn; /* user_conn handle */
} xqc_demo_cli_task_t;
typedef struct xqc_demo_cli_task_ctx_s {
/* task mode */
xqc_demo_cli_task_mode_t mode;
/* total task cnt */
int task_cnt;
/* task list */
xqc_demo_cli_task_t *tasks;
/* current task schedule info */
xqc_demo_cli_task_schedule_t schedule; /* current task index */
} xqc_demo_cli_task_ctx_t;
typedef struct xqc_demo_cli_ctx_s {
/* xquic engine context */
xqc_engine_t *engine;
/* libevent context */
struct event *ev_engine;
struct event *ev_task;
struct event *ev_kill;
struct event_base *eb; /* handle of libevent */
/* log context */
int log_fd;
char log_path[256];
/* key log context */
int keylog_fd;
/* client context */
xqc_demo_cli_client_args_t *args;
/* task schedule context */
xqc_demo_cli_task_ctx_t task_ctx;
} xqc_demo_cli_ctx_t;
typedef struct xqc_demo_cli_user_path_s {
uint64_t path_id;
uint8_t is_active;
int fd;
int rebind_fd;
struct sockaddr_in6 local_addr;
socklen_t local_addrlen;
struct sockaddr_in6 peer_addr;
socklen_t peer_addrlen;
struct event *ev_socket;
struct event *ev_rebind_socket;
struct event *ev_timeout;
uint64_t last_sock_op_time;
xqc_demo_cli_user_conn_t *user_conn;
} xqc_demo_cli_user_path_t;
typedef struct xqc_demo_cli_user_conn_s {
xqc_cid_t cid;
xqc_hq_conn_t *hqc_handle;
xqc_demo_cli_user_path_t paths[MAX_PATH_CNT];
int active_path_cnt;
int total_path_cnt;
struct event *ev_delay_req;
struct event *ev_idle_restart;
struct event *ev_close_path;
struct event *ev_rebinding_p0;
struct event *ev_rebinding_p1;
xqc_demo_cli_ctx_t *ctx;
xqc_demo_cli_task_t *task;
int send_path_standby;
int path_status; /* 0:available 1:standby */
xqc_msec_t path_status_time;
xqc_msec_t path_status_timer_threshold;
xqc_msec_t path_create_time;
xqc_flag_t remove_path_flag;
xqc_msec_t idle_timeout;
} xqc_demo_cli_user_conn_t;
static void
xqc_demo_cli_delayed_idle_restart(int fd, short what, void *arg);
void
xqc_demo_cli_continue_send_reqs(xqc_demo_cli_user_conn_t *user_conn);
void
xqc_demo_cli_send_requests(xqc_demo_cli_user_conn_t *user_conn,
xqc_demo_cli_client_args_t *args,
xqc_demo_cli_request_t *reqs, int req_cnt);
int xqc_demo_cli_init_user_path(xqc_demo_cli_user_conn_t *user_conn,
int path_seq, uint64_t path_id);
int
xqc_demo_cli_close_task(xqc_demo_cli_task_t *task)
{
xqc_demo_cli_user_conn_t *user_conn = task->user_conn;
int i;
for (i = 0; i < user_conn->total_path_cnt; i++) {
if (user_conn->paths[i].is_active) {
user_conn->paths[i].is_active = 0;
user_conn->active_path_cnt--;
/* remove event handle */
if (user_conn->paths[i].ev_socket) {
event_del(user_conn->paths[i].ev_socket);
}
event_del(user_conn->paths[i].ev_timeout);
/* close socket */
close(user_conn->paths[i].fd);
if (user_conn->paths[i].ev_rebind_socket) {
event_del(user_conn->paths[i].ev_rebind_socket);
}
if (user_conn->paths[i].rebind_fd != -1) {
close(user_conn->paths[i].rebind_fd);
}
}
}
if (user_conn->ev_delay_req) {
event_del(user_conn->ev_delay_req);
user_conn->ev_delay_req = NULL;
}
if (user_conn->ev_idle_restart) {
event_del(user_conn->ev_idle_restart);
user_conn->ev_idle_restart = NULL;
}
if (user_conn->ev_close_path) {
event_del(user_conn->ev_close_path);
user_conn->ev_close_path = NULL;
}
if (user_conn->ev_rebinding_p0) {
event_del(user_conn->ev_rebinding_p0);
user_conn->ev_rebinding_p0 = NULL;
}
if (user_conn->ev_rebinding_p1) {
event_del(user_conn->ev_rebinding_p1);
user_conn->ev_rebinding_p1 = NULL;
}
return 0;
}
/**
* [return] 1: all req suc, task finished, 0: still got req underway
*/
void
xqc_demo_cli_on_stream_fin(xqc_demo_cli_user_stream_t *user_stream)
{
xqc_demo_cli_task_ctx_t *ctx = &user_stream->user_conn->ctx->task_ctx;
xqc_demo_cli_user_conn_t *user_conn = user_stream->user_conn;
xqc_demo_cli_ctx_t *conn_ctx = user_conn->ctx;
int task_idx = user_stream->user_conn->task->task_idx;
/* all reqs are finished, close the connection */
if (++ctx->schedule.schedule_info[task_idx].req_fin_cnt
== ctx->tasks[task_idx].req_cnt)
{
ctx->schedule.schedule_info[task_idx].fin_flag = 1;
/* close xquic conn */
if (conn_ctx->args->quic_cfg.alpn_type == ALPN_H3) {
xqc_h3_conn_close(conn_ctx->engine, &user_conn->cid);
} else {
xqc_hq_conn_close(conn_ctx->engine, user_conn->hqc_handle, &user_conn->cid);
}
}
printf("task[%d], fin_cnt: %d, fin_flag: %d\n", task_idx,
ctx->schedule.schedule_info[task_idx].req_fin_cnt,
ctx->schedule.schedule_info[task_idx].fin_flag);
}
/* directly finish a task */
void
xqc_demo_cli_on_task_finish(xqc_demo_cli_ctx_t *ctx, xqc_demo_cli_task_t *task)
{
xqc_demo_cli_close_task(task);
ctx->task_ctx.schedule.schedule_info[task->task_idx].status = TASK_STATUS_FINISHED;
printf("task finished, total task_req_cnt: %d, req_fin_cnt: %d, req_sent_cnt: %d, "
"req_create_cnt: %d\n", task->req_cnt,
ctx->task_ctx.schedule.schedule_info[task->task_idx].req_fin_cnt,
ctx->task_ctx.schedule.schedule_info[task->task_idx].req_sent_cnt,
ctx->task_ctx.schedule.schedule_info[task->task_idx].req_create_cnt);
}
/* directly fail a task */
void
xqc_demo_cli_on_task_fail(xqc_demo_cli_ctx_t *ctx, xqc_demo_cli_task_t *task)
{
ctx->task_ctx.schedule.schedule_info[task->task_idx].status = TASK_STATUS_FAILED;
printf("task failed, total task_req_cnt: %d, req_fin_cnt: %d, req_sent_cnt: %d, "
"req_create_cnt: %d\n", task->req_cnt,
ctx->task_ctx.schedule.schedule_info[task->task_idx].req_fin_cnt,
ctx->task_ctx.schedule.schedule_info[task->task_idx].req_sent_cnt,
ctx->task_ctx.schedule.schedule_info[task->task_idx].req_create_cnt);
}
/******************************************************************************
* start of engine callback functions *
******************************************************************************/
void
xqc_demo_cli_set_event_timer(xqc_usec_t wake_after, void *eng_user_data)
{
xqc_demo_cli_ctx_t *ctx = (xqc_demo_cli_ctx_t *) eng_user_data;
//printf("xqc_engine_wakeup_after %llu us, now %llu\n", wake_after, xqc_now());
struct timeval tv;
tv.tv_sec = wake_after / 1000000;
tv.tv_usec = wake_after % 1000000;
event_add(ctx->ev_engine, &tv);
}
int
xqc_demo_cli_open_log_file(xqc_demo_cli_ctx_t *ctx)
{
ctx->log_fd = open(ctx->log_path, (O_WRONLY | O_APPEND | O_CREAT), 0644);
if (ctx->log_fd <= 0) {
return -1;
}
return 0;
}
int
xqc_demo_cli_close_log_file(xqc_demo_cli_ctx_t *ctx)
{
if (ctx->log_fd <= 0) {
return -1;
}
close(ctx->log_fd);
return 0;
}
void
xqc_demo_cli_write_log_file(xqc_log_level_t lvl, const void *buf, size_t size, void *engine_user_data)
{
xqc_demo_cli_ctx_t *ctx = (xqc_demo_cli_ctx_t*)engine_user_data;
if (ctx->log_fd <= 0) {
return;
}
//printf("%s", (char *)buf);
int write_len = write(ctx->log_fd, buf, size);
if (write_len < 0) {
printf("write log failed, errno: %d\n", get_sys_errno());
return;
}
write_len = write(ctx->log_fd, line_break, 1);
if (write_len < 0) {
printf("write log failed, errno: %d\n", get_sys_errno());
}
}
void
xqc_demo_cli_write_qlog_file(qlog_event_importance_t imp, const void *buf, size_t size, void *engine_user_data)
{
xqc_demo_cli_ctx_t *ctx = (xqc_demo_cli_ctx_t*)engine_user_data;
if (ctx->log_fd <= 0) {
return;
}
int write_len = write(ctx->log_fd, buf, size);
if (write_len < 0) {
printf("write qlog failed, errno: %d\n", get_sys_errno());
return;
}
write_len = write(ctx->log_fd, line_break, 1);
if (write_len < 0) {
printf("write qlog failed, errno: %d\n", get_sys_errno());
}
}
int
xqc_demo_cli_open_keylog_file(xqc_demo_cli_ctx_t *ctx)
{
if (ctx->args->env_cfg.key_output_flag) {
ctx->keylog_fd = open(ctx->args->env_cfg.key_out_path, (O_WRONLY | O_APPEND | O_CREAT), 0644);
if (ctx->keylog_fd <= 0) {
return -1;
}
}
return 0;
}
int
xqc_demo_cli_close_keylog_file(xqc_demo_cli_ctx_t *ctx)
{
if (ctx->keylog_fd <= 0) {
return -1;
}
close(ctx->keylog_fd);
ctx->keylog_fd = 0;
return 0;
}
void
xqc_demo_cli_keylog_cb(const xqc_cid_t *scid, const char *line, void *engine_user_data)
{
xqc_demo_cli_ctx_t *ctx = (xqc_demo_cli_ctx_t*)engine_user_data;
if (ctx->args->env_cfg.key_output_flag == 0) {
return;
}
if (ctx->keylog_fd <= 0) {
printf("write keys error!\n");
return;
}
int write_len = write(ctx->keylog_fd, line, strlen(line));
if (write_len < 0) {
printf("write keys failed, errno: %d\n", get_sys_errno());
return;
}
write_len = write(ctx->keylog_fd, line_break, 1);
if (write_len < 0) {
printf("write keys failed, errno: %d\n", get_sys_errno());
}
}
/******************************************************************************
* start of common callback functions *
******************************************************************************/
void
xqc_demo_cli_save_session_cb(const char *data, size_t data_len, void *conn_user_data)
{
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t*)conn_user_data;
FILE * fp = fopen(SESSION_TICKET_FILE, "wb");
int write_size = fwrite(data, 1, data_len, fp);
if (data_len != write_size) {
printf("save _session_cb error\n");
fclose(fp);
return;
}
fclose(fp);
return;
}
void
xqc_demo_cli_save_tp_cb(const char *data, size_t data_len, void *conn_user_data)
{
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t*)conn_user_data;
FILE * fp = fopen(TRANSPORT_PARAMS_FILE, "wb");
if (NULL == fp) {
printf("open file for transport parameter error\n");
return;
}
int write_size = fwrite(data, 1, data_len, fp);
if (data_len != write_size) {
fclose(fp);
return;
}
fclose(fp);
return;
}
void
xqc_demo_cli_save_token(const unsigned char *token, uint32_t token_len, void *conn_user_data)
{
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t*)conn_user_data;
int fd = open(TOKEN_FILE, O_TRUNC | O_CREAT | O_WRONLY, 0666);
if (fd < 0) {
return;
}
ssize_t n = write(fd, token, token_len);
if (n < token_len) {
close(fd);
return;
}
close(fd);
}
int
xqc_demo_cli_read_token(unsigned char *token, unsigned token_len)
{
int fd = open(TOKEN_FILE, O_RDONLY);
if (fd < 0) {
return -1;
}
ssize_t n = read(fd, token, token_len);
close(fd);
return n;
}
ssize_t
xqc_demo_cli_write_socket_ex(uint64_t path_id, const unsigned char *buf, size_t size,
const struct sockaddr *peer_addr, socklen_t peer_addrlen, void *conn_user_data)
{
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t *)conn_user_data;
ssize_t res = 0;
xqc_demo_cli_user_path_t *user_path = NULL;
int i;
for (i = 0; i < user_conn->total_path_cnt; i++) {
if (user_conn->paths[i].is_active
&& user_conn->paths[i].path_id == path_id)
{
user_path = &user_conn->paths[i];
}
}
if (user_path == NULL) {
// printf("path %"PRIu64" is not avaliable!\n", path_id);
return XQC_SOCKET_ERROR;
}
// printf("path %"PRIu64" with fd:%d\n", path_id, user_path->fd);
do {
set_sys_errno(0);
res = sendto(user_path->fd, buf, size, 0, peer_addr, peer_addrlen);
if (res < 0) {
printf("xqc_demo_cli_write_socket err %zd %s, fd: %d, buf: %p, size: %zu, "
"server_addr: %s\n", res, strerror(get_sys_errno()), user_path->fd, buf, size,
user_conn->ctx->args->net_cfg.server_addr);
if (get_sys_errno() == EAGAIN) {
res = XQC_SOCKET_EAGAIN;
}
}
user_path->last_sock_op_time = xqc_now();
} while ((res < 0) && (get_sys_errno() == EINTR));
return res;
}
ssize_t
xqc_demo_cli_write_socket(const unsigned char *buf, size_t size, const struct sockaddr *peer_addr,
socklen_t peer_addrlen, void *conn_user_data)
{
return xqc_demo_cli_write_socket_ex(0, buf, size, peer_addr, peer_addrlen, conn_user_data);
}
#if defined(XQC_SUPPORT_SENDMMSG) && !defined(XQC_SYS_WINDOWS)
ssize_t
xqc_demo_cli_write_mmsg(void *conn_user_data, struct iovec *msg_iov, unsigned int vlen,
const struct sockaddr *peer_addr, socklen_t peer_addrlen)
{
const int MAX_SEG = 128;
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t *)conn_user_data;
ssize_t res = 0;
int fd = user_conn->paths[0].fd;
struct mmsghdr mmsg[MAX_SEG];
memset(&mmsg, 0, sizeof(mmsg));
for (int i = 0; i < vlen; i++) {
mmsg[i].msg_hdr.msg_iov = &msg_iov[i];
mmsg[i].msg_hdr.msg_iovlen = 1;
}
do {
errno = 0;
res = sendmmsg(fd, mmsg, vlen, 0);
if (res < 0) {
printf("sendmmsg err %zd %s\n", res, strerror(errno));
if (errno == EAGAIN) {
res = XQC_SOCKET_EAGAIN;
}
}
} while ((res < 0) && (get_sys_errno() == EINTR));
return res;
}
#endif
void
xqc_demo_cli_conn_update_cid_notify(xqc_connection_t *conn, const xqc_cid_t *retire_cid,
const xqc_cid_t *new_cid, void *user_data)
{
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t *)user_data;
memcpy(&user_conn->cid, new_cid, sizeof(*new_cid));
}
void
xqc_demo_cli_conn_create_path(const xqc_cid_t *cid, void *conn_user_data)
{
xqc_demo_cli_user_conn_t *user_conn = conn_user_data;
xqc_demo_cli_ctx_t *ctx = user_conn->ctx;
uint64_t path_id;
int ret;
int backup = 0;
printf("ready to create path notify\n");
if (user_conn->total_path_cnt < ctx->args->net_cfg.ifcnt
&& user_conn->total_path_cnt < ctx->args->quic_cfg.init_max_path_id)
{
if (user_conn->total_path_cnt == 1 && ctx->args->quic_cfg.mp_backup) {
backup = 1;
}
ret = xqc_conn_create_path(ctx->engine, &(user_conn->cid), &path_id, backup);
if (ret < 0) {
printf("not support mp, xqc_conn_create_path err = %d\n", ret);
return;
}
if (backup == 1) {
printf("Init No.%d path (id = %"PRIu64") to STANDBY state\n", 1, path_id);
}
ret = xqc_demo_cli_init_user_path(user_conn, user_conn->total_path_cnt, path_id);
if (ret < 0) {
xqc_conn_close_path(ctx->engine, &(user_conn->cid), path_id);
return;
}
user_conn->path_create_time = xqc_now();
if (user_conn->total_path_cnt == 2 && ctx->args->quic_cfg.mp_backup) {
printf("set No.%d path (id = %"PRIu64") to STANDBY state\n", 1, path_id);
xqc_conn_mark_path_standby(ctx->engine, &(user_conn->cid), path_id);
}
}
}
void
xqc_demo_cli_path_removed(const xqc_cid_t *scid, uint64_t path_id,
void *conn_user_data)
{
xqc_demo_cli_user_conn_t *user_conn = conn_user_data;
int i;
for (i = 0; i < user_conn->total_path_cnt; i++) {
if (user_conn->paths[i].is_active
&& user_conn->paths[i].path_id == path_id)
{
user_conn->paths[i].is_active = 0;
user_conn->active_path_cnt--;
/* remove event handle */
if (user_conn->paths[i].ev_socket) {
event_del(user_conn->paths[i].ev_socket);
}
event_del(user_conn->paths[i].ev_timeout);
/* close socket */
close(user_conn->paths[i].fd);
if (user_conn->paths[i].ev_rebind_socket) {
event_del(user_conn->paths[i].ev_rebind_socket);
}
if (user_conn->paths[i].rebind_fd != -1) {
close(user_conn->paths[i].rebind_fd);
}
printf("No.%d path removed id = %"PRIu64"\n", i, path_id);
}
}
}
/******************************************************************************
* start of hq callback functions *
******************************************************************************/
int
xqc_demo_cli_hq_conn_create_notify(xqc_hq_conn_t *hqc, const xqc_cid_t *cid, void *user_data)
{
DEBUG;
// printf("xqc_demo_cli_hq_conn_create_notify, conn: %p, user_conn: %p\n", conn, user_data);
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t *)user_data;
user_conn->hqc_handle = hqc;
memcpy(&user_conn->cid, cid, sizeof(xqc_cid_t));
return 0;
}
int
xqc_demo_cli_hq_conn_close_notify(xqc_hq_conn_t *hqc, const xqc_cid_t *cid, void *user_data)
{
DEBUG;
// printf("xqc_demo_cli_hq_conn_close_notify, conn: %p, user_conn: %p\n", conn, user_data);
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t *) user_data;
xqc_demo_cli_on_task_finish(user_conn->ctx, user_conn->task);
free(user_conn);
return 0;
}
void
xqc_demo_cli_hq_conn_handshake_finished(xqc_hq_conn_t *hqc, void *conn_user_data)
{
DEBUG;
xqc_demo_cli_user_conn_t *user_conn = (xqc_demo_cli_user_conn_t *)conn_user_data;
printf("hqc[%p] handshake finished\n", hqc);
}
int
xqc_demo_cli_hq_req_send(xqc_hq_request_t *hqr, xqc_demo_cli_user_stream_t *user_stream)
{
ssize_t ret = 0;
if (user_stream->start_time == 0) {
user_stream->start_time = xqc_now();
}
ret = xqc_hq_request_send_req(hqr, user_stream->send_buf);
if (ret < 0) {
switch (-ret) {
case XQC_EAGAIN:
return 0;
default:
printf("send stream failed, ret: %zd\n", ret);
return -1;
}
} else {
user_stream->send_offset += ret;
user_stream->send_body_len += ret;
}
return ret;
}
int