From 88db584566822a03cb05f0b9df3d6e594622f518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20B=C3=A4r?= Date: Sat, 11 Jun 2022 10:00:36 +0200 Subject: [PATCH] libc: timex, socket_fs_recvmsg and sendmsg - Added timex.h as provided header file - implementation of socket_fs_recvmsg - implementation of sendmsg Changes needed for chrony port. Issue #4526 --- repos/libports/lib/mk/libc-net.mk | 6 ++ repos/libports/ports/libc.hash | 2 +- repos/libports/ports/libc.port | 2 +- repos/libports/src/lib/libc/sendmsg.c | 76 ++++++++++++++++++ .../libports/src/lib/libc/socket_fs_plugin.cc | 80 ++++++++++++++++++- 5 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 repos/libports/src/lib/libc/sendmsg.c diff --git a/repos/libports/lib/mk/libc-net.mk b/repos/libports/lib/mk/libc-net.mk index 3fb1e946fe..a3b7ed666f 100644 --- a/repos/libports/lib/mk/libc-net.mk +++ b/repos/libports/lib/mk/libc-net.mk @@ -15,6 +15,9 @@ SRC_C += gethostnamadr.c gethostbydns.c gethostbyht.c map_v4v6.c # needed for getprotobyname() SRC_C += getprotoent.c getprotoname.c +# needed by send() +SRC_C += sendmsg.c + # defines in6addr_any SRC_C += vars.c @@ -53,4 +56,7 @@ nsparser.c: nsparser.y vpath nsparser.y $(LIBC_NET_DIR) +vpath sendmsg.c $(REP_DIR)/src/lib/libc + CC_CXX_WARN_STRICT = + diff --git a/repos/libports/ports/libc.hash b/repos/libports/ports/libc.hash index a332d026c5..a471669f14 100644 --- a/repos/libports/ports/libc.hash +++ b/repos/libports/ports/libc.hash @@ -1 +1 @@ -c7cd230b11ca71979f32950803bc78b45adfa0ce +b856473bd43dea76e963f35bdbdd1d49d2d70e6b diff --git a/repos/libports/ports/libc.port b/repos/libports/ports/libc.port index b276c57968..9a3911546c 100644 --- a/repos/libports/ports/libc.port +++ b/repos/libports/ports/libc.port @@ -129,7 +129,7 @@ DIR_CONTENT(include/libc/sys) := \ utsname.h elf.h mtio.h _stdint.h atomic_common.h _ucontext.h \ _cpuset.h _bitset.h bitset.h _stdarg.h _uio.h auxv.h random.h \ _sockaddr_storage.h termios.h _termios.h _umtx.h kerneldump.h \ - conf.h disk_zone.h counter.h soundcard.h pciio.h) + conf.h disk_zone.h counter.h soundcard.h pciio.h timex.h) DIRS += include/libc/sys/disk DIR_CONTENT(include/libc/sys/disk) := $(D)/sys/sys/disk/*h diff --git a/repos/libports/src/lib/libc/sendmsg.c b/repos/libports/src/lib/libc/sendmsg.c new file mode 100644 index 0000000000..2c61f80253 --- /dev/null +++ b/repos/libports/src/lib/libc/sendmsg.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + /* + * NOTE: @author : Aditya Kousik + * @email : adit267.kousik@gmail.com + * + * This implementation is for the libc-net port of Genode + * + */ + +#include + +#include +#include +#include + +#include +#include + +#include + +ssize_t sendmsg(int s, const struct msghdr *msg, int flags) +{ + ssize_t ret; + size_t tot = 0; + int i; + char *buf, *p; + struct iovec *iov = msg->msg_iov; + + for(i = 0; i < msg->msg_iovlen; ++i) + tot += iov[i].iov_len; + buf = malloc(tot); + if (tot != 0 && buf == NULL) { + //errno = ENOMEM; + return -1; + } + p = buf; + for (i = 0; i < msg->msg_iovlen; ++i) { + memcpy (p, iov[i].iov_base, iov[i].iov_len); + p += iov[i].iov_len; + } + ret = _sendto(s, buf, tot, flags, msg->msg_name, msg->msg_namelen); + free (buf); + return ret; +} \ No newline at end of file diff --git a/repos/libports/src/lib/libc/socket_fs_plugin.cc b/repos/libports/src/lib/libc/socket_fs_plugin.cc index e9d7a044dd..6e68efe583 100644 --- a/repos/libports/src/lib/libc/socket_fs_plugin.cc +++ b/repos/libports/src/lib/libc/socket_fs_plugin.cc @@ -632,7 +632,7 @@ extern "C" int socket_fs_bind(int libc_fd, sockaddr const *addr, socklen_t addrl if (!addr) return Errno(EFAULT); if (addr->sa_family != AF_INET) { - error(__func__, ": family not supported"); + error(__func__, ": family ", addr->sa_family, " not supported"); return Errno(EAFNOSUPPORT); } @@ -842,8 +842,77 @@ extern "C" ssize_t socket_fs_recv(int libc_fd, void *buf, ::size_t len, int flag extern "C" ssize_t socket_fs_recvmsg(int libc_fd, msghdr *msg, int flags) { - warning("########## TODO ########## ", __func__); - return 0; + /* TODO just a simple implementation that handles the easy cases */ + size_t numberOfBytes = 0; + char *data = nullptr; + size_t length = 0; + size_t i; + char *buffer; + ssize_t res; + size_t amount; + socklen_t client_address_len; + + /* iterate over all msg_iov to get the number of bytes that have to be read. */ + for (i = 0; i < msg->msg_iovlen; i++) { + numberOfBytes += msg->msg_iov[i].iov_len; + /* As an optimization, we set the initial values of DATA and LEN + from the first non-empty iovec. This kicks-in in the case + where the whole packet fits into the first iovec buffer. */ + if (data == nullptr && msg->msg_iov[i].iov_len > 0) { + data = (char*)msg->msg_iov[i].iov_base; + length = msg->msg_iov[i].iov_len; + } + } + + buffer = data; + + struct sockaddr_in client_address; + client_address_len = sizeof (client_address); + + /* do socket communication */ + res = socket_fs_recvfrom(libc_fd, buffer, length, flags, + (struct sockaddr *) &client_address, + &client_address_len); + + if(res < 0) { + return res; + } + + /* copy client address to msg_name */ + if (msg->msg_name != nullptr && client_address_len > 0) { + if (msg->msg_namelen > client_address_len) { + msg->msg_namelen = client_address_len; + } + + ::memcpy (msg->msg_name, &client_address, msg->msg_namelen); + } else if (msg->msg_name != nullptr) { + msg->msg_namelen = 0; + } + + /* handle payload */ + if (buffer == data) { + buffer += length; + } else { + amount = length; + buffer = data; + for (i = 0; i < msg->msg_iovlen; i++) { +#define min(a, b) ((a) > (b) ? (b) : (a)) + size_t copy = min (msg->msg_iov[i].iov_len, amount); + ::memcpy (msg->msg_iov[i].iov_base, buffer, copy); + buffer += copy; + amount -= copy; + if (length == 0) + break; + } + + Libc::Allocator alloc { }; + destroy(alloc, data); + } + + /* handle control data, not supported yet */ + msg->msg_controllen = 0; + + return res; } @@ -1150,6 +1219,11 @@ int Socket_fs::Plugin::fcntl(File_descriptor *fd, int cmd, long arg) if (!context) return Errno(EBADF); switch (cmd) { + case F_GETFD: + return context->fd_flags(); + case F_SETFD: + context->fd_flags(arg); + return 0; case F_GETFL: return context->fd_flags() | O_RDWR; case F_SETFL: