# frozen_string_literal: true
alias original_resolv_getaddress getaddress
return Resolv.getaddress(host).to_s
rescue Resolv::ResolvError
raise SocketError, "Hostname not known: #{host}"
class TCPSocket < IPSocket
alias original_resolv_initialize initialize
def initialize(host, serv, *rest)
rest[0] = IPSocket.getaddress(rest[0]) if rest[0]
original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
class UDPSocket < IPSocket
alias original_resolv_bind bind
host = IPSocket.getaddress(host) if host != ""
original_resolv_bind(host, port)
alias original_resolv_connect connect
original_resolv_connect(IPSocket.getaddress(host), port)
alias original_resolv_send send
def send(mesg, flags, *rest)
addrs = Resolv.getaddresses(host)
rescue Resolv::ResolvError
raise SocketError, "Hostname not known: #{host}"
addrs[0...-1].each {|addr|
return original_resolv_send(mesg, flags, addr, port)
original_resolv_send(mesg, flags, addrs[-1], port)
original_resolv_send(mesg, flags, *rest)
class SOCKSSocket < TCPSocket
alias original_resolv_initialize initialize
def initialize(host, serv)
original_resolv_initialize(IPSocket.getaddress(host), port)
end if defined? SOCKSSocket