Enhance sock_addr_local syscall (#1320)

Slightly change the __wasi_sock_addr_local interface - since we already have
a `__wasi_addr_t` structure which is an union, there's no need for passing the
length around - the address buffer will always have the right length (i.e. max
of all address families).
This commit is contained in:
Marcin Kolny
2022-08-01 09:15:33 +02:00
committed by GitHub
parent 08fd714551
commit 3e77b053c3
11 changed files with 195 additions and 40 deletions

View File

@ -203,12 +203,14 @@ convert_clockid(__wasi_clockid_t in, clockid_t *out)
static void
ipv4_addr_to_wasi_addr(uint32_t addr, __wasi_ip_port_t port, __wasi_addr_t *out)
{
addr = ntohl(addr);
out->kind = IPv4;
out->addr.ip4.port = port;
out->addr.ip4.addr.n3 = (addr & 0xFF000000) >> 24;
out->addr.ip4.addr.n2 = (addr & 0x00FF0000) >> 16;
out->addr.ip4.addr.n1 = (addr & 0x0000FF00) >> 8;
out->addr.ip4.addr.n0 = (addr & 0x000000FF);
out->addr.ip4.addr.n0 = (addr & 0xFF000000) >> 24;
out->addr.ip4.addr.n1 = (addr & 0x00FF0000) >> 16;
out->addr.ip4.addr.n2 = (addr & 0x0000FF00) >> 8;
out->addr.ip4.addr.n3 = (addr & 0x000000FF);
}
// Converts an IPv6 binary address object to WASI address object.
@ -2881,16 +2883,34 @@ wasi_ssp_sock_addr_local(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct fd_table *curfds,
#endif
__wasi_fd_t fd, uint8 *buf, __wasi_size_t buf_len)
__wasi_fd_t fd, __wasi_addr_t *addr)
{
struct fd_object *fo;
uint8 buf[16];
__wasi_ip_port_t port;
uint8 is_ipv4;
int ret;
__wasi_errno_t error =
fd_object_get(curfds, &fo, fd, __WASI_RIGHT_SOCK_ADDR_LOCAL, 0);
if (error != __WASI_ESUCCESS)
return error;
ret = os_socket_addr_local(fd_number(fo), buf, sizeof(buf) / sizeof(buf[0]),
&port, &is_ipv4);
fd_object_release(fo);
return __WASI_ENOSYS;
if (ret != BHT_OK) {
return convert_errno(errno);
}
if (is_ipv4) {
ipv4_addr_to_wasi_addr(*(uint32_t *)buf, port, addr);
}
else {
ipv6_addr_to_wasi_addr((uint16 *)buf, port, addr);
}
return __WASI_ESUCCESS;
}
__wasi_errno_t