2 ** socket.c - Socket module
4 ** See Copyright Notice in mruby.h
8 #define _WIN32_WINNT 0x0501
15 #define SHUT_RDWR SD_BOTH
16 #ifndef _SSIZE_T_DEFINED
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/param.h>
25 #include <netinet/in.h>
26 #include <netinet/tcp.h>
27 #include <arpa/inet.h>
31 typedef size_t fsize_t;
38 #include "mruby/array.h"
39 #include "mruby/class.h"
40 #include "mruby/data.h"
41 #include "mruby/numeric.h"
42 #include "mruby/string.h"
43 #include "mruby/variable.h"
44 #include "mruby/error.h"
46 #include "mruby/ext/io.h"
48 #if !defined(HAVE_SA_LEN)
49 #if (defined(BSD) && (BSD >= 199006))
56 #define E_SOCKET_ERROR (mrb_class_get(mrb, "SocketError"))
58 #if !defined(mrb_cptr)
59 #define mrb_cptr_value(m,p) mrb_voidp_value((m),(p))
60 #define mrb_cptr(o) mrb_voidp(o)
61 #define mrb_cptr_p(o) mrb_voidp_p(o)
65 static const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
69 struct sockaddr_in in;
70 memset(&in, 0, sizeof(in));
71 in.sin_family = AF_INET;
72 memcpy(&in.sin_addr, src, sizeof(struct in_addr));
73 getnameinfo((struct sockaddr *)&in, sizeof(struct
74 sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
77 else if (af == AF_INET6)
79 struct sockaddr_in6 in;
80 memset(&in, 0, sizeof(in));
81 in.sin6_family = AF_INET6;
82 memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
83 getnameinfo((struct sockaddr *)&in, sizeof(struct
84 sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
90 static int inet_pton(int af, const char *src, void *dst)
92 struct addrinfo hints, *res, *ressave;
94 memset(&hints, 0, sizeof(struct addrinfo));
97 if (getaddrinfo(src, NULL, &hints, &res) != 0)
99 printf("Couldn't resolve host %s\n", src);
107 memcpy(dst, res->ai_addr, res->ai_addrlen);
111 freeaddrinfo(ressave);
118 mrb_addrinfo_getaddrinfo(mrb_state *mrb, mrb_value klass)
120 struct addrinfo hints, *res0, *res;
121 mrb_value ai, ary, family, lastai, nodename, protocol, sa, service, socktype;
123 int arena_idx, error;
124 const char *hostname = NULL, *servname = NULL;
126 ary = mrb_ary_new(mrb);
127 arena_idx = mrb_gc_arena_save(mrb); /* ary must be on arena! */
129 family = socktype = protocol = mrb_nil_value();
131 mrb_get_args(mrb, "oo|oooi", &nodename, &service, &family, &socktype, &protocol, &flags);
133 if (mrb_string_p(nodename)) {
134 hostname = RSTRING_CSTR(mrb, nodename);
135 } else if (mrb_nil_p(nodename)) {
138 mrb_raise(mrb, E_TYPE_ERROR, "nodename must be String or nil");
141 if (mrb_string_p(service)) {
142 servname = RSTRING_CSTR(mrb, service);
143 } else if (mrb_fixnum_p(service)) {
144 servname = RSTRING_PTR(mrb_fixnum_to_str(mrb, service, 10));
145 } else if (mrb_nil_p(service)) {
148 mrb_raise(mrb, E_TYPE_ERROR, "service must be String, Fixnum, or nil");
151 memset(&hints, 0, sizeof(hints));
152 hints.ai_flags = (int)flags;
154 if (mrb_fixnum_p(family)) {
155 hints.ai_family = (int)mrb_fixnum(family);
158 if (mrb_fixnum_p(socktype)) {
159 hints.ai_socktype = (int)mrb_fixnum(socktype);
162 if (mrb_fixnum_p(protocol)) {
163 hints.ai_protocol = (int)mrb_fixnum(protocol);
166 lastai = mrb_cv_get(mrb, klass, mrb_intern_lit(mrb, "_lastai"));
167 if (mrb_cptr_p(lastai)) {
168 freeaddrinfo((struct addrinfo*)mrb_cptr(lastai));
169 mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value());
172 error = getaddrinfo(hostname, servname, &hints, &res0);
174 mrb_raisef(mrb, E_SOCKET_ERROR, "getaddrinfo: %s", gai_strerror(error));
176 mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_cptr_value(mrb, res0));
178 for (res = res0; res != NULL; res = res->ai_next) {
179 sa = mrb_str_new(mrb, (char*)res->ai_addr, res->ai_addrlen);
180 ai = mrb_funcall(mrb, klass, "new", 4, sa, mrb_fixnum_value(res->ai_family), mrb_fixnum_value(res->ai_socktype), mrb_fixnum_value(res->ai_protocol));
181 mrb_ary_push(mrb, ary, ai);
182 mrb_gc_arena_restore(mrb, arena_idx);
186 mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value());
192 mrb_addrinfo_getnameinfo(mrb_state *mrb, mrb_value self)
195 mrb_value ary, host, sastr, serv;
199 mrb_get_args(mrb, "|i", &flags);
200 host = mrb_str_buf_new(mrb, NI_MAXHOST);
201 serv = mrb_str_buf_new(mrb, NI_MAXSERV);
203 sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr"));
204 if (!mrb_string_p(sastr)) {
205 mrb_raise(mrb, E_SOCKET_ERROR, "invalid sockaddr");
207 error = getnameinfo((struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr), RSTRING_PTR(host), NI_MAXHOST, RSTRING_PTR(serv), NI_MAXSERV, (int)flags);
209 mrb_raisef(mrb, E_SOCKET_ERROR, "getnameinfo: %s", gai_strerror(error));
211 ary = mrb_ary_new_capa(mrb, 2);
212 mrb_str_resize(mrb, host, strlen(RSTRING_PTR(host)));
213 mrb_ary_push(mrb, ary, host);
214 mrb_str_resize(mrb, serv, strlen(RSTRING_PTR(serv)));
215 mrb_ary_push(mrb, ary, serv);
221 mrb_addrinfo_unix_path(mrb_state *mrb, mrb_value self)
225 sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr"));
226 if (((struct sockaddr *)RSTRING_PTR(sastr))->sa_family != AF_UNIX)
227 mrb_raise(mrb, E_SOCKET_ERROR, "need AF_UNIX address");
228 if (RSTRING_LEN(sastr) < (mrb_int)offsetof(struct sockaddr_un, sun_path) + 1) {
229 return mrb_str_new(mrb, "", 0);
231 return mrb_str_new_cstr(mrb, ((struct sockaddr_un *)RSTRING_PTR(sastr))->sun_path);
237 sa2addrlist(mrb_state *mrb, const struct sockaddr *sa, socklen_t salen)
243 switch (sa->sa_family) {
246 port = ((struct sockaddr_in *)sa)->sin_port;
250 port = ((struct sockaddr_in6 *)sa)->sin6_port;
253 mrb_raise(mrb, E_ARGUMENT_ERROR, "bad af");
254 return mrb_nil_value();
257 host = mrb_str_buf_new(mrb, NI_MAXHOST);
258 if (getnameinfo(sa, salen, RSTRING_PTR(host), NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == -1)
259 mrb_sys_fail(mrb, "getnameinfo");
260 mrb_str_resize(mrb, host, strlen(RSTRING_PTR(host)));
261 ary = mrb_ary_new_capa(mrb, 4);
262 mrb_ary_push(mrb, ary, mrb_str_new_cstr(mrb, afstr));
263 mrb_ary_push(mrb, ary, mrb_fixnum_value(port));
264 mrb_ary_push(mrb, ary, host);
265 mrb_ary_push(mrb, ary, host);
270 socket_fd(mrb_state *mrb, mrb_value sock)
272 return (int)mrb_fixnum(mrb_funcall(mrb, sock, "fileno", 0));
278 struct sockaddr_storage ss;
282 if (getsockname(s, (struct sockaddr *)&ss, &salen) == -1)
288 mrb_basicsocket_getpeereid(mrb_state *mrb, mrb_value self)
290 #ifdef HAVE_GETPEEREID
296 s = socket_fd(mrb, self);
297 if (getpeereid(s, &euid, &egid) != 0)
298 mrb_sys_fail(mrb, "getpeereid");
300 ary = mrb_ary_new_capa(mrb, 2);
301 mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int)euid));
302 mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int)egid));
305 mrb_raise(mrb, E_RUNTIME_ERROR, "getpeereid is not available on this system");
306 return mrb_nil_value();
311 mrb_basicsocket_getpeername(mrb_state *mrb, mrb_value self)
313 struct sockaddr_storage ss;
317 if (getpeername(socket_fd(mrb, self), (struct sockaddr *)&ss, &salen) != 0)
318 mrb_sys_fail(mrb, "getpeername");
320 return mrb_str_new(mrb, (char*)&ss, salen);
324 mrb_basicsocket_getsockname(mrb_state *mrb, mrb_value self)
326 struct sockaddr_storage ss;
330 if (getsockname(socket_fd(mrb, self), (struct sockaddr *)&ss, &salen) != 0)
331 mrb_sys_fail(mrb, "getsockname");
333 return mrb_str_new(mrb, (char*)&ss, salen);
337 mrb_basicsocket_getsockopt(mrb_state *mrb, mrb_value self)
341 mrb_int family, level, optname;
345 mrb_get_args(mrb, "ii", &level, &optname);
346 s = socket_fd(mrb, self);
347 optlen = sizeof(opt);
348 if (getsockopt(s, (int)level, (int)optname, opt, &optlen) == -1)
349 mrb_sys_fail(mrb, "getsockopt");
350 c = mrb_const_get(mrb, mrb_obj_value(mrb_class_get(mrb, "Socket")), mrb_intern_lit(mrb, "Option"));
351 family = socket_family(s);
352 data = mrb_str_new(mrb, opt, optlen);
353 return mrb_funcall(mrb, c, "new", 4, mrb_fixnum_value(family), mrb_fixnum_value(level), mrb_fixnum_value(optname), data);
357 mrb_basicsocket_recv(mrb_state *mrb, mrb_value self)
360 mrb_int maxlen, flags = 0;
363 mrb_get_args(mrb, "i|i", &maxlen, &flags);
364 buf = mrb_str_buf_new(mrb, maxlen);
365 n = recv(socket_fd(mrb, self), RSTRING_PTR(buf), (fsize_t)maxlen, (int)flags);
367 mrb_sys_fail(mrb, "recv");
368 mrb_str_resize(mrb, buf, (mrb_int)n);
373 mrb_basicsocket_recvfrom(mrb_state *mrb, mrb_value self)
376 mrb_int maxlen, flags = 0;
377 mrb_value ary, buf, sa;
380 mrb_get_args(mrb, "i|i", &maxlen, &flags);
381 buf = mrb_str_buf_new(mrb, maxlen);
382 socklen = sizeof(struct sockaddr_storage);
383 sa = mrb_str_buf_new(mrb, socklen);
384 n = recvfrom(socket_fd(mrb, self), RSTRING_PTR(buf), (fsize_t)maxlen, (int)flags, (struct sockaddr *)RSTRING_PTR(sa), &socklen);
386 mrb_sys_fail(mrb, "recvfrom");
387 mrb_str_resize(mrb, buf, (mrb_int)n);
388 mrb_str_resize(mrb, sa, (mrb_int)socklen);
389 ary = mrb_ary_new_capa(mrb, 2);
390 mrb_ary_push(mrb, ary, buf);
391 mrb_ary_push(mrb, ary, sa);
396 mrb_basicsocket_send(mrb_state *mrb, mrb_value self)
400 mrb_value dest, mesg;
402 dest = mrb_nil_value();
403 mrb_get_args(mrb, "Si|S", &mesg, &flags, &dest);
404 if (mrb_nil_p(dest)) {
405 n = send(socket_fd(mrb, self), RSTRING_PTR(mesg), (fsize_t)RSTRING_LEN(mesg), (int)flags);
407 n = sendto(socket_fd(mrb, self), RSTRING_PTR(mesg), (fsize_t)RSTRING_LEN(mesg), (int)flags, (const struct sockaddr*)RSTRING_PTR(dest), (fsize_t)RSTRING_LEN(dest));
410 mrb_sys_fail(mrb, "send");
411 return mrb_fixnum_value((mrb_int)n);
415 mrb_basicsocket_setnonblock(mrb_state *mrb, mrb_value self)
418 mrb_bool nonblocking;
423 mrb_get_args(mrb, "b", &nonblocking);
424 fd = socket_fd(mrb, self);
426 flags = ioctlsocket(fd, FIONBIO, &mode);
427 if (flags != NO_ERROR)
428 mrb_sys_fail(mrb, "ioctlsocket");
430 flags = fcntl(fd, F_GETFL, 0);
432 mrb_sys_fail(mrb, "fcntl");
436 flags &= ~O_NONBLOCK;
437 if (fcntl(fd, F_SETFL, flags) == -1)
438 mrb_sys_fail(mrb, "fcntl");
440 return mrb_nil_value();
444 mrb_basicsocket_setsockopt(mrb_state *mrb, mrb_value self)
447 mrb_int argc, level = 0, optname;
448 mrb_value optval, so;
450 argc = mrb_get_args(mrb, "o|io", &so, &optname, &optval);
452 if (!mrb_fixnum_p(so)) {
453 mrb_raise(mrb, E_ARGUMENT_ERROR, "level is not an integer");
455 level = mrb_fixnum(so);
456 if (mrb_string_p(optval)) {
458 } else if (mrb_true_p(optval) || mrb_false_p(optval)) {
459 mrb_int i = mrb_test(optval) ? 1 : 0;
460 optval = mrb_str_new(mrb, (char*)&i, sizeof(i));
461 } else if (mrb_fixnum_p(optval)) {
462 if (optname == IP_MULTICAST_TTL || optname == IP_MULTICAST_LOOP) {
463 char uc = (char)mrb_fixnum(optval);
464 optval = mrb_str_new(mrb, &uc, sizeof(uc));
466 mrb_int i = mrb_fixnum(optval);
467 optval = mrb_str_new(mrb, (char*)&i, sizeof(i));
470 mrb_raise(mrb, E_ARGUMENT_ERROR, "optval should be true, false, an integer, or a string");
472 } else if (argc == 1) {
473 if (strcmp(mrb_obj_classname(mrb, so), "Socket::Option") != 0)
474 mrb_raise(mrb, E_ARGUMENT_ERROR, "not an instance of Socket::Option");
475 level = mrb_fixnum(mrb_funcall(mrb, so, "level", 0));
476 optname = mrb_fixnum(mrb_funcall(mrb, so, "optname", 0));
477 optval = mrb_funcall(mrb, so, "data", 0);
479 mrb_argnum_error(mrb, argc, 3, 3);
482 s = socket_fd(mrb, self);
483 if (setsockopt(s, (int)level, (int)optname, RSTRING_PTR(optval), (socklen_t)RSTRING_LEN(optval)) == -1)
484 mrb_sys_fail(mrb, "setsockopt");
485 return mrb_fixnum_value(0);
489 mrb_basicsocket_shutdown(mrb_state *mrb, mrb_value self)
491 mrb_int how = SHUT_RDWR;
493 mrb_get_args(mrb, "|i", &how);
494 if (shutdown(socket_fd(mrb, self), (int)how) != 0)
495 mrb_sys_fail(mrb, "shutdown");
496 return mrb_fixnum_value(0);
500 mrb_basicsocket_set_is_socket(mrb_state *mrb, mrb_value self)
504 mrb_get_args(mrb, "b", &b);
506 io_p = (struct mrb_io*)DATA_PTR(self);
511 return mrb_bool_value(b);
515 mrb_ipsocket_ntop(mrb_state *mrb, mrb_value klass)
520 mrb_get_args(mrb, "is", &af, &addr, &n);
521 if ((af == AF_INET && n != 4) || (af == AF_INET6 && n != 16))
522 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address");
523 if (inet_ntop((int)af, addr, buf, sizeof(buf)) == NULL)
524 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address");
525 return mrb_str_new_cstr(mrb, buf);
529 mrb_ipsocket_pton(mrb_state *mrb, mrb_value klass)
534 mrb_get_args(mrb, "is", &af, &bp, &n);
535 if ((size_t)n > sizeof(buf) - 1)
536 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address");
542 if (inet_pton(AF_INET, buf, (void *)&in.s_addr) != 1)
544 return mrb_str_new(mrb, (char*)&in.s_addr, 4);
545 } else if (af == AF_INET6) {
547 if (inet_pton(AF_INET6, buf, (void *)&in6.s6_addr) != 1)
549 return mrb_str_new(mrb, (char*)&in6.s6_addr, 16);
551 mrb_raise(mrb, E_ARGUMENT_ERROR, "unsupported address family");
554 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address");
555 return mrb_nil_value(); /* dummy */
559 mrb_ipsocket_recvfrom(mrb_state *mrb, mrb_value self)
561 struct sockaddr_storage ss;
563 mrb_value a, buf, pair;
564 mrb_int flags, maxlen;
568 fd = socket_fd(mrb, self);
570 mrb_get_args(mrb, "i|i", &maxlen, &flags);
571 buf = mrb_str_buf_new(mrb, maxlen);
572 socklen = sizeof(ss);
573 n = recvfrom(fd, RSTRING_PTR(buf), (fsize_t)maxlen, (int)flags,
574 (struct sockaddr *)&ss, &socklen);
576 mrb_sys_fail(mrb, "recvfrom");
578 mrb_str_resize(mrb, buf, (mrb_int)n);
579 a = sa2addrlist(mrb, (struct sockaddr *)&ss, socklen);
580 pair = mrb_ary_new_capa(mrb, 2);
581 mrb_ary_push(mrb, pair, buf);
582 mrb_ary_push(mrb, pair, a);
587 mrb_socket_gethostname(mrb_state *mrb, mrb_value cls)
593 bufsize = HOST_NAME_MAX + 1;
597 buf = mrb_str_buf_new(mrb, (mrb_int)bufsize);
598 if (gethostname(RSTRING_PTR(buf), (fsize_t)bufsize) != 0)
599 mrb_sys_fail(mrb, "gethostname");
600 mrb_str_resize(mrb, buf, (mrb_int)strlen(RSTRING_PTR(buf)));
605 mrb_socket_accept(mrb_state *mrb, mrb_value klass)
610 mrb_get_args(mrb, "i", &s0);
611 s1 = (int)accept(s0, NULL, NULL);
613 mrb_sys_fail(mrb, "accept");
615 return mrb_fixnum_value(s1);
619 mrb_socket_accept2(mrb_state *mrb, mrb_value klass)
621 mrb_value ary, sastr;
626 mrb_get_args(mrb, "i", &s0);
627 socklen = sizeof(struct sockaddr_storage);
628 sastr = mrb_str_buf_new(mrb, socklen);
629 s1 = (int)accept(s0, (struct sockaddr *)RSTRING_PTR(sastr), &socklen);
631 mrb_sys_fail(mrb, "accept");
633 // XXX: possible descriptor leakage here!
634 mrb_str_resize(mrb, sastr, socklen);
635 ary = mrb_ary_new_capa(mrb, 2);
636 mrb_ary_push(mrb, ary, mrb_fixnum_value(s1));
637 mrb_ary_push(mrb, ary, sastr);
642 mrb_socket_bind(mrb_state *mrb, mrb_value klass)
647 mrb_get_args(mrb, "iS", &s, &sastr);
648 if (bind((int)s, (struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr)) == -1) {
649 mrb_sys_fail(mrb, "bind");
651 return mrb_nil_value();
655 mrb_socket_connect(mrb_state *mrb, mrb_value klass)
660 mrb_get_args(mrb, "iS", &s, &sastr);
661 if (connect((int)s, (struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr)) == -1) {
662 mrb_sys_fail(mrb, "connect");
664 return mrb_nil_value();
668 mrb_socket_listen(mrb_state *mrb, mrb_value klass)
672 mrb_get_args(mrb, "ii", &s, &backlog);
673 if (listen((int)s, (int)backlog) == -1) {
674 mrb_sys_fail(mrb, "listen");
676 return mrb_nil_value();
680 mrb_socket_sockaddr_family(mrb_state *mrb, mrb_value klass)
682 const struct sockaddr *sa;
685 mrb_get_args(mrb, "S", &str);
686 if ((size_t)RSTRING_LEN(str) < offsetof(struct sockaddr, sa_family) + sizeof(sa->sa_family)) {
687 mrb_raise(mrb, E_SOCKET_ERROR, "invalid sockaddr (too short)");
689 sa = (const struct sockaddr *)RSTRING_PTR(str);
690 return mrb_fixnum_value(sa->sa_family);
694 mrb_socket_sockaddr_un(mrb_state *mrb, mrb_value klass)
697 mrb_raise(mrb, E_NOTIMP_ERROR, "sockaddr_un unsupported on Windows");
698 return mrb_nil_value();
700 struct sockaddr_un *sunp;
703 mrb_get_args(mrb, "S", &path);
704 if ((size_t)RSTRING_LEN(path) > sizeof(sunp->sun_path) - 1) {
705 mrb_raisef(mrb, E_ARGUMENT_ERROR, "too long unix socket path (max: %d bytes)", (int)sizeof(sunp->sun_path) - 1);
707 s = mrb_str_buf_new(mrb, sizeof(struct sockaddr_un));
708 sunp = (struct sockaddr_un *)RSTRING_PTR(s);
710 sunp->sun_len = sizeof(struct sockaddr_un);
712 sunp->sun_family = AF_UNIX;
713 memcpy(sunp->sun_path, RSTRING_PTR(path), RSTRING_LEN(path));
714 sunp->sun_path[RSTRING_LEN(path)] = '\0';
715 mrb_str_resize(mrb, s, sizeof(struct sockaddr_un));
721 mrb_socket_socketpair(mrb_state *mrb, mrb_value klass)
724 mrb_raise(mrb, E_NOTIMP_ERROR, "socketpair unsupported on Windows");
725 return mrb_nil_value();
728 mrb_int domain, type, protocol;
731 mrb_get_args(mrb, "iii", &domain, &type, &protocol);
732 if (socketpair(domain, type, protocol, sv) == -1) {
733 mrb_sys_fail(mrb, "socketpair");
735 // XXX: possible descriptor leakage here!
736 ary = mrb_ary_new_capa(mrb, 2);
737 mrb_ary_push(mrb, ary, mrb_fixnum_value(sv[0]));
738 mrb_ary_push(mrb, ary, mrb_fixnum_value(sv[1]));
744 mrb_socket_socket(mrb_state *mrb, mrb_value klass)
746 mrb_int domain, type, protocol;
749 mrb_get_args(mrb, "iii", &domain, &type, &protocol);
750 s = (int)socket((int)domain, (int)type, (int)protocol);
752 mrb_sys_fail(mrb, "socket");
753 return mrb_fixnum_value(s);
757 mrb_tcpsocket_allocate(mrb_state *mrb, mrb_value klass)
759 struct RClass *c = mrb_class_ptr(klass);
760 enum mrb_vtype ttype = MRB_INSTANCE_TT(c);
762 /* copied from mrb_instance_alloc() */
763 if (ttype == 0) ttype = MRB_TT_OBJECT;
764 return mrb_obj_value((struct RObject*)mrb_obj_alloc(mrb, ttype, c));
767 /* Windows overrides for IO methods on BasicSocket objects.
768 * This is because sockets on Windows are not the same as file
769 * descriptors, and thus functions which operate on file descriptors
770 * will break on socket descriptors.
774 mrb_win32_basicsocket_close(mrb_state *mrb, mrb_value self)
776 if (closesocket(socket_fd(mrb, self)) != NO_ERROR)
777 mrb_raise(mrb, E_SOCKET_ERROR, "closesocket unsuccessful");
778 return mrb_nil_value();
781 #define E_EOF_ERROR (mrb_class_get(mrb, "EOFError"))
783 mrb_win32_basicsocket_sysread(mrb_state *mrb, mrb_value self)
786 mrb_value buf = mrb_nil_value();
789 mrb_get_args(mrb, "i|S", &maxlen, &buf);
791 return mrb_nil_value();
794 if (mrb_nil_p(buf)) {
795 buf = mrb_str_new(mrb, NULL, maxlen);
797 if (RSTRING_LEN(buf) != maxlen) {
798 buf = mrb_str_resize(mrb, buf, maxlen);
801 sd = socket_fd(mrb, self);
802 ret = recv(sd, RSTRING_PTR(buf), (int)maxlen, 0);
807 buf = mrb_str_new_cstr(mrb, "");
809 mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File");
812 case SOCKET_ERROR: /* Error */
813 mrb_sys_fail(mrb, "recv");
816 if (RSTRING_LEN(buf) != ret) {
817 buf = mrb_str_resize(mrb, buf, ret);
826 mrb_win32_basicsocket_sysseek(mrb_state *mrb, mrb_value self)
828 mrb_raise(mrb, E_NOTIMP_ERROR, "sysseek not implemented for windows sockets");
829 return mrb_nil_value();
833 mrb_win32_basicsocket_syswrite(mrb_state *mrb, mrb_value self)
839 sd = socket_fd(mrb, self);
840 mrb_get_args(mrb, "S", &str);
841 n = send(sd, RSTRING_PTR(str), (int)RSTRING_LEN(str), 0);
842 if (n == SOCKET_ERROR)
843 mrb_sys_fail(mrb, "send");
844 return mrb_fixnum_value(n);
850 mrb_mruby_socket_gem_init(mrb_state* mrb)
852 struct RClass *io, *ai, *sock, *bsock, *ipsock, *tcpsock;
853 struct RClass *constants;
858 result = WSAStartup(MAKEWORD(2,2), &wsaData);
859 if (result != NO_ERROR)
860 mrb_raise(mrb, E_RUNTIME_ERROR, "WSAStartup failed");
863 ai = mrb_define_class(mrb, "Addrinfo", mrb->object_class);
864 mrb_mod_cv_set(mrb, ai, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value());
865 mrb_define_class_method(mrb, ai, "getaddrinfo", mrb_addrinfo_getaddrinfo, MRB_ARGS_REQ(2)|MRB_ARGS_OPT(4));
866 mrb_define_method(mrb, ai, "getnameinfo", mrb_addrinfo_getnameinfo, MRB_ARGS_OPT(1));
868 mrb_define_method(mrb, ai, "unix_path", mrb_addrinfo_unix_path, MRB_ARGS_NONE());
871 io = mrb_class_get(mrb, "IO");
873 bsock = mrb_define_class(mrb, "BasicSocket", io);
874 mrb_define_method(mrb, bsock, "_recvfrom", mrb_basicsocket_recvfrom, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
875 mrb_define_method(mrb, bsock, "_setnonblock", mrb_basicsocket_setnonblock, MRB_ARGS_REQ(1));
876 mrb_define_method(mrb, bsock, "getpeereid", mrb_basicsocket_getpeereid, MRB_ARGS_NONE());
877 mrb_define_method(mrb, bsock, "getpeername", mrb_basicsocket_getpeername, MRB_ARGS_NONE());
878 mrb_define_method(mrb, bsock, "getsockname", mrb_basicsocket_getsockname, MRB_ARGS_NONE());
879 mrb_define_method(mrb, bsock, "getsockopt", mrb_basicsocket_getsockopt, MRB_ARGS_REQ(2));
880 mrb_define_method(mrb, bsock, "recv", mrb_basicsocket_recv, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
881 // #recvmsg(maxlen, flags=0)
882 mrb_define_method(mrb, bsock, "send", mrb_basicsocket_send, MRB_ARGS_REQ(2)|MRB_ARGS_OPT(1));
885 mrb_define_method(mrb, bsock, "setsockopt", mrb_basicsocket_setsockopt, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(2));
886 mrb_define_method(mrb, bsock, "shutdown", mrb_basicsocket_shutdown, MRB_ARGS_OPT(1));
887 mrb_define_method(mrb, bsock, "_is_socket=", mrb_basicsocket_set_is_socket, MRB_ARGS_REQ(1));
889 ipsock = mrb_define_class(mrb, "IPSocket", bsock);
890 mrb_define_class_method(mrb, ipsock, "ntop", mrb_ipsocket_ntop, MRB_ARGS_REQ(1));
891 mrb_define_class_method(mrb, ipsock, "pton", mrb_ipsocket_pton, MRB_ARGS_REQ(2));
892 mrb_define_method(mrb, ipsock, "recvfrom", mrb_ipsocket_recvfrom, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
894 tcpsock = mrb_define_class(mrb, "TCPSocket", ipsock);
895 mrb_define_class_method(mrb, tcpsock, "_allocate", mrb_tcpsocket_allocate, MRB_ARGS_NONE());
896 mrb_define_class(mrb, "TCPServer", tcpsock);
898 mrb_define_class(mrb, "UDPSocket", ipsock);
901 sock = mrb_define_class(mrb, "Socket", bsock);
902 mrb_define_class_method(mrb, sock, "_accept", mrb_socket_accept, MRB_ARGS_REQ(1));
903 mrb_define_class_method(mrb, sock, "_accept2", mrb_socket_accept2, MRB_ARGS_REQ(1));
904 mrb_define_class_method(mrb, sock, "_bind", mrb_socket_bind, MRB_ARGS_REQ(3));
905 mrb_define_class_method(mrb, sock, "_connect", mrb_socket_connect, MRB_ARGS_REQ(3));
906 mrb_define_class_method(mrb, sock, "_listen", mrb_socket_listen, MRB_ARGS_REQ(2));
907 mrb_define_class_method(mrb, sock, "_sockaddr_family", mrb_socket_sockaddr_family, MRB_ARGS_REQ(1));
908 mrb_define_class_method(mrb, sock, "_socket", mrb_socket_socket, MRB_ARGS_REQ(3));
909 //mrb_define_class_method(mrb, sock, "gethostbyaddr", mrb_socket_gethostbyaddr, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
910 //mrb_define_class_method(mrb, sock, "gethostbyname", mrb_socket_gethostbyname, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
911 mrb_define_class_method(mrb, sock, "gethostname", mrb_socket_gethostname, MRB_ARGS_NONE());
912 //mrb_define_class_method(mrb, sock, "getservbyname", mrb_socket_getservbyname, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
913 //mrb_define_class_method(mrb, sock, "getservbyport", mrb_socket_getservbyport, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
914 mrb_define_class_method(mrb, sock, "sockaddr_un", mrb_socket_sockaddr_un, MRB_ARGS_REQ(1));
915 mrb_define_class_method(mrb, sock, "socketpair", mrb_socket_socketpair, MRB_ARGS_REQ(3));
916 //mrb_define_method(mrb, sock, "sysaccept", mrb_socket_accept, MRB_ARGS_NONE());
919 mrb_define_class(mrb, "UNIXSocket", bsock);
920 //mrb_define_class_method(mrb, usock, "pair", mrb_unixsocket_open, MRB_ARGS_OPT(2));
921 //mrb_define_class_method(mrb, usock, "socketpair", mrb_unixsocket_open, MRB_ARGS_OPT(2));
923 //mrb_define_method(mrb, usock, "recv_io", mrb_unixsocket_peeraddr, MRB_ARGS_NONE());
924 //mrb_define_method(mrb, usock, "recvfrom", mrb_unixsocket_peeraddr, MRB_ARGS_NONE());
925 //mrb_define_method(mrb, usock, "send_io", mrb_unixsocket_peeraddr, MRB_ARGS_NONE());
928 /* Windows IO Method Overrides on BasicSocket */
930 mrb_define_method(mrb, bsock, "close", mrb_win32_basicsocket_close, MRB_ARGS_NONE());
931 mrb_define_method(mrb, bsock, "sysread", mrb_win32_basicsocket_sysread, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
932 mrb_define_method(mrb, bsock, "sysseek", mrb_win32_basicsocket_sysseek, MRB_ARGS_REQ(1));
933 mrb_define_method(mrb, bsock, "syswrite", mrb_win32_basicsocket_syswrite, MRB_ARGS_REQ(1));
936 constants = mrb_define_module_under(mrb, sock, "Constants");
938 #define define_const(SYM) \
940 mrb_define_const(mrb, constants, #SYM, mrb_fixnum_value(SYM)); \
943 #include "const.cstub"
947 mrb_mruby_socket_gem_final(mrb_state* mrb)
950 ai = mrb_mod_cv_get(mrb, mrb_class_get(mrb, "Addrinfo"), mrb_intern_lit(mrb, "_lastai"));
951 if (mrb_cptr_p(ai)) {
952 freeaddrinfo((struct addrinfo*)mrb_cptr(ai));