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

ruby-changes:27187

From: akr <ko1@a...>
Date: Thu, 14 Feb 2013 20:32:49 +0900 (JST)
Subject: [ruby-changes:27187] akr:r39239 (trunk): * ext/socket: always operate length of socket addess companion with

akr	2013-02-14 20:28:49 +0900 (Thu, 14 Feb 2013)

  New Revision: 39239

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

  Log:
    * ext/socket: always operate length of socket addess companion with
      socket address. 
    
    * ext/socket/rubysocket.h (rsock_make_ipaddr): add an argument for
      socket address length.
      (rsock_ipaddr): ditto.
    
    * ext/socket/ipsocket.c (ip_addr): pass length to rsock_ipaddr.
      (ip_peeraddr): ditto.
      (ip_s_getaddress): pass length to rsock_make_ipaddr.
    
    * ext/socket/socket.c (make_addrinfo): pass length to rsock_ipaddr.
      (sock_s_getnameinfo): pass actual address length to rb_getnameinfo.
      (sock_s_unpack_sockaddr_in): pass length to rsock_make_ipaddr.
      
    * ext/socket/init.c (rsock_s_recvfrom): pass length to rsock_ipaddr.
      (rsock_s_recvfrom_nonblock): ditto.
    
    * ext/socket/tcpsocket.c (tcp_sockaddr): pass length to
      rsock_make_ipaddr.
    
    * ext/socket/raddrinfo.c (make_ipaddr0): add an argument for socket
      address length.  pass the length to rb_getnameinfo.
      (rsock_ipaddr): ditto.
      (rsock_make_ipaddr): add an argument for socket address length. 
      pass the length to make_ipaddr0.
      (make_inetaddr): pass length to make_ipaddr0.
      a local variable renamed.
      (host_str): a local variable renamed.
      (port_str): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/init.c
    trunk/ext/socket/ipsocket.c
    trunk/ext/socket/raddrinfo.c
    trunk/ext/socket/rubysocket.h
    trunk/ext/socket/socket.c
    trunk/ext/socket/tcpsocket.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39238)
+++ ChangeLog	(revision 39239)
@@ -1,3 +1,36 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Feb 14 20:11:23 2013  Tanaka Akira  <akr@f...>
+
+	* ext/socket: always operate length of socket addess companion with
+	  socket address. 
+
+	* ext/socket/rubysocket.h (rsock_make_ipaddr): add an argument for
+	  socket address length.
+	  (rsock_ipaddr): ditto.
+
+	* ext/socket/ipsocket.c (ip_addr): pass length to rsock_ipaddr.
+	  (ip_peeraddr): ditto.
+	  (ip_s_getaddress): pass length to rsock_make_ipaddr.
+
+	* ext/socket/socket.c (make_addrinfo): pass length to rsock_ipaddr.
+	  (sock_s_getnameinfo): pass actual address length to rb_getnameinfo.
+	  (sock_s_unpack_sockaddr_in): pass length to rsock_make_ipaddr.
+	  
+	* ext/socket/init.c (rsock_s_recvfrom): pass length to rsock_ipaddr.
+	  (rsock_s_recvfrom_nonblock): ditto.
+
+	* ext/socket/tcpsocket.c (tcp_sockaddr): pass length to
+	  rsock_make_ipaddr.
+
+	* ext/socket/raddrinfo.c (make_ipaddr0): add an argument for socket
+	  address length.  pass the length to rb_getnameinfo.
+	  (rsock_ipaddr): ditto.
+	  (rsock_make_ipaddr): add an argument for socket address length. 
+	  pass the length to make_ipaddr0.
+	  (make_inetaddr): pass length to make_ipaddr0.
+	  a local variable renamed.
+	  (host_str): a local variable renamed.
+	  (port_str): ditto.
+
 Thu Feb 14 14:31:43 2013  Eric Hodel  <drbrain@s...>
 
 	* lib/net/http.rb:  Removed OpenSSL dependency from Net::HTTP.
