2009-01-16 23:11:27 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
init.c -
|
|
|
|
|
|
|
|
created at: Thu Mar 31 12:21:29 JST 1994
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "rubysocket.h"
|
|
|
|
|
|
|
|
VALUE rb_cBasicSocket;
|
|
|
|
VALUE rb_cIPSocket;
|
|
|
|
VALUE rb_cTCPSocket;
|
|
|
|
VALUE rb_cTCPServer;
|
|
|
|
VALUE rb_cUDPSocket;
|
|
|
|
#ifdef AF_UNIX
|
|
|
|
VALUE rb_cUNIXSocket;
|
|
|
|
VALUE rb_cUNIXServer;
|
|
|
|
#endif
|
|
|
|
VALUE rb_cSocket;
|
2009-02-05 06:01:43 -05:00
|
|
|
VALUE rb_cAddrinfo;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
VALUE rb_eSocket;
|
|
|
|
|
|
|
|
#ifdef SOCKS
|
|
|
|
VALUE rb_cSOCKSSocket;
|
|
|
|
#endif
|
|
|
|
|
2010-02-02 04:15:19 -05:00
|
|
|
int rsock_do_not_reverse_lookup = 1;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
void
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_raise_socket_error(const char *reason, int error)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
#ifdef EAI_SYSTEM
|
|
|
|
if (error == EAI_SYSTEM) rb_sys_fail(reason);
|
|
|
|
#endif
|
|
|
|
rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_init_sock(VALUE sock, int fd)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
rb_io_t *fp;
|
2010-10-12 10:54:02 -04:00
|
|
|
#ifndef _WIN32
|
2009-10-14 11:26:40 -04:00
|
|
|
struct stat sbuf;
|
|
|
|
|
|
|
|
if (fstat(fd, &sbuf) < 0)
|
2013-04-05 22:39:44 -04:00
|
|
|
rb_sys_fail("fstat(2)");
|
2011-07-20 09:26:10 -04:00
|
|
|
rb_update_max_fd(fd);
|
2009-10-14 11:26:40 -04:00
|
|
|
if (!S_ISSOCK(sbuf.st_mode))
|
|
|
|
rb_raise(rb_eArgError, "not a socket file descriptor");
|
2009-10-14 20:29:10 -04:00
|
|
|
#else
|
2012-07-19 01:20:08 -04:00
|
|
|
rb_update_max_fd(fd);
|
2009-10-14 20:29:10 -04:00
|
|
|
if (!rb_w32_is_socket(fd))
|
|
|
|
rb_raise(rb_eArgError, "not a socket file descriptor");
|
|
|
|
#endif
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
MakeOpenFile(sock, fp);
|
|
|
|
fp->fd = fd;
|
|
|
|
fp->mode = FMODE_READWRITE|FMODE_DUPLEX;
|
|
|
|
rb_io_ascii8bit_binmode(sock);
|
2009-03-01 01:30:41 -05:00
|
|
|
if (rsock_do_not_reverse_lookup) {
|
2009-01-16 23:11:27 -05:00
|
|
|
fp->mode |= FMODE_NOREVLOOKUP;
|
|
|
|
}
|
|
|
|
rb_io_synchronized(fp);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_sendto_blocking(void *data)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-03-01 01:30:41 -05:00
|
|
|
struct rsock_send_arg *arg = data;
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE mesg = arg->mesg;
|
|
|
|
return (VALUE)sendto(arg->fd, RSTRING_PTR(mesg), RSTRING_LEN(mesg),
|
|
|
|
arg->flags, arg->to, arg->tolen);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_send_blocking(void *data)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-03-01 01:30:41 -05:00
|
|
|
struct rsock_send_arg *arg = data;
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE mesg = arg->mesg;
|
|
|
|
return (VALUE)send(arg->fd, RSTRING_PTR(mesg), RSTRING_LEN(mesg),
|
|
|
|
arg->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct recvfrom_arg {
|
|
|
|
int fd, flags;
|
|
|
|
VALUE str;
|
|
|
|
socklen_t alen;
|
2013-02-24 12:51:17 -05:00
|
|
|
union_sockaddr buf;
|
2009-01-16 23:11:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
recvfrom_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct recvfrom_arg *arg = data;
|
2013-02-16 06:44:42 -05:00
|
|
|
socklen_t len0 = arg->alen;
|
|
|
|
ssize_t ret;
|
|
|
|
ret = recvfrom(arg->fd, RSTRING_PTR(arg->str), RSTRING_LEN(arg->str),
|
2013-02-24 12:51:17 -05:00
|
|
|
arg->flags, &arg->buf.addr, &arg->alen);
|
2013-02-16 06:44:42 -05:00
|
|
|
if (ret != -1 && len0 < arg->alen)
|
|
|
|
arg->alen = len0;
|
|
|
|
return (VALUE)ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
VALUE str, klass;
|
|
|
|
struct recvfrom_arg arg;
|
|
|
|
VALUE len, flg;
|
|
|
|
long buflen;
|
|
|
|
long slen;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &len, &flg);
|
|
|
|
|
|
|
|
if (flg == Qnil) arg.flags = 0;
|
|
|
|
else arg.flags = NUM2INT(flg);
|
|
|
|
buflen = NUM2INT(len);
|
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
if (rb_io_read_pending(fptr)) {
|
|
|
|
rb_raise(rb_eIOError, "recv for buffered IO");
|
|
|
|
}
|
|
|
|
arg.fd = fptr->fd;
|
2010-04-28 04:14:13 -04:00
|
|
|
arg.alen = (socklen_t)sizeof(arg.buf);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
arg.str = str = rb_tainted_str_new(0, buflen);
|
|
|
|
klass = RBASIC(str)->klass;
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
rb_obj_hide(str);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
while (rb_io_check_closed(fptr),
|
2014-01-18 09:13:22 -05:00
|
|
|
rsock_maybe_wait_fd(arg.fd),
|
2011-02-12 00:44:23 -05:00
|
|
|
(slen = BLOCKING_REGION_FD(recvfrom_blocking, &arg)) < 0) {
|
2009-02-22 05:28:38 -05:00
|
|
|
if (!rb_io_wait_readable(fptr->fd)) {
|
|
|
|
rb_sys_fail("recvfrom(2)");
|
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
if (RBASIC(str)->klass || RSTRING_LEN(str) != buflen) {
|
|
|
|
rb_raise(rb_eRuntimeError, "buffer string modified");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* include/ruby/ruby.h: constify RBasic::klass and add
RBASIC_CLASS(obj) macro which returns a class of `obj'.
This change is a part of RGENGC branch [ruby-trunk - Feature #8339].
* object.c: add new function rb_obj_reveal().
This function reveal interal (hidden) object by rb_obj_hide().
Note that do not change class before and after hiding.
Only permitted example is:
klass = RBASIC_CLASS(obj);
rb_obj_hide(obj);
....
rb_obj_reveal(obj, klass);
TODO: API design. rb_obj_reveal() should be replaced with others.
TODO: modify constified variables using cast may be harmful for
compiler's analysis and optimizaton.
Any idea to prohibt inserting RBasic::klass directly?
If rename RBasic::klass and force to use RBASIC_CLASS(obj),
then all codes such as `RBASIC(obj)->klass' will be
compilation error. Is it acceptable? (We have similar
experience at Ruby 1.9,
for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)".
* internal.h: add some macros.
* RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal
object.
* RBASIC_SET_CLASS(obj, cls) set RBasic::klass.
* RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS
without write barrier (planned).
* RCLASS_SET_SUPER(a, b) set super class of a.
* array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c,
file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c,
parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c,
string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c:
Use above macros and functions to access RBasic::klass.
* ext/coverage/coverage.c, ext/readline/readline.c,
ext/socket/ancdata.c, ext/socket/init.c,
* ext/zlib/zlib.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 06:49:11 -04:00
|
|
|
rb_obj_reveal(str, klass);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (slen < RSTRING_LEN(str)) {
|
|
|
|
rb_str_set_len(str, slen);
|
|
|
|
}
|
|
|
|
rb_obj_taint(str);
|
|
|
|
switch (from) {
|
|
|
|
case RECV_RECV:
|
|
|
|
return str;
|
|
|
|
case RECV_IP:
|
|
|
|
#if 0
|
|
|
|
if (arg.alen != sizeof(struct sockaddr_in)) {
|
|
|
|
rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (arg.alen && arg.alen != sizeof(arg.buf)) /* OSX doesn't return a from result for connection-oriented sockets */
|
2013-02-24 12:51:17 -05:00
|
|
|
return rb_assoc_new(str, rsock_ipaddr(&arg.buf.addr, arg.alen, fptr->mode & FMODE_NOREVLOOKUP));
|
2009-01-16 23:11:27 -05:00
|
|
|
else
|
|
|
|
return rb_assoc_new(str, Qnil);
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
case RECV_UNIX:
|
2013-02-24 12:51:17 -05:00
|
|
|
return rb_assoc_new(str, rsock_unixaddr(&arg.buf.un, arg.alen));
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
case RECV_SOCKET:
|
2013-02-24 12:51:17 -05:00
|
|
|
return rb_assoc_new(str, rsock_io_socket_addrinfo(sock, &arg.buf.addr, arg.alen));
|
2009-01-16 23:11:27 -05:00
|
|
|
default:
|
2009-03-01 01:30:41 -05:00
|
|
|
rb_bug("rsock_s_recvfrom called with bad value");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
VALUE str;
|
2013-02-24 12:51:17 -05:00
|
|
|
union_sockaddr buf;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t alen = (socklen_t)sizeof buf;
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE len, flg;
|
|
|
|
long buflen;
|
|
|
|
long slen;
|
|
|
|
int fd, flags;
|
|
|
|
VALUE addr = Qnil;
|
2013-02-16 06:44:42 -05:00
|
|
|
socklen_t len0;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &len, &flg);
|
|
|
|
|
|
|
|
if (flg == Qnil) flags = 0;
|
|
|
|
else flags = NUM2INT(flg);
|
|
|
|
buflen = NUM2INT(len);
|
|
|
|
|
|
|
|
#ifdef MSG_DONTWAIT
|
|
|
|
/* MSG_DONTWAIT avoids the race condition between fcntl and recvfrom.
|
|
|
|
It is not portable, though. */
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GetOpenFile(sock, fptr);
|
|
|
|
if (rb_io_read_pending(fptr)) {
|
|
|
|
rb_raise(rb_eIOError, "recvfrom for buffered IO");
|
|
|
|
}
|
|
|
|
fd = fptr->fd;
|
|
|
|
|
|
|
|
str = rb_tainted_str_new(0, buflen);
|
|
|
|
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
rb_io_set_nonblock(fptr);
|
2013-02-16 06:44:42 -05:00
|
|
|
len0 = alen;
|
2013-02-24 12:51:17 -05:00
|
|
|
slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, &buf.addr, &alen);
|
2013-02-16 06:44:42 -05:00
|
|
|
if (slen != -1 && len0 < alen)
|
|
|
|
alen = len0;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
if (slen < 0) {
|
2009-02-21 23:38:46 -05:00
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
2013-04-08 15:58:55 -04:00
|
|
|
rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "recvfrom(2) would block");
|
2009-02-21 23:38:46 -05:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_sys_fail("recvfrom(2)");
|
|
|
|
}
|
|
|
|
if (slen < RSTRING_LEN(str)) {
|
|
|
|
rb_str_set_len(str, slen);
|
|
|
|
}
|
|
|
|
rb_obj_taint(str);
|
|
|
|
switch (from) {
|
|
|
|
case RECV_RECV:
|
|
|
|
return str;
|
|
|
|
|
|
|
|
case RECV_IP:
|
|
|
|
if (alen && alen != sizeof(buf)) /* connection-oriented socket may not return a from result */
|
2013-02-24 12:51:17 -05:00
|
|
|
addr = rsock_ipaddr(&buf.addr, alen, fptr->mode & FMODE_NOREVLOOKUP);
|
2009-01-16 23:11:27 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RECV_SOCKET:
|
2013-02-24 12:51:17 -05:00
|
|
|
addr = rsock_io_socket_addrinfo(sock, &buf.addr, alen);
|
2009-01-16 23:11:27 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-03-01 01:30:41 -05:00
|
|
|
rb_bug("rsock_s_recvfrom_nonblock called with bad value");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
return rb_assoc_new(str, addr);
|
|
|
|
}
|
|
|
|
|
2014-01-28 09:37:34 -05:00
|
|
|
/* returns true if SOCK_CLOEXEC is supported */
|
|
|
|
int rsock_detect_cloexec(int fd)
|
|
|
|
{
|
|
|
|
#ifdef SOCK_CLOEXEC
|
|
|
|
int flags = fcntl(fd, F_GETFD);
|
|
|
|
|
|
|
|
if (flags == -1)
|
|
|
|
rb_bug("rsock_detect_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, strerror(errno));
|
|
|
|
|
|
|
|
if (flags & FD_CLOEXEC)
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-05 02:46:02 -04:00
|
|
|
static int
|
2011-11-05 04:56:50 -04:00
|
|
|
rsock_socket0(int domain, int type, int proto)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2011-11-05 04:56:50 -04:00
|
|
|
int ret;
|
2011-10-31 10:38:50 -04:00
|
|
|
|
|
|
|
#ifdef SOCK_CLOEXEC
|
2014-01-28 09:37:34 -05:00
|
|
|
static int cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
|
|
|
|
|
|
|
|
if (cloexec_state > 0) { /* common path, if SOCK_CLOEXEC is defined */
|
|
|
|
ret = socket(domain, type|SOCK_CLOEXEC, proto);
|
|
|
|
if (ret >= 0) {
|
|
|
|
if (ret <= 2)
|
|
|
|
goto fix_cloexec;
|
|
|
|
goto update_max_fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cloexec_state < 0) { /* usually runs once only for detection */
|
2011-11-05 04:56:50 -04:00
|
|
|
ret = socket(domain, type|SOCK_CLOEXEC, proto);
|
2014-01-28 09:37:34 -05:00
|
|
|
if (ret >= 0) {
|
|
|
|
cloexec_state = rsock_detect_cloexec(ret);
|
|
|
|
if (cloexec_state == 0 || ret <= 2)
|
|
|
|
goto fix_cloexec;
|
|
|
|
goto update_max_fd;
|
|
|
|
}
|
|
|
|
else if (ret == -1 && errno == EINVAL) {
|
2011-11-05 04:56:50 -04:00
|
|
|
/* SOCK_CLOEXEC is available since Linux 2.6.27. Linux 2.6.18 fails with EINVAL */
|
|
|
|
ret = socket(domain, type, proto);
|
|
|
|
if (ret != -1) {
|
2014-01-28 09:37:34 -05:00
|
|
|
cloexec_state = 0;
|
|
|
|
/* fall through to fix_cloexec */
|
2011-11-05 04:56:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-28 09:37:34 -05:00
|
|
|
else { /* cloexec_state == 0 */
|
2011-11-05 04:56:50 -04:00
|
|
|
ret = socket(domain, type, proto);
|
|
|
|
}
|
2011-10-31 10:38:50 -04:00
|
|
|
#else
|
2011-11-05 02:46:02 -04:00
|
|
|
ret = socket(domain, type, proto);
|
2011-10-31 10:38:50 -04:00
|
|
|
#endif
|
2011-11-05 04:56:50 -04:00
|
|
|
if (ret == -1)
|
2011-11-05 02:46:02 -04:00
|
|
|
return -1;
|
2014-01-31 02:08:52 -05:00
|
|
|
#ifdef SOCK_CLOEXEC
|
2014-01-28 09:37:34 -05:00
|
|
|
fix_cloexec:
|
2014-01-31 02:08:52 -05:00
|
|
|
#endif
|
2014-01-28 09:37:34 -05:00
|
|
|
rb_maygvl_fd_fix_cloexec(ret);
|
2014-01-31 02:08:52 -05:00
|
|
|
#ifdef SOCK_CLOEXEC
|
2014-01-28 09:37:34 -05:00
|
|
|
update_max_fd:
|
2014-01-31 02:08:52 -05:00
|
|
|
#endif
|
2014-01-28 09:37:34 -05:00
|
|
|
rb_update_max_fd(ret);
|
2011-11-05 02:46:02 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rsock_socket(int domain, int type, int proto)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = rsock_socket0(domain, type, proto);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
|
|
|
fd = rsock_socket0(domain, type, proto);
|
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
2011-11-03 09:14:48 -04:00
|
|
|
if (0 <= fd)
|
2011-11-05 02:46:02 -04:00
|
|
|
rb_update_max_fd(fd);
|
2009-01-16 23:11:27 -05:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-05-03 21:12:04 -04:00
|
|
|
wait_connectable(int fd)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int sockerr;
|
|
|
|
socklen_t sockerrlen;
|
2011-05-03 22:44:28 -04:00
|
|
|
int revents;
|
|
|
|
int ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
for (;;) {
|
2011-05-03 22:44:28 -04:00
|
|
|
/*
|
2014-01-19 00:43:23 -05:00
|
|
|
* Stevens book says, successful finish turn on RB_WAITFD_OUT and
|
2011-05-03 22:44:28 -04:00
|
|
|
* failure finish turn on both RB_WAITFD_IN and RB_WAITFD_OUT.
|
|
|
|
*/
|
|
|
|
revents = rb_wait_for_single_fd(fd, RB_WAITFD_IN|RB_WAITFD_OUT, NULL);
|
|
|
|
|
|
|
|
if (revents & (RB_WAITFD_IN|RB_WAITFD_OUT)) {
|
|
|
|
sockerrlen = (socklen_t)sizeof(sockerr);
|
|
|
|
ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Solaris getsockopt(SO_ERROR) return -1 and set errno
|
|
|
|
* in getsockopt(). Let's return immediately.
|
|
|
|
*/
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2011-05-03 21:12:04 -04:00
|
|
|
if (sockerr == 0)
|
|
|
|
continue; /* workaround for winsock */
|
2011-05-03 22:44:28 -04:00
|
|
|
|
|
|
|
/* BSD and Linux use sockerr. */
|
2011-05-03 21:12:04 -04:00
|
|
|
errno = sockerr;
|
2011-05-03 22:44:28 -04:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((revents & (RB_WAITFD_IN|RB_WAITFD_OUT)) == RB_WAITFD_OUT) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
}
|
2011-05-03 22:44:28 -04:00
|
|
|
|
|
|
|
return ret;
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#define WAIT_IN_PROGRESS 10
|
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#define WAIT_IN_PROGRESS 10
|
|
|
|
#endif
|
|
|
|
#ifdef __linux__
|
|
|
|
/* returns correct error */
|
|
|
|
#define WAIT_IN_PROGRESS 0
|
|
|
|
#endif
|
|
|
|
#ifndef WAIT_IN_PROGRESS
|
|
|
|
/* BSD origin code apparently has a problem */
|
|
|
|
#define WAIT_IN_PROGRESS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct connect_arg {
|
|
|
|
int fd;
|
|
|
|
const struct sockaddr *sockaddr;
|
|
|
|
socklen_t len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
connect_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct connect_arg *arg = data;
|
|
|
|
return (VALUE)connect(arg->fd, arg->sockaddr, arg->len);
|
|
|
|
}
|
|
|
|
|
2009-01-27 06:07:41 -05:00
|
|
|
#if defined(SOCKS) && !defined(SOCKS5)
|
|
|
|
static VALUE
|
|
|
|
socks_connect_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct connect_arg *arg = data;
|
|
|
|
return (VALUE)Rconnect(arg->fd, arg->sockaddr, arg->len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
int
|
2009-03-01 01:48:22 -05:00
|
|
|
rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
rb_blocking_function_t *func = connect_blocking;
|
|
|
|
struct connect_arg arg;
|
|
|
|
#if WAIT_IN_PROGRESS > 0
|
|
|
|
int wait_in_progress = -1;
|
|
|
|
int sockerr;
|
|
|
|
socklen_t sockerrlen;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
arg.fd = fd;
|
|
|
|
arg.sockaddr = sockaddr;
|
|
|
|
arg.len = len;
|
|
|
|
#if defined(SOCKS) && !defined(SOCKS5)
|
|
|
|
if (socks) func = socks_connect_blocking;
|
|
|
|
#endif
|
|
|
|
for (;;) {
|
2011-02-12 00:44:23 -05:00
|
|
|
status = (int)BLOCKING_REGION_FD(func, &arg);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (status < 0) {
|
|
|
|
switch (errno) {
|
2011-05-01 11:38:53 -04:00
|
|
|
case EINTR:
|
|
|
|
#if defined(ERESTART)
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
case EAGAIN:
|
|
|
|
#ifdef EINPROGRESS
|
|
|
|
case EINPROGRESS:
|
|
|
|
#endif
|
|
|
|
#if WAIT_IN_PROGRESS > 0
|
2010-04-28 04:14:13 -04:00
|
|
|
sockerrlen = (socklen_t)sizeof(sockerr);
|
2009-01-16 23:11:27 -05:00
|
|
|
status = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen);
|
|
|
|
if (status) break;
|
|
|
|
if (sockerr) {
|
|
|
|
status = -1;
|
|
|
|
errno = sockerr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef EALREADY
|
|
|
|
case EALREADY:
|
|
|
|
#endif
|
|
|
|
#if WAIT_IN_PROGRESS > 0
|
|
|
|
wait_in_progress = WAIT_IN_PROGRESS;
|
|
|
|
#endif
|
|
|
|
status = wait_connectable(fd);
|
|
|
|
if (status) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#if WAIT_IN_PROGRESS > 0
|
|
|
|
case EINVAL:
|
|
|
|
if (wait_in_progress-- > 0) {
|
|
|
|
/*
|
|
|
|
* connect() after EINPROGRESS returns EINVAL on
|
|
|
|
* some platforms, need to check true error
|
|
|
|
* status.
|
|
|
|
*/
|
2010-04-28 04:14:13 -04:00
|
|
|
sockerrlen = (socklen_t)sizeof(sockerr);
|
2009-01-16 23:11:27 -05:00
|
|
|
status = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen);
|
|
|
|
if (!status && !sockerr) {
|
|
|
|
struct timeval tv = {0, 100000};
|
|
|
|
rb_thread_wait_for(tv);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status = -1;
|
|
|
|
errno = sockerr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EISCONN
|
|
|
|
case EISCONN:
|
|
|
|
status = 0;
|
|
|
|
errno = 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
make_fd_nonblock(int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
#ifdef F_GETFL
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags == -1) {
|
2013-04-05 22:39:44 -04:00
|
|
|
rb_sys_fail("fnctl(2)");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
flags = 0;
|
|
|
|
#endif
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(fd, F_SETFL, flags) == -1) {
|
2013-04-05 22:39:44 -04:00
|
|
|
rb_sys_fail("fnctl(2)");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 19:26:19 -04:00
|
|
|
static int
|
|
|
|
cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
|
|
|
|
{
|
|
|
|
int ret;
|
2013-01-30 07:01:51 -05:00
|
|
|
socklen_t len0 = 0;
|
2011-10-31 19:26:19 -04:00
|
|
|
#ifdef HAVE_ACCEPT4
|
|
|
|
static int try_accept4 = 1;
|
2013-02-04 01:19:40 -05:00
|
|
|
#endif
|
|
|
|
if (address_len) len0 = *address_len;
|
|
|
|
#ifdef HAVE_ACCEPT4
|
2011-10-31 19:26:19 -04:00
|
|
|
if (try_accept4) {
|
2013-04-05 12:40:36 -04:00
|
|
|
int flags = 0;
|
|
|
|
#ifdef SOCK_CLOEXEC
|
|
|
|
flags |= SOCK_CLOEXEC;
|
|
|
|
#endif
|
|
|
|
ret = accept4(socket, address, address_len, flags);
|
2011-10-31 19:26:19 -04:00
|
|
|
/* accept4 is available since Linux 2.6.28, glibc 2.10. */
|
2011-10-31 23:04:03 -04:00
|
|
|
if (ret != -1) {
|
|
|
|
if (ret <= 2)
|
|
|
|
rb_maygvl_fd_fix_cloexec(ret);
|
2013-01-30 07:01:51 -05:00
|
|
|
if (address_len && len0 < *address_len) *address_len = len0;
|
2011-10-31 23:04:03 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2013-02-04 01:19:40 -05:00
|
|
|
if (errno != ENOSYS) {
|
|
|
|
return -1;
|
2011-10-31 19:26:19 -04:00
|
|
|
}
|
2013-02-04 01:19:40 -05:00
|
|
|
try_accept4 = 0;
|
2011-10-31 19:26:19 -04:00
|
|
|
}
|
|
|
|
#endif
|
2013-02-04 01:19:40 -05:00
|
|
|
ret = accept(socket, address, address_len);
|
2011-10-31 23:04:03 -04:00
|
|
|
if (ret == -1) return -1;
|
2013-01-30 07:01:51 -05:00
|
|
|
if (address_len && len0 < *address_len) *address_len = len0;
|
2011-10-31 23:04:03 -04:00
|
|
|
rb_maygvl_fd_fix_cloexec(ret);
|
2011-10-31 19:26:19 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int fd2;
|
|
|
|
|
|
|
|
rb_secure(3);
|
|
|
|
rb_io_set_nonblock(fptr);
|
2011-10-31 19:26:19 -04:00
|
|
|
fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (fd2 < 0) {
|
2009-02-21 23:38:46 -05:00
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
case ECONNABORTED:
|
|
|
|
#if defined EPROTO
|
|
|
|
case EPROTO:
|
|
|
|
#endif
|
2013-04-08 15:58:55 -04:00
|
|
|
rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "accept(2) would block");
|
2009-02-21 23:38:46 -05:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_sys_fail("accept(2)");
|
|
|
|
}
|
2011-10-31 23:04:03 -04:00
|
|
|
rb_update_max_fd(fd2);
|
2009-01-16 23:11:27 -05:00
|
|
|
make_fd_nonblock(fd2);
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_init_sock(rb_obj_alloc(klass), fd2);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct accept_arg {
|
|
|
|
int fd;
|
|
|
|
struct sockaddr *sockaddr;
|
|
|
|
socklen_t *len;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
accept_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct accept_arg *arg = data;
|
2011-10-31 19:26:19 -04:00
|
|
|
return (VALUE)cloexec_accept(arg->fd, arg->sockaddr, arg->len);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int fd2;
|
|
|
|
int retry = 0;
|
|
|
|
struct accept_arg arg;
|
|
|
|
|
|
|
|
rb_secure(3);
|
|
|
|
arg.fd = fd;
|
|
|
|
arg.sockaddr = sockaddr;
|
|
|
|
arg.len = len;
|
|
|
|
retry:
|
2014-01-18 09:13:22 -05:00
|
|
|
rsock_maybe_wait_fd(fd);
|
2011-02-12 00:44:23 -05:00
|
|
|
fd2 = (int)BLOCKING_REGION_FD(accept_blocking, &arg);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (fd2 < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
|
|
|
if (retry) break;
|
|
|
|
rb_gc();
|
|
|
|
retry = 1;
|
|
|
|
goto retry;
|
|
|
|
default:
|
|
|
|
if (!rb_io_wait_readable(fd)) break;
|
|
|
|
retry = 0;
|
|
|
|
goto retry;
|
|
|
|
}
|
2013-04-05 22:39:44 -04:00
|
|
|
rb_sys_fail("accept(2)");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
2011-10-31 23:04:03 -04:00
|
|
|
rb_update_max_fd(fd2);
|
2009-01-16 23:11:27 -05:00
|
|
|
if (!klass) return INT2NUM(fd2);
|
2009-03-01 01:30:41 -05:00
|
|
|
return rsock_init_sock(rb_obj_alloc(klass), fd2);
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
2009-03-01 06:58:13 -05:00
|
|
|
int
|
|
|
|
rsock_getfamily(int sockfd)
|
2009-02-09 08:56:43 -05:00
|
|
|
{
|
2013-02-24 12:51:17 -05:00
|
|
|
union_sockaddr ss;
|
2010-04-28 04:14:13 -04:00
|
|
|
socklen_t sslen = (socklen_t)sizeof(ss);
|
2009-02-09 08:56:43 -05:00
|
|
|
|
2013-02-24 12:51:17 -05:00
|
|
|
ss.addr.sa_family = AF_UNSPEC;
|
|
|
|
if (getsockname(sockfd, &ss.addr, &sslen) < 0)
|
2009-07-17 00:38:27 -04:00
|
|
|
return AF_UNSPEC;
|
2009-02-09 08:56:43 -05:00
|
|
|
|
2013-02-24 12:51:17 -05:00
|
|
|
return ss.addr.sa_family;
|
2009-02-09 08:56:43 -05:00
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
void
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_socket_init()
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2011-08-15 19:08:39 -04:00
|
|
|
/*
|
|
|
|
* SocketError is the error class for socket.
|
|
|
|
*/
|
2009-01-16 23:11:27 -05:00
|
|
|
rb_eSocket = rb_define_class("SocketError", rb_eStandardError);
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_ipsocket();
|
|
|
|
rsock_init_tcpsocket();
|
|
|
|
rsock_init_tcpserver();
|
|
|
|
rsock_init_sockssocket();
|
|
|
|
rsock_init_udpsocket();
|
|
|
|
rsock_init_unixsocket();
|
|
|
|
rsock_init_unixserver();
|
|
|
|
rsock_init_sockopt();
|
|
|
|
rsock_init_ancdata();
|
|
|
|
rsock_init_addrinfo();
|
2013-05-11 04:32:26 -04:00
|
|
|
rsock_init_sockifaddr();
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_socket_constants();
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|