28 VALUE rb_cSOCKSSocket;
32 static VALUE sym_wait_readable;
46 #define is_socket(fd) rb_w32_is_socket(fd) 53 if (
fstat(fd, &sbuf) < 0)
55 return S_ISSOCK(sbuf.st_mode);
107 recvfrom_blocking(
void *data)
114 if (ret != -1 && len0 < arg->
alen)
120 rsock_strbuf(
VALUE str,
long buflen)
138 recvfrom_locktmp(
VALUE v)
160 str = rsock_strbuf(str, buflen);
188 if (arg.
alen !=
sizeof(
struct sockaddr_in)) {
204 rb_bug(
"rsock_s_recvfrom called with bad value");
223 str = rsock_strbuf(str, buflen);
228 flags |= MSG_DONTWAIT;
243 slen = recvfrom(fd,
RSTRING_PTR(str), buflen, flags, &buf.
addr, &alen);
244 if (slen != -1 && len0 < alen)
251 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 255 return sym_wait_readable;
269 if (alen && alen !=
sizeof(buf))
278 rb_bug(
"rsock_s_recvfrom_nonblock called with bad value");
300 rsock_socket0(
int domain,
int type,
int proto)
303 static int cloexec_state = -1;
305 if (cloexec_state > 0) {
306 ret = socket(domain, type|SOCK_CLOEXEC, proto);
313 else if (cloexec_state < 0) {
314 ret = socket(domain, type|SOCK_CLOEXEC, proto);
317 if (cloexec_state == 0 || ret <= 2)
321 else if (ret == -1 &&
errno == EINVAL) {
323 ret = socket(domain, type, proto);
331 ret = socket(domain, type, proto);
344 rsock_socket0(
int domain,
int type,
int proto)
346 int ret = socket(domain, type, proto);
361 fd = rsock_socket0(domain, type, proto);
364 fd = rsock_socket0(domain, type, proto);
374 wait_connectable(
int fd)
376 int sockerr, revents;
381 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (
void *)&sockerr, &sockerrlen) < 0)
398 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (
void *)&sockerr, &sockerrlen) < 0)
438 connect_blocking(
void *data)
444 #if defined(SOCKS) && !defined(SOCKS5) 446 socks_connect_blocking(
void *data)
463 #if defined(SOCKS) && !defined(SOCKS5) 464 if (socks) func = socks_connect_blocking;
478 return wait_connectable(fd);
485 make_fd_nonblock(
int fd)
489 flags =
fcntl(fd, F_GETFL);
509 static int try_accept4 = 1;
511 if (address_len) len0 = *address_len;
516 flags |= SOCK_CLOEXEC;
520 flags |= SOCK_NONBLOCK;
523 ret = accept4(socket, address, address_len, flags);
528 #ifndef SOCK_NONBLOCK 530 make_fd_nonblock(ret);
533 if (address_len && len0 < *address_len) *address_len = len0;
536 if (
errno != ENOSYS) {
542 ret = accept(socket, address, address_len);
543 if (ret == -1)
return -1;
544 if (address_len && len0 < *address_len) *address_len = len0;
547 make_fd_nonblock(ret);
559 fd2 = cloexec_accept(fptr->
fd, (
struct sockaddr*)sockaddr, len, 1);
564 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 572 return sym_wait_readable;
588 accept_blocking(
void *data)
625 if (!klass)
return INT2NUM(fd2);
647 if (getsockname(fptr->
fd, &ss.
addr, &sslen) < 0)
650 switch (ss.
addr.sa_family) {
658 return ss.
addr.sa_family;
void rb_bug(const char *fmt,...)
struct sockaddr * sockaddr
void rb_syserr_fail(int e, const char *mesg)
VALUE rsock_sendto_blocking(void *data)
void rb_update_max_fd(int fd)
void rb_io_set_nonblock(rb_io_t *fptr)
void rb_io_synchronized(rb_io_t *)
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rsock_init_sock(VALUE sock, int fd)
void rsock_init_unixsocket(void)
VALUE rb_str_locktmp_ensure(VALUE str, VALUE(*func)(VALUE), VALUE arg)
void rb_str_set_len(VALUE, long)
void rsock_init_tcpserver(void)
int rb_gc_for_fd(int err)
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
#define FMODE_NOREVLOOKUP
void rsock_init_sockifaddr(void)
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
void rsock_init_socket_constants(void)
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
VALUE rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex, enum sock_recv_type from)
#define GetOpenFile(obj, fp)
void rsock_init_addrinfo(void)
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
VALUE rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
VALUE rb_obj_taint(VALUE)
call-seq: obj.taint -> obj
void rsock_init_tcpsocket(void)
void rsock_init_sockssocket(void)
VALUE rsock_send_blocking(void *data)
int rsock_getfamily(rb_io_t *fptr)
int rsock_socket(int domain, int type, int proto)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
void rsock_init_ancdata(void)
void rb_sys_fail(const char *mesg)
char * gai_strerror(int ecode)
VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
void rb_str_modify_expand(VALUE, long)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
void rsock_init_udpsocket(void)
int rsock_do_not_reverse_lookup
void rb_readwrite_syserr_fail(enum rb_io_wait_readwrite writable, int n, const char *mesg)
int rsock_detect_cloexec(int fd)
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
VALUE rb_blocking_function_t(void *)
int rb_reserved_fd_p(int fd)
register unsigned int len
const struct sockaddr * sockaddr
void rb_str_modify(VALUE)
int rb_io_read_pending(rb_io_t *)
void rb_fd_fix_cloexec(int fd)
void rsock_raise_socket_error(const char *reason, int error)
#define is_socket(fd, path)
#define rsock_maybe_wait_fd(fd)
VALUE rb_io_ascii8bit_binmode(VALUE)
int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks)
RUBY_EXTERN char * strerror(int)
void rsock_init_ipsocket(void)
void rsock_init_unixserver(void)
#define MSG_DONTWAIT_RELIABLE
RUBY_EXTERN VALUE rb_eIOError
#define MakeOpenFile(obj, fp)
void rsock_init_sockopt(void)
VALUE rb_tainted_str_new(const char *, long)
int rb_io_wait_readable(int)
void rsock_init_socket_init(void)
void rb_io_check_closed(rb_io_t *)
#define BLOCKING_REGION_FD(func, arg)
void rb_maygvl_fd_fix_cloexec(int fd)