2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 #include <sys/syscallsubr.h>
35 #include <sys/systm.h>
38 #include <contrib/cloudabi/cloudabi64_types.h>
40 #include <compat/cloudabi/cloudabi_util.h>
42 #include <compat/cloudabi64/cloudabi64_proto.h>
43 #include <compat/cloudabi64/cloudabi64_util.h>
45 static MALLOC_DEFINE(M_SOCKET, "socket", "CloudABI socket");
48 cloudabi64_sys_sock_recv(struct thread *td,
49 struct cloudabi64_sys_sock_recv_args *uap)
51 struct sockaddr_storage ss;
52 cloudabi64_recv_in_t ri;
53 cloudabi64_recv_out_t ro = {};
54 cloudabi64_iovec_t iovobj;
55 struct msghdr msghdr = {};
56 const cloudabi64_iovec_t *user_iov;
60 error = copyin(uap->in, &ri, sizeof(ri));
64 /* Convert results in cloudabi_recv_in_t to struct msghdr. */
65 if (ri.ri_data_len > UIO_MAXIOV)
67 msghdr.msg_iovlen = ri.ri_data_len;
68 msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec),
70 user_iov = TO_PTR(ri.ri_data);
71 for (i = 0; i < msghdr.msg_iovlen; i++) {
72 error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
74 free(msghdr.msg_iov, M_SOCKET);
77 msghdr.msg_iov[i].iov_base = TO_PTR(iovobj.buf);
78 msghdr.msg_iov[i].iov_len = iovobj.buf_len;
80 msghdr.msg_name = &ss;
81 msghdr.msg_namelen = sizeof(ss);
82 if (ri.ri_flags & CLOUDABI_MSG_PEEK)
83 msghdr.msg_flags |= MSG_PEEK;
84 if (ri.ri_flags & CLOUDABI_MSG_WAITALL)
85 msghdr.msg_flags |= MSG_WAITALL;
87 /* TODO(ed): Add file descriptor passing. */
88 error = kern_recvit(td, uap->sock, &msghdr, UIO_SYSSPACE, NULL);
89 free(msghdr.msg_iov, M_SOCKET);
93 /* Convert results in msghdr to cloudabi_recv_out_t. */
94 ro.ro_datalen = td->td_retval[0];
95 cloudabi_convert_sockaddr((struct sockaddr *)&ss,
96 MIN(msghdr.msg_namelen, sizeof(ss)), &ro.ro_peername);
98 return (copyout(&ro, uap->out, sizeof(ro)));
102 cloudabi64_sys_sock_send(struct thread *td,
103 struct cloudabi64_sys_sock_send_args *uap)
105 cloudabi64_send_in_t si;
106 cloudabi64_send_out_t so = {};
107 cloudabi64_ciovec_t iovobj;
108 struct msghdr msghdr = {};
109 const cloudabi64_ciovec_t *user_iov;
113 error = copyin(uap->in, &si, sizeof(si));
117 /* Convert results in cloudabi_send_in_t to struct msghdr. */
118 if (si.si_data_len > UIO_MAXIOV)
120 msghdr.msg_iovlen = si.si_data_len;
121 msghdr.msg_iov = malloc(msghdr.msg_iovlen * sizeof(struct iovec),
123 user_iov = TO_PTR(si.si_data);
124 for (i = 0; i < msghdr.msg_iovlen; i++) {
125 error = copyin(&user_iov[i], &iovobj, sizeof(iovobj));
127 free(msghdr.msg_iov, M_SOCKET);
130 msghdr.msg_iov[i].iov_base = TO_PTR(iovobj.buf);
131 msghdr.msg_iov[i].iov_len = iovobj.buf_len;
134 flags = MSG_NOSIGNAL;
135 if (si.si_flags & CLOUDABI_MSG_EOR)
138 /* TODO(ed): Add file descriptor passing. */
139 error = kern_sendit(td, uap->sock, &msghdr, flags, NULL, UIO_USERSPACE);
140 free(msghdr.msg_iov, M_SOCKET);
144 /* Convert results in msghdr to cloudabi_send_out_t. */
145 so.so_datalen = td->td_retval[0];
146 td->td_retval[0] = 0;
147 return (copyout(&so, uap->out, sizeof(so)));