2009-01-16 23:11:27 -05:00
|
|
|
/************************************************
|
|
|
|
|
2014-02-21 23:02:43 -05:00
|
|
|
raddrinfo.c -
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
created at: Thu Mar 31 12:21:29 JST 1994
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "rubysocket.h"
|
|
|
|
|
|
|
|
#if defined(INET6) && (defined(LOOKUP_ORDER_HACK_INET) || defined(LOOKUP_ORDER_HACK_INET6))
|
|
|
|
#define LOOKUP_ORDERS (sizeof(lookup_order_table) / sizeof(lookup_order_table[0]))
|
|
|
|
static const int lookup_order_table[] = {
|
|
|
|
#if defined(LOOKUP_ORDER_HACK_INET)
|
|
|
|
PF_INET, PF_INET6, PF_UNSPEC,
|
|
|
|
#elif defined(LOOKUP_ORDER_HACK_INET6)
|
|
|
|
PF_INET6, PF_INET, PF_UNSPEC,
|
|
|
|
#else
|
|
|
|
/* should not happen */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
ruby_getaddrinfo(const char *nodename, const char *servname,
|
|
|
|
const struct addrinfo *hints, struct addrinfo **res)
|
|
|
|
{
|
|
|
|
struct addrinfo tmp_hints;
|
|
|
|
int i, af, error;
|
|
|
|
|
|
|
|
if (hints->ai_family != PF_UNSPEC) {
|
|
|
|
return getaddrinfo(nodename, servname, hints, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < LOOKUP_ORDERS; i++) {
|
|
|
|
af = lookup_order_table[i];
|
|
|
|
MEMCPY(&tmp_hints, hints, struct addrinfo, 1);
|
|
|
|
tmp_hints.ai_family = af;
|
|
|
|
error = getaddrinfo(nodename, servname, &tmp_hints, res);
|
|
|
|
if (error) {
|
|
|
|
if (tmp_hints.ai_family == PF_UNSPEC) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo((node),(serv),(hints),(res))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_AIX)
|
|
|
|
static int
|
|
|
|
ruby_getaddrinfo__aix(const char *nodename, const char *servname,
|
2010-09-13 12:02:35 -04:00
|
|
|
const struct addrinfo *hints, struct addrinfo **res)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int error = getaddrinfo(nodename, servname, hints, res);
|
|
|
|
struct addrinfo *r;
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
for (r = *res; r != NULL; r = r->ai_next) {
|
|
|
|
if (r->ai_addr->sa_family == 0)
|
|
|
|
r->ai_addr->sa_family = r->ai_family;
|
|
|
|
if (r->ai_addr->sa_len == 0)
|
|
|
|
r->ai_addr->sa_len = r->ai_addrlen;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#undef getaddrinfo
|
|
|
|
#define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__aix((node),(serv),(hints),(res))
|
|
|
|
static int
|
|
|
|
ruby_getnameinfo__aix(const struct sockaddr *sa, size_t salen,
|
|
|
|
char *host, size_t hostlen,
|
|
|
|
char *serv, size_t servlen, int flags)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *sa6;
|
|
|
|
u_int32_t *a6;
|
|
|
|
|
|
|
|
if (sa->sa_family == AF_INET6) {
|
|
|
|
sa6 = (struct sockaddr_in6 *)sa;
|
|
|
|
a6 = sa6->sin6_addr.u6_addr.u6_addr32;
|
|
|
|
|
|
|
|
if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) {
|
|
|
|
strncpy(host, "::", hostlen);
|
|
|
|
snprintf(serv, servlen, "%d", sa6->sin6_port);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
|
|
|
}
|
|
|
|
#undef getnameinfo
|
|
|
|
#define getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \
|
|
|
|
ruby_getnameinfo__aix((sa), (salen), (host), (hostlen), (serv), (servlen), (flags))
|
|
|
|
#endif
|
|
|
|
|
2010-06-23 11:05:25 -04:00
|
|
|
static int str_is_number(const char *);
|
2010-06-23 08:25:07 -04:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
static int
|
|
|
|
ruby_getaddrinfo__darwin(const char *nodename, const char *servname,
|
2010-09-14 19:26:03 -04:00
|
|
|
const struct addrinfo *hints, struct addrinfo **res)
|
2010-06-23 08:25:07 -04:00
|
|
|
{
|
2010-06-23 17:24:31 -04:00
|
|
|
/* fix [ruby-core:29427] */
|
2010-06-23 08:25:07 -04:00
|
|
|
const char *tmp_servname;
|
|
|
|
struct addrinfo tmp_hints;
|
2010-09-14 19:26:03 -04:00
|
|
|
int error;
|
|
|
|
|
2010-06-23 08:25:07 -04:00
|
|
|
tmp_servname = servname;
|
|
|
|
MEMCPY(&tmp_hints, hints, struct addrinfo, 1);
|
|
|
|
if (nodename && servname) {
|
2010-06-23 11:05:25 -04:00
|
|
|
if (str_is_number(tmp_servname) && atoi(servname) == 0) {
|
2010-06-23 08:25:07 -04:00
|
|
|
tmp_servname = NULL;
|
|
|
|
#ifdef AI_NUMERICSERV
|
|
|
|
if (tmp_hints.ai_flags) tmp_hints.ai_flags &= ~AI_NUMERICSERV;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 17:24:31 -04:00
|
|
|
|
2010-09-14 19:26:03 -04:00
|
|
|
error = getaddrinfo(nodename, tmp_servname, &tmp_hints, res);
|
|
|
|
if (error == 0) {
|
2010-06-23 17:24:31 -04:00
|
|
|
/* [ruby-dev:23164] */
|
|
|
|
struct addrinfo *r;
|
|
|
|
r = *res;
|
|
|
|
while (r) {
|
|
|
|
if (! r->ai_socktype) r->ai_socktype = hints->ai_socktype;
|
|
|
|
if (! r->ai_protocol) {
|
|
|
|
if (r->ai_socktype == SOCK_DGRAM) {
|
|
|
|
r->ai_protocol = IPPROTO_UDP;
|
|
|
|
}
|
|
|
|
else if (r->ai_socktype == SOCK_STREAM) {
|
|
|
|
r->ai_protocol = IPPROTO_TCP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r = r->ai_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 08:25:07 -04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#undef getaddrinfo
|
|
|
|
#define getaddrinfo(node,serv,hints,res) ruby_getaddrinfo__darwin((node),(serv),(hints),(res))
|
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifndef GETADDRINFO_EMU
|
|
|
|
struct getaddrinfo_arg
|
|
|
|
{
|
|
|
|
const char *node;
|
|
|
|
const char *service;
|
|
|
|
const struct addrinfo *hints;
|
|
|
|
struct addrinfo **res;
|
|
|
|
};
|
|
|
|
|
2012-07-10 09:57:11 -04:00
|
|
|
static void *
|
2009-01-16 23:11:27 -05:00
|
|
|
nogvl_getaddrinfo(void *arg)
|
|
|
|
{
|
2013-06-10 07:59:36 -04:00
|
|
|
int ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
struct getaddrinfo_arg *ptr = arg;
|
2013-06-10 08:51:48 -04:00
|
|
|
ret = getaddrinfo(ptr->node, ptr->service, ptr->hints, ptr->res);
|
2013-06-10 07:59:36 -04:00
|
|
|
#ifdef __linux__
|
|
|
|
/* On Linux (mainly Ubuntu 13.04) /etc/nsswitch.conf has mdns4 and
|
|
|
|
* it cause getaddrinfo to return EAI_SYSTEM/ENOENT. [ruby-list:49420]
|
|
|
|
*/
|
|
|
|
if (ret == EAI_SYSTEM && errno == ENOENT)
|
|
|
|
ret = EAI_NONAME;
|
|
|
|
#endif
|
|
|
|
return (void *)(VALUE)ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-19 05:37:43 -05:00
|
|
|
static int
|
|
|
|
numeric_getaddrinfo(const char *node, const char *service,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **res)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_INET_PTON
|
2014-02-22 22:18:56 -05:00
|
|
|
# if defined __MINGW64__
|
|
|
|
# define inet_pton(f,s,d) rb_w32_inet_pton(f,s,d)
|
|
|
|
# endif
|
|
|
|
|
2014-02-19 05:37:43 -05:00
|
|
|
if (node && (!service || strspn(service, "0123456789") == strlen(service))) {
|
2014-02-22 20:29:25 -05:00
|
|
|
static const struct {
|
|
|
|
int socktype;
|
|
|
|
int protocol;
|
|
|
|
} list[] = {
|
|
|
|
{ SOCK_STREAM, IPPROTO_TCP },
|
|
|
|
{ SOCK_DGRAM, IPPROTO_UDP },
|
|
|
|
{ SOCK_RAW, 0 }
|
|
|
|
};
|
|
|
|
struct addrinfo *ai = NULL;
|
2014-02-19 05:37:43 -05:00
|
|
|
int port = service ? (unsigned short)atoi(service): 0;
|
|
|
|
int hint_family = hints ? hints->ai_family : PF_UNSPEC;
|
|
|
|
int hint_socktype = hints ? hints->ai_socktype : 0;
|
|
|
|
int hint_protocol = hints ? hints->ai_protocol : 0;
|
|
|
|
char ipv4addr[4];
|
|
|
|
#ifdef AF_INET6
|
|
|
|
char ipv6addr[16];
|
|
|
|
if ((hint_family == PF_UNSPEC || hint_family == PF_INET6) &&
|
|
|
|
strspn(node, "0123456789abcdefABCDEF.:") == strlen(node) &&
|
|
|
|
inet_pton(AF_INET6, node, ipv6addr)) {
|
|
|
|
int i;
|
|
|
|
for (i = numberof(list)-1; 0 <= i; i--) {
|
|
|
|
if ((hint_socktype == 0 || hint_socktype == list[i].socktype) &&
|
|
|
|
(hint_protocol == 0 || list[i].protocol == 0 || hint_protocol == list[i].protocol)) {
|
2014-02-20 08:22:44 -05:00
|
|
|
struct addrinfo *ai0 = xcalloc(1, sizeof(struct addrinfo));
|
2014-02-19 05:37:43 -05:00
|
|
|
struct sockaddr_in6 *sa = xmalloc(sizeof(struct sockaddr_in6));
|
|
|
|
INIT_SOCKADDR_IN6(sa, sizeof(struct sockaddr_in6));
|
|
|
|
memcpy(&sa->sin6_addr, ipv6addr, sizeof(ipv6addr));
|
|
|
|
sa->sin6_port = htons(port);
|
|
|
|
ai0->ai_family = PF_INET6;
|
|
|
|
ai0->ai_socktype = list[i].socktype;
|
|
|
|
ai0->ai_protocol = hint_protocol ? hint_protocol : list[i].protocol;
|
|
|
|
ai0->ai_addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
ai0->ai_addr = (struct sockaddr *)sa;
|
|
|
|
ai0->ai_canonname = NULL;
|
|
|
|
ai0->ai_next = ai;
|
|
|
|
ai = ai0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if ((hint_family == PF_UNSPEC || hint_family == PF_INET) &&
|
|
|
|
strspn(node, "0123456789.") == strlen(node) &&
|
|
|
|
inet_pton(AF_INET, node, ipv4addr)) {
|
|
|
|
int i;
|
|
|
|
for (i = numberof(list)-1; 0 <= i; i--) {
|
|
|
|
if ((hint_socktype == 0 || hint_socktype == list[i].socktype) &&
|
|
|
|
(hint_protocol == 0 || list[i].protocol == 0 || hint_protocol == list[i].protocol)) {
|
2014-02-20 08:22:44 -05:00
|
|
|
struct addrinfo *ai0 = xcalloc(1, sizeof(struct addrinfo));
|
2014-02-19 05:37:43 -05:00
|
|
|
struct sockaddr_in *sa = xmalloc(sizeof(struct sockaddr_in));
|
|
|
|
INIT_SOCKADDR_IN(sa, sizeof(struct sockaddr_in));
|
|
|
|
memcpy(&sa->sin_addr, ipv4addr, sizeof(ipv4addr));
|
|
|
|
sa->sin_port = htons(port);
|
|
|
|
ai0->ai_family = PF_INET;
|
|
|
|
ai0->ai_socktype = list[i].socktype;
|
|
|
|
ai0->ai_protocol = hint_protocol ? hint_protocol : list[i].protocol;
|
|
|
|
ai0->ai_addrlen = sizeof(struct sockaddr_in);
|
|
|
|
ai0->ai_addr = (struct sockaddr *)sa;
|
|
|
|
ai0->ai_canonname = NULL;
|
|
|
|
ai0->ai_next = ai;
|
|
|
|
ai = ai0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ai) {
|
|
|
|
*res = ai;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return EAI_FAIL;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
int
|
|
|
|
rb_getaddrinfo(const char *node, const char *service,
|
|
|
|
const struct addrinfo *hints,
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo **res)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2014-02-19 04:38:24 -05:00
|
|
|
struct addrinfo *ai;
|
|
|
|
int ret;
|
2014-02-19 05:37:43 -05:00
|
|
|
int allocated_by_malloc = 0;
|
2014-02-19 04:38:24 -05:00
|
|
|
|
2014-02-19 05:37:43 -05:00
|
|
|
ret = numeric_getaddrinfo(node, service, hints, &ai);
|
|
|
|
if (ret == 0)
|
|
|
|
allocated_by_malloc = 1;
|
|
|
|
else {
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifdef GETADDRINFO_EMU
|
2014-02-19 05:37:43 -05:00
|
|
|
ret = getaddrinfo(node, service, hints, &ai);
|
2009-01-16 23:11:27 -05:00
|
|
|
#else
|
2014-02-19 05:37:43 -05:00
|
|
|
struct getaddrinfo_arg arg;
|
2014-02-21 11:04:50 -05:00
|
|
|
MEMZERO(&arg, struct getaddrinfo_arg, 1);
|
2014-02-19 05:37:43 -05:00
|
|
|
arg.node = node;
|
|
|
|
arg.service = service;
|
|
|
|
arg.hints = hints;
|
|
|
|
arg.res = &ai;
|
|
|
|
ret = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getaddrinfo, &arg, RUBY_UBF_IO, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
2014-02-19 05:37:43 -05:00
|
|
|
}
|
2014-02-19 04:38:24 -05:00
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
*res = (struct rb_addrinfo *)xmalloc(sizeof(struct rb_addrinfo));
|
2014-02-19 05:37:43 -05:00
|
|
|
(*res)->allocated_by_malloc = allocated_by_malloc;
|
2014-02-19 04:38:24 -05:00
|
|
|
(*res)->ai = ai;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_freeaddrinfo(struct rb_addrinfo *ai)
|
|
|
|
{
|
2014-02-19 05:37:43 -05:00
|
|
|
if (!ai->allocated_by_malloc)
|
|
|
|
freeaddrinfo(ai->ai);
|
|
|
|
else {
|
|
|
|
struct addrinfo *ai1, *ai2;
|
|
|
|
ai1 = ai->ai;
|
|
|
|
while (ai1) {
|
|
|
|
ai2 = ai1->ai_next;
|
|
|
|
xfree(ai1->ai_addr);
|
|
|
|
xfree(ai1);
|
|
|
|
ai1 = ai2;
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 04:38:24 -05:00
|
|
|
xfree(ai);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef GETADDRINFO_EMU
|
|
|
|
struct getnameinfo_arg
|
|
|
|
{
|
|
|
|
const struct sockaddr *sa;
|
|
|
|
socklen_t salen;
|
|
|
|
char *host;
|
|
|
|
size_t hostlen;
|
|
|
|
char *serv;
|
|
|
|
size_t servlen;
|
|
|
|
int flags;
|
|
|
|
};
|
|
|
|
|
2012-07-10 09:57:11 -04:00
|
|
|
static void *
|
2009-01-16 23:11:27 -05:00
|
|
|
nogvl_getnameinfo(void *arg)
|
|
|
|
{
|
|
|
|
struct getnameinfo_arg *ptr = arg;
|
2012-07-10 23:17:35 -04:00
|
|
|
return (void *)(VALUE)getnameinfo(ptr->sa, ptr->salen,
|
|
|
|
ptr->host, (socklen_t)ptr->hostlen,
|
|
|
|
ptr->serv, (socklen_t)ptr->servlen,
|
|
|
|
ptr->flags);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_getnameinfo(const struct sockaddr *sa, socklen_t salen,
|
|
|
|
char *host, size_t hostlen,
|
|
|
|
char *serv, size_t servlen, int flags)
|
|
|
|
{
|
|
|
|
#ifdef GETADDRINFO_EMU
|
|
|
|
return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
|
|
|
#else
|
|
|
|
struct getnameinfo_arg arg;
|
|
|
|
int ret;
|
|
|
|
arg.sa = sa;
|
|
|
|
arg.salen = salen;
|
|
|
|
arg.host = host;
|
|
|
|
arg.hostlen = hostlen;
|
|
|
|
arg.serv = serv;
|
|
|
|
arg.servlen = servlen;
|
|
|
|
arg.flags = flags;
|
2012-07-10 23:17:35 -04:00
|
|
|
ret = (int)(VALUE)rb_thread_call_without_gvl(nogvl_getnameinfo, &arg, RUBY_UBF_IO, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-14 06:28:49 -05:00
|
|
|
make_ipaddr0(struct sockaddr *addr, socklen_t addrlen, char *buf, size_t buflen)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2013-02-14 06:28:49 -05:00
|
|
|
error = rb_getnameinfo(addr, addrlen, buf, buflen, NULL, 0, NI_NUMERICHOST);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (error) {
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error("getnameinfo", error);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2013-02-14 06:28:49 -05:00
|
|
|
rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-02-10 10:39:16 -05:00
|
|
|
char hbuf[1024];
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2013-02-14 06:28:49 -05:00
|
|
|
make_ipaddr0(addr, addrlen, hbuf, sizeof(hbuf));
|
2009-02-10 10:39:16 -05:00
|
|
|
return rb_str_new2(hbuf);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-14 06:28:49 -05:00
|
|
|
make_inetaddr(unsigned int host, char *buf, size_t buflen)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
2013-02-15 17:08:25 -05:00
|
|
|
INIT_SOCKADDR_IN(&sin, sizeof(sin));
|
2009-01-16 23:11:27 -05:00
|
|
|
sin.sin_addr.s_addr = host;
|
2013-02-14 06:28:49 -05:00
|
|
|
make_ipaddr0((struct sockaddr*)&sin, sizeof(sin), buf, buflen);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-06-23 11:05:25 -04:00
|
|
|
str_is_number(const char *p)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
if (!p || *p == '\0')
|
|
|
|
return 0;
|
|
|
|
ep = NULL;
|
|
|
|
(void)STRTOUL(p, &ep, 10);
|
|
|
|
if (ep && *ep == '\0')
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2013-02-14 06:28:49 -05:00
|
|
|
host_str(VALUE host, char *hbuf, size_t hbuflen, int *flags_ptr)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
if (NIL_P(host)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (rb_obj_is_kind_of(host, rb_cInteger)) {
|
2010-04-28 04:14:13 -04:00
|
|
|
unsigned int i = NUM2UINT(host);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2013-02-14 06:28:49 -05:00
|
|
|
make_inetaddr(htonl(i), hbuf, hbuflen);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
|
|
|
|
return hbuf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
SafeStringValue(host);
|
|
|
|
name = RSTRING_PTR(host);
|
|
|
|
if (!name || *name == 0 || (name[0] == '<' && strcmp(name, "<any>") == 0)) {
|
2013-02-14 06:28:49 -05:00
|
|
|
make_inetaddr(INADDR_ANY, hbuf, hbuflen);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
|
|
|
|
}
|
|
|
|
else if (name[0] == '<' && strcmp(name, "<broadcast>") == 0) {
|
2013-02-14 06:28:49 -05:00
|
|
|
make_inetaddr(INADDR_BROADCAST, hbuf, hbuflen);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (flags_ptr) *flags_ptr |= AI_NUMERICHOST;
|
|
|
|
}
|
2013-02-14 06:28:49 -05:00
|
|
|
else if (strlen(name) >= hbuflen) {
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eArgError, "hostname too long (%"PRIuSIZE")",
|
|
|
|
strlen(name));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(hbuf, name);
|
|
|
|
}
|
|
|
|
return hbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2013-02-14 06:28:49 -05:00
|
|
|
port_str(VALUE port, char *pbuf, size_t pbuflen, int *flags_ptr)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
if (NIL_P(port)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (FIXNUM_P(port)) {
|
2013-02-14 06:28:49 -05:00
|
|
|
snprintf(pbuf, pbuflen, "%ld", FIX2LONG(port));
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifdef AI_NUMERICSERV
|
|
|
|
if (flags_ptr) *flags_ptr |= AI_NUMERICSERV;
|
|
|
|
#endif
|
|
|
|
return pbuf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *serv;
|
|
|
|
|
|
|
|
SafeStringValue(port);
|
|
|
|
serv = RSTRING_PTR(port);
|
2013-02-14 06:28:49 -05:00
|
|
|
if (strlen(serv) >= pbuflen) {
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eArgError, "service name too long (%"PRIuSIZE")",
|
|
|
|
strlen(serv));
|
|
|
|
}
|
|
|
|
strcpy(pbuf, serv);
|
|
|
|
return pbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo*
|
2009-03-01 01:48:22 -05:00
|
|
|
rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo* res = NULL;
|
2009-01-16 23:11:27 -05:00
|
|
|
char *hostp, *portp;
|
|
|
|
int error;
|
|
|
|
char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
|
|
|
|
int additional_flags = 0;
|
|
|
|
|
|
|
|
hostp = host_str(host, hbuf, sizeof(hbuf), &additional_flags);
|
|
|
|
portp = port_str(port, pbuf, sizeof(pbuf), &additional_flags);
|
|
|
|
|
2010-06-23 11:05:25 -04:00
|
|
|
if (socktype_hack && hints->ai_socktype == 0 && str_is_number(portp)) {
|
2009-01-16 23:11:27 -05:00
|
|
|
hints->ai_socktype = SOCK_DGRAM;
|
|
|
|
}
|
|
|
|
hints->ai_flags |= additional_flags;
|
|
|
|
|
|
|
|
error = rb_getaddrinfo(hostp, portp, hints, &res);
|
|
|
|
if (error) {
|
|
|
|
if (hostp && hostp[strlen(hostp)-1] == '\n') {
|
|
|
|
rb_raise(rb_eSocket, "newline at the end of hostname");
|
|
|
|
}
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error("getaddrinfo", error);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo*
|
2009-03-01 01:48:22 -05:00
|
|
|
rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
MEMZERO(&hints, struct addrinfo, 1);
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = socktype;
|
|
|
|
hints.ai_flags = flags;
|
2009-03-01 01:48:22 -05:00
|
|
|
return rsock_getaddrinfo(host, port, &hints, 1);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2013-02-14 06:28:49 -05:00
|
|
|
rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
VALUE family, port, addr1, addr2;
|
|
|
|
VALUE ary;
|
|
|
|
int error;
|
|
|
|
char hbuf[1024], pbuf[1024];
|
|
|
|
ID id;
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
id = rsock_intern_family(sockaddr->sa_family);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (id) {
|
|
|
|
family = rb_str_dup(rb_id2str(id));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(pbuf, "unknown:%d", sockaddr->sa_family);
|
|
|
|
family = rb_str_new2(pbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
addr1 = Qnil;
|
|
|
|
if (!norevlookup) {
|
2013-02-14 06:28:49 -05:00
|
|
|
error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
|
2009-01-16 23:11:27 -05:00
|
|
|
NULL, 0, 0);
|
|
|
|
if (! error) {
|
|
|
|
addr1 = rb_str_new2(hbuf);
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 06:28:49 -05:00
|
|
|
error = rb_getnameinfo(sockaddr, sockaddrlen, hbuf, sizeof(hbuf),
|
2009-01-16 23:11:27 -05:00
|
|
|
pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV);
|
|
|
|
if (error) {
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error("getnameinfo", error);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
addr2 = rb_str_new2(hbuf);
|
|
|
|
if (addr1 == Qnil) {
|
|
|
|
addr1 = addr2;
|
|
|
|
}
|
|
|
|
port = INT2FIX(atoi(pbuf));
|
|
|
|
ary = rb_ary_new3(4, family, port, addr1, addr2);
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
2012-04-25 23:34:12 -04:00
|
|
|
VALUE
|
|
|
|
rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2012-04-25 23:34:12 -04:00
|
|
|
char *s, *e;
|
|
|
|
s = sockaddr->sun_path;
|
|
|
|
e = (char *)sockaddr + len;
|
|
|
|
while (s < e && *(e-1) == '\0')
|
|
|
|
e--;
|
|
|
|
if (s <= e)
|
|
|
|
return rb_str_new(s, e-s);
|
2009-01-16 23:11:27 -05:00
|
|
|
else
|
2012-04-25 23:34:12 -04:00
|
|
|
return rb_str_new2("");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
return rb_assoc_new(rb_str_new2("AF_UNIX"),
|
2012-04-25 23:34:12 -04:00
|
|
|
rsock_unixpath_str(sockaddr, len));
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
2013-01-25 03:15:26 -05:00
|
|
|
|
|
|
|
socklen_t
|
2013-01-25 18:33:50 -05:00
|
|
|
rsock_unix_sockaddr_len(VALUE path)
|
2013-01-25 03:15:26 -05:00
|
|
|
{
|
|
|
|
#ifdef __linux__
|
2013-01-30 23:59:31 -05:00
|
|
|
if (RSTRING_LEN(path) == 0) {
|
|
|
|
/* autobind; see unix(7) for details. */
|
|
|
|
return (socklen_t) sizeof(sa_family_t);
|
|
|
|
}
|
|
|
|
else if (RSTRING_PTR(path)[0] == '\0') {
|
2013-01-25 03:15:26 -05:00
|
|
|
/* abstract namespace; see unix(7) for details. */
|
2013-04-17 07:01:17 -04:00
|
|
|
if (SOCKLEN_MAX - offsetof(struct sockaddr_un, sun_path) < (size_t)RSTRING_LEN(path))
|
|
|
|
rb_raise(rb_eArgError, "Linux abstract socket too long");
|
2013-01-25 03:15:26 -05:00
|
|
|
return (socklen_t) offsetof(struct sockaddr_un, sun_path) +
|
2013-04-17 07:01:17 -04:00
|
|
|
RSTRING_SOCKLEN(path);
|
2013-01-25 03:15:26 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif
|
|
|
|
return (socklen_t) sizeof(struct sockaddr_un);
|
|
|
|
#ifdef __linux__
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct hostent_arg {
|
|
|
|
VALUE host;
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo* addr;
|
2013-02-15 02:54:38 -05:00
|
|
|
VALUE (*ipaddr)(struct sockaddr*, socklen_t);
|
2009-01-16 23:11:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
make_hostent_internal(struct hostent_arg *arg)
|
|
|
|
{
|
|
|
|
VALUE host = arg->host;
|
2014-02-19 04:38:24 -05:00
|
|
|
struct addrinfo* addr = arg->addr->ai;
|
2013-02-15 02:54:38 -05:00
|
|
|
VALUE (*ipaddr)(struct sockaddr*, socklen_t) = arg->ipaddr;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
struct addrinfo *ai;
|
|
|
|
struct hostent *h;
|
|
|
|
VALUE ary, names;
|
|
|
|
char **pch;
|
|
|
|
const char* hostp;
|
|
|
|
char hbuf[NI_MAXHOST];
|
|
|
|
|
|
|
|
ary = rb_ary_new();
|
|
|
|
if (addr->ai_canonname) {
|
|
|
|
hostp = addr->ai_canonname;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
hostp = host_str(host, hbuf, sizeof(hbuf), NULL);
|
|
|
|
}
|
|
|
|
rb_ary_push(ary, rb_str_new2(hostp));
|
|
|
|
|
|
|
|
if (addr->ai_canonname && (h = gethostbyname(addr->ai_canonname))) {
|
|
|
|
names = rb_ary_new();
|
|
|
|
if (h->h_aliases != NULL) {
|
|
|
|
for (pch = h->h_aliases; *pch; pch++) {
|
|
|
|
rb_ary_push(names, rb_str_new2(*pch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
names = rb_ary_new2(0);
|
|
|
|
}
|
|
|
|
rb_ary_push(ary, names);
|
|
|
|
rb_ary_push(ary, INT2NUM(addr->ai_family));
|
|
|
|
for (ai = addr; ai; ai = ai->ai_next) {
|
|
|
|
rb_ary_push(ary, (*ipaddr)(ai->ai_addr, ai->ai_addrlen));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2010-02-26 07:06:17 -05:00
|
|
|
VALUE
|
2014-02-19 04:38:24 -05:00
|
|
|
rsock_freeaddrinfo(VALUE arg)
|
2010-02-26 07:06:17 -05:00
|
|
|
{
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo *addr = (struct rb_addrinfo *)arg;
|
|
|
|
rb_freeaddrinfo(addr);
|
2010-02-26 07:06:17 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE
|
2014-02-19 04:38:24 -05:00
|
|
|
rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t))
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
struct hostent_arg arg;
|
|
|
|
|
|
|
|
arg.host = host;
|
|
|
|
arg.addr = addr;
|
|
|
|
arg.ipaddr = ipaddr;
|
|
|
|
return rb_ensure(make_hostent_internal, (VALUE)&arg,
|
2010-02-26 07:06:17 -05:00
|
|
|
rsock_freeaddrinfo, (VALUE)addr);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
VALUE inspectname;
|
|
|
|
VALUE canonname;
|
|
|
|
int pfamily;
|
|
|
|
int socktype;
|
|
|
|
int protocol;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t sockaddr_len;
|
2013-02-24 12:51:17 -05:00
|
|
|
union_sockaddr addr;
|
2009-01-16 23:11:27 -05:00
|
|
|
} rb_addrinfo_t;
|
|
|
|
|
|
|
|
static void
|
2009-12-30 16:03:51 -05:00
|
|
|
addrinfo_mark(void *ptr)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-12-30 16:03:51 -05:00
|
|
|
rb_addrinfo_t *rai = ptr;
|
2009-01-16 23:11:27 -05:00
|
|
|
if (rai) {
|
|
|
|
rb_gc_mark(rai->inspectname);
|
|
|
|
rb_gc_mark(rai->canonname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-30 16:03:51 -05:00
|
|
|
#define addrinfo_free RUBY_TYPED_DEFAULT_FREE
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
addrinfo_memsize(const void *ptr)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-12-30 16:03:51 -05:00
|
|
|
return ptr ? sizeof(rb_addrinfo_t) : 0;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
2009-12-30 16:03:51 -05:00
|
|
|
static const rb_data_type_t addrinfo_type = {
|
|
|
|
"socket/addrinfo",
|
2010-07-18 03:31:54 -04:00
|
|
|
{addrinfo_mark, addrinfo_free, addrinfo_memsize,},
|
2009-12-30 16:03:51 -05:00
|
|
|
};
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
static VALUE
|
|
|
|
addrinfo_s_allocate(VALUE klass)
|
|
|
|
{
|
2009-12-30 16:03:51 -05:00
|
|
|
return TypedData_Wrap_Struct(klass, &addrinfo_type, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:20:33 -04:00
|
|
|
#define IS_ADDRINFO(obj) rb_typeddata_is_kind_of((obj), &addrinfo_type)
|
2009-12-30 16:03:51 -05:00
|
|
|
static inline rb_addrinfo_t *
|
2009-01-16 23:11:27 -05:00
|
|
|
check_addrinfo(VALUE self)
|
|
|
|
{
|
2009-12-30 16:03:51 -05:00
|
|
|
return rb_check_typeddata(self, &addrinfo_type);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static rb_addrinfo_t *
|
|
|
|
get_addrinfo(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = check_addrinfo(self);
|
|
|
|
|
|
|
|
if (!rai) {
|
|
|
|
rb_raise(rb_eTypeError, "uninitialized socket address");
|
|
|
|
}
|
|
|
|
return rai;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static rb_addrinfo_t *
|
|
|
|
alloc_addrinfo()
|
|
|
|
{
|
2014-07-25 17:34:35 -04:00
|
|
|
rb_addrinfo_t *rai = ZALLOC(rb_addrinfo_t);
|
2009-01-16 23:11:27 -05:00
|
|
|
rai->inspectname = Qnil;
|
|
|
|
rai->canonname = Qnil;
|
|
|
|
return rai;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-04-28 04:14:13 -04:00
|
|
|
init_addrinfo(rb_addrinfo_t *rai, struct sockaddr *sa, socklen_t len,
|
2009-01-16 23:11:27 -05:00
|
|
|
int pfamily, int socktype, int protocol,
|
|
|
|
VALUE canonname, VALUE inspectname)
|
|
|
|
{
|
2010-09-23 08:42:49 -04:00
|
|
|
if ((socklen_t)sizeof(rai->addr) < len)
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eArgError, "sockaddr string too big");
|
|
|
|
memcpy((void *)&rai->addr, (void *)sa, len);
|
|
|
|
rai->sockaddr_len = len;
|
|
|
|
|
|
|
|
rai->pfamily = pfamily;
|
|
|
|
rai->socktype = socktype;
|
|
|
|
rai->protocol = protocol;
|
|
|
|
rai->canonname = canonname;
|
|
|
|
rai->inspectname = inspectname;
|
|
|
|
}
|
|
|
|
|
2009-01-20 06:19:09 -05:00
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_addrinfo_new(struct sockaddr *addr, socklen_t len,
|
|
|
|
int family, int socktype, int protocol,
|
|
|
|
VALUE canonname, VALUE inspectname)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
VALUE a;
|
|
|
|
rb_addrinfo_t *rai;
|
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
a = addrinfo_s_allocate(rb_cAddrinfo);
|
2009-01-16 23:11:27 -05:00
|
|
|
DATA_PTR(a) = rai = alloc_addrinfo();
|
|
|
|
init_addrinfo(rai, addr, len, family, socktype, protocol, canonname, inspectname);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
static struct rb_addrinfo *
|
2009-01-16 23:11:27 -05:00
|
|
|
call_getaddrinfo(VALUE node, VALUE service,
|
|
|
|
VALUE family, VALUE socktype, VALUE protocol, VALUE flags,
|
|
|
|
int socktype_hack)
|
|
|
|
{
|
2014-02-19 04:38:24 -05:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct rb_addrinfo *res;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
MEMZERO(&hints, struct addrinfo, 1);
|
2009-03-01 01:30:41 -05:00
|
|
|
hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
if (!NIL_P(socktype)) {
|
2009-03-01 01:30:41 -05:00
|
|
|
hints.ai_socktype = rsock_socktype_arg(socktype);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
if (!NIL_P(protocol)) {
|
|
|
|
hints.ai_protocol = NUM2INT(protocol);
|
|
|
|
}
|
|
|
|
if (!NIL_P(flags)) {
|
|
|
|
hints.ai_flags = NUM2INT(flags);
|
|
|
|
}
|
2009-03-01 01:48:22 -05:00
|
|
|
res = rsock_getaddrinfo(node, service, &hints, socktype_hack);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
if (res == NULL)
|
|
|
|
rb_raise(rb_eSocket, "host not found");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-01-17 01:03:42 -05:00
|
|
|
static VALUE make_inspectname(VALUE node, VALUE service, struct addrinfo *res);
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
static void
|
|
|
|
init_addrinfo_getaddrinfo(rb_addrinfo_t *rai, VALUE node, VALUE service,
|
|
|
|
VALUE family, VALUE socktype, VALUE protocol, VALUE flags,
|
2009-01-17 01:03:42 -05:00
|
|
|
VALUE inspectnode, VALUE inspectservice)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 1);
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE canonname;
|
2014-02-19 04:38:24 -05:00
|
|
|
VALUE inspectname = rb_str_equal(node, inspectnode) ? Qnil : make_inspectname(inspectnode, inspectservice, res->ai);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
canonname = Qnil;
|
2014-02-19 04:38:24 -05:00
|
|
|
if (res->ai->ai_canonname) {
|
|
|
|
canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname);
|
2009-01-16 23:11:27 -05:00
|
|
|
OBJ_FREEZE(canonname);
|
|
|
|
}
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
init_addrinfo(rai, res->ai->ai_addr, res->ai->ai_addrlen,
|
2009-01-16 23:11:27 -05:00
|
|
|
NUM2INT(family), NUM2INT(socktype), NUM2INT(protocol),
|
|
|
|
canonname, inspectname);
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
rb_freeaddrinfo(res);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-01-17 01:03:42 -05:00
|
|
|
make_inspectname(VALUE node, VALUE service, struct addrinfo *res)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
VALUE inspectname = Qnil;
|
2009-01-17 01:03:42 -05:00
|
|
|
|
|
|
|
if (res) {
|
2012-02-02 06:51:25 -05:00
|
|
|
/* drop redundant information which also shown in address:port part. */
|
2009-01-17 01:03:42 -05:00
|
|
|
char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
|
|
|
|
int ret;
|
|
|
|
ret = rb_getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
|
|
|
|
sizeof(hbuf), pbuf, sizeof(pbuf),
|
|
|
|
NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
if (ret == 0) {
|
2012-05-23 03:13:21 -04:00
|
|
|
if (RB_TYPE_P(node, T_STRING) && strcmp(hbuf, RSTRING_PTR(node)) == 0)
|
2009-01-17 01:03:42 -05:00
|
|
|
node = Qnil;
|
2012-05-23 03:13:21 -04:00
|
|
|
if (RB_TYPE_P(service, T_STRING) && strcmp(pbuf, RSTRING_PTR(service)) == 0)
|
2009-01-17 01:03:42 -05:00
|
|
|
service = Qnil;
|
2012-05-23 03:13:21 -04:00
|
|
|
else if (RB_TYPE_P(service, T_FIXNUM) && atoi(pbuf) == FIX2INT(service))
|
2009-01-17 01:03:42 -05:00
|
|
|
service = Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 03:13:21 -04:00
|
|
|
if (RB_TYPE_P(node, T_STRING)) {
|
2009-01-16 23:11:27 -05:00
|
|
|
inspectname = rb_str_dup(node);
|
|
|
|
}
|
2012-05-23 03:13:21 -04:00
|
|
|
if (RB_TYPE_P(service, T_STRING)) {
|
2009-01-16 23:11:27 -05:00
|
|
|
if (NIL_P(inspectname))
|
|
|
|
inspectname = rb_sprintf(":%s", StringValueCStr(service));
|
|
|
|
else
|
|
|
|
rb_str_catf(inspectname, ":%s", StringValueCStr(service));
|
|
|
|
}
|
2012-05-23 03:13:21 -04:00
|
|
|
else if (RB_TYPE_P(service, T_FIXNUM) && FIX2INT(service) != 0)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
if (NIL_P(inspectname))
|
|
|
|
inspectname = rb_sprintf(":%d", FIX2INT(service));
|
|
|
|
else
|
|
|
|
rb_str_catf(inspectname, ":%d", FIX2INT(service));
|
|
|
|
}
|
|
|
|
if (!NIL_P(inspectname)) {
|
|
|
|
OBJ_INFECT(inspectname, node);
|
|
|
|
OBJ_INFECT(inspectname, service);
|
|
|
|
OBJ_FREEZE(inspectname);
|
|
|
|
}
|
|
|
|
return inspectname;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
addrinfo_firstonly_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
|
|
|
|
{
|
|
|
|
VALUE ret;
|
|
|
|
VALUE canonname;
|
|
|
|
VALUE inspectname;
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
inspectname = make_inspectname(node, service, res->ai);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
canonname = Qnil;
|
2014-02-19 04:38:24 -05:00
|
|
|
if (res->ai->ai_canonname) {
|
|
|
|
canonname = rb_tainted_str_new_cstr(res->ai->ai_canonname);
|
2009-01-16 23:11:27 -05:00
|
|
|
OBJ_FREEZE(canonname);
|
|
|
|
}
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
ret = rsock_addrinfo_new(res->ai->ai_addr, res->ai->ai_addrlen,
|
|
|
|
res->ai->ai_family, res->ai->ai_socktype,
|
|
|
|
res->ai->ai_protocol,
|
2009-03-01 01:30:41 -05:00
|
|
|
canonname, inspectname);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
rb_freeaddrinfo(res);
|
2009-01-16 23:11:27 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
addrinfo_list_new(VALUE node, VALUE service, VALUE family, VALUE socktype, VALUE protocol, VALUE flags)
|
|
|
|
{
|
|
|
|
VALUE ret;
|
|
|
|
struct addrinfo *r;
|
|
|
|
VALUE inspectname;
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo *res = call_getaddrinfo(node, service, family, socktype, protocol, flags, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
inspectname = make_inspectname(node, service, res->ai);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
ret = rb_ary_new();
|
2014-02-19 04:38:24 -05:00
|
|
|
for (r = res->ai; r; r = r->ai_next) {
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE addr;
|
|
|
|
VALUE canonname = Qnil;
|
|
|
|
|
|
|
|
if (r->ai_canonname) {
|
|
|
|
canonname = rb_tainted_str_new_cstr(r->ai_canonname);
|
|
|
|
OBJ_FREEZE(canonname);
|
|
|
|
}
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
addr = rsock_addrinfo_new(r->ai_addr, r->ai_addrlen,
|
|
|
|
r->ai_family, r->ai_socktype, r->ai_protocol,
|
|
|
|
canonname, inspectname);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
rb_ary_push(ret, addr);
|
|
|
|
}
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
rb_freeaddrinfo(res);
|
2009-01-16 23:11:27 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
static void
|
2009-02-07 23:53:17 -05:00
|
|
|
init_unix_addrinfo(rb_addrinfo_t *rai, VALUE path, int socktype)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
struct sockaddr_un un;
|
2013-01-25 03:15:26 -05:00
|
|
|
socklen_t len;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
StringValue(path);
|
|
|
|
|
2012-04-25 23:34:12 -04:00
|
|
|
if (sizeof(un.sun_path) < (size_t)RSTRING_LEN(path))
|
2012-04-25 10:05:10 -04:00
|
|
|
rb_raise(rb_eArgError,
|
|
|
|
"too long unix socket path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
|
2012-04-25 23:34:12 -04:00
|
|
|
(size_t)RSTRING_LEN(path), sizeof(un.sun_path));
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2013-02-15 17:19:05 -05:00
|
|
|
INIT_SOCKADDR_UN(&un, sizeof(struct sockaddr_un));
|
2009-01-16 23:11:27 -05:00
|
|
|
memcpy((void*)&un.sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2013-01-25 18:33:50 -05:00
|
|
|
len = rsock_unix_sockaddr_len(path);
|
2013-01-25 03:15:26 -05:00
|
|
|
init_addrinfo(rai, (struct sockaddr *)&un, len,
|
2010-04-28 04:14:13 -04:00
|
|
|
PF_UNIX, socktype, 0, Qnil, Qnil);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.new(sockaddr) => addrinfo
|
|
|
|
* Addrinfo.new(sockaddr, family) => addrinfo
|
|
|
|
* Addrinfo.new(sockaddr, family, socktype) => addrinfo
|
|
|
|
* Addrinfo.new(sockaddr, family, socktype, protocol) => addrinfo
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* returns a new instance of Addrinfo.
|
2009-11-04 07:02:37 -05:00
|
|
|
* The instance contains sockaddr, family, socktype, protocol.
|
2009-01-16 23:11:27 -05:00
|
|
|
* sockaddr means struct sockaddr which can be used for connect(2), etc.
|
|
|
|
* family, socktype and protocol are integers which is used for arguments of socket(2).
|
|
|
|
*
|
|
|
|
* sockaddr is specified as an array or a string.
|
|
|
|
* The array should be compatible to the value of IPSocket#addr or UNIXSocket#addr.
|
|
|
|
* The string should be struct sockaddr as generated by
|
|
|
|
* Socket.sockaddr_in or Socket.unpack_sockaddr_un.
|
|
|
|
*
|
|
|
|
* sockaddr examples:
|
2010-04-22 04:04:13 -04:00
|
|
|
* - ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"]
|
|
|
|
* - ["AF_INET6", 42304, "ip6-localhost", "::1"]
|
|
|
|
* - ["AF_UNIX", "/tmp/sock"]
|
2009-01-16 23:11:27 -05:00
|
|
|
* - Socket.sockaddr_in("smtp", "2001:DB8::1")
|
|
|
|
* - Socket.sockaddr_in(80, "172.18.22.42")
|
|
|
|
* - Socket.sockaddr_in(80, "www.ruby-lang.org")
|
|
|
|
* - Socket.sockaddr_un("/tmp/sock")
|
|
|
|
*
|
|
|
|
* In an AF_INET/AF_INET6 sockaddr array, the 4th element,
|
2009-02-05 06:01:43 -05:00
|
|
|
* numeric IP address, is used to construct socket address in the Addrinfo instance.
|
2009-02-26 09:18:09 -05:00
|
|
|
* If the 3rd element, textual host name, is non-nil, it is also recorded but used only for Addrinfo#inspect.
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* family is specified as an integer to specify the protocol family such as Socket::PF_INET.
|
|
|
|
* It can be a symbol or a string which is the constant name
|
|
|
|
* with or without PF_ prefix such as :INET, :INET6, :UNIX, "PF_INET", etc.
|
2009-11-04 07:02:37 -05:00
|
|
|
* If omitted, PF_UNSPEC is assumed.
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* socktype is specified as an integer to specify the socket type such as Socket::SOCK_STREAM.
|
|
|
|
* It can be a symbol or a string which is the constant name
|
|
|
|
* with or without SOCK_ prefix such as :STREAM, :DGRAM, :RAW, "SOCK_STREAM", etc.
|
2009-11-04 07:02:37 -05:00
|
|
|
* If omitted, 0 is assumed.
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* protocol is specified as an integer to specify the protocol such as Socket::IPPROTO_TCP.
|
|
|
|
* It must be an integer, unlike family and socktype.
|
2009-11-04 07:02:37 -05:00
|
|
|
* If omitted, 0 is assumed.
|
2009-01-16 23:11:27 -05:00
|
|
|
* Note that 0 is reasonable value for most protocols, except raw socket.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai;
|
|
|
|
VALUE sockaddr_arg, sockaddr_ary, pfamily, socktype, protocol;
|
|
|
|
int i_pfamily, i_socktype, i_protocol;
|
|
|
|
struct sockaddr *sockaddr_ptr;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t sockaddr_len;
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE canonname = Qnil, inspectname = Qnil;
|
|
|
|
|
|
|
|
if (check_addrinfo(self))
|
|
|
|
rb_raise(rb_eTypeError, "already initialized socket address");
|
|
|
|
DATA_PTR(self) = rai = alloc_addrinfo();
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "13", &sockaddr_arg, &pfamily, &socktype, &protocol);
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
i_pfamily = NIL_P(pfamily) ? PF_UNSPEC : rsock_family_arg(pfamily);
|
|
|
|
i_socktype = NIL_P(socktype) ? 0 : rsock_socktype_arg(socktype);
|
2009-01-16 23:11:27 -05:00
|
|
|
i_protocol = NIL_P(protocol) ? 0 : NUM2INT(protocol);
|
|
|
|
|
|
|
|
sockaddr_ary = rb_check_array_type(sockaddr_arg);
|
|
|
|
if (!NIL_P(sockaddr_ary)) {
|
|
|
|
VALUE afamily = rb_ary_entry(sockaddr_ary, 0);
|
|
|
|
int af;
|
|
|
|
StringValue(afamily);
|
2009-03-01 01:30:41 -05:00
|
|
|
if (rsock_family_to_int(RSTRING_PTR(afamily), RSTRING_LEN(afamily), &af) == -1)
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_raise(rb_eSocket, "unknown address family: %s", StringValueCStr(afamily));
|
|
|
|
switch (af) {
|
|
|
|
case AF_INET: /* ["AF_INET", 46102, "localhost.localdomain", "127.0.0.1"] */
|
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6: /* ["AF_INET6", 42304, "ip6-localhost", "::1"] */
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
VALUE service = rb_ary_entry(sockaddr_ary, 1);
|
|
|
|
VALUE nodename = rb_ary_entry(sockaddr_ary, 2);
|
|
|
|
VALUE numericnode = rb_ary_entry(sockaddr_ary, 3);
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
service = INT2NUM(NUM2INT(service));
|
|
|
|
if (!NIL_P(nodename))
|
|
|
|
StringValue(nodename);
|
|
|
|
StringValue(numericnode);
|
|
|
|
flags = AI_NUMERICHOST;
|
|
|
|
#ifdef AI_NUMERICSERV
|
|
|
|
flags |= AI_NUMERICSERV;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
init_addrinfo_getaddrinfo(rai, numericnode, service,
|
|
|
|
INT2NUM(i_pfamily ? i_pfamily : af), INT2NUM(i_socktype), INT2NUM(i_protocol),
|
|
|
|
INT2NUM(flags),
|
2009-01-17 01:03:42 -05:00
|
|
|
nodename, service);
|
2009-01-16 23:11:27 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
case AF_UNIX: /* ["AF_UNIX", "/tmp/sock"] */
|
|
|
|
{
|
|
|
|
VALUE path = rb_ary_entry(sockaddr_ary, 1);
|
|
|
|
StringValue(path);
|
2009-02-07 23:53:17 -05:00
|
|
|
init_unix_addrinfo(rai, path, SOCK_STREAM);
|
2009-01-16 23:11:27 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eSocket, "unexpected address family");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValue(sockaddr_arg);
|
|
|
|
sockaddr_ptr = (struct sockaddr *)RSTRING_PTR(sockaddr_arg);
|
2013-04-06 03:58:51 -04:00
|
|
|
sockaddr_len = RSTRING_SOCKLEN(sockaddr_arg);
|
2009-01-16 23:11:27 -05:00
|
|
|
init_addrinfo(rai, sockaddr_ptr, sockaddr_len,
|
|
|
|
i_pfamily, i_socktype, i_protocol,
|
|
|
|
canonname, inspectname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_afamily(struct sockaddr *addr, socklen_t len)
|
|
|
|
{
|
2010-08-19 06:54:56 -04:00
|
|
|
if ((socklen_t)((char*)&addr->sa_family + sizeof(addr->sa_family) - (char*)addr) <= len)
|
2009-01-16 23:11:27 -05:00
|
|
|
return addr->sa_family;
|
|
|
|
else
|
|
|
|
return AF_UNSPEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ai_get_afamily(rb_addrinfo_t *rai)
|
|
|
|
{
|
2013-02-24 12:51:17 -05:00
|
|
|
return get_afamily(&rai->addr.addr, rai->sockaddr_len);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2009-01-18 03:03:24 -05:00
|
|
|
inspect_sockaddr(VALUE addrinfo, VALUE ret)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-01-18 03:03:24 -05:00
|
|
|
rb_addrinfo_t *rai = get_addrinfo(addrinfo);
|
2013-05-11 04:32:26 -04:00
|
|
|
union_sockaddr *sockaddr = &rai->addr;
|
|
|
|
socklen_t socklen = rai->sockaddr_len;
|
|
|
|
return rsock_inspect_sockaddr((struct sockaddr *)sockaddr, socklen, ret);
|
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2013-05-11 04:32:26 -04:00
|
|
|
VALUE
|
|
|
|
rsock_inspect_sockaddr(struct sockaddr *sockaddr_arg, socklen_t socklen, VALUE ret)
|
|
|
|
{
|
|
|
|
union_sockaddr *sockaddr = (union_sockaddr *)sockaddr_arg;
|
|
|
|
if (socklen == 0) {
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_str_cat2(ret, "empty-sockaddr");
|
|
|
|
}
|
2013-05-11 04:32:26 -04:00
|
|
|
else if ((long)socklen < ((char*)&sockaddr->addr.sa_family + sizeof(sockaddr->addr.sa_family)) - (char*)sockaddr)
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_str_cat2(ret, "too-short-sockaddr");
|
|
|
|
else {
|
2013-05-11 04:32:26 -04:00
|
|
|
switch (sockaddr->addr.sa_family) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
{
|
|
|
|
rb_str_cat2(ret, "UNSPEC");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in *addr;
|
|
|
|
int port;
|
2013-05-11 04:32:26 -04:00
|
|
|
addr = &sockaddr->in;
|
2013-05-11 09:04:39 -04:00
|
|
|
if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+0+1) <= socklen)
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_catf(ret, "%d", ((unsigned char*)&addr->sin_addr)[0]);
|
|
|
|
else
|
|
|
|
rb_str_cat2(ret, "?");
|
2013-05-11 09:04:39 -04:00
|
|
|
if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+1+1) <= socklen)
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[1]);
|
|
|
|
else
|
|
|
|
rb_str_cat2(ret, ".?");
|
2013-05-11 09:04:39 -04:00
|
|
|
if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+2+1) <= socklen)
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[2]);
|
|
|
|
else
|
|
|
|
rb_str_cat2(ret, ".?");
|
2013-05-11 09:04:39 -04:00
|
|
|
if ((socklen_t)(((char*)&addr->sin_addr)-(char*)addr+3+1) <= socklen)
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_catf(ret, ".%d", ((unsigned char*)&addr->sin_addr)[3]);
|
|
|
|
else
|
|
|
|
rb_str_cat2(ret, ".?");
|
|
|
|
|
2013-05-11 09:04:39 -04:00
|
|
|
if ((socklen_t)(((char*)&addr->sin_port)-(char*)addr+(int)sizeof(addr->sin_port)) < socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
port = ntohs(addr->sin_port);
|
|
|
|
if (port)
|
|
|
|
rb_str_catf(ret, ":%d", port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_cat2(ret, ":?");
|
|
|
|
}
|
|
|
|
if ((socklen_t)sizeof(struct sockaddr_in) != socklen)
|
|
|
|
rb_str_catf(ret, " (%d bytes for %d bytes sockaddr_in)",
|
|
|
|
(int)socklen,
|
|
|
|
(int)sizeof(struct sockaddr_in));
|
2009-01-16 23:11:27 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-28 21:25:39 -05:00
|
|
|
#ifdef AF_INET6
|
2009-01-16 23:11:27 -05:00
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *addr;
|
|
|
|
char hbuf[1024];
|
|
|
|
int port;
|
|
|
|
int error;
|
2013-05-11 04:32:26 -04:00
|
|
|
if (socklen < (socklen_t)sizeof(struct sockaddr_in6)) {
|
|
|
|
rb_str_catf(ret, "too-short-AF_INET6-sockaddr %d bytes", (int)socklen);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-05-11 04:32:26 -04:00
|
|
|
addr = &sockaddr->in6;
|
2009-01-16 23:11:27 -05:00
|
|
|
/* use getnameinfo for scope_id.
|
|
|
|
* RFC 4007: IPv6 Scoped Address Architecture
|
|
|
|
* draft-ietf-ipv6-scope-api-00.txt: Scoped Address Extensions to the IPv6 Basic Socket API
|
|
|
|
*/
|
2013-05-11 04:32:26 -04:00
|
|
|
error = getnameinfo(&sockaddr->addr, socklen,
|
2010-04-28 04:14:13 -04:00
|
|
|
hbuf, (socklen_t)sizeof(hbuf), NULL, 0,
|
2009-01-16 23:11:27 -05:00
|
|
|
NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
if (error) {
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error("getnameinfo", error);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
if (addr->sin6_port == 0) {
|
|
|
|
rb_str_cat2(ret, hbuf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port = ntohs(addr->sin6_port);
|
|
|
|
rb_str_catf(ret, "[%s]:%d", hbuf, port);
|
|
|
|
}
|
2013-05-11 04:32:26 -04:00
|
|
|
if ((socklen_t)sizeof(struct sockaddr_in6) < socklen)
|
|
|
|
rb_str_catf(ret, "(sockaddr %d bytes too long)", (int)(socklen - sizeof(struct sockaddr_in6)));
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
2013-05-11 04:32:26 -04:00
|
|
|
struct sockaddr_un *addr = &sockaddr->un;
|
2012-04-25 23:34:12 -04:00
|
|
|
char *p, *s, *e;
|
2009-01-16 23:11:27 -05:00
|
|
|
s = addr->sun_path;
|
2013-05-11 04:32:26 -04:00
|
|
|
e = (char*)addr + socklen;
|
2012-04-25 23:34:12 -04:00
|
|
|
while (s < e && *(e-1) == '\0')
|
|
|
|
e--;
|
2009-01-16 23:11:27 -05:00
|
|
|
if (e < s)
|
|
|
|
rb_str_cat2(ret, "too-short-AF_UNIX-sockaddr");
|
|
|
|
else if (s == e)
|
|
|
|
rb_str_cat2(ret, "empty-path-AF_UNIX-sockaddr");
|
|
|
|
else {
|
|
|
|
int printable_only = 1;
|
|
|
|
p = s;
|
2012-04-25 23:34:12 -04:00
|
|
|
while (p < e) {
|
2009-01-16 23:11:27 -05:00
|
|
|
printable_only = printable_only && ISPRINT(*p) && !ISSPACE(*p);
|
|
|
|
p++;
|
|
|
|
}
|
2012-04-25 23:34:12 -04:00
|
|
|
if (printable_only) { /* only printable, no space */
|
|
|
|
if (s[0] != '/') /* relative path */
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_cat2(ret, "UNIX ");
|
2012-04-25 23:34:12 -04:00
|
|
|
rb_str_cat(ret, s, p - s);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_cat2(ret, "UNIX");
|
2009-01-16 23:11:27 -05:00
|
|
|
while (s < e)
|
|
|
|
rb_str_catf(ret, ":%02x", (unsigned char)*s++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-11 04:32:26 -04:00
|
|
|
#ifdef AF_PACKET
|
|
|
|
/* GNU/Linux */
|
|
|
|
case AF_PACKET:
|
|
|
|
{
|
|
|
|
struct sockaddr_ll *addr;
|
|
|
|
const char *sep = "[";
|
|
|
|
#define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0);
|
|
|
|
|
|
|
|
addr = (struct sockaddr_ll *)sockaddr;
|
|
|
|
|
|
|
|
rb_str_cat2(ret, "PACKET");
|
|
|
|
|
2013-05-11 09:04:39 -04:00
|
|
|
if (offsetof(struct sockaddr_ll, sll_protocol) + sizeof(addr->sll_protocol) <= (size_t)socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
CATSEP;
|
|
|
|
rb_str_catf(ret, "protocol=%d", ntohs(addr->sll_protocol));
|
|
|
|
}
|
2013-05-11 09:04:39 -04:00
|
|
|
if (offsetof(struct sockaddr_ll, sll_ifindex) + sizeof(addr->sll_ifindex) <= (size_t)socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
char buf[IFNAMSIZ];
|
|
|
|
CATSEP;
|
|
|
|
if (if_indextoname(addr->sll_ifindex, buf) == NULL)
|
|
|
|
rb_str_catf(ret, "ifindex=%d", addr->sll_ifindex);
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, "%s", buf);
|
|
|
|
}
|
2013-05-11 09:04:39 -04:00
|
|
|
if (offsetof(struct sockaddr_ll, sll_hatype) + sizeof(addr->sll_hatype) <= (size_t)socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
CATSEP;
|
|
|
|
rb_str_catf(ret, "hatype=%d", addr->sll_hatype);
|
|
|
|
}
|
2013-05-11 09:04:39 -04:00
|
|
|
if (offsetof(struct sockaddr_ll, sll_pkttype) + sizeof(addr->sll_pkttype) <= (size_t)socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
CATSEP;
|
|
|
|
if (addr->sll_pkttype == PACKET_HOST)
|
|
|
|
rb_str_cat2(ret, "HOST");
|
|
|
|
else if (addr->sll_pkttype == PACKET_BROADCAST)
|
|
|
|
rb_str_cat2(ret, "BROADCAST");
|
|
|
|
else if (addr->sll_pkttype == PACKET_MULTICAST)
|
|
|
|
rb_str_cat2(ret, "MULTICAST");
|
|
|
|
else if (addr->sll_pkttype == PACKET_OTHERHOST)
|
|
|
|
rb_str_cat2(ret, "OTHERHOST");
|
|
|
|
else if (addr->sll_pkttype == PACKET_OUTGOING)
|
|
|
|
rb_str_cat2(ret, "OUTGOING");
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, "pkttype=%d", addr->sll_pkttype);
|
|
|
|
}
|
|
|
|
if (socklen != (socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen)) {
|
|
|
|
CATSEP;
|
2013-05-11 09:04:39 -04:00
|
|
|
if (offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen) <= (size_t)socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_catf(ret, "halen=%d", addr->sll_halen);
|
|
|
|
}
|
|
|
|
}
|
2013-05-11 09:04:39 -04:00
|
|
|
if (offsetof(struct sockaddr_ll, sll_addr) < (size_t)socklen) {
|
2013-05-11 04:32:26 -04:00
|
|
|
socklen_t len, i;
|
|
|
|
CATSEP;
|
|
|
|
rb_str_cat2(ret, "hwaddr");
|
|
|
|
len = addr->sll_halen;
|
2013-05-11 09:04:39 -04:00
|
|
|
if ((size_t)socklen < offsetof(struct sockaddr_ll, sll_addr) + len)
|
2013-05-11 04:32:26 -04:00
|
|
|
len = socklen - offsetof(struct sockaddr_ll, sll_addr);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
rb_str_cat2(ret, i == 0 ? "=" : ":");
|
|
|
|
rb_str_catf(ret, "%02x", addr->sll_addr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socklen < (socklen_t)(offsetof(struct sockaddr_ll, sll_halen) + sizeof(addr->sll_halen)) ||
|
|
|
|
(socklen_t)(offsetof(struct sockaddr_ll, sll_addr) + addr->sll_halen) != socklen) {
|
|
|
|
CATSEP;
|
|
|
|
rb_str_catf(ret, "(%d bytes for %d bytes sockaddr_ll)",
|
|
|
|
(int)socklen, (int)sizeof(struct sockaddr_ll));
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_str_cat2(ret, "]");
|
|
|
|
#undef CATSEP
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-12 03:09:26 -04:00
|
|
|
#if defined(AF_LINK) && defined(HAVE_TYPE_STRUCT_SOCKADDR_DL)
|
2013-05-11 04:32:26 -04:00
|
|
|
/* AF_LINK is defined in 4.4BSD derivations since Net2.
|
|
|
|
link_ntoa is also defined at Net2.
|
|
|
|
However Debian GNU/kFreeBSD defines AF_LINK but
|
|
|
|
don't have link_ntoa. */
|
|
|
|
case AF_LINK:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Simple implementation using link_ntoa():
|
|
|
|
* This doesn't work on Debian GNU/kFreeBSD 6.0.7 (squeeze).
|
|
|
|
* Also, the format is bit different.
|
|
|
|
*
|
|
|
|
* rb_str_catf(ret, "LINK %s", link_ntoa(&sockaddr->dl));
|
|
|
|
* break;
|
|
|
|
*/
|
|
|
|
struct sockaddr_dl *addr = &sockaddr->dl;
|
|
|
|
char *np = NULL, *ap = NULL, *endp;
|
|
|
|
int nlen = 0, alen = 0;
|
|
|
|
int i, off;
|
|
|
|
const char *sep = "[";
|
|
|
|
#define CATSEP do { rb_str_cat2(ret, sep); sep = " "; } while (0);
|
|
|
|
|
|
|
|
rb_str_cat2(ret, "LINK");
|
|
|
|
|
|
|
|
endp = ((char *)addr) + socklen;
|
|
|
|
|
|
|
|
if (offsetof(struct sockaddr_dl, sdl_data) < socklen) {
|
|
|
|
np = addr->sdl_data;
|
|
|
|
nlen = addr->sdl_nlen;
|
|
|
|
if (endp - np < nlen)
|
2013-05-11 14:28:32 -04:00
|
|
|
nlen = (int)(endp - np);
|
2013-05-11 04:32:26 -04:00
|
|
|
}
|
|
|
|
off = addr->sdl_nlen;
|
|
|
|
|
|
|
|
if (offsetof(struct sockaddr_dl, sdl_data) + off < socklen) {
|
|
|
|
ap = addr->sdl_data + off;
|
|
|
|
alen = addr->sdl_alen;
|
|
|
|
if (endp - ap < alen)
|
2013-05-11 14:28:32 -04:00
|
|
|
alen = (int)(endp - ap);
|
2013-05-11 04:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CATSEP;
|
|
|
|
if (np)
|
|
|
|
rb_str_catf(ret, "%.*s", nlen, np);
|
|
|
|
else
|
|
|
|
rb_str_cat2(ret, "?");
|
|
|
|
|
|
|
|
if (ap && 0 < alen) {
|
|
|
|
CATSEP;
|
|
|
|
for (i = 0; i < alen; i++)
|
|
|
|
rb_str_catf(ret, "%s%02x", i == 0 ? "" : ":", (unsigned char)ap[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_nlen) + sizeof(addr->sdl_nlen)) ||
|
|
|
|
socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_alen) + sizeof(addr->sdl_alen)) ||
|
|
|
|
socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_slen) + sizeof(addr->sdl_slen)) ||
|
|
|
|
/* longer length is possible behavior because struct sockaddr_dl has "minimum work area, can be larger" as the last field.
|
|
|
|
* cf. Net2:/usr/src/sys/net/if_dl.h. */
|
|
|
|
socklen < (socklen_t)(offsetof(struct sockaddr_dl, sdl_data) + addr->sdl_nlen + addr->sdl_alen + addr->sdl_slen)) {
|
|
|
|
CATSEP;
|
|
|
|
rb_str_catf(ret, "(%d bytes for %d bytes sockaddr_dl)",
|
|
|
|
(int)socklen, (int)sizeof(struct sockaddr_dl));
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_str_cat2(ret, "]");
|
|
|
|
#undef CATSEP
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
default:
|
|
|
|
{
|
2013-05-11 04:32:26 -04:00
|
|
|
ID id = rsock_intern_family(sockaddr->addr.sa_family);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (id == 0)
|
2013-05-11 04:32:26 -04:00
|
|
|
rb_str_catf(ret, "unknown address family %d", sockaddr->addr.sa_family);
|
2009-01-16 23:11:27 -05:00
|
|
|
else
|
|
|
|
rb_str_catf(ret, "%s address format unknown", rb_id2name(id));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-18 03:03:24 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.inspect => string
|
|
|
|
*
|
|
|
|
* returns a string which shows addrinfo in human-readable form.
|
|
|
|
*
|
2012-02-02 06:51:25 -05:00
|
|
|
* Addrinfo.tcp("localhost", 80).inspect #=> "#<Addrinfo: 127.0.0.1:80 TCP (localhost)>"
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.unix("/tmp/sock").inspect #=> "#<Addrinfo: /tmp/sock SOCK_STREAM>"
|
2009-01-18 03:03:24 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
if (rai->pfamily && ai_get_afamily(rai) != rai->pfamily) {
|
2009-03-01 01:30:41 -05:00
|
|
|
ID id = rsock_intern_protocol_family(rai->pfamily);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " PF_\?\?\?(%d)", rai->pfamily);
|
|
|
|
}
|
|
|
|
|
|
|
|
internet_p = rai->pfamily == PF_INET;
|
|
|
|
#ifdef INET6
|
|
|
|
internet_p = internet_p || rai->pfamily == PF_INET6;
|
|
|
|
#endif
|
|
|
|
if (internet_p && rai->socktype == SOCK_STREAM &&
|
|
|
|
(rai->protocol == 0 || rai->protocol == IPPROTO_TCP)) {
|
|
|
|
rb_str_cat2(ret, " TCP");
|
|
|
|
}
|
|
|
|
else if (internet_p && rai->socktype == SOCK_DGRAM &&
|
|
|
|
(rai->protocol == 0 || rai->protocol == IPPROTO_UDP)) {
|
|
|
|
rb_str_cat2(ret, " UDP");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (rai->socktype) {
|
2009-03-01 01:30:41 -05:00
|
|
|
ID id = rsock_intern_socktype(rai->socktype);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(id));
|
|
|
|
else
|
|
|
|
rb_str_catf(ret, " SOCK_\?\?\?(%d)", rai->socktype);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rai->protocol) {
|
|
|
|
if (internet_p) {
|
2009-03-01 01:30:41 -05:00
|
|
|
ID id = rsock_intern_ipproto(rai->protocol);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (id)
|
|
|
|
rb_str_catf(ret, " %s", rb_id2name(id));
|
|
|
|
else
|
|
|
|
goto unknown_protocol;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unknown_protocol:
|
|
|
|
rb_str_catf(ret, " UNKNOWN_PROTOCOL(%d)", rai->protocol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(rai->canonname)) {
|
|
|
|
VALUE name = rai->canonname;
|
|
|
|
rb_str_catf(ret, " %s", StringValueCStr(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(rai->inspectname)) {
|
|
|
|
VALUE name = rai->inspectname;
|
|
|
|
rb_str_catf(ret, " (%s)", StringValueCStr(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_str_buf_cat2(ret, ">");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-18 03:03:24 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.inspect_sockaddr => string
|
|
|
|
*
|
|
|
|
* returns a string which shows the sockaddr in _addrinfo_ with human-readable form.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* 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"
|
2009-01-18 03:03:24 -05:00
|
|
|
*
|
|
|
|
*/
|
2013-04-06 01:50:42 -04:00
|
|
|
VALUE
|
|
|
|
rsock_addrinfo_inspect_sockaddr(VALUE self)
|
2009-01-18 03:03:24 -05:00
|
|
|
{
|
|
|
|
return inspect_sockaddr(self, rb_str_new("", 0));
|
|
|
|
}
|
|
|
|
|
2009-01-17 13:14:34 -05:00
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
addrinfo_mdump(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
VALUE sockaddr, afamily, pfamily, socktype, protocol, canonname, inspectname;
|
|
|
|
int afamily_int = ai_get_afamily(rai);
|
|
|
|
ID id;
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
id = rsock_intern_protocol_family(rai->pfamily);
|
2009-01-17 13:14:34 -05:00
|
|
|
if (id == 0)
|
|
|
|
rb_raise(rb_eSocket, "unknown protocol family: %d", rai->pfamily);
|
2009-01-17 13:35:15 -05:00
|
|
|
pfamily = rb_id2str(id);
|
2009-01-17 13:14:34 -05:00
|
|
|
|
|
|
|
if (rai->socktype == 0)
|
|
|
|
socktype = INT2FIX(0);
|
|
|
|
else {
|
2009-03-01 01:30:41 -05:00
|
|
|
id = rsock_intern_socktype(rai->socktype);
|
2009-01-17 13:14:34 -05:00
|
|
|
if (id == 0)
|
|
|
|
rb_raise(rb_eSocket, "unknown socktype: %d", rai->socktype);
|
2009-01-17 13:35:15 -05:00
|
|
|
socktype = rb_id2str(id);
|
2009-01-17 13:14:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rai->protocol == 0)
|
|
|
|
protocol = INT2FIX(0);
|
|
|
|
else if (IS_IP_FAMILY(afamily_int)) {
|
2009-03-01 01:30:41 -05:00
|
|
|
id = rsock_intern_ipproto(rai->protocol);
|
2009-01-17 13:14:34 -05:00
|
|
|
if (id == 0)
|
|
|
|
rb_raise(rb_eSocket, "unknown IP protocol: %d", rai->protocol);
|
2009-01-17 13:35:15 -05:00
|
|
|
protocol = rb_id2str(id);
|
2009-01-17 13:14:34 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eSocket, "unknown protocol: %d", rai->protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
canonname = rai->canonname;
|
|
|
|
|
|
|
|
inspectname = rai->inspectname;
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
id = rsock_intern_family(afamily_int);
|
2009-01-17 13:14:34 -05:00
|
|
|
if (id == 0)
|
|
|
|
rb_raise(rb_eSocket, "unknown address family: %d", afamily_int);
|
2009-01-17 13:35:15 -05:00
|
|
|
afamily = rb_id2str(id);
|
2009-01-17 13:14:34 -05:00
|
|
|
|
|
|
|
switch(afamily_int) {
|
2009-01-18 19:23:48 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2009-01-17 13:14:34 -05:00
|
|
|
case AF_UNIX:
|
|
|
|
{
|
2013-02-24 12:51:17 -05:00
|
|
|
struct sockaddr_un *su = &rai->addr.un;
|
2009-01-17 13:14:34 -05:00
|
|
|
char *s, *e;
|
|
|
|
s = su->sun_path;
|
2012-04-25 23:34:12 -04:00
|
|
|
e = (char*)su + rai->sockaddr_len;
|
2009-01-17 13:14:34 -05:00
|
|
|
while (s < e && *(e-1) == '\0')
|
|
|
|
e--;
|
|
|
|
sockaddr = rb_str_new(s, e-s);
|
|
|
|
break;
|
|
|
|
}
|
2009-01-18 19:23:48 -05:00
|
|
|
#endif
|
2009-01-17 13:14:34 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
char hbuf[NI_MAXHOST], pbuf[NI_MAXSERV];
|
|
|
|
int error;
|
2013-02-24 12:51:17 -05:00
|
|
|
error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
|
2010-04-28 04:14:13 -04:00
|
|
|
hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf),
|
2009-01-17 13:14:34 -05:00
|
|
|
NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
if (error) {
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error("getnameinfo", error);
|
2009-01-17 13:14:34 -05:00
|
|
|
}
|
|
|
|
sockaddr = rb_assoc_new(rb_str_new_cstr(hbuf), rb_str_new_cstr(pbuf));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_ary_new3(7, afamily, sockaddr, pfamily, socktype, protocol, canonname, inspectname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :nodoc: */
|
|
|
|
static VALUE
|
|
|
|
addrinfo_mload(VALUE self, VALUE ary)
|
|
|
|
{
|
|
|
|
VALUE v;
|
|
|
|
VALUE canonname, inspectname;
|
|
|
|
int afamily, pfamily, socktype, protocol;
|
2013-02-24 12:51:17 -05:00
|
|
|
union_sockaddr ss;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t len;
|
2009-01-17 13:14:34 -05:00
|
|
|
rb_addrinfo_t *rai;
|
|
|
|
|
|
|
|
if (check_addrinfo(self))
|
|
|
|
rb_raise(rb_eTypeError, "already initialized socket address");
|
|
|
|
|
|
|
|
ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 0);
|
2009-01-17 13:35:15 -05:00
|
|
|
StringValue(v);
|
2009-03-01 01:30:41 -05:00
|
|
|
if (rsock_family_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &afamily) == -1)
|
2009-01-17 13:14:34 -05:00
|
|
|
rb_raise(rb_eTypeError, "unexpected address family");
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 2);
|
2009-01-17 13:35:15 -05:00
|
|
|
StringValue(v);
|
2009-03-01 01:30:41 -05:00
|
|
|
if (rsock_family_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &pfamily) == -1)
|
2009-01-17 13:14:34 -05:00
|
|
|
rb_raise(rb_eTypeError, "unexpected protocol family");
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 3);
|
|
|
|
if (v == INT2FIX(0))
|
|
|
|
socktype = 0;
|
|
|
|
else {
|
2009-01-17 13:35:15 -05:00
|
|
|
StringValue(v);
|
2009-03-01 01:30:41 -05:00
|
|
|
if (rsock_socktype_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &socktype) == -1)
|
2009-01-17 13:14:34 -05:00
|
|
|
rb_raise(rb_eTypeError, "unexpected socktype");
|
|
|
|
}
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 4);
|
|
|
|
if (v == INT2FIX(0))
|
|
|
|
protocol = 0;
|
|
|
|
else {
|
2009-01-17 13:35:15 -05:00
|
|
|
StringValue(v);
|
2009-01-17 13:14:34 -05:00
|
|
|
if (IS_IP_FAMILY(afamily)) {
|
2009-03-01 01:30:41 -05:00
|
|
|
if (rsock_ipproto_to_int(RSTRING_PTR(v), RSTRING_LEN(v), &protocol) == -1)
|
2009-01-17 13:14:34 -05:00
|
|
|
rb_raise(rb_eTypeError, "unexpected protocol");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eTypeError, "unexpected protocol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 5);
|
|
|
|
if (NIL_P(v))
|
|
|
|
canonname = Qnil;
|
|
|
|
else {
|
|
|
|
StringValue(v);
|
|
|
|
canonname = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 6);
|
|
|
|
if (NIL_P(v))
|
|
|
|
inspectname = Qnil;
|
|
|
|
else {
|
|
|
|
StringValue(v);
|
|
|
|
inspectname = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
v = rb_ary_entry(ary, 1);
|
|
|
|
switch(afamily) {
|
2009-01-18 19:23:48 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2009-01-17 13:14:34 -05:00
|
|
|
case AF_UNIX:
|
|
|
|
{
|
2009-03-02 07:13:00 -05:00
|
|
|
struct sockaddr_un uaddr;
|
2013-02-15 17:19:05 -05:00
|
|
|
INIT_SOCKADDR_UN(&uaddr, sizeof(struct sockaddr_un));
|
2009-01-17 13:14:34 -05:00
|
|
|
|
|
|
|
StringValue(v);
|
2012-04-25 23:34:12 -04:00
|
|
|
if (sizeof(uaddr.sun_path) < (size_t)RSTRING_LEN(v))
|
2012-04-25 10:05:10 -04:00
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
|
2012-04-25 23:34:12 -04:00
|
|
|
(size_t)RSTRING_LEN(v), sizeof(uaddr.sun_path));
|
2009-03-02 07:13:00 -05:00
|
|
|
memcpy(uaddr.sun_path, RSTRING_PTR(v), RSTRING_LEN(v));
|
2010-04-28 04:14:13 -04:00
|
|
|
len = (socklen_t)sizeof(uaddr);
|
2009-03-02 07:13:00 -05:00
|
|
|
memcpy(&ss, &uaddr, len);
|
2009-01-17 13:14:34 -05:00
|
|
|
break;
|
|
|
|
}
|
2009-01-18 19:23:48 -05:00
|
|
|
#endif
|
2009-01-17 13:14:34 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
VALUE pair = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
|
2014-02-19 04:38:24 -05:00
|
|
|
struct rb_addrinfo *res;
|
2009-01-17 13:14:34 -05:00
|
|
|
int flags = AI_NUMERICHOST;
|
|
|
|
#ifdef AI_NUMERICSERV
|
|
|
|
flags |= AI_NUMERICSERV;
|
|
|
|
#endif
|
|
|
|
res = call_getaddrinfo(rb_ary_entry(pair, 0), rb_ary_entry(pair, 1),
|
|
|
|
INT2NUM(pfamily), INT2NUM(socktype), INT2NUM(protocol),
|
|
|
|
INT2NUM(flags), 1);
|
|
|
|
|
2014-02-19 04:38:24 -05:00
|
|
|
len = res->ai->ai_addrlen;
|
|
|
|
memcpy(&ss, res->ai->ai_addr, res->ai->ai_addrlen);
|
2009-01-17 13:14:34 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DATA_PTR(self) = rai = alloc_addrinfo();
|
2013-02-24 12:51:17 -05:00
|
|
|
init_addrinfo(rai, &ss.addr, len,
|
2009-01-17 13:14:34 -05:00
|
|
|
pfamily, socktype, protocol,
|
|
|
|
canonname, inspectname);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.afamily => integer
|
|
|
|
*
|
|
|
|
* returns the address family as an integer.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("localhost", 80).afamily == Socket::AF_INET #=> true
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_afamily(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return INT2NUM(ai_get_afamily(rai));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.pfamily => integer
|
|
|
|
*
|
|
|
|
* returns the protocol family as an integer.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("localhost", 80).pfamily == Socket::PF_INET #=> true
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_pfamily(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return INT2NUM(rai->pfamily);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.socktype => integer
|
|
|
|
*
|
|
|
|
* returns the socket type as an integer.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("localhost", 80).socktype == Socket::SOCK_STREAM #=> true
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_socktype(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return INT2NUM(rai->socktype);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.protocol => integer
|
|
|
|
*
|
|
|
|
* returns the socket type as an integer.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("localhost", 80).protocol == Socket::IPPROTO_TCP #=> true
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_protocol(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return INT2NUM(rai->protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.to_sockaddr => string
|
2011-06-17 10:53:56 -04:00
|
|
|
* addrinfo.to_s => string
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* returns the socket address as packed struct sockaddr string.
|
|
|
|
*
|
2010-04-22 04:04:13 -04:00
|
|
|
* Addrinfo.tcp("localhost", 80).to_sockaddr
|
2009-01-16 23:11:27 -05:00
|
|
|
* #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_to_sockaddr(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
VALUE ret;
|
|
|
|
ret = rb_str_new((char*)&rai->addr, rai->sockaddr_len);
|
|
|
|
OBJ_INFECT(ret, self);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.canonname => string or nil
|
|
|
|
*
|
|
|
|
* returns the canonical name as an string.
|
|
|
|
*
|
|
|
|
* nil is returned if no canonical name.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* The canonical name is set by Addrinfo.getaddrinfo when AI_CANONNAME is specified.
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* list = Addrinfo.getaddrinfo("www.ruby-lang.org", 80, :INET, :STREAM, nil, Socket::AI_CANONNAME)
|
2012-02-02 06:51:25 -05:00
|
|
|
* p list[0] #=> #<Addrinfo: 221.186.184.68:80 TCP carbon.ruby-lang.org (www.ruby-lang.org)>
|
2009-01-16 23:11:27 -05:00
|
|
|
* p list[0].canonname #=> "carbon.ruby-lang.org"
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_canonname(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return rai->canonname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.ip? => true or false
|
|
|
|
*
|
|
|
|
* returns true if addrinfo is internet (IPv4/IPv6) address.
|
|
|
|
* returns false otherwise.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).ip? #=> true
|
|
|
|
* Addrinfo.tcp("::1", 80).ip? #=> true
|
|
|
|
* Addrinfo.unix("/tmp/sock").ip? #=> false
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ip_p(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
return IS_IP_FAMILY(family) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.ipv4? => true or false
|
|
|
|
*
|
|
|
|
* returns true if addrinfo is IPv4 address.
|
|
|
|
* returns false otherwise.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).ipv4? #=> true
|
|
|
|
* Addrinfo.tcp("::1", 80).ipv4? #=> false
|
|
|
|
* Addrinfo.unix("/tmp/sock").ipv4? #=> false
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv4_p(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return ai_get_afamily(rai) == AF_INET ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.ipv6? => true or false
|
|
|
|
*
|
|
|
|
* returns true if addrinfo is IPv6 address.
|
|
|
|
* returns false otherwise.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).ipv6? #=> false
|
|
|
|
* Addrinfo.tcp("::1", 80).ipv6? #=> true
|
|
|
|
* Addrinfo.unix("/tmp/sock").ipv6? #=> false
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_p(VALUE self)
|
|
|
|
{
|
|
|
|
#ifdef AF_INET6
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
return ai_get_afamily(rai) == AF_INET6 ? Qtrue : Qfalse;
|
|
|
|
#else
|
|
|
|
return Qfalse;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.unix? => true or false
|
|
|
|
*
|
|
|
|
* returns true if addrinfo is UNIX address.
|
|
|
|
* returns false otherwise.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).unix? #=> false
|
|
|
|
* Addrinfo.tcp("::1", 80).unix? #=> false
|
|
|
|
* Addrinfo.unix("/tmp/sock").unix? #=> true
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_unix_p(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
#ifdef AF_UNIX
|
|
|
|
return ai_get_afamily(rai) == AF_UNIX ? Qtrue : Qfalse;
|
|
|
|
#else
|
|
|
|
return Qfalse;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.getnameinfo => [nodename, service]
|
|
|
|
* addrinfo.getnameinfo(flags) => [nodename, service]
|
|
|
|
*
|
|
|
|
* returns nodename and service as a pair of strings.
|
|
|
|
* This converts struct sockaddr in addrinfo to textual representation.
|
|
|
|
*
|
|
|
|
* flags should be bitwise OR of Socket::NI_??? constants.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).getnameinfo #=> ["localhost", "www"]
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).getnameinfo(Socket::NI_NUMERICSERV)
|
2009-01-16 23:11:27 -05:00
|
|
|
* #=> ["localhost", "80"]
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_getnameinfo(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
VALUE vflags;
|
|
|
|
char hbuf[1024], pbuf[1024];
|
|
|
|
int flags, error;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &vflags);
|
|
|
|
|
|
|
|
flags = NIL_P(vflags) ? 0 : NUM2INT(vflags);
|
|
|
|
|
|
|
|
if (rai->socktype == SOCK_DGRAM)
|
|
|
|
flags |= NI_DGRAM;
|
|
|
|
|
2013-02-24 12:51:17 -05:00
|
|
|
error = getnameinfo(&rai->addr.addr, rai->sockaddr_len,
|
2010-04-28 04:14:13 -04:00
|
|
|
hbuf, (socklen_t)sizeof(hbuf), pbuf, (socklen_t)sizeof(pbuf),
|
2009-01-16 23:11:27 -05:00
|
|
|
flags);
|
|
|
|
if (error) {
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error("getnameinfo", error);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.ip_unpack => [addr, port]
|
|
|
|
*
|
|
|
|
* Returns the IP address and port number as 2-element array.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).ip_unpack #=> ["127.0.0.1", 80]
|
|
|
|
* Addrinfo.tcp("::1", 80).ip_unpack #=> ["::1", 80]
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ip_unpack(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
VALUE vflags;
|
|
|
|
VALUE ret, portstr;
|
|
|
|
|
|
|
|
if (!IS_IP_FAMILY(family))
|
|
|
|
rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
|
|
|
|
|
|
|
|
vflags = INT2NUM(NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
ret = addrinfo_getnameinfo(1, &vflags, self);
|
|
|
|
portstr = rb_ary_entry(ret, 1);
|
|
|
|
rb_ary_store(ret, 1, INT2NUM(atoi(StringValueCStr(portstr))));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-18 02:47:35 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.ip_address => string
|
|
|
|
*
|
|
|
|
* Returns the IP address as a string.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).ip_address #=> "127.0.0.1"
|
|
|
|
* Addrinfo.tcp("::1", 80).ip_address #=> "::1"
|
2009-01-18 02:47:35 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ip_address(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
VALUE vflags;
|
|
|
|
VALUE ret;
|
|
|
|
|
|
|
|
if (!IS_IP_FAMILY(family))
|
|
|
|
rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
|
|
|
|
|
|
|
|
vflags = INT2NUM(NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
ret = addrinfo_getnameinfo(1, &vflags, self);
|
|
|
|
return rb_ary_entry(ret, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.ip_port => port
|
|
|
|
*
|
|
|
|
* Returns the port number as an integer.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("127.0.0.1", 80).ip_port #=> 80
|
|
|
|
* Addrinfo.tcp("::1", 80).ip_port #=> 80
|
2009-01-18 02:47:35 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ip_port(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
int port;
|
|
|
|
|
2009-01-18 21:00:52 -05:00
|
|
|
if (!IS_IP_FAMILY(family)) {
|
|
|
|
bad_family:
|
|
|
|
#ifdef AF_INET6
|
2009-01-18 02:47:35 -05:00
|
|
|
rb_raise(rb_eSocket, "need IPv4 or IPv6 address");
|
2009-01-18 21:00:52 -05:00
|
|
|
#else
|
|
|
|
rb_raise(rb_eSocket, "need IPv4 address");
|
|
|
|
#endif
|
|
|
|
}
|
2009-01-18 02:47:35 -05:00
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (rai->sockaddr_len != sizeof(struct sockaddr_in))
|
|
|
|
rb_raise(rb_eSocket, "unexpected sockaddr size for IPv4");
|
2013-02-24 12:51:17 -05:00
|
|
|
port = ntohs(rai->addr.in.sin_port);
|
2009-01-18 02:47:35 -05:00
|
|
|
break;
|
|
|
|
|
2009-01-18 21:00:52 -05:00
|
|
|
#ifdef AF_INET6
|
2009-01-18 02:47:35 -05:00
|
|
|
case AF_INET6:
|
|
|
|
if (rai->sockaddr_len != sizeof(struct sockaddr_in6))
|
|
|
|
rb_raise(rb_eSocket, "unexpected sockaddr size for IPv6");
|
2013-02-24 12:51:17 -05:00
|
|
|
port = ntohs(rai->addr.in6.sin6_port);
|
2009-01-18 02:47:35 -05:00
|
|
|
break;
|
2009-01-18 21:00:52 -05:00
|
|
|
#endif
|
2009-01-18 02:47:35 -05:00
|
|
|
|
|
|
|
default:
|
2009-01-18 21:00:52 -05:00
|
|
|
goto bad_family;
|
2009-01-18 02:47:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return INT2NUM(port);
|
|
|
|
}
|
|
|
|
|
2009-02-01 07:28:50 -05:00
|
|
|
static int
|
|
|
|
extract_in_addr(VALUE self, uint32_t *addrp)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
if (family != AF_INET) return 0;
|
2013-02-24 12:51:17 -05:00
|
|
|
*addrp = ntohl(rai->addr.in.sin_addr.s_addr);
|
2009-02-01 07:28:50 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv4 private address (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv4_private_p(VALUE self)
|
|
|
|
{
|
|
|
|
uint32_t a;
|
|
|
|
if (!extract_in_addr(self, &a)) return Qfalse;
|
|
|
|
if ((a & 0xff000000) == 0x0a000000 || /* 10.0.0.0/8 */
|
|
|
|
(a & 0xfff00000) == 0xac100000 || /* 172.16.0.0/12 */
|
|
|
|
(a & 0xffff0000) == 0xc0a80000) /* 192.168.0.0/16 */
|
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv4 loopback address (127.0.0.0/8).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv4_loopback_p(VALUE self)
|
|
|
|
{
|
|
|
|
uint32_t a;
|
|
|
|
if (!extract_in_addr(self, &a)) return Qfalse;
|
|
|
|
if ((a & 0xff000000) == 0x7f000000) /* 127.0.0.0/8 */
|
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv4 multicast address (224.0.0.0/4).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv4_multicast_p(VALUE self)
|
|
|
|
{
|
|
|
|
uint32_t a;
|
|
|
|
if (!extract_in_addr(self, &a)) return Qfalse;
|
|
|
|
if ((a & 0xf0000000) == 0xe0000000) /* 224.0.0.0/4 */
|
|
|
|
return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2009-02-01 22:48:31 -05:00
|
|
|
#ifdef INET6
|
2009-01-31 06:09:51 -05:00
|
|
|
|
|
|
|
static struct in6_addr *
|
|
|
|
extract_in6_addr(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
if (family != AF_INET6) return NULL;
|
2013-02-24 12:51:17 -05:00
|
|
|
return &rai->addr.in6.sin6_addr;
|
2009-01-31 06:09:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 unspecified address (::).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_unspecified_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_UNSPECIFIED(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 loopback address (::1).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_loopback_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_LOOPBACK(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 multicast address (ff00::/8).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_multicast_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_MULTICAST(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 link local address (ff80::/10).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_linklocal_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_LINKLOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 site local address (ffc0::/10).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_sitelocal_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_SITELOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2012-07-19 20:32:35 -04:00
|
|
|
/*
|
|
|
|
* Returns true for IPv6 unique local address (fc00::/7, RFC4193).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_unique_local_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_UNIQUE_LOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2009-01-31 06:09:51 -05:00
|
|
|
/*
|
|
|
|
* Returns true for IPv4-mapped IPv6 address (::ffff:0:0/80).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_v4mapped_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_V4MAPPED(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv4-compatible IPv6 address (::/80).
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_v4compat_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_V4COMPAT(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 multicast node-local scope address.
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_mc_nodelocal_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_MC_NODELOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 multicast link-local scope address.
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_mc_linklocal_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_MC_LINKLOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 multicast site-local scope address.
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_mc_sitelocal_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_MC_SITELOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 multicast organization-local scope address.
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_mc_orglocal_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_MC_ORGLOCAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true for IPv6 multicast global scope address.
|
|
|
|
* It returns false otherwise.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_mc_global_p(VALUE self)
|
|
|
|
{
|
|
|
|
struct in6_addr *addr = extract_in6_addr(self);
|
|
|
|
if (addr && IN6_IS_ADDR_MC_GLOBAL(addr)) return Qtrue;
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
2009-02-04 10:10:03 -05:00
|
|
|
/*
|
|
|
|
* Returns IPv4 address of IPv4 mapped/compatible IPv6 address.
|
|
|
|
* It returns nil if +self+ is not IPv4 mapped/compatible IPv6 address.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.ip("::192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
|
|
|
|
* Addrinfo.ip("::ffff:192.0.2.3").ipv6_to_ipv4 #=> #<Addrinfo: 192.0.2.3>
|
|
|
|
* Addrinfo.ip("::1").ipv6_to_ipv4 #=> nil
|
|
|
|
* Addrinfo.ip("192.0.2.3").ipv6_to_ipv4 #=> nil
|
|
|
|
* Addrinfo.unix("/tmp/sock").ipv6_to_ipv4 #=> nil
|
2009-02-04 10:10:03 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_ipv6_to_ipv4(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
struct in6_addr *addr;
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
if (family != AF_INET6) return Qnil;
|
2013-02-24 12:51:17 -05:00
|
|
|
addr = &rai->addr.in6.sin6_addr;
|
2009-02-04 10:10:03 -05:00
|
|
|
if (IN6_IS_ADDR_V4MAPPED(addr) || IN6_IS_ADDR_V4COMPAT(addr)) {
|
|
|
|
struct sockaddr_in sin4;
|
2013-02-15 17:08:25 -05:00
|
|
|
INIT_SOCKADDR_IN(&sin4, sizeof(sin4));
|
2009-02-04 10:10:03 -05:00
|
|
|
memcpy(&sin4.sin_addr, (char*)addr + sizeof(*addr) - sizeof(sin4.sin_addr), sizeof(sin4.sin_addr));
|
2010-04-28 04:14:13 -04:00
|
|
|
return rsock_addrinfo_new((struct sockaddr *)&sin4, (socklen_t)sizeof(sin4),
|
2009-03-01 01:30:41 -05:00
|
|
|
PF_INET, rai->socktype, rai->protocol,
|
|
|
|
rai->canonname, rai->inspectname);
|
2009-02-04 10:10:03 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-31 06:09:51 -05:00
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* addrinfo.unix_path => path
|
|
|
|
*
|
|
|
|
* Returns the socket path as a string.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.unix("/tmp/sock").unix_path #=> "/tmp/sock"
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_unix_path(VALUE self)
|
|
|
|
{
|
|
|
|
rb_addrinfo_t *rai = get_addrinfo(self);
|
|
|
|
int family = ai_get_afamily(rai);
|
|
|
|
struct sockaddr_un *addr;
|
|
|
|
char *s, *e;
|
|
|
|
|
|
|
|
if (family != AF_UNIX)
|
|
|
|
rb_raise(rb_eSocket, "need AF_UNIX address");
|
|
|
|
|
2013-02-24 12:51:17 -05:00
|
|
|
addr = &rai->addr.un;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
s = addr->sun_path;
|
|
|
|
e = (char*)addr + rai->sockaddr_len;
|
|
|
|
if (e < s)
|
2012-04-25 10:05:10 -04:00
|
|
|
rb_raise(rb_eSocket, "too short AF_UNIX address: %"PRIuSIZE" bytes given for minimum %"PRIuSIZE" bytes.",
|
|
|
|
(size_t)rai->sockaddr_len, (size_t)(s - (char *)addr));
|
2009-01-16 23:11:27 -05:00
|
|
|
if (addr->sun_path + sizeof(addr->sun_path) < e)
|
2012-04-25 10:05:10 -04:00
|
|
|
rb_raise(rb_eSocket,
|
|
|
|
"too long AF_UNIX path (%"PRIuSIZE" bytes given but %"PRIuSIZE" bytes max)",
|
|
|
|
(size_t)(e - addr->sun_path), sizeof(addr->sun_path));
|
2009-01-16 23:11:27 -05:00
|
|
|
while (s < e && *(e-1) == '\0')
|
|
|
|
e--;
|
|
|
|
return rb_str_new(s, e-s);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol, flags) => [addrinfo, ...]
|
|
|
|
* Addrinfo.getaddrinfo(nodename, service, family, socktype, protocol) => [addrinfo, ...]
|
|
|
|
* Addrinfo.getaddrinfo(nodename, service, family, socktype) => [addrinfo, ...]
|
|
|
|
* Addrinfo.getaddrinfo(nodename, service, family) => [addrinfo, ...]
|
|
|
|
* Addrinfo.getaddrinfo(nodename, service) => [addrinfo, ...]
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* returns a list of addrinfo objects as an array.
|
|
|
|
*
|
|
|
|
* This method converts nodename (hostname) and service (port) to addrinfo.
|
|
|
|
* Since the conversion is not unique, the result is a list of addrinfo objects.
|
|
|
|
*
|
|
|
|
* nodename or service can be nil if no conversion intended.
|
|
|
|
*
|
2009-11-04 07:02:37 -05:00
|
|
|
* family, socktype and protocol are hint for preferred protocol.
|
2010-04-22 04:04:13 -04:00
|
|
|
* If the result will be used for a socket with SOCK_STREAM,
|
2009-01-16 23:11:27 -05:00
|
|
|
* SOCK_STREAM should be specified as socktype.
|
2009-02-05 06:01:43 -05:00
|
|
|
* If so, Addrinfo.getaddrinfo returns addrinfo list appropriate for SOCK_STREAM.
|
2009-01-16 23:11:27 -05:00
|
|
|
* If they are omitted or nil is given, the result is not restricted.
|
2010-04-22 04:04:13 -04:00
|
|
|
*
|
2009-11-04 07:02:37 -05:00
|
|
|
* Similarly, PF_INET6 as family restricts for IPv6.
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
2012-04-20 06:51:25 -04:00
|
|
|
* flags should be bitwise OR of Socket::AI_??? constants such as follows.
|
|
|
|
* Note that the exact list of the constants depends on OS.
|
|
|
|
*
|
|
|
|
* AI_PASSIVE Get address to use with bind()
|
|
|
|
* AI_CANONNAME Fill in the canonical name
|
|
|
|
* AI_NUMERICHOST Prevent host name resolution
|
|
|
|
* AI_NUMERICSERV Prevent service name resolution
|
|
|
|
* AI_V4MAPPED Accept IPv4-mapped IPv6 addresses
|
|
|
|
* AI_ALL Allow all addresses
|
|
|
|
* AI_ADDRCONFIG Accept only if any address is assigned
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
2009-01-17 10:40:30 -05:00
|
|
|
* Note that socktype should be specified whenever application knows the usage of the address.
|
2009-11-04 07:02:37 -05:00
|
|
|
* Some platform causes an error when socktype is omitted and servname is specified as an integer
|
2009-01-17 10:40:30 -05:00
|
|
|
* because some port numbers, 512 for example, are ambiguous without socktype.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.getaddrinfo("www.kame.net", 80, nil, :STREAM)
|
2012-02-02 06:51:25 -05:00
|
|
|
* #=> [#<Addrinfo: 203.178.141.194:80 TCP (www.kame.net)>,
|
|
|
|
* # #<Addrinfo: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80 TCP (www.kame.net)>]
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_s_getaddrinfo(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE node, service, family, socktype, protocol, flags;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "24", &node, &service, &family, &socktype, &protocol, &flags);
|
|
|
|
return addrinfo_list_new(node, service, family, socktype, protocol, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.ip(host) => addrinfo
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* returns an addrinfo object for IP address.
|
|
|
|
*
|
2009-01-17 00:52:16 -05:00
|
|
|
* The port, socktype, protocol of the result is filled by zero.
|
|
|
|
* So, it is not appropriate to create a socket.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.ip("localhost") #=> #<Addrinfo: 127.0.0.1 (localhost)>
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_s_ip(VALUE self, VALUE host)
|
|
|
|
{
|
|
|
|
VALUE ret;
|
|
|
|
rb_addrinfo_t *rai;
|
|
|
|
ret = addrinfo_firstonly_new(host, Qnil,
|
|
|
|
INT2NUM(PF_UNSPEC), INT2FIX(0), INT2FIX(0), INT2FIX(0));
|
|
|
|
rai = get_addrinfo(ret);
|
|
|
|
rai->socktype = 0;
|
|
|
|
rai->protocol = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp(host, port) => addrinfo
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* returns an addrinfo object for TCP address.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.tcp("localhost", "smtp") #=> #<Addrinfo: 127.0.0.1:25 TCP (localhost:smtp)>
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_s_tcp(VALUE self, VALUE host, VALUE port)
|
|
|
|
{
|
|
|
|
return addrinfo_firstonly_new(host, port,
|
|
|
|
INT2NUM(PF_UNSPEC), INT2NUM(SOCK_STREAM), INT2NUM(IPPROTO_TCP), INT2FIX(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.udp(host, port) => addrinfo
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* returns an addrinfo object for UDP address.
|
|
|
|
*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo.udp("localhost", "daytime") #=> #<Addrinfo: 127.0.0.1:13 UDP (localhost:daytime)>
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
addrinfo_s_udp(VALUE self, VALUE host, VALUE port)
|
|
|
|
{
|
|
|
|
return addrinfo_firstonly_new(host, port,
|
|
|
|
INT2NUM(PF_UNSPEC), INT2NUM(SOCK_DGRAM), INT2NUM(IPPROTO_UDP), INT2FIX(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2009-02-07 23:53:17 -05:00
|
|
|
* Addrinfo.unix(path [, socktype]) => addrinfo
|
2009-01-16 23:11:27 -05:00
|
|
|
*
|
|
|
|
* returns an addrinfo object for UNIX socket address.
|
|
|
|
*
|
2009-02-07 23:53:17 -05:00
|
|
|
* _socktype_ specifies the socket type.
|
|
|
|
* If it is omitted, :STREAM is used.
|
|
|
|
*
|
|
|
|
* Addrinfo.unix("/tmp/sock") #=> #<Addrinfo: /tmp/sock SOCK_STREAM>
|
|
|
|
* Addrinfo.unix("/tmp/sock", :DGRAM) #=> #<Addrinfo: /tmp/sock SOCK_DGRAM>
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
2009-02-07 23:53:17 -05:00
|
|
|
addrinfo_s_unix(int argc, VALUE *argv, VALUE self)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-02-07 23:53:17 -05:00
|
|
|
VALUE path, vsocktype, addr;
|
|
|
|
int socktype;
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_addrinfo_t *rai;
|
|
|
|
|
2009-02-07 23:53:17 -05:00
|
|
|
rb_scan_args(argc, argv, "11", &path, &vsocktype);
|
|
|
|
|
|
|
|
if (NIL_P(vsocktype))
|
|
|
|
socktype = SOCK_STREAM;
|
|
|
|
else
|
2009-03-01 01:30:41 -05:00
|
|
|
socktype = rsock_socktype_arg(vsocktype);
|
2009-02-07 23:53:17 -05:00
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
addr = addrinfo_s_allocate(rb_cAddrinfo);
|
2009-01-16 23:11:27 -05:00
|
|
|
DATA_PTR(addr) = rai = alloc_addrinfo();
|
2009-02-07 23:53:17 -05:00
|
|
|
init_unix_addrinfo(rai, path, socktype);
|
2009-01-16 23:11:27 -05:00
|
|
|
OBJ_INFECT(addr, path);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_sockaddr_string_value(volatile VALUE *v)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
VALUE val = *v;
|
2009-12-30 16:03:51 -05:00
|
|
|
if (IS_ADDRINFO(val)) {
|
2009-01-16 23:11:27 -05:00
|
|
|
*v = addrinfo_to_sockaddr(val);
|
|
|
|
}
|
|
|
|
StringValue(*v);
|
|
|
|
return *v;
|
|
|
|
}
|
|
|
|
|
2013-04-06 01:50:42 -04:00
|
|
|
VALUE
|
|
|
|
rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *rai_ret)
|
|
|
|
{
|
|
|
|
VALUE val = *v;
|
|
|
|
*rai_ret = Qnil;
|
|
|
|
if (IS_ADDRINFO(val)) {
|
|
|
|
*v = addrinfo_to_sockaddr(val);
|
|
|
|
*rai_ret = val;
|
|
|
|
}
|
|
|
|
StringValue(*v);
|
|
|
|
return *v;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
char *
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_sockaddr_string_value_ptr(volatile VALUE *v)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_sockaddr_string_value(v);
|
2009-01-16 23:11:27 -05:00
|
|
|
return RSTRING_PTR(*v);
|
|
|
|
}
|
|
|
|
|
2009-01-17 11:39:03 -05:00
|
|
|
VALUE
|
|
|
|
rb_check_sockaddr_string_type(VALUE val)
|
|
|
|
{
|
2009-12-30 16:03:51 -05:00
|
|
|
if (IS_ADDRINFO(val))
|
2009-01-17 11:39:03 -05:00
|
|
|
return addrinfo_to_sockaddr(val);
|
|
|
|
return rb_check_string_type(val);
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int family;
|
|
|
|
int socktype;
|
|
|
|
int ret;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t optlen = (socklen_t)sizeof(socktype);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
/* assumes protocol family and address family are identical */
|
|
|
|
family = get_afamily(addr, len);
|
|
|
|
|
|
|
|
ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&socktype, &optlen);
|
|
|
|
if (ret == -1) {
|
|
|
|
rb_sys_fail("getsockopt(SO_TYPE)");
|
|
|
|
}
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_addrinfo_new(addr, len, family, socktype, 0, Qnil, Qnil);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
|
|
|
|
switch (TYPE(io)) {
|
|
|
|
case T_FIXNUM:
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_fd_socket_addrinfo(FIX2INT(io), addr, len);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
case T_BIGNUM:
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_fd_socket_addrinfo(NUM2INT(io), addr, len);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
case T_FILE:
|
|
|
|
GetOpenFile(io, fptr);
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_fd_socket_addrinfo(fptr->fd, addr, len);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eTypeError, "neither IO nor file descriptor");
|
|
|
|
}
|
2012-04-13 19:45:37 -04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-02-05 06:01:43 -05:00
|
|
|
* Addrinfo class
|
2009-01-16 23:11:27 -05:00
|
|
|
*/
|
|
|
|
void
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_addrinfo(void)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2011-08-15 19:08:39 -04:00
|
|
|
/*
|
|
|
|
* The Addrinfo class maps <tt>struct addrinfo</tt> to ruby. This
|
|
|
|
* structure identifies an Internet host and a service.
|
|
|
|
*/
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_cAddrinfo = rb_define_class("Addrinfo", rb_cData);
|
|
|
|
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);
|
2013-04-06 01:50:42 -04:00
|
|
|
rb_define_method(rb_cAddrinfo, "inspect_sockaddr", rsock_addrinfo_inspect_sockaddr, 0);
|
2009-02-05 06:01:43 -05:00
|
|
|
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);
|
|
|
|
rb_define_singleton_method(rb_cAddrinfo, "udp", addrinfo_s_udp, 2);
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2009-02-07 23:53:17 -05:00
|
|
|
rb_define_singleton_method(rb_cAddrinfo, "unix", addrinfo_s_unix, -1);
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "afamily", addrinfo_afamily, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "pfamily", addrinfo_pfamily, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "socktype", addrinfo_socktype, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "protocol", addrinfo_protocol, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "canonname", addrinfo_canonname, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "ipv4?", addrinfo_ipv4_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6?", addrinfo_ipv6_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "unix?", addrinfo_unix_p, 0);
|
2009-01-31 06:09:51 -05:00
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "ip?", addrinfo_ip_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ip_unpack", addrinfo_ip_unpack, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ip_address", addrinfo_ip_address, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ip_port", addrinfo_ip_port, 0);
|
2009-01-31 06:09:51 -05:00
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "ipv4_private?", addrinfo_ipv4_private_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv4_loopback?", addrinfo_ipv4_loopback_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv4_multicast?", addrinfo_ipv4_multicast_p, 0);
|
2009-02-01 07:28:50 -05:00
|
|
|
|
2009-02-01 22:48:31 -05:00
|
|
|
#ifdef INET6
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_unspecified?", addrinfo_ipv6_unspecified_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_loopback?", addrinfo_ipv6_loopback_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_multicast?", addrinfo_ipv6_multicast_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_linklocal?", addrinfo_ipv6_linklocal_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_sitelocal?", addrinfo_ipv6_sitelocal_p, 0);
|
2012-07-19 20:32:35 -04:00
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_unique_local?", addrinfo_ipv6_unique_local_p, 0);
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_v4mapped?", addrinfo_ipv6_v4mapped_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_v4compat?", addrinfo_ipv6_v4compat_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_mc_nodelocal?", addrinfo_ipv6_mc_nodelocal_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_mc_linklocal?", addrinfo_ipv6_mc_linklocal_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_mc_sitelocal?", addrinfo_ipv6_mc_sitelocal_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_mc_orglocal?", addrinfo_ipv6_mc_orglocal_p, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_mc_global?", addrinfo_ipv6_mc_global_p, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cAddrinfo, "ipv6_to_ipv4", addrinfo_ipv6_to_ipv4, 0);
|
2009-01-31 06:09:51 -05:00
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "unix_path", addrinfo_unix_path, 0);
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "to_sockaddr", addrinfo_to_sockaddr, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "to_s", addrinfo_to_sockaddr, 0); /* compatibility for ruby before 1.9.2 */
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "getnameinfo", addrinfo_getnameinfo, -1);
|
2009-01-17 13:14:34 -05:00
|
|
|
|
2009-02-05 06:01:43 -05:00
|
|
|
rb_define_method(rb_cAddrinfo, "marshal_dump", addrinfo_mdump, 0);
|
|
|
|
rb_define_method(rb_cAddrinfo, "marshal_load", addrinfo_mload, 1);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|