11require " socket"
22require " ../../dns"
33
4+ {% begin % }
45struct Socket::Addrinfo
56 QUERY_INET = [DNS ::Resource ::A ::RECORD_TYPE ]
67 QUERY_INET6 = [DNS ::Resource ::AAAA ::RECORD_TYPE ]
78 QUERY_UNSPEC = [DNS ::Resource ::AAAA ::RECORD_TYPE , DNS ::Resource ::A ::RECORD_TYPE ]
89
9- private def self.getaddrinfo (domain , service , family , type , protocol , timeout , & )
10+ private def self.getaddrinfo (domain , service , family , type , protocol , timeout ,
11+ {% if compare_versions(Crystal ::VERSION , " 1.19.0" ) >= 0 % }
12+ flags = 0 ,
13+ {% end % }
14+ & )
1015 # fallback to the original implementation in these cases
11- if family.unix? || Socket :: IPAddress .valid?(domain) || domain.includes?('/' ) || DNS .select_resolver(domain).is_a?(DNS ::Resolver ::System )
16+ if family.unix? || domain.includes?('/' ) || DNS .select_resolver(domain).is_a?(DNS ::Resolver ::System )
1217 domain = URI ::Punycode .to_ascii domain
1318 Crystal ::System ::Addrinfo .getaddrinfo(domain, service, family, type , protocol, timeout) do |addrinfo |
1419 yield addrinfo
@@ -35,9 +40,12 @@ struct Socket::Addrinfo
3540 ip_address = record.ip_address.address
3641 found = true
3742
38- # NOTE:: ideally we set AI_NUMERICHOST, supported on all platforms, to ensure no blocking takes place
39- # currently not possible in crystal as we don't have direct access to `ai_flags` field
40- Crystal ::System ::Addrinfo .getaddrinfo(ip_address, service, family, type , protocol, timeout) do |addrinfo |
43+ # We set AI_NUMERICHOST, supported on all platforms, to ensure no blocking takes place
44+ Crystal ::System ::Addrinfo .getaddrinfo(ip_address, service, family, type , protocol, timeout,
45+ {% if compare_versions(Crystal ::VERSION , " 1.19.0" ) >= 0 % }
46+ ::LibC ::AI_NUMERICHOST
47+ {% end % }
48+ ) do |addrinfo |
4149 yield addrinfo
4250 end
4351 end
@@ -50,3 +58,4 @@ struct Socket::Addrinfo
5058 raise Socket ::Addrinfo ::Error .new(message: " Hostname lookup for #{ domain } failed: No address found" , cause: error)
5159 end
5260end
61+ {% end % }
0 commit comments