ruby-changes:27208
From: akr <ko1@a...>
Date: Sat, 16 Feb 2013 07:45:26 +0900 (JST)
Subject: [ruby-changes:27208] akr:r39260 (trunk): * ext/socket/extconf.rb: test struct sockaddr_un and its member,
akr 2013-02-16 07:19:05 +0900 (Sat, 16 Feb 2013) New Revision: 39260 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39260 Log: * ext/socket/extconf.rb: test struct sockaddr_un and its member, sun_len. * ext/socket/sockport.h (INIT_SOCKADDR_UN): new macro defined. * ext/socket/socket.c (sock_s_pack_sockaddr_un): use INIT_SOCKADDR_UN. * ext/socket/unixsocket.c (rsock_init_unixsock): ditto. * ext/socket/raddrinfo.c (init_unix_addrinfo): ditto. (addrinfo_mload): ditto. Modified files: trunk/ChangeLog trunk/ext/socket/extconf.rb trunk/ext/socket/raddrinfo.c trunk/ext/socket/socket.c trunk/ext/socket/sockport.h trunk/ext/socket/unixsocket.c Index: ChangeLog =================================================================== --- ChangeLog (revision 39259) +++ ChangeLog (revision 39260) @@ -1,3 +1,17 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Feb 16 07:16:49 2013 Tanaka Akira <akr@f...> + + * ext/socket/extconf.rb: test struct sockaddr_un and its member, + sun_len. + + * ext/socket/sockport.h (INIT_SOCKADDR_UN): new macro defined. + + * ext/socket/socket.c (sock_s_pack_sockaddr_un): use INIT_SOCKADDR_UN. + + * ext/socket/unixsocket.c (rsock_init_unixsock): ditto. + + * ext/socket/raddrinfo.c (init_unix_addrinfo): ditto. + (addrinfo_mload): ditto. + Sat Feb 16 07:05:59 2013 Tanaka Akira <akr@f...> * ext/socket/sockport.h (INIT_SOCKADDR_IN): don't need family Index: ext/socket/raddrinfo.c =================================================================== --- ext/socket/raddrinfo.c (revision 39259) +++ ext/socket/raddrinfo.c (revision 39260) @@ -797,9 +797,7 @@ init_unix_addrinfo(rb_addrinfo_t *rai, V https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L797 "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", (size_t)RSTRING_LEN(path), sizeof(un.sun_path)); - MEMZERO(&un, struct sockaddr_un, 1); - - un.sun_family = AF_UNIX; + INIT_SOCKADDR_UN(&un, sizeof(struct sockaddr_un)); memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); len = rsock_unix_sockaddr_len(path); @@ -1317,8 +1315,7 @@ addrinfo_mload(VALUE self, VALUE ary) https://github.com/ruby/ruby/blob/trunk/ext/socket/raddrinfo.c#L1315 case AF_UNIX: { struct sockaddr_un uaddr; - MEMZERO(&uaddr, struct sockaddr_un, 1); - uaddr.sun_family = AF_UNIX; + INIT_SOCKADDR_UN(&uaddr, sizeof(struct sockaddr_un)); StringValue(v); if (sizeof(uaddr.sun_path) < (size_t)RSTRING_LEN(v)) Index: ext/socket/sockport.h =================================================================== --- ext/socket/sockport.h (revision 39259) +++ ext/socket/sockport.h (revision 39260) @@ -47,6 +47,24 @@ https://github.com/ruby/ruby/blob/trunk/ext/socket/sockport.h#L47 SET_SIN_LEN(init_sockaddr_ptr, init_sockaddr_len); \ } while (0) + +/* for strict-aliasing rule */ +#ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN +# ifdef HAVE_STRUCT_SOCKADDR_IN_SUN_LEN +# define SET_SUN_LEN(sa, len) (void)((sa)->sun_len = (len)) +# else +# define SET_SUN_LEN(sa, len) SET_SA_LEN((struct sockaddr *)(sa), (len)) +# endif +# define INIT_SOCKADDR_UN(addr, len) \ + do { \ + struct sockaddr_un *init_sockaddr_ptr = (addr); \ + socklen_t init_sockaddr_len = (len); \ + memset(init_sockaddr_ptr, 0, init_sockaddr_len); \ + init_sockaddr_ptr->sun_family = AF_UNIX; \ + SET_SUN_LEN(init_sockaddr_ptr, init_sockaddr_len); \ + } while (0) +#endif + #ifndef IN_MULTICAST # define IN_CLASSD(i) (((long)(i) & 0xf0000000) == 0xe0000000) # define IN_MULTICAST(i) IN_CLASSD(i) Index: ext/socket/extconf.rb =================================================================== --- ext/socket/extconf.rb (revision 39259) +++ ext/socket/extconf.rb (revision 39260) @@ -50,6 +50,13 @@ end https://github.com/ruby/ruby/blob/trunk/ext/socket/extconf.rb#L50 end } +have_struct_member("struct sockaddr", "sa_len", headers) # 4.4BSD +have_struct_member("struct sockaddr_in", "sin_len", headers) # 4.4BSD + +if have_type("struct sockaddr_un", headers) # POSIX + have_struct_member("struct sockaddr_un", "sun_len", headers) # 4.4BSD +end + have_type("struct sockaddr_storage", headers) have_type("struct addrinfo", headers) @@ -72,10 +79,6 @@ have_type("struct ip_mreq", headers) # 4 https://github.com/ruby/ruby/blob/trunk/ext/socket/extconf.rb#L79 have_type("struct ip_mreqn", headers) # Linux 2.4 have_type("struct ipv6_mreq", headers) # RFC 3493 -# 4.4BSD -have_struct_member("struct sockaddr", "sa_len", headers) -have_struct_member("struct sockaddr_in", "sin_len", headers) - # doug's fix, NOW add -Dss_family... only if required! doug = proc {have_struct_member("struct sockaddr_storage", "ss_family", headers)} if (doug[] or Index: ext/socket/socket.c =================================================================== --- ext/socket/socket.c (revision 39259) +++ ext/socket/socket.c (revision 39260) @@ -1427,8 +1427,7 @@ sock_s_pack_sockaddr_un(VALUE self, VALU https://github.com/ruby/ruby/blob/trunk/ext/socket/socket.c#L1427 VALUE addr; StringValue(path); - MEMZERO(&sockaddr, struct sockaddr_un, 1); - sockaddr.sun_family = AF_UNIX; + INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un)); if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) { rb_raise(rb_eArgError, "too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)", (size_t)RSTRING_LEN(path), sizeof(sockaddr.sun_path)); Index: ext/socket/unixsocket.c =================================================================== --- ext/socket/unixsocket.c (revision 39259) +++ ext/socket/unixsocket.c (revision 39260) @@ -39,8 +39,7 @@ rsock_init_unixsock(VALUE sock, VALUE pa https://github.com/ruby/ruby/blob/trunk/ext/socket/unixsocket.c#L39 rb_sys_fail("socket(2)"); } - MEMZERO(&sockaddr, struct sockaddr_un, 1); - sockaddr.sun_family = AF_UNIX; + INIT_SOCKADDR_UN(&sockaddr, sizeof(struct sockaddr_un)); if (sizeof(sockaddr.sun_path) < (size_t)RSTRING_LEN(path)) { rb_raise(rb_eArgError, "too long unix socket path (%ldbytes given but %dbytes max)", RSTRING_LEN(path), (int)sizeof(sockaddr.sun_path)); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/