2018-01-23 01:32:02 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* net/udp/udp_sendto_buffered.c
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01: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
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01: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.
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) && \
|
|
|
|
defined(CONFIG_NET_UDP_WRITE_BUFFERS)
|
|
|
|
|
|
|
|
#if defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_NET_UDP_WRBUFFER_DEBUG)
|
|
|
|
/* Force debug output (from this file only) */
|
|
|
|
|
|
|
|
# undef CONFIG_DEBUG_NET
|
|
|
|
# define CONFIG_DEBUG_NET 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2018-01-23 01:32:02 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/udp.h>
|
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "inet/inet.h"
|
|
|
|
#include "arp/arp.h"
|
|
|
|
#include "icmpv6/icmpv6.h"
|
|
|
|
#include "neighbor/neighbor.h"
|
|
|
|
#include "udp/udp.h"
|
|
|
|
#include "devif/devif.h"
|
2020-01-16 18:20:09 +01:00
|
|
|
#include "utils/utils.h"
|
2018-01-23 01:32:02 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2019-12-31 16:26:14 +01:00
|
|
|
|
2018-01-23 01:32:02 +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
|
|
|
|
|
|
|
|
/* Debug */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP_WRBUFFER_DUMP
|
|
|
|
# define BUF_DUMP(msg,buf,len) lib_dumpbuffer(msg,buf,len)
|
|
|
|
#else
|
|
|
|
# define BUF_DUMP(msg,buf,len)
|
2018-01-23 02:33:14 +01:00
|
|
|
# undef UDP_WBDUMP
|
|
|
|
# define UDP_WBDUMP(msg,wrb,len,offset)
|
2018-01-23 01:32:02 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NEED_IPDOMAIN_SUPPORT
|
|
|
|
static inline void sendto_ipselect(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct udp_conn_s *conn);
|
|
|
|
#endif
|
2022-02-08 16:24:04 +01:00
|
|
|
static int sendto_next_transfer(FAR struct udp_conn_s *conn);
|
2018-01-23 01:32:02 +01: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);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-01-23 02:27:05 +01:00
|
|
|
* Name: sendto_writebuffer_release
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-01-23 02:27:05 +01:00
|
|
|
* Release the write buffer at the head of the write buffer queue.
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2022-02-08 16:24:04 +01:00
|
|
|
* conn - The UDP connection of interest
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2018-01-23 02:27:05 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
2018-01-23 01:32:02 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
static void sendto_writebuffer_release(FAR struct udp_conn_s *conn)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2018-01-23 02:27:05 +01:00
|
|
|
FAR struct udp_wrbuffer_s *wrb;
|
|
|
|
int ret = OK;
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2018-01-23 02:27:05 +01:00
|
|
|
do
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2018-01-23 02:27:05 +01:00
|
|
|
/* Check if the write queue became empty */
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2018-01-23 02:27:05 +01:00
|
|
|
if (sq_empty(&conn->write_q))
|
|
|
|
{
|
|
|
|
/* Yes.. stifle any further callbacks until more write data is
|
|
|
|
* enqueued.
|
|
|
|
*/
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2022-02-07 04:03:03 +01:00
|
|
|
conn->sndcb->flags = 0;
|
|
|
|
conn->sndcb->priv = NULL;
|
|
|
|
conn->sndcb->event = NULL;
|
2018-01-23 02:27:05 +01:00
|
|
|
wrb = NULL;
|
2019-07-01 20:25:32 +02:00
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_UDP_NOTIFIER
|
2019-07-01 20:25:32 +02:00
|
|
|
/* Notify any waiters that the write buffers have been drained. */
|
|
|
|
|
|
|
|
udp_writebuffer_signal(conn);
|
|
|
|
#endif
|
2018-01-23 02:27:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Remove the write buffer from the head of the write buffer queue
|
|
|
|
* and release it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wrb = (FAR struct udp_wrbuffer_s *)sq_remfirst(&conn->write_q);
|
|
|
|
DEBUGASSERT(wrb != NULL);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2018-01-23 02:27:05 +01:00
|
|
|
udp_wrbuffer_release(wrb);
|
|
|
|
|
|
|
|
/* Set up for the next packet transfer by setting the connection
|
|
|
|
* address to the address of the next packet now at the header of
|
|
|
|
* the write buffer queue.
|
|
|
|
*/
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
ret = sendto_next_transfer(conn);
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
2018-01-23 02:27:05 +01:00
|
|
|
}
|
|
|
|
while (wrb != NULL && ret < 0);
|
2021-07-19 15:45:46 +02:00
|
|
|
|
|
|
|
#if CONFIG_NET_SEND_BUFSIZE > 0
|
|
|
|
/* Notify the send buffer available if wrbbuffer drained */
|
|
|
|
|
|
|
|
udp_sendbuffer_notify(conn);
|
|
|
|
#endif /* CONFIG_NET_SEND_BUFSIZE */
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sendto_ipselect
|
|
|
|
*
|
|
|
|
* 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:
|
2018-01-23 01:32:02 +01:00
|
|
|
* dev - The structure of the network driver that caused the event
|
2022-02-08 16:24:04 +01:00
|
|
|
* conn - The UDP connection of interest
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef NEED_IPDOMAIN_SUPPORT
|
|
|
|
static inline void sendto_ipselect(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct udp_conn_s *conn)
|
|
|
|
{
|
|
|
|
/* Which domain the socket support */
|
|
|
|
|
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)))
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
|
|
|
/* Select the IPv4 domain */
|
|
|
|
|
|
|
|
udp_ipv4_select(dev);
|
|
|
|
}
|
|
|
|
else /* if (conn->domain == PF_INET6) */
|
|
|
|
{
|
|
|
|
/* Select the IPv6 domain */
|
|
|
|
|
|
|
|
udp_ipv6_select(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sendto_next_transfer
|
|
|
|
*
|
|
|
|
* Description:
|
2019-10-19 00:04:33 +02:00
|
|
|
* Setup for the next packet transfer. That function is called (1)
|
|
|
|
* psock_udp_sendto() by when the new UDP packet is buffered at the head of
|
|
|
|
* the write queue and (2) by sendto_writebuffer_release() when that
|
|
|
|
* previously queued write buffer was sent and a new write buffer lies at
|
|
|
|
* the head of the write queue.
|
2018-01-23 01:32:02 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-01-23 01:32:02 +01:00
|
|
|
* conn - The UDP connection structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
static int sendto_next_transfer(FAR struct udp_conn_s *conn)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
|
|
|
FAR struct udp_wrbuffer_s *wrb;
|
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
|
|
|
|
/* Set the UDP "connection" to the destination address of the write buffer
|
|
|
|
* at the head of the queue.
|
|
|
|
*/
|
|
|
|
|
|
|
|
wrb = (FAR struct udp_wrbuffer_s *)sq_peek(&conn->write_q);
|
2018-01-23 14:32:17 +01:00
|
|
|
if (wrb == NULL)
|
|
|
|
{
|
|
|
|
ninfo("Write buffer queue is empty\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2021-12-17 07:35:51 +01:00
|
|
|
/* Has this address already been bound to a local port (lport)? */
|
|
|
|
|
|
|
|
if (!conn->lport)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2021-12-17 07:35:51 +01:00
|
|
|
/* No.. Find an unused local port number and bind it to the
|
|
|
|
* connection structure.
|
|
|
|
*/
|
|
|
|
|
2022-01-18 08:38:00 +01:00
|
|
|
conn->lport = HTONS(udp_select_port(conn->domain, &conn->u));
|
2024-04-12 05:11:54 +02:00
|
|
|
if (!conn->lport)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to get a local port!\n");
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 01:00:30 +01:00
|
|
|
/* Get the device that will handle the remote packet transfers. This
|
|
|
|
* should never be NULL.
|
2019-10-19 00:04:33 +02:00
|
|
|
*
|
|
|
|
* REVISIT: There is a logical error here for the case where there are
|
|
|
|
* multiple network devices. In that case, the packets may need to be sent
|
|
|
|
* in a different order than they were queued. Forcing FIFO packet
|
|
|
|
* transmission could harm performance.
|
2018-10-30 01:00:30 +01:00
|
|
|
*/
|
|
|
|
|
2021-12-17 07:35:51 +01:00
|
|
|
dev = udp_find_raddr_device(conn, &wrb->wb_dest);
|
2018-10-30 01:00:30 +01:00
|
|
|
if (dev == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: udp_find_raddr_device failed\n");
|
|
|
|
return -ENETUNREACH;
|
|
|
|
}
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2018-01-23 01:46:58 +01:00
|
|
|
/* Make sure that the device is in the UP state */
|
|
|
|
|
|
|
|
if ((dev->d_flags & IFF_UP) == 0)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: device is DOWN\n");
|
|
|
|
return -EHOSTUNREACH;
|
|
|
|
}
|
|
|
|
|
2023-12-12 11:52:01 +01:00
|
|
|
#ifndef CONFIG_NET_IPFRAG
|
|
|
|
/* Sanity check if the packet len (with IP hdr) is greater than the MTU */
|
|
|
|
|
|
|
|
if (wrb->wb_iob->io_pktlen > devif_get_mtu(dev))
|
|
|
|
{
|
|
|
|
nerr("ERROR: Packet too long to send!\n");
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/* If this is not the same device that we used in the last call to
|
|
|
|
* udp_callback_alloc(), then we need to release and reallocate the old
|
|
|
|
* callback instance.
|
|
|
|
*/
|
|
|
|
|
2022-02-07 04:03:03 +01:00
|
|
|
if (conn->sndcb != NULL && conn->dev != dev)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2022-02-07 04:03:03 +01:00
|
|
|
udp_callback_free(conn->dev, conn, conn->sndcb);
|
|
|
|
conn->sndcb = NULL;
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate resources to receive a callback from this device if the
|
|
|
|
* callback is not already in place.
|
|
|
|
*/
|
|
|
|
|
2022-02-07 04:03:03 +01:00
|
|
|
if (conn->sndcb == NULL)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2022-02-07 04:03:03 +01:00
|
|
|
conn->sndcb = udp_callback_alloc(dev, conn);
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if the callback has been allocated */
|
|
|
|
|
2022-02-07 04:03:03 +01:00
|
|
|
if (conn->sndcb == NULL)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
|
|
|
/* A buffer allocation error occurred */
|
|
|
|
|
|
|
|
nerr("ERROR: Failed to allocate callback\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->dev = dev;
|
|
|
|
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2022-02-07 04:03:03 +01:00
|
|
|
conn->sndcb->flags = (UDP_POLL | NETDEV_DOWN);
|
2022-02-08 16:24:04 +01:00
|
|
|
conn->sndcb->priv = (FAR void *)conn;
|
2022-02-07 04:03:03 +01:00
|
|
|
conn->sndcb->event = sendto_eventhandler;
|
2018-01-23 14:32:17 +01:00
|
|
|
|
|
|
|
/* Notify the device driver of the availability of TX data */
|
|
|
|
|
|
|
|
netdev_txnotify_dev(dev);
|
2018-01-23 01:32:02 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: sendto_eventhandler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called to perform the actual send operation when
|
|
|
|
* polled by the lower, device interfacing layer.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-01-23 01:32:02 +01:00
|
|
|
* dev The structure of the network driver that caused the event
|
|
|
|
* conn The connection structure associated with the socket
|
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
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)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct udp_conn_s *conn = pvpriv;
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
DEBUGASSERT(dev != NULL && conn != NULL);
|
2020-10-13 03:37:15 +02:00
|
|
|
|
|
|
|
ninfo("flags: %04x\n", flags);
|
|
|
|
|
|
|
|
/* Check if the network device has gone down */
|
|
|
|
|
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
|
|
|
{
|
|
|
|
ninfo("Device down: %04x\n", flags);
|
|
|
|
|
|
|
|
/* Free the write buffer at the head of the queue and attempt to setup
|
|
|
|
* the next transfer.
|
|
|
|
*/
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
sendto_writebuffer_release(conn);
|
2020-10-13 03:37:15 +02:00
|
|
|
return flags;
|
|
|
|
}
|
2019-10-19 00:04:33 +02:00
|
|
|
|
|
|
|
/* The UDP socket should be bound to a device. Make sure that the polling
|
|
|
|
* device is the one that we are bound to.
|
|
|
|
*
|
|
|
|
* REVISIT: There is a logical error here for the case where there are
|
|
|
|
* multiple network devices. In that case, the packets may need to be sent
|
|
|
|
* in a different order than they were queued. The packet we may need to
|
|
|
|
* send on this device may not be at the head of the list. Forcing FIFO
|
|
|
|
* packet transmission could degrade performance!
|
|
|
|
*/
|
|
|
|
|
2020-10-13 03:37:15 +02:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2019-10-19 00:04:33 +02:00
|
|
|
DEBUGASSERT(conn->dev != NULL);
|
|
|
|
if (dev != conn->dev)
|
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/* Check for a normal polling cycle and if the outgoing packet is
|
|
|
|
* available. It would not be available if it has been claimed by a send
|
|
|
|
* event serving a different thread -OR- if the output buffer currently
|
|
|
|
* contains unprocessed incoming data. In these cases we will just have
|
|
|
|
* to wait for the next polling cycle.
|
|
|
|
*
|
|
|
|
* And, of course, we can do nothing if we have no data in the write
|
|
|
|
* buffers to send.
|
|
|
|
*/
|
|
|
|
|
2018-01-23 14:32:17 +01:00
|
|
|
if (dev->d_sndlen <= 0 && (flags & UDP_NEWDATA) == 0 &&
|
2018-01-23 01:32:02 +01:00
|
|
|
(flags & UDP_POLL) != 0 && !sq_empty(&conn->write_q))
|
|
|
|
{
|
2022-11-23 12:39:40 +01:00
|
|
|
uint16_t udpiplen = udpip_hdrsize(conn);
|
2019-09-18 20:33:41 +02:00
|
|
|
FAR struct udp_wrbuffer_s *wrb;
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
/* Peek at the head of the write queue (but don't remove anything
|
|
|
|
* from the write queue yet). We know from the above test that
|
|
|
|
* the write_q is not empty.
|
|
|
|
*/
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
wrb = (FAR struct udp_wrbuffer_s *)sq_peek(&conn->write_q);
|
|
|
|
DEBUGASSERT(wrb != NULL);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2021-12-17 07:35:51 +01:00
|
|
|
/* If the udp socket not connected, it is possible to have
|
|
|
|
* multi-different destination address in each iob entry,
|
|
|
|
* update the remote address every time to avoid sent to the
|
|
|
|
* incorrect destination.
|
|
|
|
*/
|
|
|
|
|
|
|
|
udp_connect(conn, (FAR const struct sockaddr *)&wrb->wb_dest);
|
|
|
|
|
2023-01-11 09:09:20 +01:00
|
|
|
/* Then set-up to send that amount of data with the offset
|
|
|
|
* corresponding to the size of the IP-dependent address structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
netdev_iob_replace(dev, wrb->wb_iob);
|
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
/* Get the amount of data that we can send in the next packet.
|
|
|
|
* We will send either the remaining data in the buffer I/O
|
|
|
|
* buffer chain, or as much as will fit given the MSS and current
|
|
|
|
* window size.
|
|
|
|
*/
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2023-01-11 09:09:20 +01:00
|
|
|
dev->d_sndlen = wrb->wb_iob->io_pktlen - udpiplen;
|
|
|
|
ninfo("wrb=%p sndlen=%d\n", wrb, dev->d_sndlen);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2024-05-07 09:25:39 +02:00
|
|
|
/* Do not need to release wb_iob, the life cycle of wb_iob is
|
|
|
|
* handed over to the network device
|
|
|
|
*/
|
|
|
|
|
|
|
|
wrb->wb_iob = NULL;
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
#ifdef NEED_IPDOMAIN_SUPPORT
|
2019-09-18 20:33:41 +02:00
|
|
|
/* 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-01-23 01:32:02 +01:00
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
sendto_ipselect(dev, conn);
|
2018-01-23 01:32:02 +01:00
|
|
|
#endif
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
/* Free the write buffer at the head of the queue and attempt to
|
|
|
|
* setup the next transfer.
|
|
|
|
*/
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
sendto_writebuffer_release(conn);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
/* Only one data can be sent by low level driver at once,
|
|
|
|
* tell the caller stop polling the other connections.
|
|
|
|
*/
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2019-09-18 20:33:41 +02:00
|
|
|
flags &= ~UDP_POLL;
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue waiting */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2022-12-14 11:29:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_send_gettimeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the send timeout
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static unsigned int udp_send_gettimeout(clock_t start, unsigned int timeout)
|
|
|
|
{
|
|
|
|
unsigned int elapse;
|
|
|
|
|
|
|
|
if (timeout != UINT_MAX)
|
|
|
|
{
|
|
|
|
elapse = TICK2MSEC(clock_systime_ticks() - start);
|
|
|
|
if (elapse >= timeout)
|
|
|
|
{
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeout -= elapse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_udp_sendto
|
|
|
|
*
|
|
|
|
* 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-11-09 05:07:27 +01:00
|
|
|
size_t len, int flags,
|
|
|
|
FAR const struct sockaddr *to, socklen_t tolen)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
|
|
|
FAR struct udp_wrbuffer_s *wrb;
|
2022-02-08 06:18:01 +01:00
|
|
|
FAR struct udp_conn_s *conn;
|
2022-06-25 13:53:50 +02:00
|
|
|
unsigned int timeout;
|
2022-11-23 12:39:40 +01:00
|
|
|
uint16_t udpiplen;
|
2020-02-19 19:21:28 +01:00
|
|
|
bool nonblock;
|
2019-10-19 00:04:33 +02:00
|
|
|
bool empty;
|
2018-01-23 01:32:02 +01:00
|
|
|
int ret = OK;
|
2022-12-14 11:29:31 +01:00
|
|
|
clock_t start;
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
/* Get the underlying the UDP connection structure. */
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
|
2023-08-03 05:14:53 +02:00
|
|
|
/* The length of a datagram to be up to 65,535 octets */
|
|
|
|
|
|
|
|
if (len > 65535)
|
|
|
|
{
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
#if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR)
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
|
2018-01-23 01:32:02 +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;
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:32:02 +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);
|
2018-01-23 01:32:02 +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)
|
2018-01-23 01:32:02 +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;
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/* Make sure that the IP address mapping is in the Neighbor Table */
|
|
|
|
|
2023-08-10 05:43:27 +02:00
|
|
|
ret = icmpv6_neighbor(NULL, destipaddr);
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ICMPv6_NEIGHBOR */
|
|
|
|
|
|
|
|
/* Did we successfully get the address mapping? */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Not reachable\n");
|
|
|
|
return -ENETUNREACH;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
nonblock = _SS_ISNONBLOCK(conn->sconn.s_flags) ||
|
|
|
|
(flags & MSG_DONTWAIT) != 0;
|
2022-12-14 11:29:31 +01:00
|
|
|
start = clock_systime_ticks();
|
2022-06-25 13:53:50 +02:00
|
|
|
timeout = _SO_TIMEOUT(conn->sconn.s_sndtimeo);
|
2020-02-19 19:21:28 +01:00
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/* Dump the incoming buffer */
|
|
|
|
|
2020-01-21 09:43:14 +01:00
|
|
|
BUF_DUMP("psock_udp_sendto", buf, len);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
2019-12-31 16:52:40 +01:00
|
|
|
net_lock();
|
|
|
|
|
2021-07-19 15:45:46 +02:00
|
|
|
#if CONFIG_NET_SEND_BUFSIZE > 0
|
|
|
|
/* If the send buffer size exceeds the send limit,
|
|
|
|
* wait for the write buffer to be released
|
|
|
|
*/
|
|
|
|
|
2022-03-31 05:40:08 +02:00
|
|
|
while (udp_wrbuffer_inqueue_size(conn) + len > conn->sndbufs)
|
2021-07-19 15:45:46 +02:00
|
|
|
{
|
|
|
|
if (nonblock)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait_uninterruptible(&conn->sndsem,
|
2022-12-14 11:29:31 +01:00
|
|
|
udp_send_gettimeout(start, timeout));
|
2022-06-25 13:53:50 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (ret == -ETIMEDOUT)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2021-07-19 15:45:46 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_SEND_BUFSIZE */
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/* Allocate a write buffer. Careful, the network will be momentarily
|
|
|
|
* unlocked here.
|
|
|
|
*/
|
|
|
|
|
2024-05-09 09:55:24 +02:00
|
|
|
#ifdef CONFIG_NET_JUMBO_FRAME
|
|
|
|
|
|
|
|
/* alloc iob of gso pkt for udp data */
|
|
|
|
|
|
|
|
wrb = udp_wrbuffer_tryalloc(len + udpip_hdrsize(conn) +
|
|
|
|
CONFIG_NET_LL_GUARDSIZE);
|
|
|
|
#else
|
2020-02-19 19:21:28 +01:00
|
|
|
if (nonblock)
|
2019-12-31 16:52:40 +01:00
|
|
|
{
|
|
|
|
wrb = udp_wrbuffer_tryalloc();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-14 11:29:31 +01:00
|
|
|
wrb = udp_wrbuffer_timedalloc(udp_send_gettimeout(start,
|
|
|
|
timeout));
|
2019-12-31 16:52:40 +01:00
|
|
|
}
|
2024-05-09 09:55:24 +02:00
|
|
|
#endif
|
2019-12-31 16:52:40 +01:00
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
if (wrb == NULL)
|
|
|
|
{
|
|
|
|
/* A buffer allocation error occurred */
|
|
|
|
|
|
|
|
nerr("ERROR: Failed to allocate write buffer\n");
|
2022-06-25 13:53:50 +02:00
|
|
|
|
|
|
|
if (nonblock || timeout != UINT_MAX)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
/* Initialize the write buffer
|
|
|
|
*
|
|
|
|
* Check if the socket is connected
|
|
|
|
*/
|
2018-04-27 05:58:09 +02:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISCONNECTED(conn->sconn.s_flags))
|
2018-04-27 05:58:09 +02:00
|
|
|
{
|
|
|
|
/* Yes.. get the connection address from the connection structure */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (conn->domain == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in *addr4 =
|
|
|
|
(FAR struct sockaddr_in *)&wrb->wb_dest;
|
|
|
|
|
|
|
|
addr4->sin_family = AF_INET;
|
|
|
|
addr4->sin_port = conn->rport;
|
|
|
|
net_ipv4addr_copy(addr4->sin_addr.s_addr, conn->u.ipv4.raddr);
|
2020-02-25 15:53:39 +01:00
|
|
|
memset(addr4->sin_zero, 0, sizeof(addr4->sin_zero));
|
2018-04-27 05:58:09 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in6 *addr6 =
|
|
|
|
(FAR struct sockaddr_in6 *)&wrb->wb_dest;
|
|
|
|
|
|
|
|
addr6->sin6_family = AF_INET6;
|
|
|
|
addr6->sin6_port = conn->rport;
|
2020-11-09 05:07:27 +01:00
|
|
|
net_ipv6addr_copy(addr6->sin6_addr.s6_addr,
|
|
|
|
conn->u.ipv6.raddr);
|
2018-04-27 05:58:09 +02:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not connected. Use the provided destination address */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&wrb->wb_dest, to, tolen);
|
2023-12-25 14:00:01 +01:00
|
|
|
udp_connect(conn, to);
|
2018-04-27 05:58:09 +02:00
|
|
|
}
|
2018-01-23 01:32:02 +01:00
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
/* Skip l2/l3/l4 offset before copy */
|
|
|
|
|
|
|
|
udpiplen = udpip_hdrsize(conn);
|
|
|
|
|
|
|
|
iob_reserve(wrb->wb_iob, CONFIG_NET_LL_GUARDSIZE);
|
2023-05-17 10:11:21 +02:00
|
|
|
iob_update_pktlen(wrb->wb_iob, udpiplen, false);
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
/* Copy the user data into the write buffer. We cannot wait for
|
|
|
|
* buffer space if the socket was opened non-blocking.
|
|
|
|
*/
|
|
|
|
|
2020-02-19 19:21:28 +01:00
|
|
|
if (nonblock)
|
2018-01-23 01:32:02 +01:00
|
|
|
{
|
2022-08-08 04:21:03 +02:00
|
|
|
ret = iob_trycopyin(wrb->wb_iob, (FAR uint8_t *)buf,
|
2023-01-11 10:00:55 +01:00
|
|
|
len, udpiplen, false);
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-16 18:20:09 +01:00
|
|
|
unsigned int count;
|
|
|
|
int blresult;
|
|
|
|
|
|
|
|
/* iob_copyin might wait for buffers to be freed, but if
|
|
|
|
* network is locked this might never happen, since network
|
|
|
|
* driver is also locked, therefore we need to break the lock
|
|
|
|
*/
|
|
|
|
|
|
|
|
blresult = net_breaklock(&count);
|
2022-11-23 12:39:40 +01:00
|
|
|
ret = iob_copyin(wrb->wb_iob, (FAR uint8_t *)buf,
|
2023-01-11 10:00:55 +01:00
|
|
|
len, udpiplen, false);
|
2020-01-16 18:20:09 +01:00
|
|
|
if (blresult >= 0)
|
|
|
|
{
|
|
|
|
net_restorelock(count);
|
|
|
|
}
|
2018-01-23 01:32:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_wrb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump I/O buffer chain */
|
|
|
|
|
2018-01-23 02:33:14 +01:00
|
|
|
UDP_WBDUMP("I/O buffer chain", wrb, wrb->wb_iob->io_pktlen, 0);
|
2018-01-23 01:32:02 +01:00
|
|
|
|
|
|
|
/* sendto_eventhandler() will send data in FIFO order from the
|
2018-01-23 14:32:17 +01:00
|
|
|
* conn->write_q.
|
|
|
|
*
|
|
|
|
* REVISIT: Why FIFO order? Because it is easy. In a real world
|
|
|
|
* environment where there are multiple network devices this might
|
|
|
|
* be inefficient because we could be sending data to different
|
|
|
|
* device out-of-queued-order to optimize performance. Sending
|
|
|
|
* data to different networks from a single UDP socket is probably
|
|
|
|
* not a very common use case, however.
|
2018-01-23 01:32:02 +01:00
|
|
|
*/
|
|
|
|
|
2019-10-19 00:04:33 +02:00
|
|
|
empty = sq_empty(&conn->write_q);
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
sq_addlast(&wrb->wb_node, &conn->write_q);
|
|
|
|
ninfo("Queued WRB=%p pktlen=%u write_q(%p,%p)\n",
|
|
|
|
wrb, wrb->wb_iob->io_pktlen,
|
|
|
|
conn->write_q.head, conn->write_q.tail);
|
|
|
|
|
2019-10-19 00:04:33 +02:00
|
|
|
if (empty)
|
2019-07-03 02:02:23 +02:00
|
|
|
{
|
2019-10-19 00:04:33 +02:00
|
|
|
/* The new write buffer lies at the head of the write queue. Set
|
|
|
|
* up for the next packet transfer by setting the connection
|
|
|
|
* address to the address of the next packet now at the header of
|
|
|
|
* the write buffer queue.
|
|
|
|
*/
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
ret = sendto_next_transfer(conn);
|
2019-10-19 00:04:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
sq_remlast(&conn->write_q);
|
2019-10-19 00:04:33 +02:00
|
|
|
goto errout_with_wrb;
|
|
|
|
}
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
2018-01-23 01:32:02 +01:00
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of bytes that will be sent */
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
|
|
|
errout_with_wrb:
|
|
|
|
udp_wrbuffer_release(wrb);
|
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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. No space in the buffer is actually reserved, so it is
|
|
|
|
* possible that the write may still block if the buffer is filled by
|
|
|
|
* another means.
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
* OK
|
|
|
|
* At least one byte of data could be successfully written.
|
|
|
|
* -EWOULDBLOCK
|
|
|
|
* There is no room in the output buffer.
|
|
|
|
* -EBADF
|
|
|
|
* An invalid descriptor was specified.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
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
|
|
|
{
|
|
|
|
/* Verify that we received a valid socket */
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
if (conn == NULL)
|
2018-09-12 18:10:54 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: Invalid socket\n");
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In order to setup the send, we need to have at least one free write
|
2019-03-11 19:48:17 +01:00
|
|
|
* buffer head and at least one free IOB to initialize the write buffer
|
|
|
|
* head.
|
2018-09-12 18:10:54 +02:00
|
|
|
*
|
2019-03-11 19:48:17 +01:00
|
|
|
* REVISIT: The send will still block if we are unable to buffer the
|
|
|
|
* entire user-provided buffer which may be quite large. We will almost
|
|
|
|
* certainly need to have more than one free IOB, but we don't know how
|
|
|
|
* many more.
|
2018-09-12 18:10:54 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-11 10:00:55 +01:00
|
|
|
if (udp_wrbuffer_test() < 0 || iob_navail(false) <= 0)
|
2018-09-12 18:10:54 +02:00
|
|
|
{
|
|
|
|
return -EWOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2021-07-19 15:45:46 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_sendbuffer_notify
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Notify the send buffer semaphore
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The UDP connection of interest
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Called from user logic with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if CONFIG_NET_SEND_BUFSIZE > 0
|
|
|
|
void udp_sendbuffer_notify(FAR struct udp_conn_s *conn)
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
nxsem_get_value(&conn->sndsem, &val);
|
|
|
|
if (val < 0)
|
|
|
|
{
|
|
|
|
nxsem_post(&conn->sndsem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_SEND_BUFSIZE */
|
|
|
|
|
2018-01-23 01:32:02 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP && CONFIG_NET_UDP_WRITE_BUFFERS */
|