Update to RDoc 2.1.0 r112
[rbx.git] / stdlib / resolv-replace.rb
blob5d15b4577c51c9b4cc8334caef73a55786c56602
1 require 'socket'
2 require 'resolv'
4 class << IPSocket
5   alias original_resolv_getaddress getaddress
6   def getaddress(host)
7     begin
8       return Resolv.getaddress(host).to_s
9     rescue Resolv::ResolvError
10       raise SocketError, "Hostname not known: #{host}"
11     end
12   end
13 end
15 class TCPSocket
16   alias original_resolv_initialize initialize
17   def initialize(host, serv, *rest)
18     rest[0] = IPSocket.getaddress(rest[0]) unless rest.empty?
19     original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
20   end
21 end
23 class UDPSocket
24   alias original_resolv_bind bind
25   def bind(host, port)
26     original_resolv_bind(IPSocket.getaddress(host), port)
27   end
29   alias original_resolv_connect connect
30   def connect(host, port)
31     original_resolv_connect(IPSocket.getaddress(host), port)
32   end
34   alias original_resolv_send send
35   def send(mesg, flags, *rest)
36     if rest.length == 2
37       host, port = rest
38       begin
39         addrs = Resolv.getaddresses(host)
40       rescue Resolv::ResolvError
41         raise SocketError, "Hostname not known: #{host}"
42       end
43       err = nil
44       addrs[0...-1].each {|addr|
45         begin
46           return original_resolv_send(mesg, flags, addr, port)
47         rescue SystemCallError
48         end
49       }
50       original_resolv_send(mesg, flags, addrs[-1], port)
51     else
52       original_resolv_send(mesg, flags, *rest)
53     end
54   end
55 end
57 class SOCKSSocket
58   alias original_resolv_initialize initialize
59   def initialize(host, serv)
60     original_resolv_initialize(IPSocket.getaddress(host), port)
61   end
62 end if defined? SOCKSSocket