2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/sendto.c
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
2017-07-14 22:41:46 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011-2015, 2017 Gregory Nutt. All rights reserved.
|
2012-01-30 22:29:59 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* 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>
|
2009-12-15 16:57:25 +01:00
|
|
|
#include <stdint.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
#include <assert.h>
|
2007-09-03 22:34:44 +02:00
|
|
|
#include <errno.h>
|
2007-11-30 21:46:29 +01:00
|
|
|
#include <debug.h>
|
2013-04-30 16:12:54 +02:00
|
|
|
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2014-07-05 00:38:51 +02:00
|
|
|
#include "socket/socket.h"
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2015-01-17 15:33:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: psock_sendto
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
|
2007-09-09 19:20:56 +02:00
|
|
|
* socket, the parameters to and 'tolen' are ignored (and the error EISCONN
|
2007-09-03 22:34:44 +02:00
|
|
|
* may be returned when they are not NULL and 0), and the error ENOTCONN is
|
|
|
|
* returned when the socket was not actually connected.
|
|
|
|
*
|
|
|
|
* Parameters:
|
2012-01-30 22:29:59 +01:00
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
2007-09-03 22:34:44 +02:00
|
|
|
* 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 error,
|
|
|
|
* -1 is returned, and errno is set appropriately:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
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)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2015-01-29 18:05:04 +01:00
|
|
|
ssize_t nsent;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
DEBUGASSERT(psock != NULL && buf != NULL);
|
|
|
|
|
2007-11-22 22:59:30 +01:00
|
|
|
/* If to is NULL or tolen is zero, then this function is same as send (for
|
|
|
|
* connected socket types)
|
|
|
|
*/
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
if (to == NULL || tolen <= 0)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_LOCAL_STREAM) || \
|
|
|
|
defined(CONFIG_NET_USRSOCK)
|
2012-01-30 22:29:59 +01:00
|
|
|
return psock_send(psock, buf, len, flags);
|
2007-11-22 22:59:30 +01:00
|
|
|
#else
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: No 'to' address\n");
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EINVAL;
|
2007-11-22 22:59:30 +01:00
|
|
|
goto errout;
|
|
|
|
#endif
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
/* Verify that the psock corresponds to valid, allocated socket */
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2017-07-14 20:48:53 +02:00
|
|
|
if (psock == NULL || psock->s_crefs <= 0)
|
2008-09-01 15:59:54 +02:00
|
|
|
{
|
2017-07-13 17:27:56 +02:00
|
|
|
nerr("ERROR: Invalid socket\n");
|
|
|
|
errcode = EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
2017-07-12 23:07:32 +02:00
|
|
|
|
2017-07-14 20:48:53 +02:00
|
|
|
/* Let the address family's sendto() method handle the operation */
|
2017-07-12 23:07:32 +02:00
|
|
|
|
2017-07-14 20:48:53 +02:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_sendto != NULL);
|
|
|
|
nsent = psock->s_sockif->si_sendto(psock, buf, len, flags, to, tolen);
|
2007-09-09 21:47:52 +02:00
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Check if the domain-specific sendto() logic failed */
|
2007-09-16 00:45:45 +02:00
|
|
|
|
2015-01-29 18:05:04 +01:00
|
|
|
if (nsent < 0)
|
2007-09-16 00:45:45 +02:00
|
|
|
{
|
2017-07-12 23:07:32 +02:00
|
|
|
nerr("ERROR: Family-specific send failed: %ld\n", (long)nsent);
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = -nsent;
|
2007-09-16 00:45:45 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-01-29 18:05:04 +01:00
|
|
|
return nsent;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
errout:
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2007-09-03 22:34:44 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: sendto
|
2012-01-30 22:29:59 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 error,
|
|
|
|
* -1 is returned, and errno is set appropriately:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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;
|
2016-12-09 22:17:58 +01:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* sendto() is a cancellation point */
|
|
|
|
|
2016-12-10 15:36:58 +01:00
|
|
|
(void)enter_cancellation_point();
|
2012-01-30 22:29:59 +01:00
|
|
|
|
|
|
|
/* Get the underlying socket structure */
|
|
|
|
|
|
|
|
psock = sockfd_socket(sockfd);
|
|
|
|
|
|
|
|
/* And let psock_sendto do all of the work */
|
|
|
|
|
2016-12-09 22:17:58 +01:00
|
|
|
ret = psock_sendto(psock, buf, len, flags, to, tolen);
|
|
|
|
leave_cancellation_point();
|
|
|
|
return ret;
|
2012-01-30 22:29:59 +01:00
|
|
|
}
|