Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Ruby Resolv instead of LibC for hostname lookup #739

Merged
merged 2 commits into from Mar 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions lib/excon/socket.rb
@@ -1,4 +1,6 @@
# frozen_string_literal: true
require 'resolv'

module Excon
class Socket
include Utils
Expand Down Expand Up @@ -95,20 +97,17 @@ def local_port
def connect
@socket = nil
exception = nil
hostname = @data[:hostname]
port = @port
family = @data[:family]

if @data[:proxy]
family = @data[:proxy][:family] || ::Socket::Constants::AF_UNSPEC
args = [@data[:proxy][:hostname], @data[:proxy][:port], family, ::Socket::Constants::SOCK_STREAM]
else
family = @data[:family] || ::Socket::Constants::AF_UNSPEC
args = [@data[:hostname], @port, family, ::Socket::Constants::SOCK_STREAM]
end
if RUBY_VERSION >= '1.9.2' && defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby'
args << nil << nil << false # no reverse lookup
hostname = @data[:proxy][:hostname]
port = @data[:proxy][:port]
family = @data[:proxy][:family]
end
addrinfo = ::Socket.getaddrinfo(*args)

addrinfo.each do |_, port, _, ip, a_family, s_type|
Resolv.each_address(hostname) do |ip|
# already succeeded on previous addrinfo
if @socket
break
Expand All @@ -120,8 +119,8 @@ def connect
# nonblocking connect
begin
sockaddr = ::Socket.sockaddr_in(port, ip)

socket = ::Socket.new(a_family, s_type, 0)
addrinfo = Addrinfo.getaddrinfo(ip, port, family, :STREAM).first
socket = ::Socket.new(addrinfo.pfamily, addrinfo.socktype, addrinfo.protocol)

if @data[:reuseaddr]
socket.setsockopt(::Socket::Constants::SOL_SOCKET, ::Socket::Constants::SO_REUSEADDR, true)
Expand Down Expand Up @@ -151,6 +150,8 @@ def connect
end
end

exception ||= Resolv::ResolvError.new("no address for #{hostname}")

# this will be our last encountered exception
fail exception unless @socket

Expand Down