1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
akr 2009-01-15 17:06:37 +00:00
parent d9395fc466
commit aaddab6dea
3 changed files with 37 additions and 2 deletions

View file

@ -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]),

View file

@ -5360,6 +5360,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
@ -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

View file

@ -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))