* net/udp/udp_sendto_unbuffered.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* 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>
#include <assert.h>
#include <nuttx/semaphore.h>
#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"
#include "icmpv6/icmpv6.h"
#include "socket/socket.h"
#include "utils/utils.h"
#include "udp/udp.h"
* Pre-processor Definitions
****************************************************************************/
* 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
{
FAR struct udp_conn_s *st_conn;
FAR struct devif_callback_s *st_cb;
FAR struct net_driver_s *st_dev;
sem_t st_sem;
uint16_t st_buflen;
const char *st_buffer;
int st_sndlen;
};
* Private Functions
****************************************************************************/
* 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.
*
* Input Parameters:
* dev - The structure of the network driver that caused the event
* pstate - sendto state structure
*
* Returned Value:
* None
*
* Assumptions:
* This function must be called with the network locked.
*
****************************************************************************/
#ifdef NEED_IPDOMAIN_SUPPORT
static inline void sendto_ipselect(FAR struct net_driver_s *dev,
FAR struct sendto_s *pstate)
{
FAR struct udp_conn_s *conn = pstate->st_conn;
if (conn->domain == PF_INET ||
(conn->domain == PF_INET6 &&
ip6_is_ipv4addr((FAR struct in6_addr *)conn->u.ipv6.raddr)))
{
udp_ipv4_select(dev);
}
else
{
udp_ipv6_select(dev);
}
}
#endif
* Name: sendto_eventhandler
*
* Description:
* This function is called to perform the actual send operation when
* polled by the lower, device interfacing layer.
*
* Input Parameters:
* dev The structure of the network driver that caused the event
* 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:
* This function must be called with the network locked.
*
****************************************************************************/
static uint32_t sendto_eventhandler(FAR struct net_driver_s *dev,
FAR void *pvpriv, uint32_t flags)
{
FAR struct sendto_s *pstate = pvpriv;
DEBUGASSERT(pstate != NULL && pstate->st_dev != NULL);
if (pstate != NULL)
{
* polling device is the one that we are bound to.
*/
if (dev != pstate->st_dev)
{
return flags;
}
ninfo("flags: %" PRIx32 "\n", flags);
* the wait now with an error.
*/
if ((flags & NETDEV_DOWN) != 0)
{
nwarn("WARNING: Network is down\n");
pstate->st_sndlen = -ENETUNREACH;
}
* by a sendto 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.
*/
else if (dev->d_sndlen > 0 || (flags & UDP_NEWDATA) != 0)
{
* wait for the next polling cycle and check again.
*/
return flags;
}
else
{
if (pstate->st_buflen > 0)
{
int ret = devif_send(dev, pstate->st_buffer, pstate->st_buflen,
udpip_hdrsize(pstate->st_conn));
if (ret <= 0)
{
pstate->st_sndlen = ret;
goto end_wait;
}
}
else
{
if (netdev_iob_prepare(dev, false, 0) != OK)
{
pstate->st_sndlen = -ENOMEM;
goto end_wait;
}
iob_update_pktlen(dev->d_iob, udpip_hdrsize(pstate->st_conn),
false);
dev->d_sndlen = 0;
}
dev->d_len = dev->d_iob->io_pktlen;
#ifdef NEED_IPDOMAIN_SUPPORT
* 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;
}
end_wait:
pstate->st_cb->flags = 0;
pstate->st_cb->priv = NULL;
pstate->st_cb->event = NULL;
nxsem_post(&pstate->st_sem);
}
return flags;
}
* 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,
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 = OK;
if (psock == NULL || psock->s_type != SOCK_DGRAM ||
psock->s_conn == NULL)
{
nerr("ERROR: Invalid socket\n");
return -EBADF;
}
* 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.
*/
conn = psock->s_conn;
if (to != NULL && _SS_ISCONNECTED(conn->sconn.s_flags))
{
* already connected.
*/
return -EISCONN;
}
* must be provided.
*/
else if (to == NULL && !_SS_ISCONNECTED(conn->sconn.s_flags))
{
* address is set.
*/
return -EDESTADDRREQ;
}
#if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR)
#ifdef CONFIG_NET_ARP_SEND
* the ARP table.
*/
if (psock->s_domain == PF_INET)
{
in_addr_t destipaddr;
if (_SS_ISCONNECTED(conn->sconn.s_flags))
{
* null).
*/
destipaddr = conn->u.ipv4.raddr;
}
else
{
FAR const struct sockaddr_in *into;
* argument.
*/
into = (FAR const struct sockaddr_in *)to;
destipaddr = into->sin_addr.s_addr;
}
ret = arp_send(destipaddr);
}
#endif
#ifdef CONFIG_NET_ICMPv6_NEIGHBOR
* the neighbor table.
*/
if (psock->s_domain == PF_INET6)
{
FAR const uint16_t *destipaddr;
if (_SS_ISCONNECTED(conn->sconn.s_flags))
{
* null).
*/
destipaddr = conn->u.ipv6.raddr;
}
else
{
FAR const struct sockaddr_in6 *into;
* argument.
*/
into = (FAR const struct sockaddr_in6 *)to;
destipaddr = into->sin6_addr.s6_addr16;
}
ret = icmpv6_neighbor(NULL, destipaddr);
}
#endif
if (ret < 0)
{
nerr("ERROR: Peer not reachable\n");
return -ENETUNREACH;
}
#endif
* locked because we don't want anything to happen until we are
* ready.
*/
memset(&state, 0, sizeof(struct sendto_s));
nxsem_init(&state.st_sem, 0, 0);
state.st_buflen = len;
state.st_buffer = buf;
* asynchronous processing.
*/
state.st_conn = conn;
if (!_SS_ISCONNECTED(conn->sconn.s_flags))
{
* structure to the sendto() destination address.
*/
ret = udp_connect(conn, to);
if (ret < 0)
{
nerr("ERROR: udp_connect failed: %d\n", ret);
return ret;
}
}
* should never be NULL.
*/
state.st_dev = udp_find_raddr_device(conn, NULL);
if (state.st_dev == NULL)
{
nerr("ERROR: udp_find_raddr_device failed\n");
return -ENETUNREACH;
}
if (IFF_IS_RUNNING(state.st_dev->d_flags) == 0)
{
nwarn("WARNING: device is DOWN\n");
return -EHOSTUNREACH;
}
conn_dev_lock(&conn->sconn, state.st_dev);
ret = -ENOMEM;
state.st_cb = udp_callback_alloc(state.st_dev, conn);
if (state.st_cb)
{
state.st_cb->flags = (UDP_POLL | NETDEV_DOWN);
state.st_cb->priv = (FAR void *)&state;
state.st_cb->event = sendto_eventhandler;
netdev_txnotify_dev(state.st_dev, UDP_POLL);
* occur. NOTES: conn_dev_sem_timedwait will also terminate if a signal
* is received.
*/
ret = conn_dev_sem_timedwait(&state.st_sem, true,
_SO_TIMEOUT(conn->sconn.s_sndtimeo),
&conn->sconn, state.st_dev);
if (ret >= 0)
{
* transferred.
*/
ret = state.st_sndlen;
}
udp_callback_free(state.st_dev, conn, state.st_cb);
}
nxsem_destroy(&state.st_sem);
conn_dev_unlock(&conn->sconn, state.st_dev);
return ret;
}
* 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:
* conn A reference to UDP connection structure.
*
* Returned Value:
* OK (Always can send).
*
* Assumptions:
* None
*
****************************************************************************/
int psock_udp_cansend(FAR struct udp_conn_s *conn)
{
return OK;
}
#endif