diff options
-rw-r--r-- | ext/socket/ipsocket.c | 38 | ||||
-rw-r--r-- | test/socket/test_tcp.rb | 9 | ||||
-rw-r--r-- | test/socket/test_udp.rb | 11 |
3 files changed, 58 insertions, 0 deletions
diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c index dbf403b39a..dadf10f6a5 100644 --- a/ext/socket/ipsocket.c +++ b/ext/socket/ipsocket.c @@ -191,6 +191,43 @@ rsock_revlookup_flag(VALUE revlookup, int *norevlookup) /* * call-seq: + * ipsocket.inspect -> string + * + * Return a string describing this IPSocket object. + */ +static VALUE +ip_inspect(VALUE sock) +{ + VALUE str = rb_call_super(0, 0); + rb_io_t *fptr = RFILE(sock)->fptr; + union_sockaddr addr; + socklen_t len = (socklen_t)sizeof addr; + ID id; + if (fptr && fptr->fd >= 0 && + getsockname(fptr->fd, &addr.addr, &len) >= 0 && + (id = rsock_intern_family(addr.addr.sa_family)) != 0) { + VALUE family = rb_id2str(id); + char hbuf[1024], pbuf[1024]; + long slen = RSTRING_LEN(str); + const char last = (slen > 1 && RSTRING_PTR(str)[slen - 1] == '>') ? + (--slen, '>') : 0; + str = rb_str_subseq(str, 0, slen); + rb_str_cat_cstr(str, ", "); + rb_str_append(str, family); + if (!rb_getnameinfo(&addr.addr, len, hbuf, sizeof(hbuf), + pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { + rb_str_cat_cstr(str, ", "); + rb_str_cat_cstr(str, hbuf); + rb_str_cat_cstr(str, ", "); + rb_str_cat_cstr(str, pbuf); + } + if (last) rb_str_cat(str, &last, 1); + } + return str; +} + +/* + * call-seq: * ipsocket.addr([reverse_lookup]) => [address_family, port, hostname, numeric_address] * * Returns the local address as an array which contains @@ -332,6 +369,7 @@ rsock_init_ipsocket(void) * IPSocket is the super class of TCPSocket and UDPSocket. */ rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket); + rb_define_method(rb_cIPSocket, "inspect", ip_inspect, 0); rb_define_method(rb_cIPSocket, "addr", ip_addr, -1); rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1); rb_define_method(rb_cIPSocket, "recvfrom", ip_recvfrom, -1); diff --git a/test/socket/test_tcp.rb b/test/socket/test_tcp.rb index 450d1b35b5..11325fdedb 100644 --- a/test/socket/test_tcp.rb +++ b/test/socket/test_tcp.rb @@ -8,6 +8,15 @@ end class TestSocket_TCPSocket < Test::Unit::TestCase + def test_inspect + TCPServer.open("localhost", 0) {|server| + assert_match(/AF_INET/, server.inspect) + TCPSocket.open("localhost", server.addr[1]) {|client| + assert_match(/AF_INET/, client.inspect) + } + } + end + def test_initialize_failure # These addresses are chosen from TEST-NET-1, TEST-NET-2, and TEST-NET-3. # [RFC 5737] diff --git a/test/socket/test_udp.rb b/test/socket/test_udp.rb index 5b23cce288..bec2181918 100644 --- a/test/socket/test_udp.rb +++ b/test/socket/test_udp.rb @@ -15,6 +15,17 @@ class TestSocket_UDPSocket < Test::Unit::TestCase assert_nothing_raised { UDPSocket.open(:AF_INET) {} } end + def test_inspect + UDPSocket.open() {|sock| + assert_match(/AF_INET\b/, sock.inspect) + } + if Socket.const_defined?(:AF_INET6) + UDPSocket.open(Socket::AF_INET6) {|sock| + assert_match(/AF_INET6\b/, sock.inspect) + } + end + end + def test_connect s = UDPSocket.new host = Object.new |