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
|
|
|
*
|
2015-01-28 20:41:24 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2015 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>
|
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-06-25 02:55:01 +02:00
|
|
|
#include "udp/udp.h"
|
2017-04-02 19:15:46 +02:00
|
|
|
#include "sixlowpan/sixlowpan.h"
|
2015-01-28 20:41:24 +01:00
|
|
|
#include "local/local.h"
|
2014-07-05 00:38:51 +02:00
|
|
|
#include "socket/socket.h"
|
2017-03-31 16:58:14 +02:00
|
|
|
#include "usrsock/usrsock.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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-30 22:29:59 +01:00
|
|
|
* Function: 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-28 20:41:24 +01:00
|
|
|
socklen_t minlen;
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_LOCAL_DGRAM) || \
|
|
|
|
defined(CONFIG_NET_USRSOCK)
|
2015-01-29 18:05:04 +01:00
|
|
|
ssize_t nsent;
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
if (!to || !tolen)
|
|
|
|
{
|
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-03-31 16:58:14 +02:00
|
|
|
#ifdef CONFIG_NET_USRSOCK
|
|
|
|
if (psock->s_type == SOCK_USRSOCK_TYPE)
|
|
|
|
{
|
|
|
|
/* Perform the usrsock sendto operation */
|
|
|
|
|
|
|
|
nsent = usrsock_sendto(psock, buf, len, to, tolen);
|
|
|
|
if (nsent < 0)
|
|
|
|
{
|
|
|
|
errcode = -nsent;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsent;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
2015-01-18 17:33:27 +01:00
|
|
|
switch (to->sa_family)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
case AF_INET:
|
|
|
|
minlen = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-01-18 17:33:27 +01:00
|
|
|
case AF_INET6:
|
|
|
|
minlen = sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif
|
2015-01-18 17:33:27 +01:00
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-27 01:47:48 +01:00
|
|
|
case AF_LOCAL:
|
|
|
|
minlen = sizeof(sa_family_t);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2015-01-18 17:33:27 +01:00
|
|
|
default:
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Unrecognized address family: %d\n", to->sa_family);
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EAFNOSUPPORT;
|
2015-01-18 17:33:27 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tolen < minlen)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Invalid address length: %d < %d\n", tolen, minlen);
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2007-09-03 22:34:44 +02:00
|
|
|
goto errout;
|
2015-01-18 17:33:27 +01:00
|
|
|
}
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2012-01-30 22:29:59 +01:00
|
|
|
/* Verify that the psock corresponds to valid, allocated socket */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Invalid socket\n");
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2007-09-03 22:34:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is a connected socket, then return EISCONN */
|
|
|
|
|
|
|
|
if (psock->s_type != SOCK_DGRAM)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Connected socket\n");
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EISCONN;
|
2007-09-03 22:34:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_LOCAL_DGRAM)
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Now handle the sendto() operation according to the socket domain,
|
|
|
|
* currently either IP or Unix domains.
|
|
|
|
*/
|
2014-08-19 00:22:14 +02:00
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2007-09-03 22:34:44 +02:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-28 20:41:24 +01:00
|
|
|
if (psock->s_domain == PF_LOCAL)
|
2015-01-17 14:42:09 +01:00
|
|
|
#endif
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2015-01-29 18:05:04 +01:00
|
|
|
nsent = psock_local_sendto(psock, buf, len, flags, to, tolen);
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-28 20:41:24 +01:00
|
|
|
else
|
|
|
|
#endif
|
2008-09-01 15:59:54 +02:00
|
|
|
{
|
2017-04-02 19:15:46 +02:00
|
|
|
#if defined(CONFIG_NET_6LOWPAN)
|
|
|
|
/* Try 6loWPAN UDP packet sendto() */
|
|
|
|
|
|
|
|
nsent = psock_6lowpan_udp_sendto(psock, buf, len, flags, to, tolen);
|
|
|
|
|
|
|
|
#if defined(CONFIG_NETDEV_MULTINIC) && defined(NET_UDP_HAVE_STACK)
|
|
|
|
if (nsent < 0)
|
|
|
|
{
|
|
|
|
/* UDP/IP packet sendto */
|
|
|
|
|
|
|
|
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NETDEV_MULTINIC && NET_UDP_HAVE_STACK */
|
|
|
|
#elif defined(NET_UDP_HAVE_STACK)
|
2015-01-29 18:05:04 +01:00
|
|
|
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
|
2017-03-31 16:58:14 +02:00
|
|
|
#else
|
|
|
|
nsent = -ENOSYS;
|
2017-04-02 19:15:46 +02:00
|
|
|
#endif /* CONFIG_NET_6LOWPAN */
|
2008-09-01 15:59:54 +02:00
|
|
|
}
|
2015-01-28 20:41:24 +01:00
|
|
|
#endif /* CONFIG_NET_UDP */
|
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
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: UDP or Unix domain sendto() 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
|
|
|
#else
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOSYS;
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_UDP || CONFIG_NET_LOCAL_DGRAM */
|
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
|
|
|
/****************************************************************************
|
|
|
|
* Function: 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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
}
|