| /** |
| * Copyright (c) 2023-2024 Marcin Niestroj |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| /** |
| * @file |
| * |
| * Zephyr (top) side of NSOS (Native Simulator Offloaded Sockets). |
| */ |
| |
| #undef _POSIX_C_SOURCE |
| #define _POSIX_C_SOURCE 200809L |
| |
| #include <zephyr/logging/log.h> |
| LOG_MODULE_REGISTER(nsos_sockets); |
| |
| #include <soc.h> |
| #include <string.h> |
| #include <zephyr/net/ethernet.h> |
| #include <zephyr/net/net_ip.h> |
| #include <zephyr/net/offloaded_netdev.h> |
| #include <zephyr/net/socket_offload.h> |
| #include <zephyr/posix/fcntl.h> |
| #include <zephyr/sys/fdtable.h> |
| #include <zephyr/sys/dlist.h> |
| |
| #include "sockets_internal.h" |
| #include "nsos.h" |
| #include "nsos_errno.h" |
| #include "nsos_fcntl.h" |
| #include "nsos_netdb.h" |
| #include "nsos_socket.h" |
| |
| #include "nsi_host_trampolines.h" |
| |
| BUILD_ASSERT(CONFIG_HEAP_MEM_POOL_SIZE > 0); |
| |
| #define NSOS_IRQ_FLAGS (0) |
| #define NSOS_IRQ_PRIORITY (2) |
| |
| struct nsos_socket; |
| |
| struct nsos_socket_poll { |
| struct nsos_mid_pollfd mid; |
| struct k_poll_signal signal; |
| |
| sys_dnode_t node; |
| }; |
| |
| struct nsos_socket { |
| int fd; |
| |
| k_timeout_t recv_timeout; |
| k_timeout_t send_timeout; |
| |
| struct nsos_socket_poll poll; |
| }; |
| |
| static sys_dlist_t nsos_polls = SYS_DLIST_STATIC_INIT(&nsos_polls); |
| |
| static int socket_family_to_nsos_mid(int family, int *family_mid) |
| { |
| switch (family) { |
| case AF_UNSPEC: |
| *family_mid = NSOS_MID_AF_UNSPEC; |
| break; |
| case AF_INET: |
| *family_mid = NSOS_MID_AF_INET; |
| break; |
| case AF_INET6: |
| *family_mid = NSOS_MID_AF_INET6; |
| break; |
| case AF_UNIX: |
| *family_mid = NSOS_MID_AF_UNIX; |
| break; |
| case AF_PACKET: |
| *family_mid = NSOS_MID_AF_PACKET; |
| break; |
| default: |
| return -NSOS_MID_EAFNOSUPPORT; |
| } |
| |
| return 0; |
| } |
| |
| static int socket_proto_to_nsos_mid(int proto, int *proto_mid) |
| { |
| switch (proto) { |
| case IPPROTO_IP: |
| *proto_mid = NSOS_MID_IPPROTO_IP; |
| break; |
| case IPPROTO_ICMP: |
| *proto_mid = NSOS_MID_IPPROTO_ICMP; |
| break; |
| case IPPROTO_IGMP: |
| *proto_mid = NSOS_MID_IPPROTO_IGMP; |
| break; |
| case IPPROTO_IPIP: |
| *proto_mid = NSOS_MID_IPPROTO_IPIP; |
| break; |
| case IPPROTO_TCP: |
| *proto_mid = NSOS_MID_IPPROTO_TCP; |
| break; |
| case IPPROTO_UDP: |
| *proto_mid = NSOS_MID_IPPROTO_UDP; |
| break; |
| case IPPROTO_IPV6: |
| *proto_mid = NSOS_MID_IPPROTO_IPV6; |
| break; |
| case IPPROTO_RAW: |
| *proto_mid = NSOS_MID_IPPROTO_RAW; |
| break; |
| case htons(IPPROTO_ETH_P_ALL): |
| *proto_mid = NSOS_MID_IPPROTO_ETH_P_ALL; |
| break; |
| default: |
| return -NSOS_MID_EPROTONOSUPPORT; |
| } |
| |
| return 0; |
| } |
| |
| static int socket_type_to_nsos_mid(int type, int *type_mid) |
| { |
| switch (type) { |
| case SOCK_STREAM: |
| *type_mid = NSOS_MID_SOCK_STREAM; |
| break; |
| case SOCK_DGRAM: |
| *type_mid = NSOS_MID_SOCK_DGRAM; |
| break; |
| case SOCK_RAW: |
| *type_mid = NSOS_MID_SOCK_RAW; |
| break; |
| default: |
| return -NSOS_MID_ESOCKTNOSUPPORT; |
| } |
| |
| return 0; |
| } |
| |
| static int socket_flags_to_nsos_mid(int flags) |
| { |
| int flags_mid = 0; |
| |
| nsos_socket_flag_convert(&flags, ZSOCK_MSG_PEEK, |
| &flags_mid, NSOS_MID_MSG_PEEK); |
| nsos_socket_flag_convert(&flags, ZSOCK_MSG_TRUNC, |
| &flags_mid, NSOS_MID_MSG_TRUNC); |
| nsos_socket_flag_convert(&flags, ZSOCK_MSG_DONTWAIT, |
| &flags_mid, NSOS_MID_MSG_DONTWAIT); |
| nsos_socket_flag_convert(&flags, ZSOCK_MSG_WAITALL, |
| &flags_mid, NSOS_MID_MSG_WAITALL); |
| |
| if (flags != 0) { |
| return -NSOS_MID_EINVAL; |
| } |
| |
| return flags_mid; |
| } |
| |
| static const struct socket_op_vtable nsos_socket_fd_op_vtable; |
| |
| static int nsos_socket_create(int family, int type, int proto) |
| { |
| int fd; |
| struct nsos_socket *sock; |
| int family_mid; |
| int type_mid; |
| int proto_mid; |
| int err; |
| |
| err = socket_family_to_nsos_mid(family, &family_mid); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| err = socket_type_to_nsos_mid(type, &type_mid); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| err = socket_proto_to_nsos_mid(proto, &proto_mid); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| fd = zvfs_reserve_fd(); |
| if (fd < 0) { |
| return -1; |
| } |
| |
| sock = k_malloc(sizeof(*sock)); |
| if (!sock) { |
| errno = ENOMEM; |
| goto free_fd; |
| } |
| |
| sock->fd = fd; |
| sock->recv_timeout = K_FOREVER; |
| sock->send_timeout = K_FOREVER; |
| |
| sock->poll.mid.fd = nsos_adapt_socket(family_mid, type_mid, proto_mid); |
| if (sock->poll.mid.fd < 0) { |
| errno = errno_from_nsos_mid(-sock->poll.mid.fd); |
| goto free_sock; |
| } |
| |
| zvfs_finalize_typed_fd(fd, sock, &nsos_socket_fd_op_vtable.fd_vtable, ZVFS_MODE_IFSOCK); |
| |
| return fd; |
| |
| free_sock: |
| k_free(sock); |
| |
| free_fd: |
| zvfs_free_fd(fd); |
| |
| return -1; |
| } |
| |
| static int nsos_adapt_get_zephyr_errno(void) |
| { |
| return errno_from_nsos_mid(nsos_adapt_get_errno()); |
| } |
| |
| static ssize_t nsos_read(void *obj, void *buf, size_t sz) |
| { |
| struct nsos_socket *sock = obj; |
| int ret; |
| |
| ret = nsi_host_read(sock->poll.mid.fd, buf, sz); |
| if (ret < 0) { |
| errno = nsos_adapt_get_zephyr_errno(); |
| } |
| |
| return ret; |
| } |
| |
| static ssize_t nsos_write(void *obj, const void *buf, size_t sz) |
| { |
| struct nsos_socket *sock = obj; |
| int ret; |
| |
| ret = nsi_host_write(sock->poll.mid.fd, buf, sz); |
| if (ret < 0) { |
| errno = nsos_adapt_get_zephyr_errno(); |
| } |
| |
| return ret; |
| } |
| |
| static int nsos_close(void *obj) |
| { |
| struct nsos_socket *sock = obj; |
| int ret; |
| |
| ret = nsi_host_close(sock->poll.mid.fd); |
| if (ret < 0) { |
| errno = nsos_adapt_get_zephyr_errno(); |
| } |
| |
| k_free(sock); |
| |
| return ret; |
| } |
| |
| static void pollcb(struct nsos_mid_pollfd *mid) |
| { |
| struct nsos_socket_poll *poll = CONTAINER_OF(mid, struct nsos_socket_poll, mid); |
| |
| k_poll_signal_raise(&poll->signal, poll->mid.revents); |
| } |
| |
| static int nsos_poll_prepare(struct nsos_socket *sock, struct zsock_pollfd *pfd, |
| struct k_poll_event **pev, struct k_poll_event *pev_end, |
| struct nsos_socket_poll *poll) |
| { |
| unsigned int signaled; |
| int flags; |
| |
| poll->mid.events = pfd->events; |
| poll->mid.revents = 0; |
| poll->mid.cb = pollcb; |
| |
| if (*pev == pev_end) { |
| return -ENOMEM; |
| } |
| |
| k_poll_signal_init(&poll->signal); |
| k_poll_event_init(*pev, K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY, &poll->signal); |
| |
| sys_dlist_append(&nsos_polls, &poll->node); |
| |
| nsos_adapt_poll_add(&poll->mid); |
| |
| /* Let other sockets use another k_poll_event */ |
| (*pev)++; |
| |
| signaled = 0; |
| flags = 0; |
| |
| k_poll_signal_check(&poll->signal, &signaled, &flags); |
| if (!signaled) { |
| return 0; |
| } |
| |
| /* Events are ready, don't wait */ |
| return -EALREADY; |
| } |
| |
| static int nsos_poll_update(struct nsos_socket *sock, struct zsock_pollfd *pfd, |
| struct k_poll_event **pev, struct nsos_socket_poll *poll) |
| { |
| unsigned int signaled; |
| int flags; |
| |
| (*pev)++; |
| |
| signaled = 0; |
| flags = 0; |
| |
| if (!sys_dnode_is_linked(&poll->node)) { |
| nsos_adapt_poll_update(&poll->mid); |
| return 0; |
| } |
| |
| nsos_adapt_poll_remove(&poll->mid); |
| sys_dlist_remove(&poll->node); |
| |
| k_poll_signal_check(&poll->signal, &signaled, &flags); |
| if (!signaled) { |
| return 0; |
| } |
| |
| pfd->revents = flags; |
| |
| return 0; |
| } |
| |
| static int nsos_ioctl(void *obj, unsigned int request, va_list args) |
| { |
| struct nsos_socket *sock = obj; |
| |
| switch (request) { |
| case ZFD_IOCTL_POLL_PREPARE: { |
| struct zsock_pollfd *pfd; |
| struct k_poll_event **pev; |
| struct k_poll_event *pev_end; |
| |
| pfd = va_arg(args, struct zsock_pollfd *); |
| pev = va_arg(args, struct k_poll_event **); |
| pev_end = va_arg(args, struct k_poll_event *); |
| |
| return nsos_poll_prepare(obj, pfd, pev, pev_end, &sock->poll); |
| } |
| |
| case ZFD_IOCTL_POLL_UPDATE: { |
| struct zsock_pollfd *pfd; |
| struct k_poll_event **pev; |
| |
| pfd = va_arg(args, struct zsock_pollfd *); |
| pev = va_arg(args, struct k_poll_event **); |
| |
| return nsos_poll_update(obj, pfd, pev, &sock->poll); |
| } |
| |
| case ZFD_IOCTL_POLL_OFFLOAD: |
| return -EOPNOTSUPP; |
| |
| case F_GETFL: { |
| int flags; |
| |
| flags = nsos_adapt_fcntl_getfl(sock->poll.mid.fd); |
| |
| return fl_from_nsos_mid(flags); |
| } |
| |
| case F_SETFL: { |
| int flags = va_arg(args, int); |
| int ret; |
| |
| ret = fl_to_nsos_mid_strict(flags); |
| if (ret < 0) { |
| return -errno_from_nsos_mid(-ret); |
| } |
| |
| ret = nsos_adapt_fcntl_setfl(sock->poll.mid.fd, flags); |
| |
| return -errno_from_nsos_mid(-ret); |
| } |
| |
| case ZFD_IOCTL_FIONREAD: { |
| int *avail = va_arg(args, int *); |
| int ret; |
| |
| ret = nsos_adapt_fionread(sock->poll.mid.fd, avail); |
| |
| return -errno_from_nsos_mid(-ret); |
| } |
| } |
| |
| return -EINVAL; |
| } |
| |
| static int sockaddr_to_nsos_mid(const struct sockaddr *addr, socklen_t addrlen, |
| struct nsos_mid_sockaddr **addr_mid, size_t *addrlen_mid) |
| { |
| if (!addr || !addrlen) { |
| *addr_mid = NULL; |
| *addrlen_mid = 0; |
| |
| return 0; |
| } |
| |
| switch (addr->sa_family) { |
| case AF_INET: { |
| const struct sockaddr_in *addr_in = |
| (const struct sockaddr_in *)addr; |
| struct nsos_mid_sockaddr_in *addr_in_mid = |
| (struct nsos_mid_sockaddr_in *)*addr_mid; |
| |
| if (addrlen < sizeof(*addr_in)) { |
| return -NSOS_MID_EINVAL; |
| } |
| |
| addr_in_mid->sin_family = NSOS_MID_AF_INET; |
| addr_in_mid->sin_port = addr_in->sin_port; |
| addr_in_mid->sin_addr = addr_in->sin_addr.s_addr; |
| |
| *addrlen_mid = sizeof(*addr_in_mid); |
| |
| return 0; |
| } |
| case AF_INET6: { |
| const struct sockaddr_in6 *addr_in = |
| (const struct sockaddr_in6 *)addr; |
| struct nsos_mid_sockaddr_in6 *addr_in_mid = |
| (struct nsos_mid_sockaddr_in6 *)*addr_mid; |
| |
| if (addrlen < sizeof(*addr_in)) { |
| return -NSOS_MID_EINVAL; |
| } |
| |
| addr_in_mid->sin6_family = NSOS_MID_AF_INET6; |
| addr_in_mid->sin6_port = addr_in->sin6_port; |
| memcpy(addr_in_mid->sin6_addr, addr_in->sin6_addr.s6_addr, |
| sizeof(addr_in_mid->sin6_addr)); |
| addr_in_mid->sin6_scope_id = addr_in->sin6_scope_id; |
| |
| *addrlen_mid = sizeof(*addr_in_mid); |
| |
| return 0; |
| } |
| case AF_UNIX: { |
| const struct sockaddr_un *addr_un = |
| (const struct sockaddr_un *)addr; |
| struct nsos_mid_sockaddr_un *addr_un_mid = |
| (struct nsos_mid_sockaddr_un *)*addr_mid; |
| |
| if (addrlen < sizeof(*addr_un)) { |
| return -NSOS_MID_EINVAL; |
| } |
| |
| addr_un_mid->sun_family = NSOS_MID_AF_UNIX; |
| memcpy(addr_un_mid->sun_path, addr_un->sun_path, |
| sizeof(addr_un_mid->sun_path)); |
| |
| *addrlen_mid = sizeof(*addr_un_mid); |
| |
| return 0; |
| } |
| case AF_PACKET: { |
| const struct sockaddr_ll *addr_ll = |
| (const struct sockaddr_ll *)addr; |
| struct nsos_mid_sockaddr_ll *addr_ll_mid = |
| (struct nsos_mid_sockaddr_ll *)*addr_mid; |
| |
| if (addrlen < sizeof(*addr_ll)) { |
| return -NSOS_MID_EINVAL; |
| } |
| |
| addr_ll_mid->sll_family = NSOS_MID_AF_UNIX; |
| addr_ll_mid->sll_protocol = addr_ll->sll_protocol; |
| addr_ll_mid->sll_ifindex = addr_ll->sll_ifindex; |
| addr_ll_mid->sll_hatype = addr_ll->sll_hatype; |
| addr_ll_mid->sll_pkttype = addr_ll->sll_pkttype; |
| addr_ll_mid->sll_halen = addr_ll->sll_halen; |
| memcpy(addr_ll_mid->sll_addr, addr_ll->sll_addr, |
| sizeof(addr_ll->sll_addr)); |
| |
| *addrlen_mid = sizeof(*addr_ll_mid); |
| |
| return 0; |
| } |
| } |
| |
| return -NSOS_MID_EINVAL; |
| } |
| |
| static int sockaddr_from_nsos_mid(struct sockaddr *addr, socklen_t *addrlen, |
| const struct nsos_mid_sockaddr *addr_mid, size_t addrlen_mid) |
| { |
| if (!addr || !addrlen) { |
| return 0; |
| } |
| |
| switch (addr_mid->sa_family) { |
| case NSOS_MID_AF_INET: { |
| const struct nsos_mid_sockaddr_in *addr_in_mid = |
| (const struct nsos_mid_sockaddr_in *)addr_mid; |
| struct sockaddr_in addr_in; |
| |
| addr_in.sin_family = AF_INET; |
| addr_in.sin_port = addr_in_mid->sin_port; |
| addr_in.sin_addr.s_addr = addr_in_mid->sin_addr; |
| |
| memcpy(addr, &addr_in, MIN(*addrlen, sizeof(addr_in))); |
| *addrlen = sizeof(addr_in); |
| |
| return 0; |
| } |
| case NSOS_MID_AF_INET6: { |
| const struct nsos_mid_sockaddr_in6 *addr_in_mid = |
| (const struct nsos_mid_sockaddr_in6 *)addr_mid; |
| struct sockaddr_in6 addr_in; |
| |
| addr_in.sin6_family = AF_INET6; |
| addr_in.sin6_port = addr_in_mid->sin6_port; |
| memcpy(addr_in.sin6_addr.s6_addr, addr_in_mid->sin6_addr, |
| sizeof(addr_in.sin6_addr.s6_addr)); |
| addr_in.sin6_scope_id = addr_in_mid->sin6_scope_id; |
| |
| memcpy(addr, &addr_in, MIN(*addrlen, sizeof(addr_in))); |
| *addrlen = sizeof(addr_in); |
| |
| return 0; |
| } |
| } |
| |
| return -NSOS_MID_EINVAL; |
| } |
| |
| static int nsos_wait_for_poll(struct nsos_socket *sock, int events, |
| k_timeout_t timeout) |
| { |
| struct zsock_pollfd pfd = { |
| .fd = sock->fd, |
| .events = events, |
| }; |
| struct k_poll_event poll_events[1]; |
| struct k_poll_event *pev = poll_events; |
| struct k_poll_event *pev_end = poll_events + ARRAY_SIZE(poll_events); |
| struct nsos_socket_poll socket_poll = {}; |
| int ret; |
| |
| ret = nsos_adapt_dup(sock->poll.mid.fd); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| socket_poll.mid.fd = ret; |
| |
| ret = nsos_poll_prepare(sock, &pfd, &pev, pev_end, &socket_poll); |
| if (ret == -EALREADY) { |
| ret = 0; |
| goto poll_update; |
| } else if (ret < 0) { |
| goto close_dup; |
| } |
| |
| ret = k_poll(poll_events, ARRAY_SIZE(poll_events), timeout); |
| if (ret != 0 && ret != -EAGAIN && ret != -EINTR) { |
| goto poll_update; |
| } |
| |
| ret = 0; |
| |
| poll_update: |
| pev = poll_events; |
| nsos_poll_update(sock, &pfd, &pev, &socket_poll); |
| |
| close_dup: |
| nsi_host_close(socket_poll.mid.fd); |
| |
| return_ret: |
| if (ret < 0) { |
| return -errno_to_nsos_mid(-ret); |
| } |
| |
| return 0; |
| } |
| |
| static int nsos_poll_if_blocking(struct nsos_socket *sock, int events, |
| k_timeout_t timeout, int flags) |
| { |
| int sock_flags; |
| bool non_blocking; |
| |
| if (flags & ZSOCK_MSG_DONTWAIT) { |
| non_blocking = true; |
| } else { |
| sock_flags = nsos_adapt_fcntl_getfl(sock->poll.mid.fd); |
| non_blocking = sock_flags & NSOS_MID_O_NONBLOCK; |
| } |
| |
| if (!non_blocking) { |
| return nsos_wait_for_poll(sock, events, timeout); |
| } |
| |
| return 0; |
| } |
| |
| static int nsos_bind(void *obj, const struct sockaddr *addr, socklen_t addrlen) |
| { |
| struct nsos_socket *sock = obj; |
| struct nsos_mid_sockaddr_storage addr_storage_mid; |
| struct nsos_mid_sockaddr *addr_mid = (struct nsos_mid_sockaddr *)&addr_storage_mid; |
| size_t addrlen_mid; |
| int ret; |
| |
| ret = sockaddr_to_nsos_mid(addr, addrlen, &addr_mid, &addrlen_mid); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| ret = nsos_adapt_bind(sock->poll.mid.fd, addr_mid, addrlen_mid); |
| |
| return_ret: |
| if (ret < 0) { |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| static int nsos_connect_blocking(struct nsos_socket *sock, |
| struct nsos_mid_sockaddr *addr_mid, |
| size_t addrlen_mid, |
| int fcntl_flags) |
| { |
| int clear_nonblock_ret; |
| int ret; |
| |
| ret = nsos_adapt_fcntl_setfl(sock->poll.mid.fd, fcntl_flags | NSOS_MID_O_NONBLOCK); |
| if (ret < 0) { |
| return ret; |
| } |
| |
| ret = nsos_adapt_connect(sock->poll.mid.fd, addr_mid, addrlen_mid); |
| if (ret == -NSOS_MID_EINPROGRESS) { |
| int so_err; |
| size_t so_err_len = sizeof(so_err); |
| |
| ret = nsos_wait_for_poll(sock, ZSOCK_POLLOUT, sock->send_timeout); |
| if (ret < 0) { |
| goto clear_nonblock; |
| } |
| |
| ret = nsos_adapt_getsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_ERROR, &so_err, &so_err_len); |
| if (ret < 0) { |
| goto clear_nonblock; |
| } |
| |
| ret = so_err; |
| } |
| |
| clear_nonblock: |
| clear_nonblock_ret = nsos_adapt_fcntl_setfl(sock->poll.mid.fd, fcntl_flags); |
| if (clear_nonblock_ret < 0) { |
| LOG_ERR("Failed to clear O_NONBLOCK: %d", clear_nonblock_ret); |
| } |
| |
| return ret; |
| } |
| |
| static int nsos_connect(void *obj, const struct sockaddr *addr, socklen_t addrlen) |
| { |
| struct nsos_socket *sock = obj; |
| struct nsos_mid_sockaddr_storage addr_storage_mid; |
| struct nsos_mid_sockaddr *addr_mid = (struct nsos_mid_sockaddr *)&addr_storage_mid; |
| size_t addrlen_mid; |
| int flags; |
| int ret; |
| |
| ret = sockaddr_to_nsos_mid(addr, addrlen, &addr_mid, &addrlen_mid); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| flags = nsos_adapt_fcntl_getfl(sock->poll.mid.fd); |
| |
| if (flags & NSOS_MID_O_NONBLOCK) { |
| ret = nsos_adapt_connect(sock->poll.mid.fd, addr_mid, addrlen_mid); |
| } else { |
| ret = nsos_connect_blocking(sock, addr_mid, addrlen_mid, flags); |
| } |
| |
| return_ret: |
| if (ret < 0) { |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| static int nsos_listen(void *obj, int backlog) |
| { |
| struct nsos_socket *sock = obj; |
| int ret; |
| |
| ret = nsos_adapt_listen(sock->poll.mid.fd, backlog); |
| if (ret < 0) { |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| static int nsos_accept(void *obj, struct sockaddr *addr, socklen_t *addrlen) |
| { |
| struct nsos_socket *accept_sock = obj; |
| struct nsos_mid_sockaddr_storage addr_storage_mid; |
| struct nsos_mid_sockaddr *addr_mid = (struct nsos_mid_sockaddr *)&addr_storage_mid; |
| size_t addrlen_mid = sizeof(addr_storage_mid); |
| int adapt_fd; |
| int zephyr_fd; |
| struct nsos_socket *conn_sock; |
| int ret; |
| |
| ret = nsos_poll_if_blocking(accept_sock, ZSOCK_POLLIN, |
| accept_sock->recv_timeout, 0); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| ret = nsos_adapt_accept(accept_sock->poll.mid.fd, addr_mid, &addrlen_mid); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| adapt_fd = ret; |
| |
| ret = sockaddr_from_nsos_mid(addr, addrlen, addr_mid, addrlen_mid); |
| if (ret < 0) { |
| goto close_adapt_fd; |
| } |
| |
| zephyr_fd = zvfs_reserve_fd(); |
| if (zephyr_fd < 0) { |
| ret = -errno_to_nsos_mid(-zephyr_fd); |
| goto close_adapt_fd; |
| } |
| |
| conn_sock = k_malloc(sizeof(*conn_sock)); |
| if (!conn_sock) { |
| ret = -NSOS_MID_ENOMEM; |
| goto free_zephyr_fd; |
| } |
| |
| conn_sock->fd = zephyr_fd; |
| conn_sock->poll.mid.fd = adapt_fd; |
| |
| zvfs_finalize_typed_fd(zephyr_fd, conn_sock, &nsos_socket_fd_op_vtable.fd_vtable, |
| ZVFS_MODE_IFSOCK); |
| |
| return zephyr_fd; |
| |
| free_zephyr_fd: |
| zvfs_free_fd(zephyr_fd); |
| |
| close_adapt_fd: |
| nsi_host_close(adapt_fd); |
| |
| return_ret: |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| static ssize_t nsos_sendto(void *obj, const void *buf, size_t len, int flags, |
| const struct sockaddr *addr, socklen_t addrlen) |
| { |
| struct nsos_socket *sock = obj; |
| struct nsos_mid_sockaddr_storage addr_storage_mid; |
| struct nsos_mid_sockaddr *addr_mid = (struct nsos_mid_sockaddr *)&addr_storage_mid; |
| size_t addrlen_mid = sizeof(addr_storage_mid); |
| int flags_mid; |
| int ret; |
| |
| ret = socket_flags_to_nsos_mid(flags); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| flags_mid = ret; |
| |
| ret = sockaddr_to_nsos_mid(addr, addrlen, &addr_mid, &addrlen_mid); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| ret = nsos_poll_if_blocking(sock, ZSOCK_POLLOUT, sock->send_timeout, flags); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| ret = nsos_adapt_sendto(sock->poll.mid.fd, buf, len, flags_mid, |
| addr_mid, addrlen_mid); |
| |
| return_ret: |
| if (ret < 0) { |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| static ssize_t nsos_sendmsg(void *obj, const struct msghdr *msg, int flags) |
| { |
| struct nsos_socket *sock = obj; |
| struct nsos_mid_sockaddr_storage addr_storage_mid; |
| struct nsos_mid_sockaddr *addr_mid = (struct nsos_mid_sockaddr *)&addr_storage_mid; |
| size_t addrlen_mid = sizeof(addr_storage_mid); |
| struct nsos_mid_msghdr msg_mid; |
| struct nsos_mid_iovec *msg_iov; |
| int flags_mid; |
| int ret; |
| |
| ret = socket_flags_to_nsos_mid(flags); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| flags_mid = ret; |
| |
| ret = sockaddr_to_nsos_mid(msg->msg_name, msg->msg_namelen, &addr_mid, &addrlen_mid); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| msg_iov = k_calloc(msg->msg_iovlen, sizeof(*msg_iov)); |
| if (!msg_iov) { |
| ret = -NSOS_MID_ENOMEM; |
| goto return_ret; |
| } |
| |
| for (size_t i = 0; i < msg->msg_iovlen; i++) { |
| msg_iov[i].iov_base = msg->msg_iov[i].iov_base; |
| msg_iov[i].iov_len = msg->msg_iov[i].iov_len; |
| } |
| |
| msg_mid.msg_name = addr_mid; |
| msg_mid.msg_namelen = addrlen_mid; |
| msg_mid.msg_iov = msg_iov; |
| msg_mid.msg_iovlen = msg->msg_iovlen; |
| msg_mid.msg_control = NULL; |
| msg_mid.msg_controllen = 0; |
| msg_mid.msg_flags = 0; |
| |
| ret = nsos_poll_if_blocking(sock, ZSOCK_POLLOUT, sock->send_timeout, flags); |
| if (ret < 0) { |
| goto free_msg_iov; |
| } |
| |
| ret = nsos_adapt_sendmsg(sock->poll.mid.fd, &msg_mid, flags_mid); |
| |
| free_msg_iov: |
| k_free(msg_iov); |
| |
| return_ret: |
| if (ret < 0) { |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| static ssize_t nsos_recvfrom(void *obj, void *buf, size_t len, int flags, |
| struct sockaddr *addr, socklen_t *addrlen) |
| { |
| struct nsos_socket *sock = obj; |
| struct nsos_mid_sockaddr_storage addr_storage_mid; |
| struct nsos_mid_sockaddr *addr_mid = (struct nsos_mid_sockaddr *)&addr_storage_mid; |
| size_t addrlen_mid = sizeof(addr_storage_mid); |
| int flags_mid; |
| int ret; |
| |
| ret = socket_flags_to_nsos_mid(flags); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| flags_mid = ret; |
| |
| ret = nsos_poll_if_blocking(sock, ZSOCK_POLLIN, sock->recv_timeout, flags); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| ret = nsos_adapt_recvfrom(sock->poll.mid.fd, buf, len, flags_mid, |
| addr_mid, &addrlen_mid); |
| if (ret < 0) { |
| goto return_ret; |
| } |
| |
| sockaddr_from_nsos_mid(addr, addrlen, addr_mid, addrlen_mid); |
| |
| return_ret: |
| if (ret < 0) { |
| errno = errno_from_nsos_mid(-ret); |
| return -1; |
| } |
| |
| return ret; |
| } |
| |
| static ssize_t nsos_recvmsg(void *obj, struct msghdr *msg, int flags) |
| { |
| errno = ENOTSUP; |
| return -1; |
| } |
| |
| static int socket_type_from_nsos_mid(int type_mid, int *type) |
| { |
| switch (type_mid) { |
| case NSOS_MID_SOCK_STREAM: |
| *type = SOCK_STREAM; |
| break; |
| case NSOS_MID_SOCK_DGRAM: |
| *type = SOCK_DGRAM; |
| break; |
| case NSOS_MID_SOCK_RAW: |
| *type = SOCK_RAW; |
| break; |
| default: |
| return -NSOS_MID_ESOCKTNOSUPPORT; |
| } |
| |
| return 0; |
| } |
| |
| static int socket_proto_from_nsos_mid(int proto_mid, int *proto) |
| { |
| switch (proto_mid) { |
| case NSOS_MID_IPPROTO_IP: |
| *proto = IPPROTO_IP; |
| break; |
| case NSOS_MID_IPPROTO_ICMP: |
| *proto = IPPROTO_ICMP; |
| break; |
| case NSOS_MID_IPPROTO_IGMP: |
| *proto = IPPROTO_IGMP; |
| break; |
| case NSOS_MID_IPPROTO_IPIP: |
| *proto = IPPROTO_IPIP; |
| break; |
| case NSOS_MID_IPPROTO_TCP: |
| *proto = IPPROTO_TCP; |
| break; |
| case NSOS_MID_IPPROTO_UDP: |
| *proto = IPPROTO_UDP; |
| break; |
| case NSOS_MID_IPPROTO_IPV6: |
| *proto = IPPROTO_IPV6; |
| break; |
| case NSOS_MID_IPPROTO_RAW: |
| *proto = IPPROTO_RAW; |
| break; |
| case NSOS_MID_IPPROTO_ETH_P_ALL: |
| *proto = htons(IPPROTO_ETH_P_ALL); |
| break; |
| default: |
| return -NSOS_MID_EPROTONOSUPPORT; |
| } |
| |
| return 0; |
| } |
| |
| static int socket_family_from_nsos_mid(int family_mid, int *family) |
| { |
| switch (family_mid) { |
| case NSOS_MID_AF_UNSPEC: |
| *family = AF_UNSPEC; |
| break; |
| case NSOS_MID_AF_INET: |
| *family = AF_INET; |
| break; |
| case NSOS_MID_AF_INET6: |
| *family = AF_INET6; |
| break; |
| case NSOS_MID_AF_UNIX: |
| *family = AF_UNIX; |
| break; |
| case NSOS_MID_AF_PACKET: |
| *family = AF_PACKET; |
| break; |
| default: |
| return -NSOS_MID_EAFNOSUPPORT; |
| } |
| |
| return 0; |
| } |
| |
| static int nsos_getsockopt_int(struct nsos_socket *sock, int nsos_mid_level, int nsos_mid_optname, |
| void *optval, socklen_t *optlen) |
| { |
| size_t nsos_mid_optlen = sizeof(int); |
| int err; |
| |
| if (*optlen != sizeof(int)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| err = nsos_adapt_getsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_KEEPALIVE, optval, &nsos_mid_optlen); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| *optlen = nsos_mid_optlen; |
| |
| return 0; |
| } |
| |
| static int nsos_getsockopt(void *obj, int level, int optname, |
| void *optval, socklen_t *optlen) |
| { |
| struct nsos_socket *sock = obj; |
| |
| switch (level) { |
| case SOL_SOCKET: |
| switch (optname) { |
| case SO_ERROR: { |
| int nsos_mid_err; |
| int err; |
| |
| if (*optlen != sizeof(int)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| err = nsos_adapt_getsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_ERROR, &nsos_mid_err, NULL); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| *(int *)optval = errno_from_nsos_mid(nsos_mid_err); |
| |
| return 0; |
| } |
| case SO_TYPE: { |
| int nsos_mid_type; |
| int err; |
| |
| if (*optlen != sizeof(nsos_mid_type)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| err = nsos_adapt_getsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_TYPE, &nsos_mid_type, NULL); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| err = socket_type_from_nsos_mid(nsos_mid_type, optval); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| return 0; |
| } |
| case SO_PROTOCOL: { |
| int nsos_mid_proto; |
| int err; |
| |
| if (*optlen != sizeof(nsos_mid_proto)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| err = nsos_adapt_getsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_PROTOCOL, &nsos_mid_proto, NULL); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| err = socket_proto_from_nsos_mid(nsos_mid_proto, optval); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| return 0; |
| } |
| case SO_DOMAIN: { |
| int nsos_mid_family; |
| int err; |
| |
| if (*optlen != sizeof(nsos_mid_family)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| err = nsos_adapt_getsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_DOMAIN, &nsos_mid_family, NULL); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| err = socket_family_from_nsos_mid(nsos_mid_family, optval); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| return 0; |
| } |
| case SO_RCVBUF: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_RCVBUF, |
| optval, optlen); |
| case SO_SNDBUF: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_SNDBUF, |
| optval, optlen); |
| case SO_REUSEADDR: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_REUSEADDR, |
| optval, optlen); |
| case SO_REUSEPORT: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_REUSEPORT, |
| optval, optlen); |
| case SO_KEEPALIVE: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_KEEPALIVE, |
| optval, optlen); |
| } |
| break; |
| |
| case IPPROTO_TCP: |
| switch (optname) { |
| case TCP_NODELAY: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_NODELAY, |
| optval, optlen); |
| case TCP_KEEPIDLE: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_KEEPIDLE, |
| optval, optlen); |
| case TCP_KEEPINTVL: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_KEEPINTVL, |
| optval, optlen); |
| case TCP_KEEPCNT: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_KEEPCNT, |
| optval, optlen); |
| } |
| break; |
| |
| case IPPROTO_IPV6: |
| switch (optname) { |
| case IPV6_V6ONLY: |
| return nsos_getsockopt_int(sock, |
| NSOS_MID_IPPROTO_IPV6, NSOS_MID_IPV6_V6ONLY, |
| optval, optlen); |
| } |
| break; |
| } |
| |
| errno = EOPNOTSUPP; |
| return -1; |
| } |
| |
| static int nsos_setsockopt_int(struct nsos_socket *sock, int nsos_mid_level, int nsos_mid_optname, |
| const void *optval, socklen_t optlen) |
| { |
| int err; |
| |
| if (optlen != sizeof(int)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| err = nsos_adapt_setsockopt(sock->poll.mid.fd, nsos_mid_level, nsos_mid_optname, |
| optval, optlen); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| static int nsos_setsockopt(void *obj, int level, int optname, |
| const void *optval, socklen_t optlen) |
| { |
| struct nsos_socket *sock = obj; |
| |
| switch (level) { |
| case SOL_SOCKET: |
| switch (optname) { |
| case SO_PRIORITY: { |
| int nsos_mid_priority; |
| int err; |
| |
| if (optlen != sizeof(uint8_t)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| nsos_mid_priority = *(uint8_t *)optval; |
| |
| err = nsos_adapt_setsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_PRIORITY, &nsos_mid_priority, |
| sizeof(nsos_mid_priority)); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| return 0; |
| } |
| case SO_RCVTIMEO: { |
| const struct zsock_timeval *tv = optval; |
| struct nsos_mid_timeval nsos_mid_tv; |
| int err; |
| |
| if (optlen != sizeof(struct zsock_timeval)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| nsos_mid_tv.tv_sec = tv->tv_sec; |
| nsos_mid_tv.tv_usec = tv->tv_usec; |
| |
| err = nsos_adapt_setsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_RCVTIMEO, &nsos_mid_tv, |
| sizeof(nsos_mid_tv)); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| if (tv->tv_sec == 0 && tv->tv_usec == 0) { |
| sock->recv_timeout = K_FOREVER; |
| } else { |
| sock->recv_timeout = K_USEC(tv->tv_sec * 1000000LL + tv->tv_usec); |
| } |
| |
| return 0; |
| } |
| case SO_SNDTIMEO: { |
| const struct zsock_timeval *tv = optval; |
| struct nsos_mid_timeval nsos_mid_tv; |
| int err; |
| |
| if (optlen != sizeof(struct zsock_timeval)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| nsos_mid_tv.tv_sec = tv->tv_sec; |
| nsos_mid_tv.tv_usec = tv->tv_usec; |
| |
| err = nsos_adapt_setsockopt(sock->poll.mid.fd, NSOS_MID_SOL_SOCKET, |
| NSOS_MID_SO_SNDTIMEO, &nsos_mid_tv, |
| sizeof(nsos_mid_tv)); |
| if (err) { |
| errno = errno_from_nsos_mid(-err); |
| return -1; |
| } |
| |
| if (tv->tv_sec == 0 && tv->tv_usec == 0) { |
| sock->send_timeout = K_FOREVER; |
| } else { |
| sock->send_timeout = K_USEC(tv->tv_sec * 1000000LL + tv->tv_usec); |
| } |
| |
| return 0; |
| } |
| case SO_RCVBUF: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_RCVBUF, |
| optval, optlen); |
| case SO_SNDBUF: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_SNDBUF, |
| optval, optlen); |
| case SO_REUSEADDR: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_REUSEADDR, |
| optval, optlen); |
| case SO_REUSEPORT: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_REUSEPORT, |
| optval, optlen); |
| case SO_LINGER: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_LINGER, |
| optval, optlen); |
| case SO_KEEPALIVE: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_SOL_SOCKET, NSOS_MID_SO_KEEPALIVE, |
| optval, optlen); |
| } |
| break; |
| |
| case IPPROTO_TCP: |
| switch (optname) { |
| case TCP_NODELAY: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_NODELAY, |
| optval, optlen); |
| case TCP_KEEPIDLE: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_KEEPIDLE, |
| optval, optlen); |
| case TCP_KEEPINTVL: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_KEEPINTVL, |
| optval, optlen); |
| case TCP_KEEPCNT: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_IPPROTO_TCP, NSOS_MID_TCP_KEEPCNT, |
| optval, optlen); |
| } |
| break; |
| |
| case IPPROTO_IPV6: |
| switch (optname) { |
| case IPV6_V6ONLY: |
| return nsos_setsockopt_int(sock, |
| NSOS_MID_IPPROTO_IPV6, NSOS_MID_IPV6_V6ONLY, |
| optval, optlen); |
| } |
| break; |
| } |
| |
| errno = EOPNOTSUPP; |
| return -1; |
| } |
| |
| static const struct socket_op_vtable nsos_socket_fd_op_vtable = { |
| .fd_vtable = { |
| .read = nsos_read, |
| .write = nsos_write, |
| .close = nsos_close, |
| .ioctl = nsos_ioctl, |
| }, |
| .bind = nsos_bind, |
| .connect = nsos_connect, |
| .listen = nsos_listen, |
| .accept = nsos_accept, |
| .sendto = nsos_sendto, |
| .sendmsg = nsos_sendmsg, |
| .recvfrom = nsos_recvfrom, |
| .recvmsg = nsos_recvmsg, |
| .getsockopt = nsos_getsockopt, |
| .setsockopt = nsos_setsockopt, |
| }; |
| |
| static bool nsos_is_supported(int family, int type, int proto) |
| { |
| int dummy; |
| int err; |
| |
| err = socket_family_to_nsos_mid(family, &dummy); |
| if (err) { |
| return false; |
| } |
| |
| err = socket_type_to_nsos_mid(type, &dummy); |
| if (err) { |
| return false; |
| } |
| |
| err = socket_proto_to_nsos_mid(proto, &dummy); |
| if (err) { |
| return false; |
| } |
| |
| return true; |
| } |
| |
| NET_SOCKET_OFFLOAD_REGISTER(nsos, CONFIG_NET_SOCKETS_OFFLOAD_PRIORITY, AF_UNSPEC, |
| nsos_is_supported, nsos_socket_create); |
| |
| struct zsock_addrinfo_wrap { |
| struct zsock_addrinfo addrinfo; |
| struct sockaddr_storage addr_storage; |
| struct nsos_mid_addrinfo *addrinfo_mid; |
| }; |
| |
| /* |
| * (Zephyr) |
| * zsock_addrinfo_wrap |
| * ----------------------- |
| * | zsock_addrinfo | |
| * ----------------------- (trampoline) |
| * | sockaddr_storage | nsos_addrinfo_wrap |
| * ----------------------- ----------------------------- |
| * | nsos_mid_addrinfo * | -> | nsos_mid_addrinfo | |
| * ----------------------- ----------------------------- |
| * | nsos_mid_sockaddr_storage | |
| * ----------------------------- (Linux host) |
| * | addrinfo * | -> addrinfo |
| * ----------------------------- |
| */ |
| |
| static int addrinfo_from_nsos_mid(struct nsos_mid_addrinfo *nsos_res, |
| struct zsock_addrinfo **res) |
| { |
| struct zsock_addrinfo_wrap *res_wraps; |
| size_t idx_res = 0; |
| size_t n_res = 0; |
| |
| for (struct nsos_mid_addrinfo *res_p = nsos_res; res_p; res_p = res_p->ai_next) { |
| n_res++; |
| } |
| |
| if (n_res == 0) { |
| return 0; |
| } |
| |
| res_wraps = k_calloc(n_res, sizeof(*res_wraps)); |
| if (!res_wraps) { |
| return -ENOMEM; |
| } |
| |
| for (struct nsos_mid_addrinfo *res_p = nsos_res; res_p; res_p = res_p->ai_next, idx_res++) { |
| struct zsock_addrinfo_wrap *wrap = &res_wraps[idx_res]; |
| |
| wrap->addrinfo_mid = res_p; |
| |
| wrap->addrinfo.ai_flags = res_p->ai_flags; |
| wrap->addrinfo.ai_family = res_p->ai_family; |
| wrap->addrinfo.ai_socktype = res_p->ai_socktype; |
| wrap->addrinfo.ai_protocol = res_p->ai_protocol; |
| |
| wrap->addrinfo.ai_addr = |
| (struct sockaddr *)&wrap->addr_storage; |
| wrap->addrinfo.ai_addrlen = sizeof(wrap->addr_storage); |
| |
| sockaddr_from_nsos_mid(wrap->addrinfo.ai_addr, &wrap->addrinfo.ai_addrlen, |
| res_p->ai_addr, res_p->ai_addrlen); |
| |
| wrap->addrinfo.ai_canonname = |
| res_p->ai_canonname ? strdup(res_p->ai_canonname) : NULL; |
| wrap->addrinfo.ai_next = &wrap[1].addrinfo; |
| } |
| |
| res_wraps[n_res - 1].addrinfo.ai_next = NULL; |
| |
| *res = &res_wraps->addrinfo; |
| |
| return 0; |
| } |
| |
| static int nsos_getaddrinfo(const char *node, const char *service, |
| const struct zsock_addrinfo *hints, |
| struct zsock_addrinfo **res) |
| { |
| struct nsos_mid_addrinfo hints_mid; |
| struct nsos_mid_addrinfo *res_mid; |
| int system_errno; |
| int ret; |
| |
| if (!res) { |
| return -EINVAL; |
| } |
| |
| if (hints) { |
| hints_mid.ai_flags = hints->ai_flags; |
| hints_mid.ai_family = hints->ai_family; |
| hints_mid.ai_socktype = hints->ai_socktype; |
| hints_mid.ai_protocol = hints->ai_protocol; |
| } |
| |
| ret = nsos_adapt_getaddrinfo(node, service, |
| hints ? &hints_mid : NULL, |
| &res_mid, |
| &system_errno); |
| if (ret < 0) { |
| if (ret == NSOS_MID_EAI_SYSTEM) { |
| errno = errno_from_nsos_mid(system_errno); |
| } |
| |
| return eai_from_nsos_mid(ret); |
| } |
| |
| ret = addrinfo_from_nsos_mid(res_mid, res); |
| if (ret < 0) { |
| errno = -ret; |
| return DNS_EAI_SYSTEM; |
| } |
| |
| return ret; |
| } |
| |
| static void nsos_freeaddrinfo(struct zsock_addrinfo *res) |
| { |
| struct zsock_addrinfo_wrap *wrap = |
| CONTAINER_OF(res, struct zsock_addrinfo_wrap, addrinfo); |
| |
| for (struct zsock_addrinfo *res_p = res; res_p; res_p = res_p->ai_next) { |
| free(res_p->ai_canonname); |
| } |
| |
| nsos_adapt_freeaddrinfo(wrap->addrinfo_mid); |
| k_free(wrap); |
| } |
| |
| static const struct socket_dns_offload nsos_dns_ops = { |
| .getaddrinfo = nsos_getaddrinfo, |
| .freeaddrinfo = nsos_freeaddrinfo, |
| }; |
| |
| static void nsos_isr(const void *obj) |
| { |
| struct nsos_socket_poll *poll; |
| |
| SYS_DLIST_FOR_EACH_CONTAINER(&nsos_polls, poll, node) { |
| if (poll->mid.revents) { |
| poll->mid.cb(&poll->mid); |
| } |
| } |
| } |
| |
| static int nsos_socket_offload_init(const struct device *arg) |
| { |
| ARG_UNUSED(arg); |
| |
| IRQ_CONNECT(NSOS_IRQ, NSOS_IRQ_PRIORITY, |
| nsos_isr, NULL, NSOS_IRQ_FLAGS); |
| irq_enable(NSOS_IRQ); |
| |
| return 0; |
| } |
| |
| static void nsos_iface_api_init(struct net_if *iface) |
| { |
| iface->if_dev->socket_offload = nsos_socket_create; |
| |
| socket_offload_dns_register(&nsos_dns_ops); |
| } |
| |
| static int nsos_iface_enable(const struct net_if *iface, bool enabled) |
| { |
| ARG_UNUSED(iface); |
| ARG_UNUSED(enabled); |
| return 0; |
| } |
| |
| static struct offloaded_if_api nsos_iface_offload_api = { |
| .iface_api.init = nsos_iface_api_init, |
| .enable = nsos_iface_enable, |
| }; |
| |
| NET_DEVICE_OFFLOAD_INIT(nsos_socket, "nsos_socket", |
| nsos_socket_offload_init, |
| NULL, |
| NULL, NULL, |
| 0, &nsos_iface_offload_api, NET_ETH_MTU); |