-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathipsocket.c
More file actions
1523 lines (1317 loc) · 53.5 KB
/
ipsocket.c
File metadata and controls
1523 lines (1317 loc) · 53.5 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
/************************************************
ipsocket.c -
created at: Thu Mar 31 12:21:29 JST 1994
Copyright (C) 1993-2007 Yukihiro Matsumoto
************************************************/
#include "rubysocket.h"
#include <stdio.h>
struct inetsock_arg
{
VALUE self;
VALUE io;
struct {
VALUE host, serv;
struct rb_addrinfo *res;
} remote, local;
int type;
VALUE resolv_timeout;
VALUE connect_timeout;
};
static VALUE
inetsock_cleanup(VALUE v)
{
struct inetsock_arg *arg = (void *)v;
if (arg->remote.res) {
rb_freeaddrinfo(arg->remote.res);
arg->remote.res = 0;
}
if (arg->local.res) {
rb_freeaddrinfo(arg->local.res);
arg->local.res = 0;
}
if (arg->io != Qnil) {
rb_io_close(arg->io);
arg->io = Qnil;
}
return Qnil;
}
static VALUE
init_inetsock_internal(VALUE v)
{
struct inetsock_arg *arg = (void *)v;
int error = 0;
int type = arg->type;
struct addrinfo *res, *lres;
int status = 0, local = 0;
int family = AF_UNSPEC;
const char *syscall = 0;
VALUE connect_timeout = arg->connect_timeout;
arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv,
family, SOCK_STREAM,
(type == INET_SERVER) ? AI_PASSIVE : 0);
/*
* Maybe also accept a local address
*/
if (type != INET_SERVER && (!NIL_P(arg->local.host) || !NIL_P(arg->local.serv))) {
arg->local.res = rsock_addrinfo(arg->local.host, arg->local.serv,
family, SOCK_STREAM, 0);
}
VALUE io = Qnil;
for (res = arg->remote.res->ai; res; res = res->ai_next) {
#if !defined(INET6) && defined(AF_INET6)
if (res->ai_family == AF_INET6)
continue;
#endif
lres = NULL;
if (arg->local.res) {
for (lres = arg->local.res->ai; lres; lres = lres->ai_next) {
if (lres->ai_family == res->ai_family)
break;
}
if (!lres) {
if (res->ai_next || status < 0)
continue;
/* Use a different family local address if no choice, this
* will cause EAFNOSUPPORT. */
lres = arg->local.res->ai;
}
}
status = rsock_socket(res->ai_family,res->ai_socktype,res->ai_protocol);
syscall = "socket(2)";
if (status < 0) {
error = errno;
continue;
}
int fd = status;
io = arg->io = rsock_init_sock(arg->self, fd);
if (type == INET_SERVER) {
#if !defined(_WIN32) && !defined(__CYGWIN__)
status = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(char*)&status, (socklen_t)sizeof(status));
#endif
status = bind(fd, res->ai_addr, res->ai_addrlen);
syscall = "bind(2)";
}
else {
if (lres) {
#if !defined(_WIN32) && !defined(__CYGWIN__)
status = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(char*)&status, (socklen_t)sizeof(status));
#endif
status = bind(fd, lres->ai_addr, lres->ai_addrlen);
local = status;
syscall = "bind(2)";
}
if (status >= 0) {
status = rsock_connect(io, res->ai_addr, res->ai_addrlen, (type == INET_SOCKS), connect_timeout);
syscall = "connect(2)";
}
}
if (status < 0) {
error = errno;
arg->io = Qnil;
rb_io_close(io);
io = Qnil;
continue;
} else {
break;
}
}
if (status < 0) {
VALUE host, port;
if (local < 0) {
host = arg->local.host;
port = arg->local.serv;
} else {
host = arg->remote.host;
port = arg->remote.serv;
}
rsock_syserr_fail_host_port(error, syscall, host, port);
}
// Don't close the socket in `inetsock_cleanup` if we are returning it:
arg->io = Qnil;
if (type == INET_SERVER && io != Qnil) {
status = listen(rb_io_descriptor(io), SOMAXCONN);
if (status < 0) {
error = errno;
rb_io_close(io);
rb_syserr_fail(error, "listen(2)");
}
}
/* create new instance */
return io;
}
#if FAST_FALLBACK_INIT_INETSOCK_IMPL == 0
VALUE
rsock_init_inetsock(VALUE self, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type, VALUE resolv_timeout, VALUE connect_timeout, VALUE _fast_fallback, VALUE _test_mode_settings)
{
struct inetsock_arg arg;
arg.self = self;
arg.io = Qnil;
arg.remote.host = remote_host;
arg.remote.serv = remote_serv;
arg.remote.res = 0;
arg.local.host = local_host;
arg.local.serv = local_serv;
arg.local.res = 0;
arg.type = type;
arg.resolv_timeout = resolv_timeout;
arg.connect_timeout = connect_timeout;
return rb_ensure(init_inetsock_internal, (VALUE)&arg,
inetsock_cleanup, (VALUE)&arg);
}
#elif FAST_FALLBACK_INIT_INETSOCK_IMPL == 1
#define IPV6_ENTRY_POS 0
#define IPV4_ENTRY_POS 1
#define RESOLUTION_ERROR 0
#define SYSCALL_ERROR 1
static int
is_specified_ip_address(const char *hostname)
{
if (!hostname) return false;
struct in_addr ipv4addr;
struct in6_addr ipv6addr;
return (inet_pton(AF_INET6, hostname, &ipv6addr) == 1 ||
inet_pton(AF_INET, hostname, &ipv4addr) == 1);
}
struct fast_fallback_inetsock_arg
{
VALUE self;
VALUE io;
struct {
VALUE host, serv;
struct rb_addrinfo *res;
} remote, local;
int type;
VALUE resolv_timeout;
VALUE connect_timeout;
const char *hostp, *portp;
int *families;
int family_size;
int additional_flags;
struct fast_fallback_getaddrinfo_entry *getaddrinfo_entries[2];
struct fast_fallback_getaddrinfo_shared *getaddrinfo_shared;
rb_fdset_t readfds, writefds;
int wait;
int connection_attempt_fds_size;
int *connection_attempt_fds;
VALUE test_mode_settings;
};
static struct fast_fallback_getaddrinfo_shared *
allocate_fast_fallback_getaddrinfo_shared(int family_size)
{
struct fast_fallback_getaddrinfo_shared *shared;
shared = (struct fast_fallback_getaddrinfo_shared *)calloc(
1,
sizeof(struct fast_fallback_getaddrinfo_shared) + (family_size == 1 ? 0 : 2) * sizeof(struct fast_fallback_getaddrinfo_entry)
);
return shared;
}
static void
allocate_fast_fallback_getaddrinfo_hints(struct addrinfo *hints, int family, int remote_addrinfo_hints, int additional_flags)
{
MEMZERO(hints, struct addrinfo, 1);
hints->ai_family = family;
hints->ai_socktype = SOCK_STREAM;
hints->ai_protocol = IPPROTO_TCP;
hints->ai_flags = remote_addrinfo_hints;
hints->ai_flags |= additional_flags;
}
static int*
allocate_connection_attempt_fds(int additional_capacity)
{
int *fds = (int *)malloc(additional_capacity * sizeof(int));
if (!fds) rb_syserr_fail(errno, "malloc(3)");
for (int i = 0; i < additional_capacity; i++) fds[i] = -1;
return fds;
}
static int
reallocate_connection_attempt_fds(int **fds, int current_capacity, int additional_capacity)
{
int new_capacity = current_capacity + additional_capacity;
int *new_fds;
new_fds = realloc(*fds, new_capacity * sizeof(int));
if (new_fds == NULL) {
rb_syserr_fail(errno, "realloc(3)");
}
*fds = new_fds;
for (int i = current_capacity; i < new_capacity; i++) (*fds)[i] = -1;
return new_capacity;
}
struct hostname_resolution_result
{
struct addrinfo *ai;
int finished;
int has_error;
};
struct hostname_resolution_store
{
struct hostname_resolution_result v6;
struct hostname_resolution_result v4;
int is_all_finised;
};
static int
any_addrinfos(struct hostname_resolution_store *resolution_store)
{
return resolution_store->v6.ai || resolution_store->v4.ai;
}
static struct timespec
current_clocktime_ts(void)
{
struct timespec ts;
if ((clock_gettime(CLOCK_MONOTONIC, &ts)) < 0) {
rb_syserr_fail(errno, "clock_gettime(2)");
}
return ts;
}
static void
set_timeout_tv(struct timeval *tv, long ms, struct timespec from)
{
long sec = ms / 1000;
long nsec = (ms % 1000) * 1000000;
long result_sec = from.tv_sec + sec;
long result_nsec = from.tv_nsec + nsec;
result_sec += result_nsec / 1000000000;
result_nsec = result_nsec % 1000000000;
tv->tv_sec = result_sec;
tv->tv_usec = (int)(result_nsec / 1000);
}
static struct timeval
add_ts_to_tv(struct timeval tv, struct timespec ts)
{
long ts_usec = ts.tv_nsec / 1000;
tv.tv_sec += ts.tv_sec;
tv.tv_usec += ts_usec;
if (tv.tv_usec >= 1000000) {
tv.tv_sec += tv.tv_usec / 1000000;
tv.tv_usec = tv.tv_usec % 1000000;
}
return tv;
}
static VALUE
tv_to_seconds(struct timeval *timeout) {
if (timeout == NULL) return Qnil;
double seconds = (double)timeout->tv_sec + (double)timeout->tv_usec / 1000000.0;
return DBL2NUM(seconds);
}
static int
is_infinity(struct timeval tv)
{
// { -1, -1 } as infinity
return tv.tv_sec == -1 || tv.tv_usec == -1;
}
static int
is_timeout_tv(struct timeval *timeout_tv, struct timespec now) {
if (!timeout_tv) return false;
if (timeout_tv->tv_sec == -1 && timeout_tv->tv_usec == -1) return false;
struct timespec ts;
ts.tv_sec = timeout_tv->tv_sec;
ts.tv_nsec = timeout_tv->tv_usec * 1000;
if (now.tv_sec > ts.tv_sec) return true;
if (now.tv_sec == ts.tv_sec && now.tv_nsec >= ts.tv_nsec) return true;
return false;
}
static struct timeval *
select_expires_at(
struct hostname_resolution_store *resolution_store,
struct timeval *resolution_delay,
struct timeval *connection_attempt_delay,
struct timeval *user_specified_resolv_timeout_at,
struct timeval *user_specified_connect_timeout_at
) {
if (any_addrinfos(resolution_store)) {
return resolution_delay ? resolution_delay : connection_attempt_delay;
}
struct timeval *timeout = NULL;
if (user_specified_resolv_timeout_at) {
if (is_infinity(*user_specified_resolv_timeout_at)) return NULL;
timeout = user_specified_resolv_timeout_at;
}
if (user_specified_connect_timeout_at) {
if (is_infinity(*user_specified_connect_timeout_at)) return NULL;
if (!timeout || timercmp(user_specified_connect_timeout_at, timeout, >)) {
return user_specified_connect_timeout_at;
}
}
return timeout;
}
static struct timeval
tv_to_timeout(struct timeval *ends_at, struct timespec now)
{
struct timeval delay;
struct timespec expires_at;
expires_at.tv_sec = ends_at->tv_sec;
expires_at.tv_nsec = ends_at->tv_usec * 1000;
struct timespec diff;
diff.tv_sec = expires_at.tv_sec - now.tv_sec;
if (expires_at.tv_nsec >= now.tv_nsec) {
diff.tv_nsec = expires_at.tv_nsec - now.tv_nsec;
} else {
diff.tv_sec -= 1;
diff.tv_nsec = (1000000000 + expires_at.tv_nsec) - now.tv_nsec;
}
delay.tv_sec = diff.tv_sec;
delay.tv_usec = (int)diff.tv_nsec / 1000;
return delay;
}
static struct addrinfo *
pick_addrinfo(struct hostname_resolution_store *resolution_store, int last_family)
{
int priority_on_v6[2] = { AF_INET6, AF_INET };
int priority_on_v4[2] = { AF_INET, AF_INET6 };
int *precedences = last_family == AF_INET6 ? priority_on_v4 : priority_on_v6;
struct addrinfo *selected_ai = NULL;
for (int i = 0; i < 2; i++) {
if (precedences[i] == AF_INET6) {
selected_ai = resolution_store->v6.ai;
if (selected_ai) {
resolution_store->v6.ai = selected_ai->ai_next;
break;
}
} else {
selected_ai = resolution_store->v4.ai;
if (selected_ai) {
resolution_store->v4.ai = selected_ai->ai_next;
break;
}
}
}
return selected_ai;
}
static void
socket_nonblock_set(int fd)
{
int flags = fcntl(fd, F_GETFL);
if (flags < 0) rb_syserr_fail(errno, "fcntl(2)");
if ((flags & O_NONBLOCK) != 0) return;
flags |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, flags) < 0) rb_syserr_fail(errno, "fcntl(2)");
return;
}
static int
in_progress_fds(int fds_size)
{
return fds_size > 0;
}
static void
remove_connection_attempt_fd(int *fds, int *fds_size, int removing_fd) {
int i, j;
for (i = 0; i < *fds_size; i++) {
if (fds[i] != removing_fd) continue;
for (j = i; j < *fds_size - 1; j++) {
fds[j] = fds[j + 1];
}
(*fds_size)--;
fds[*fds_size] = -1;
break;
}
}
struct fast_fallback_error
{
int type;
int ecode;
};
static VALUE
init_fast_fallback_inetsock_internal(VALUE v)
{
struct fast_fallback_inetsock_arg *arg = (void *)v;
VALUE io = arg->io;
VALUE resolv_timeout = arg->resolv_timeout;
VALUE connect_timeout = arg->connect_timeout;
VALUE test_mode_settings = arg->test_mode_settings;
struct addrinfo *remote_ai = NULL, *local_ai = NULL;
int connected_fd = -1, status = 0, local_status = 0;
int remote_addrinfo_hints = 0;
struct fast_fallback_error last_error = { 0, 0 };
const char *syscall = 0;
VALUE host, serv;
#ifdef HAVE_CONST_AI_ADDRCONFIG
remote_addrinfo_hints |= AI_ADDRCONFIG;
#endif
pthread_t threads[arg->family_size];
char resolved_type[2];
ssize_t resolved_type_size;
int hostname_resolution_waiter = -1, hostname_resolution_notifier = -1;
int pipefd[2];
int nfds = 0;
struct timeval *ends_at = NULL;
struct timeval delay = (struct timeval){ -1, -1 };
struct timeval *delay_p = NULL;
struct hostname_resolution_store resolution_store;
resolution_store.is_all_finised = false;
resolution_store.v6.ai = NULL;
resolution_store.v6.finished = false;
resolution_store.v6.has_error = false;
resolution_store.v4.ai = NULL;
resolution_store.v4.finished = false;
resolution_store.v4.has_error = false;
int last_family = 0;
int additional_capacity = 10;
int current_capacity = additional_capacity;
arg->connection_attempt_fds = allocate_connection_attempt_fds(additional_capacity);
arg->connection_attempt_fds_size = 0;
struct timeval resolution_delay_storage;
struct timeval *resolution_delay_expires_at = NULL;
struct timeval connection_attempt_delay_strage;
struct timeval *connection_attempt_delay_expires_at = NULL;
struct timeval user_specified_resolv_timeout_storage;
struct timeval *user_specified_resolv_timeout_at = NULL;
struct timeval user_specified_connect_timeout_storage;
struct timeval *user_specified_connect_timeout_at = NULL;
struct timespec now = current_clocktime_ts();
/* start of hostname resolution */
if (arg->family_size == 1) {
arg->wait = -1;
arg->getaddrinfo_shared = NULL;
int family = arg->families[0];
arg->remote.res = rsock_addrinfo(
arg->remote.host,
arg->remote.serv,
family,
SOCK_STREAM,
0
);
if (family == AF_INET6) {
resolution_store.v6.ai = arg->remote.res->ai;
resolution_store.v6.finished = true;
resolution_store.v4.finished = true;
} else if (family == AF_INET) {
resolution_store.v4.ai = arg->remote.res->ai;
resolution_store.v4.finished = true;
resolution_store.v6.finished = true;
}
resolution_store.is_all_finised = true;
} else {
if (pipe(pipefd) != 0) rb_syserr_fail(errno, "pipe(2)");
hostname_resolution_waiter = pipefd[0];
int waiter_flags = fcntl(hostname_resolution_waiter, F_GETFL, 0);
if (waiter_flags < 0) rb_syserr_fail(errno, "fcntl(2)");
if ((fcntl(hostname_resolution_waiter, F_SETFL, waiter_flags | O_NONBLOCK)) < 0) {
rb_syserr_fail(errno, "fcntl(2)");
}
arg->wait = hostname_resolution_waiter;
hostname_resolution_notifier = pipefd[1];
arg->getaddrinfo_shared = allocate_fast_fallback_getaddrinfo_shared(arg->family_size);
if (!arg->getaddrinfo_shared) rb_syserr_fail(errno, "calloc(3)");
rb_nativethread_lock_initialize(&arg->getaddrinfo_shared->lock);
arg->getaddrinfo_shared->notify = hostname_resolution_notifier;
arg->getaddrinfo_shared->node = arg->hostp ? ruby_strdup(arg->hostp) : NULL;
arg->getaddrinfo_shared->service = arg->portp ? ruby_strdup(arg->portp) : NULL;
arg->getaddrinfo_shared->refcount = arg->family_size + 1;
for (int i = 0; i < arg->family_size; i++) {
arg->getaddrinfo_entries[i] = &arg->getaddrinfo_shared->getaddrinfo_entries[i];
arg->getaddrinfo_entries[i]->shared = arg->getaddrinfo_shared;
struct addrinfo getaddrinfo_hints[arg->family_size];
allocate_fast_fallback_getaddrinfo_hints(
&getaddrinfo_hints[i],
arg->families[i],
remote_addrinfo_hints,
arg->additional_flags
);
arg->getaddrinfo_entries[i]->hints = getaddrinfo_hints[i];
arg->getaddrinfo_entries[i]->ai = NULL;
arg->getaddrinfo_entries[i]->family = arg->families[i];
arg->getaddrinfo_entries[i]->refcount = 2;
arg->getaddrinfo_entries[i]->has_syserr = false;
arg->getaddrinfo_entries[i]->test_sleep_ms = 0;
arg->getaddrinfo_entries[i]->test_ecode = 0;
/* for testing HEv2 */
if (!NIL_P(test_mode_settings) && RB_TYPE_P(test_mode_settings, T_HASH)) {
const char *family_sym = arg->families[i] == AF_INET6 ? "ipv6" : "ipv4";
VALUE test_delay_setting = rb_hash_aref(test_mode_settings, ID2SYM(rb_intern("delay")));
if (!NIL_P(test_delay_setting)) {
VALUE rb_test_delay_ms = rb_hash_aref(test_delay_setting, ID2SYM(rb_intern(family_sym)));
long test_delay_ms = NIL_P(rb_test_delay_ms) ? 0 : rb_test_delay_ms;
arg->getaddrinfo_entries[i]->test_sleep_ms = test_delay_ms;
}
VALUE test_error_setting = rb_hash_aref(test_mode_settings, ID2SYM(rb_intern("error")));
if (!NIL_P(test_error_setting)) {
VALUE rb_test_ecode = rb_hash_aref(test_error_setting, ID2SYM(rb_intern(family_sym)));
if (!NIL_P(rb_test_ecode)) {
arg->getaddrinfo_entries[i]->test_ecode = NUM2INT(rb_test_ecode);
}
}
}
if (raddrinfo_pthread_create(&threads[i], fork_safe_do_fast_fallback_getaddrinfo, arg->getaddrinfo_entries[i]) != 0) {
rsock_raise_resolution_error("getaddrinfo(3)", EAI_AGAIN);
}
pthread_detach(threads[i]);
}
if (NIL_P(resolv_timeout)) {
user_specified_resolv_timeout_storage = (struct timeval){ -1, -1 };
} else {
struct timeval resolv_timeout_tv = rb_time_interval(resolv_timeout);
user_specified_resolv_timeout_storage = add_ts_to_tv(resolv_timeout_tv, now);
}
user_specified_resolv_timeout_at = &user_specified_resolv_timeout_storage;
}
while (true) {
/* start of connection */
if (any_addrinfos(&resolution_store) &&
!resolution_delay_expires_at &&
!connection_attempt_delay_expires_at) {
while ((remote_ai = pick_addrinfo(&resolution_store, last_family))) {
int fd = -1;
#if !defined(INET6) && defined(AF_INET6)
if (remote_ai->ai_family == AF_INET6) {
if (any_addrinfos(&resolution_store)) continue;
if (!in_progress_fds(arg->connection_attempt_fds_size)) break;
if (resolution_store.is_all_finised) break;
if (local_status < 0) {
host = arg->local.host;
serv = arg->local.serv;
} else {
host = arg->remote.host;
serv = arg->remote.serv;
}
if (last_error.type == RESOLUTION_ERROR) {
rsock_raise_resolution_error(syscall, last_error.ecode);
} else {
rsock_syserr_fail_host_port(last_error.ecode, syscall, host, serv);
}
}
#endif
local_ai = NULL;
if (arg->local.res) {
for (local_ai = arg->local.res->ai; local_ai; local_ai = local_ai->ai_next) {
if (local_ai->ai_family == remote_ai->ai_family) break;
}
if (!local_ai) {
if (any_addrinfos(&resolution_store)) continue;
if (in_progress_fds(arg->connection_attempt_fds_size)) break;
if (!resolution_store.is_all_finised) break;
/* Use a different family local address if no choice, this
* will cause EAFNOSUPPORT. */
rsock_syserr_fail_host_port(EAFNOSUPPORT, syscall, arg->local.host, arg->local.serv);
}
}
status = rsock_socket(remote_ai->ai_family, remote_ai->ai_socktype, remote_ai->ai_protocol);
syscall = "socket(2)";
if (status < 0) {
last_error.type = SYSCALL_ERROR;
last_error.ecode = errno;
if (any_addrinfos(&resolution_store)) continue;
if (in_progress_fds(arg->connection_attempt_fds_size)) break;
if (!resolution_store.is_all_finised) break;
if (local_status < 0) {
host = arg->local.host;
serv = arg->local.serv;
} else {
host = arg->remote.host;
serv = arg->remote.serv;
}
if (last_error.type == RESOLUTION_ERROR) {
rsock_raise_resolution_error(syscall, last_error.ecode);
} else {
rsock_syserr_fail_host_port(last_error.ecode, syscall, host, serv);
}
}
fd = status;
if (local_ai) {
#if !defined(_WIN32) && !defined(__CYGWIN__)
status = 1;
if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&status, (socklen_t)sizeof(status))) < 0) {
rb_syserr_fail(errno, "setsockopt(2)");
}
#endif
status = bind(fd, local_ai->ai_addr, local_ai->ai_addrlen);
local_status = status;
syscall = "bind(2)";
if (status < 0) {
last_error.type = SYSCALL_ERROR;
last_error.ecode = errno;
close(fd);
if (any_addrinfos(&resolution_store)) continue;
if (in_progress_fds(arg->connection_attempt_fds_size)) break;
if (!resolution_store.is_all_finised) break;
if (local_status < 0) {
host = arg->local.host;
serv = arg->local.serv;
} else {
host = arg->remote.host;
serv = arg->remote.serv;
}
if (last_error.type == RESOLUTION_ERROR) {
rsock_raise_resolution_error(syscall, last_error.ecode);
} else {
rsock_syserr_fail_host_port(last_error.ecode, syscall, host, serv);
}
}
}
syscall = "connect(2)";
if (any_addrinfos(&resolution_store) ||
in_progress_fds(arg->connection_attempt_fds_size) ||
!resolution_store.is_all_finised) {
socket_nonblock_set(fd);
status = connect(fd, remote_ai->ai_addr, remote_ai->ai_addrlen);
last_family = remote_ai->ai_family;
} else {
if (!NIL_P(connect_timeout)) {
user_specified_connect_timeout_storage = rb_time_interval(connect_timeout);
user_specified_connect_timeout_at = &user_specified_connect_timeout_storage;
}
VALUE timeout =
(user_specified_connect_timeout_at && is_infinity(*user_specified_connect_timeout_at)) ?
Qnil : tv_to_seconds(user_specified_connect_timeout_at);
io = arg->io = rsock_init_sock(arg->self, fd);
status = rsock_connect(io, remote_ai->ai_addr, remote_ai->ai_addrlen, 0, timeout);
}
if (status == 0) {
connected_fd = fd;
break;
}
if (errno == EINPROGRESS) {
if (current_capacity == arg->connection_attempt_fds_size) {
current_capacity = reallocate_connection_attempt_fds(
&arg->connection_attempt_fds,
current_capacity,
additional_capacity
);
}
arg->connection_attempt_fds[arg->connection_attempt_fds_size] = fd;
(arg->connection_attempt_fds_size)++;
set_timeout_tv(&connection_attempt_delay_strage, 250, now);
connection_attempt_delay_expires_at = &connection_attempt_delay_strage;
if (!any_addrinfos(&resolution_store)) {
if (NIL_P(connect_timeout)) {
user_specified_connect_timeout_storage = (struct timeval){ -1, -1 };
} else {
struct timeval connect_timeout_tv = rb_time_interval(connect_timeout);
user_specified_connect_timeout_storage = add_ts_to_tv(connect_timeout_tv, now);
}
user_specified_connect_timeout_at = &user_specified_connect_timeout_storage;
}
break;
}
last_error.type = SYSCALL_ERROR;
last_error.ecode = errno;
if (NIL_P(io)) {
close(fd);
} else {
rb_io_close(io);
}
if (any_addrinfos(&resolution_store)) continue;
if (in_progress_fds(arg->connection_attempt_fds_size)) break;
if (!resolution_store.is_all_finised) break;
if (local_status < 0) {
host = arg->local.host;
serv = arg->local.serv;
} else {
host = arg->remote.host;
serv = arg->remote.serv;
}
if (last_error.type == RESOLUTION_ERROR) {
rsock_raise_resolution_error(syscall, last_error.ecode);
} else {
rsock_syserr_fail_host_port(last_error.ecode, syscall, host, serv);
}
}
}
if (connected_fd >= 0) break;
ends_at = select_expires_at(
&resolution_store,
resolution_delay_expires_at,
connection_attempt_delay_expires_at,
user_specified_resolv_timeout_at,
user_specified_connect_timeout_at
);
if (ends_at) {
delay = tv_to_timeout(ends_at, now);
delay_p = &delay;
} else {
if (((resolution_store.v6.finished && !resolution_store.v4.finished) ||
(resolution_store.v4.finished && !resolution_store.v6.finished)) &&
!any_addrinfos(&resolution_store) &&
!in_progress_fds(arg->connection_attempt_fds_size)) {
/* A limited timeout is introduced to prevent select(2) from hanging when it is exclusively
* waiting for name resolution and write(2) failure occurs in a child thread. */
delay.tv_sec = 0;
delay.tv_usec = 50000;
delay_p = &delay;
} else {
delay_p = NULL;
}
}
nfds = 0;
rb_fd_zero(&arg->writefds);
if (in_progress_fds(arg->connection_attempt_fds_size)) {
int n = 0;
for (int i = 0; i < arg->connection_attempt_fds_size; i++) {
int cfd = arg->connection_attempt_fds[i];
if (cfd < 0) continue;
if (cfd > n) n = cfd;
rb_fd_set(cfd, &arg->writefds);
}
if (n > 0) n++;
nfds = n;
}
rb_fd_zero(&arg->readfds);
if (arg->family_size > 1) {
rb_fd_set(hostname_resolution_waiter, &arg->readfds);
if ((hostname_resolution_waiter + 1) > nfds) {
nfds = hostname_resolution_waiter + 1;
}
}
status = rb_thread_fd_select(nfds, &arg->readfds, &arg->writefds, NULL, delay_p);
now = current_clocktime_ts();
if (is_timeout_tv(resolution_delay_expires_at, now)) {
resolution_delay_expires_at = NULL;
}
if (is_timeout_tv(connection_attempt_delay_expires_at, now)) {
connection_attempt_delay_expires_at = NULL;
}
if (status < 0 && (errno && errno != EINTR)) rb_syserr_fail(errno, "select(2)");
if (status > 0) {
/* check for connection */
if (in_progress_fds(arg->connection_attempt_fds_size)) {
for (int i = 0; i < arg->connection_attempt_fds_size; i++) {
int fd = arg->connection_attempt_fds[i];
if (fd < 0 || !rb_fd_isset(fd, &arg->writefds)) continue;
int err;
socklen_t len = sizeof(err);
status = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
if (status < 0) {
last_error.type = SYSCALL_ERROR;
last_error.ecode = errno;
close(fd);
if (any_addrinfos(&resolution_store)) continue;
if (in_progress_fds(arg->connection_attempt_fds_size)) break;
if (!resolution_store.is_all_finised) break;
if (local_status < 0) {
host = arg->local.host;
serv = arg->local.serv;
} else {
host = arg->remote.host;
serv = arg->remote.serv;
}
if (last_error.type == RESOLUTION_ERROR) {
rsock_raise_resolution_error(syscall, last_error.ecode);
} else {
rsock_syserr_fail_host_port(last_error.ecode, syscall, host, serv);
}
}
if (err == 0) { /* success */
remove_connection_attempt_fd(
arg->connection_attempt_fds,
&arg->connection_attempt_fds_size,
fd
);
connected_fd = fd;
break;
} else { /* fail */
close(fd);
remove_connection_attempt_fd(
arg->connection_attempt_fds,
&arg->connection_attempt_fds_size,
fd
);
last_error.type = SYSCALL_ERROR;
last_error.ecode = err;
}
}
if (connected_fd >= 0) break;
if (!in_progress_fds(arg->connection_attempt_fds_size)) {
if (!any_addrinfos(&resolution_store) && resolution_store.is_all_finised) {
if (local_status < 0) {
host = arg->local.host;
serv = arg->local.serv;
} else {
host = arg->remote.host;
serv = arg->remote.serv;
}
if (last_error.type == RESOLUTION_ERROR) {
rsock_raise_resolution_error(syscall, last_error.ecode);
} else {
rsock_syserr_fail_host_port(last_error.ecode, syscall, host, serv);
}
}
connection_attempt_delay_expires_at = NULL;
user_specified_connect_timeout_at = NULL;
}
}
/* check for hostname resolution */
if (!resolution_store.is_all_finised && rb_fd_isset(hostname_resolution_waiter, &arg->readfds)) {
while (true) {
resolved_type_size = read(
hostname_resolution_waiter,
resolved_type,
sizeof(resolved_type) - 1
);
if (resolved_type_size > 0) {
resolved_type[resolved_type_size] = '\0';
if (resolved_type[0] == IPV6_HOSTNAME_RESOLVED) {
resolution_store.v6.finished = true;
if (arg->getaddrinfo_entries[IPV6_ENTRY_POS]->err &&
arg->getaddrinfo_entries[IPV6_ENTRY_POS]->err != EAI_ADDRFAMILY) {
if (!resolution_store.v4.finished || resolution_store.v4.has_error) {