rsock_addrinfo: specify address family
* ext/socket/rsock_addrinfo (rsock_addrinfo): specify address family. [Fix GH-1052] * ext/socket/udpsocket.c (udp_connect, udp_bind, udp_send): address family by the receiver. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
18e4a39c9a
commit
61053459cf
@ -1,3 +1,11 @@
|
|||||||
|
Tue Oct 13 12:14:10 2015 Craig Davison <craig65535@gmail.com>
|
||||||
|
|
||||||
|
* ext/socket/rsock_addrinfo (rsock_addrinfo): specify address
|
||||||
|
family. [Fix GH-1052]
|
||||||
|
|
||||||
|
* ext/socket/udpsocket.c (udp_connect, udp_bind, udp_send):
|
||||||
|
address family by the receiver.
|
||||||
|
|
||||||
Sun Oct 11 07:09:19 2015 Koichi Sasada <ko1@atdot.net>
|
Sun Oct 11 07:09:19 2015 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* vm_insnhelper.c (vm_push_frame): initialize other than sp (and ep)
|
* vm_insnhelper.c (vm_push_frame): initialize other than sp (and ep)
|
||||||
|
@ -45,16 +45,19 @@ init_inetsock_internal(struct inetsock_arg *arg)
|
|||||||
int type = arg->type;
|
int type = arg->type;
|
||||||
struct addrinfo *res, *lres;
|
struct addrinfo *res, *lres;
|
||||||
int fd, status = 0, local = 0;
|
int fd, status = 0, local = 0;
|
||||||
|
int family = AF_UNSPEC;
|
||||||
const char *syscall = 0;
|
const char *syscall = 0;
|
||||||
|
|
||||||
arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv, SOCK_STREAM,
|
arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv,
|
||||||
(type == INET_SERVER) ? AI_PASSIVE : 0);
|
family, SOCK_STREAM,
|
||||||
|
(type == INET_SERVER) ? AI_PASSIVE : 0);
|
||||||
/*
|
/*
|
||||||
* Maybe also accept a local address
|
* Maybe also accept a local address
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (type != INET_SERVER && (!NIL_P(arg->local.host) || !NIL_P(arg->local.serv))) {
|
if (type != INET_SERVER && (!NIL_P(arg->local.host) || !NIL_P(arg->local.serv))) {
|
||||||
arg->local.res = rsock_addrinfo(arg->local.host, arg->local.serv, SOCK_STREAM, 0);
|
arg->local.res = rsock_addrinfo(arg->local.host, arg->local.serv,
|
||||||
|
family, SOCK_STREAM, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
arg->fd = fd = -1;
|
arg->fd = fd = -1;
|
||||||
@ -308,7 +311,7 @@ static VALUE
|
|||||||
ip_s_getaddress(VALUE obj, VALUE host)
|
ip_s_getaddress(VALUE obj, VALUE host)
|
||||||
{
|
{
|
||||||
union_sockaddr addr;
|
union_sockaddr addr;
|
||||||
struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, SOCK_STREAM, 0);
|
struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, 0);
|
||||||
socklen_t len = res->ai->ai_addrlen;
|
socklen_t len = res->ai->ai_addrlen;
|
||||||
|
|
||||||
/* just take the first one */
|
/* just take the first one */
|
||||||
|
@ -518,13 +518,25 @@ rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_h
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
rsock_fd_family(int fd)
|
||||||
|
{
|
||||||
|
struct sockaddr sa = { 0 };
|
||||||
|
socklen_t sa_len = sizeof(sa);
|
||||||
|
|
||||||
|
if (fd < 0 || getsockname(fd, &sa, &sa_len) != 0) {
|
||||||
|
return AF_UNSPEC;
|
||||||
|
}
|
||||||
|
return sa.sa_family;
|
||||||
|
}
|
||||||
|
|
||||||
struct rb_addrinfo*
|
struct rb_addrinfo*
|
||||||
rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags)
|
rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags)
|
||||||
{
|
{
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
|
|
||||||
MEMZERO(&hints, struct addrinfo, 1);
|
MEMZERO(&hints, struct addrinfo, 1);
|
||||||
hints.ai_family = AF_UNSPEC;
|
hints.ai_family = family;
|
||||||
hints.ai_socktype = socktype;
|
hints.ai_socktype = socktype;
|
||||||
hints.ai_flags = flags;
|
hints.ai_flags = flags;
|
||||||
return rsock_getaddrinfo(host, port, &hints, 1);
|
return rsock_getaddrinfo(host, port, &hints, 1);
|
||||||
|
@ -295,7 +295,8 @@ int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo
|
|||||||
void rb_freeaddrinfo(struct rb_addrinfo *ai);
|
void rb_freeaddrinfo(struct rb_addrinfo *ai);
|
||||||
VALUE rsock_freeaddrinfo(VALUE arg);
|
VALUE rsock_freeaddrinfo(VALUE arg);
|
||||||
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
|
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
|
||||||
struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags);
|
int rsock_fd_family(int fd);
|
||||||
|
struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags);
|
||||||
struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
|
struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
|
||||||
VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
|
VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
|
||||||
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
|
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
|
||||||
|
@ -1140,7 +1140,9 @@ sock_sockaddr(struct sockaddr *addr, socklen_t len)
|
|||||||
static VALUE
|
static VALUE
|
||||||
sock_s_gethostbyname(VALUE obj, VALUE host)
|
sock_s_gethostbyname(VALUE obj, VALUE host)
|
||||||
{
|
{
|
||||||
return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), sock_sockaddr);
|
struct rb_addrinfo *res =
|
||||||
|
rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
|
||||||
|
return rsock_make_hostent(host, res, sock_sockaddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1518,7 +1520,7 @@ sock_s_getnameinfo(int argc, VALUE *argv)
|
|||||||
static VALUE
|
static VALUE
|
||||||
sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
|
sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
|
||||||
{
|
{
|
||||||
struct rb_addrinfo *res = rsock_addrinfo(host, port, 0, 0);
|
struct rb_addrinfo *res = rsock_addrinfo(host, port, AF_UNSPEC, 0, 0);
|
||||||
VALUE addr = rb_str_new((char*)res->ai->ai_addr, res->ai->ai_addrlen);
|
VALUE addr = rb_str_new((char*)res->ai->ai_addr, res->ai->ai_addrlen);
|
||||||
|
|
||||||
rb_freeaddrinfo(res);
|
rb_freeaddrinfo(res);
|
||||||
|
@ -50,8 +50,9 @@ tcp_sockaddr(struct sockaddr *addr, socklen_t len)
|
|||||||
static VALUE
|
static VALUE
|
||||||
tcp_s_gethostbyname(VALUE obj, VALUE host)
|
tcp_s_gethostbyname(VALUE obj, VALUE host)
|
||||||
{
|
{
|
||||||
return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME),
|
struct rb_addrinfo *res =
|
||||||
tcp_sockaddr);
|
rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
|
||||||
|
return rsock_make_hostent(host, res, tcp_sockaddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -87,7 +87,7 @@ udp_connect(VALUE sock, VALUE host, VALUE port)
|
|||||||
VALUE ret;
|
VALUE ret;
|
||||||
|
|
||||||
GetOpenFile(sock, arg.fptr);
|
GetOpenFile(sock, arg.fptr);
|
||||||
arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
|
arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0);
|
||||||
ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
|
ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
|
||||||
rsock_freeaddrinfo, (VALUE)arg.res);
|
rsock_freeaddrinfo, (VALUE)arg.res);
|
||||||
if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
|
if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
|
||||||
@ -131,7 +131,7 @@ udp_bind(VALUE sock, VALUE host, VALUE port)
|
|||||||
VALUE ret;
|
VALUE ret;
|
||||||
|
|
||||||
GetOpenFile(sock, arg.fptr);
|
GetOpenFile(sock, arg.fptr);
|
||||||
arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
|
arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0);
|
||||||
ret = rb_ensure(udp_bind_internal, (VALUE)&arg,
|
ret = rb_ensure(udp_bind_internal, (VALUE)&arg,
|
||||||
rsock_freeaddrinfo, (VALUE)arg.res);
|
rsock_freeaddrinfo, (VALUE)arg.res);
|
||||||
if (!ret) rsock_sys_fail_host_port("bind(2)", host, port);
|
if (!ret) rsock_sys_fail_host_port("bind(2)", host, port);
|
||||||
@ -207,7 +207,7 @@ udp_send(int argc, VALUE *argv, VALUE sock)
|
|||||||
GetOpenFile(sock, arg.fptr);
|
GetOpenFile(sock, arg.fptr);
|
||||||
arg.sarg.fd = arg.fptr->fd;
|
arg.sarg.fd = arg.fptr->fd;
|
||||||
arg.sarg.flags = NUM2INT(flags);
|
arg.sarg.flags = NUM2INT(flags);
|
||||||
arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
|
arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0);
|
||||||
ret = rb_ensure(udp_send_internal, (VALUE)&arg,
|
ret = rb_ensure(udp_send_internal, (VALUE)&arg,
|
||||||
rsock_freeaddrinfo, (VALUE)arg.res);
|
rsock_freeaddrinfo, (VALUE)arg.res);
|
||||||
if (!ret) rsock_sys_fail_host_port("sendto(2)", host, port);
|
if (!ret) rsock_sys_fail_host_port("sendto(2)", host, port);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user