2016-08-21 16:01:30 +00:00
|
|
|
/*-
|
2017-03-22 06:43:10 +00:00
|
|
|
* Copyright (c) 2015-2017 Nuxi, https://nuxi.nl/
|
2016-08-21 16:01:30 +00:00
|
|
|
*
|
|
|
|
* 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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 <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include <contrib/cloudabi/cloudabi32_types.h>
|
|
|
|
|
|
|
|
#include <compat/cloudabi/cloudabi_util.h>
|
|
|
|
|
|
|
|
#include <compat/cloudabi32/cloudabi32_proto.h>
|
|
|
|
#include <compat/cloudabi32/cloudabi32_util.h>
|
|
|
|
|
|
|
|
static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket");
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi32_sys_sock_recv(struct thread *td,
|
|
|
|
struct cloudabi32_sys_sock_recv_args *uap)
|
|
|
|
{
|
|
|
|
cloudabi32_recv_in_t ri;
|
|
|
|
cloudabi32_recv_out_t ro = {};
|
|
|
|
cloudabi32_iovec_t iovobj;
|
2017-03-22 19:20:39 +00:00
|
|
|
struct iovec *iov;
|
2016-08-21 16:01:30 +00:00
|
|
|
const cloudabi32_iovec_t *user_iov;
|
2017-03-22 19:20:39 +00:00
|
|
|
size_t i, rdatalen, rfdslen;
|
2016-08-21 16:01:30 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
error = copyin(uap->in, &ri, sizeof(ri));
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
2017-03-22 19:20:39 +00:00
|
|
|
/* Convert iovecs to native format. */
|
2017-01-17 22:05:52 +00:00
|
|
|
if (ri.ri_data_len > UIO_MAXIOV)
|
2016-08-21 16:01:30 +00:00
|
|
|
return (EINVAL);
|
2017-03-22 19:20:39 +00:00
|
|
|
iov = malloc(ri.ri_data_len * sizeof(struct iovec),
|
2016-08-21 16:01:30 +00:00
|
|
|
M_SOCKET, M_WAITOK);
|
|
|
|
user_iov = TO_PTR(ri.ri_data);
|
2017-03-22 19:20:39 +00:00
|
|
|
for (i = 0; i < ri.ri_data_len; i++) {
|
2016-08-21 16:01:30 +00:00
|
|
|
error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
|
|
|
|
if (error != 0) {
|
2017-03-22 19:20:39 +00:00
|
|
|
free(iov, M_SOCKET);
|
2016-08-21 16:01:30 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2017-03-22 19:20:39 +00:00
|
|
|
iov[i].iov_base = TO_PTR(iovobj.buf);
|
|
|
|
iov[i].iov_len = iovobj.buf_len;
|
2016-08-21 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 19:20:39 +00:00
|
|
|
error = cloudabi_sock_recv(td, uap->sock, iov, ri.ri_data_len,
|
|
|
|
TO_PTR(ri.ri_fds), ri.ri_fds_len, ri.ri_flags, &rdatalen,
|
|
|
|
&rfdslen, &ro.ro_peername, &ro.ro_flags);
|
|
|
|
free(iov, M_SOCKET);
|
2016-08-21 16:01:30 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
2017-03-22 19:20:39 +00:00
|
|
|
ro.ro_datalen = rdatalen;
|
|
|
|
ro.ro_fdslen = rfdslen;
|
2016-08-21 16:01:30 +00:00
|
|
|
return (copyout(&ro, uap->out, sizeof(ro)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cloudabi32_sys_sock_send(struct thread *td,
|
|
|
|
struct cloudabi32_sys_sock_send_args *uap)
|
|
|
|
{
|
|
|
|
cloudabi32_send_in_t si;
|
|
|
|
cloudabi32_send_out_t so = {};
|
|
|
|
cloudabi32_ciovec_t iovobj;
|
2017-03-22 06:43:10 +00:00
|
|
|
struct iovec *iov;
|
2016-08-21 16:01:30 +00:00
|
|
|
const cloudabi32_ciovec_t *user_iov;
|
2017-03-22 06:43:10 +00:00
|
|
|
size_t datalen, i;
|
|
|
|
int error;
|
2016-08-21 16:01:30 +00:00
|
|
|
|
|
|
|
error = copyin(uap->in, &si, sizeof(si));
|
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
2017-03-22 06:43:10 +00:00
|
|
|
/* Convert iovecs to native format. */
|
2017-01-17 22:05:52 +00:00
|
|
|
if (si.si_data_len > UIO_MAXIOV)
|
2016-08-21 16:01:30 +00:00
|
|
|
return (EINVAL);
|
2017-03-22 06:43:10 +00:00
|
|
|
iov = malloc(si.si_data_len * sizeof(struct iovec),
|
2016-08-21 16:01:30 +00:00
|
|
|
M_SOCKET, M_WAITOK);
|
|
|
|
user_iov = TO_PTR(si.si_data);
|
2017-03-22 06:43:10 +00:00
|
|
|
for (i = 0; i < si.si_data_len; i++) {
|
2016-08-21 16:01:30 +00:00
|
|
|
error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
|
|
|
|
if (error != 0) {
|
2017-03-22 06:43:10 +00:00
|
|
|
free(iov, M_SOCKET);
|
2016-08-21 16:01:30 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2017-03-22 06:43:10 +00:00
|
|
|
iov[i].iov_base = TO_PTR(iovobj.buf);
|
|
|
|
iov[i].iov_len = iovobj.buf_len;
|
2016-08-21 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 06:43:10 +00:00
|
|
|
error = cloudabi_sock_send(td, uap->sock, iov, si.si_data_len,
|
|
|
|
TO_PTR(si.si_fds), si.si_fds_len, si.si_flags, &datalen);
|
|
|
|
free(iov, M_SOCKET);
|
2016-08-21 16:01:30 +00:00
|
|
|
if (error != 0)
|
|
|
|
return (error);
|
|
|
|
|
2017-03-22 06:43:10 +00:00
|
|
|
so.so_datalen = datalen;
|
2016-08-21 16:01:30 +00:00
|
|
|
return (copyout(&so, uap->out, sizeof(so)));
|
|
|
|
}
|