summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-18 08:03:24 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-01-18 08:03:24 +0000
commitdb881044bcaf3fce9fbce99f6af8d3cd353ef05e (patch)
treebe2772f7bd3419b4ea6de37067b1b6295f912f24
parentd620500dda16cfc3777894541a417d6153d5a1fb (diff)
* 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
-rw-r--r--ChangeLog8
-rw-r--r--ext/socket/raddrinfo.c60
-rw-r--r--test/socket/test_addrinfo.rb15
3 files changed, 67 insertions, 16 deletions
diff --git a/ChangeLog b/ChangeLog
index 577ee4053aa..94c168e9b92 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c
index ee586fac314..43bbad28a06 100644
--- a/ext/socket/raddrinfo.c
+++ b/ext/socket/raddrinfo.c
@@ -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);
diff --git a/test/socket/test_addrinfo.rb b/test/socket/test_addrinfo.rb
index 326c6f8a864..bccf2774e42 100644
--- a/test/socket/test_addrinfo.rb
+++ b/test/socket/test_addrinfo.rb
@@ -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))