IPSocket#inspect
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58351 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
cb52dda146
commit
8c84803d80
@ -189,6 +189,43 @@ rsock_revlookup_flag(VALUE revlookup, int *norevlookup)
|
|||||||
#undef return_norevlookup
|
#undef return_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:
|
* call-seq:
|
||||||
* ipsocket.addr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
|
* ipsocket.addr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
|
||||||
@ -332,6 +369,7 @@ rsock_init_ipsocket(void)
|
|||||||
* IPSocket is the super class of TCPSocket and UDPSocket.
|
* IPSocket is the super class of TCPSocket and UDPSocket.
|
||||||
*/
|
*/
|
||||||
rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
|
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, "addr", ip_addr, -1);
|
||||||
rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
|
rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
|
||||||
rb_define_method(rb_cIPSocket, "recvfrom", ip_recvfrom, -1);
|
rb_define_method(rb_cIPSocket, "recvfrom", ip_recvfrom, -1);
|
||||||
|
@ -8,6 +8,15 @@ end
|
|||||||
|
|
||||||
|
|
||||||
class TestSocket_TCPSocket < Test::Unit::TestCase
|
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
|
def test_initialize_failure
|
||||||
# These addresses are chosen from TEST-NET-1, TEST-NET-2, and TEST-NET-3.
|
# These addresses are chosen from TEST-NET-1, TEST-NET-2, and TEST-NET-3.
|
||||||
# [RFC 5737]
|
# [RFC 5737]
|
||||||
|
@ -15,6 +15,17 @@ class TestSocket_UDPSocket < Test::Unit::TestCase
|
|||||||
assert_nothing_raised { UDPSocket.open(:AF_INET) {} }
|
assert_nothing_raised { UDPSocket.open(:AF_INET) {} }
|
||||||
end
|
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
|
def test_connect
|
||||||
s = UDPSocket.new
|
s = UDPSocket.new
|
||||||
host = Object.new
|
host = Object.new
|
||||||
|
Loading…
x
Reference in New Issue
Block a user