mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 08:33:58 +02:00
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
3 changed files with 58 additions and 0 deletions
|
@ -189,6 +189,43 @@ rsock_revlookup_flag(VALUE revlookup, int *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:
|
||||
* 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.
|
||||
*/
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue