2020-01-21 08:50:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/udp/udp_recvfrom.c
|
|
|
|
*
|
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
|
2020-01-21 08:50:39 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-01-21 08:50:39 +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.
|
2020-01-21 08:50:39 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2023-11-17 08:44:49 +01:00
|
|
|
#include <sys/time.h>
|
2020-01-22 16:29:26 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2020-01-21 08:50:39 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#include <nuttx/net/udp.h>
|
2022-08-24 08:55:56 +02:00
|
|
|
#include <netinet/in.h>
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "udp/udp.h"
|
|
|
|
#include "socket/socket.h"
|
2023-04-20 06:15:24 +02:00
|
|
|
#include "utils/utils.h"
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct udp_recvfrom_s
|
|
|
|
{
|
2022-02-08 16:24:04 +01:00
|
|
|
FAR struct udp_conn_s *ir_conn; /* Connection associated with the socket */
|
2020-01-21 08:50:39 +01:00
|
|
|
FAR struct devif_callback_s *ir_cb; /* Reference to callback instance */
|
2022-08-24 08:55:56 +02:00
|
|
|
FAR struct msghdr *ir_msg; /* Receive info and buffer */
|
2020-01-21 08:50:39 +01:00
|
|
|
sem_t ir_sem; /* Semaphore signals recv completion */
|
|
|
|
ssize_t ir_recvlen; /* The received length */
|
|
|
|
int ir_result; /* Success:OK, failure:negated errno */
|
2023-05-25 13:52:41 +02:00
|
|
|
int ir_flags; /* Flags on received message. */
|
2020-01-21 08:50:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-11-17 08:44:49 +01:00
|
|
|
#ifdef CONFIG_NET_TIMESTAMP
|
|
|
|
static void udp_store_cmsg_timestamp(FAR struct udp_recvfrom_s *pstate,
|
|
|
|
FAR struct timespec *timestamp)
|
|
|
|
{
|
|
|
|
FAR struct msghdr *msg = pstate->ir_msg;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, timestamp);
|
|
|
|
cmsg_append(msg, SOL_SOCKET, SO_TIMESTAMP,
|
|
|
|
&tv, sizeof(struct timeval));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-21 11:42:57 +02:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
2022-08-24 08:55:56 +02:00
|
|
|
static void udp_recvpktinfo(FAR struct udp_recvfrom_s *pstate,
|
|
|
|
FAR void *srcaddr, uint8_t ifindex)
|
|
|
|
{
|
2023-04-20 06:15:24 +02:00
|
|
|
FAR struct msghdr *msg = pstate->ir_msg;
|
|
|
|
FAR struct udp_conn_s *conn = pstate->ir_conn;
|
2022-08-24 08:55:56 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2023-04-21 11:42:57 +02:00
|
|
|
if (conn->domain == PF_INET &&
|
|
|
|
_SO_GETOPT(conn->sconn.s_options, IP_PKTINFO))
|
2022-08-24 08:55:56 +02:00
|
|
|
{
|
2023-04-20 06:15:24 +02:00
|
|
|
FAR struct sockaddr_in *infrom = srcaddr;
|
2023-05-17 23:16:14 +02:00
|
|
|
struct in_pktinfo pktinfo;
|
2023-04-20 06:15:24 +02:00
|
|
|
|
|
|
|
pktinfo.ipi_ifindex = ifindex;
|
|
|
|
pktinfo.ipi_addr.s_addr = infrom->sin_addr.s_addr;
|
|
|
|
pktinfo.ipi_spec_dst.s_addr = conn->u.ipv4.laddr;
|
|
|
|
|
|
|
|
cmsg_append(msg, IPPROTO_IP, IP_PKTINFO, &pktinfo, sizeof(pktinfo));
|
2022-08-24 08:55:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2023-04-21 11:42:57 +02:00
|
|
|
if (conn->domain == PF_INET6 &&
|
|
|
|
_SO_GETOPT(conn->sconn.s_options, IPV6_RECVPKTINFO))
|
2022-08-24 08:55:56 +02:00
|
|
|
{
|
2023-04-20 06:15:24 +02:00
|
|
|
FAR struct sockaddr_in6 *infrom = srcaddr;
|
2023-05-17 23:16:14 +02:00
|
|
|
struct in6_pktinfo pktinfo;
|
2023-04-20 06:15:24 +02:00
|
|
|
|
|
|
|
pktinfo.ipi6_ifindex = ifindex;
|
|
|
|
net_ipv6addr_copy(&pktinfo.ipi6_addr, infrom->sin6_addr.s6_addr);
|
|
|
|
|
|
|
|
cmsg_append(msg, IPPROTO_IPV6, IPV6_PKTINFO, &pktinfo,
|
|
|
|
sizeof(pktinfo));
|
2022-08-24 08:55:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
2023-04-21 11:42:57 +02:00
|
|
|
#else
|
|
|
|
#define udp_recvpktinfo(p, s, i) {(void)(p); (void)(s); (void)(i);}
|
|
|
|
#endif
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_recvfrom_newdata
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Copy the read data from the packet
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-11-23 12:39:40 +01:00
|
|
|
* dev The structure of the network driver that generated the event
|
2020-01-21 08:50:39 +01:00
|
|
|
* pstate recvfrom state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2022-11-23 12:39:40 +01:00
|
|
|
* None.
|
2020-01-21 08:50:39 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
static inline size_t udp_recvfrom_newdata(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct udp_recvfrom_s *pstate)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
|
|
|
size_t recvlen;
|
|
|
|
|
|
|
|
/* Get the length of the data to return */
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
if (dev->d_len > pstate->ir_msg->msg_iov->iov_len)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
2022-08-24 08:55:56 +02:00
|
|
|
recvlen = pstate->ir_msg->msg_iov->iov_len;
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
recvlen = dev->d_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the new appdata into the user buffer */
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
recvlen = iob_copyout(pstate->ir_msg->msg_iov->iov_base, dev->d_iob,
|
|
|
|
recvlen, dev->d_appdata - dev->d_iob->io_data -
|
|
|
|
dev->d_iob->io_offset);
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-09-05 08:01:43 +02:00
|
|
|
/* Update the size of the data read */
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-09-05 08:01:43 +02:00
|
|
|
pstate->ir_recvlen = recvlen;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
dev->d_len = 0;
|
2022-11-23 12:39:40 +01:00
|
|
|
|
|
|
|
return recvlen;
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void udp_readahead(struct udp_recvfrom_s *pstate)
|
|
|
|
{
|
2022-02-08 16:24:04 +01:00
|
|
|
FAR struct udp_conn_s *conn = pstate->ir_conn;
|
2020-01-21 08:50:39 +01:00
|
|
|
FAR struct iob_s *iob;
|
|
|
|
|
|
|
|
/* Check there is any UDP datagram already buffered in a read-ahead
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pstate->ir_recvlen = -1;
|
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
if ((iob = conn->readahead) != NULL)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
2023-07-04 07:24:51 +02:00
|
|
|
int recvlen;
|
|
|
|
int offset = 0;
|
|
|
|
uint16_t datalen;
|
2020-01-21 08:50:39 +01:00
|
|
|
uint8_t src_addr_size;
|
2022-11-23 12:39:40 +01:00
|
|
|
uint8_t ifindex;
|
2023-07-04 07:24:51 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
uint8_t srcaddr[sizeof(struct sockaddr_in6)];
|
|
|
|
#else
|
|
|
|
uint8_t srcaddr[sizeof(struct sockaddr_in)];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Unflatten saved connection information
|
2023-11-17 08:44:49 +01:00
|
|
|
* Layout: |datalen|ifindex|src_addr_size|src_addr|[timestamp]|data|
|
2023-07-04 07:24:51 +02:00
|
|
|
*/
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
recvlen = iob_copyout((FAR uint8_t *)&datalen, iob,
|
|
|
|
sizeof(datalen), offset);
|
|
|
|
offset += sizeof(datalen);
|
|
|
|
DEBUGASSERT(recvlen == sizeof(datalen));
|
2022-08-24 08:55:56 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
2023-07-04 07:24:51 +02:00
|
|
|
recvlen = iob_copyout(&ifindex, iob, sizeof(ifindex), offset);
|
|
|
|
offset += sizeof(ifindex);
|
|
|
|
DEBUGASSERT(recvlen == sizeof(ifindex));
|
2022-11-23 12:39:40 +01:00
|
|
|
#else
|
2023-04-21 11:42:57 +02:00
|
|
|
ifindex = 1;
|
2022-08-24 08:55:56 +02:00
|
|
|
#endif
|
2023-07-04 07:24:51 +02:00
|
|
|
recvlen = iob_copyout(&src_addr_size, iob,
|
|
|
|
sizeof(src_addr_size), offset);
|
|
|
|
offset += sizeof(src_addr_size);
|
|
|
|
DEBUGASSERT(recvlen == sizeof(src_addr_size));
|
|
|
|
|
|
|
|
recvlen = iob_copyout(srcaddr, iob, src_addr_size, offset);
|
|
|
|
offset += src_addr_size;
|
|
|
|
DEBUGASSERT(recvlen == src_addr_size);
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2023-11-17 08:44:49 +01:00
|
|
|
#ifdef CONFIG_NET_TIMESTAMP
|
|
|
|
/* Unpack stored timestamp if SO_TIMESTAMP socket option is enabled */
|
|
|
|
|
|
|
|
if (conn->timestamp)
|
|
|
|
{
|
|
|
|
struct timespec timestamp;
|
|
|
|
recvlen = iob_copyout((FAR uint8_t *)×tamp, iob,
|
|
|
|
sizeof(struct timespec), offset);
|
|
|
|
DEBUGASSERT(recvlen == sizeof(struct timespec));
|
|
|
|
|
|
|
|
udp_store_cmsg_timestamp(pstate, ×tamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += sizeof(struct timespec);
|
|
|
|
#endif
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
/* Copy to user */
|
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
recvlen = iob_copyout(pstate->ir_msg->msg_iov->iov_base, iob,
|
|
|
|
MIN(pstate->ir_msg->msg_iov->iov_len, datalen),
|
|
|
|
offset);
|
2022-11-23 12:39:40 +01:00
|
|
|
|
|
|
|
/* Update the accumulated size of the data read */
|
|
|
|
|
|
|
|
pstate->ir_recvlen = recvlen;
|
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
ninfo("Received %d bytes (of %d, total %d)\n",
|
|
|
|
recvlen, datalen, iob->io_pktlen);
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-09-05 08:08:13 +02:00
|
|
|
if (pstate->ir_msg->msg_name)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
2022-09-05 08:08:13 +02:00
|
|
|
pstate->ir_msg->msg_namelen =
|
|
|
|
src_addr_size > pstate->ir_msg->msg_namelen ?
|
|
|
|
pstate->ir_msg->msg_namelen : src_addr_size;
|
|
|
|
|
|
|
|
memcpy(pstate->ir_msg->msg_name, srcaddr,
|
|
|
|
pstate->ir_msg->msg_namelen);
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
udp_recvpktinfo(pstate, srcaddr, ifindex);
|
|
|
|
|
2023-07-04 07:24:51 +02:00
|
|
|
/* Remove the packet from the head of the I/O buffer chain. */
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2023-05-25 13:52:41 +02:00
|
|
|
if (!(pstate->ir_flags & MSG_PEEK))
|
|
|
|
{
|
2023-07-04 07:24:51 +02:00
|
|
|
if (offset + datalen >= iob->io_pktlen)
|
|
|
|
{
|
|
|
|
iob_free_chain(iob);
|
|
|
|
conn->readahead = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conn->readahead = iob_trimhead(iob, offset + datalen);
|
|
|
|
}
|
2023-05-25 13:52:41 +02:00
|
|
|
}
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_sender
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Getting the sender's address from the UDP packet
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device driver data structure
|
|
|
|
* pstate - the recvfrom state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void udp_sender(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct udp_recvfrom_s *pstate)
|
|
|
|
{
|
2022-08-24 08:55:56 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
uint8_t srcaddr[sizeof(struct sockaddr_in6)];
|
|
|
|
#else
|
|
|
|
uint8_t srcaddr[sizeof(struct sockaddr_in)];
|
|
|
|
#endif
|
|
|
|
socklen_t fromlen = 0;
|
|
|
|
|
2020-01-21 08:50:39 +01:00
|
|
|
/* Get the family from the packet type, IP address from the IP header, and
|
|
|
|
* the port number from the UDP header.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (IFF_IS_IPv6(dev->d_flags))
|
|
|
|
#endif
|
|
|
|
{
|
2022-08-24 08:55:56 +02:00
|
|
|
FAR struct sockaddr_in6 *infrom = (FAR struct sockaddr_in6 *)srcaddr;
|
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv6BUF;
|
|
|
|
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
infrom->sin6_family = AF_INET6;
|
|
|
|
infrom->sin6_port = udp->srcport;
|
|
|
|
fromlen = sizeof(struct sockaddr_in6);
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
net_ipv6addr_copy(infrom->sin6_addr.s6_addr, ipv6->srcipaddr);
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2022-08-24 08:55:56 +02:00
|
|
|
FAR struct udp_conn_s *conn = pstate->ir_conn;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
if (conn->domain == PF_INET6)
|
|
|
|
{
|
2020-01-21 08:50:39 +01:00
|
|
|
/* Hybrid dual-stack IPv6/IPv4 implementations recognize a special
|
|
|
|
* class of addresses, the IPv4-mapped IPv6 addresses.
|
|
|
|
*/
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
FAR struct sockaddr_in6 *infrom6 =
|
|
|
|
(FAR struct sockaddr_in6 *)srcaddr;
|
2023-08-18 11:54:07 +02:00
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
2022-08-24 08:55:56 +02:00
|
|
|
in_addr_t ipv4addr;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
/* Encode the IPv4 address as an IPv4-mapped IPv6 address */
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
infrom6->sin6_family = AF_INET6;
|
|
|
|
infrom6->sin6_port = udp->srcport;
|
|
|
|
fromlen = sizeof(struct sockaddr_in6);
|
2023-08-18 11:54:07 +02:00
|
|
|
ipv4addr = net_ip4addr_conv32(ipv4->srcipaddr);
|
2022-08-24 08:55:56 +02:00
|
|
|
ip6_map_ipv4addr(ipv4addr, infrom6->sin6_addr.s6_addr16);
|
|
|
|
}
|
|
|
|
else
|
2020-01-21 08:50:39 +01:00
|
|
|
#endif
|
2022-08-24 08:55:56 +02:00
|
|
|
{
|
|
|
|
FAR struct sockaddr_in *infrom = (FAR struct sockaddr_in *)srcaddr;
|
|
|
|
FAR struct udp_hdr_s *udp = UDPIPv4BUF;
|
|
|
|
FAR struct ipv4_hdr_s *ipv4 = IPv4BUF;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
infrom->sin_family = AF_INET;
|
|
|
|
infrom->sin_port = udp->srcport;
|
|
|
|
fromlen = sizeof(struct sockaddr_in);
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
net_ipv4addr_copy(infrom->sin_addr.s_addr,
|
|
|
|
net_ip4addr_conv32(ipv4->srcipaddr));
|
|
|
|
memset(infrom->sin_zero, 0, sizeof(infrom->sin_zero));
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2022-08-24 08:55:56 +02:00
|
|
|
|
|
|
|
if (pstate->ir_msg->msg_name)
|
|
|
|
{
|
|
|
|
pstate->ir_msg->msg_namelen = fromlen > pstate->ir_msg->msg_namelen ?
|
|
|
|
pstate->ir_msg->msg_namelen : fromlen;
|
|
|
|
memcpy(pstate->ir_msg->msg_name, srcaddr, pstate->ir_msg->msg_namelen);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
|
|
|
udp_recvpktinfo(pstate, srcaddr, dev->d_ifindex);
|
|
|
|
#else
|
2023-04-21 11:42:57 +02:00
|
|
|
udp_recvpktinfo(pstate, srcaddr, 1);
|
2022-08-24 08:55:56 +02:00
|
|
|
#endif
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_terminate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Terminate the UDP transfer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pstate - The recvfrom state structure
|
|
|
|
* result - The result of the operation
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void udp_terminate(FAR struct udp_recvfrom_s *pstate, int result)
|
|
|
|
{
|
|
|
|
/* Don't allow any further UDP call backs. */
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
pstate->ir_cb->flags = 0;
|
|
|
|
pstate->ir_cb->priv = NULL;
|
|
|
|
pstate->ir_cb->event = NULL;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/* Save the result of the transfer */
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
pstate->ir_result = result;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/* Wake up the waiting thread, returning the number of bytes
|
|
|
|
* actually read.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxsem_post(&pstate->ir_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_eventhandler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called with the network locked to perform the actual
|
|
|
|
* UDP receive operation via by the lower, device interfacing layer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev The structure of the network driver that generated the event.
|
2022-08-25 15:17:57 +02:00
|
|
|
* pvpriv An instance of struct udp_recvfrom_s cast to void*
|
2020-01-21 08:50:39 +01:00
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint16_t udp_eventhandler(FAR struct net_driver_s *dev,
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct udp_recvfrom_s *pstate = pvpriv;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
ninfo("flags: %04x\n", flags);
|
|
|
|
|
|
|
|
/* 'priv' might be null in some race conditions (?) */
|
|
|
|
|
|
|
|
if (pstate)
|
|
|
|
{
|
|
|
|
/* 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. */
|
|
|
|
|
|
|
|
nerr("ERROR: Network is down\n");
|
|
|
|
udp_terminate(pstate, -ENETUNREACH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If new data is available, then complete the read action. */
|
|
|
|
|
|
|
|
else if ((flags & UDP_NEWDATA) != 0)
|
|
|
|
{
|
2023-11-17 08:44:49 +01:00
|
|
|
/* Save packet timestamp, if requested */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TIMESTAMP
|
|
|
|
if (pstate->ir_conn->timestamp)
|
|
|
|
{
|
|
|
|
udp_store_cmsg_timestamp(pstate, &dev->d_rxtime);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
/* Save the sender's address in the caller's 'from' location */
|
|
|
|
|
|
|
|
udp_sender(dev, pstate);
|
|
|
|
|
2020-01-21 08:50:39 +01:00
|
|
|
/* Copy the data from the packet */
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
udp_recvfrom_newdata(dev, pstate);
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/* We are finished. */
|
|
|
|
|
|
|
|
ninfo("UDP done\n");
|
|
|
|
|
|
|
|
/* Don't allow any further UDP call backs. */
|
|
|
|
|
|
|
|
udp_terminate(pstate, OK);
|
|
|
|
|
2023-05-25 13:52:41 +02:00
|
|
|
/* In read-ahead mode, UDP_NEWDATA and iob need to be reserved
|
|
|
|
* and let udp_callback to call net_dataevent and put this packet
|
|
|
|
* into conn->readahead
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!(pstate->ir_flags & MSG_PEEK))
|
|
|
|
{
|
|
|
|
/* Indicate that the data has been consumed */
|
2020-01-21 08:50:39 +01:00
|
|
|
|
2023-05-25 13:52:41 +02:00
|
|
|
flags &= ~UDP_NEWDATA;
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2023-05-25 13:52:41 +02:00
|
|
|
/* Indicate no data in the buffer */
|
2022-11-23 12:39:40 +01:00
|
|
|
|
2023-05-25 13:52:41 +02:00
|
|
|
netdev_iob_release(dev);
|
|
|
|
}
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_recvfrom_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the state structure
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-02-08 16:24:04 +01:00
|
|
|
* conn The UDP connection of interest
|
2022-08-24 08:55:56 +02:00
|
|
|
* msg Receive info and buffer for receive data
|
2020-01-21 08:50:39 +01:00
|
|
|
* pstate A pointer to the state structure to be initialized
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-02-08 16:24:04 +01:00
|
|
|
static void udp_recvfrom_initialize(FAR struct udp_conn_s *conn,
|
2022-08-24 08:55:56 +02:00
|
|
|
FAR struct msghdr *msg,
|
2023-05-25 13:52:41 +02:00
|
|
|
FAR struct udp_recvfrom_s *pstate,
|
|
|
|
int flags)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
|
|
|
/* Initialize the state structure. */
|
|
|
|
|
|
|
|
memset(pstate, 0, sizeof(struct udp_recvfrom_s));
|
|
|
|
nxsem_init(&pstate->ir_sem, 0, 0); /* Doesn't really fail */
|
|
|
|
|
2023-05-25 13:52:41 +02:00
|
|
|
pstate->ir_msg = msg;
|
|
|
|
pstate->ir_flags = flags;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/* Set up the start time for the timeout */
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
pstate->ir_conn = conn;
|
2020-01-21 08:50:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The only un-initialization that has to be performed is destroying the
|
|
|
|
* semaphore.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define udp_recvfrom_uninitialize(s) nxsem_destroy(&(s)->ir_sem)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_recvfrom_result
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Evaluate the result of the recv operations
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2023-01-13 07:51:38 +01:00
|
|
|
* result The result of the net_sem_timedwait operation
|
|
|
|
* (may indicate EINTR)
|
2020-01-21 08:50:39 +01:00
|
|
|
* pstate A pointer to the state structure to be initialized
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The result of the recv operation with errno set appropriately
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t udp_recvfrom_result(int result, struct udp_recvfrom_s *pstate)
|
|
|
|
{
|
|
|
|
/* Check for a error/timeout detected by the event handler. Errors are
|
|
|
|
* signaled by negative errno values for the rcv length
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pstate->ir_result < 0)
|
|
|
|
{
|
|
|
|
/* This might return EAGAIN on a timeout or ENOTCONN on loss of
|
|
|
|
* connection (TCP only)
|
|
|
|
*/
|
|
|
|
|
|
|
|
return pstate->ir_result;
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
/* If net_sem_timedwait failed, then we were probably reawakened by a
|
|
|
|
* signal. In this case, net_sem_timedwait will have returned negated
|
|
|
|
* errno appropriately.
|
2020-01-21 08:50:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pstate->ir_recvlen;
|
|
|
|
}
|
|
|
|
|
2024-05-16 04:08:16 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: udp_notify_recvcpu
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function will check current cpu id with conn->rcvcpu, if
|
|
|
|
* not same, then use netdev_notify_recvcpu to notify the new cpu id
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - A reference to UDP connection structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* conn is not NULL.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_RSS
|
|
|
|
static void udp_notify_recvcpu(FAR struct udp_conn_s *conn)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu = up_cpu_index();
|
|
|
|
if (cpu != conn->rcvcpu)
|
|
|
|
{
|
|
|
|
if (conn->domain == PF_INET)
|
|
|
|
{
|
|
|
|
netdev_notify_recvcpu(conn->dev, cpu, conn->domain,
|
|
|
|
&(conn->u.ipv4.laddr), conn->lport,
|
|
|
|
&(conn->u.ipv4.raddr), conn->rport);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
netdev_notify_recvcpu(conn->dev, cpu, conn->domain,
|
|
|
|
&(conn->u.ipv6.laddr), conn->lport,
|
|
|
|
&(conn->u.ipv6.raddr), conn->rport);
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->rcvcpu = cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define udp_notify_recvcpu(c)
|
|
|
|
#endif /* CONFIG_NETDEV_RSS */
|
|
|
|
|
2020-01-21 08:50:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_udp_recvfrom
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform the recvfrom operation for a UDP SOCK_DGRAM
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Pointer to the socket structure for the SOCK_DRAM socket
|
2022-08-24 08:55:56 +02:00
|
|
|
* msg Receive info and buffer for receive data
|
2020-01-21 08:50:39 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters received. On error,
|
|
|
|
* -errno is returned (see recvfrom for list of errnos).
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct udp_conn_s *conn = psock->s_conn;
|
2020-01-21 08:50:39 +01:00
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
struct udp_recvfrom_s state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Perform the UDP recvfrom() operation */
|
|
|
|
|
|
|
|
/* Initialize the state structure. This is done with the network locked
|
|
|
|
* because we don't want anything to happen until we are ready.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_lock();
|
2023-05-25 13:52:41 +02:00
|
|
|
udp_recvfrom_initialize(conn, msg, &state, flags);
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/* Copy the read-ahead data from the packet */
|
|
|
|
|
|
|
|
udp_readahead(&state);
|
|
|
|
|
|
|
|
/* The default return value is the number of bytes that we just copied
|
|
|
|
* into the user buffer. We will return this if the socket has become
|
|
|
|
* disconnected or if the user request was completely satisfied with
|
|
|
|
* data from the readahead buffers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = state.ir_recvlen;
|
|
|
|
|
|
|
|
/* Handle non-blocking UDP sockets */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISNONBLOCK(conn->sconn.s_flags) || (flags & MSG_DONTWAIT) != 0)
|
2020-01-21 08:50:39 +01:00
|
|
|
{
|
|
|
|
/* Return the number of bytes read from the read-ahead buffer if
|
|
|
|
* something was received (already in 'ret'); EAGAIN if not.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Nothing was received */
|
|
|
|
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 10:42:18 +02:00
|
|
|
/* It is okay to block if we need to. If there is space to receive
|
|
|
|
* anything more, then we will wait to receive the data. Otherwise
|
|
|
|
* return the number of bytes read from the read-ahead buffer
|
|
|
|
* (already in 'ret').
|
2020-01-21 08:50:39 +01:00
|
|
|
*
|
|
|
|
* NOTE: that udp_readahead() may set state.ir_recvlen == -1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (state.ir_recvlen <= 0)
|
|
|
|
{
|
|
|
|
/* Get the device that will handle the packet transfers. This may be
|
|
|
|
* NULL if the UDP socket is bound to INADDR_ANY. In that case, no
|
|
|
|
* NETDEV_DOWN notifications will be received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev = udp_find_laddr_device(conn);
|
|
|
|
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
|
|
|
state.ir_cb = udp_callback_alloc(dev, conn);
|
|
|
|
if (state.ir_cb)
|
|
|
|
{
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2022-08-24 08:55:56 +02:00
|
|
|
state.ir_cb->flags = (UDP_NEWDATA | NETDEV_DOWN);
|
|
|
|
state.ir_cb->priv = (FAR void *)&state;
|
|
|
|
state.ir_cb->event = udp_eventhandler;
|
2020-01-21 08:50:39 +01:00
|
|
|
|
|
|
|
/* Wait for either the receive to complete or for an error/timeout
|
2023-01-13 07:51:38 +01:00
|
|
|
* to occur. net_sem_timedwait will also terminate if a signal is
|
2020-01-21 08:50:39 +01:00
|
|
|
* received.
|
|
|
|
*/
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_timedwait(&state.ir_sem,
|
2022-02-07 07:36:54 +01:00
|
|
|
_SO_TIMEOUT(conn->sconn.s_rcvtimeo));
|
2020-01-21 08:50:39 +01:00
|
|
|
if (ret == -ETIMEDOUT)
|
|
|
|
{
|
|
|
|
ret = -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that no further events are processed */
|
|
|
|
|
|
|
|
udp_callback_free(dev, conn, state.ir_cb);
|
|
|
|
ret = udp_recvfrom_result(ret, &state);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-16 04:08:16 +02:00
|
|
|
udp_notify_recvcpu(conn);
|
|
|
|
|
2020-01-21 08:50:39 +01:00
|
|
|
net_unlock();
|
|
|
|
udp_recvfrom_uninitialize(&state);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|