Ruby: ext/socket/init.c Source File

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
14 VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc);
15#endif
16
17 VALUE rb_cBasicSocket;
18 VALUE rb_cIPSocket;
19 VALUE rb_cTCPSocket;
20 VALUE rb_cTCPServer;
21 VALUE rb_cUDPSocket;
22#ifdef AF_UNIX
23 VALUE rb_cUNIXSocket;
24 VALUE rb_cUNIXServer;
25#endif
26 VALUE rb_cSocket;
27 VALUE rb_cAddrinfo;
28
29 VALUE rb_eSocket;
30
31#ifdef SOCKS
32 VALUE rb_cSOCKSSocket;
33#endif
34
35 int rsock_do_not_reverse_lookup = 1;
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
47 rb_encoding *enc = rb_default_internal_encoding();
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));
51 rb_exc_raise(rb_exc_new_str(rb_eSocket, msg));
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
61 is_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
77 VALUE
78rsock_init_sock(VALUE sock, int fd)
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
86 rb_update_max_fd(fd);
87 MakeOpenFile(sock, fp);
88 fp->fd = fd;
89 fp->mode = FMODE_READWRITE|FMODE_DUPLEX;
90 rb_io_ascii8bit_binmode(sock);
91 if (rsock_do_not_reverse_lookup) {
92 fp->mode |= FMODE_NOREVLOOKUP;
93 }
94 rb_io_synchronized(fp);
95
96 return sock;
97}
98
99 VALUE
100rsock_sendto_blocking(void *data)
101{
102 struct rsock_send_arg *arg = data;
103 VALUE mesg = arg->mesg;
104 ssize_t ret;
105 do_write_retry(sendto(arg->fd, RSTRING_PTR(mesg), RSTRING_LEN(mesg),
106 arg->flags, arg->to, arg->tolen));
107 return (VALUE)ret;
108}
109
110 VALUE
111rsock_send_blocking(void *data)
112{
113 struct rsock_send_arg *arg = data;
114 VALUE mesg = arg->mesg;
115 ssize_t ret;
116 do_write_retry(send(arg->fd, RSTRING_PTR(mesg), RSTRING_LEN(mesg),
117 arg->flags));
118 return (VALUE)ret;
119}
120
121 struct recvfrom_arg {
122 int fd, flags;
123 VALUE str;
124 size_t length;
125 socklen_t alen;
126 union_sockaddr buf;
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
150 StringValue(str);
151 len = RSTRING_LEN(str);
152 if (len >= buflen) {
153 rb_str_modify(str);
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
168 VALUE
169rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
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),
195 rsock_maybe_wait_fd(arg.fd),
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
230 VALUE
231rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str,
232 VALUE ex, enum sock_recv_type from)
233{
234 rb_io_t *fptr;
235 union_sockaddr buf;
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
261 if (!MSG_DONTWAIT_RELIABLE)
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: */
322 VALUE
323 rsock_read_nonblock(VALUE sock, VALUE length, VALUE buf, VALUE ex)
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) {
334 rb_str_set_len(str, 0);
335 return str;
336 }
337
338 ptr = RSTRING_PTR(str);
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;
346 rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE,
347 e, "read would block");
348 }
349 rb_syserr_fail_path(e, fptr->pathv);
350 }
351 }
352 if (n != RSTRING_LEN(str)) {
353 rb_str_modify(str);
354 rb_str_set_len(str, n);
355 }
356 if (n == 0) {
357 if (ex == Qfalse) return Qnil;
358 rb_eof_error();
359 }
360
361 return str;
362}
363
364/* :nodoc: */
365 VALUE
366 rsock_write_nonblock(VALUE sock, VALUE str, VALUE ex)
367{
368 rb_io_t *fptr;
369 long n;
370
371 if (!RB_TYPE_P(str, T_STRING))
372 str = rb_obj_as_string(str);
373
374 sock = rb_io_get_write_io(sock);
375 GetOpenFile(sock, fptr);
376 rb_io_check_writable(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;
401 rb_readwrite_syserr_fail(RB_IO_WAIT_WRITABLE, e,
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
430 rsock_make_fd_nonblock(result);
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)
448 rb_update_max_fd(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 */
491 revents = rb_wait_for_single_fd(fd, RB_WAITFD_IN|RB_WAITFD_OUT, timeout);
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
536 struct connect_arg {
537 int fd;
538 socklen_t len;
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
590rsock_make_fd_nonblock(int fd)
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
628 rsock_make_fd_nonblock(result);
629#endif
630#else
631 int result = accept(socket, address, address_len);
632 if (result == -1) return -1;
633
634 rb_maygvl_fd_fix_cloexec(result);
635 rsock_make_fd_nonblock(result);
636#endif
637
638 if (address_len && len0 < *address_len) *address_len = len0;
639 return result;
640}
641
642 VALUE
643rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr,
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
671 struct accept_arg {
672 int fd;
673 struct sockaddr *sockaddr;
674 socklen_t *len;
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
684 VALUE
685rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
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:
695 rsock_maybe_wait_fd(fd);
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
720rsock_getfamily(rb_io_t *fptr)
721{
722 union_sockaddr ss;
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
752rsock_init_socket_init(void)
753{
754 /*
755 * SocketError is the error class for socket.
756 */
757 rb_eSocket = rb_define_class("SocketError", rb_eStandardError);
758 rsock_init_ipsocket();
759 rsock_init_tcpsocket();
760 rsock_init_tcpserver();
761 rsock_init_sockssocket();
762 rsock_init_udpsocket();
763 rsock_init_unixsocket();
764 rsock_init_unixserver();
765 rsock_init_sockopt();
766 rsock_init_ancdata();
767 rsock_init_addrinfo();
768 rsock_init_sockifaddr();
769 rsock_init_socket_constants();
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}
EAI_SYSTEM
#define EAI_SYSTEM
Definition: addrinfo.h:88
rsock_init_ancdata
void rsock_init_ancdata(void)
Definition: ancdata.c:1693
rb_assoc_new
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:975
rsock_init_socket_constants
void rsock_init_socket_constants(void)
Definition: constants.c:140
ptr
struct RIMemo * ptr
Definition: debug.c:88
rb_default_internal_encoding
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1734
len
uint8_t len
Definition: escape.c:17
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
rb_gc
void rb_gc(void)
Definition: gc.c:9497
gai_strerror
char * gai_strerror(int ecode)
Definition: getaddrinfo.c:207
socklen_t
int socklen_t
Definition: getaddrinfo.c:83
rb_eIOError
VALUE rb_eIOError
Definition: io.c:185
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
rb_syserr_fail
void rb_syserr_fail(int e, const char *mesg)
Definition: error.c:3029
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_exc_raise
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
rb_bug
void rb_bug(const char *fmt,...)
Definition: error.c:768
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:1054
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:1057
rb_exc_new_str
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Definition: error.c:1107
rb_sys_fail
void rb_sys_fail(const char *mesg)
Definition: error.c:3041
rb_obj_alloc
VALUE rb_obj_alloc(VALUE)
Allocates an instance of klass.
Definition: object.c:1900
rsock_init_sockifaddr
void rsock_init_sockifaddr(void)
Definition: ifaddr.c:454
rb_readwrite_syserr_fail
void rb_readwrite_syserr_fail(enum rb_io_wait_readwrite, int, const char *)
Definition: io.c:13122
RB_IO_WAIT_WRITABLE
#define RB_IO_WAIT_WRITABLE
Definition: error.h:42
RB_IO_WAIT_READABLE
#define RB_IO_WAIT_READABLE
Definition: error.h:41
rb_io_ascii8bit_binmode
VALUE rb_io_ascii8bit_binmode(VALUE)
Definition: io.c:5495
rb_update_max_fd
void rb_update_max_fd(int fd)
Definition: io.c:233
rb_io_flush
VALUE rb_io_flush(VALUE)
Definition: io.c:2052
rb_reserved_fd_p
int rb_reserved_fd_p(int fd)
rb_fd_fix_cloexec
void rb_fd_fix_cloexec(int fd)
Definition: io.c:283
rb_str_concat
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:3217
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2842
rb_str_modify
void rb_str_modify(VALUE)
Definition: string.c:2262
rb_str_modify_expand
void rb_str_modify_expand(VALUE, long)
Definition: string.c:2270
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1529
rb_blocking_function_t
VALUE rb_blocking_function_t(void *)
Definition: thread.h:60
ID2SYM
#define ID2SYM
Definition: symbol.h:44
rb_intern
ID rb_intern(const char *)
Definition: symbol.c:785
FMODE_READWRITE
#define FMODE_READWRITE
Definition: io.h:107
GetOpenFile
#define GetOpenFile
Definition: io.h:125
RB_WAITFD_OUT
#define RB_WAITFD_OUT
Definition: io.h:41
rb_wait_for_single_fd
int rb_wait_for_single_fd(int fd, int events, struct timeval *tv)
Definition: io.c:1384
rb_io_check_closed
void rb_io_check_closed(rb_io_t *)
Definition: io.c:775
rb_io_check_writable
void rb_io_check_writable(rb_io_t *)
Definition: io.c:980
MakeOpenFile
#define MakeOpenFile
Definition: io.h:130
FMODE_DUPLEX
#define FMODE_DUPLEX
Definition: io.h:111
rb_io_read_pending
int rb_io_read_pending(rb_io_t *)
Definition: io.c:992
RB_WAITFD_IN
#define RB_WAITFD_IN
Definition: io.h:39
rb_io_get_write_io
VALUE rb_io_get_write_io(VALUE io)
Definition: io.c:802
rb_io_set_nonblock
void rb_io_set_nonblock(rb_io_t *fptr)
Definition: io.c:2942
rb_io_synchronized
void rb_io_synchronized(rb_io_t *)
Definition: io.c:6486
rb_eof_error
void rb_eof_error(void)
Definition: io.c:754
rb_io_wait_readable
int rb_io_wait_readable(int fd)
Definition: io.c:1307
rsock_raise_socket_error
void rsock_raise_socket_error(const char *reason, int error)
Definition: init.c:39
rsock_make_fd_nonblock
void rsock_make_fd_nonblock(int fd)
Definition: init.c:590
rsock_sendto_blocking
VALUE rsock_sendto_blocking(void *data)
Definition: init.c:100
do_write_retry
#define do_write_retry(code)
Definition: init.c:74
rsock_s_recvfrom_nonblock
VALUE rsock_s_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex, enum sock_recv_type from)
Definition: init.c:231
rb_eSocket
VALUE rb_eSocket
Definition: init.c:29
rb_cAddrinfo
VALUE rb_cAddrinfo
Definition: init.c:27
rb_cIPSocket
VALUE rb_cIPSocket
Definition: init.c:18
rsock_getfamily
int rsock_getfamily(rb_io_t *fptr)
Definition: init.c:720
rsock_s_accept_nonblock
VALUE rsock_s_accept_nonblock(VALUE klass, VALUE ex, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:643
rsock_s_accept
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
Definition: init.c:685
rsock_socket
int rsock_socket(int domain, int type, int proto)
Definition: init.c:437
rb_cUDPSocket
VALUE rb_cUDPSocket
Definition: init.c:21
rsock_init_sock
VALUE rsock_init_sock(VALUE sock, int fd)
Definition: init.c:78
rsock_do_not_reverse_lookup
int rsock_do_not_reverse_lookup
Definition: init.c:35
rsock_s_recvfrom
VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from)
Definition: init.c:169
rsock_send_blocking
VALUE rsock_send_blocking(void *data)
Definition: init.c:111
rb_cTCPSocket
VALUE rb_cTCPSocket
Definition: init.c:19
rsock_init_socket_init
void rsock_init_socket_init(void)
Definition: init.c:752
rsock_connect
int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks, struct timeval *timeout)
Definition: init.c:559
rb_cSocket
VALUE rb_cSocket
Definition: init.c:26
rb_cBasicSocket
VALUE rb_cBasicSocket
Definition: init.c:17
rb_cTCPServer
VALUE rb_cTCPServer
Definition: init.c:20
NUM2INT
#define NUM2INT
Definition: int.h:44
INT2NUM
#define INT2NUM
Definition: int.h:43
rb_syserr_fail_path
#define rb_syserr_fail_path(err, path)
Definition: error.h:38
rb_maygvl_fd_fix_cloexec
void rb_maygvl_fd_fix_cloexec(int fd)
Definition: io.c:260
rb_gc_for_fd
int rb_gc_for_fd(int err)
Definition: io.c:1010
rb_str_locktmp_ensure
VALUE rb_str_locktmp_ensure(VALUE str, VALUE(*func)(VALUE), VALUE arg)
Definition: string.c:2835
rb_thread_io_blocking_region
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
Definition: thread.c:1815
is_socket
#define is_socket(fd, path)
Definition: io.c:730
long
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
int
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
buf
voidpf void * buf
Definition: ioapi.h:138
rsock_init_ipsocket
void rsock_init_ipsocket(void)
Definition: ipsocket.c:386
LONG2FIX
#define LONG2FIX
Definition: long.h:49
NUM2LONG
#define NUM2LONG
Definition: long.h:51
MEMMOVE
#define MEMMOVE(p1, p2, type, n)
Definition: memory.h:130
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
rsock_init_sockopt
void rsock_init_sockopt(void)
Definition: option.c:1438
rsock_init_addrinfo
void rsock_init_addrinfo(void)
Definition: raddrinfo.c:2563
rsock_ipaddr
VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
Definition: raddrinfo.c:555
rsock_io_socket_addrinfo
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len)
Definition: raddrinfo.c:2537
StringValue
#define StringValue(v)
Definition: rstring.h:50
argc
int argc
Definition: ruby.c:240
argv
char ** argv
Definition: ruby.c:241
rubysocket.h
rsock_init_udpsocket
void rsock_init_udpsocket(void)
Definition: udpsocket.c:230
rsock_init_tcpserver
void rsock_init_tcpserver(void)
Definition: tcpserver.c:106
FMODE_SOCK
#define FMODE_SOCK
Definition: rubysocket.h:263
EWOULDBLOCK
#define EWOULDBLOCK
Definition: rubysocket.h:164
FMODE_INET
#define FMODE_INET
Definition: rubysocket.h:261
rsock_init_sockssocket
void rsock_init_sockssocket(void)
Definition: sockssocket.c:58
rsock_write_nonblock
VALUE rsock_write_nonblock(VALUE sock, VALUE buf, VALUE ex)
rsock_init_tcpsocket
void rsock_init_tcpsocket(void)
Definition: tcpsocket.c:88
MSG_DONTWAIT_RELIABLE
#define MSG_DONTWAIT_RELIABLE
Definition: rubysocket.h:459
rsock_init_unixsocket
void rsock_init_unixsocket(void)
Definition: unixsocket.c:572
sock_recv_type
sock_recv_type
Definition: rubysocket.h:363
RECV_SOCKET
@ RECV_SOCKET
Definition: rubysocket.h:367
RECV_IP
@ RECV_IP
Definition: rubysocket.h:365
RECV_RECV
@ RECV_RECV
Definition: rubysocket.h:364
RECV_UNIX
@ RECV_UNIX
Definition: rubysocket.h:366
rsock_read_nonblock
VALUE rsock_read_nonblock(VALUE sock, VALUE length, VALUE buf, VALUE ex)
BLOCKING_REGION_FD
#define BLOCKING_REGION_FD(func, arg)
Definition: rubysocket.h:290
FMODE_INET6
#define FMODE_INET6
Definition: rubysocket.h:262
rsock_maybe_wait_fd
#define rsock_maybe_wait_fd(fd)
Definition: rubysocket.h:451
rsock_init_unixserver
void rsock_init_unixserver(void)
Definition: unixserver.c:109
FMODE_NOREVLOOKUP
#define FMODE_NOREVLOOKUP
Definition: rubysocket.h:257
FMODE_UNIX
#define FMODE_UNIX
Definition: rubysocket.h:260
AF_UNSPEC
#define AF_UNSPEC
Definition: sockport.h:101
Qnil
#define Qnil
Definition: special_consts.h:51
Qfalse
#define Qfalse
Definition: special_consts.h:50
NIL_P
#define NIL_P
Definition: special_consts.h:46
rb_sprintf
VALUE rb_sprintf(const char *,...)
Definition: sprintf.c:1203
OnigEncodingTypeST
Definition: onigmo.h:160
accept_arg
Definition: init.c:671
accept_arg::len
socklen_t * len
Definition: init.c:674
accept_arg::sockaddr
struct sockaddr * sockaddr
Definition: init.c:673
accept_arg::fd
int fd
Definition: init.c:672
connect_arg
Definition: init.c:536
connect_arg::len
socklen_t len
Definition: init.c:538
connect_arg::fd
int fd
Definition: init.c:537
connect_arg::sockaddr
const struct sockaddr * sockaddr
Definition: init.c:539
rb_io_t
Definition: io.h:61
rb_io_t::fd
int fd
Definition: io.h:65
rb_io_t::wbuf
rb_io_buffer_t wbuf
Definition: io.h:72
rb_io_t::pathv
VALUE pathv
Definition: io.h:69
rb_io_t::mode
int mode
Definition: io.h:66
rb_io_t::rbuf
rb_io_buffer_t rbuf
Definition: io.h:72
recvfrom_arg
Definition: init.c:121
recvfrom_arg::fd
int fd
Definition: init.c:122
recvfrom_arg::str
VALUE str
Definition: init.c:123
recvfrom_arg::alen
socklen_t alen
Definition: init.c:125
recvfrom_arg::flags
int flags
Definition: init.c:122
recvfrom_arg::length
size_t length
Definition: init.c:124
recvfrom_arg::buf
union_sockaddr buf
Definition: init.c:126
rsock_send_arg
Definition: rubysocket.h:352
rsock_send_arg::tolen
socklen_t tolen
Definition: rubysocket.h:356
rsock_send_arg::flags
int flags
Definition: rubysocket.h:353
rsock_send_arg::fd
int fd
Definition: rubysocket.h:353
rsock_send_arg::mesg
VALUE mesg
Definition: rubysocket.h:354
rsock_send_arg::to
struct sockaddr * to
Definition: rubysocket.h:355
timeval
Definition: missing.h:51
union_sockaddr
Definition: rubysocket.h:217
union_sockaddr::addr
struct sockaddr addr
Definition: rubysocket.h:218
error
void error(const char *msg)
Definition: untgz.c:593
VALUE
unsigned long VALUE
Definition: value.h:38
T_STRING
#define T_STRING
Definition: value_type.h:77
rb_w32_conv_from_wchar
VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc)
Definition: win32.c:2266
stat
#define stat
Definition: win32.h:195
EISCONN
#define EISCONN
Definition: win32.h:531
O_NONBLOCK
#define O_NONBLOCK
Definition: win32.h:584
ECONNABORTED
#define ECONNABORTED
Definition: win32.h:522
EHOSTUNREACH
#define EHOSTUNREACH
Definition: win32.h:556
ETIMEDOUT
#define ETIMEDOUT
Definition: win32.h:543
EINPROGRESS
#define EINPROGRESS
Definition: win32.h:471
fstat
#define fstat(fd, st)
Definition: win32.h:202
EALREADY
#define EALREADY
Definition: win32.h:474
fcntl
int fcntl(int, int,...)
Definition: win32.c:4338
ECONNREFUSED
#define ECONNREFUSED
Definition: win32.h:546
EPROTOTYPE
#define EPROTOTYPE
Definition: win32.h:486
F_SETFL
#define F_SETFL
Definition: win32.h:581