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

ruby-changes:28102

From: akr <ko1@a...>
Date: Sat, 6 Apr 2013 14:50:53 +0900 (JST)
Subject: [ruby-changes:28102] akr:r40154 (trunk): * ext/socket/rubysocket.h (SockAddrStringValueWithAddrinfo): New macro.

akr	2013-04-06 14:50:42 +0900 (Sat, 06 Apr 2013)

  New Revision: 40154

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

  Log:
    * ext/socket/rubysocket.h (SockAddrStringValueWithAddrinfo): New macro.
      (rsock_sockaddr_string_value_with_addrinfo): New declaration.
      (rsock_addrinfo_inspect_sockaddr): Ditto.
      (rsock_sys_fail_addrinfo): Ditto.
      (rsock_sys_fail_sockaddr_or_addrinfo): Ditto.
    
    * ext/socket/raddrinfo.c (rsock_addrinfo_inspect_sockaddr): Renamed
      from addrinfo_inspect_sockaddr and exported.
      (rsock_sockaddr_string_value_with_addrinfo): New function to obtain
      string and possibly addrinfo object.
    
    * ext/socket/socket.c (rsock_sys_fail_sockaddr): Don't use
      rsock_sys_fail_host_port which is IP dependent.  Invoke
      rsock_sys_fail_addrinfo.
      (rsock_sys_fail_addrinfo): New function using
      rsock_addrinfo_inspect_sockaddr.
      (rsock_sys_fail_addrinfo_or_sockaddr): New function.
      (sock_connect): Use SockAddrStringValueWithAddrinfo and
      rsock_sys_fail_addrinfo_or_sockaddr.
      (sock_connect_nonblock): Ditto.
      (sock_bind): Ditto.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/raddrinfo.c
    trunk/ext/socket/rubysocket.h
    trunk/ext/socket/socket.c
    trunk/test/socket/test_socket.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40153)
+++ ChangeLog	(revision 40154)
@@ -1,3 +1,27 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Apr  6 14:28:23 2013  Tanaka Akira  <akr@f...>
+
+	* ext/socket/rubysocket.h (SockAddrStringValueWithAddrinfo): New macro.
+	  (rsock_sockaddr_string_value_with_addrinfo): New declaration.
+	  (rsock_addrinfo_inspect_sockaddr): Ditto.
+	  (rsock_sys_fail_addrinfo): Ditto.
+	  (rsock_sys_fail_sockaddr_or_addrinfo): Ditto.
+
+	* ext/socket/raddrinfo.c (rsock_addrinfo_inspect_sockaddr): Renamed
+	  from addrinfo_inspect_sockaddr and exported.
+	  (rsock_sockaddr_string_value_with_addrinfo): New function to obtain
+	  string and possibly addrinfo object.
+
+	* ext/socket/socket.c (rsock_sys_fail_sockaddr): Don't use
+	  rsock_sys_fail_host_port which is IP dependent.  Invoke
+	  rsock_sys_fail_addrinfo.
+	  (rsock_sys_fail_addrinfo): New function using
+	  rsock_addrinfo_inspect_sockaddr.
+	  (rsock_sys_fail_addrinfo_or_sockaddr): New function.
+	  (sock_connect): Use SockAddrStringValueWithAddrinfo and
+	  rsock_sys_fail_addrinfo_or_sockaddr.
+	  (sock_connect_nonblock): Ditto.
+	  (sock_bind): Ditto.
+
 Sat Apr  6 13:34:20 2013  Tanaka Akira  <akr@f...>
 
 	* ext/socket/socket.c (rsock_sys_fail_sockaddr): Delete 2nd argument.
Index: ext/socket/rubysocket.h
===================================================================
--- ext/socket/rubysocket.h	(revision 40153)
+++ ext/socket/rubysocket.h	(revision 40154)
@@ -227,8 +227,11 @@ int Rconnect(); https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L227
 
 #define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
 #define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
