[前][次][番号順一覧][スレッド一覧]

ruby-changes:29634

From: akr <ko1@a...>
Date: Fri, 28 Jun 2013 12:16:19 +0900 (JST)
Subject: [ruby-changes:29634] akr:r41686 (trunk): * ext/socket/ipsocket.c (init_inetsock_internal): Don't use local

akr	2013-06-28 12:16:07 +0900 (Fri, 28 Jun 2013)

  New Revision: 41686

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41686

  Log:
    * ext/socket/ipsocket.c (init_inetsock_internal): Don't use local
      addresses which address family is different to remote address.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/ipsocket.c
    trunk/test/socket/test_tcp.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 41685)
+++ ChangeLog	(revision 41686)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jun 28 12:14:04 2013  Tanaka Akira  <akr@f...>
+
+	* ext/socket/ipsocket.c (init_inetsock_internal): Don't use local
+	  addresses which address family is different to remote address.
+
 Fri Jun 28 08:06:22 2013  Tanaka Akira  <akr@f...>
 
 	* bignum.c (bigand_int): Add arguments, xn and hibitsx.
Index: ext/socket/ipsocket.c
===================================================================
--- ext/socket/ipsocket.c	(revision 41685)
+++ ext/socket/ipsocket.c	(revision 41686)
@@ -42,7 +42,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L42
 init_inetsock_internal(struct inetsock_arg *arg)
 {
     int type = arg->type;
-    struct addrinfo *res;
+    struct addrinfo *res, *lres;
     int fd, status = 0, local = 0;
     const char *syscall = 0;
 
@@ -62,6 +62,15 @@ init_inetsock_internal(struct inetsock_a https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L62
 	if (res->ai_family == AF_INET6)
 	    continue;
 #endif
+        lres = NULL;
+        if (arg->local.res) {
+            for (lres = arg->local.res; lres; lres = lres->ai_next) {
+                if (lres->ai_family == res->ai_family)
+                    break;
+            }
+            if (!lres)
+                continue;
+        }
 	status = rsock_socket(res->ai_family,res->ai_socktype,res->ai_protocol);
 	syscall = "socket(2)";
 	fd = status;
@@ -79,8 +88,8 @@ init_inetsock_internal(struct inetsock_a https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L88
 	    syscall = "bind(2)";
 	}
 	else {
-	    if (arg->local.res) {
-		status = bind(fd, arg->local.res->ai_addr, arg->local.res->ai_addrlen);
+	    if (lres) {
+		status = bind(fd, lres->ai_addr, lres->ai_addrlen);
 		local = status;
 		syscall = "bind(2)";
 	    }
Index: test/socket/test_tcp.rb
===================================================================
--- test/socket/test_tcp.rb	(revision 41685)
+++ test/socket/test_tcp.rb	(revision 41686)
@@ -7,19 +7,21 @@ end https://github.com/ruby/ruby/blob/trunk/test/socket/test_tcp.rb#L7
 
 class TestSocket_TCPSocket < Test::Unit::TestCase
   def test_initialize_failure
-    s = TCPServer.new("localhost", nil)
+    addr = '127.0.0.1'
+
+    s = TCPServer.new(addr, nil)
     server_port = s.addr[1]
 
-    c = TCPSocket.new("localhost", server_port)
+    c = TCPSocket.new(addr, server_port)
     client_port = c.addr[1]
 
     begin
       # TCPServer.new uses SO_REUSEADDR so we must create a failure on the
       # local address.
-      TCPSocket.new("localhost", server_port, "localhost", client_port)
+      TCPSocket.new(addr, server_port, addr, client_port)
       flunk "expected SystemCallError"
     rescue SystemCallError => e
-      assert_match "for \"localhost\" port #{client_port}", e.message
+      assert_match "for \"#{addr}\" port #{client_port}", e.message
     end
   end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]