2003-09-26 13:35:44 +04:00
|
|
|
require 'socket'
|
2001-05-30 13:10:30 +04:00
|
|
|
require 'resolv'
|
|
|
|
|
|
|
|
class << IPSocket
|
2010-03-22 06:33:14 +03:00
|
|
|
# :stopdoc:
|
2001-05-30 13:10:30 +04:00
|
|
|
alias original_resolv_getaddress getaddress
|
2010-03-22 06:33:14 +03:00
|
|
|
# :startdoc:
|
2001-05-30 13:10:30 +04:00
|
|
|
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
|
|
|
|
|
2010-03-21 15:10:53 +03:00
|
|
|
class TCPSocket < IPSocket
|
2010-03-22 06:33:14 +03:00
|
|
|
# :stopdoc:
|
2003-09-26 13:35:44 +04:00
|
|
|
alias original_resolv_initialize initialize
|
2010-03-22 06:33:14 +03:00
|
|
|
# :startdoc:
|
2003-09-26 13:35:44 +04:00
|
|
|
def initialize(host, serv, *rest)
|
2013-03-11 17:47:04 +04:00
|
|
|
rest[0] = IPSocket.getaddress(rest[0]) if rest[0]
|
2004-02-01 18:21:24 +03:00
|
|
|
original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
|
2001-05-30 13:10:30 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-21 15:10:53 +03:00
|
|
|
class UDPSocket < IPSocket
|
2010-03-22 06:33:14 +03:00
|
|
|
# :stopdoc:
|
2003-09-26 13:35:44 +04:00
|
|
|
alias original_resolv_bind bind
|
2010-03-22 06:33:14 +03:00
|
|
|
# :startdoc:
|
2003-09-26 13:35:44 +04:00
|
|
|
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
|
|
|
|
|
2010-03-22 06:33:14 +03:00
|
|
|
# :stopdoc:
|
2001-05-30 13:10:30 +04:00
|
|
|
alias original_resolv_connect connect
|
2010-03-22 06:33:14 +03:00
|
|
|
# :startdoc:
|
2001-05-30 13:10:30 +04:00
|
|
|
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
|
|
|
|
|
2010-03-22 06:33:14 +03:00
|
|
|
# :stopdoc:
|
2001-05-30 13:10:30 +04:00
|
|
|
alias original_resolv_send send
|
2010-03-22 06:33:14 +03:00
|
|
|
# :startdoc:
|
2001-05-30 13:10:30 +04:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2010-03-21 15:10:53 +03:00
|
|
|
class SOCKSSocket < TCPSocket
|
2010-03-22 06:33:14 +03:00
|
|
|
# :stopdoc:
|
2003-09-26 13:35:44 +04:00
|
|
|
alias original_resolv_initialize initialize
|
2010-03-22 06:33:14 +03:00
|
|
|
# :startdoc:
|
2003-09-26 13:35:44 +04:00
|
|
|
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
|