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;
}