diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-15 17:06:37 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-01-15 17:06:37 +0000 |
commit | aaddab6dea1381b5491121b6b36c21d65fadcf56 (patch) | |
tree | 43371c9b0c7118c451af1af3f8ccf08990941fd1 | |
parent | d9395fc46630c113204cece80781f31373b5a818 (diff) | |
download | ruby-aaddab6dea1381b5491121b6b36c21d65fadcf56.tar.gz |
* ext/socket/socket.c (addrinfo_s_ip): new method AddrInfo.ip.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21579 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | ext/socket/socket.c | 26 | ||||
-rw-r--r-- | test/socket/test_addrinfo.rb | 9 |
3 files changed, 37 insertions, 2 deletions
@@ -1,3 +1,7 @@ +Fri Jan 16 02:05:55 2009 Tanaka Akira <akr@fsij.org> + + * ext/socket/socket.c (addrinfo_s_ip): new method AddrInfo.ip. + Fri Jan 16 01:42:50 2009 Koichi Sasada <ko1@atdot.net> * thread.c (call_trace_proc): as Matz said ([ruby-core:21183]), diff --git a/ext/socket/socket.c b/ext/socket/socket.c index cd6acd4647..f361e49ba7 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -5362,6 +5362,27 @@ addrinfo_s_getaddrinfo(int argc, VALUE *argv, VALUE self) /* * call-seq: + * AddrInfo.ip(host) => addrinfo + * + * returns an addrinfo object for IP address. + * + * AddrInfo.ip("localhost") #=> #<AddrInfo: 127.0.0.1 (localhost)> + */ +static VALUE +addrinfo_s_ip(VALUE self, VALUE host) +{ + VALUE ret; + rb_addrinfo_t *rai; + ret = addrinfo_firstonly_new(host, Qnil, + INT2NUM(PF_UNSPEC), INT2FIX(0), INT2FIX(0), INT2FIX(0)); + rai = get_addrinfo(ret); + rai->socktype = 0; + rai->protocol = 0; + return ret; +} + +/* + * call-seq: * AddrInfo.tcp(host, port) => addrinfo * * returns an addrinfo object for TCP address. @@ -5372,7 +5393,7 @@ static VALUE addrinfo_s_tcp(VALUE self, VALUE host, VALUE port) { return addrinfo_firstonly_new(host, port, - INT2NUM(PF_UNSPEC), INT2NUM(SOCK_STREAM), INT2FIX(IPPROTO_TCP), INT2FIX(0)); + INT2NUM(PF_UNSPEC), INT2NUM(SOCK_STREAM), INT2NUM(IPPROTO_TCP), INT2FIX(0)); } /* @@ -5387,7 +5408,7 @@ static VALUE addrinfo_s_udp(VALUE self, VALUE host, VALUE port) { return addrinfo_firstonly_new(host, port, - INT2NUM(PF_UNSPEC), INT2NUM(SOCK_DGRAM), INT2FIX(IPPROTO_UDP), INT2FIX(0)); + INT2NUM(PF_UNSPEC), INT2NUM(SOCK_DGRAM), INT2NUM(IPPROTO_UDP), INT2FIX(0)); } #ifdef HAVE_SYS_UN_H @@ -5592,6 +5613,7 @@ Init_socket() rb_define_method(rb_cAddrInfo, "initialize", addrinfo_initialize, -1); rb_define_method(rb_cAddrInfo, "inspect", addrinfo_inspect, 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); rb_define_singleton_method(rb_cAddrInfo, "udp", addrinfo_s_udp, 2); #ifdef HAVE_SYS_UN_H diff --git a/test/socket/test_addrinfo.rb b/test/socket/test_addrinfo.rb index d205a7671a..519279c77d 100644 --- a/test/socket/test_addrinfo.rb +++ b/test/socket/test_addrinfo.rb @@ -7,6 +7,15 @@ require "test/unit" require "tempfile" class TestSocketAddrInfo < Test::Unit::TestCase + def test_addrinfo_ip + ai = AddrInfo.ip("127.0.0.1") + assert_equal([0, "127.0.0.1"], Socket.unpack_sockaddr_in(ai)) + assert_equal(Socket::AF_INET, ai.afamily) + assert_equal(Socket::PF_INET, ai.pfamily) + assert_equal(0, ai.socktype) + assert_equal(0, ai.protocol) + end + def test_addrinfo_tcp ai = AddrInfo.tcp("127.0.0.1", 80) assert_equal([80, "127.0.0.1"], Socket.unpack_sockaddr_in(ai)) |