2017-10-18 16:15:51 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2014-2017 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "_sck.h"
|
|
|
|
|
2018-01-18 16:27:17 +00:00
|
|
|
#if defined(HAVE_ACCEPT4)
|
|
|
|
# define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2017-10-18 16:15:51 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static moo_pfrc_t pf_open_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
|
|
|
moo_oop_t dom, type, proto;
|
2018-01-18 16:27:17 +00:00
|
|
|
int fd = -1, typev;
|
2017-10-18 16:15:51 +00:00
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t))
|
|
|
|
);
|
|
|
|
|
|
|
|
dom = MOO_STACK_GETARG(moo, nargs, 0);
|
|
|
|
type = MOO_STACK_GETARG(moo, nargs, 1);
|
|
|
|
proto = MOO_STACK_GETARG(moo, nargs, 2);
|
|
|
|
|
|
|
|
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OOP_IS_SMOOI(dom) && MOO_OOP_IS_SMOOI(type) && MOO_OOP_IS_SMOOI(proto));
|
|
|
|
|
2018-01-18 16:27:17 +00:00
|
|
|
typev = MOO_OOP_TO_SMOOI(type);
|
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
|
|
|
typev |= SOCK_NONBLOCK | SOCK_CLOEXEC;
|
|
|
|
create_socket:
|
|
|
|
#endif
|
2018-01-19 13:29:15 +00:00
|
|
|
|
2018-01-18 16:27:17 +00:00
|
|
|
fd = socket (MOO_OOP_TO_SMOOI(dom), typev, MOO_OOP_TO_SMOOI(proto));
|
2017-10-18 16:15:51 +00:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
2018-01-18 16:27:17 +00:00
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
|
|
|
if (errno == EINVAL && (typev & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
|
|
|
|
{
|
|
|
|
typev &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
|
|
|
|
goto create_socket;
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-17 15:20:58 +00:00
|
|
|
moo_seterrwithsyserr (moo, errno);
|
2017-10-18 16:15:51 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MOO_IN_SMOOI_RANGE(fd))
|
|
|
|
{
|
|
|
|
/* the file descriptor is too big to be represented as a small integer */
|
2017-12-17 15:20:58 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_ERANGE, "socket handle %d not in the permitted range", fd);
|
2017-10-18 16:15:51 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2018-01-18 16:27:17 +00:00
|
|
|
|
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
|
|
|
if (!(typev & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
|
|
|
|
#endif
|
|
|
|
{
|
2018-01-19 09:33:47 +00:00
|
|
|
int fl;
|
2018-01-18 16:27:17 +00:00
|
|
|
|
2018-01-19 09:33:47 +00:00
|
|
|
fl = fcntl(fd, F_GETFL, 0);
|
|
|
|
if (fl == -1)
|
2018-01-18 16:27:17 +00:00
|
|
|
{
|
2018-01-19 09:33:47 +00:00
|
|
|
fcntl_failure:
|
2018-01-18 16:27:17 +00:00
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
goto oops;
|
|
|
|
}
|
2018-01-19 09:33:47 +00:00
|
|
|
|
|
|
|
fl |= O_NONBLOCK;
|
|
|
|
#if defined(O_CLOEXEC)
|
|
|
|
fl |= O_CLOEXEC;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETFL, fl) == -1) goto fcntl_failure;
|
2018-01-18 16:27:17 +00:00
|
|
|
}
|
2017-10-18 16:15:51 +00:00
|
|
|
|
|
|
|
sck->handle = MOO_SMOOI_TO_OOP(fd);
|
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
|
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
if (fd >= 0) close (fd);
|
2017-12-11 16:27:53 +00:00
|
|
|
return MOO_PF_FAILURE;
|
2017-10-18 16:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static moo_pfrc_t pf_close_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle)
|
|
|
|
);
|
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
if (close(MOO_OOP_TO_SMOOI(sck->handle)) == -1)
|
|
|
|
{
|
2017-12-17 15:20:58 +00:00
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
2017-10-18 16:15:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sck->handle = MOO_SMOOI_TO_OOP(-1);
|
2017-10-30 01:11:18 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
2017-12-17 15:20:58 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2017-10-18 16:15:51 +00:00
|
|
|
}
|
2017-10-30 01:11:18 +00:00
|
|
|
}
|
2017-10-18 16:15:51 +00:00
|
|
|
|
2017-12-11 16:27:53 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EBADHND, "bad socket handle - %O", sck->handle);
|
|
|
|
return MOO_PF_FAILURE;
|
2017-10-18 16:15:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 19:07:31 +00:00
|
|
|
static moo_pfrc_t pf_bind_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
2018-01-14 15:01:56 +00:00
|
|
|
moo_oop_t arg;
|
|
|
|
int fd, enable;
|
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
arg = MOO_STACK_GETARG(moo, nargs, 0);
|
|
|
|
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle));
|
|
|
|
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OBJ_IS_BYTE_POINTER(arg));
|
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
|
|
|
if (fd <= -1)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
enable = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, MOO_SIZEOF(int)) == -1 ||
|
|
|
|
bind(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), moo_sck_addr_len((sck_addr_t*)MOO_OBJ_GET_BYTE_SLOT(arg))) == -1)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
2017-12-30 19:07:31 +00:00
|
|
|
|
2018-01-14 15:01:56 +00:00
|
|
|
static moo_pfrc_t pf_accept_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck, newsck;
|
|
|
|
moo_oop_t arg;
|
|
|
|
sck_len_t addrlen;
|
2018-01-19 09:33:47 +00:00
|
|
|
int fd, newfd, fl;
|
2017-12-30 19:07:31 +00:00
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
2018-01-14 15:01:56 +00:00
|
|
|
arg = MOO_STACK_GETARG(moo, nargs, 0);
|
|
|
|
|
2017-12-30 19:07:31 +00:00
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle));
|
2018-01-14 15:01:56 +00:00
|
|
|
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OBJ_IS_BYTE_POINTER(arg));
|
2017-12-30 19:07:31 +00:00
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
2018-01-14 15:01:56 +00:00
|
|
|
if (fd <= -1)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
2017-12-30 19:07:31 +00:00
|
|
|
|
2018-01-14 15:01:56 +00:00
|
|
|
addrlen = MOO_OBJ_GET_SIZE(arg);
|
2018-01-14 15:38:06 +00:00
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
|
|
|
|
newfd = accept4(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC);
|
2018-01-18 16:27:17 +00:00
|
|
|
if (newfd == -1)
|
2018-01-14 15:38:06 +00:00
|
|
|
{
|
2018-01-18 16:27:17 +00:00
|
|
|
if (errno != ENOSYS)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto accept_done;
|
2018-01-14 15:38:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-01-18 16:27:17 +00:00
|
|
|
newfd = accept(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), &addrlen);
|
2018-01-14 15:01:56 +00:00
|
|
|
if (newfd == -1)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
2018-01-19 09:33:47 +00:00
|
|
|
fl = fcntl(newfd, F_GETFL, 0);
|
|
|
|
if (fl == -1)
|
2018-01-18 16:27:17 +00:00
|
|
|
{
|
2018-01-19 09:33:47 +00:00
|
|
|
fcntl_failure:
|
2018-01-18 16:27:17 +00:00
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
close (newfd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
2018-01-19 09:33:47 +00:00
|
|
|
|
|
|
|
fl |= O_NONBLOCK;
|
|
|
|
#if defined(O_CLOEXEC)
|
|
|
|
fl |= O_CLOEXEC;
|
|
|
|
#endif
|
|
|
|
if (fcntl(newfd, F_SETFL, fl) == -1) goto fcntl_failure;
|
2018-01-18 16:27:17 +00:00
|
|
|
|
|
|
|
accept_done:
|
2018-01-14 15:01:56 +00:00
|
|
|
newsck = (oop_sck_t)moo_instantiate (moo, MOO_OBJ_GET_CLASS(sck), MOO_NULL, 0);
|
|
|
|
if (!newsck)
|
|
|
|
{
|
|
|
|
close (newfd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MOO_IN_SMOOI_RANGE(newfd))
|
|
|
|
{
|
|
|
|
/* the file descriptor is too big to be represented as a small integer */
|
|
|
|
moo_seterrbfmt (moo, MOO_ERANGE, "socket handle %d not in the permitted range", newfd);
|
|
|
|
close (newfd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
newsck->handle = MOO_SMOOI_TO_OOP(newfd);
|
|
|
|
|
2018-01-14 15:11:53 +00:00
|
|
|
MOO_STACK_SETRET (moo, nargs, (moo_oop_t)newsck);
|
2018-01-14 15:01:56 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moo_pfrc_t pf_listen_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
|
|
|
moo_oop_t arg;
|
|
|
|
int fd, n;
|
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
arg = MOO_STACK_GETARG(moo, nargs, 0);
|
|
|
|
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle));
|
|
|
|
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OOP_IS_SMOOI(arg));
|
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
|
|
|
if (fd <= -1)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = listen(fd, MOO_OOP_TO_SMOOI(arg));
|
2017-12-30 19:07:31 +00:00
|
|
|
if (n == -1)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-01-14 15:01:56 +00:00
|
|
|
|
2017-10-18 16:15:51 +00:00
|
|
|
static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
2018-01-18 16:27:17 +00:00
|
|
|
int fd, n;
|
2018-01-11 16:29:43 +00:00
|
|
|
moo_oop_t arg;
|
2017-10-18 16:15:51 +00:00
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
2018-01-12 16:06:05 +00:00
|
|
|
arg = MOO_STACK_GETARG(moo, nargs, 0);
|
2018-01-11 16:29:43 +00:00
|
|
|
|
2017-10-18 16:15:51 +00:00
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle));
|
2018-01-11 16:29:43 +00:00
|
|
|
MOO_PF_CHECK_ARGS (moo, nargs, MOO_OBJ_IS_BYTE_POINTER(arg));
|
2017-10-18 16:15:51 +00:00
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
2018-01-14 15:01:56 +00:00
|
|
|
if (fd <= -1)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
2017-10-18 16:15:51 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-01-12 16:06:05 +00:00
|
|
|
n = connect(fd, (struct sockaddr*)MOO_OBJ_GET_BYTE_SLOT(arg), moo_sck_addr_len((sck_addr_t*)MOO_OBJ_GET_BYTE_SLOT(arg)));
|
2017-10-18 16:15:51 +00:00
|
|
|
}
|
|
|
|
while (n == -1 && errno == EINTR);
|
2018-01-19 13:29:15 +00:00
|
|
|
|
2017-10-18 16:15:51 +00:00
|
|
|
if (n == -1 && errno != EINPROGRESS)
|
|
|
|
{
|
2018-01-18 16:27:17 +00:00
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
2017-10-18 16:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
2017-12-18 13:34:47 +00:00
|
|
|
|
2018-01-14 15:01:56 +00:00
|
|
|
static moo_pfrc_t pf_get_socket_error (moo_t* moo, moo_ooi_t nargs)
|
2017-12-18 13:34:47 +00:00
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
2018-01-14 15:01:56 +00:00
|
|
|
int fd, ret;
|
|
|
|
sck_len_t len;
|
2017-12-18 13:34:47 +00:00
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle)
|
|
|
|
);
|
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
2018-01-14 15:01:56 +00:00
|
|
|
if (fd <= -1)
|
2017-12-18 13:34:47 +00:00
|
|
|
{
|
2018-01-14 15:01:56 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
2017-12-18 13:34:47 +00:00
|
|
|
|
2018-01-14 15:01:56 +00:00
|
|
|
len = MOO_SIZEOF(ret);
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&ret, &len) == -1)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
2017-12-18 13:34:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 15:01:56 +00:00
|
|
|
/* if ret == EINPROGRESS .. it's in progress */
|
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ret));
|
2017-12-18 13:34:47 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-12-20 16:25:20 +00:00
|
|
|
static moo_pfrc_t pf_read_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
|
|
|
moo_oop_byte_t buf;
|
|
|
|
int fd;
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle)
|
|
|
|
);
|
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
|
|
|
if (fd <= -1)
|
|
|
|
{
|
2018-01-14 15:01:56 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
2017-12-20 16:25:20 +00:00
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (moo_oop_byte_t)MOO_STACK_GETARG (moo, nargs, 0);
|
|
|
|
if (!MOO_OBJ_IS_BYTE_POINTER(buf))
|
|
|
|
{
|
2018-01-14 15:01:56 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "buffer not a byte array - %O", buf);
|
2017-12-20 16:25:20 +00:00
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = recv (fd, MOO_OBJ_GET_BYTE_SLOT(buf), MOO_OBJ_GET_SIZE(buf), 0);
|
|
|
|
if (n <= -1 && errno != EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOO_ASSERT (moo, MOO_IN_SMOOI_RANGE(n));
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(n));
|
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moo_pfrc_t pf_write_socket (moo_t* moo, moo_ooi_t nargs)
|
|
|
|
{
|
|
|
|
oop_sck_t sck;
|
|
|
|
moo_oop_byte_t buf;
|
2018-01-24 13:29:36 +00:00
|
|
|
moo_oow_t offset, length, maxlen;
|
2017-12-20 16:25:20 +00:00
|
|
|
int fd;
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
sck = (oop_sck_t)MOO_STACK_GETRCV(moo, nargs);
|
|
|
|
MOO_PF_CHECK_RCV (moo,
|
|
|
|
MOO_OOP_IS_POINTER(sck) &&
|
|
|
|
MOO_OBJ_BYTESOF(sck) >= (MOO_SIZEOF(*sck) - MOO_SIZEOF(moo_obj_t)) &&
|
|
|
|
MOO_OOP_IS_SMOOI(sck->handle)
|
|
|
|
);
|
|
|
|
|
|
|
|
fd = MOO_OOP_TO_SMOOI(sck->handle);
|
|
|
|
if (fd <= -1)
|
|
|
|
{
|
2018-01-14 15:01:56 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "bad socket handle - %d", fd);
|
2017-12-20 16:25:20 +00:00
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = (moo_oop_byte_t)MOO_STACK_GETARG (moo, nargs, 0);
|
|
|
|
if (!MOO_OBJ_IS_BYTE_POINTER(buf))
|
|
|
|
{
|
2018-01-14 15:01:56 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "buffer not a byte array - %O", buf);
|
2017-12-20 16:25:20 +00:00
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-01-24 13:29:36 +00:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
maxlen = MOO_OBJ_GET_SIZE(buf);
|
|
|
|
length = maxlen;
|
|
|
|
|
|
|
|
if (nargs >= 2)
|
|
|
|
{
|
|
|
|
moo_oop_t tmp;
|
|
|
|
|
|
|
|
tmp = MOO_STACK_GETARG(moo, nargs, 1);
|
|
|
|
if (moo_inttooow (moo, tmp, &offset) <= 0)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset - %O", tmp);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nargs >= 3)
|
|
|
|
{
|
|
|
|
tmp = MOO_STACK_GETARG(moo, nargs, 2);
|
|
|
|
if (moo_inttooow (moo, tmp, &length) <= 0)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "invalid length - %O", tmp);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset >= maxlen) offset = maxlen - 1;
|
|
|
|
if (length > maxlen - offset) length = maxlen - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = send (fd, &MOO_OBJ_GET_BYTE_SLOT(buf)[offset], length, 0);
|
2017-12-20 16:25:20 +00:00
|
|
|
if (n <= -1 && errno != EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
moo_seterrwithsyserr (moo, errno);
|
|
|
|
return MOO_PF_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOO_ASSERT (moo, MOO_IN_SMOOI_RANGE(n));
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(n));
|
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-10-18 16:15:51 +00:00
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
typedef struct fnctab_t fnctab_t;
|
|
|
|
struct fnctab_t
|
|
|
|
{
|
|
|
|
moo_method_type_t type;
|
|
|
|
moo_ooch_t mthname[15];
|
|
|
|
int variadic;
|
|
|
|
moo_pfimpl_t handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define C MOO_METHOD_CLASS
|
|
|
|
#define I MOO_METHOD_INSTANCE
|
|
|
|
|
|
|
|
#define MA MOO_TYPE_MAX(moo_oow_t)
|
|
|
|
|
|
|
|
static moo_pfinfo_t pfinfos[] =
|
|
|
|
{
|
2018-01-14 15:01:56 +00:00
|
|
|
{ I, { 'a','c','c','e','p','t',':','\0' }, 0, { pf_accept_socket, 1, 1 } },
|
|
|
|
{ I, { 'b','i','n','d',':','\0' }, 0, { pf_bind_socket, 1, 1 } },
|
|
|
|
{ I, { 'c','l','o','s','e','\0' }, 0, { pf_close_socket, 0, 0 } },
|
|
|
|
{ I, { 'c','o','n','n','e','c','t',':','\0' }, 0, { pf_connect, 1, 1 } },
|
|
|
|
{ I, { 'l','i','s','t','e','n',':','\0' }, 0, { pf_listen_socket, 1, 1 } },
|
|
|
|
{ I, { 'o','p','e','n','\0' }, 0, { pf_open_socket, 3, 3 } },
|
|
|
|
{ I, { 'r','e','a','d','B','y','t','e','s',':','\0' }, 0, { pf_read_socket, 1, 1 } },
|
|
|
|
{ I, { 's','o','c','k','e','t','E','r','r','o','r','\0' }, 0, { pf_get_socket_error, 0, 0 } },
|
2018-01-24 13:29:36 +00:00
|
|
|
{ I, { 'w','r','i','t','e','B','y','t','e','s',':','\0' }, 0, { pf_write_socket, 1, 1 } },
|
|
|
|
{ I, { 'w','r','i','t','e','B','y','t','e','s',':','o','f','f','s','e','t',':','l','e','n','g','t','h',':','\0' }, 0, { pf_write_socket, 3, 3 } }
|
2017-10-18 16:15:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
|
|
|
|
{
|
|
|
|
/*if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(sck_t), MOO_NULL) <= -1) return -1;*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moo_pfbase_t* query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name, moo_oow_t namelen)
|
|
|
|
{
|
|
|
|
return moo_findpfbase (moo, pfinfos, MOO_COUNTOF(pfinfos), name, namelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unload (moo_t* moo, moo_mod_t* mod)
|
|
|
|
{
|
|
|
|
/* TODO: anything? close open open dll handles? For that, pf_open must store the value it returns to mod->ctx or somewhere..*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int moo_mod_sck (moo_t* moo, moo_mod_t* mod)
|
|
|
|
{
|
|
|
|
mod->import = import;
|
|
|
|
mod->query = query;
|
|
|
|
mod->unload = unload;
|
|
|
|
mod->ctx = MOO_NULL;
|
|
|
|
return 0;
|
|
|
|
}
|