-
Notifications
You must be signed in to change notification settings - Fork 11
/
tut.c
1418 lines (1236 loc) · 39 KB
/
tut.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 (c) 2020 LiteSpeed Technologies */
/*
* tut.c is the example program to illustrate lsquic API usage.
*/
#define _GNU_SOURCE /* For struct in6_pktinfo */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/ssl.h>
#define EV_STANDALONE 1
#define EV_API_STATIC 1
#include "ev.c"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#include "lsquic.h"
static FILE *s_log_fh;
struct tut
{
/* Common elements needed by both client and server: */
enum {
TUT_SERVER = 1 << 0,
} tut_flags;
int tut_sock_fd; /* socket */
ev_io tut_sock_w; /* socket watcher */
ev_timer tut_timer;
struct ev_loop *tut_loop;
lsquic_engine_t *tut_engine;
struct sockaddr_storage tut_local_sas;
union
{
struct client
{
ev_io stdin_w; /* stdin watcher */
struct lsquic_conn *conn;
size_t sz; /* Size of bytes read is stored here */
char buf[0x100]; /* Read up to this many bytes */
} c;
} tut_u;
};
static void tut_process_conns (struct tut *);
static int
tut_log_buf (void *ctx, const char *buf, size_t len)
{
FILE *out = ctx;
fwrite(buf, 1, len, out);
fflush(out);
return 0;
}
static const struct lsquic_logger_if logger_if = { tut_log_buf, };
static int s_verbose;
static void
LOG (const char *fmt, ...)
{
if (s_verbose)
{
va_list ap;
fprintf(s_log_fh, "LOG: ");
va_start(ap, fmt);
(void) vfprintf(s_log_fh, fmt, ap);
va_end(ap);
fprintf(s_log_fh, "\n");
}
}
static SSL_CTX *s_ssl_ctx;
static int
tut_load_cert (const char *cert_file, const char *key_file)
{
int rv = -1;
s_ssl_ctx = SSL_CTX_new(TLS_method());
if (!s_ssl_ctx)
{
LOG("SSL_CTX_new failed");
goto end;
}
SSL_CTX_set_min_proto_version(s_ssl_ctx, TLS1_3_VERSION);
SSL_CTX_set_max_proto_version(s_ssl_ctx, TLS1_3_VERSION);
SSL_CTX_set_default_verify_paths(s_ssl_ctx);
if (1 != SSL_CTX_use_certificate_chain_file(s_ssl_ctx, cert_file))
{
LOG("SSL_CTX_use_certificate_chain_file failed");
goto end;
}
if (1 != SSL_CTX_use_PrivateKey_file(s_ssl_ctx, key_file,
SSL_FILETYPE_PEM))
{
LOG("SSL_CTX_use_PrivateKey_file failed");
goto end;
}
rv = 0;
end:
if (rv != 0)
{
if (s_ssl_ctx)
SSL_CTX_free(s_ssl_ctx);
s_ssl_ctx = NULL;
}
return rv;
}
static SSL_CTX *
tut_get_ssl_ctx (void *peer_ctx)
{
return s_ssl_ctx;
}
enum ctl_what
{
CW_SENDADDR = 1 << 0,
CW_ECN = 1 << 1,
};
static void
tut_setup_control_msg (struct msghdr *msg, enum ctl_what cw,
const struct lsquic_out_spec *spec, unsigned char *buf, size_t bufsz)
{
struct cmsghdr *cmsg;
struct sockaddr_in *local_sa;
struct sockaddr_in6 *local_sa6;
struct in_pktinfo info;
struct in6_pktinfo info6;
size_t ctl_len;
msg->msg_control = buf;
msg->msg_controllen = bufsz;
/* Need to zero the buffer due to a bug(?) in CMSG_NXTHDR. See
* https://stackoverflow.com/questions/27601849/cmsg-nxthdr-returns-null-even-though-there-are-more-cmsghdr-objects
*/
memset(buf, 0, bufsz);
ctl_len = 0;
for (cmsg = CMSG_FIRSTHDR(msg); cw && cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
{
if (cw & CW_SENDADDR)
{
if (AF_INET == spec->dest_sa->sa_family)
{
local_sa = (struct sockaddr_in *) spec->local_sa;
memset(&info, 0, sizeof(info));
info.ipi_spec_dst = local_sa->sin_addr;
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(info));
ctl_len += CMSG_SPACE(sizeof(info));
memcpy(CMSG_DATA(cmsg), &info, sizeof(info));
}
else
{
local_sa6 = (struct sockaddr_in6 *) spec->local_sa;
memset(&info6, 0, sizeof(info6));
info6.ipi6_addr = local_sa6->sin6_addr;
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
cmsg->cmsg_len = CMSG_LEN(sizeof(info6));
memcpy(CMSG_DATA(cmsg), &info6, sizeof(info6));
ctl_len += CMSG_SPACE(sizeof(info6));
}
cw &= ~CW_SENDADDR;
}
else if (cw & CW_ECN)
{
if (AF_INET == spec->dest_sa->sa_family)
{
const int tos = spec->ecn;
cmsg->cmsg_level = IPPROTO_IP;
cmsg->cmsg_type = IP_TOS;
cmsg->cmsg_len = CMSG_LEN(sizeof(tos));
memcpy(CMSG_DATA(cmsg), &tos, sizeof(tos));
ctl_len += CMSG_SPACE(sizeof(tos));
}
else
{
const int tos = spec->ecn;
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_TCLASS;
cmsg->cmsg_len = CMSG_LEN(sizeof(tos));
memcpy(CMSG_DATA(cmsg), &tos, sizeof(tos));
ctl_len += CMSG_SPACE(sizeof(tos));
}
cw &= ~CW_ECN;
}
else
assert(0);
}
msg->msg_controllen = ctl_len;
}
/* A simple version of ea_packets_out -- does not use ancillary messages */
static int
tut_packets_out_v0 (void *packets_out_ctx, const struct lsquic_out_spec *specs,
unsigned count)
{
unsigned n;
int fd, s = 0;
struct msghdr msg;
if (0 == count)
return 0;
n = 0;
msg.msg_flags = 0;
msg.msg_control = NULL;
msg.msg_controllen = 0;
do
{
fd = (int) (uint64_t) specs[n].peer_ctx;
msg.msg_name = (void *) specs[n].dest_sa;
msg.msg_namelen = (AF_INET == specs[n].dest_sa->sa_family ?
sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6)),
msg.msg_iov = specs[n].iov;
msg.msg_iovlen = specs[n].iovlen;
s = sendmsg(fd, &msg, 0);
if (s < 0)
{
LOG("sendmsg failed: %s", strerror(errno));
break;
}
++n;
}
while (n < count);
if (n < count)
LOG("could not send all of them"); /* TODO */
if (n > 0)
return n;
else
{
assert(s < 0);
return -1;
}
}
/* A more complicated version of ea_packets_out -- this one sets source IP
* address and ECN.
*/
static int
tut_packets_out_v1 (void *packets_out_ctx, const struct lsquic_out_spec *specs,
unsigned count)
{
struct tut *const tut = packets_out_ctx;
unsigned n;
int fd, s = 0;
struct msghdr msg;
enum ctl_what cw;
union {
/* cmsg(3) recommends union for proper alignment */
unsigned char buf[
CMSG_SPACE(MAX(sizeof(struct in_pktinfo),
sizeof(struct in6_pktinfo))) + CMSG_SPACE(sizeof(int))
];
struct cmsghdr cmsg;
} ancil;
if (0 == count)
return 0;
n = 0;
msg.msg_flags = 0;
do
{
fd = (int) (uint64_t) specs[n].peer_ctx;
msg.msg_name = (void *) specs[n].dest_sa;
msg.msg_namelen = (AF_INET == specs[n].dest_sa->sa_family ?
sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6)),
msg.msg_iov = specs[n].iov;
msg.msg_iovlen = specs[n].iovlen;
/* Set up ancillary message */
if (tut->tut_flags & TUT_SERVER)
cw = CW_SENDADDR;
else
cw = 0;
if (specs[n].ecn)
cw |= CW_ECN;
if (cw)
tut_setup_control_msg(&msg, cw, &specs[n], ancil.buf,
sizeof(ancil.buf));
else
{
msg.msg_control = NULL;
msg.msg_controllen = 0;
}
s = sendmsg(fd, &msg, 0);
if (s < 0)
{
LOG("sendmsg failed: %s", strerror(errno));
break;
}
++n;
}
while (n < count);
if (n < count)
LOG("could not send all of them"); /* TODO */
if (n > 0)
return n;
else
{
assert(s < 0);
return -1;
}
}
static int (*const tut_packets_out[]) (void *packets_out_ctx,
const struct lsquic_out_spec *specs, unsigned count) =
{
tut_packets_out_v0,
tut_packets_out_v1,
};
static void
tut_usage (const char *argv0)
{
const char *name;
name = strchr(argv0, '/');
if (name)
++name;
else
name = argv0;
fprintf(stdout,
"Usage: %s [-c cert -k key] [options] IP port\n"
"\n"
" -c cert.file Certificate.\n"
" -k key.file Key file.\n"
" -f log.file Log message to this log file. If not specified, the\n"
" messages are printed to stderr.\n"
" -L level Set library-wide log level. Defaults to 'warn'.\n"
" -l module=level Set log level of specific module. Several of these\n"
" can be specified via multiple -l flags or by combining\n"
" these with comma, e.g. -l event=debug,conn=info.\n"
" -v Verbose: log program messages as well.\n"
" -b VERSION Use callbacks version VERSION.\n"
" -p VERSION Use packets_out version VERSION.\n"
" -w VERSION Use server write callback version VERSION.\n"
" -o opt=val Set lsquic engine setting to some value, overriding the\n"
" defaults. For example,\n"
" -o version=ff00001c -o cc_algo=2\n"
" -G DIR Log TLS secrets to a file in directory DIR.\n"
" -h Print this help screen and exit.\n"
, name);
}
static lsquic_conn_ctx_t *
tut_client_on_new_conn (void *stream_if_ctx, struct lsquic_conn *conn)
{
struct tut *const tut = stream_if_ctx;
tut->tut_u.c.conn = conn;
LOG("created connection");
return (void *) tut;
}
static void
tut_client_on_hsk_done (lsquic_conn_t *conn, enum lsquic_hsk_status status)
{
struct tut *const tut = (void *) lsquic_conn_get_ctx(conn);
switch (status)
{
case LSQ_HSK_OK:
case LSQ_HSK_RESUMED_OK:
LOG("handshake successful, start stdin watcher");
ev_io_start(tut->tut_loop, &tut->tut_u.c.stdin_w);
break;
default:
LOG("handshake failed");
break;
}
}
static void
tut_client_on_conn_closed (struct lsquic_conn *conn)
{
struct tut *const tut = (void *) lsquic_conn_get_ctx(conn);
LOG("client connection closed -- stop reading from socket");
ev_io_stop(tut->tut_loop, &tut->tut_sock_w);
}
static lsquic_stream_ctx_t *
tut_client_on_new_stream (void *stream_if_ctx, struct lsquic_stream *stream)
{
struct tut *tut = stream_if_ctx;
LOG("created new stream, we want to write");
lsquic_stream_wantwrite(stream, 1);
/* return tut: we don't have any stream-specific context */
return (void *) tut;
}
/* Echo whatever comes back from server, no verification */
static void
tut_client_on_read_v0 (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut *tut = (struct tut *) h;
ssize_t nread;
unsigned char buf[3];
nread = lsquic_stream_read(stream, buf, sizeof(buf));
if (nread > 0)
{
fwrite(buf, 1, nread, stdout);
fflush(stdout);
}
else if (nread == 0)
{
LOG("read to end-of-stream: close and read from stdin again");
lsquic_stream_shutdown(stream, 0);
ev_io_start(tut->tut_loop, &tut->tut_u.c.stdin_w);
}
else
{
LOG("error reading from stream (%s) -- exit loop");
ev_break(tut->tut_loop, EVBREAK_ONE);
}
}
static size_t
tut_client_readf_v1 (void *ctx, const unsigned char *data, size_t len, int fin)
{
if (len)
{
fwrite(data, 1, len, stdout);
fflush(stdout);
}
return len;
}
/* Same functionality as tut_client_on_read_v0(), but use a readf callback */
static void
tut_client_on_read_v1 (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut *tut = (struct tut *) h;
ssize_t nread;
nread = lsquic_stream_readf(stream, tut_client_readf_v1, NULL);
if (nread == 0)
{
LOG("read to end-of-stream: close and read from stdin again");
lsquic_stream_shutdown(stream, 0);
ev_io_start(tut->tut_loop, &tut->tut_u.c.stdin_w);
}
else if (nread < 0)
{
LOG("error reading from stream (%s) -- exit loop");
ev_break(tut->tut_loop, EVBREAK_ONE);
}
}
/* Alternatively, pass `stream' to lsquic_stream_readf() and call
* lsquic_stream_get_ctx() to get struct tut *
*/
struct client_read_v2_ctx {
struct tut *tut;
lsquic_stream_t *stream;
};
static size_t
tut_client_readf_v2 (void *ctx, const unsigned char *data, size_t len, int fin)
{
struct client_read_v2_ctx *v2ctx = ctx;
if (len)
fwrite(data, 1, len, stdout);
if (fin)
{
fflush(stdout);
LOG("read to end-of-stream: close and read from stdin again");
lsquic_stream_shutdown(v2ctx->stream, 0);
ev_io_start(v2ctx->tut->tut_loop, &v2ctx->tut->tut_u.c.stdin_w);
}
return len;
}
/* A bit different from v1: act on fin. This version saves an extra on_read()
* call at the cost of some complexity.
*/
static void
tut_client_on_read_v2 (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut *tut = (struct tut *) h;
ssize_t nread;
struct client_read_v2_ctx v2ctx = { tut, stream, };
nread = lsquic_stream_readf(stream, tut_client_readf_v2, &v2ctx);
if (nread < 0)
{
LOG("error reading from stream (%s) -- exit loop");
ev_break(tut->tut_loop, EVBREAK_ONE);
}
}
/* Write out the whole line to stream, shutdown write end, and switch
* to reading the response.
*/
static void
tut_client_on_write (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
lsquic_conn_t *conn;
struct tut *tut;
ssize_t nw;
conn = lsquic_stream_conn(stream);
tut = (void *) lsquic_conn_get_ctx(conn);
nw = lsquic_stream_write(stream, tut->tut_u.c.buf, tut->tut_u.c.sz);
if (nw > 0)
{
tut->tut_u.c.sz -= (size_t) nw;
if (tut->tut_u.c.sz == 0)
{
LOG("wrote all %zd bytes to stream, switch to reading",
(size_t) nw);
lsquic_stream_shutdown(stream, 1); /* This flushes as well */
lsquic_stream_wantread(stream, 1);
}
else
{
memmove(tut->tut_u.c.buf, tut->tut_u.c.buf + nw, tut->tut_u.c.sz);
LOG("wrote %zd bytes to stream, still have %zd bytes to write",
(size_t) nw, tut->tut_u.c.sz);
}
}
else
{
/* When `on_write()' is called, the library guarantees that at least
* something can be written. If not, that's an error whether 0 or -1
* is returned.
*/
LOG("stream_write() returned %ld, abort connection", (long) nw);
lsquic_conn_abort(lsquic_stream_conn(stream));
}
}
static void
tut_client_on_close (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
LOG("stream closed");
}
static void (*const tut_client_on_read[])
(lsquic_stream_t *, lsquic_stream_ctx_t *h) =
{
tut_client_on_read_v0,
tut_client_on_read_v1,
tut_client_on_read_v2,
};
static struct lsquic_stream_if tut_client_callbacks =
{
.on_new_conn = tut_client_on_new_conn,
.on_hsk_done = tut_client_on_hsk_done,
.on_conn_closed = tut_client_on_conn_closed,
.on_new_stream = tut_client_on_new_stream,
.on_read = tut_client_on_read_v0,
.on_write = tut_client_on_write,
.on_close = tut_client_on_close,
};
static lsquic_conn_ctx_t *
tut_server_on_new_conn (void *stream_if_ctx, struct lsquic_conn *conn)
{
struct tut *const tut = stream_if_ctx;
LOG("created new connection");
return (void *) tut; /* Pointer to tut is the connection context */
}
static void
tut_server_on_conn_closed (lsquic_conn_t *conn)
{
LOG("closed connection");
}
struct tut_server_stream_ctx
{
size_t tssc_sz; /* Number of bytes in tsc_buf */
off_t tssc_off; /* Number of bytes written to stream */
unsigned char tssc_buf[0x100]; /* Bytes read in from client */
};
static lsquic_stream_ctx_t *
tut_server_on_new_stream (void *stream_if_ctx, struct lsquic_stream *stream)
{
struct tut_server_stream_ctx *tssc;
/* Allocate a new buffer per stream. There is no reason why the echo
* server could not process several echo streams at the same time.
*/
tssc = malloc(sizeof(*tssc));
if (!tssc)
{
LOG("cannot allocate server stream context");
lsquic_conn_abort(lsquic_stream_conn(stream));
return NULL;
}
tssc->tssc_sz = 0;
tssc->tssc_off = 0;
lsquic_stream_wantread(stream, 1);
LOG("created new echo stream -- want to read");
return (void *) tssc;
}
static void
reverse_string (unsigned char *p, size_t len)
{
unsigned char *q, tmp;
q = p + len - 1;
while (p < q)
{
tmp = *p;
*p = *q;
*q = tmp;
++p;
--q;
}
}
/* Read until newline and then echo it back */
static void
tut_server_on_read (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut_server_stream_ctx *const tssc = (void *) h;
ssize_t nread;
unsigned char buf[1];
nread = lsquic_stream_read(stream, buf, sizeof(buf));
if (nread > 0)
{
tssc->tssc_buf[ tssc->tssc_sz ] = buf[0];
++tssc->tssc_sz;
if (buf[0] == (unsigned char) '\n'
|| tssc->tssc_sz == sizeof(tssc->tssc_buf))
{
LOG("read newline or filled buffer, switch to writing");
reverse_string(tssc->tssc_buf,
tssc->tssc_sz - (buf[0] == (unsigned char) '\n'));
lsquic_stream_wantread(stream, 0);
lsquic_stream_wantwrite(stream, 1);
}
}
else if (nread == 0)
{
LOG("read EOF");
lsquic_stream_shutdown(stream, 0);
if (tssc->tssc_sz)
lsquic_stream_wantwrite(stream, 1);
}
else
{
/* This should not happen */
LOG("error reading from stream (errno: %d) -- abort connection", errno);
lsquic_conn_abort(lsquic_stream_conn(stream));
}
}
static void
tut_server_on_write_v0 (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut_server_stream_ctx *const tssc = (void *) h;
ssize_t nw;
assert(tssc->tssc_sz > 0);
nw = lsquic_stream_write(stream, tssc->tssc_buf + tssc->tssc_off,
tssc->tssc_sz - tssc->tssc_off);
if (nw > 0)
{
tssc->tssc_off += nw;
if (tssc->tssc_off == tssc->tssc_sz)
{
LOG("wrote all %zd bytes to stream, close stream",
(size_t) nw);
lsquic_stream_close(stream);
}
else
LOG("wrote %zd bytes to stream, still have %zd bytes to write",
(size_t) nw, tssc->tssc_sz - tssc->tssc_off);
}
else
{
/* When `on_write()' is called, the library guarantees that at least
* something can be written. If not, that's an error whether 0 or -1
* is returned.
*/
LOG("stream_write() returned %ld, abort connection", (long) nw);
lsquic_conn_abort(lsquic_stream_conn(stream));
}
}
static size_t
tssc_read (void *ctx, void *buf, size_t count)
{
struct tut_server_stream_ctx *tssc = ctx;
if (count > tssc->tssc_sz - tssc->tssc_off)
count = tssc->tssc_sz - tssc->tssc_off;
memcpy(buf, tssc->tssc_buf + tssc->tssc_off, count);
tssc->tssc_off += count;
return count;
}
static size_t
tssc_size (void *ctx)
{
struct tut_server_stream_ctx *tssc = ctx;
return tssc->tssc_sz - tssc->tssc_off;
}
/* Same functionality as tut_server_on_write_v0(), but use the "reader"
* callbacks. This is most useful when data comes from a different source
* such as file descriptor.
*/
static void
tut_server_on_write_v1 (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut_server_stream_ctx *const tssc = (void *) h;
struct lsquic_reader reader = { tssc_read, tssc_size, tssc, };
const size_t left = tssc->tssc_sz;
ssize_t nw;
nw = lsquic_stream_writef(stream, &reader);
if (nw > 0 && tssc->tssc_off == tssc->tssc_sz)
{
LOG("wrote all %zd bytes to stream, close stream", left);
lsquic_stream_close(stream);
}
else if (nw < 0)
{
LOG("stream_write() returned %ld, abort connection", (long) nw);
lsquic_conn_abort(lsquic_stream_conn(stream));
}
}
static void
tut_server_on_close (struct lsquic_stream *stream, lsquic_stream_ctx_t *h)
{
struct tut_server_stream_ctx *const tssc = (void *) h;
free(tssc);
LOG("stream closed");
}
static void (*const tut_server_on_write[])(lsquic_stream_t *,
lsquic_stream_ctx_t *) =
{
tut_server_on_write_v0,
tut_server_on_write_v1,
};
static struct lsquic_stream_if tut_server_callbacks =
{
.on_new_conn = tut_server_on_new_conn,
.on_conn_closed = tut_server_on_conn_closed,
.on_new_stream = tut_server_on_new_stream,
.on_read = tut_server_on_read,
.on_write = tut_server_on_write_v0,
.on_close = tut_server_on_close,
};
/* Read one byte at a time -- when user hits enter, send line to server */
static void
tut_read_stdin (EV_P_ ev_io *w, int revents)
{
struct tut *const tut = w->data;
ssize_t nr;
assert(tut->tut_u.c.sz < sizeof(tut->tut_u.c.buf));
nr = read(w->fd, tut->tut_u.c.buf + tut->tut_u.c.sz, 1);
if (nr > 0)
{
tut->tut_u.c.sz += nr;
if (tut->tut_u.c.buf[tut->tut_u.c.sz - 1] == '\n'
|| sizeof(tut->tut_u.c.buf) == tut->tut_u.c.sz)
{
LOG("read up to newline (or filled buffer): make new stream");
lsquic_conn_make_stream(tut->tut_u.c.conn);
ev_io_stop(tut->tut_loop, w);
tut_process_conns(tut);
}
}
else if (nr == 0)
{
LOG("read EOF: stop reading from stdin, close connection");
ev_io_stop(tut->tut_loop, w);
ev_io_stop(tut->tut_loop, &tut->tut_u.c.stdin_w);
lsquic_conn_close(tut->tut_u.c.conn);
tut_process_conns(tut);
}
else
{
LOG("error reading from stdin: %s", strerror(errno));
ev_break(tut->tut_loop, EVBREAK_ONE);
}
}
static int
tut_set_nonblocking (int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
if (-1 == flags)
return -1;
flags |= O_NONBLOCK;
if (0 != fcntl(fd, F_SETFL, flags))
return -1;
return 0;
}
/* ToS is used to get ECN value */
static int
tut_set_ecn (int fd, const struct sockaddr *sa)
{
int on, s;
on = 1;
if (AF_INET == sa->sa_family)
s = setsockopt(fd, IPPROTO_IP, IP_RECVTOS, &on, sizeof(on));
else
s = setsockopt(fd, IPPROTO_IPV6, IPV6_RECVTCLASS, &on, sizeof(on));
if (s != 0)
perror("setsockopt(ecn)");
return s;
}
/* Set up the socket to return original destination address in ancillary data */
static int
tut_set_origdst (int fd, const struct sockaddr *sa)
{
int on, s;
on = 1;
if (AF_INET == sa->sa_family)
s = setsockopt(fd, IPPROTO_IP,
#if defined(IP_RECVORIGDSTADDR)
IP_RECVORIGDSTADDR,
#else
IP_PKTINFO,
#endif
&on, sizeof(on));
else
s = setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on));
if (s != 0)
perror("setsockopt");
return s;
}
static void
tut_timer_expired (EV_P_ ev_timer *timer, int revents)
{
tut_process_conns(timer->data);
}
static void
tut_process_conns (struct tut *tut)
{
int diff;
ev_tstamp timeout;
ev_timer_stop(tut->tut_loop, &tut->tut_timer);
lsquic_engine_process_conns(tut->tut_engine);
if (lsquic_engine_earliest_adv_tick(tut->tut_engine, &diff))
{
if (diff >= LSQUIC_DF_CLOCK_GRANULARITY)
/* Expected case: convert to seconds */
timeout = (ev_tstamp) diff / 1000000;
else if (diff <= 0)
/* It should not happen often that the next tick is in the past
* as we just processed connections. Avoid a busy loop by
* scheduling an event:
*/
timeout = 0.0;
else
/* Round up to granularity */
timeout = (ev_tstamp) LSQUIC_DF_CLOCK_GRANULARITY / 1000000;
LOG("converted diff %d usec to %.4lf seconds", diff, timeout);
ev_timer_init(&tut->tut_timer, tut_timer_expired, timeout, 0.);
ev_timer_start(tut->tut_loop, &tut->tut_timer);
}
}
static void
tut_proc_ancillary (struct msghdr *msg, struct sockaddr_storage *storage,
int *ecn)
{
const struct in6_pktinfo *in6_pkt;
struct cmsghdr *cmsg;
for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
{
if (cmsg->cmsg_level == IPPROTO_IP &&
cmsg->cmsg_type ==
#if defined(IP_RECVORIGDSTADDR)
IP_ORIGDSTADDR
#else
IP_PKTINFO
#endif
)
{
#if defined(IP_RECVORIGDSTADDR)
memcpy(storage, CMSG_DATA(cmsg), sizeof(struct sockaddr_in));
#else
const struct in_pktinfo *in_pkt;
in_pkt = (void *) CMSG_DATA(cmsg);
((struct sockaddr_in *) storage)->sin_addr = in_pkt->ipi_addr;
#endif
}
else if (cmsg->cmsg_level == IPPROTO_IPV6 &&
cmsg->cmsg_type == IPV6_PKTINFO)
{
in6_pkt = (void *) CMSG_DATA(cmsg);
((struct sockaddr_in6 *) storage)->sin6_addr =
in6_pkt->ipi6_addr;
}
else if ((cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_TOS)
|| (cmsg->cmsg_level == IPPROTO_IPV6
&& cmsg->cmsg_type == IPV6_TCLASS))
{
memcpy(ecn, CMSG_DATA(cmsg), sizeof(*ecn));
*ecn &= IPTOS_ECN_MASK;
}