Skip to content

Commit 2792acc

Browse files
ioquatixbruno-
andauthored
Add scheduler hook Addrinfo.getaddrinfo. (#4375)
Co-authored-by: Bruno Sutic <code@brunosutic.com>
1 parent 688b217 commit 2792acc

9 files changed

Lines changed: 626 additions & 278 deletions

File tree

‎doc/fiber.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ class Scheduler
9090
def timeout_after(duration, klass, *arguments, &block)
9191
end
9292

93+
# Resolve hostname to an array of IP addresses.
94+
# This hook is optional.
95+
# @parameter hostname [String] Example: "www.ruby-lang.org".
96+
# @returns [Array] An array of IPv4 and/or IPv6 address strings that the hostname resolves to.
97+
def address_resolve(hostname)
98+
end
99+
93100
# Block the calling fiber.
94101
# @parameter blocker [Object] What we are waiting on, informational only.
95102
# @parameter timeout [Numeric | Nil] The amount of time to wait for in seconds.

‎ext/socket/depend‎

Lines changed: 225 additions & 210 deletions
Large diffs are not rendered by default.

‎ext/socket/raddrinfo.c‎

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -284,40 +284,6 @@ numeric_getaddrinfo(const char *node, const char *service,
284284
return EAI_FAIL;
285285
}
286286

287-
int
288-
rb_getaddrinfo(const char *node, const char *service,
289-
const struct addrinfo *hints,
290-
struct rb_addrinfo **res)
291-
{
292-
struct addrinfo *ai;
293-
int ret;
294-
int allocated_by_malloc = 0;
295-
296-
ret = numeric_getaddrinfo(node, service, hints, &ai);
297-
if (ret == 0)
298-
allocated_by_malloc = 1;
299-
else {
300-
#ifdef GETADDRINFO_EMU
301-
ret = getaddrinfo(node, service, hints, &ai);
302-
#else
303-
struct getaddrinfo_arg arg;
304-
MEMZERO(&arg, struct getaddrinfo_arg, 1);
305-
arg.node = node;
306-
arg.service = service;
307-
arg.hints = hints;
308-
arg.res = &ai;
309-
ret = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getaddrinfo, &arg, RUBY_UBF_IO, 0);
310-
#endif
311-
}
312-
313-
if (ret == 0) {
314-
*res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
315-
(*res)->allocated_by_malloc = allocated_by_malloc;
316-
(*res)->ai = ai;
317-
}
318-
return ret;
319-
}
320-
321287
void
322288
rb_freeaddrinfo(struct rb_addrinfo *ai)
323289
{
@@ -498,12 +464,63 @@ port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr)
498464
}
499465
}
500466

467+
static int
468+
rb_scheduler_getaddrinfo(VALUE scheduler, VALUE host, const char *service,
469+
const struct addrinfo *hints, struct rb_addrinfo **res)
470+
{
471+
int error, res_allocated = 0, _additional_flags = 0;
472+
long i, len;
473+
struct addrinfo *ai, *ai_tail = NULL;
474+
char *hostp;
475+
char _hbuf[NI_MAXHOST];
476+
VALUE ip_addresses_array, ip_address;
477+
478+
ip_addresses_array = rb_fiber_scheduler_address_resolve(scheduler, host);
479+
480+
if (ip_addresses_array == Qundef) {
481+
// Returns EAI_FAIL if the scheduler hook is not implemented:
482+
return EAI_FAIL;
483+
} else if (ip_addresses_array == Qnil) {
484+
len = 0;
485+
} else {
486+
len = RARRAY_LEN(ip_addresses_array);
487+
}
488+
489+
for(i=0; i<len; i++) {
490+
ip_address = rb_ary_entry(ip_addresses_array, i);
491+
hostp = host_str(ip_address, _hbuf, sizeof(_hbuf), &_additional_flags);
492+
error = numeric_getaddrinfo(hostp, service, hints, &ai);
493+
if (error == 0) {
494+
if (!res_allocated) {
495+
res_allocated = 1;
496+
*res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
497+
(*res)->allocated_by_malloc = 1;
498+
(*res)->ai = ai;
499+
ai_tail = ai;
500+
} else {
501+
while (ai_tail->ai_next) {
502+
ai_tail = ai_tail->ai_next;
503+
}
504+
ai_tail->ai_next = ai;
505+
ai_tail = ai;
506+
}
507+
}
508+
}
509+
510+
if (res_allocated) { // At least one valid result.
511+
return 0;
512+
} else {
513+
return EAI_NONAME;
514+
}
515+
}
516+
501517
struct rb_addrinfo*
502518
rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
503519
{
504520
struct rb_addrinfo* res = NULL;
521+
struct addrinfo *ai;
505522
char *hostp, *portp;
506-
int error;
523+
int error = 0;
507524
char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
508525
int additional_flags = 0;
509526

@@ -515,7 +532,43 @@ rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_h
515532
}
516533
hints->ai_flags |= additional_flags;
517534

