diff --git a/include/sys/socket.h b/include/sys/socket.h index b66c1783bc8..50a9cc0c29d 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -42,6 +42,7 @@ ****************************************************************************/ #include +#include /**************************************************************************** * Pre-processor Definitions @@ -228,6 +229,25 @@ # define SOMAXCONN 0 #endif +/* Definitions associated with sendmsg/recvmsg */ + +#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg)) + +#define CMSG_ALIGN(len) \ + (((len)+sizeof(long)-1) & ~(sizeof(long)-1)) +#define CMSG_DATA(cmsg) \ + ((void *)((char *)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr)))) +#define CMSG_SPACE(len) \ + (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len)) +#define CMSG_LEN(len) \ + (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) + +#define __CMSG_FIRSTHDR(ctl, len) \ + ((len) >= sizeof(struct cmsghdr) ? (FAR struct cmsghdr *)(ctl) : \ + (FAR struct cmsghdr *)NULL) +#define CMSG_FIRSTHDR(msg) \ + __CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen) + /**************************************************************************** * Type Definitions ****************************************************************************/ @@ -272,6 +292,49 @@ struct linger int l_linger; /* Linger time, in seconds. */ }; +struct msghdr +{ + void *msg_name; /* Socket name */ + int msg_namelen; /* Length of name */ + struct iovec *msg_iov; /* Data blocks */ + unsigned long msg_iovlen; /* Number of blocks */ + void *msg_control; /* Per protocol magic (eg BSD file descriptor passing) */ + unsigned long msg_controllen; /* Length of cmsg list */ + unsigned int msg_flags; +}; + +struct cmsghdr +{ + unsigned long cmsg_len; /* Data byte count, including hdr */ + int cmsg_level; /* Originating protocol */ + int cmsg_type; /* Protocol-specific type */ +}; + +/**************************************************************************** + * Inline Functions + ****************************************************************************/ + +static inline struct cmsghdr *__cmsg_nxthdr(FAR void *__ctl, + unsigned int __size, + FAR struct cmsghdr *__cmsg) +{ + FAR struct cmsghdr *__ptr; + + __ptr = (struct cmsghdr *)(((unsigned char *)__cmsg) + CMSG_ALIGN(__cmsg->cmsg_len)); + if ((unsigned long)((char *)(__ptr + 1) - (char *)__ctl) > __size) + { + return (struct cmsghdr *)0; + } + + return __ptr; +} + +static inline struct cmsghdr *cmsg_nxthdr(FAR struct msghdr *__msg, + FAR struct cmsghdr *__cmsg) +{ + return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg); +} + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -312,6 +375,9 @@ int getsockname(int sockfd, FAR struct sockaddr *addr, int getpeername(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen); +ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags); +ssize_t sendmsg(int sockfd, FAR struct msghdr *msg, int flags); + #undef EXTERN #if defined(__cplusplus) } diff --git a/libs/libc/net/Make.defs b/libs/libc/net/Make.defs index e67b04cfaf9..15ec22ecf03 100644 --- a/libs/libc/net/Make.defs +++ b/libs/libc/net/Make.defs @@ -40,7 +40,7 @@ CSRCS += lib_inetaddr.c lib_inetaton.c lib_inetntoa.c CSRCS += lib_inetntop.c lib_inetpton.c ifeq ($(CONFIG_NET),y) -CSRCS += lib_shutdown.c +CSRCS += lib_recvmsg.c lib_sendmsg.c lib_shutdown.c endif # Routing table support diff --git a/libs/libc/net/lib_recvmsg.c b/libs/libc/net/lib_recvmsg.c new file mode 100644 index 00000000000..984a16fff37 --- /dev/null +++ b/libs/libc/net/lib_recvmsg.c @@ -0,0 +1,89 @@ +/**************************************************************************** + * libc/net/lib_recvmsg.c + * + * Copyright (C) 2007, 2008, 2012, 2008 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#ifdef CONFIG_NET + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: recvmsg + * + * Description: + * The recvmsg() call is identical to recvfrom() with a NULL from parameter. + * + * Parameters: + * sockfd Socket descriptor of socket + * buf Buffer to receive data + * len Length of buffer + * flags Receive flags + * + * Returned Value: + * (see recvfrom) + * + * Assumptions: + * + ****************************************************************************/ + +ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags) +{ + FAR void *buf = msg->msg_iov->iov_base; + FAR struct sockaddr *from = msg->msg_name; + FAR socklen_t *fromlen = (FAR socklen_t *)&msg->msg_namelen; + size_t len = msg->msg_iov->iov_len; + + if (msg->msg_iovlen == 1) + { + return recvfrom(sockfd, buf, len, flags, from, fromlen); + } + else + { + set_errno(ENOTSUP); + return ERROR; + } +} + +#endif /* CONFIG_NET */ diff --git a/libs/libc/net/lib_sendmsg.c b/libs/libc/net/lib_sendmsg.c new file mode 100644 index 00000000000..0d09faa354a --- /dev/null +++ b/libs/libc/net/lib_sendmsg.c @@ -0,0 +1,89 @@ +/**************************************************************************** + * libc/net/lib_sendmsg.c + * + * Copyright (C) 2007, 2008, 2012, 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#ifdef CONFIG_NET + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: sendmsg + * + * Description: + * The sendmsg() call is identical to sendto() with a NULL from parameter. + * + * Parameters: + * sockfd Socket descriptor of socket + * buf Buffer to send data + * len Length of buffer + * flags Receive flags + * + * Returned Value: + * (see sendto) + * + * Assumptions: + * + ****************************************************************************/ + +ssize_t sendmsg(int sockfd, FAR struct msghdr *msg, int flags) +{ + FAR void *buf = msg->msg_iov->iov_base; + FAR struct sockaddr *to = msg->msg_name; + socklen_t tolen = msg->msg_namelen; + size_t len = msg->msg_iov->iov_len; + + if (msg->msg_iovlen == 1) + { + return sendto(sockfd, buf, len, flags, to, tolen); + } + else + { + set_errno(ENOTSUP); + return ERROR; + } +} + +#endif /* CONFIG_NET */