diff options
Diffstat (limited to 'ext/socket/init.c')
-rw-r--r-- | ext/socket/init.c | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/ext/socket/init.c b/ext/socket/init.c index c3f33964a8..178aad0948 100644 --- a/ext/socket/init.c +++ b/ext/socket/init.c @@ -39,24 +39,31 @@ rsock_raise_socket_error(const char *reason, int error) rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error)); } -VALUE -rsock_init_sock(VALUE sock, int fd) +#ifdef _WIN32 +#define is_socket(fd) rb_w32_is_socket(fd) +#else +static int +is_socket(int fd) { - rb_io_t *fp; -#ifndef _WIN32 struct stat sbuf; if (fstat(fd, &sbuf) < 0) rb_sys_fail("fstat(2)"); - rb_update_max_fd(fd); - if (!S_ISSOCK(sbuf.st_mode)) - rb_raise(rb_eArgError, "not a socket file descriptor"); -#else - rb_update_max_fd(fd); - if (!rb_w32_is_socket(fd)) - rb_raise(rb_eArgError, "not a socket file descriptor"); + return S_ISSOCK(sbuf.st_mode); +} #endif +VALUE +rsock_init_sock(VALUE sock, int fd) +{ + rb_io_t *fp; + + if (!is_socket(fd)) { + errno = EBADF; + rb_sys_fail("not a socket file descriptor"); + } + + rb_update_max_fd(fd); MakeOpenFile(sock, fp); fp->fd = fd; fp->mode = FMODE_READWRITE|FMODE_DUPLEX; |