ruby-changes:10564
From: akr <ko1@a...>
Date: Sun, 8 Feb 2009 13:53:28 +0900 (JST)
Subject: [ruby-changes:10564] Ruby:r22120 (trunk): * ext/socket/raddrinfo.c (init_unix_addrinfo): add socktype argument.
akr 2009-02-08 13:53:17 +0900 (Sun, 08 Feb 2009) New Revision: 22120 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22120 Log: * ext/socket/raddrinfo.c (init_unix_addrinfo): add socktype argument. (addrinfo_initialize): follow init_unix_addrinfo change. (addrinfo_s_unix): add optional argument: socktype Modified files: trunk/ChangeLog trunk/ext/socket/raddrinfo.c trunk/test/socket/test_addrinfo.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 22119) +++ ChangeLog (revision 22120) @@ -1,3 +1,9 @@ +Sun Feb 8 13:52:02 2009 Tanaka Akira <akr@f...> + + * ext/socket/raddrinfo.c (init_unix_addrinfo): add socktype argument. + (addrinfo_initialize): follow init_unix_addrinfo change. + (addrinfo_s_unix): add optional argument: socktype + Sun Feb 8 13:09:32 2009 Nobuyoshi Nakada <nobu@r...> * configure.in (RUBY_APPEND_OPTION, RUBY_APPEND_OPTIONS), Index: ext/socket/raddrinfo.c =================================================================== --- ext/socket/raddrinfo.c (revision 22119) +++ ext/socket/raddrinfo.c (revision 22120) @@ -719,7 +719,7 @@ #ifdef HAVE_SYS_UN_H static void -init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path) +init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype) { struct sockaddr_un un; @@ -734,7 +734,7 @@ un.sun_family = AF_UNIX; memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); - init_addrinfo(rai, (struct sockaddr *)&un, sizeof(un), AF_UNIX, SOCK_STREAM, 0, Qnil, Qnil); + init_addrinfo(rai, (struct sockaddr *)&un, sizeof(un), PF_UNIX, socktype, 0, Qnil, Qnil); } #endif @@ -843,7 +843,7 @@ { VALUE path = rb_ary_entry(sockaddr_ary, 1); StringValue(path); - init_unix_addrinfo(rai, path); + init_unix_addrinfo(rai, path, SOCK_STREAM); break; } #endif @@ -2002,21 +2002,33 @@ /* * call-seq: - * Addrinfo.unix(path) => addrinfo + * Addrinfo.unix(path [, socktype]) => addrinfo * * returns an addrinfo object for UNIX socket address. * - * Addrinfo.unix("/tmp/sock") #=> #<Addrinfo: /tmp/sock SOCK_STREAM> + * _socktype_ specifies the socket type. + * If it is omitted, :STREAM is used. + * + * Addrinfo.unix("/tmp/sock") #=> #<Addrinfo: /tmp/sock SOCK_STREAM> + * Addrinfo.unix("/tmp/sock", :DGRAM) #=> #<Addrinfo: /tmp/sock SOCK_DGRAM> */ static VALUE -addrinfo_s_unix(VALUE self, VALUE path) +addrinfo_s_unix(int argc, VALUE *argv, VALUE self) { - VALUE addr; + VALUE path, vsocktype, addr; + int socktype; rb_addrinfo_t *rai; + rb_scan_args(argc, argv, "11", &path, &vsocktype); + + if (NIL_P(vsocktype)) + socktype = SOCK_STREAM; + else + socktype = socktype_arg(vsocktype); + addr = addrinfo_s_allocate(rb_cAddrinfo); DATA_PTR(addr) = rai = alloc_addrinfo(); - init_unix_addrinfo(rai, path); + init_unix_addrinfo(rai, path, socktype); OBJ_INFECT(addr, path); return addr; } @@ -2105,7 +2117,7 @@ rb_define_singleton_method(rb_cAddrinfo, "tcp", addrinfo_s_tcp, 2); rb_define_singleton_method(rb_cAddrinfo, "udp", addrinfo_s_udp, 2); #ifdef HAVE_SYS_UN_H - rb_define_singleton_method(rb_cAddrinfo, "unix", addrinfo_s_unix, 1); + rb_define_singleton_method(rb_cAddrinfo, "unix", addrinfo_s_unix, -1); #endif rb_define_method(rb_cAddrinfo, "afamily", addrinfo_afamily, 0); Index: test/socket/test_addrinfo.rb =================================================================== --- test/socket/test_addrinfo.rb (revision 22119) +++ test/socket/test_addrinfo.rb (revision 22120) @@ -563,6 +563,15 @@ assert_equal(0, ai.protocol) end + def test_addrinfo_unix_dgram + ai = Addrinfo.unix("/tmp/sock", :DGRAM) + assert_equal("/tmp/sock", Socket.unpack_sockaddr_un(ai)) + assert_equal(Socket::AF_UNIX, ai.afamily) + assert_equal(Socket::PF_UNIX, ai.pfamily) + assert_equal(Socket::SOCK_DGRAM, ai.socktype) + assert_equal(0, ai.protocol) + end + def test_addrinfo_unix_path ai = Addrinfo.unix("/tmp/sock1") assert_equal("/tmp/sock1", ai.unix_path) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/