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
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
int rsock_do_not_reverse_lookup = 0;
|
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;
|
2009-10-14 11:26:40 -04:00
|
|
|
struct stat sbuf;
|
|
|
|
|
2009-10-14 20:29:10 -04:00
|
|
|
#ifndef _WIN32
|
2009-10-14 11:26:40 -04:00
|
|
|
if (fstat(fd, &sbuf) < 0)
|
|
|
|
rb_sys_fail(0);
|
|
|
|
if (!S_ISSOCK(sbuf.st_mode))
|
|
|
|
rb_raise(rb_eArgError, "not a socket file descriptor");
|
2009-10-14 20:29:10 -04:00
|
|
|
#else
|
|
|
|
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;
|
2009-02-10 09:46:51 -05:00
|
|
|
struct sockaddr_storage buf;
|
2009-01-16 23:11:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
recvfrom_blocking(void *data)
|
|
|
|
{
|
|
|
|
struct recvfrom_arg *arg = data;
|
|
|
|
return (VALUE)recvfrom(arg->fd, RSTRING_PTR(arg->str), RSTRING_LEN(arg->str),
|
2009-02-10 09:46:51 -05:00
|
|
|
arg->flags, (struct sockaddr*)&arg->buf, &arg->alen);
|
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;
|
|
|
|
arg.alen = sizeof(arg.buf);
|
|
|
|
|
|
|
|
arg.str = str = rb_tainted_str_new(0, buflen);
|
|
|
|
klass = RBASIC(str)->klass;
|
|
|
|
RBASIC(str)->klass = 0;
|
|
|
|
|
|
|
|
while (rb_io_check_closed(fptr),
|
|
|
|
rb_thread_wait_fd(arg.fd),
|
|
|
|
(slen = BLOCKING_REGION(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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RBASIC(str)->klass = klass;
|
|
|
|
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 */
|
2009-03-01 01:30:41 -05:00
|
|
|
return rb_assoc_new(str, rsock_ipaddr((struct sockaddr*)&arg.buf, 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:
|
2009-03-01 01:30:41 -05:00
|
|
|
return rb_assoc_new(str, rsock_unixaddr((struct sockaddr_un*)&arg.buf, arg.alen));
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
|
|
|
case RECV_SOCKET:
|
2009-03-01 01:30:41 -05:00
|
|
|
return rb_assoc_new(str, rsock_io_socket_addrinfo(sock, (struct sockaddr*)&arg.buf, 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;
|
2009-02-10 09:46:51 -05:00
|
|
|
struct sockaddr_storage buf;
|
2009-01-16 23:11:27 -05:00
|
|
|
socklen_t alen = sizeof buf;
|
|
|
|
VALUE len, flg;
|
|
|
|
long buflen;
|
|
|
|
long slen;
|
|
|
|
int fd, flags;
|
|
|
|
VALUE addr = Qnil;
|
|
|
|
|
|
|
|
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);
|
2009-02-10 09:46:51 -05:00
|
|
|
slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, (struct sockaddr*)&buf, &alen);
|
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
|
2009-03-19 07:40:38 -04:00
|
|
|
rb_mod_sys_fail(rb_mWaitReadable, "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 */
|
2009-03-01 01:30:41 -05:00
|
|
|
addr = rsock_ipaddr((struct sockaddr*)&buf, fptr->mode & FMODE_NOREVLOOKUP);
|
2009-01-16 23:11:27 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RECV_SOCKET:
|
2009-03-01 01:30:41 -05:00
|
|
|
addr = rsock_io_socket_addrinfo(sock, (struct sockaddr*)&buf, 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-01 01:48:22 -05:00
|
|
|
rsock_socket(int domain, int type, int proto)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket(domain, type, proto);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
|
|
|
fd = socket(domain, type, proto);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
wait_connectable0(int fd, rb_fdset_t *fds_w, rb_fdset_t *fds_e)
|
|
|
|
{
|
|
|
|
int sockerr;
|
|
|
|
socklen_t sockerrlen;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
rb_fd_zero(fds_w);
|
|
|
|
rb_fd_zero(fds_e);
|
|
|
|
|
|
|
|
rb_fd_set(fd, fds_w);
|
|
|
|
rb_fd_set(fd, fds_e);
|
|
|
|
|
|
|
|
rb_thread_select(fd+1, 0, rb_fd_ptr(fds_w), rb_fd_ptr(fds_e), 0);
|
|
|
|
|
|
|
|
if (rb_fd_isset(fd, fds_w)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (rb_fd_isset(fd, fds_e)) {
|
|
|
|
sockerrlen = sizeof(sockerr);
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr,
|
|
|
|
&sockerrlen) == 0) {
|
|
|
|
if (sockerr == 0)
|
|
|
|
continue; /* workaround for winsock */
|
|
|
|
errno = sockerr;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wait_connectable_arg {
|
|
|
|
int fd;
|
|
|
|
rb_fdset_t fds_w;
|
|
|
|
rb_fdset_t fds_e;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef HAVE_RB_FD_INIT
|
|
|
|
static VALUE
|
|
|
|
try_wait_connectable(VALUE arg)
|
|
|
|
{
|
|
|
|
struct wait_connectable_arg *p = (struct wait_connectable_arg *)arg;
|
|
|
|
return (VALUE)wait_connectable0(p->fd, &p->fds_w, &p->fds_e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
wait_connectable_ensure(VALUE arg)
|
|
|
|
{
|
|
|
|
struct wait_connectable_arg *p = (struct wait_connectable_arg *)arg;
|
|
|
|
rb_fd_term(&p->fds_w);
|
|
|
|
rb_fd_term(&p->fds_e);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
wait_connectable(int fd)
|
|
|
|
{
|
|
|
|
struct wait_connectable_arg arg;
|
|
|
|
|
|
|
|
rb_fd_init(&arg.fds_w);
|
|
|
|
rb_fd_init(&arg.fds_e);
|
|
|
|
#ifdef HAVE_RB_FD_INIT
|
|
|
|
arg.fd = fd;
|
|
|
|
return (int)rb_ensure(try_wait_connectable, (VALUE)&arg,
|
|
|
|
wait_connectable_ensure,(VALUE)&arg);
|
|
|
|
#else
|
|
|
|
return wait_connectable0(fd, &arg.fds_w, &arg.fds_e);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 (;;) {
|
|
|
|
status = (int)BLOCKING_REGION(func, &arg);
|
|
|
|
if (status < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
#ifdef EINPROGRESS
|
|
|
|
case EINPROGRESS:
|
|
|
|
#endif
|
|
|
|
#if WAIT_IN_PROGRESS > 0
|
|
|
|
sockerrlen = sizeof(sockerr);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
sockerrlen = sizeof(sockerr);
|
|
|
|
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) {
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
flags = 0;
|
|
|
|
#endif
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
if (fcntl(fd, F_SETFL, flags) == -1) {
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
fd2 = accept(fptr->fd, (struct sockaddr*)sockaddr, len);
|
|
|
|
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
|
2009-03-19 07:40:38 -04:00
|
|
|
rb_mod_sys_fail(rb_mWaitReadable, "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)");
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
return (VALUE)accept(arg->fd, arg->sockaddr, arg->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
|
|
|
rb_thread_wait_fd(fd);
|
|
|
|
fd2 = BLOCKING_REGION(accept_blocking, &arg);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t sslen = sizeof(ss);
|
|
|
|
|
|
|
|
ss.ss_family = AF_UNSPEC;
|
|
|
|
if (getsockname(sockfd, (struct sockaddr*)&ss, &sslen) < 0)
|
2009-07-17 00:38:27 -04:00
|
|
|
return AF_UNSPEC;
|
2009-02-09 08:56:43 -05:00
|
|
|
|
|
|
|
return ss.ss_family;
|
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
/*
|
|
|
|
* SocketError is the error class for socket.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Init_socket_init()
|
|
|
|
{
|
|
|
|
rb_eSocket = rb_define_class("SocketError", rb_eStandardError);
|
|
|
|
Init_ipsocket();
|
|
|
|
Init_tcpsocket();
|
|
|
|
Init_tcpserver();
|
|
|
|
Init_sockssocket();
|
|
|
|
Init_udpsocket();
|
|
|
|
Init_unixsocket();
|
|
|
|
Init_unixserver();
|
2009-02-01 20:53:02 -05:00
|
|
|
Init_sockopt();
|
2009-02-01 21:51:02 -05:00
|
|
|
Init_ancdata();
|
2009-01-16 23:11:27 -05:00
|
|
|
Init_addrinfo();
|
|
|
|
Init_socket_constants();
|
|
|
|
}
|