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

* ext/socket/raddrinfo.c (addrinfo_inspect_sockaddr): new

method AddrInfo#inspect_sockaddr.
  (inspect_sockaddr): extracted from addrinfo_inspect.
  (addrinfo_inspect): use inspect_sockaddr.
  (Init_addrinfo): define the new method.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21648 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-01-18 08:03:24 +00:00
parent d620500dda
commit db881044bc
3 changed files with 67 additions and 16 deletions

View file

@ -1,3 +1,11 @@
Sun Jan 18 16:56:46 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/raddrinfo.c (addrinfo_inspect_sockaddr): new
method AddrInfo#inspect_sockaddr.
(inspect_sockaddr): extracted from addrinfo_inspect.
(addrinfo_inspect): use inspect_sockaddr.
(Init_addrinfo): define the new method.
Sun Jan 18 16:46:37 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/raddrinfo.c (addrinfo_ip_address): new method

View file

@ -879,24 +879,10 @@ ai_get_afamily(rb_addrinfo_t *rai)
return get_afamily((struct sockaddr *)&rai->addr, rai->sockaddr_len);
}
/*
* call-seq:
* addrinfo.inspect => string
*
* returns a string which shows addrinfo in human-readable form.
*
* AddrInfo.tcp("localhost", 80).inspect #=> "#<AddrInfo: 127.0.0.1:80 TCP (localhost:80)>"
* AddrInfo.unix("/tmp/sock").inspect #=> "#<AddrInfo: /tmp/sock SOCK_STREAM>"
*
*/
static VALUE
addrinfo_inspect(VALUE self)
inspect_sockaddr(VALUE addrinfo, VALUE ret)
{
rb_addrinfo_t *rai = get_addrinfo(self);
int internet_p;
VALUE ret;
ret = rb_sprintf("#<%s: ", rb_obj_classname(self));
rb_addrinfo_t *rai = get_addrinfo(addrinfo);
if (rai->sockaddr_len == 0) {
rb_str_cat2(ret, "empty-sockaddr");
@ -1023,6 +1009,30 @@ addrinfo_inspect(VALUE self)
}
}
return ret;
}
/*
* call-seq:
* addrinfo.inspect => string
*
* returns a string which shows addrinfo in human-readable form.
*
* AddrInfo.tcp("localhost", 80).inspect #=> "#<AddrInfo: 127.0.0.1:80 TCP (localhost:80)>"
* AddrInfo.unix("/tmp/sock").inspect #=> "#<AddrInfo: /tmp/sock SOCK_STREAM>"
*
*/
static VALUE
addrinfo_inspect(VALUE self)
{
rb_addrinfo_t *rai = get_addrinfo(self);
int internet_p;
VALUE ret;
ret = rb_sprintf("#<%s: ", rb_obj_classname(self));
inspect_sockaddr(self, ret);
if (rai->pfamily && ai_get_afamily(rai) != rai->pfamily) {
ID id = intern_protocol_family(rai->pfamily);
if (id)
@ -1081,6 +1091,23 @@ addrinfo_inspect(VALUE self)
return ret;
}
/*
* call-seq:
* addrinfo.inspect_sockaddr => string
*
* returns a string which shows the sockaddr in _addrinfo_ with human-readable form.
*
* AddrInfo.tcp("localhost", 80).inspect_sockaddr #=> "127.0.0.1:80"
* AddrInfo.tcp("ip6-localhost", 80).inspect_sockaddr #=> "[::1]:80"
* AddrInfo.unix("/tmp/sock").inspect_sockaddr #=> "/tmp/sock"
*
*/
static VALUE
addrinfo_inspect_sockaddr(VALUE self)
{
return inspect_sockaddr(self, rb_str_new("", 0));
}
/* :nodoc: */
static VALUE
addrinfo_mdump(VALUE self)
@ -1816,6 +1843,7 @@ Init_addrinfo(void)
rb_define_alloc_func(rb_cAddrInfo, addrinfo_s_allocate);
rb_define_method(rb_cAddrInfo, "initialize", addrinfo_initialize, -1);
rb_define_method(rb_cAddrInfo, "inspect", addrinfo_inspect, 0);
rb_define_method(rb_cAddrInfo, "inspect_sockaddr", addrinfo_inspect_sockaddr, 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);

View file

@ -41,6 +41,11 @@ class TestSocketAddrInfo < Test::Unit::TestCase
assert_equal(80, ai.ip_port)
end
def test_addrinfo_inspect_sockaddr
ai = AddrInfo.tcp("127.0.0.1", 80)
assert_equal("127.0.0.1:80", ai.inspect_sockaddr)
end
def test_addrinfo_new_inet
ai = AddrInfo.new(["AF_INET", 46102, "localhost.localdomain", "127.0.0.2"])
assert_equal([46102, "127.0.0.2"], Socket.unpack_sockaddr_in(ai))
@ -305,6 +310,11 @@ class TestSocketAddrInfo < Test::Unit::TestCase
assert_equal(80, ai.ip_port)
end
def test_addrinfo_inspect_sockaddr_inet6
ai = AddrInfo.tcp("::1", 80)
assert_equal("[::1]:80", ai.inspect_sockaddr)
end
def test_marshal_inet6
ai1 = AddrInfo.tcp("::1", 80)
ai2 = Marshal.load(Marshal.dump(ai1))
@ -334,6 +344,11 @@ class TestSocketAddrInfo < Test::Unit::TestCase
assert_equal("/tmp/sock1", ai.unix_path)
end
def test_addrinfo_inspect_sockaddr_unix
ai = AddrInfo.unix("/tmp/test_addrinfo_inspect_sockaddr_unix")
assert_equal("/tmp/test_addrinfo_inspect_sockaddr_unix", ai.inspect_sockaddr)
end
def test_addrinfo_new_unix
ai = AddrInfo.new(["AF_UNIX", "/tmp/sock"])
assert_equal("/tmp/sock", Socket.unpack_sockaddr_un(ai))