* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / lib / resolv-replace.rb
blob63d58cea27df4a0751fff4419afc6dfcc306101a
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     host = IPSocket.getaddress(host) if host != ""
27     original_resolv_bind(host, port)
28   end
30   alias original_resolv_connect connect
31   def connect(host, port)
32     original_resolv_connect(IPSocket.getaddress(host), port)
33   end
35   alias original_resolv_send send
36   def send(mesg, flags, *rest)
37     if rest.length == 2
38       host, port = rest
39       begin
40         addrs = Resolv.getaddresses(host)
41       rescue Resolv::ResolvError
42         raise SocketError, "Hostname not known: #{host}"
43       end
44       err = nil
45       addrs[0...-1].each {|addr|
46         begin
47           return original_resolv_send(mesg, flags, addr, port)
48         rescue SystemCallError
49         end
50       }
51       original_resolv_send(mesg, flags, addrs[-1], port)
52     else
53       original_resolv_send(mesg, flags, *rest)
54     end
55   end
56 end
58 class SOCKSSocket
59   alias original_resolv_initialize initialize
60   def initialize(host, serv)
61     original_resolv_initialize(IPSocket.getaddress(host), port)
62   end
63 end if defined? SOCKSSocket