518-
error = rb_getaddrinfo(hostp, portp, hints, &res);
535+
error = numeric_getaddrinfo(hostp, portp, hints, &ai);
536+
if (error == 0) {
537+
res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
538+
res->allocated_by_malloc = 1;
539+
res->ai = ai;
540+
} else {
541+
VALUE scheduler = rb_fiber_scheduler_current();
542+
int resolved = 0;
543+
544+
if (scheduler != Qnil && hostp && !(hints->ai_flags & AI_NUMERICHOST)) {
545+
error = rb_scheduler_getaddrinfo(scheduler, host, portp, hints, &res);
546+
547+
if (error != EAI_FAIL) {
548+
resolved = 1;
549+
}
550+
}
551+
552+
if (!resolved) {
553+
#ifdef GETADDRINFO_EMU
554+
error = getaddrinfo(hostp, portp, hints, &ai);
555+
#else
556+
struct getaddrinfo_arg arg;
557+
MEMZERO(&arg, struct getaddrinfo_arg, 1);
558+
arg.node = hostp;
559+
arg.service = portp;
560+
arg.hints = hints;
561+
arg.res = &ai;
562+
error = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getaddrinfo, &arg, RUBY_UBF_IO, 0);
563+
#endif
564+
if (error == 0) {
565+
res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
566+
res->allocated_by_malloc = 0;
567+
res->ai = ai;
568+
}
569+
}
570+
}
571+
519572
if (error) {
520573
if (hostp && hostp[strlen(hostp)-1] == '\n') {
521574
rb_raise(rb_eSocket, "newline at the end of hostname");

‎ext/socket/rubysocket.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
#include "ruby/thread.h"
140140
#include "ruby/util.h"
141141
#include "sockport.h"
142+
#include "ruby/fiber/scheduler.h"
142143

143144
#ifndef HAVE_TYPE_SOCKLEN_T
144145
typedef int socklen_t;
@@ -313,7 +314,6 @@ struct rb_addrinfo {
313314
struct addrinfo *ai;
314315
int allocated_by_malloc;
315316
};
316-
int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res);
317317
void rb_freeaddrinfo(struct rb_addrinfo *ai);
318318
VALUE rsock_freeaddrinfo(VALUE arg);
319319
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);

‎ext/socket/socket.c‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,34 +1279,10 @@ sock_s_getnameinfo(int argc, VALUE *argv, VALUE _)
12791279
rb_raise(rb_eArgError, "array size should be 3 or 4, %ld given",
12801280
RARRAY_LEN(sa));
12811281
}
1282-
/* host */
1283-
if (NIL_P(host)) {
1284-
hptr = NULL;
1285-
}
1286-
else {
1287-
strncpy(hbuf, StringValueCStr(host), sizeof(hbuf));
1288-
hbuf[sizeof(hbuf) - 1] = '\0';
1289-
hptr = hbuf;
1290-
}
1291-
/* port */
1292-
if (NIL_P(port)) {
1293-
strcpy(pbuf, "0");
1294-
pptr = NULL;
1295-
}
1296-
else if (FIXNUM_P(port)) {
1297-
snprintf(pbuf, sizeof(pbuf), "%ld", NUM2LONG(port));
1298-
pptr = pbuf;
1299-
}
1300-
else {
1301-
strncpy(pbuf, StringValueCStr(port), sizeof(pbuf));
1302-
pbuf[sizeof(pbuf) - 1] = '\0';
1303-
pptr = pbuf;
1304-
}
13051282
hints.ai_socktype = (fl & NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
13061283
/* af */
13071284
hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
1308-
error = rb_getaddrinfo(hptr, pptr, &hints, &res);
1309-
if (error) goto error_exit_addr;
1285+
res = rsock_getaddrinfo(host, port, &hints, 0);
13101286
sap = res->ai->ai_addr;
13111287
salen = res->ai->ai_addrlen;
13121288
}
@@ -1336,12 +1312,6 @@ sock_s_getnameinfo(int argc, VALUE *argv, VALUE _)
13361312
}
13371313
return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
13381314

1339-
error_exit_addr:
1340-
saved_errno = errno;
1341-
if (res) rb_freeaddrinfo(res);
1342-
errno = saved_errno;
1343-
rsock_raise_socket_error("getaddrinfo", error);
1344-
13451315
error_exit_name:
13461316
saved_errno = errno;
13471317
if (res) rb_freeaddrinfo(res);

‎include/ruby/fiber/scheduler.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ VALUE rb_fiber_scheduler_io_wait_writable(VALUE scheduler, VALUE io);
4444
VALUE rb_fiber_scheduler_io_read(VALUE scheduler, VALUE io, VALUE buffer, size_t offset, size_t length);
4545
VALUE rb_fiber_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t offset, size_t length);
4646

47+
VALUE rb_fiber_scheduler_address_resolve(VALUE scheduler, VALUE hostname);
48+
4749
RBIMPL_SYMBOL_EXPORT_END()
4850

4951
#endif /* RUBY_FIBER_SCHEDULER_H */

‎scheduler.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ static ID id_io_read;
2525
static ID id_io_write;
2626
static ID id_io_wait;
2727

28+
static ID id_address_resolve;
29+
2830
void
2931
Init_Fiber_Scheduler(void)
3032
{
@@ -40,6 +42,8 @@ Init_Fiber_Scheduler(void)
4042
id_io_read = rb_intern_const("io_read");
4143
id_io_write = rb_intern_const("io_write");
4244
id_io_wait = rb_intern_const("io_wait");
45+
46+
id_address_resolve = rb_intern_const("address_resolve");
4347
}
4448

4549
VALUE
@@ -200,3 +204,13 @@ rb_fiber_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t offs
200204
// We should ensure string has capacity to receive data, and then resize it afterwards.
201205
return rb_check_funcall(scheduler, id_io_write, 4, arguments);
202206
}
207+
208+
VALUE
209+
rb_fiber_scheduler_address_resolve(VALUE scheduler, VALUE hostname)
210+
{
211+
VALUE arguments[] = {
212+
hostname
213+
};
214+
215+
return rb_check_funcall(scheduler, id_address_resolve, 1, arguments);
216+
}

‎test/fiber/scheduler.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,10 @@ def fiber(&block)
223223

224224
return fiber
225225
end
226+
227+
def address_resolve(hostname)
228+
Thread.new do
229+
Addrinfo.getaddrinfo(hostname, nil).map(&:ip_address).uniq
230+
end.value
231+
end
226232
end

0 commit comments

Comments
 (0)