-
Notifications
You must be signed in to change notification settings - Fork 320
/
test_common.c
2427 lines (2233 loc) · 63.3 KB
/
test_common.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) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */
#if __GNUC__
#define _GNU_SOURCE /* For struct in6_pktinfo */
#endif
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#if defined(__APPLE__)
# define __APPLE_USE_RFC_3542 1
#endif
#ifndef WIN32
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#else
#include <Windows.h>
#include <WinSock2.h>
#include <MSWSock.h>
#include<io.h>
#pragma warning(disable:4996)//posix name deprecated
#define close closesocket
#endif
#include <sys/stat.h>
#include <sys/queue.h>
#include <fcntl.h>
#include "test_config.h"
#if HAVE_REGEX
#ifndef WIN32
#include <regex.h>
#else
#include <pcreposix.h>
#endif
#endif
#include <event2/event.h>
#include "lsquic.h"
#include "test_common.h"
#include "prog.h"
#include "lsxpack_header.h"
#include "../src/liblsquic/lsquic_logger.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#ifndef LSQUIC_USE_POOLS
#define LSQUIC_USE_POOLS 1
#endif
#if __linux__
# define NDROPPED_SZ CMSG_SPACE(sizeof(uint32_t)) /* SO_RXQ_OVFL */
#else
# define NDROPPED_SZ 0
#endif
#if __linux__ && defined(IP_RECVORIGDSTADDR)
# define DST_MSG_SZ sizeof(struct sockaddr_in)
#elif WIN32
# define DST_MSG_SZ sizeof(struct sockaddr_in)
#elif __linux__
# define DST_MSG_SZ sizeof(struct in_pktinfo)
#else
# define DST_MSG_SZ sizeof(struct sockaddr_in)
#endif
#if ECN_SUPPORTED
#define ECN_SZ CMSG_SPACE(sizeof(int))
#else
#define ECN_SZ 0
#endif
#define MAX_PACKET_SZ 0xffff
#define CTL_SZ (CMSG_SPACE(MAX(DST_MSG_SZ, \
sizeof(struct in6_pktinfo))) + NDROPPED_SZ + ECN_SZ)
/* There are `n_alloc' elements in `vecs', `local_addresses', and
* `peer_addresses' arrays. `ctlmsg_data' is n_alloc * CTL_SZ. Each packets
* gets a single `vecs' element that points somewhere into `packet_data'.
*
* `n_alloc' is calculated at run-time based on the socket's receive buffer
* size.
*/
struct packets_in
{
unsigned char *packet_data;
unsigned char *ctlmsg_data;
#ifndef WIN32
struct iovec *vecs;
#else
WSABUF *vecs;
#endif
#if ECN_SUPPORTED
int *ecn;
#endif
struct sockaddr_storage *local_addresses,
*peer_addresses;
unsigned n_alloc;
unsigned data_sz;
};
#if WIN32
LPFN_WSARECVMSG pfnWSARecvMsg;
GUID recvGuid = WSAID_WSARECVMSG;
LPFN_WSASENDMSG pfnWSASendMsg;
GUID sendGuid = WSAID_WSASENDMSG;
CRITICAL_SECTION initLock;
LONG initialized = 0;
static void getExtensionPtrs()
{
if (InterlockedCompareExchange(&initialized, 1, 0) == 0)
{
InitializeCriticalSection(&initLock);
}
EnterCriticalSection(&initLock);
if(pfnWSARecvMsg == NULL|| pfnWSASendMsg == NULL)
{
SOCKET sock= socket(PF_INET, SOCK_DGRAM, 0);
DWORD dwBytes;
int rc = 0;
if (pfnWSARecvMsg == NULL)
{
rc = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, &recvGuid,
sizeof(recvGuid), &pfnWSARecvMsg, sizeof(pfnWSARecvMsg),
&dwBytes, NULL, NULL);
}
if (rc != SOCKET_ERROR)
{
if (pfnWSASendMsg == NULL)
{
rc = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
&sendGuid, sizeof(sendGuid), &pfnWSASendMsg,
sizeof(pfnWSASendMsg), &dwBytes, NULL, NULL);
}
}
if (rc == SOCKET_ERROR)
{
LSQ_ERROR("Can't get extension function pointers: %d",
WSAGetLastError());
}
closesocket(sock);
}
LeaveCriticalSection(&initLock);
}
#endif
static struct packets_in *
allocate_packets_in (SOCKET_TYPE fd)
{
struct packets_in *packs_in;
unsigned n_alloc;
socklen_t opt_len;
int recvsz;
opt_len = sizeof(recvsz);
if (0 != getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&recvsz, &opt_len))
{
LSQ_ERROR("getsockopt failed: %s", strerror(errno));
return NULL;
}
n_alloc = (unsigned) recvsz / 1370;
LSQ_INFO("socket buffer size: %d bytes; max # packets is set to %u",
recvsz, n_alloc);
recvsz += MAX_PACKET_SZ;
packs_in = malloc(sizeof(*packs_in));
packs_in->data_sz = recvsz;
packs_in->n_alloc = n_alloc;
packs_in->packet_data = malloc(recvsz);
packs_in->ctlmsg_data = malloc(n_alloc * CTL_SZ);
packs_in->vecs = malloc(n_alloc * sizeof(packs_in->vecs[0]));
packs_in->local_addresses = malloc(n_alloc * sizeof(packs_in->local_addresses[0]));
packs_in->peer_addresses = malloc(n_alloc * sizeof(packs_in->peer_addresses[0]));
#if ECN_SUPPORTED
packs_in->ecn = malloc(n_alloc * sizeof(packs_in->ecn[0]));
#endif
return packs_in;
}
static void
free_packets_in (struct packets_in *packs_in)
{
#if ECN_SUPPORTED
free(packs_in->ecn);
#endif
free(packs_in->peer_addresses);
free(packs_in->local_addresses);
free(packs_in->ctlmsg_data);
free(packs_in->vecs);
free(packs_in->packet_data);
free(packs_in);
}
void
sport_destroy (struct service_port *sport)
{
if (sport->ev)
{
event_del(sport->ev);
event_free(sport->ev);
}
if (sport->fd >= 0)
(void) CLOSE_SOCKET(sport->fd);
if (sport->packs_in)
free_packets_in(sport->packs_in);
free(sport->sp_token_buf);
free(sport);
}
struct service_port *
sport_new (const char *optarg, struct prog *prog)
{
struct service_port *const sport = calloc(1, sizeof(*sport));
#if HAVE_REGEX
regex_t re;
regmatch_t matches[5];
int re_code;
const char *port_str;
char errbuf[80];
#else
char *port_str;
#endif
int port, e;
const char *host;
struct addrinfo hints, *res = NULL;
#if __linux__
sport->n_dropped = 0;
sport->drop_init = 0;
#endif
sport->ev = NULL;
sport->packs_in = NULL;
sport->fd = -1;
char *const addr = strdup(optarg);
#if __linux__
char *if_name;
if_name = strrchr(addr, ',');
if (if_name)
{
strncpy(sport->if_name, if_name + 1, sizeof(sport->if_name) - 1);
sport->if_name[ sizeof(sport->if_name) - 1 ] = '\0';
*if_name = '\0';
}
else
sport->if_name[0] = '\0';
#endif
#if HAVE_REGEX
re_code = regcomp(&re, "^(.*):([0-9][0-9]*)$"
"|^([0-9][0-9]*)$"
"|^(..*)$"
, REG_EXTENDED);
if (re_code != 0)
{
regerror(re_code, &re, errbuf, sizeof(errbuf));
LSQ_ERROR("cannot compile regex: %s", errbuf);
goto err;
}
if (0 != regexec(&re, addr, sizeof(matches) / sizeof(matches[0]),
matches, 0))
{
LSQ_ERROR("Invalid argument `%s'", addr);
goto err;
}
if (matches[1].rm_so >= 0)
{
addr[ matches[1].rm_so + matches[1].rm_eo ] = '\0';
host = addr;
port_str = &addr[ matches[2].rm_so ];
port = atoi(port_str);
}
else if (matches[3].rm_so >= 0)
{
if (!prog->prog_hostname)
{
LSQ_ERROR("hostname is not specified");
goto err;
}
host = prog->prog_hostname;
port_str = &addr[ matches[3].rm_so ];
port = atoi(port_str);
}
else
{
assert(matches[4].rm_so >= 0);
host = addr;
port_str = "443";
port = 443;
}
#else
host = addr;
port_str = strrchr(addr, ':');
if (port_str)
{
*port_str++ = '\0';
port = atoi(port_str);
}
else
{
port_str = "443";
port = 443;
}
#endif
assert(host);
LSQ_DEBUG("host: %s; port: %d", host, port);
if (strlen(host) > sizeof(sport->host) - 1)
{
LSQ_ERROR("argument `%s' too long", host);
goto err;
}
strcpy(sport->host, host);
struct sockaddr_in *const sa4 = (void *) &sport->sas;
struct sockaddr_in6 *const sa6 = (void *) &sport->sas;
if (inet_pton(AF_INET, host, &sa4->sin_addr)) {
sa4->sin_family = AF_INET;
sa4->sin_port = htons(port);
} else if (memset(sa6, 0, sizeof(*sa6)),
inet_pton(AF_INET6, host, &sa6->sin6_addr)) {
sa6->sin6_family = AF_INET6;
sa6->sin6_port = htons(port);
} else
{
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICSERV;
if (prog->prog_ipver == 4)
hints.ai_family = AF_INET;
else if (prog->prog_ipver == 6)
hints.ai_family = AF_INET6;
e = getaddrinfo(host, port_str, &hints, &res);
if (e != 0)
{
LSQ_ERROR("could not resolve %s:%s: %s", host, port_str,
gai_strerror(e));
goto err;
}
if (res->ai_addrlen > sizeof(sport->sas))
{
LSQ_ERROR("resolved socket length is too long");
goto err;
}
memcpy(&sport->sas, res->ai_addr, res->ai_addrlen);
if (!prog->prog_hostname)
prog->prog_hostname = sport->host;
}
#if HAVE_REGEX
if (0 == re_code)
regfree(&re);
#endif
if (res)
freeaddrinfo(res);
free(addr);
sport->sp_prog = prog;
return sport;
err:
#if HAVE_REGEX
if (0 == re_code)
regfree(&re);
#endif
if (res)
freeaddrinfo(res);
free(sport);
free(addr);
return NULL;
}
/* Replace IP address part of `sa' with that provided in ancillary messages
* in `msg'.
*/
static void
proc_ancillary (
#ifndef WIN32
struct msghdr
#else
WSAMSG
#endif
*msg, struct sockaddr_storage *storage
#if __linux__
, uint32_t *n_dropped
#endif
#if ECN_SUPPORTED
, int *ecn
#endif
)
{
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 __linux__ && defined(IP_RECVORIGDSTADDR)
IP_ORIGDSTADDR
#elif __linux__ || WIN32 || __APPLE__
IP_PKTINFO
#else
IP_RECVDSTADDR
#endif
)
{
#if __linux__ && defined(IP_RECVORIGDSTADDR)
memcpy(storage, CMSG_DATA(cmsg), sizeof(struct sockaddr_in));
#elif WIN32
const struct in_pktinfo *in_pkt;
in_pkt = (void *) WSA_CMSG_DATA(cmsg);
((struct sockaddr_in *) storage)->sin_addr = in_pkt->ipi_addr;
#elif __linux__ || __APPLE__
const struct in_pktinfo *in_pkt;
in_pkt = (void *) CMSG_DATA(cmsg);
((struct sockaddr_in *) storage)->sin_addr = in_pkt->ipi_addr;
#else
memcpy(&((struct sockaddr_in *) storage)->sin_addr,
CMSG_DATA(cmsg), sizeof(struct in_addr));
#endif
}
else if (cmsg->cmsg_level == IPPROTO_IPV6 &&
cmsg->cmsg_type == IPV6_PKTINFO)
{
#ifndef WIN32
in6_pkt = (void *) CMSG_DATA(cmsg);
#else
in6_pkt = (void *) WSA_CMSG_DATA(cmsg);
#endif
((struct sockaddr_in6 *) storage)->sin6_addr =
in6_pkt->ipi6_addr;
}
#if __linux__
else if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SO_RXQ_OVFL)
memcpy(n_dropped, CMSG_DATA(cmsg), sizeof(*n_dropped));
#endif
#if ECN_SUPPORTED
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;
}
#ifdef __FreeBSD__
else if (cmsg->cmsg_level == IPPROTO_IP
&& cmsg->cmsg_type == IP_RECVTOS)
{
unsigned char tos;
memcpy(&tos, CMSG_DATA(cmsg), sizeof(tos));
*ecn = tos & IPTOS_ECN_MASK;
}
#endif
#endif
}
}
struct read_iter
{
struct service_port *ri_sport;
unsigned ri_idx; /* Current element */
unsigned ri_off; /* Offset into packet_data */
};
enum rop { ROP_OK, ROP_NOROOM, ROP_ERROR, };
static enum rop
read_one_packet (struct read_iter *iter)
{
unsigned char *ctl_buf;
struct packets_in *packs_in;
#if __linux__
uint32_t n_dropped;
#endif
#ifndef WIN32
ssize_t nread;
#else
DWORD nread;
int socket_ret;
#endif
struct sockaddr_storage *local_addr;
struct service_port *sport;
sport = iter->ri_sport;
packs_in = sport->packs_in;
if (iter->ri_idx >= packs_in->n_alloc ||
iter->ri_off + MAX_PACKET_SZ > packs_in->data_sz)
{
LSQ_DEBUG("out of room in packets_in");
return ROP_NOROOM;
}
#ifndef WIN32
packs_in->vecs[iter->ri_idx].iov_base = packs_in->packet_data + iter->ri_off;
packs_in->vecs[iter->ri_idx].iov_len = MAX_PACKET_SZ;
#else
packs_in->vecs[iter->ri_idx].buf = (char*)packs_in->packet_data + iter->ri_off;
packs_in->vecs[iter->ri_idx].len = MAX_PACKET_SZ;
#endif
#ifndef WIN32
top:
#endif
ctl_buf = packs_in->ctlmsg_data + iter->ri_idx * CTL_SZ;
#ifndef WIN32
struct msghdr msg = {
.msg_name = &packs_in->peer_addresses[iter->ri_idx],
.msg_namelen = sizeof(packs_in->peer_addresses[iter->ri_idx]),
.msg_iov = &packs_in->vecs[iter->ri_idx],
.msg_iovlen = 1,
.msg_control = ctl_buf,
.msg_controllen = CTL_SZ,
};
nread = recvmsg(sport->fd, &msg, 0);
if (-1 == nread) {
if (!(EAGAIN == errno || EWOULDBLOCK == errno))
LSQ_ERROR("recvmsg: %s", strerror(errno));
return ROP_ERROR;
}
if (msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC))
{
if (msg.msg_flags & MSG_TRUNC)
LSQ_INFO("packet truncated - drop it");
if (msg.msg_flags & MSG_CTRUNC)
LSQ_WARN("packet's auxilicary data truncated - drop it");
goto top;
}
#else
WSAMSG msg = {
.name = (LPSOCKADDR)&packs_in->peer_addresses[iter->ri_idx],
.namelen = sizeof(packs_in->peer_addresses[iter->ri_idx]),
.lpBuffers = &packs_in->vecs[iter->ri_idx],
.dwBufferCount = 1,
.Control = {CTL_SZ,(char*)ctl_buf}
};
socket_ret = pfnWSARecvMsg(sport->fd, &msg, &nread, NULL, NULL);
if (SOCKET_ERROR == socket_ret) {
if (WSAEWOULDBLOCK != WSAGetLastError())
LSQ_ERROR("recvmsg: %d", WSAGetLastError());
return ROP_ERROR;
}
#endif
local_addr = &packs_in->local_addresses[iter->ri_idx];
memcpy(local_addr, &sport->sp_local_addr, sizeof(*local_addr));
#if __linux__
n_dropped = 0;
#endif
#if ECN_SUPPORTED
packs_in->ecn[iter->ri_idx] = 0;
#endif
proc_ancillary(&msg, local_addr
#if __linux__
, &n_dropped
#endif
#if ECN_SUPPORTED
, &packs_in->ecn[iter->ri_idx]
#endif
);
#if LSQUIC_ECN_BLACK_HOLE && ECN_SUPPORTED
{
const char *s;
s = getenv("LSQUIC_ECN_BLACK_HOLE");
if (s && atoi(s) && packs_in->ecn[iter->ri_idx])
{
LSQ_NOTICE("ECN blackhole: drop packet");
return ROP_OK;
}
}
#endif
#if __linux__
if (sport->drop_init)
{
if (sport->n_dropped < n_dropped)
LSQ_INFO("dropped %u packets", n_dropped - sport->n_dropped);
}
else
sport->drop_init = 1;
sport->n_dropped = n_dropped;
#endif
#ifndef WIN32
packs_in->vecs[iter->ri_idx].iov_len = nread;
#else
packs_in->vecs[iter->ri_idx].len = nread;
#endif
iter->ri_off += nread;
iter->ri_idx += 1;
return ROP_OK;
}
#if HAVE_RECVMMSG
static enum rop
read_using_recvmmsg (struct read_iter *iter)
{
#if __linux__
uint32_t n_dropped;
#endif
int s;
unsigned n;
struct sockaddr_storage *local_addr;
struct service_port *const sport = iter->ri_sport;
struct packets_in *const packs_in = sport->packs_in;
/* XXX TODO We allocate this array on the stack and initialize the
* headers each time the function is invoked. This is suboptimal.
* What we should really be doing is allocate mmsghdrs as part of
* packs_in and initialize it there. While we are at it, we should
* make packs_in shared between all service ports.
*/
struct mmsghdr mmsghdrs[ packs_in->n_alloc ];
/* Sanity check: we assume that the iterator is reset */
assert(iter->ri_off == 0 && iter->ri_idx == 0);
/* Initialize mmsghdrs */
for (n = 0; n < sizeof(mmsghdrs) / sizeof(mmsghdrs[0]); ++n)
{
packs_in->vecs[n].iov_base = packs_in->packet_data + MAX_PACKET_SZ * n;
packs_in->vecs[n].iov_len = MAX_PACKET_SZ;
mmsghdrs[n].msg_hdr = (struct msghdr) {
.msg_name = &packs_in->peer_addresses[n],
.msg_namelen = sizeof(packs_in->peer_addresses[n]),
.msg_iov = &packs_in->vecs[n],
.msg_iovlen = 1,
.msg_control = packs_in->ctlmsg_data + CTL_SZ * n,
.msg_controllen = CTL_SZ,
};
}
/* Read packets */
s = recvmmsg(sport->fd, mmsghdrs, n, 0, NULL);
if (s < 0)
{
if (!(EAGAIN == errno || EWOULDBLOCK == errno))
LSQ_ERROR("recvmmsg: %s", strerror(errno));
return ROP_ERROR;
}
/* Process ancillary data and update vecs */
for (n = 0; n < (unsigned) s; ++n)
{
local_addr = &packs_in->local_addresses[n];
memcpy(local_addr, &sport->sp_local_addr, sizeof(*local_addr));
#if __linux__
n_dropped = 0;
#endif
#if ECN_SUPPORTED
packs_in->ecn[n] = 0;
#endif
proc_ancillary(&mmsghdrs[n].msg_hdr, local_addr
#if __linux__
, &n_dropped
#endif
#if ECN_SUPPORTED
, &packs_in->ecn[n]
#endif
);
#if __linux__
if (sport->drop_init)
{
if (sport->n_dropped < n_dropped)
LSQ_INFO("dropped %u packets", n_dropped - sport->n_dropped);
}
else
sport->drop_init = 1;
sport->n_dropped = n_dropped;
#endif
packs_in->vecs[n].iov_len = mmsghdrs[n].msg_len;
}
iter->ri_idx = n;
return n == sizeof(mmsghdrs) / sizeof(mmsghdrs[0]) ? ROP_NOROOM : ROP_OK;
}
#endif
#if __GNUC__
# define UNLIKELY(cond) __builtin_expect(cond, 0)
#else
# define UNLIKELY(cond) cond
#endif
static void
read_handler (evutil_socket_t fd, short flags, void *ctx)
{
struct service_port *sport = ctx;
lsquic_engine_t *const engine = sport->engine;
struct packets_in *packs_in = sport->packs_in;
struct read_iter iter;
unsigned n, n_batches;
/* Save the value in case program is stopped packs_in is freed: */
const unsigned n_alloc = packs_in->n_alloc;
enum rop rop;
n_batches = 0;
iter.ri_sport = sport;
sport->sp_prog->prog_read_count += 1;
do
{
iter.ri_off = 0;
iter.ri_idx = 0;
#if HAVE_RECVMMSG
if (sport->sp_prog->prog_use_recvmmsg)
rop = read_using_recvmmsg(&iter);
else
#endif
do
rop = read_one_packet(&iter);
while (ROP_OK == rop);
if (UNLIKELY(ROP_ERROR == rop && (sport->sp_flags & SPORT_CONNECT)
&& errno == ECONNREFUSED))
{
LSQ_ERROR("connection refused: exit program");
prog_cleanup(sport->sp_prog);
exit(1);
}
n_batches += iter.ri_idx > 0;
for (n = 0; n < iter.ri_idx; ++n)
if (0 > lsquic_engine_packet_in(engine,
#ifndef WIN32
packs_in->vecs[n].iov_base,
packs_in->vecs[n].iov_len,
#else
(const unsigned char *) packs_in->vecs[n].buf,
packs_in->vecs[n].len,
#endif
(struct sockaddr *) &packs_in->local_addresses[n],
(struct sockaddr *) &packs_in->peer_addresses[n],
sport,
#if ECN_SUPPORTED
packs_in->ecn[n]
#else
0
#endif
))
break;
if (n > 0)
prog_process_conns(sport->sp_prog);
}
while (ROP_NOROOM == rop && !prog_is_stopped());
if (n_batches)
n += n_alloc * (n_batches - 1);
LSQ_DEBUG("read %u packet%.*s in %u batch%s", n, n != 1, "s", n_batches, n_batches != 1 ? "es" : "");
}
static int
add_to_event_loop (struct service_port *sport, struct event_base *eb)
{
sport->ev = event_new(eb, sport->fd, EV_READ|EV_PERSIST, read_handler,
sport);
if (sport->ev)
{
event_add(sport->ev, NULL);
return 0;
}
else
return -1;
}
int
sport_init_server (struct service_port *sport, struct lsquic_engine *engine,
struct event_base *eb)
{
const struct sockaddr *sa_local = (struct sockaddr *) &sport->sas;
int sockfd, saved_errno, s;
#ifndef WIN32
int flags;
#endif
SOCKOPT_VAL on = 1;
socklen_t socklen;
char addr_str[0x20];
switch (sa_local->sa_family)
{
case AF_INET:
socklen = sizeof(struct sockaddr_in);
break;
case AF_INET6:
socklen = sizeof(struct sockaddr_in6);
break;
default:
errno = EINVAL;
return -1;
}
#if WIN32
getExtensionPtrs();
#endif
sockfd = socket(sa_local->sa_family, SOCK_DGRAM, 0);
if (-1 == sockfd)
return -1;
if (AF_INET6 == sa_local->sa_family
&& setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
CHAR_CAST &on, sizeof(on)) == -1)
{
close(sockfd);
return -1;
}
if (0 != bind(sockfd, sa_local, socklen)) {
saved_errno = errno;
LSQ_WARN("bind failed: %s", strerror(errno));
close(sockfd);
errno = saved_errno;
return -1;
}
/* Make socket non-blocking */
#ifndef WIN32
flags = fcntl(sockfd, F_GETFL);
if (-1 == flags) {
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
flags |= O_NONBLOCK;
if (0 != fcntl(sockfd, F_SETFL, flags)) {
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
#else
{
u_long on = 1;
ioctlsocket(sockfd, FIONBIO, &on);
}
#endif
on = 1;
if (AF_INET == sa_local->sa_family)
s = setsockopt(sockfd, IPPROTO_IP,
#if __linux__ && defined(IP_RECVORIGDSTADDR)
IP_RECVORIGDSTADDR,
#elif __linux__ || __APPLE__ || defined(WIN32)
IP_PKTINFO,
#else
IP_RECVDSTADDR,
#endif
CHAR_CAST &on, sizeof(on));
else
{
#ifndef WIN32
s = setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on));
#else
s = setsockopt(sockfd, IPPROTO_IPV6, IPV6_PKTINFO, CHAR_CAST &on, sizeof(on));
#endif
}
if (0 != s)
{
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
#if (__linux__ && !defined(IP_RECVORIGDSTADDR)) || __APPLE__ || defined(WIN32)
/* Need to set IP_PKTINFO for sending */
if (AF_INET == sa_local->sa_family)
{
on = 1;
s = setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, CHAR_CAST &on, sizeof(on));
if (0 != s)
{
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
}
#elif IP_RECVDSTADDR != IP_SENDSRCADDR
/* On FreeBSD, IP_RECVDSTADDR is the same as IP_SENDSRCADDR, but I do not
* know about other BSD systems.
*/
if (AF_INET == sa_local->sa_family)
{
on = 1;
s = setsockopt(sockfd, IPPROTO_IP, IP_SENDSRCADDR, &on, sizeof(on));
if (0 != s)
{
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
}
#endif
#if __linux__ && defined(SO_RXQ_OVFL)
on = 1;
s = setsockopt(sockfd, SOL_SOCKET, SO_RXQ_OVFL, &on, sizeof(on));
if (0 != s)
{
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
#endif
#if __linux__
if (sport->if_name[0] &&
0 != setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, sport->if_name,
IFNAMSIZ))
{
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
#endif
#if LSQUIC_DONTFRAG_SUPPORTED
if (!(sport->sp_flags & SPORT_FRAGMENT_OK))
{
if (AF_INET == sa_local->sa_family)
{
#if __linux__
on = IP_PMTUDISC_PROBE;
s = setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, &on,
sizeof(on));
#else
on = 1;
s = setsockopt(sockfd, IPPROTO_IP, IP_DONTFRAG, CHAR_CAST &on, sizeof(on));
#endif
if (0 != s)
{
saved_errno = errno;
close(sockfd);
errno = saved_errno;
return -1;
}
}
#if __linux__
else if (AF_INET6 == sa_local->sa_family)
{
int on = IP_PMTUDISC_PROBE;
s = setsockopt(sockfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &on, sizeof(on));
}
#endif
}
#endif
#if ECN_SUPPORTED
on = 1;
if (AF_INET == sa_local->sa_family)
{
s = setsockopt(sockfd, IPPROTO_IP, IP_RECVTOS,
CHAR_CAST &on, sizeof(on));
if (!s)
s = setsockopt(sockfd, IPPROTO_IP, IP_TOS,
CHAR_CAST &on, sizeof(on));
}
else
{
s = setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVTCLASS,
CHAR_CAST &on, sizeof(on));
if (!s)
s = setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS,