Add the remaining pointer size independent CloudABI socket system calls.

CloudABI uses a structure called cloudabi_sockstat_t. Think of it as
'struct stat' for sockets. It is used by functions such as
getsockname(), getpeername(), some of the getsockopt() values, etc.

This change implements the sock_stat_get() system call that returns a
copy of this structure. The accept() system call should also return a
full copy of this structure eventually, but for now we're only
interested in the peer address. Add a TODO() to make sure this is
patched up later on.

Differential Revision:	https://reviews.freebsd.org/D3218
This commit is contained in:
Ed Schouten 2015-08-05 08:18:05 +00:00
parent 4958fab8cd
commit db1c8ee585
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=286312
2 changed files with 120 additions and 4 deletions

View File

@ -27,14 +27,62 @@
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/capsicum.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/syscallsubr.h>
#include <sys/sysproto.h>
#include <sys/systm.h>
#include <sys/un.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <compat/cloudabi/cloudabi_proto.h>
#include <compat/cloudabi/cloudabi_syscalldefs.h>
#include <compat/cloudabi/cloudabi_util.h>
/* Converts FreeBSD's struct sockaddr to CloudABI's cloudabi_sockaddr_t. */
void
cloudabi_convert_sockaddr(const struct sockaddr *sa, socklen_t sal,
cloudabi_sockaddr_t *rsa)
{
const struct sockaddr_in *sin;
const struct sockaddr_in6 *sin6;
/* Zero-sized socket address. */
if (sal < offsetof(struct sockaddr, sa_family) + sizeof(sa->sa_family))
return;
switch (sa->sa_family) {
case AF_INET:
if (sal < sizeof(struct sockaddr_in))
return;
sin = (const struct sockaddr_in *)sa;
rsa->sa_family = CLOUDABI_AF_INET;
memcpy(&rsa->sa_inet.addr, &sin->sin_addr,
sizeof(rsa->sa_inet.addr));
rsa->sa_inet.port = ntohs(sin->sin_port);
return;
case AF_INET6:
if (sal < sizeof(struct sockaddr_in6))
return;
sin6 = (const struct sockaddr_in6 *)sa;
rsa->sa_family = CLOUDABI_AF_INET6;
memcpy(&rsa->sa_inet6.addr, &sin6->sin6_addr,
sizeof(rsa->sa_inet6.addr));
rsa->sa_inet6.port = ntohs(sin6->sin6_port);
return;
case AF_UNIX:
rsa->sa_family = CLOUDABI_AF_UNIX;
return;
}
}
/* Copies a pathname into a UNIX socket address structure. */
static int
@ -62,9 +110,27 @@ int
cloudabi_sys_sock_accept(struct thread *td,
struct cloudabi_sys_sock_accept_args *uap)
{
struct sockaddr *sa;
cloudabi_sockstat_t ss = {};
socklen_t sal;
int error;
/* Not implemented. */
return (ENOSYS);
if (uap->buf == NULL) {
/* Only return the new file descriptor number. */
return (kern_accept(td, uap->s, NULL, NULL, NULL));
} else {
/* Also return properties of the new socket descriptor. */
sal = MAX(sizeof(struct sockaddr_in),
sizeof(struct sockaddr_in6));
error = kern_accept(td, uap->s, (void *)&sa, &sal, NULL);
if (error != 0)
return (error);
/* TODO(ed): Fill the other members of cloudabi_sockstat_t. */
cloudabi_convert_sockaddr(sa, sal, &ss.ss_peername);
free(sa, M_SONAME);
return (copyout(&ss, uap->buf, sizeof(ss)));
}
}
int
@ -134,7 +200,51 @@ int
cloudabi_sys_sock_stat_get(struct thread *td,
struct cloudabi_sys_sock_stat_get_args *uap)
{
cloudabi_sockstat_t ss = {};
cap_rights_t rights;
struct file *fp;
struct sockaddr *sa;
struct socket *so;
int error;
/* Not implemented. */
return (ENOSYS);
error = getsock_cap(td, uap->fd, cap_rights_init(&rights,
CAP_GETSOCKOPT | CAP_GETPEERNAME | CAP_GETSOCKNAME), &fp, NULL);
if (error != 0)
return (error);
so = fp->f_data;
CURVNET_SET(so->so_vnet);
/* Set ss_sockname. */
error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
if (error == 0) {
cloudabi_convert_sockaddr(sa, sa->sa_len, &ss.ss_sockname);
free(sa, M_SONAME);
}
/* Set ss_peername. */
if ((so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) != 0) {
error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
if (error == 0) {
cloudabi_convert_sockaddr(sa, sa->sa_len,
&ss.ss_peername);
free(sa, M_SONAME);
}
}
CURVNET_RESTORE();
/* Set ss_error. */
SOCK_LOCK(so);
ss.ss_error = so->so_error;
if ((uap->flags & CLOUDABI_SOCKSTAT_CLEAR_ERROR) != 0)
so->so_error = 0;
SOCK_UNLOCK(so);
/* Set ss_state. */
if ((so->so_options & SO_ACCEPTCONN) != 0)
ss.ss_state |= CLOUDABI_SOCKSTAT_ACCEPTCONN;
fdrop(fp, td);
return (copyout(&ss, uap->buf, sizeof(ss)));
}

View File

@ -28,6 +28,8 @@
#ifndef _CLOUDABI_UTIL_H_
#define _CLOUDABI_UTIL_H_
#include <sys/socket.h>
#include <compat/cloudabi/cloudabi_syscalldefs.h>
struct file;
@ -41,6 +43,10 @@ int cloudabi_clock_time_get(struct thread *, cloudabi_clockid_t,
/* Converts a FreeBSD errno to a CloudABI errno. */
cloudabi_errno_t cloudabi_convert_errno(int);
/* Converts FreeBSD's struct sockaddr to CloudABI's cloudabi_sockaddr_t. */
void cloudabi_convert_sockaddr(const struct sockaddr *, socklen_t,
cloudabi_sockaddr_t *);
/* Converts a file descriptor to a CloudABI file descriptor type. */
cloudabi_filetype_t cloudabi_convert_filetype(const struct file *);