Index: ext/socket/tcpsocket.c
===================================================================
--- ext/socket/tcpsocket.c	(revision 39238)
+++ ext/socket/tcpsocket.c	(revision 39239)
@@ -34,7 +34,7 @@ tcp_init(int argc, VALUE *argv, VALUE so https://github.com/ruby/ruby/blob/trunk/ext/socket/tcpsocket.c#L34
 static VALUE
 tcp_sockaddr(struct sockaddr *addr, size_t len)
 {
-    return rsock_make_ipaddr(addr);
+    return rsock_make_ipaddr(addr, len);
 }
 
 /*
Index: ext/socket/rubysocket.h
===================================================================
--- ext/socket/rubysocket.h	(revision 39238)
+++ ext/socket/rubysocket.h	(revision 39239)
@@ -235,8 +235,8 @@ VALUE rsock_io_socket_addrinfo(VALUE io, https://github.com/ruby/ruby/blob/trunk/ext/socket/rubysocket.h#L235
 
 VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname);
 
-VALUE rsock_make_ipaddr(struct sockaddr *addr);
-VALUE rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup);
+VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen);
+VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup);
 VALUE rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, size_t));
 
 int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
Index: ext/socket/init.c
===================================================================
--- ext/socket/init.c	(revision 39238)
+++ ext/socket/init.c	(revision 39239)
@@ -155,7 +155,7 @@ rsock_s_recvfrom(VALUE sock, int argc, V https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L155
 	}
 #endif
 	if (arg.alen && arg.alen != sizeof(arg.buf)) /* OSX doesn't return a from result for connection-oriented sockets */
-	    return rb_assoc_new(str, rsock_ipaddr((struct sockaddr*)&arg.buf, fptr->mode & FMODE_NOREVLOOKUP));
+	    return rb_assoc_new(str, rsock_ipaddr((struct sockaddr*)&arg.buf, arg.alen, fptr->mode & FMODE_NOREVLOOKUP));
 	else
 	    return rb_assoc_new(str, Qnil);
 
@@ -227,7 +227,7 @@ rsock_s_recvfrom_nonblock(VALUE sock, in https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L227
 
       case RECV_IP:
         if (alen && alen != sizeof(buf)) /* connection-oriented socket may not return a from result */
-            addr = rsock_ipaddr((struct sockaddr*)&buf, fptr->mode & FMODE_NOREVLOOKUP);
+            addr = rsock_ipaddr((struct sockaddr*)&buf, alen, fptr->mode & FMODE_NOREVLOOKUP);
         break;
 
       case RECV_SOCKET:
Index: ext/socket/raddrinfo.c
===================================================================
--- ext/socket/raddrinfo.c	(revision 39238)
+++ ext/socket/raddrinfo.c	(revision 39239)
@@ -229,27 +229,27 @@ rb_getnameinfo(const struct sockaddr *sa https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L229
 }
 
 static void
-make_ipaddr0(struct sockaddr *addr, char *buf, size_t len)
+make_ipaddr0(struct sockaddr *addr, socklen_t addrlen, char *buf, size_t buflen)
 {
     int error;
 
-    error = rb_getnameinfo(addr, SA_LEN(addr), buf, len, NULL, 0, NI_NUMERICHOST);
+    error = rb_getnameinfo(addr, addrlen, buf, buflen, NULL, 0, NI_NUMERICHOST);
     if (error) {
         rsock_raise_socket_error("getnameinfo", error);
     }
 }
 
 VALUE
-rsock_make_ipaddr(struct sockaddr *addr)
+rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
 {
     char hbuf[1024];
 
-    make_ipaddr0(addr, hbuf, sizeof(hbuf));
+    make_ipaddr0(addr, addrlen, hbuf, sizeof(hbuf));
     return rb_str_new2(hbuf);
 }
 
 static void
-make_inetaddr(unsigned int host, char *buf, size_t len)
+make_inetaddr(unsigned int host, char *buf, size_t buflen)
 {
     struct sockaddr_in sin;
 
@@ -257,7 +257,7 @@ make_inetaddr(unsigned int host, char *b https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L257
     sin.sin_family = AF_INET;
     SET_SIN_LEN(&sin, sizeof(sin));
     sin.sin_addr.s_addr = host;
-    make_ipaddr0((struct sockaddr*)&sin, buf, len);
+    make_ipaddr0((struct sockaddr*)&sin, sizeof(sin), buf, buflen);
 }
 
 static int
@@ -276,7 +276,7 @@ str_is_number(const char *p) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L276
 }
 
 static char*
-host_str(VALUE host, char *hbuf, size_t len, int *flags_ptr)
+host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr)
 {
     if (NIL_P(host)) {
         return NULL;
@@ -284,7 +284,7 @@ host_str(VALUE host, char *hbuf, size_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L284
     else if (rb_obj_is_kind_of(host, rb_cInteger)) {
         unsigned int i = NUM2UINT(host);
 
-        make_inetaddr(htonl(i), hbuf, len);
+        make_inetaddr(htonl(i), hbuf, hbuflen);
         if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
         return hbuf;
     }
@@ -294,14 +294,14 @@ host_str(VALUE host, char *hbuf, size_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L294
         SafeStringValue(host);
         name = RSTRING_PTR(host);
         if (!name || *name == 0 || (name[0] == '<' && strcmp(name, "<any>") == 0)) {
-            make_inetaddr(INADDR_ANY, hbuf, len);
+            make_inetaddr(INADDR_ANY, hbuf, hbuflen);
             if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
         }
         else if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
-            make_inetaddr(INADDR_BROADCAST, hbuf, len);
+            make_inetaddr(INADDR_BROADCAST, hbuf, hbuflen);
             if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
         }
-        else if (strlen(name) >= len) {
+        else if (strlen(name) >= hbuflen) {
             rb_raise(rb_eArgError, "hostname too long (%"PRIuSIZE")",
                 strlen(name));
         }
@@ -313,13 +313,13 @@ host_str(VALUE host, char *hbuf, size_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L313
 }
 
 static char*
-port_str(VALUE port, char *pbuf, size_t len, int *flags_ptr)
+port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr)
 {
     if (NIL_P(port)) {
         return 0;
     }
     else if (FIXNUM_P(port)) {
-        snprintf(pbuf, len, "%ld", FIX2LONG(port));
+        snprintf(pbuf, pbuflen, "%ld", FIX2LONG(port));
 #ifdef AI_NUMERICSERV
         if (flags_ptr) *flags_ptr |= AI_NUMERICSERV;
 #endif
@@ -330,7 +330,7 @@ port_str(VALUE port, char *pbuf, size_t https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L330
 
         SafeStringValue(port);
         serv = RSTRING_PTR(port);
-        if (strlen(serv) >= len) {
+        if (strlen(serv) >= pbuflen) {
             rb_raise(rb_eArgError, "service name too long (%"PRIuSIZE")",
                 strlen(serv));
         }
@@ -380,7 +380,7 @@ rsock_addrinfo(VALUE host, VALUE port, i https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L380
 }
 
 VALUE
-rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup)
+rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
 {
     VALUE family, port, addr1, addr2;
     VALUE ary;
@@ -399,13 +399,13 @@ rsock_ipaddr(struct sockaddr *sockaddr, https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L399
 
     addr1 = Qnil;
     if (!norevlookup) {
-        error = rb_getnameinfo(sockaddr, SA_LEN(sockaddr), hbuf, sizeof(hbuf),
+        error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
                                NULL, 0, 0);
         if (! error) {
             addr1 = rb_str_new2(hbuf);
         }
     }
-    error = rb_getnameinfo(sockaddr, SA_LEN(sockaddr), hbuf, sizeof(hbuf),
+    error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
                            pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV);
     if (error) {
         rsock_raise_socket_error("getnameinfo", error);
Index: ext/socket/ipsocket.c
===================================================================
--- ext/socket/ipsocket.c	(revision 39238)
+++ ext/socket/ipsocket.c	(revision 39239)
@@ -194,7 +194,7 @@ ip_addr(int argc, VALUE *argv, VALUE soc https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L194
 	norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
     if (getsockname(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
 	rb_sys_fail("getsockname(2)");
-    return rsock_ipaddr((struct sockaddr*)&addr, norevlookup);
+    return rsock_ipaddr((struct sockaddr*)&addr, len, norevlookup);
 }
 
 /*
@@ -235,7 +235,7 @@ ip_peeraddr(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L235
 	norevlookup = fptr->mode & FMODE_NOREVLOOKUP;
     if (getpeername(fptr->fd, (struct sockaddr*)&addr, &len) < 0)
 	rb_sys_fail("getpeername(2)");
-    return rsock_ipaddr((struct sockaddr*)&addr, norevlookup);
+    return rsock_ipaddr((struct sockaddr*)&addr, len, norevlookup);
 }
 
 /*
@@ -285,7 +285,7 @@ ip_s_getaddress(VALUE obj, VALUE host) https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L285
     memcpy(&addr, res->ai_addr, res->ai_addrlen);
     freeaddrinfo(res);
 
-    return rsock_make_ipaddr((struct sockaddr*)&addr);
+    return rsock_make_ipaddr((struct sockaddr*)&addr, res->ai_addrlen);
 }
 
 void
Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c	(revision 39238)
+++ ext/socket/socket.c	(revision 39239)
@@ -937,7 +937,7 @@ make_addrinfo(struct addrinfo *res0, int https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L937
     }
     base = rb_ary_new();
     for (res = res0; res; res = res->ai_next) {
-	ary = rsock_ipaddr(res->ai_addr, norevlookup);
+	ary = rsock_ipaddr(res->ai_addr, res->ai_addrlen, norevlookup);
 	if (res->ai_canonname) {
 	    RARRAY_PTR(ary)[2] = rb_str_new2(res->ai_canonname);
 	}
@@ -1219,6 +1219,7 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1219
     int error;
     struct sockaddr_storage ss;
     struct sockaddr *sap;
+    socklen_t salen;
 
     sa = flags = Qnil;
     rb_scan_args(argc, argv, "11", &sa, &flags);
@@ -1238,6 +1239,7 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1239
 	    rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
 	}
 	sap = (struct sockaddr*)&ss;
+        salen = RSTRING_LEN(sa);
 	goto call_nameinfo;
     }
     tmp = rb_check_array_type(sa);
@@ -1299,13 +1301,14 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1301
 	error = rb_getaddrinfo(hptr, pptr, &hints, &res);
 	if (error) goto error_exit_addr;
 	sap = res->ai_addr;
+        salen = res->ai_addrlen;
     }
     else {
 	rb_raise(rb_eTypeError, "expecting String or Array");
     }
 
   call_nameinfo:
-    error = rb_getnameinfo(sap, SA_LEN(sap), hbuf, sizeof(hbuf),
+    error = rb_getnameinfo(sap, salen, hbuf, sizeof(hbuf),
 			   pbuf, sizeof(pbuf), fl);
     if (error) goto error_exit_name;
     if (res) {
@@ -1313,7 +1316,8 @@ sock_s_getnameinfo(int argc, VALUE *argv https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1316
 	    char hbuf2[1024], pbuf2[1024];
 
 	    sap = r->ai_addr;
-	    error = rb_getnameinfo(sap, SA_LEN(sap), hbuf2, sizeof(hbuf2),
+            salen = r->ai_addrlen;
+	    error = rb_getnameinfo(sap, salen, hbuf2, sizeof(hbuf2),
 				   pbuf2, sizeof(pbuf2), fl);
 	    if (error) goto error_exit_name;
 	    if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
@@ -1399,7 +1403,7 @@ sock_s_unpack_sockaddr_in(VALUE self, VA https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1403
         rb_raise(rb_eArgError, "not an AF_INET sockaddr");
 #endif
     }
-    host = rsock_make_ipaddr((struct sockaddr*)sockaddr);
+    host = rsock_make_ipaddr((struct sockaddr*)sockaddr, RSTRING_LEN(addr));
     OBJ_INFECT(host, addr);
     return rb_assoc_new(INT2NUM(ntohs(sockaddr->sin_port)), host);
 }

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

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