+#define SockAddrStringValueWithAddrinfo(v, rai_ret) rsock_sockaddr_string_value_with_addrinfo(&(v), &(rai_ret))
 VALUE rsock_sockaddr_string_value(volatile VALUE *);
 char *rsock_sockaddr_string_value_ptr(volatile VALUE *);
+VALUE rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *ai_ret);
+
 VALUE rb_check_sockaddr_string_type(VALUE);
 
 NORETURN(void rsock_raise_socket_error(const char *, int));
@@ -250,6 +253,7 @@ VALUE rsock_fd_socket_addrinfo(int fd, s https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L253
 VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
 
 VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname);
+VALUE rsock_addrinfo_inspect_sockaddr(VALUE rai);
 
 VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen);
 VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup);
@@ -335,6 +339,8 @@ void rsock_init_socket_init(void); https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L339
 
 NORETURN(void rsock_sys_fail_host_port(const char *, VALUE, VALUE));
 NORETURN(void rsock_sys_fail_path(const char *, VALUE));
-NORETURN(void rsock_sys_fail_sockaddr(const char *, VALUE));
+NORETURN(void rsock_sys_fail_sockaddr(const char *, VALUE addr));
+NORETURN(void rsock_sys_fail_addrinfo(const char *, VALUE rai));
+NORETURN(void rsock_sys_fail_sockaddr_or_addrinfo(const char *, VALUE addr, VALUE rai));
 
 #endif
