Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
init.c
Go to the documentation of this file.
1/************************************************
2
3 init.c -
4
5 created at: Thu Mar 31 12:21:29 JST 1994
6
7 Copyright (C) 1993-2007 Yukihiro Matsumoto
8
9************************************************/
10
11#include "rubysocket.h"
12
13#ifdef _WIN32
14VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc);
15#endif
16
22#ifdef AF_UNIX
23VALUE rb_cUNIXSocket;
24VALUE rb_cUNIXServer;
25#endif
28
30
31#ifdef SOCKS
32VALUE rb_cSOCKSSocket;
33#endif
34
36static VALUE sym_wait_readable;
37
38void
39rsock_raise_socket_error(const char *reason, int error)
40{
41#ifdef EAI_SYSTEM
42 int e;
43 if (error == EAI_SYSTEM && (e = errno) != 0)
44 rb_syserr_fail(e, reason);
45#endif
46#ifdef _WIN32
48 VALUE msg = rb_sprintf("%s: ", reason);
49 if (!enc) enc = rb_default_internal_encoding();
50 rb_str_concat(msg, rb_w32_conv_from_wchar(gai_strerrorW(error), enc));
52#else
53 rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
54#endif
55}
56
57#ifdef _WIN32
58#define is_socket(fd) rb_w32_is_socket(fd)
59#else
60static int
61is_socket(int fd)
62{
63 struct stat sbuf;
64
65 if (fstat(fd, &sbuf) < 0)
66 rb_sys_fail("fstat(2)");
67 return S_ISSOCK(sbuf.st_mode);
68}
69#endif
70
71#if defined __APPLE__
72# define do_write_retry(code) do {ret = code;} while (ret == -1 && errno == EPROTOTYPE)
73#else
74# define do_write_retry(code) ret = code
75#endif
76
79{
80 rb_io_t *fp;
81
82 if (!is_socket(fd) || rb_reserved_fd_p(fd)) {
83 rb_syserr_fail(EBADF, "not a socket file descriptor");
84 }
85
87 MakeOpenFile(sock, fp);
88 fp->fd = fd;
93 }
95
96 return sock;
97}
98
101{
102 struct rsock_send_arg *arg = data;
103 VALUE mesg = arg->mesg;
104 ssize_t ret;
106 arg->flags, arg->to, arg->tolen));
107 return (VALUE)ret;
108}
109
110VALUE
112{
113 struct rsock_send_arg *arg = data;
114 VALUE mesg = arg->mesg;
115 ssize_t ret;
117 arg->flags));
118 return (VALUE)ret;
119}
120
122 int fd, flags;
124 size_t length;
127};
128
129static VALUE
130recvfrom_blocking(void *data)
131{
132 struct recvfrom_arg *arg = data;
133 socklen_t len0 = arg->alen;
134 ssize_t ret;
135 ret = recvfrom(arg->fd, RSTRING_PTR(arg->str), arg->length,
136 arg->flags, &arg->buf.addr, &arg->alen);
137 if (ret != -1 && len0 < arg->alen)
138 arg->alen = len0;
139
140 return (VALUE)ret;
141}
142
143static VALUE
144rsock_strbuf(VALUE str, long buflen)
145{
146 long len;
147
148 if (NIL_P(str)) return rb_str_new(0, buflen);
149
152 if (len >= buflen) {
154 } else {
155 rb_str_modify_expand(str, buflen - len);
156 }
157 return str;
158}
159
160static VALUE
161recvfrom_locktmp(VALUE v)
162{
163 struct recvfrom_arg *arg = (struct recvfrom_arg *)v;
164
165 return rb_thread_io_blocking_region(recvfrom_blocking, arg, arg->fd);
166}
167
168VALUE
170{
171 rb_io_t *fptr;
172 VALUE str;
173 struct recvfrom_arg arg;
174 VALUE len, flg;
175 long buflen;
176 long slen;
177
178 rb_scan_args(argc, argv, "12", &len, &flg, &str);
179
180 if (flg == Qnil) arg.flags = 0;
181 else arg.flags = NUM2INT(flg);
182 buflen = NUM2INT(len);
183 str = rsock_strbuf(str, buflen);
184
185 GetOpenFile(sock, fptr);
186 if (rb_io_read_pending(fptr)) {
187 rb_raise(rb_eIOError, "recv for buffered IO");
188 }
189 arg.fd = fptr->fd;
190 arg.alen = (socklen_t)sizeof(arg.buf);
191 arg.str = str;
192 arg.length = buflen;
193
194 while (rb_io_check_closed(fptr),
196 (slen = (long)rb_str_locktmp_ensure(str, recvfrom_locktmp,
197 (VALUE)&arg)) < 0) {
198 if (!rb_io_wait_readable(fptr->fd)) {
199 rb_sys_fail("recvfrom(2)");
200 }
201 }
202
203 /* Resize the string to the amount of data received */
204 rb_str_set_len(str, slen);
205 switch (from) {
206 case RECV_RECV:
207 return str;
208 case RECV_IP:
209#if 0
210 if (arg.alen != sizeof(struct sockaddr_in)) {
211 rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
212 }
213#endif
214 if (arg.alen && arg.alen != sizeof(arg.buf)) /* OSX doesn't return a from result for connection-oriented sockets */
215 return rb_assoc_new(str, rsock_ipaddr(&arg.buf.addr, arg.alen, fptr->mode & FMODE_NOREVLOOKUP));
216 else
217 return rb_assoc_new(str, Qnil);
218
219#ifdef HAVE_SYS_UN_H
220 case RECV_UNIX:
221 return rb_assoc_new(str, rsock_unixaddr(&arg.buf.un, arg.alen));
222#endif
223 case RECV_SOCKET:
224 return rb_assoc_new(str, rsock_io_socket_addrinfo(sock, &arg.buf.addr, arg.alen));
225 default:
226 rb_bug("rsock_s_recvfrom called with bad value");
227 }
228}
229
230VALUE
232 VALUE ex, enum sock_recv_type from)
233{
234 rb_io_t *fptr;
236 socklen_t alen = (socklen_t)sizeof buf;
237 long buflen;
238 long slen;
239 int fd, flags;
240 VALUE addr = Qnil;
241 socklen_t len0;
242
243 flags = NUM2INT(flg);
244 buflen = NUM2INT(len);
245 str = rsock_strbuf(str, buflen);
246
247#ifdef MSG_DONTWAIT
248 /* MSG_DONTWAIT avoids the race condition between fcntl and recvfrom.
249 It is not portable, though. */
250 flags |= MSG_DONTWAIT;
251#endif
252
253 GetOpenFile(sock, fptr);
254 if (rb_io_read_pending(fptr)) {
255 rb_raise(rb_eIOError, "recvfrom for buffered IO");
256 }
257 fd = fptr->fd;
258
259 rb_io_check_closed(fptr);
260
262 rb_io_set_nonblock(fptr);
263
264 len0 = alen;
265 slen = recvfrom(fd, RSTRING_PTR(str), buflen, flags, &buf.addr, &alen);
266 if (slen != -1 && len0 < alen)
267 alen = len0;
268
269 if (slen < 0) {
270 int e = errno;
271 switch (e) {
272 case EAGAIN:
273#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
274 case EWOULDBLOCK:
275#endif
276 if (ex == Qfalse)
277 return sym_wait_readable;
278 rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE, e, "recvfrom(2) would block");
279 }
280 rb_syserr_fail(e, "recvfrom(2)");
281 }
282 if (slen != RSTRING_LEN(str)) {
283 rb_str_set_len(str, slen);
284 }
285 switch (from) {
286 case RECV_RECV:
287 return str;
288
289 case RECV_IP:
290 if (alen && alen != sizeof(buf)) /* connection-oriented socket may not return a from result */
291 addr = rsock_ipaddr(&buf.addr, alen, fptr->mode & FMODE_NOREVLOOKUP);
292 break;
293
294 case RECV_SOCKET:
295 addr = rsock_io_socket_addrinfo(sock, &buf.addr, alen);
296 break;
297
298 default:
299 rb_bug("rsock_s_recvfrom_nonblock called with bad value");
300 }
301 return rb_assoc_new(str, addr);
302}
303
304#if MSG_DONTWAIT_RELIABLE
305static VALUE sym_wait_writable;
306
307/* copied from io.c :< */
308static long
309read_buffered_data(char *ptr, long len, rb_io_t *fptr)
310{
311 int n = fptr->rbuf.len;
312
313 if (n <= 0) return 0;
314 if (n > len) n = (int)len;
315 MEMMOVE(ptr, fptr->rbuf.ptr+fptr->rbuf.off, char, n);
316 fptr->rbuf.off += n;
317 fptr->rbuf.len -= n;
318 return n;
319}
320
321/* :nodoc: */
322VALUE
324{
325 rb_io_t *fptr;
326 long n;
327 long len = NUM2LONG(length);
328 VALUE str = rsock_strbuf(buf, len);
329 char *ptr;
330
331 GetOpenFile(sock, fptr);
332
333 if (len == 0) {
335 return str;
336 }
337
339 n = read_buffered_data(ptr, len, fptr);
340 if (n <= 0) {
341 n = (long)recv(fptr->fd, ptr, len, MSG_DONTWAIT);
342 if (n < 0) {
343 int e = errno;
344 if ((e == EWOULDBLOCK || e == EAGAIN)) {
345 if (ex == Qfalse) return sym_wait_readable;
347 e, "read would block");
348 }
349 rb_syserr_fail_path(e, fptr->pathv);
350 }
351 }
352 if (n != RSTRING_LEN(str)) {
355 }
356 if (n == 0) {
357 if (ex == Qfalse) return Qnil;
358 rb_eof_error();
359 }
360
361 return str;
362}
363
364/* :nodoc: */
365VALUE
367{
368 rb_io_t *fptr;
369 long n;
370
371 if (!RB_TYPE_P(str, T_STRING))
373
374 sock = rb_io_get_write_io(sock);
375 GetOpenFile(sock, fptr);
377
378 /*
379 * As with IO#write_nonblock, we may block if somebody is relying on
380 * buffered I/O; but nobody actually hits this because pipes and sockets
381 * are not userspace-buffered in Ruby by default.
382 */
383 if (fptr->wbuf.len > 0) {
384 rb_io_flush(sock);
385 }
386
387#ifdef __APPLE__
388 again:
389#endif
390 n = (long)send(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str), MSG_DONTWAIT);
391 if (n < 0) {
392 int e = errno;
393
394#ifdef __APPLE__
395 if (e == EPROTOTYPE) {
396 goto again;
397 }
398#endif
399 if (e == EWOULDBLOCK || e == EAGAIN) {
400 if (ex == Qfalse) return sym_wait_writable;
402 "write would block");
403 }
404 rb_syserr_fail_path(e, fptr->pathv);
405 }
406
407 return LONG2FIX(n);
408}
409#endif /* MSG_DONTWAIT_RELIABLE */
410
411static int
412rsock_socket0(int domain, int type, int proto)
413{
414#ifdef SOCK_CLOEXEC
415 type |= SOCK_CLOEXEC;
416#endif
417
418#ifdef SOCK_NONBLOCK
419 type |= SOCK_NONBLOCK;
420#endif
421
422 int result = socket(domain, type, proto);
423
424 if (result == -1)
425 return -1;
426
427 rb_fd_fix_cloexec(result);
428
429#ifndef SOCK_NONBLOCK
431#endif
432
433 return result;
434}
435
436int
437rsock_socket(int domain, int type, int proto)
438{
439 int fd;
440
441 fd = rsock_socket0(domain, type, proto);
442 if (fd < 0) {
443 if (rb_gc_for_fd(errno)) {
444 fd = rsock_socket0(domain, type, proto);
445 }
446 }
447 if (0 <= fd)
449 return fd;
450}
451
452/* emulate blocking connect behavior on EINTR or non-blocking socket */
453static int
454wait_connectable(int fd, struct timeval *timeout)
455{
456 int sockerr, revents;
457 socklen_t sockerrlen;
458
459 sockerrlen = (socklen_t)sizeof(sockerr);
460 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen) < 0)
461 return -1;
462
463 /* necessary for non-blocking sockets (at least ECONNREFUSED) */
464 switch (sockerr) {
465 case 0:
466 break;
467#ifdef EALREADY
468 case EALREADY:
469#endif
470#ifdef EISCONN
471 case EISCONN:
472#endif
473#ifdef ECONNREFUSED
474 case ECONNREFUSED:
475#endif
476#ifdef EHOSTUNREACH
477 case EHOSTUNREACH:
478#endif
479 errno = sockerr;
480 return -1;
481 }
482
483 /*
484 * Stevens book says, successful finish turn on RB_WAITFD_OUT and
485 * failure finish turn on both RB_WAITFD_IN and RB_WAITFD_OUT.
486 * So it's enough to wait only RB_WAITFD_OUT and check the pending error
487 * by getsockopt().
488 *
489 * Note: rb_wait_for_single_fd already retries on EINTR/ERESTART
490 */
492
493 if (revents < 0)
494 return -1;
495
496 sockerrlen = (socklen_t)sizeof(sockerr);
497 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr, &sockerrlen) < 0)
498 return -1;
499
500 switch (sockerr) {
501 case 0:
502 /*
503 * be defensive in case some platforms set SO_ERROR on the original,
504 * interrupted connect()
505 */
506
507 /* when the connection timed out, no errno is set and revents is 0. */
508 if (timeout && revents == 0) {
509 errno = ETIMEDOUT;
510 return -1;
511 }
512 case EINTR:
513#ifdef ERESTART
514 case ERESTART:
515#endif
516 case EAGAIN:
517#ifdef EINPROGRESS
518 case EINPROGRESS:
519#endif
520#ifdef EALREADY
521 case EALREADY:
522#endif
523#ifdef EISCONN
524 case EISCONN:
525#endif
526 return 0; /* success */
527 default:
528 /* likely (but not limited to): ECONNREFUSED, ETIMEDOUT, EHOSTUNREACH */
529 errno = sockerr;
530 return -1;
531 }
532
533 return 0;
534}
535
537 int fd;
539 const struct sockaddr *sockaddr;
540};
541
542static VALUE
543connect_blocking(void *data)
544{
545 struct connect_arg *arg = data;
546 return (VALUE)connect(arg->fd, arg->sockaddr, arg->len);
547}
548
549#if defined(SOCKS) && !defined(SOCKS5)
550static VALUE
551socks_connect_blocking(void *data)
552{
553 struct connect_arg *arg = data;
554 return (VALUE)Rconnect(arg->fd, arg->sockaddr, arg->len);
555}
556#endif
557
558int
559rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks, struct timeval *timeout)
560{
561 int status;
562 rb_blocking_function_t *func = connect_blocking;
563 struct connect_arg arg;
564
565 arg.fd = fd;
566 arg.sockaddr = sockaddr;
567 arg.len = len;
568#if defined(SOCKS) && !defined(SOCKS5)
569 if (socks) func = socks_connect_blocking;
570#endif
571 status = (int)BLOCKING_REGION_FD(func, &arg);
572
573 if (status < 0) {
574 switch (errno) {
575 case EINTR:
576#ifdef ERESTART
577 case ERESTART:
578#endif
579 case EAGAIN:
580#ifdef EINPROGRESS
581 case EINPROGRESS:
582#endif
583 return wait_connectable(fd, timeout);
584 }
585 }
586 return status;
587}
588
589void
591{
592#ifdef _WIN32
593 return;
594#endif
595
596 int flags;
597#ifdef F_GETFL
598 flags = fcntl(fd, F_GETFL);
599 if (flags == -1) {
600 rb_sys_fail("fnctl(2)");
601 }
602#else
603 flags = 0;
604#endif
605 flags |= O_NONBLOCK;
606 if (fcntl(fd, F_SETFL, flags) == -1) {
607 rb_sys_fail("fnctl(2)");
608 }
609}
610
611static int
612cloexec_accept(int socket, struct sockaddr *address, socklen_t *address_len)
613{
614 socklen_t len0 = 0;
615 if (address_len) len0 = *address_len;
616
617#ifdef HAVE_ACCEPT4
618 int flags = SOCK_CLOEXEC;
619
620#ifdef SOCK_NONBLOCK
621 flags |= SOCK_NONBLOCK;
622#endif
623
624 int result = accept4(socket, address, address_len, flags);
625 if (result == -1) return -1;
626
627#ifndef SOCK_NONBLOCK
629#endif
630#else
631 int result = accept(socket, address, address_len);
632 if (result == -1) return -1;
633
636#endif
637
638 if (address_len && len0 < *address_len) *address_len = len0;
639 return result;
640}
641
642VALUE
644 struct sockaddr *sockaddr, socklen_t *len)
645{
646 int fd2;
647
648 rb_io_set_nonblock(fptr);
649 fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len);
650 if (fd2 < 0) {
651 int e = errno;
652 switch (e) {
653 case EAGAIN:
654#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
655 case EWOULDBLOCK:
656#endif
657 case ECONNABORTED:
658#if defined EPROTO
659 case EPROTO:
660#endif
661 if (ex == Qfalse)
662 return sym_wait_readable;
663 rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE, e, "accept(2) would block");
664 }
665 rb_syserr_fail(e, "accept(2)");
666 }
667 rb_update_max_fd(fd2);
668 return rsock_init_sock(rb_obj_alloc(klass), fd2);
669}
670
672 int fd;
675};
676
677static VALUE
678accept_blocking(void *data)
679{
680 struct accept_arg *arg = data;
681 return (VALUE)cloexec_accept(arg->fd, arg->sockaddr, arg->len);
682}
683
684VALUE
686{
687 int fd2;
688 int retry = 0;
689 struct accept_arg arg;
690
691 arg.fd = fd;
692 arg.sockaddr = sockaddr;
693 arg.len = len;
694 retry:
696 fd2 = (int)BLOCKING_REGION_FD(accept_blocking, &arg);
697 if (fd2 < 0) {
698 int e = errno;
699 switch (e) {
700 case EMFILE:
701 case ENFILE:
702 case ENOMEM:
703 if (retry) break;
704 rb_gc();
705 retry = 1;
706 goto retry;
707 default:
708 if (!rb_io_wait_readable(fd)) break;
709 retry = 0;
710 goto retry;
711 }
712 rb_syserr_fail(e, "accept(2)");
713 }
714 rb_update_max_fd(fd2);
715 if (!klass) return INT2NUM(fd2);
716 return rsock_init_sock(rb_obj_alloc(klass), fd2);
717}
718
719int
721{
723 socklen_t sslen = (socklen_t)sizeof(ss);
724 int cached = fptr->mode & FMODE_SOCK;
725
726 if (cached) {
727 switch (cached) {
728#ifdef AF_UNIX
729 case FMODE_UNIX: return AF_UNIX;
730#endif
731 case FMODE_INET: return AF_INET;
732 case FMODE_INET6: return AF_INET6;
733 }
734 }
735
736 ss.addr.sa_family = AF_UNSPEC;
737 if (getsockname(fptr->fd, &ss.addr, &sslen) < 0)
738 return AF_UNSPEC;
739
740 switch (ss.addr.sa_family) {
741#ifdef AF_UNIX
742 case AF_UNIX: fptr->mode |= FMODE_UNIX; break;
743#endif
744 case AF_INET: fptr->mode |= FMODE_INET; break;
745 case AF_INET6: fptr->mode |= FMODE_INET6; break;
746 }
747
748 return ss.addr.sa_family;
749}
750
751void
753{
754 /*
755 * SocketError is the error class for socket.
756 */
770
771#undef rb_intern
772 sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
773
774#if MSG_DONTWAIT_RELIABLE
775 sym_wait_writable = ID2SYM(rb_intern("wait_writable"));
776#endif
777}
#define EAI_SYSTEM
Definition: addrinfo.h:88
void rsock_init_ancdata(void)
Definition: ancdata.c:1693
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:975
void rsock_init_socket_constants(void)
Definition: constants.c:140
struct RIMemo * ptr
Definition: debug.c:88
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1734
uint8_t len
Definition: escape.c:17
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
void rb_gc(void)
Definition: gc.c:9497
char * gai_strerror(int ecode)
Definition: getaddrinfo.c:207
int socklen_t
Definition: getaddrinfo.c:83
VALUE rb_eIOError
Definition: io.c:185
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
void rb_syserr_fail(int e, const char *mesg)
Definition: error.c:3029
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
void rb_bug(const char *fmt,...)
Definition: error.c:768
VALUE rb_eStandardError
Definition: error.c:1054
VALUE rb_eTypeError
Definition: error.c:1057
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:1107
void rb_sys_fail(const char *mesg)
Definition: error.c:3041
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
Definition: object.c:1900
void rsock_init_sockifaddr(void)
Definition: ifaddr.c:454
void rb_readwrite_syserr_fail(enum rb_io_wait_readwrite, int, const char *)
Definition: io.c:13122
#define RB_IO_WAIT_WRITABLE
Definition: error.h:42
#define RB_IO_WAIT_READABLE
Definition: error.h:41
VALUE rb_io_ascii8bit_binmode(VALUE)
Definition: io.c:5495
void rb_update_max_fd(int fd)
Definition: io.c:233
VALUE rb_io_flush(VALUE)
Definition: io.c:2052
int rb_reserved_fd_p(int fd)
void rb_fd_fix_cloexec(int fd)
Definition: io.c:283
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:3217
#define rb_str_new(str, len)
Definition: string.h:213
void rb_str_set_len(VALUE, long)
Definition: string.c:2842
void rb_str_modify(VALUE)
Definition: string.c:2262
void rb_str_modify_expand(VALUE, long)
Definition: string.c:2270
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1529
VALUE rb_blocking_function_t(void *)
Definition: thread.h:60
#define ID2SYM
Definition: symbol.h:44
ID rb_intern(const char *)
Definition: symbol.c:785
#define FMODE_READWRITE
Definition: io.h:107
#define GetOpenFile
Definition: io.h:125
#define RB_WAITFD_OUT
Definition: io.h:41
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: io.c:1384
void rb_io_check_closed(rb_io_t *)
Definition: io.c:775
void rb_io_check_writable(rb_io_t *)
Definition: io.c:980
#define MakeOpenFile
Definition: io.h:130
#define FMODE_DUPLEX
Definition: io.h:111
int rb_io_read_pending(rb_io_t *)
Definition: io.c:992
#define RB_WAITFD_IN
Definition: io.h:39
VALUE rb_io_get_write_io(VALUE io)
Definition: io.c:802
void rb_io_set_nonblock(rb_io_t *fptr)
Definition: io.c:2942
void rb_io_synchronized(rb_io_t *)
Definition: io.c:6486
void rb_eof_error(void)
Definition: io.c:754
int rb_io_wait_readable(int fd)
Definition: io.c:1307
void rsock_raise_socket_error(const char *reason, int error)
Definition: init.c:39
void rsock_make_fd_nonblock(int fd)
Definition: init.c:590
VALUE rsock_sendto_blocking(void *data)
Definition: init.c:100
#define do_write_retry(code)
Definition: init.c:74
VALUE rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex, enum sock_recv_type from)
Definition: init.c:231
VALUE rb_eSocket
Definition: init.c:29
VALUE rb_cAddrinfo
Definition: init.c:27
VALUE rb_cIPSocket
Definition: init.c:18
int rsock_getfamily(rb_io_t *fptr)
Definition: init.c:720
VALUE rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:643
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:685
int rsock_socket(int domain, int type, int proto)
Definition: init.c:437
VALUE rb_cUDPSocket
Definition: init.c:21
VALUE rsock_init_sock(VALUE sock, int fd)
Definition: init.c:78
int rsock_do_not_reverse_lookup
Definition: init.c:35
VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
Definition: init.c:169
VALUE rsock_send_blocking(void *data)
Definition: init.c:111
VALUE rb_cTCPSocket
Definition: init.c:19
void rsock_init_socket_init(void)
Definition: init.c:752
int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks, struct timeval *timeout)
Definition: init.c:559
VALUE rb_cSocket
Definition: init.c:26
VALUE rb_cBasicSocket
Definition: init.c:17
VALUE rb_cTCPServer
Definition: init.c:20
#define NUM2INT
Definition: int.h:44
#define INT2NUM
Definition: int.h:43
#define rb_syserr_fail_path(err, path)
Definition: error.h:38
void rb_maygvl_fd_fix_cloexec(int fd)
Definition: io.c:260
int rb_gc_for_fd(int err)
Definition: io.c:1010
VALUE rb_str_locktmp_ensure(VALUE str, VALUE(*func)(VALUE), VALUE arg)
Definition: string.c:2835
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
Definition: thread.c:1815
#define is_socket(fd, path)
Definition: io.c:730
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
voidpf void * buf
Definition: ioapi.h:138
void rsock_init_ipsocket(void)
Definition: ipsocket.c:386
#define LONG2FIX
Definition: long.h:49
#define NUM2LONG
Definition: long.h:51
#define MEMMOVE(p1, p2, type, n)
Definition: memory.h:130
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
void rsock_init_sockopt(void)
Definition: option.c:1438
void rsock_init_addrinfo(void)
Definition: raddrinfo.c:2563
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
Definition: raddrinfo.c:555
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
Definition: raddrinfo.c:2537
#define StringValue(v)
Definition: rstring.h:50
int argc
Definition: ruby.c:240
char ** argv
Definition: ruby.c:241
void rsock_init_udpsocket(void)
Definition: udpsocket.c:230
void rsock_init_tcpserver(void)
Definition: tcpserver.c:106
#define FMODE_SOCK
Definition: rubysocket.h:263
#define EWOULDBLOCK
Definition: rubysocket.h:164
#define FMODE_INET
Definition: rubysocket.h:261
void rsock_init_sockssocket(void)
Definition: sockssocket.c:58
VALUE rsock_write_nonblock(VALUE sock, VALUE buf, VALUE ex)
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:88
#define MSG_DONTWAIT_RELIABLE
Definition: rubysocket.h:459
void rsock_init_unixsocket(void)
Definition: unixsocket.c:572
sock_recv_type
Definition: rubysocket.h:363
@ RECV_SOCKET
Definition: rubysocket.h:367
@ RECV_IP
Definition: rubysocket.h:365
@ RECV_RECV
Definition: rubysocket.h:364
@ RECV_UNIX
Definition: rubysocket.h:366
VALUE rsock_read_nonblock(VALUE sock, VALUE length, VALUE buf, VALUE ex)
#define BLOCKING_REGION_FD(func, arg)
Definition: rubysocket.h:290
#define FMODE_INET6
Definition: rubysocket.h:262
#define rsock_maybe_wait_fd(fd)
Definition: rubysocket.h:451
void rsock_init_unixserver(void)
Definition: unixserver.c:109
#define FMODE_NOREVLOOKUP
Definition: rubysocket.h:257
#define FMODE_UNIX
Definition: rubysocket.h:260
#define AF_UNSPEC
Definition: sockport.h:101
#define Qnil
#define Qfalse
#define NIL_P
VALUE rb_sprintf(const char *,...)
Definition: sprintf.c:1203
socklen_t * len
Definition: init.c:674
struct sockaddr * sockaddr
Definition: init.c:673
int fd
Definition: init.c:672
socklen_t len
Definition: init.c:538
int fd
Definition: init.c:537
const struct sockaddr * sockaddr
Definition: init.c:539
Definition: io.h:61
int fd
Definition: io.h:65
rb_io_buffer_t wbuf
Definition: io.h:72
VALUE pathv
Definition: io.h:69
int mode
Definition: io.h:66
rb_io_buffer_t rbuf
Definition: io.h:72
int fd
Definition: init.c:122
VALUE str
Definition: init.c:123
socklen_t alen
Definition: init.c:125
int flags
Definition: init.c:122
size_t length
Definition: init.c:124
union_sockaddr buf
Definition: init.c:126
socklen_t tolen
Definition: rubysocket.h:356
struct sockaddr * to
Definition: rubysocket.h:355
struct sockaddr addr
Definition: rubysocket.h:218
void error(const char *msg)
Definition: untgz.c:593
unsigned long VALUE
Definition: value.h:38
#define T_STRING
Definition: value_type.h:77
VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc)
Definition: win32.c:2266
#define stat
Definition: win32.h:195
#define EISCONN
Definition: win32.h:531
#define O_NONBLOCK
Definition: win32.h:584
#define ECONNABORTED
Definition: win32.h:522
#define EHOSTUNREACH
Definition: win32.h:556
#define ETIMEDOUT
Definition: win32.h:543
#define EINPROGRESS
Definition: win32.h:471
#define fstat(fd, st)
Definition: win32.h:202
#define EALREADY
Definition: win32.h:474
int fcntl(int, int,...)
Definition: win32.c:4338
#define ECONNREFUSED
Definition: win32.h:546
#define EPROTOTYPE
Definition: win32.h:486
#define F_SETFL
Definition: win32.h:581