123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /****************************************************************************
- * net/socket/sendto.c
- *
- * Copyright (C) 2007-2009, 2011-2015, 2017 Gregory Nutt. All rights reserved.
- * Author: Gregory Nutt <gnutt@nuttx.org>
- *
- * 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 <nuttx/config.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdint.h>
- #include <assert.h>
- #include <errno.h>
- #include <debug.h>
- #include <nuttx/cancelpt.h>
- #include <nuttx/net/net.h>
- #include "socket/socket.h"
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: psock_sendto
- *
- * Description:
- * If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
- * socket, the parameters to and 'tolen' are ignored (and the error EISCONN
- * may be returned when they are not NULL and 0), and the error ENOTCONN is
- * returned when the socket was not actually connected.
- *
- * Input Parameters:
- * psock A pointer to a NuttX-specific, internal socket structure
- * buf Data to send
- * len Length of data to send
- * flags Send flags
- * to Address of recipient
- * tolen The length of the address structure
- *
- * Returned Value:
- * On success, returns the number of characters sent. On a negated errno
- * value is returned. One of:
- *
- * EAGAIN or EWOULDBLOCK
- * The socket is marked non-blocking and the requested operation
- * would block.
- * EBADF
- * An invalid descriptor was specified.
- * ECONNRESET
- * Connection reset by peer.
- * EDESTADDRREQ
- * The socket is not connection-mode, and no peer address is set.
- * EFAULT
- * An invalid user space address was specified for a parameter.
- * EINTR
- * A signal occurred before any data was transmitted.
- * EINVAL
- * Invalid argument passed.
- * EISCONN
- * The connection-mode socket was connected already but a recipient
- * was specified. (Now either this error is returned, or the recipient
- * specification is ignored.)
- * EMSGSIZE
- * The socket type requires that message be sent atomically, and the
- * size of the message to be sent made this impossible.
- * ENOBUFS
- * The output queue for a network interface was full. This generally
- * indicates that the interface has stopped sending, but may be
- * caused by transient congestion.
- * ENOMEM
- * No memory available.
- * ENOTCONN
- * The socket is not connected, and no target has been given.
- * ENOTSOCK
- * The argument s is not a socket.
- * EOPNOTSUPP
- * Some bit in the flags argument is inappropriate for the socket
- * type.
- * EPIPE
- * The local end has been shut down on a connection oriented socket.
- * In this case the process will also receive a SIGPIPE unless
- * MSG_NOSIGNAL is set.
- *
- ****************************************************************************/
- ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf,
- size_t len, int flags, FAR const struct sockaddr *to,
- socklen_t tolen)
- {
- ssize_t nsent;
- /* Verify that non-NULL pointers were passed */
- #ifdef CONFIG_DEBUG_FEATURES
- if (buf == NULL)
- {
- return -EINVAL;
- }
- #endif
- /* If to is NULL or tolen is zero, then this function is same as send (for
- * connected socket types)
- */
- if (to == NULL || tolen <= 0)
- {
- #if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_LOCAL_STREAM) || \
- defined(CONFIG_NET_USRSOCK)
- return psock_send(psock, buf, len, flags);
- #else
- nerr("ERROR: No 'to' address\n");
- return -EINVAL;
- #endif
- }
- /* Verify that the psock corresponds to valid, allocated socket */
- if (psock == NULL || psock->s_crefs <= 0)
- {
- nerr("ERROR: Invalid socket\n");
- return -EBADF;
- }
- /* Let the address family's sendto() method handle the operation */
- DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_sendto != NULL);
- nsent = psock->s_sockif->si_sendto(psock, buf, len, flags, to, tolen);
- /* Check if the domain-specific sendto() logic failed */
- if (nsent < 0)
- {
- nerr("ERROR: Family-specific send failed: %ld\n", (long)nsent);
- return nsent;
- }
- return nsent;
- }
- /****************************************************************************
- * Name: sendto
- *
- * Description:
- * If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
- * socket, the parameters to and 'tolen' are ignored (and the error EISCONN
- * may be returned when they are not NULL and 0), and the error ENOTCONN is
- * returned when the socket was not actually connected.
- *
- * Input Parameters:
- * sockfd Socket descriptor of socket
- * buf Data to send
- * len Length of data to send
- * flags Send flags
- * to Address of recipient
- * tolen The length of the address structure
- *
- * Returned Value:
- * On success, returns the number of characters sent. On any failure, a
- * negated errno value is returned. One of:
- *
- * EAGAIN or EWOULDBLOCK
- * The socket is marked non-blocking and the requested operation
- * would block.
- * EBADF
- * An invalid descriptor was specified.
- * ECONNRESET
- * Connection reset by peer.
- * EDESTADDRREQ
- * The socket is not connection-mode, and no peer address is set.
- * EFAULT
- * An invalid user space address was specified for a parameter.
- * EINTR
- * A signal occurred before any data was transmitted.
- * EINVAL
- * Invalid argument passed.
- * EISCONN
- * The connection-mode socket was connected already but a recipient
- * was specified. (Now either this error is returned, or the recipient
- * specification is ignored.)
- * EMSGSIZE
- * The socket type requires that message be sent atomically, and the
- * size of the message to be sent made this impossible.
- * ENOBUFS
- * The output queue for a network interface was full. This generally
- * indicates that the interface has stopped sending, but may be
- * caused by transient congestion.
- * ENOMEM
- * No memory available.
- * ENOTCONN
- * The socket is not connected, and no target has been given.
- * ENOTSOCK
- * The argument s is not a socket.
- * EOPNOTSUPP
- * Some bit in the flags argument is inappropriate for the socket
- * type.
- * EPIPE
- * The local end has been shut down on a connection oriented socket.
- * In this case the process will also receive a SIGPIPE unless
- * MSG_NOSIGNAL is set.
- *
- ****************************************************************************/
- ssize_t sendto(int sockfd, FAR const void *buf, size_t len, int flags,
- FAR const struct sockaddr *to, socklen_t tolen)
- {
- FAR struct socket *psock;
- ssize_t ret;
- /* sendto() is a cancellation point */
- (void)enter_cancellation_point();
- /* Get the underlying socket structure */
- psock = sockfd_socket(sockfd);
- /* And let psock_sendto do all of the work */
- ret = psock_sendto(psock, buf, len, flags, to, tolen);
- if (ret < 0)
- {
- set_errno((int)-ret);
- ret = ERROR;
- }
- leave_cancellation_point();
- return ret;
- }
|