Index: ext/socket/raddrinfo.c
===================================================================
--- ext/socket/raddrinfo.c	(revision 40153)
+++ ext/socket/raddrinfo.c	(revision 40154)
@@ -1158,8 +1158,8 @@ addrinfo_inspect(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1158
  *   Addrinfo.unix("/tmp/sock").inspect_sockaddr        #=> "/tmp/sock"
  *
  */
-static VALUE
-addrinfo_inspect_sockaddr(VALUE self)
+VALUE
+rsock_addrinfo_inspect_sockaddr(VALUE self)
 {
     return inspect_sockaddr(self, rb_str_new("", 0));
 }
@@ -2127,6 +2127,19 @@ rsock_sockaddr_string_value(volatile VAL https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L2127
     return *v;
 }
 
+VALUE
+rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *rai_ret)
+{
+    VALUE val = *v;
+    *rai_ret = Qnil;
+    if (IS_ADDRINFO(val)) {
+        *v = addrinfo_to_sockaddr(val);
+        *rai_ret = val;
+    }
+    StringValue(*v);
+    return *v;
+}
+
 char *
 rsock_sockaddr_string_value_ptr(volatile VALUE *v)
 {
@@ -2198,7 +2211,7 @@ rsock_init_addrinfo(void) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L2211
     rb_define_alloc_func(rb_cAddrinfo, addrinfo_s_allocate);
     rb_define_method(rb_cAddrinfo, "initialize", addrinfo_initialize, -1);
     rb_define_method(rb_cAddrinfo, "inspect", addrinfo_inspect, 0);
-    rb_define_method(rb_cAddrinfo, "inspect_sockaddr", addrinfo_inspect_sockaddr, 0);
+    rb_define_method(rb_cAddrinfo, "inspect_sockaddr", rsock_addrinfo_inspect_sockaddr, 0);
     rb_define_singleton_method(rb_cAddrinfo, "getaddrinfo", addrinfo_s_getaddrinfo, -1);
     rb_define_singleton_method(rb_cAddrinfo, "ip", addrinfo_s_ip, 1);
     rb_define_singleton_method(rb_cAddrinfo, "tcp", addrinfo_s_tcp, 2);
Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c	(revision 40153)
+++ ext/socket/socket.c	(revision 40154)
@@ -49,11 +49,34 @@ rsock_sys_fail_path(const char *mesg, VA https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L49
 void
 rsock_sys_fail_sockaddr(const char *mesg, VALUE addr)
 {
-    VALUE host_port = sock_s_unpack_sockaddr_in(rb_cSocket, addr);
+    VALUE rai;
 
-    rsock_sys_fail_host_port(mesg,
-	    RARRAY_PTR(host_port)[1],
-	    RARRAY_PTR(host_port)[0]);
+    rai = rsock_addrinfo_new(
+            (struct sockaddr *)RSTRING_PTR(addr),
+            RSTRING_LEN(addr),
+            PF_UNSPEC, 0, 0, Qnil, Qnil);
+
+    rsock_sys_fail_addrinfo(mesg, rai);
+}
+
+void
+rsock_sys_fail_addrinfo(const char *mesg, VALUE rai)
+{
+    VALUE str, message;
+
+    str = rsock_addrinfo_inspect_sockaddr(rai);
+    message = rb_sprintf("%s for %s", mesg, StringValueCStr(str));
+
+    rb_sys_fail_str(message);
+}
+
+void
+rsock_sys_fail_addrinfo_or_sockaddr(const char *mesg, VALUE addr, VALUE rai)
+{
+    if (NIL_P(rai))
+        rsock_sys_fail_sockaddr(mesg, addr);
+    else
+        rsock_sys_fail_addrinfo(mesg, rai);
 }
 
 static void
@@ -350,16 +373,17 @@ rsock_sock_s_socketpair(int argc, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L373
 static VALUE
 sock_connect(VALUE sock, VALUE addr)
 {
+    VALUE rai;
     rb_io_t *fptr;
     int fd, n;
 
-    SockAddrStringValue(addr);
+    SockAddrStringValueWithAddrinfo(addr, rai);
     addr = rb_str_new4(addr);
     GetOpenFile(sock, fptr);
     fd = fptr->fd;
     n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr), 0);
     if (n < 0) {
-	rsock_sys_fail_sockaddr("connect(2)", addr);
+	rsock_sys_fail_addrinfo_or_sockaddr("connect(2)", addr, rai);
     }
 
     return INT2FIX(n);
@@ -410,10 +434,11 @@ sock_connect(VALUE sock, VALUE addr) https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L434
 static VALUE
 sock_connect_nonblock(VALUE sock, VALUE addr)
 {
+    VALUE rai;
     rb_io_t *fptr;
     int n;
 
-    SockAddrStringValue(addr);
+    SockAddrStringValueWithAddrinfo(addr, rai);
     addr = rb_str_new4(addr);
     GetOpenFile(sock, fptr);
     rb_io_set_nonblock(fptr);
@@ -421,7 +446,7 @@ sock_connect_nonblock(VALUE sock, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L446
     if (n < 0) {
         if (errno == EINPROGRESS)
             rb_mod_sys_fail(rb_mWaitWritable, "connect(2) would block");
-	rsock_sys_fail_sockaddr("connect(2)", addr);
+	rsock_sys_fail_addrinfo_or_sockaddr("connect(2)", addr, rai);
     }
 
     return INT2FIX(n);
@@ -516,12 +541,13 @@ sock_connect_nonblock(VALUE sock, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L541
 static VALUE
 sock_bind(VALUE sock, VALUE addr)
 {
+    VALUE rai;
     rb_io_t *fptr;
 
-    SockAddrStringValue(addr);
+    SockAddrStringValueWithAddrinfo(addr, rai);
     GetOpenFile(sock, fptr);
     if (bind(fptr->fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr)) < 0)
-	rsock_sys_fail_sockaddr("bind(2)", addr);
+	rsock_sys_fail_addrinfo_or_sockaddr("bind(2)", addr, rai);
 
     return INT2FIX(0);
 }
Index: test/socket/test_socket.rb
===================================================================
--- test/socket/test_socket.rb	(revision 40153)
+++ test/socket/test_socket.rb	(revision 40154)
@@ -81,7 +81,7 @@ class TestSocket < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/socket/test_socket.rb#L81
           s.bind(Socket.sockaddr_in(port, "127.0.0.1"))
         end
 
-        assert_match "bind(2) for \"127.0.0.1\" port #{port}", e.message
+        assert_match "bind(2) for 127.0.0.1:#{port}", e.message
       }
     }
   end

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

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