diff --git a/arch/sim/src/Makefile b/arch/sim/src/Makefile index d238853df6fe6..506602dddf642 100644 --- a/arch/sim/src/Makefile +++ b/arch/sim/src/Makefile @@ -131,7 +131,10 @@ ifeq ($(CONFIG_FS_LARGEFILE),y) endif HOSTSRCS = sim_hostirq.c sim_hostmemory.c sim_hostmisc.c sim_hosttime.c sim_hostuart.c -HOSTSRCS += sim_hostfs.c sim_errno.c +HOSTSRCS += sim_hostfs.c sim_hostsocket.c sim_errno.c +ifeq ($(CONFIG_HOST_LINUX),y) + HOSTSRCS += sim_hostepoll.c +endif hostfs.h: $(TOPDIR)/include/nuttx/fs/hostfs.h @echo "CP: $<" diff --git a/arch/sim/src/nuttx-names.in b/arch/sim/src/nuttx-names.in index 9a47af018a863..2af82ad9e51a7 100644 --- a/arch/sim/src/nuttx-names.in +++ b/arch/sim/src/nuttx-names.in @@ -50,6 +50,9 @@ NXSYMBOLS(closedir) NXSYMBOLS(connect) NXSYMBOLS(dlsym) NXSYMBOLS(dup) +NXSYMBOLS(epoll_create1) +NXSYMBOLS(epoll_ctl) +NXSYMBOLS(epoll_wait) NXSYMBOLS(exit) NXSYMBOLS(fchmod) NXSYMBOLS(fchown) @@ -59,8 +62,11 @@ NXSYMBOLS(fdopen) NXSYMBOLS(feof) NXSYMBOLS(fopen) NXSYMBOLS(fprintf) +NXSYMBOLS(fgets) NXSYMBOLS(fread) NXSYMBOLS(free) +NXSYMBOLS(freeaddrinfo) +NXSYMBOLS(freeifaddrs) NXSYMBOLS(fseek) NXSYMBOLS(fstat) NXSYMBOLS(fsync) @@ -71,9 +77,14 @@ NXSYMBOLS(fwrite) NXSYMBOLS(getpeername) NXSYMBOLS(getsockname) NXSYMBOLS(getenv) +NXSYMBOLS(getaddrinfo) +NXSYMBOLS(getifaddrs) NXSYMBOLS(getpid) NXSYMBOLS(getsockopt) +NXSYMBOLS(htons) NXSYMBOLS(if_nametoindex) +NXSYMBOLS(inet_ntop) +NXSYMBOLS(inet_pton) NXSYMBOLS(ioctl) NXSYMBOLS(listen) NXSYMBOLS(longjmp) @@ -83,11 +94,13 @@ NXSYMBOLS(malloc_size) NXSYMBOLS(malloc_usable_size) NXSYMBOLS(memchr) NXSYMBOLS(memcpy) +NXSYMBOLS(memset) NXSYMBOLS(memfd_create) NXSYMBOLS(mkdir) NXSYMBOLS(mmap) NXSYMBOLS(mprotect) NXSYMBOLS(munmap) +NXSYMBOLS(ntohs) NXSYMBOLS(open) NXSYMBOLS(opendir) NXSYMBOLS(pclose) @@ -97,6 +110,7 @@ NXSYMBOLS(poll) NXSYMBOLS(popen) NXSYMBOLS(posix_memalign) NXSYMBOLS(posix_spawn) +NXSYMBOLS(pread) NXSYMBOLS(pthread_attr_init) NXSYMBOLS(pthread_attr_setstack) NXSYMBOLS(pthread_attr_destroy) @@ -120,18 +134,22 @@ NXSYMBOLS(pthread_setspecific) #endif NXSYMBOLS(pthread_self) NXSYMBOLS(pthread_sigmask) +NXSYMBOLS(pwrite) NXSYMBOLS(puts) NXSYMBOLS(read) NXSYMBOLS(readdir) NXSYMBOLS(readlink) NXSYMBOLS(readv) NXSYMBOLS(realloc) +NXSYMBOLS(recv) NXSYMBOLS(recvfrom) +NXSYMBOLS(remove) NXSYMBOLS(rename) NXSYMBOLS(rewinddir) NXSYMBOLS(rmdir) NXSYMBOLS(sched_yield) NXSYMBOLS(select) +NXSYMBOLS(send) NXSYMBOLS(sendmsg) NXSYMBOLS(sendto) NXSYMBOLS(setbuf) diff --git a/arch/sim/src/sim/posix/sim_hostepoll.c b/arch/sim/src/sim/posix/sim_hostepoll.c new file mode 100644 index 0000000000000..09fd61d113caf --- /dev/null +++ b/arch/sim/src/sim/posix/sim_hostepoll.c @@ -0,0 +1,116 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_hostepoll.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include "sim_internal.h" +#include "sim_hostepoll.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int host_epoll_create1(int flags) +{ + int hostflags = 0; + int ret; + + if (flags & NUTTX_EPOLL_CLOEXEC) + { + hostflags |= EPOLL_CLOEXEC; + flags &= ~NUTTX_EPOLL_CLOEXEC; + } + + if (flags != 0) + { + return -EINVAL; + } + + ret = epoll_create1(hostflags); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_epoll_ctl(int epoll_fd, int op, int fd, uint32_t events, + uintptr_t data) +{ + struct epoll_event event; + struct epoll_event *eventptr = NULL; + int ret; + + memset(&event, 0, sizeof(event)); + + if (op != EPOLL_CTL_DEL) + { + event.events = events; + event.data.ptr = (void *)data; + eventptr = &event; + } + + ret = epoll_ctl(epoll_fd, op, fd, eventptr); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_epoll_wait(int epoll_fd, struct host_epoll_event *events, + int maxevents, int timeout_ms) +{ + struct epoll_event *hostevents; + int ret; + int i; + + if (events == NULL || maxevents <= 0 || + (size_t)maxevents > SIZE_MAX / sizeof(*hostevents)) + { + return -EINVAL; + } + + hostevents = malloc(maxevents * sizeof(*hostevents)); + if (hostevents == NULL) + { + return -ENOMEM; + } + + ret = epoll_wait(epoll_fd, hostevents, maxevents, timeout_ms); + if (ret < 0) + { + ret = host_errno_convert(-errno); + goto out; + } + + for (i = 0; i < ret; i++) + { + events[i].events = hostevents[i].events; + events[i].data = (uintptr_t)hostevents[i].data.ptr; + } + +out: + free(hostevents); + return ret; +} diff --git a/arch/sim/src/sim/posix/sim_hostfs.c b/arch/sim/src/sim/posix/sim_hostfs.c index 8716e9fe5123f..5f29a5b2244dd 100644 --- a/arch/sim/src/sim/posix/sim_hostfs.c +++ b/arch/sim/src/sim/posix/sim_hostfs.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "hostfs.h" #include "sim_internal.h" @@ -43,6 +44,189 @@ * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: host_oflags_convert + ****************************************************************************/ + +static int host_oflags_convert(int flags) +{ + int mapflags = 0; + + switch (flags & NUTTX_O_ACCMODE) + { + case NUTTX_O_RDONLY: + mapflags = O_RDONLY; + break; + + case NUTTX_O_WRONLY: + mapflags = O_WRONLY; + break; + + case NUTTX_O_RDWR: + mapflags = O_RDWR; + break; + } + + if (flags & NUTTX_O_APPEND) + { + mapflags |= O_APPEND; + } + + if (flags & NUTTX_O_CREAT) + { + mapflags |= O_CREAT; + } + + if (flags & NUTTX_O_EXCL) + { + mapflags |= O_EXCL; + } + + if (flags & NUTTX_O_TRUNC) + { + mapflags |= O_TRUNC; + } + + if (flags & NUTTX_O_NONBLOCK) + { + mapflags |= O_NONBLOCK; + } + + if (flags & NUTTX_O_SYNC) + { + mapflags |= O_SYNC; + } + +#ifdef O_DIRECT + if (flags & NUTTX_O_DIRECT) + { + mapflags |= O_DIRECT; + } +#endif + + if (flags & NUTTX_O_CLOEXEC) + { + mapflags |= O_CLOEXEC; + } + + if (flags & NUTTX_O_DIRECTORY) + { + mapflags |= O_DIRECTORY; + } + + return mapflags; +} + +/**************************************************************************** + * Name: host_oflags_revert + ****************************************************************************/ + +static int host_oflags_revert(int flags) +{ + int mapflags = 0; + + switch (flags & O_ACCMODE) + { + case O_RDONLY: + mapflags = NUTTX_O_RDONLY; + break; + + case O_WRONLY: + mapflags = NUTTX_O_WRONLY; + break; + + case O_RDWR: + mapflags = NUTTX_O_RDWR; + break; + } + + if (flags & O_APPEND) + { + mapflags |= NUTTX_O_APPEND; + } + + if (flags & O_CREAT) + { + mapflags |= NUTTX_O_CREAT; + } + + if (flags & O_EXCL) + { + mapflags |= NUTTX_O_EXCL; + } + + if (flags & O_TRUNC) + { + mapflags |= NUTTX_O_TRUNC; + } + + if (flags & O_NONBLOCK) + { + mapflags |= NUTTX_O_NONBLOCK; + } + + if (flags & O_SYNC) + { + mapflags |= NUTTX_O_SYNC; + } + +#ifdef O_DIRECT + if (flags & O_DIRECT) + { + mapflags |= NUTTX_O_DIRECT; + } +#endif + + if (flags & O_CLOEXEC) + { + mapflags |= NUTTX_O_CLOEXEC; + } + + if (flags & O_DIRECTORY) + { + mapflags |= NUTTX_O_DIRECTORY; + } + + return mapflags; +} + +/**************************************************************************** + * Name: host_fcntl_cmd_convert + ****************************************************************************/ + +static int host_fcntl_cmd_convert(int cmd) +{ + switch (cmd) + { + case NUTTX_F_DUPFD: + return F_DUPFD; + + case NUTTX_F_GETFD: + return F_GETFD; + + case NUTTX_F_GETFL: + return F_GETFL; + + case NUTTX_F_GETLK: + return F_GETLK; + + case NUTTX_F_SETFD: + return F_SETFD; + + case NUTTX_F_SETFL: + return F_SETFL; + + case NUTTX_F_SETLK: + return F_SETLK; + + case NUTTX_F_SETLKW: + return F_SETLKW; + + default: + return -EINVAL; + } +} + /**************************************************************************** * Name: host_stat_convert ****************************************************************************/ @@ -130,71 +314,7 @@ static void host_stat_convert(struct stat *hostbuf, struct nuttx_stat_s *buf) int host_open(const char *pathname, int flags, int mode) { - int mapflags = 0; - - /* Perform flag mapping */ - - switch (flags & NUTTX_O_ACCMODE) - { - case NUTTX_O_RDONLY: - mapflags = O_RDONLY; - break; - - case NUTTX_O_WRONLY: - mapflags = O_WRONLY; - break; - - case NUTTX_O_RDWR: - mapflags = O_RDWR; - break; - } - - if (flags & NUTTX_O_APPEND) - { - mapflags |= O_APPEND; - } - - if (flags & NUTTX_O_CREAT) - { - mapflags |= O_CREAT; - } - - if (flags & NUTTX_O_EXCL) - { - mapflags |= O_EXCL; - } - - if (flags & NUTTX_O_TRUNC) - { - mapflags |= O_TRUNC; - } - - if (flags & NUTTX_O_NONBLOCK) - { - mapflags |= O_NONBLOCK; - } - - if (flags & NUTTX_O_SYNC) - { - mapflags |= O_SYNC; - } - -#ifdef O_DIRECT - if (flags & NUTTX_O_DIRECT) - { - mapflags |= O_DIRECT; - } -#endif - - if (flags & NUTTX_O_CLOEXEC) - { - mapflags |= O_CLOEXEC; - } - - if (flags & NUTTX_O_DIRECTORY) - { - mapflags |= O_DIRECTORY; - } + int mapflags = host_oflags_convert(flags); int ret = open(pathname, mapflags, mode); if (ret == -1) @@ -256,6 +376,38 @@ nuttx_ssize_t host_write(int fd, const void *buf, nuttx_size_t count) return ret; } +/**************************************************************************** + * Name: host_pread + ****************************************************************************/ + +nuttx_ssize_t host_pread(int fd, void *buf, nuttx_size_t count, + nuttx_off_t offset) +{ + nuttx_ssize_t ret = pread(fd, buf, count, offset); + if (ret == -1) + { + ret = host_errno_convert(-errno); + } + + return ret; +} + +/**************************************************************************** + * Name: host_pwrite + ****************************************************************************/ + +nuttx_ssize_t host_pwrite(int fd, const void *buf, nuttx_size_t count, + nuttx_off_t offset) +{ + nuttx_ssize_t ret = pwrite(fd, buf, count, offset); + if (ret == -1) + { + ret = host_errno_convert(-errno); + } + + return ret; +} + /**************************************************************************** * Name: host_lseek ****************************************************************************/ @@ -274,6 +426,89 @@ nuttx_off_t host_lseek(int fd, nuttx_off_t pos, nuttx_off_t offset, return ret; } +/**************************************************************************** + * Name: host_fcntl + ****************************************************************************/ + +int host_fcntl(int fd, int cmd, ...) +{ + struct nuttx_flock_s *lock; + struct flock hostlock; + int hostcmd = host_fcntl_cmd_convert(cmd); + va_list ap; + int arg; + int ret; + + if (hostcmd < 0) + { + return hostcmd; + } + + va_start(ap, cmd); + + switch (cmd) + { + case NUTTX_F_GETFD: + ret = fcntl(fd, hostcmd); + break; + + case NUTTX_F_GETFL: + ret = fcntl(fd, hostcmd); + if (ret >= 0) + { + ret = host_oflags_revert(ret); + } + + break; + + case NUTTX_F_DUPFD: + case NUTTX_F_SETFD: + case NUTTX_F_SETFL: + arg = va_arg(ap, int); + if (cmd == NUTTX_F_SETFL) + { + arg = host_oflags_convert(arg); + } + + ret = fcntl(fd, hostcmd, arg); + break; + + case NUTTX_F_GETLK: + case NUTTX_F_SETLK: + case NUTTX_F_SETLKW: + lock = va_arg(ap, struct nuttx_flock_s *); + hostlock.l_type = lock->l_type; + hostlock.l_whence = lock->l_whence; + hostlock.l_start = lock->l_start; + hostlock.l_len = lock->l_len; + hostlock.l_pid = lock->l_pid; + + ret = fcntl(fd, hostcmd, &hostlock); + if (ret >= 0) + { + lock->l_type = hostlock.l_type; + lock->l_whence = hostlock.l_whence; + lock->l_start = hostlock.l_start; + lock->l_len = hostlock.l_len; + lock->l_pid = hostlock.l_pid; + } + break; + + default: + ret = -1; + errno = EINVAL; + break; + } + + va_end(ap); + if (ret < 0) + { + ret = host_errno_convert(-errno); + } + + return ret; +} + /**************************************************************************** * Name: host_ioctl ****************************************************************************/ @@ -302,6 +537,21 @@ void host_sync(int fd) fsync(fd); } +/**************************************************************************** + * Name: host_fsync + ****************************************************************************/ + +int host_fsync(int fd) +{ + int ret = fsync(fd); + if (ret < 0) + { + ret = host_errno_convert(-errno); + } + + return ret; +} + /**************************************************************************** * Name: host_dup ****************************************************************************/ @@ -560,6 +810,21 @@ int host_unlink(const char *pathname) return ret; } +/**************************************************************************** + * Name: host_remove + ****************************************************************************/ + +int host_remove(const char *pathname) +{ + int ret = remove(pathname); + if (ret < 0) + { + ret = host_errno_convert(-errno); + } + + return ret; +} + /**************************************************************************** * Name: host_mkdir ****************************************************************************/ @@ -630,6 +895,21 @@ int host_stat(const char *path, struct nuttx_stat_s *buf) return ret; } +/**************************************************************************** + * Name: host_access + ****************************************************************************/ + +int host_access(const char *path, int mode) +{ + int ret = access(path, mode); + if (ret < 0) + { + ret = host_errno_convert(-errno); + } + + return ret; +} + /**************************************************************************** * Name: host_chstat ****************************************************************************/ @@ -690,3 +970,36 @@ int host_chstat(const char *path, const struct nuttx_stat_s *buf, int flags) return 0; } + +/**************************************************************************** + * Name: host_popen + ****************************************************************************/ + +nuttx_file_t host_popen(const char *command, const char *mode) +{ + return (nuttx_file_t)(uintptr_t)popen(command, mode); +} + +/**************************************************************************** + * Name: host_fgets + ****************************************************************************/ + +char *host_fgets(char *buf, nuttx_size_t len, nuttx_file_t stream) +{ + return fgets(buf, len, (FILE *)(uintptr_t)stream); +} + +/**************************************************************************** + * Name: host_pclose + ****************************************************************************/ + +int host_pclose(nuttx_file_t stream) +{ + int ret = pclose((FILE *)(uintptr_t)stream); + if (ret < 0) + { + ret = host_errno_convert(-errno); + } + + return ret; +} diff --git a/arch/sim/src/sim/posix/sim_hostsocket.c b/arch/sim/src/sim/posix/sim_hostsocket.c new file mode 100644 index 0000000000000..0ac369416a61c --- /dev/null +++ b/arch/sim/src/sim/posix/sim_hostsocket.c @@ -0,0 +1,703 @@ +/**************************************************************************** + * arch/sim/src/sim/posix/sim_hostsocket.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include "sim_internal.h" + +#ifdef CONFIG_HOST_LINUX +# include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sim_hostsocket.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef PF_BLUETOOTH +# define PF_BLUETOOTH 31 +#endif + +#ifndef AF_BLUETOOTH +# define AF_BLUETOOTH PF_BLUETOOTH +#endif + +#ifndef BTPROTO_HCI +# define BTPROTO_HCI 1 +#endif + +#ifndef AF_NETLINK +# define AF_NETLINK 16 +#endif + +#ifndef PF_NETLINK +# define PF_NETLINK AF_NETLINK +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct sockaddr_hci +{ + sa_family_t hci_family; + unsigned short hci_dev; + unsigned short hci_channel; +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int host_socket_domain_convert(int domain) +{ + switch (domain) + { + case NUTTX_PF_UNIX: + return PF_UNIX; + + case NUTTX_PF_INET: + return PF_INET; + + case NUTTX_PF_INET6: + return PF_INET6; + + case NUTTX_PF_NETLINK: + return PF_NETLINK; + + case NUTTX_PF_BLUETOOTH: + return PF_BLUETOOTH; + + default: + return -EAFNOSUPPORT; + } +} + +static int host_socket_type_convert(int type) +{ + int hosttype; + + switch (type & NUTTX_SOCK_TYPE_MASK) + { + case NUTTX_SOCK_STREAM: + hosttype = SOCK_STREAM; + break; + + case NUTTX_SOCK_DGRAM: + hosttype = SOCK_DGRAM; + break; + + case NUTTX_SOCK_RAW: + hosttype = SOCK_RAW; + break; + + case NUTTX_SOCK_SEQPACKET: + hosttype = SOCK_SEQPACKET; + break; + + default: + return -EPROTONOSUPPORT; + } + + if (type & NUTTX_SOCK_CLOEXEC) + { + hosttype |= SOCK_CLOEXEC; + } + + if (type & NUTTX_SOCK_NONBLOCK) + { + hosttype |= SOCK_NONBLOCK; + } + + return hosttype; +} + +static int host_socket_protocol_convert(int domain, int protocol) +{ + if (domain == NUTTX_PF_BLUETOOTH && protocol == BTPROTO_HCI) + { + return BTPROTO_HCI; + } + + return protocol; +} + +static int host_socket_level_convert(int level) +{ + switch (level) + { + case NUTTX_SOL_SOCKET: + return SOL_SOCKET; + + case NUTTX_IPPROTO_TCP: + return IPPROTO_TCP; + + default: + return -ENOPROTOOPT; + } +} + +static int host_socket_option_convert(int level, int option) +{ + if (level == NUTTX_SOL_SOCKET) + { + switch (option) + { + case NUTTX_SO_ERROR: + return SO_ERROR; + + case NUTTX_SO_RCVBUF: + return SO_RCVBUF; + + case NUTTX_SO_RCVTIMEO: + return SO_RCVTIMEO; + + case NUTTX_SO_REUSEADDR: + return SO_REUSEADDR; + + case NUTTX_SO_SNDBUF: + return SO_SNDBUF; + + case NUTTX_SO_SNDTIMEO: + return SO_SNDTIMEO; + + default: + return -ENOPROTOOPT; + } + } + + if (level == NUTTX_IPPROTO_TCP) + { + switch (option) + { + case NUTTX_TCP_NODELAY: + return TCP_NODELAY; + + default: + return -ENOPROTOOPT; + } + } + + return -ENOPROTOOPT; +} + +static int host_socket_addr_from_native(const struct sockaddr *addr, + socklen_t addrlen, + struct host_socket_addr *hostaddr) +{ + memset(hostaddr, 0, sizeof(*hostaddr)); + + if (addr->sa_family == AF_INET) + { + const struct sockaddr_in *in = (const struct sockaddr_in *)addr; + + if (addrlen < sizeof(*in)) + { + return -EINVAL; + } + + hostaddr->type = HOST_SOCKET_ADDR_IPV4; + memcpy(hostaddr->ipv4.addr, &in->sin_addr.s_addr, + sizeof(hostaddr->ipv4.addr)); + hostaddr->ipv4.port = ntohs(in->sin_port); + return 0; + } + + if (addr->sa_family == AF_INET6) + { + const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *)addr; + + if (addrlen < sizeof(*in6)) + { + return -EINVAL; + } + + hostaddr->type = HOST_SOCKET_ADDR_IPV6; + memcpy(hostaddr->ipv6.addr, &in6->sin6_addr.s6_addr, + sizeof(hostaddr->ipv6.addr)); + hostaddr->ipv6.port = ntohs(in6->sin6_port); + return 0; + } + + return -EAFNOSUPPORT; +} + +static int host_socket_addr_to_native(const struct host_socket_addr *addr, + struct sockaddr_storage *storage, + socklen_t *addrlen) +{ + memset(storage, 0, sizeof(*storage)); + + switch (addr->type) + { + case HOST_SOCKET_ADDR_HCI: + { + struct sockaddr_hci hci; + + memset(&hci, 0, sizeof(hci)); + hci.hci_family = AF_BLUETOOTH; + hci.hci_dev = addr->hci.dev; + hci.hci_channel = addr->hci.channel; + memcpy(storage, &hci, sizeof(hci)); + *addrlen = sizeof(hci); + return 0; + } + + case HOST_SOCKET_ADDR_UNIX: + { + struct sockaddr_un un; + + memset(&un, 0, sizeof(un)); + un.sun_family = AF_UNIX; + snprintf(un.sun_path, sizeof(un.sun_path), "%s", addr->un.path); + memcpy(storage, &un, sizeof(un)); + *addrlen = sizeof(un); + return 0; + } + + case HOST_SOCKET_ADDR_IPV4: + { + struct sockaddr_in in; + + memset(&in, 0, sizeof(in)); + in.sin_family = AF_INET; + in.sin_port = htons(addr->ipv4.port); + memcpy(&in.sin_addr.s_addr, addr->ipv4.addr, + sizeof(addr->ipv4.addr)); + memcpy(storage, &in, sizeof(in)); + *addrlen = sizeof(in); + return 0; + } + + case HOST_SOCKET_ADDR_IPV6: + { + struct sockaddr_in6 in6; + + memset(&in6, 0, sizeof(in6)); + in6.sin6_family = AF_INET6; + in6.sin6_port = htons(addr->ipv6.port); + memcpy(&in6.sin6_addr.s6_addr, addr->ipv6.addr, + sizeof(addr->ipv6.addr)); + memcpy(storage, &in6, sizeof(in6)); + *addrlen = sizeof(in6); + return 0; + } + + case HOST_SOCKET_ADDR_NETLINK: + { +#ifdef CONFIG_HOST_LINUX + struct sockaddr_nl nl; + + memset(&nl, 0, sizeof(nl)); + nl.nl_family = AF_NETLINK; + nl.nl_pid = addr->netlink.pid; + nl.nl_groups = addr->netlink.groups; + memcpy(storage, &nl, sizeof(nl)); + *addrlen = sizeof(nl); + return 0; +#else + return -EAFNOSUPPORT; +#endif + } + + default: + return -EAFNOSUPPORT; + } +} + +static int host_socket_gai_error_convert(int ret) +{ + if (ret == 0) + { + return 0; + } + + switch (ret) + { + case EAI_AGAIN: + return -EAGAIN; + + case EAI_BADFLAGS: + case EAI_SERVICE: + return -EINVAL; + + case EAI_FAMILY: + return -EAFNOSUPPORT; + + case EAI_MEMORY: + return -ENOMEM; + + case EAI_NONAME: + return -ENOENT; + + default: + return -EIO; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int host_socket(int domain, int type, int protocol) +{ + int hostdomain = host_socket_domain_convert(domain); + int hosttype = host_socket_type_convert(type); + int hostprotocol = host_socket_protocol_convert(domain, protocol); + int ret; + + if (hostdomain < 0) + { + return hostdomain; + } + + if (hosttype < 0) + { + return hosttype; + } + + ret = socket(hostdomain, hosttype, hostprotocol); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_bind(int fd, const struct host_socket_addr *addr) +{ + struct sockaddr_storage storage; + socklen_t addrlen; + int ret; + + ret = host_socket_addr_to_native(addr, &storage, &addrlen); + if (ret < 0) + { + return ret; + } + + ret = bind(fd, (struct sockaddr *)&storage, addrlen); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_connect(int fd, const struct host_socket_addr *addr) +{ + struct sockaddr_storage storage; + socklen_t addrlen; + int ret; + + ret = host_socket_addr_to_native(addr, &storage, &addrlen); + if (ret < 0) + { + return ret; + } + + ret = connect(fd, (struct sockaddr *)&storage, addrlen); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_listen(int fd, int backlog) +{ + int ret = listen(fd, backlog); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_accept(int fd) +{ + int ret = accept(fd, NULL, NULL); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_setsockopt(int fd, int level, int option, const void *value, + uint32_t value_len) +{ + int hostlevel = host_socket_level_convert(level); + int hostoption = host_socket_option_convert(level, option); + int ret; + + if (hostlevel < 0) + { + return hostlevel; + } + + if (hostoption < 0) + { + return hostoption; + } + + ret = setsockopt(fd, hostlevel, hostoption, value, (socklen_t)value_len); + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +int host_poll(int fd, int events, int *revents, int timeout_ms) +{ + struct pollfd pfd; + int ret; + + memset(&pfd, 0, sizeof(pfd)); + pfd.fd = fd; + pfd.events = events; + + ret = poll(&pfd, 1, timeout_ms); + if (ret < 0) + { + return host_errno_convert(-errno); + } + + if (revents != NULL) + { + *revents = pfd.revents; + } + + return ret; +} + +int host_getsockopt_error(int fd) +{ + socklen_t len = sizeof(int); + int err = 0; + int ret; + + ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len); + if (ret < 0) + { + return host_errno_convert(-errno); + } + + return -err; +} + +int host_getaddrinfo(const char *node, const char *service, + const struct host_addrinfo *hints, + struct host_addrinfo **res) +{ + struct addrinfo native_hints; + struct addrinfo *result; + struct addrinfo *cur; + struct host_addrinfo **next; + int family; + int socktype; + int protocol; + int hostfamily; + int hosttype; + int hostprotocol; + int ret; + + if (res == NULL) + { + return -EINVAL; + } + + *res = NULL; + family = hints == NULL ? NUTTX_PF_UNSPEC : hints->family; + socktype = hints == NULL ? 0 : hints->socktype; + protocol = hints == NULL ? 0 : hints->protocol; + + hostfamily = family == NUTTX_PF_UNSPEC ? AF_UNSPEC : + host_socket_domain_convert(family); + if (hostfamily < 0) + { + return hostfamily; + } + + hosttype = socktype == 0 ? 0 : host_socket_type_convert(socktype); + if (hosttype < 0) + { + return hosttype; + } + + hostprotocol = host_socket_protocol_convert(family, protocol); + + memset(&native_hints, 0, sizeof(native_hints)); + native_hints.ai_flags = hints == NULL ? 0 : hints->flags; + native_hints.ai_family = hostfamily; + native_hints.ai_socktype = hosttype; + native_hints.ai_protocol = hostprotocol; + + ret = getaddrinfo(node, service, &native_hints, &result); + if (ret != 0) + { + return host_socket_gai_error_convert(ret); + } + + ret = -EAFNOSUPPORT; + next = res; + for (cur = result; cur != NULL; cur = cur->ai_next) + { + struct host_addrinfo *entry; + + entry = calloc(1, sizeof(*entry)); + if (entry == NULL) + { + host_freeaddrinfo(*res); + *res = NULL; + ret = -ENOMEM; + break; + } + + ret = host_socket_addr_from_native(cur->ai_addr, cur->ai_addrlen, + &entry->addr); + if (ret == 0) + { + entry->flags = cur->ai_flags; + entry->family = cur->ai_family; + entry->socktype = cur->ai_socktype; + entry->protocol = cur->ai_protocol; + *next = entry; + next = &entry->next; + continue; + } + + free(entry); + } + + if (*res != NULL) + { + ret = 0; + } + + freeaddrinfo(result); + return ret; +} + +void host_freeaddrinfo(struct host_addrinfo *res) +{ + while (res != NULL) + { + struct host_addrinfo *next = res->next; + + free(res); + res = next; + } +} + +int host_getifaddrs(struct host_ifaddrs **ifap) +{ + struct ifaddrs *native; + struct ifaddrs *cur; + struct host_ifaddrs **next; + int ret; + + if (ifap == NULL) + { + return -EINVAL; + } + + *ifap = NULL; + ret = getifaddrs(&native); + if (ret < 0) + { + return host_errno_convert(-errno); + } + + next = ifap; + for (cur = native; cur != NULL; cur = cur->ifa_next) + { + struct host_ifaddrs *entry; + + if (cur->ifa_addr == NULL) + { + continue; + } + + entry = calloc(1, sizeof(*entry)); + if (entry == NULL) + { + host_freeifaddrs(*ifap); + *ifap = NULL; + freeifaddrs(native); + return -ENOMEM; + } + + snprintf(entry->name, sizeof(entry->name), "%s", + cur->ifa_name == NULL ? "" : cur->ifa_name); + entry->flags = cur->ifa_flags; + + ret = host_socket_addr_from_native(cur->ifa_addr, + sizeof(struct sockaddr_storage), + &entry->addr); + if (ret == 0) + { + entry->has_addr = 1; + *next = entry; + next = &entry->next; + continue; + } + + free(entry); + } + + freeifaddrs(native); + return 0; +} + +void host_freeifaddrs(struct host_ifaddrs *ifa) +{ + while (ifa != NULL) + { + struct host_ifaddrs *next = ifa->next; + + free(ifa); + ifa = next; + } +} + +ssize_t host_recv(int fd, void *data, size_t len, int flags) +{ + ssize_t ret; + + do + { + ret = recv(fd, data, len, flags); + } + while (ret < 0 && errno == EINTR); + + return ret < 0 ? host_errno_convert(-errno) : ret; +} + +ssize_t host_send(int fd, const void *data, size_t len, int flags) +{ + ssize_t ret; + + do + { + ret = send(fd, data, len, flags); + } + while (ret < 0 && errno == EINTR); + + return ret < 0 ? host_errno_convert(-errno) : ret; +} diff --git a/arch/sim/src/sim/sim_hostepoll.h b/arch/sim/src/sim/sim_hostepoll.h new file mode 100644 index 0000000000000..3dfffd3be7402 --- /dev/null +++ b/arch/sim/src/sim/sim_hostepoll.h @@ -0,0 +1,60 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_hostepoll.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_HOSTEPOLL_H +#define __ARCH_SIM_SRC_SIM_HOSTEPOLL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "hostfs.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define NUTTX_EPOLL_CLOEXEC NUTTX_O_CLOEXEC + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +struct host_epoll_event +{ + uint32_t events; + uintptr_t data; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int host_epoll_create1(int flags); +int host_epoll_ctl(int epoll_fd, int op, int fd, uint32_t events, + uintptr_t data); +int host_epoll_wait(int epoll_fd, struct host_epoll_event *events, + int maxevents, int timeout_ms); + +#endif /* __ARCH_SIM_SRC_SIM_HOSTEPOLL_H */ diff --git a/arch/sim/src/sim/sim_hostsock.h b/arch/sim/src/sim/sim_hostsock.h new file mode 100644 index 0000000000000..809970266f4ec --- /dev/null +++ b/arch/sim/src/sim/sim_hostsock.h @@ -0,0 +1,205 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_hostsock.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_HOSTSOCK_H +#define __ARCH_SIM_SRC_SIM_HOSTSOCK_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#ifndef __SIM__ +# include +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef __SIM__ + +#define NUTTX_IPPROTO_IP 0 +#define NUTTX_IPPROTO_HOPOPTS 0 +#define NUTTX_IPPROTO_ICMP 1 +#define NUTTX_IPPROTO_IGMP 2 +#define NUTTX_IPPROTO_IPIP 4 +#define NUTTX_IPPROTO_TCP 6 +#define NUTTX_IPPROTO_EGP 8 +#define NUTTX_IPPROTO_PUP 12 +#define NUTTX_IPPROTO_UDP 17 +#define NUTTX_IPPROTO_IDP 22 +#define NUTTX_IPPROTO_TP 29 +#define NUTTX_IPPROTO_DCCP 33 +#define NUTTX_IPPROTO_IPV6 41 +#define NUTTX_IPPROTO_ROUTING 43 +#define NUTTX_IPPROTO_FRAGMENT 44 +#define NUTTX_IPPROTO_RSVP 46 +#define NUTTX_IPPROTO_GRE 47 +#define NUTTX_IPPROTO_ESP 50 +#define NUTTX_IPPROTO_AH 51 +#define NUTTX_IPPROTO_ICMP6 58 +#define NUTTX_IPPROTO_NONE 59 +#define NUTTX_IPPROTO_DSTOPTS 60 +#define NUTTX_IPPROTO_MTP 92 +#define NUTTX_IPPROTO_ENCAP 98 +#define NUTTX_IPPROTO_BEETPH 94 +#define NUTTX_IPPROTO_PIM 103 +#define NUTTX_IPPROTO_COMP 108 +#define NUTTX_IPPROTO_SCTP 132 +#define NUTTX_IPPROTO_UDPLITE 136 +#define NUTTX_IPPROTO_MPLS 137 +#define NUTTX_IPPROTO_RAW 255 + +#define NUTTX_PF_UNSPEC 0 +#define NUTTX_PF_UNIX 1 +#define NUTTX_PF_LOCAL 1 +#define NUTTX_PF_INET 2 +#define NUTTX_PF_INET6 10 +#define NUTTX_PF_NETLINK 16 +#define NUTTX_PF_ROUTE NUTTX_PF_NETLINK +#define NUTTX_PF_PACKET 17 +#define NUTTX_PF_CAN 29 +#define NUTTX_PF_BLUETOOTH 31 +#define NUTTX_PF_IEEE802154 36 +#define NUTTX_PF_PKTRADIO 64 +#define NUTTX_PF_RPMSG 65 + +#define NUTTX_AF_UNSPEC NUTTX_PF_UNSPEC +#define NUTTX_AF_UNIX NUTTX_PF_UNIX +#define NUTTX_AF_LOCAL NUTTX_PF_LOCAL +#define NUTTX_AF_INET NUTTX_PF_INET +#define NUTTX_AF_INET6 NUTTX_PF_INET6 +#define NUTTX_AF_NETLINK NUTTX_PF_NETLINK +#define NUTTX_AF_ROUTE NUTTX_PF_ROUTE +#define NUTTX_AF_PACKET NUTTX_PF_PACKET +#define NUTTX_AF_CAN NUTTX_PF_CAN +#define NUTTX_AF_BLUETOOTH NUTTX_PF_BLUETOOTH +#define NUTTX_AF_IEEE802154 NUTTX_PF_IEEE802154 +#define NUTTX_AF_PKTRADIO NUTTX_PF_PKTRADIO +#define NUTTX_AF_RPMSG NUTTX_PF_RPMSG + +#define NUTTX_SOCK_UNSPEC 0 +#define NUTTX_SOCK_STREAM 1 +#define NUTTX_SOCK_DGRAM 2 +#define NUTTX_SOCK_RAW 3 +#define NUTTX_SOCK_RDM 4 +#define NUTTX_SOCK_SEQPACKET 5 +#define NUTTX_SOCK_PACKET 10 +#define NUTTX_SOCK_CLOEXEC 02000000 +#define NUTTX_SOCK_NONBLOCK 00004000 + +#define NUTTX_SOCK_MAX (NUTTX_SOCK_PACKET + 1) +#define NUTTX_SOCK_TYPE_MASK 0xf + +#define NUTTX_MSG_OOB 0x0001 +#define NUTTX_MSG_PEEK 0x0002 +#define NUTTX_MSG_DONTROUTE 0x0004 +#define NUTTX_MSG_CTRUNC 0x0008 +#define NUTTX_MSG_PROXY 0x0010 +#define NUTTX_MSG_TRUNC 0x0020 +#define NUTTX_MSG_DONTWAIT 0x0040 +#define NUTTX_MSG_EOR 0x0080 +#define NUTTX_MSG_WAITALL 0x0100 +#define NUTTX_MSG_FIN 0x0200 +#define NUTTX_MSG_SYN 0x0400 +#define NUTTX_MSG_CONFIRM 0x0800 +#define NUTTX_MSG_RST 0x1000 +#define NUTTX_MSG_ERRQUEUE 0x2000 +#define NUTTX_MSG_NOSIGNAL 0x4000 +#define NUTTX_MSG_MORE 0x8000 + +#define NUTTX_SOL_SOCKET 1 +#define NUTTX_SO_ACCEPTCONN 0 +#define NUTTX_SO_BROADCAST 1 +#define NUTTX_SO_DEBUG 2 +#define NUTTX_SO_DONTROUTE 3 +#define NUTTX_SO_ERROR 4 +#define NUTTX_SO_KEEPALIVE 5 +#define NUTTX_SO_LINGER 6 +#define NUTTX_SO_OOBINLINE 7 +#define NUTTX_SO_RCVBUF 8 +#define NUTTX_SO_RCVLOWAT 9 +#define NUTTX_SO_RCVTIMEO 10 +#define NUTTX_SO_REUSEADDR 11 +#define NUTTX_SO_SNDBUF 12 +#define NUTTX_SO_SNDLOWAT 13 +#define NUTTX_SO_SNDTIMEO 14 +#define NUTTX_SO_TYPE 15 +#define NUTTX_SO_TIMESTAMP 16 +#define NUTTX_SO_BINDTODEVICE 17 + +#define NUTTX_SO_SNDBUFFORCE 32 +#define NUTTX_SO_RCVBUFFORCE 33 +#define NUTTX_SO_RXQ_OVFL 40 + +#define NUTTX_SOL_IP NUTTX_IPPROTO_IP +#define NUTTX_SOL_IPV6 NUTTX_IPPROTO_IPV6 +#define NUTTX_SOL_TCP NUTTX_IPPROTO_TCP +#define NUTTX_SOL_UDP NUTTX_IPPROTO_UDP + +#define NUTTX_TCP_NODELAY (NUTTX___SO_PROTOCOL + 0) + +#define NUTTX_SOL_HCI 0 +#define NUTTX_SOL_L2CAP 6 +#define NUTTX_SOL_SCO 17 +#define NUTTX_SOL_RFCOMM 18 + +#define NUTTX___SO_PROTOCOL 16 + +#define NUTTX_SHUT_RD 1 +#define NUTTX_SHUT_WR 2 +#define NUTTX_SHUT_RDWR 3 + +#define NUTTX_SCM_RIGHTS 0x01 +#define NUTTX_SCM_CREDENTIALS 0x02 +#define NUTTX_SCM_SECURITY 0x03 + +#define NUTTX_IP_MULTICAST_IF (NUTTX__SO_PROTOCOL + 1) +#define NUTTX_IP_MULTICAST_TTL (NUTTX__SO_PROTOCOL + 2) +#define NUTTX_IP_MULTICAST_LOOP (NUTTX__SO_PROTOCOL + 3) +#define NUTTX_IP_ADD_MEMBERSHIP (NUTTX__SO_PROTOCOL + 4) +#define NUTTX_IP_DROP_MEMBERSHIP (NUTTX__SO_PROTOCOL + 5) +#define NUTTX_IP_UNBLOCK_SOURCE (NUTTX__SO_PROTOCOL + 6) +#define NUTTX_IP_BLOCK_SOURCE (NUTTX__SO_PROTOCOL + 7) +#define NUTTX_IP_ADD_SOURCE_MEMBERSHIP (NUTTX__SO_PROTOCOL + 8) +#define NUTTX_IP_DROP_SOURCE_MEMBERSHIP (NUTTX__SO_PROTOCOL + 9) +#define NUTTX_IP_MSFILTER (NUTTX__SO_PROTOCOL + 10) +#define NUTTX_IP_MULTICAST_ALL (NUTTX__SO_PROTOCOL + 11) +#define NUTTX_IP_PKTINFO (NUTTX__SO_PROTOCOL + 12) +#define NUTTX_IP_TOS (NUTTX__SO_PROTOCOL + 13) +#define NUTTX_IP_TTL (NUTTX__SO_PROTOCOL + 14) + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +typedef unsigned int nuttx_socklen_t; + +struct nuttx_sockaddr +{ + sa_family_t sa_family; + char sa_data[14]; +}; + +#endif /* __SIM__ */ + +#endif /* __ARCH_SIM_SRC_SIM_HOSTSOCK_H */ diff --git a/arch/sim/src/sim/sim_hostsocket.h b/arch/sim/src/sim/sim_hostsocket.h new file mode 100644 index 0000000000000..cedf0a650f07c --- /dev/null +++ b/arch/sim/src/sim/sim_hostsocket.h @@ -0,0 +1,129 @@ +/**************************************************************************** + * arch/sim/src/sim/sim_hostsocket.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_SIM_SRC_SIM_HOSTSOCKET_H +#define __ARCH_SIM_SRC_SIM_HOSTSOCKET_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +#include "sim_hostsock.h" + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +enum host_socket_addr_type +{ + HOST_SOCKET_ADDR_HCI, + HOST_SOCKET_ADDR_UNIX, + HOST_SOCKET_ADDR_IPV4, + HOST_SOCKET_ADDR_IPV6, + HOST_SOCKET_ADDR_NETLINK, +}; + +struct host_socket_addr +{ + enum host_socket_addr_type type; + union + { + struct + { + uint16_t dev; + uint16_t channel; + } hci; + + struct + { + char path[108]; + } un; + + struct + { + uint8_t addr[4]; + uint16_t port; + } ipv4; + + struct + { + uint8_t addr[16]; + uint16_t port; + } ipv6; + + struct + { + uint32_t pid; + uint32_t groups; + } netlink; + }; +}; + +struct host_addrinfo +{ + int flags; + int family; + int socktype; + int protocol; + struct host_socket_addr addr; + struct host_addrinfo *next; +}; + +#define HOST_IFNAMSIZ 64 + +struct host_ifaddrs +{ + struct host_ifaddrs *next; + char name[HOST_IFNAMSIZ]; + unsigned int flags; + uint8_t has_addr; + struct host_socket_addr addr; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int host_socket(int domain, int type, int protocol); +int host_bind(int fd, const struct host_socket_addr *addr); +int host_connect(int fd, const struct host_socket_addr *addr); +int host_listen(int fd, int backlog); +int host_accept(int fd); +int host_setsockopt(int fd, int level, int option, const void *value, + uint32_t value_len); +int host_poll(int fd, int events, int *revents, int timeout_ms); +int host_getsockopt_error(int fd); +int host_getaddrinfo(const char *node, const char *service, + const struct host_addrinfo *hints, + struct host_addrinfo **res); +void host_freeaddrinfo(struct host_addrinfo *res); +int host_getifaddrs(struct host_ifaddrs **ifap); +void host_freeifaddrs(struct host_ifaddrs *ifa); +ssize_t host_recv(int fd, void *data, size_t len, int flags); +ssize_t host_send(int fd, const void *data, size_t len, int flags); + +#endif /* __ARCH_SIM_SRC_SIM_HOSTSOCKET_H */ diff --git a/arch/sim/src/sim/sim_hostusrsock.h b/arch/sim/src/sim/sim_hostusrsock.h index e5fb2f10d3d31..d12c2340b2e2e 100644 --- a/arch/sim/src/sim/sim_hostusrsock.h +++ b/arch/sim/src/sim/sim_hostusrsock.h @@ -27,165 +27,10 @@ * Included Files ****************************************************************************/ -#ifndef __SIM__ -# include -#endif - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ +#include "sim_hostsock.h" #ifdef __SIM__ -#define NUTTX_IPPROTO_IP 0 -#define NUTTX_IPPROTO_HOPOPTS 0 -#define NUTTX_IPPROTO_ICMP 1 -#define NUTTX_IPPROTO_IGMP 2 -#define NUTTX_IPPROTO_IPIP 4 -#define NUTTX_IPPROTO_TCP 6 -#define NUTTX_IPPROTO_EGP 8 -#define NUTTX_IPPROTO_PUP 12 -#define NUTTX_IPPROTO_UDP 17 -#define NUTTX_IPPROTO_IDP 22 -#define NUTTX_IPPROTO_TP 29 -#define NUTTX_IPPROTO_DCCP 33 -#define NUTTX_IPPROTO_IPV6 41 -#define NUTTX_IPPROTO_ROUTING 43 -#define NUTTX_IPPROTO_FRAGMENT 44 -#define NUTTX_IPPROTO_RSVP 46 -#define NUTTX_IPPROTO_GRE 47 -#define NUTTX_IPPROTO_ESP 50 -#define NUTTX_IPPROTO_AH 51 -#define NUTTX_IPPROTO_ICMP6 58 -#define NUTTX_IPPROTO_NONE 59 -#define NUTTX_IPPROTO_DSTOPTS 60 -#define NUTTX_IPPROTO_MTP 92 -#define NUTTX_IPPROTO_ENCAP 98 -#define NUTTX_IPPROTO_BEETPH 94 -#define NUTTX_IPPROTO_PIM 103 -#define NUTTX_IPPROTO_COMP 108 -#define NUTTX_IPPROTO_SCTP 132 -#define NUTTX_IPPROTO_UDPLITE 136 -#define NUTTX_IPPROTO_MPLS 137 -#define NUTTX_IPPROTO_RAW 255 - -#define NUTTX_PF_UNSPEC 0 -#define NUTTX_PF_UNIX 1 -#define NUTTX_PF_LOCAL 1 -#define NUTTX_PF_INET 2 -#define NUTTX_PF_INET6 10 -#define NUTTX_PF_NETLINK 16 -#define NUTTX_PF_ROUTE NUTTX_PF_NETLINK -#define NUTTX_PF_PACKET 17 -#define NUTTX_PF_CAN 29 -#define NUTTX_PF_BLUETOOTH 31 -#define NUTTX_PF_IEEE802154 36 -#define NUTTX_PF_PKTRADIO 64 -#define NUTTX_PF_RPMSG 65 - -#define NUTTX_AF_UNSPEC NUTTX_PF_UNSPEC -#define NUTTX_AF_UNIX NUTTX_PF_UNIX -#define NUTTX_AF_LOCAL NUTTX_PF_LOCAL -#define NUTTX_AF_INET NUTTX_PF_INET -#define NUTTX_AF_INET6 NUTTX_PF_INET6 -#define NUTTX_AF_NETLINK NUTTX_PF_NETLINK -#define NUTTX_AF_ROUTE NUTTX_PF_ROUTE -#define NUTTX_AF_PACKET NUTTX_PF_PACKET -#define NUTTX_AF_CAN NUTTX_PF_CAN -#define NUTTX_AF_BLUETOOTH NUTTX_PF_BLUETOOTH -#define NUTTX_AF_IEEE802154 NUTTX_PF_IEEE802154 -#define NUTTX_AF_PKTRADIO NUTTX_PF_PKTRADIO -#define NUTTX_AF_RPMSG NUTTX_PF_RPMSG - -#define NUTTX_SOCK_UNSPEC 0 -#define NUTTX_SOCK_STREAM 1 -#define NUTTX_SOCK_DGRAM 2 -#define NUTTX_SOCK_RAW 3 -#define NUTTX_SOCK_RDM 4 -#define NUTTX_SOCK_SEQPACKET 5 -#define NUTTX_SOCK_PACKET 10 -#define NUTTX_SOCK_CLOEXEC 02000000 -#define NUTTX_SOCK_NONBLOCK 00004000 - -#define NUTTX_SOCK_MAX (NUTTX_SOCK_PACKET + 1) -#define NUTTX_SOCK_TYPE_MASK 0xf - -#define NUTTX_MSG_OOB 0x0001 -#define NUTTX_MSG_PEEK 0x0002 -#define NUTTX_MSG_DONTROUTE 0x0004 -#define NUTTX_MSG_CTRUNC 0x0008 -#define NUTTX_MSG_PROXY 0x0010 -#define NUTTX_MSG_TRUNC 0x0020 -#define NUTTX_MSG_DONTWAIT 0x0040 -#define NUTTX_MSG_EOR 0x0080 -#define NUTTX_MSG_WAITALL 0x0100 -#define NUTTX_MSG_FIN 0x0200 -#define NUTTX_MSG_SYN 0x0400 -#define NUTTX_MSG_CONFIRM 0x0800 -#define NUTTX_MSG_RST 0x1000 -#define NUTTX_MSG_ERRQUEUE 0x2000 -#define NUTTX_MSG_NOSIGNAL 0x4000 -#define NUTTX_MSG_MORE 0x8000 - -#define NUTTX_SOL_SOCKET 1 -#define NUTTX_SO_ACCEPTCONN 0 -#define NUTTX_SO_BROADCAST 1 -#define NUTTX_SO_DEBUG 2 -#define NUTTX_SO_DONTROUTE 3 -#define NUTTX_SO_ERROR 4 -#define NUTTX_SO_KEEPALIVE 5 -#define NUTTX_SO_LINGER 6 -#define NUTTX_SO_OOBINLINE 7 -#define NUTTX_SO_RCVBUF 8 -#define NUTTX_SO_RCVLOWAT 9 -#define NUTTX_SO_RCVTIMEO 10 -#define NUTTX_SO_REUSEADDR 11 -#define NUTTX_SO_SNDBUF 12 -#define NUTTX_SO_SNDLOWAT 13 -#define NUTTX_SO_SNDTIMEO 14 -#define NUTTX_SO_TYPE 15 -#define NUTTX_SO_TIMESTAMP 16 -#define NUTTX_SO_BINDTODEVICE 17 - -#define NUTTX_SO_SNDBUFFORCE 32 -#define NUTTX_SO_RCVBUFFORCE 33 -#define NUTTX_SO_RXQ_OVFL 40 - -#define NUTTX_SOL_IP NUTTX_IPPROTO_IP -#define NUTTX_SOL_IPV6 NUTTX_IPPROTO_IPV6 -#define NUTTX_SOL_TCP NUTTX_IPPROTO_TCP -#define NUTTX_SOL_UDP NUTTX_IPPROTO_UDP - -#define NUTTX_SOL_HCI 0 -#define NUTTX_SOL_L2CAP 6 -#define NUTTX_SOL_SCO 17 -#define NUTTX_SOL_RFCOMM 18 - -#define NUTTX___SO_PROTOCOL 16 - -#define NUTTX_SHUT_RD 1 -#define NUTTX_SHUT_WR 2 -#define NUTTX_SHUT_RDWR 3 - -#define NUTTX_SCM_RIGHTS 0x01 -#define NUTTX_SCM_CREDENTIALS 0x02 -#define NUTTX_SCM_SECURITY 0x03 - -#define NUTTX_IP_MULTICAST_IF (NUTTX__SO_PROTOCOL + 1) -#define NUTTX_IP_MULTICAST_TTL (NUTTX__SO_PROTOCOL + 2) -#define NUTTX_IP_MULTICAST_LOOP (NUTTX__SO_PROTOCOL + 3) -#define NUTTX_IP_ADD_MEMBERSHIP (NUTTX__SO_PROTOCOL + 4) -#define NUTTX_IP_DROP_MEMBERSHIP (NUTTX__SO_PROTOCOL + 5) -#define NUTTX_IP_UNBLOCK_SOURCE (NUTTX__SO_PROTOCOL + 6) -#define NUTTX_IP_BLOCK_SOURCE (NUTTX__SO_PROTOCOL + 7) -#define NUTTX_IP_ADD_SOURCE_MEMBERSHIP (NUTTX__SO_PROTOCOL + 8) -#define NUTTX_IP_DROP_SOURCE_MEMBERSHIP (NUTTX__SO_PROTOCOL + 9) -#define NUTTX_IP_MSFILTER (NUTTX__SO_PROTOCOL + 10) -#define NUTTX_IP_MULTICAST_ALL (NUTTX__SO_PROTOCOL + 11) -#define NUTTX_IP_PKTINFO (NUTTX__SO_PROTOCOL + 12) -#define NUTTX_IP_TOS (NUTTX__SO_PROTOCOL + 13) -#define NUTTX_IP_TTL (NUTTX__SO_PROTOCOL + 14) - /* Event message flags */ #define NUTTX_USRSOCK_EVENT_ABORT (1 << 1) @@ -194,11 +39,9 @@ #define NUTTX_USRSOCK_EVENT_REMOTE_CLOSED (1 << 4) /**************************************************************************** - * Type Definitions + * Public Type Definitions ****************************************************************************/ -typedef unsigned int nuttx_socklen_t; - #ifdef CONFIG_NET_IPv6 struct nuttx_sockaddr_storage { @@ -213,12 +56,6 @@ struct nuttx_sockaddr_storage }; #endif -struct nuttx_sockaddr -{ - sa_family_t sa_family; /* Address family: See AF_* definitions */ - char sa_data[14]; /* 14-bytes data (actually variable length) */ -}; - struct nuttx_linger { int l_onoff; /* Indicates whether linger option is enabled. */ diff --git a/include/nuttx/fs/hostfs.h b/include/nuttx/fs/hostfs.h index 85840d23b5e83..68c9dfba208f5 100644 --- a/include/nuttx/fs/hostfs.h +++ b/include/nuttx/fs/hostfs.h @@ -32,9 +32,11 @@ # include # include # include +# include # include #else # include +# include # include #endif @@ -113,6 +115,21 @@ #define NUTTX_CH_STAT_ATIME (1 << 3) #define NUTTX_CH_STAT_MTIME (1 << 4) +/* These must exactly match the definitions from include/fcntl.h: */ + +#define NUTTX_F_DUPFD 0 +#define NUTTX_F_GETFD 1 +#define NUTTX_F_GETFL 2 +#define NUTTX_F_GETLK 4 +#define NUTTX_F_SETFD 8 +#define NUTTX_F_SETFL 9 +#define NUTTX_F_SETLK 11 +#define NUTTX_F_SETLKW 12 + +#define NUTTX_F_RDLCK 0 +#define NUTTX_F_WRLCK 1 +#define NUTTX_F_UNLCK 2 + #endif /* __SIM__ */ /**************************************************************************** @@ -149,6 +166,7 @@ typedef uint32_t nuttx_blkcnt_t; typedef unsigned int nuttx_mode_t; typedef int nuttx_fsid_t[2]; +typedef uintptr_t nuttx_file_t; /* These must match the definition in include/time.h */ @@ -203,6 +221,15 @@ struct nuttx_stat_s nuttx_blkcnt_t st_blocks; /* Number of blocks allocated */ }; +struct nuttx_flock_s +{ + int16_t l_type; + int16_t l_whence; + nuttx_off_t l_start; + nuttx_off_t l_len; + int32_t l_pid; +}; + #endif /* __SIM__ */ /**************************************************************************** @@ -214,10 +241,16 @@ int host_open(const char *pathname, int flags, int mode); int host_close(int fd); nuttx_ssize_t host_read(int fd, void *buf, nuttx_size_t count); nuttx_ssize_t host_write(int fd, const void *buf, nuttx_size_t count); +nuttx_ssize_t host_pread(int fd, void *buf, nuttx_size_t count, + nuttx_off_t offset); +nuttx_ssize_t host_pwrite(int fd, const void *buf, nuttx_size_t count, + nuttx_off_t offset); nuttx_off_t host_lseek(int fd, nuttx_off_t pos, nuttx_off_t offset, int whence); +int host_fcntl(int fd, int cmd, ...); int host_ioctl(int fd, int request, unsigned long arg); void host_sync(int fd); +int host_fsync(int fd); int host_dup(int fd); int host_fstat(int fd, struct nuttx_stat_s *buf); int host_fchstat(int fd, const struct nuttx_stat_s *buf, @@ -229,20 +262,31 @@ void host_rewinddir(void *dirp); int host_closedir(void *dirp); int host_statfs(const char *path, struct nuttx_statfs_s *buf); int host_unlink(const char *pathname); +int host_remove(const char *pathname); int host_mkdir(const char *pathname, int mode); int host_rmdir(const char *pathname); int host_rename(const char *oldpath, const char *newpath); int host_stat(const char *path, struct nuttx_stat_s *buf); +int host_access(const char *path, int mode); int host_chstat(const char *path, const struct nuttx_stat_s *buf, int flags); +nuttx_file_t host_popen(const char *command, const char *mode); +char *host_fgets(char *buf, nuttx_size_t len, nuttx_file_t stream); +int host_pclose(nuttx_file_t stream); +int host_system(char *buf, size_t len, const char *fmt, ...); #else int host_open(const char *pathname, int flags, int mode); int host_close(int fd); ssize_t host_read(int fd, void *buf, size_t count); ssize_t host_write(int fd, const void *buf, size_t count); +ssize_t host_pread(int fd, void *buf, size_t count, off_t offset); +ssize_t host_pwrite(int fd, const void *buf, size_t count, + off_t offset); off_t host_lseek(int fd, off_t pos, off_t offset, int whence); +int host_fcntl(int fd, int cmd, ...); int host_ioctl(int fd, int request, unsigned long arg); void host_sync(int fd); +int host_fsync(int fd); int host_dup(int fd); int host_fstat(int fd, struct stat *buf); int host_fchstat(int fd, const struct stat *buf, int flags); @@ -253,12 +297,18 @@ void host_rewinddir(void *dirp); int host_closedir(void *dirp); int host_statfs(const char *path, struct statfs *buf); int host_unlink(const char *pathname); +int host_remove(const char *pathname); int host_mkdir(const char *pathname, int mode); int host_rmdir(const char *pathname); int host_rename(const char *oldpath, const char *newpath); int host_stat(const char *path, struct stat *buf); +int host_access(const char *path, int mode); int host_chstat(const char *path, const struct stat *buf, int flags); +FILE *host_popen(const char *command, const char *mode); +char *host_fgets(char *buf, size_t len, FILE *stream); +int host_pclose(FILE *stream); +int host_system(char *buf, size_t len, const char *fmt, ...); #endif /* __SIM__ */ #endif /* __INCLUDE_NUTTX_FS_HOSTFS_H */