2003-09-26 13:35:44 +04:00
|
|
|
require 'socket'
|
2001-05-30 13:10:30 +04:00
|
|
|
require 'resolv'
|
|
|
|
|
|
|
|
class << IPSocket
|
|
|
|
alias original_resolv_getaddress getaddress
|
|
|
|
def getaddress(host)
|
2004-02-01 18:21:24 +03:00
|
|
|
begin
|
|
|
|
return Resolv.getaddress(host).to_s
|
|
|
|
rescue Resolv::ResolvError
|
2004-02-01 20:42:08 +03:00
|
|
|
raise SocketError, "Hostname not known: #{host}"
|
2004-02-01 18:21:24 +03:00
|
|
|
end
|
2001-05-30 13:10:30 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-26 13:35:44 +04:00
|
|
|
class TCPSocket
|
|
|
|
alias original_resolv_initialize initialize
|
|
|
|
def initialize(host, serv, *rest)
|
2004-02-01 18:21:24 +03:00
|
|
|
rest[0] = IPSocket.getaddress(rest[0]) unless rest.empty?
|
|
|
|
original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
|
2001-05-30 13:10:30 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class UDPSocket
|
2003-09-26 13:35:44 +04:00
|
|
|
alias original_resolv_bind bind
|
|
|
|
def bind(host, port)
|
2008-08-08 05:58:40 +04:00
|
|
|
host = IPSocket.getaddress(host) if host != ""
|
|
|
|
original_resolv_bind(host, port)
|
2003-09-26 13:35:44 +04:00
|
|
|
end
|
|
|
|
|
2001-05-30 13:10:30 +04:00
|
|
|
alias original_resolv_connect connect
|
|
|
|
def connect(host, port)
|
2004-02-01 18:21:24 +03:00
|
|
|
original_resolv_connect(IPSocket.getaddress(host), port)
|
2001-05-30 13:10:30 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
alias original_resolv_send send
|
|
|
|
def send(mesg, flags, *rest)
|
2004-02-01 18:21:24 +03:00
|
|
|
if rest.length == 2
|
|
|
|
host, port = rest
|
|
|
|
begin
|
|
|
|
addrs = Resolv.getaddresses(host)
|
|
|
|
rescue Resolv::ResolvError
|
2004-02-01 20:42:08 +03:00
|
|
|
raise SocketError, "Hostname not known: #{host}"
|
2004-02-01 18:21:24 +03:00
|
|
|
end
|
|
|
|
err = nil
|
|
|
|
addrs[0...-1].each {|addr|
|
|
|
|
begin
|
|
|
|
return original_resolv_send(mesg, flags, addr, port)
|
|
|
|
rescue SystemCallError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
original_resolv_send(mesg, flags, addrs[-1], port)
|
|
|
|
else
|
|
|
|
original_resolv_send(mesg, flags, *rest)
|
|
|
|
end
|
2001-05-30 13:10:30 +04:00
|
|
|
end
|
|
|
|
end
|
2003-09-26 13:35:44 +04:00
|
|
|
|
|
|
|
class SOCKSSocket
|
|
|
|
alias original_resolv_initialize initialize
|
|
|
|
def initialize(host, serv)
|
2004-02-01 18:21:24 +03:00
|
|
|
original_resolv_initialize(IPSocket.getaddress(host), port)
|
2003-09-26 13:35:44 +04:00
|
|
|
end
|
|
|
|
end if defined? SOCKSSocket
|