2015-01-28 20:41:24 +01:00
|
|
|
/****************************************************************************
|
2020-01-21 09:43:14 +01:00
|
|
|
* net/udp/udp_sendto_unbuffered.c
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-01-28 20:41:24 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
{
|
2022-11-24 08:45:09 +01:00
|
|
|
FAR struct udp_conn_s *st_conn; /* The UDP connection of interest */
|
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 */
|
2015-01-28 20:41:24 +01:00
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
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)
|
|
|
|
{
|
2022-11-24 08:45:09 +01:00
|
|
|
FAR struct udp_conn_s *conn = pstate->st_conn;
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Which domain the socket support */
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2023-08-18 11:54:07 +02:00
|
|
|
if (conn->domain == PF_INET ||
|
|
|
|
(conn->domain == PF_INET6 &&
|
|
|
|
ip6_is_ipv4addr((FAR struct in6_addr *)conn->u.ipv6.raddr)))
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
|
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
|
|
|
* 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,
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct sendto_s *pstate = pvpriv;
|
2015-01-28 20:41:24 +01:00
|
|
|
|
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,
|
2020-01-04 11:37:46 +01:00
|
|
|
* wait for the next polling cycle and check again.
|
2015-10-08 23:10:04 +02:00
|
|
|
*/
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
return flags;
|
2015-01-28 20:41:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* It looks like we are good to send the data */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2023-01-11 09:09:20 +01:00
|
|
|
/* Copy the user data into d_appdata and send it */
|
|
|
|
|
2023-05-29 23:26:19 +02:00
|
|
|
int ret = devif_send(dev, pstate->st_buffer, pstate->st_buflen,
|
|
|
|
udpip_hdrsize(pstate->st_conn));
|
|
|
|
if (ret <= 0)
|
2023-01-11 09:09:20 +01:00
|
|
|
{
|
2023-05-29 23:26:19 +02:00
|
|
|
pstate->st_sndlen = ret;
|
|
|
|
goto end_wait;
|
2023-01-11 09:09:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
#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
|
|
|
|
|
|
|
|
pstate->st_sndlen = pstate->st_buflen;
|
|
|
|
}
|
|
|
|
|
2023-05-29 23:26:19 +02:00
|
|
|
end_wait:
|
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
2023-01-11 09:09:20 +01:00
|
|
|
pstate->st_cb->flags = 0;
|
|
|
|
pstate->st_cb->priv = NULL;
|
|
|
|
pstate->st_cb->event = NULL;
|
2015-01-28 20:41:24 +01:00
|
|
|
|
|
|
|
/* 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,
|
2020-05-17 16:47:40 +02:00
|
|
|
size_t len, int flags,
|
|
|
|
FAR const struct sockaddr *to, socklen_t tolen)
|
2015-01-28 20:41:24 +01:00
|
|
|
{
|
|
|
|
FAR struct udp_conn_s *conn;
|
|
|
|
struct sendto_s state;
|
2024-01-05 08:35:06 +01:00
|
|
|
int ret = OK;
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (psock == NULL || psock->s_type != SOCK_DGRAM ||
|
|
|
|
psock->s_conn == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket\n");
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
conn = psock->s_conn;
|
|
|
|
|
|
|
|
if (to != NULL && _SS_ISCONNECTED(conn->sconn.s_flags))
|
2018-04-25 16:43:38 +02:00
|
|
|
{
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
else if (to == NULL && !_SS_ISCONNECTED(conn->sconn.s_flags))
|
2018-04-27 05:58:09 +02:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (psock->s_domain == PF_INET)
|
|
|
|
{
|
2018-05-17 15:46:09 +02:00
|
|
|
in_addr_t destipaddr;
|
|
|
|
|
|
|
|
/* Check if the socket is connection mode */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISCONNECTED(conn->sconn.s_flags))
|
2018-05-17 15:46:09 +02:00
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2024-01-05 08:35:06 +01:00
|
|
|
if (psock->s_domain == PF_INET6)
|
2015-02-02 20:44:31 +01:00
|
|
|
{
|
2018-05-17 15:46:09 +02:00
|
|
|
FAR const uint16_t *destipaddr;
|
|
|
|
|
|
|
|
/* Check if the socket is connection mode */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISCONNECTED(conn->sconn.s_flags))
|
2018-05-17 15:46:09 +02:00
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
2023-08-10 05:43:27 +02:00
|
|
|
ret = icmpv6_neighbor(NULL, 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));
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&state.st_sem, 0, 0);
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2015-01-28 20:41:24 +01:00
|
|
|
state.st_buflen = len;
|
|
|
|
state.st_buffer = buf;
|
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
/* Save the reference to the conn structure if it will be needed for
|
2015-01-28 20:41:24 +01:00
|
|
|
* asynchronous processing.
|
|
|
|
*/
|
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
state.st_conn = conn;
|
2015-01-28 20:41:24 +01:00
|
|
|
|
2018-04-27 05:58:09 +02:00
|
|
|
/* Check if the socket is connected */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (!_SS_ISCONNECTED(conn->sconn.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.
|
|
|
|
*/
|
|
|
|
|
2021-12-17 07:35:51 +01:00
|
|
|
state.st_dev = udp_find_raddr_device(conn, NULL);
|
2019-10-18 22:20:34 +02:00
|
|
|
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 */
|
|
|
|
|
2020-01-04 11:37:46 +01:00
|
|
|
ret = -ENOMEM; /* Assume allocation failure */
|
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
|
2023-01-13 07:51:38 +01:00
|
|
|
* occur. NOTES: net_sem_timedwait will also terminate if a signal
|
2017-09-02 18:27:03 +02:00
|
|
|
* is received.
|
2015-01-28 20:41:24 +01:00
|
|
|
*/
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait(&state.st_sem,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_sndtimeo));
|
2020-01-04 11:37:46 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-05-17 16:47:40 +02:00
|
|
|
/* The result of the sendto operation is the number of bytes
|
|
|
|
* transferred.
|
|
|
|
*/
|
2020-01-04 11:37:46 +01:00
|
|
|
|
|
|
|
ret = state.st_sndlen;
|
|
|
|
}
|
2015-01-28 20:41:24 +01:00
|
|
|
|
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
|
|
|
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:
|
2022-02-08 16:24:04 +01:00
|
|
|
* conn A reference to UDP connection structure.
|
2018-09-12 18:10:54 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2019-11-24 17:30:48 +01:00
|
|
|
* OK (Always can send).
|
2018-09-12 18:10:54 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
int psock_udp_cansend(FAR struct udp_conn_s *conn)
|
2018-09-12 18:10:54 +02:00
|
|
|
{
|
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 */
|