2015-01-28 20:41:24 +01:00
|
|
|
/****************************************************************************
|
2018-01-23 01:32:02 +01:00
|
|
|
* net/udp/udp_psock_sendto_unbuffered.c
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
2019-10-18 22:20:34 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011-2016, 2018-2019 Gregory Nutt. All rights
|
2018-01-23 01:32:02 +01:00
|
|
|
* reserved.
|
2015-01-28 20:41:24 +01:00
|
|
|
* 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>
|
|
|
|
#ifdef CONFIG_NET_UDP
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2015-05-13 15:24:22 +02:00
|
|
|
#include <assert.h>
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2016-11-03 19:17:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-01-28 20:41:24 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/udp.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "arp/arp.h"
|
2015-02-02 20:44:31 +01:00
|
|
|
#include "icmpv6/icmpv6.h"
|
2015-01-28 20:41:24 +01:00
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "udp/udp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-12-31 16:26:14 +01:00
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
/* If both IPv4 and IPv6 support are both enabled, then we will need to build
|
|
|
|
* in some additional domain selection support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
# define NEED_IPDOMAIN_SUPPORT 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct sendto_s
|
|
|
|
{
|
2018-01-22 21:14:32 +01:00
|
|
|
#if defined(CONFIG_NET_SOCKOPTS) || defined(NEED_IPDOMAIN_SUPPORT)
|
2015-01-28 20:41:24 +01:00
|
|
|
FAR struct socket *st_sock; /* Points to the parent socket structure */
|
|
|
|
#endif
|
2019-10-18 22:20:34 +02:00
|
|
|
FAR struct devif_callback_s *st_cb; /* Reference to callback instance */
|
|
|
|
FAR struct net_driver_s *st_dev; /* Driver that will perform the transmission */
|
2018-01-22 21:14:32 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2018-06-16 20:16:13 +02:00
|
|
|
clock_t st_time; /* Last send time for determining timeout */
|
2015-01-28 20:41:24 +01:00
|
|
|
#endif
|
|
|
|
sem_t st_sem; /* Semaphore signals sendto completion */
|
|
|
|
uint16_t st_buflen; /* Length of send buffer (error if <0) */
|
|
|
|
const char *st_buffer; /* Pointer to send buffer */
|
|
|
|
int st_sndlen; /* Result of the send (length sent or negated errno) */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-01-23 01:32:02 +01:00
|
|
|
* Name: sendto_timeout
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check for send timeout.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-01-28 20:41:24 +01:00
|
|
|
* pstate - sendto state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* TRUE:timeout FALSE:no timeout
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-07-05 19:01:16 +02:00
|
|
|
* The network is locked
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-01-22 21:14:32 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2018-01-23 01:32:02 +01:00
|
|
|
static inline int sendto_timeout(FAR struct sendto_s *pstate)
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
|
|
|
FAR struct socket *psock;
|
|
|
|
|
|
|
|
/* Check for a timeout configured via setsockopts(SO_SNDTIMEO).
|
|
|
|
* If none... we well let the send wait forever.
|
|
|
|
*/
|
|
|
|
|
|
|
|
psock = pstate->st_sock;
|
|
|
|
if (psock && psock->s_sndtimeo != 0)
|
|
|
|
{
|
|
|
|
/* Check if the configured timeout has elapsed */
|
|
|
|
|
|
|
|
return net_timeo(pstate->st_time, psock->s_sndtimeo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No timeout */
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-01-22 21:14:32 +01:00
|
|
|
#endif /* CONFIG_NET_SOCKOPTS */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: sendto_ipselect
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If both IPv4 and IPv6 support are enabled, then we will need to select
|
|
|
|
* which one to use when generating the outgoing packet. If only one
|
|
|
|
* domain is selected, then the setup is already in place and we need do
|
|
|
|
* nothing.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-02 18:27:03 +02:00
|
|
|
* dev - The structure of the network driver that caused the event
|
2015-01-28 20:41:24 +01:00
|
|
|
* pstate - sendto state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function must be called with the network locked.
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NEED_IPDOMAIN_SUPPORT
|
|
|
|
static inline void sendto_ipselect(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct sendto_s *pstate)
|
|
|
|
{
|
|
|
|
FAR struct socket *psock = pstate->st_sock;
|
|
|
|
DEBUGASSERT(psock);
|
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Which domain the socket support */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
|
|
|
if (psock->s_domain == PF_INET)
|
|
|
|
{
|
|
|
|
/* Select the IPv4 domain */
|
|
|
|
|
|
|
|
udp_ipv4_select(dev);
|
|
|
|
}
|
2016-02-26 13:45:37 +01:00
|
|
|
else /* if (psock->s_domain == PF_INET6) */
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
|
|
|
/* Select the IPv6 domain */
|
|
|
|
|
2016-02-26 13:45:37 +01:00
|
|
|
DEBUGASSERT(psock->s_domain == PF_INET6);
|
2017-07-05 15:27:17 +02:00
|
|
|
udp_ipv6_select(dev);
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: sendto_eventhandler
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function is called to perform the actual send operation when
|
|
|
|
* polled by the lower, device interfacing layer.
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-09-02 18:27:03 +02:00
|
|
|
* dev The structure of the network driver that caused the event
|
2015-01-28 20:41:24 +01:00
|
|
|
* conn An instance of the UDP connection structure cast to void *
|
|
|
|
* pvpriv An instance of struct sendto_s cast to void*
|
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Modified value of the input flags
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-09-02 18:27:03 +02:00
|
|
|
* This function must be called with the network locked.
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t sendto_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *conn, FAR void *pvpriv,
|
|
|
|
uint16_t flags)
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
|
|
|
FAR struct sendto_s *pstate = (FAR struct sendto_s *)pvpriv;
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
DEBUGASSERT(pstate != NULL && pstate->st_dev != NULL);
|
2019-10-18 22:20:34 +02:00
|
|
|
if (pstate != NULL)
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
2019-10-18 22:20:34 +02:00
|
|
|
/* The TCP socket should be bound to a device. Make sure that the
|
|
|
|
* polling device is the one that we are bound to.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dev != pstate->st_dev)
|
|
|
|
{
|
|
|
|
/* Ignore this poll. Wait for the right device */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
ninfo("flags: %04x\n", flags);
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* If the network device has gone down, then we will have terminate
|
|
|
|
* the wait now with an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
|
|
|
/* Terminate the transfer with an error. */
|
|
|
|
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: Network is down\n");
|
2015-05-29 18:21:06 +02:00
|
|
|
pstate->st_sndlen = -ENETUNREACH;
|
|
|
|
}
|
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Check if the outgoing packet is available. It may have been claimed
|
2017-09-02 18:27:03 +02:00
|
|
|
* by a sendto event serving a different thread -OR- if the output
|
2015-01-28 20:41:24 +01:00
|
|
|
* buffer currently contains unprocessed incoming data. In these cases
|
|
|
|
* we will just have to wait for the next polling cycle.
|
|
|
|
*/
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
else if (dev->d_sndlen > 0 || (flags & UDP_NEWDATA) != 0)
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
2015-10-08 23:10:04 +02:00
|
|
|
/* Another thread has beat us sending data or the buffer is busy,
|
|
|
|
* Check for a timeout. If not timed out, wait for the next
|
|
|
|
* polling cycle and check again.
|
|
|
|
*/
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2018-01-22 21:14:32 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2018-01-23 01:32:02 +01:00
|
|
|
if (sendto_timeout(pstate))
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
|
|
|
/* Yes.. report the timeout */
|
|
|
|
|
2016-06-20 17:37:08 +02:00
|
|
|
nwarn("WARNING: SEND timeout\n");
|
2015-01-28 20:41:24 +01:00
|
|
|
pstate->st_sndlen = -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
else
|
2018-01-22 21:14:32 +01:00
|
|
|
#endif /* CONFIG_NET_SOCKOPTS */
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
2019-07-03 02:02:23 +02:00
|
|
|
/* No timeout. Just wait for the next polling cycle */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
return flags;
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It looks like we are good to send the data */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef NEED_IPDOMAIN_SUPPORT
|
|
|
|
/* If both IPv4 and IPv6 support are enabled, then we will need to
|
|
|
|
* select which one to use when generating the outgoing packet.
|
|
|
|
* If only one domain is selected, then the setup is already in
|
|
|
|
* place and we need do nothing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sendto_ipselect(dev, pstate);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Copy the user data into d_appdata and send it */
|
|
|
|
|
|
|
|
devif_send(dev, pstate->st_buffer, pstate->st_buflen);
|
|
|
|
pstate->st_sndlen = pstate->st_buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
|
|
|
pstate->st_cb->flags = 0;
|
|
|
|
pstate->st_cb->priv = NULL;
|
|
|
|
pstate->st_cb->event = NULL;
|
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&pstate->st_sem);
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: psock_udp_sendto
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the UDP-specific logic of the standard
|
|
|
|
* sendto() socket operation.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* NOTE: All input parameters were verified by sendto() before this
|
|
|
|
* function was called.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
|
|
|
* a negated errno value is returned. See the description in
|
|
|
|
* net/socket/sendto.c for the list of appropriate return value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t psock_udp_sendto(FAR struct socket *psock, FAR const void *buf,
|
|
|
|
size_t len, int flags, FAR const struct sockaddr *to,
|
|
|
|
socklen_t tolen)
|
|
|
|
{
|
|
|
|
FAR struct udp_conn_s *conn;
|
|
|
|
struct sendto_s state;
|
|
|
|
int ret;
|
|
|
|
|
2018-04-25 16:43:38 +02:00
|
|
|
/* If the UDP socket was previously assigned a remote peer address via
|
|
|
|
* connect(), then as with connection-mode socket, sendto() may not be
|
|
|
|
* used with a non-NULL destination address. Normally send() would be
|
|
|
|
* used with such connected UDP sockets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (to != NULL && _SS_ISCONNECTED(psock->s_flags))
|
|
|
|
{
|
2018-04-28 02:26:38 +02:00
|
|
|
/* EISCONN - A destination address was specified and the socket is
|
|
|
|
* already connected.
|
|
|
|
*/
|
|
|
|
|
2018-04-25 16:43:38 +02:00
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
2018-04-27 05:58:09 +02:00
|
|
|
/* Otherwise, if the socket is not connected, then a destination address
|
|
|
|
* must be provided.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (to == NULL && !_SS_ISCONNECTED(psock->s_flags))
|
|
|
|
{
|
2018-04-28 02:26:38 +02:00
|
|
|
/* EDESTADDRREQ - The socket is not connection-mode and no peer\
|
|
|
|
* address is set.
|
|
|
|
*/
|
|
|
|
|
2018-04-27 05:58:09 +02:00
|
|
|
return -EDESTADDRREQ;
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:46:09 +02:00
|
|
|
/* Get the underlying the UDP connection structure. */
|
|
|
|
|
|
|
|
conn = (FAR struct udp_conn_s *)psock->s_conn;
|
|
|
|
DEBUGASSERT(conn);
|
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
#if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR)
|
2015-01-30 15:54:40 +01:00
|
|
|
#ifdef CONFIG_NET_ARP_SEND
|
2018-05-17 15:46:09 +02:00
|
|
|
/* Assure the the IPv4 destination address maps to a valid MAC address in
|
|
|
|
* the ARP table.
|
|
|
|
*/
|
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6_NEIGHBOR
|
|
|
|
if (psock->s_domain == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
2018-05-17 15:46:09 +02:00
|
|
|
in_addr_t destipaddr;
|
|
|
|
|
|
|
|
/* Check if the socket is connection mode */
|
|
|
|
|
|
|
|
if (_SS_ISCONNECTED(psock->s_flags))
|
|
|
|
{
|
|
|
|
/* Yes.. use the connected remote address (the 'to' address is
|
|
|
|
* null).
|
|
|
|
*/
|
|
|
|
|
|
|
|
destipaddr = conn->u.ipv4.raddr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR const struct sockaddr_in *into;
|
|
|
|
|
|
|
|
/* No.. use the destination address provided by the non-NULL 'to'
|
|
|
|
* argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
into = (FAR const struct sockaddr_in *)to;
|
|
|
|
destipaddr = into->sin_addr.s_addr;
|
|
|
|
}
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
/* Make sure that the IP address mapping is in the ARP table */
|
|
|
|
|
2018-05-17 15:46:09 +02:00
|
|
|
ret = arp_send(destipaddr);
|
2015-02-02 20:44:31 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ARP_SEND */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_ICMPv6_NEIGHBOR
|
2018-05-17 15:46:09 +02:00
|
|
|
/* Assure the the IPv6 destination address maps to a valid MAC address in
|
|
|
|
* the neighbor table.
|
|
|
|
*/
|
|
|
|
|
2015-02-02 20:44:31 +01:00
|
|
|
#ifdef CONFIG_NET_ARP_SEND
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2018-05-17 15:46:09 +02:00
|
|
|
FAR const uint16_t *destipaddr;
|
|
|
|
|
|
|
|
/* Check if the socket is connection mode */
|
|
|
|
|
|
|
|
if (_SS_ISCONNECTED(psock->s_flags))
|
|
|
|
{
|
|
|
|
/* Yes.. use the connected remote address (the 'to' address is
|
|
|
|
* null).
|
|
|
|
*/
|
|
|
|
|
|
|
|
destipaddr = conn->u.ipv6.raddr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR const struct sockaddr_in6 *into;
|
|
|
|
|
|
|
|
/* No.. use the destination address provided by the non-NULL 'to'
|
|
|
|
* argument.
|
|
|
|
*/
|
|
|
|
|
|
|
|
into = (FAR const struct sockaddr_in6 *)to;
|
|
|
|
destipaddr = into->sin6_addr.s6_addr16;
|
|
|
|
}
|
2015-02-02 20:44:31 +01:00
|
|
|
|
|
|
|
/* Make sure that the IP address mapping is in the Neighbor Table */
|
2015-01-30 15:54:40 +01:00
|
|
|
|
2018-05-17 15:46:09 +02:00
|
|
|
ret = icmpv6_neighbor(destipaddr);
|
2015-02-02 20:44:31 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ICMPv6_NEIGHBOR */
|
|
|
|
|
|
|
|
/* Did we successfully get the address mapping? */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Peer not reachable\n");
|
2015-01-28 20:41:24 +01:00
|
|
|
return -ENETUNREACH;
|
|
|
|
}
|
2015-02-02 20:44:31 +01:00
|
|
|
#endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Initialize the state structure. This is done with the network
|
|
|
|
* locked because we don't want anything to happen until we are
|
|
|
|
* ready.
|
2015-01-28 20:41:24 +01:00
|
|
|
*/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-01-28 20:41:24 +01:00
|
|
|
memset(&state, 0, sizeof(struct sendto_s));
|
2016-11-03 19:17:02 +01:00
|
|
|
|
|
|
|
/* This semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&state.st_sem, 0, 0);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_setprotocol(&state.st_sem, SEM_PRIO_NONE);
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
state.st_buflen = len;
|
|
|
|
state.st_buffer = buf;
|
|
|
|
|
2018-01-22 21:14:32 +01:00
|
|
|
#if defined(CONFIG_NET_SOCKOPTS) || defined(NEED_IPDOMAIN_SUPPORT)
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Save the reference to the socket structure if it will be needed for
|
|
|
|
* asynchronous processing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
state.st_sock = psock;
|
|
|
|
#endif
|
|
|
|
|
2018-01-22 21:14:32 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Set the initial time for calculating timeouts */
|
|
|
|
|
|
|
|
state.st_time = clock_systimer();
|
|
|
|
#endif
|
|
|
|
|
2018-04-27 05:58:09 +02:00
|
|
|
/* Check if the socket is connected */
|
|
|
|
|
|
|
|
if (!_SS_ISCONNECTED(psock->s_flags))
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
2018-04-27 05:58:09 +02:00
|
|
|
/* No.. Call udp_connect() to set the remote address in the connection
|
|
|
|
* structure to the sendto() destination address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = udp_connect(conn, to);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: udp_connect failed: %d\n", ret);
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* Get the device that will handle the remote packet transfers. This
|
|
|
|
* should never be NULL.
|
|
|
|
*/
|
|
|
|
|
2019-10-18 22:20:34 +02:00
|
|
|
state.st_dev = udp_find_raddr_device(conn);
|
|
|
|
if (state.st_dev == NULL)
|
2015-05-29 18:21:06 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: udp_find_raddr_device failed\n");
|
2015-05-29 18:21:06 +02:00
|
|
|
ret = -ENETUNREACH;
|
|
|
|
goto errout_with_lock;
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
2015-05-29 18:21:06 +02:00
|
|
|
|
2018-01-23 01:46:58 +01:00
|
|
|
/* Make sure that the device is in the UP state */
|
|
|
|
|
2019-10-18 22:20:34 +02:00
|
|
|
if ((state.st_dev->d_flags & IFF_UP) == 0)
|
2018-01-23 01:46:58 +01:00
|
|
|
{
|
|
|
|
nwarn("WARNING: device is DOWN\n");
|
|
|
|
ret = -EHOSTUNREACH;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2019-10-18 22:20:34 +02:00
|
|
|
state.st_cb = udp_callback_alloc(state.st_dev, conn);
|
2015-01-28 20:41:24 +01:00
|
|
|
if (state.st_cb)
|
|
|
|
{
|
2015-05-29 18:21:06 +02:00
|
|
|
state.st_cb->flags = (UDP_POLL | NETDEV_DOWN);
|
2015-10-08 23:10:04 +02:00
|
|
|
state.st_cb->priv = (FAR void *)&state;
|
2017-08-29 22:08:04 +02:00
|
|
|
state.st_cb->event = sendto_eventhandler;
|
2015-01-28 20:41:24 +01:00
|
|
|
|
|
|
|
/* Notify the device driver of the availability of TX data */
|
|
|
|
|
2019-10-18 22:20:34 +02:00
|
|
|
netdev_txnotify_dev(state.st_dev);
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Wait for either the receive to complete or for an error/timeout to
|
|
|
|
* occur. NOTES: net_lockedwait will also terminate if a signal
|
|
|
|
* is received.
|
2015-01-28 20:41:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
net_lockedwait(&state.st_sem);
|
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Make sure that no further events are processed */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2019-10-18 22:20:34 +02:00
|
|
|
udp_callback_free(state.st_dev, conn, state.st_cb);
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* The result of the sendto operation is the number of bytes transferred */
|
|
|
|
|
|
|
|
ret = state.st_sndlen;
|
|
|
|
|
|
|
|
errout_with_lock:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* Release the semaphore */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&state.st_sem);
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2015-05-29 18:21:06 +02:00
|
|
|
/* Unlock the network and return the result of the sendto() operation */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-05-29 18:21:06 +02:00
|
|
|
return ret;
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 18:10:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_udp_cansend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_udp_cansend() returns a value indicating if a write to the socket
|
|
|
|
* would block. It is still possible that the write may block if another
|
|
|
|
* write occurs first.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock An instance of the internal socket structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2019-11-24 17:30:48 +01:00
|
|
|
* OK (Always can send).
|
2018-09-12 18:10:54 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int psock_udp_cansend(FAR struct socket *psock)
|
|
|
|
{
|
2019-11-24 17:30:48 +01:00
|
|
|
return OK;
|
2018-09-12 18:10:54 +02:00
|
|
|
}
|
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
#endif /* CONFIG_NET_UDP */
|