ruby-changes:10510
From: akr <ko1@a...>
Date: Thu, 5 Feb 2009 09:12:00 +0900 (JST)
Subject: [ruby-changes:10510] Ruby:r22064 (trunk): don't connect to unspecified address (0.0.0.0). OpenBSD reject it.
akr 2009-02-05 09:09:39 +0900 (Thu, 05 Feb 2009) New Revision: 22064 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22064 Log: don't connect to unspecified address (0.0.0.0). OpenBSD reject it. Modified files: trunk/test/socket/test_addrinfo.rb Index: test/socket/test_addrinfo.rb =================================================================== --- test/socket/test_addrinfo.rb (revision 22063) +++ test/socket/test_addrinfo.rb (revision 22064) @@ -8,6 +8,18 @@ class TestSocketAddrInfo < Test::Unit::TestCase HAS_UNIXSOCKET = defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM + def tcp_unspecified_to_loopback(addrinfo) + if addrinfo.ipv4? && addrinfo.ip_address == "0.0.0.0" + AddrInfo.tcp("127.0.0.1", addrinfo.ip_port) + elsif addrinfo.ipv6? && addrinfo.ipv6_unspecified? + AddrInfo.tcp("::1", addrinfo.ip_port) + elsif addrinfo.ipv6? && (ai = addrinfo.ipv6_to_ipv4) && ai.ip_address == "0.0.0.0" + AddrInfo.tcp("127.0.0.1", addrinfo.ip_port) + else + addrinfo + end + end + def test_addrinfo_ip ai = AddrInfo.ip("127.0.0.1") assert_equal([0, "127.0.0.1"], Socket.unpack_sockaddr_in(ai)) @@ -319,6 +331,7 @@ def test_connect_from TCPServer.open("0.0.0.0", 0) {|serv| serv_ai = AddrInfo.new(serv.getsockname, :INET, :STREAM) + serv_ai = tcp_unspecified_to_loopback(serv_ai) port = random_port begin serv_ai.connect_from("0.0.0.0", port) {|s1| @@ -338,6 +351,7 @@ def test_connect_to TCPServer.open("0.0.0.0", 0) {|serv| serv_ai = AddrInfo.new(serv.getsockname, :INET, :STREAM) + serv_ai = tcp_unspecified_to_loopback(serv_ai) port = random_port client_ai = AddrInfo.tcp("0.0.0.0", port) begin @@ -358,6 +372,7 @@ def test_connect TCPServer.open("0.0.0.0", 0) {|serv| serv_ai = AddrInfo.new(serv.getsockname, :INET, :STREAM) + serv_ai = tcp_unspecified_to_loopback(serv_ai) begin serv_ai.connect {|s1| s2 = serv.accept @@ -392,7 +407,11 @@ begin client_ai.listen {|serv| assert_equal(port, serv.local_address.ip_port) - TCPSocket.open(*serv.local_address.ip_unpack) {|s1| + serv_addr, serv_port = serv.local_address.ip_unpack + case serv_addr + when "0.0.0.0" then serv_addr = "127.0.0.1" + end + TCPSocket.open(serv_addr, serv_port) {|s1| s2, addr = serv.accept begin assert_equal(s1.local_address.ip_unpack, addr.ip_unpack) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/