@@ -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-
321287void
322288rb_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+
501517struct rb_addrinfo *
502518rsock_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" );
0 commit comments