* net/pkt/pkt_recvmsg.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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <assert.h>
#include <arch/irq.h>
#include <nuttx/semaphore.h>
#include <nuttx/net/net.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/ethernet.h>
#include "netdev/netdev.h"
#include "devif/devif.h"
#include "pkt/pkt.h"
#include "socket/socket.h"
#include "utils/utils.h"
#include <netinet/if_ether.h>
#include <netpacket/packet.h>
* Private Types
****************************************************************************/
struct pkt_recvfrom_s
{
FAR struct pkt_conn_s *pr_conn;
FAR struct devif_callback_s *pr_cb;
FAR struct msghdr *pr_msg;
sem_t pr_sem;
ssize_t pr_recvlen;
int pr_result;
uint8_t pr_type;
};
* Name: pkt_add_recvlen
*
* Description:
* Update information about space available for new data and update size
* of data in buffer, This logic accounts for the case where
* recvfrom_udpreadahead() sets state.pr_recvlen == -1 .
*
* Input Parameters:
* pstate recvfrom state structure
* recvlen size of new data appended to buffer
*
* Returned Value:
* None
*
****************************************************************************/
static inline void pkt_add_recvlen(FAR struct pkt_recvfrom_s *pstate,
size_t recvlen)
{
if (pstate->pr_recvlen < 0)
{
pstate->pr_recvlen = 0;
}
pstate->pr_recvlen += recvlen;
}
* Name: pkt_recvfrom_newdata
*
* Description:
* Copy the read data from the packet
*
* Input Parameters:
* dev The structure of the network driver that caused the event.
* pstate recvfrom state structure
*
* Returned Value:
* None.
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static void pkt_recvfrom_newdata(FAR struct net_driver_s *dev,
FAR struct pkt_recvfrom_s *pstate)
{
unsigned int offset = 0;
size_t recvlen;
#ifdef CONFIG_NET_TIMESTAMP
cmsg_store_timestamp(pstate->pr_msg, &dev->d_iob->io_time,
pstate->pr_conn->sconn.s_options);
#endif
recvlen = MIN(pstate->pr_msg->msg_iov->iov_len, dev->d_len);
if (pstate->pr_type == SOCK_RAW)
{
offset = -NET_LL_HDRLEN(dev);
}
recvlen = iob_copyout(pstate->pr_msg->msg_iov->iov_base,
dev->d_iob, recvlen, offset);
ninfo("Received %d bytes (of %d)\n", (int)recvlen, (int)dev->d_len);
pkt_add_recvlen(pstate, recvlen);
}
* Name: pkt_recvfrom_sender
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
* Assumptions:
*
****************************************************************************/
static inline void pkt_recvfrom_sender(int ifindex, FAR struct iob_s *iob,
FAR struct pkt_recvfrom_s *pstate)
{
FAR struct sockaddr_ll *from;
if (pstate->pr_msg->msg_name != NULL)
{
from = (FAR struct sockaddr_ll *)pstate->pr_msg->msg_name;
from->sll_family = AF_PACKET;
from->sll_ifindex = ifindex;
from->sll_protocol = pstate->pr_type;
from->sll_halen = ETH_ALEN;
if (iob != NULL && iob->io_pktlen >= sizeof(struct eth_hdr_s))
{
FAR struct eth_hdr_s *ethhdr =
(FAR struct eth_hdr_s *)iob->io_data;
memcpy(from->sll_addr, ethhdr->src, ETH_ALEN);
}
else
{
memset(from->sll_addr, 0, sizeof(from->sll_addr));
}
pstate->pr_msg->msg_namelen = sizeof(struct sockaddr_ll);
}
}
* Name: pkt_recvfrom_eventhandler
*
* Description:
*
* Input Parameters:
*
* Returned Value:
*
* Assumptions:
*
****************************************************************************/
static uint32_t pkt_recvfrom_eventhandler(FAR struct net_driver_s *dev,
FAR void *pvpriv, uint32_t flags)
{
struct pkt_recvfrom_s *pstate = pvpriv;
ninfo("flags: %" PRIx32 "\n", flags);
if (pstate)
{
#ifdef CONFIG_NET_TIMESTAMPING
if ((flags & PKT_NEWDATA) != 0 && dev->d_iob->io_conn != NULL)
{
pstate->pr_cb->flags = 0;
pstate->pr_cb->priv = NULL;
pstate->pr_cb->event = NULL;
pstate->pr_result = -EAGAIN;
nxsem_post(&pstate->pr_sem);
}
else
#endif
if ((flags & PKT_NEWDATA) != 0)
{
pkt_recvfrom_newdata(dev, pstate);
ninfo("PKT done\n");
pstate->pr_cb->flags = 0;
pstate->pr_cb->priv = NULL;
pstate->pr_cb->event = NULL;
pkt_recvfrom_sender(dev->d_ifindex, dev->d_iob, pstate);
flags &= ~PKT_NEWDATA;
* actually read.
*/
nxsem_post(&pstate->pr_sem);
}
}
return flags;
}
* Name: pkt_recvfrom_initialize
*
* Description:
* Initialize the state structure
*
* Input Parameters:
* conn The PKT connection of interest
* msg Receive info and buffer for receive data
* pstate A pointer to the state structure to be initialized
* type Protocol type
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
static void pkt_recvfrom_initialize(FAR struct pkt_conn_s *conn,
FAR struct msghdr *msg,
FAR struct pkt_recvfrom_s *pstate,
uint8_t type)
{
memset(pstate, 0, sizeof(struct pkt_recvfrom_s));
nxsem_init(&pstate->pr_sem, 0, 0);
pstate->pr_conn = conn;
pstate->pr_msg = msg;
pstate->pr_type = type;
}
* semaphore.
*/
#define pkt_recvfrom_uninitialize(s) nxsem_destroy(&(s)->pr_sem)
* Name: pkt_recvfrom_result
*
* Description:
* Evaluate the result of the recv operations
*
* Input Parameters:
* result The result of the conn_dev_sem_timedwait operation
* (may indicate EINTR)
* 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 pkt_recvfrom_result(int result,
FAR struct pkt_recvfrom_s *pstate)
{
* signaled by negative errno values for the rcv length
*/
if (pstate->pr_result < 0)
{
* connection (TCP only)
*/
return pstate->pr_result;
}
* signal. In this case, conn_dev_sem_timedwait will have returned negated
* errno appropriately.
*/
if (result < 0)
{
return result;
}
return pstate->pr_recvlen;
}
* Name: pkt_readdata
*
* Description:
* Copy the buffered data to the user buffer based on the flag errmsg.
*
* Input Parameters:
* pstate The state structure of the recv operation
*
* Returned Value:
* copy length or -ENODATA
*
* Assumptions:
* The network is locked.
*
****************************************************************************/
static void append_timestamp(FAR struct pkt_recvfrom_s *pstate,
FAR struct iob_s *iob)
{
#ifdef CONFIG_NET_TIMESTAMP
FAR struct pkt_conn_s *conn = pstate->pr_conn;
cmsg_store_timestamp(pstate->pr_msg, &iob->io_time,
conn->sconn.s_options);
#endif
}
#ifdef CONFIG_NET_TIMESTAMPING
static void append_timestamping(FAR struct pkt_recvfrom_s *pstate,
FAR struct iob_s *iob)
{
FAR struct pkt_conn_s *conn = pstate->pr_conn;
struct timespec ts[3];
memset(&ts, 0, sizeof(ts));
ts[0].tv_sec = iob->io_time.tv_sec;
ts[0].tv_nsec = iob->io_time.tv_nsec;
ts[2].tv_sec = iob->io_time.tv_sec;
ts[2].tv_nsec = iob->io_time.tv_nsec;
cmsg_append(pstate->pr_msg, SOL_SOCKET, SO_TIMESTAMPING, &ts,
sizeof(ts));
pstate->pr_msg->msg_flags |= MSG_ERRQUEUE;
}
#endif
static inline int pkt_readdata(FAR struct pkt_recvfrom_s *pstate,
FAR struct iob_queue_s *iobq,
CODE void (*tsfunc)(FAR struct pkt_recvfrom_s *,
FAR struct iob_s *))
{
FAR struct pkt_conn_s *conn = pstate->pr_conn;
FAR struct net_driver_s *dev;
FAR struct iob_s *iob;
int recvlen;
int offset = 0;
pstate->pr_recvlen = -ENODATA;
if ((iob = iob_remove_queue(iobq)) != NULL)
{
DEBUGASSERT(iob->io_pktlen > 0);
dev = pkt_find_device(conn);
if (pstate->pr_type == SOCK_DGRAM)
{
if (dev != NULL)
{
offset = NET_LL_HDRLEN(dev);
}
else
{
offset = sizeof(struct eth_hdr_s);
}
}
recvlen = iob_copyout(pstate->pr_msg->msg_iov->iov_base, iob,
pstate->pr_msg->msg_iov->iov_len, offset);
pstate->pr_recvlen = recvlen;
ninfo("Received %d bytes (of %u)\n", recvlen, iob->io_pktlen);
tsfunc(pstate, iob);
if (dev != NULL && pstate->pr_msg->msg_name != NULL)
{
pkt_recvfrom_sender(dev->d_ifindex, iob, pstate);
}
iob_free_chain(iob);
}
return pstate->pr_recvlen;
}
* Public Functions
****************************************************************************/
* Name: pkt_recvmsg
*
* Description:
* Implements the socket recvmsg interface for the case of the AF_INET
* and AF_INET6 address families. pkt_recvmsg() receives messages from
* a socket, and may be used to receive data on a socket whether or not it
* is connection-oriented.
*
* If 'msg_name' is not NULL, and the underlying protocol provides the
* source address, this source address is filled in. The argument
* 'msg_namelen' is initialized to the size of the buffer associated with
* msg_name, and modified on return to indicate the actual size of the
* address stored there.
*
* Input Parameters:
* psock A pointer to a NuttX-specific, internal socket structure
* msg Buffer to receive the message
* flags Receive flags
*
* Returned Value:
* On success, returns the number of characters received. If no data is
* available to be received and the peer has performed an orderly shutdown,
* recvmsg() will return 0. Otherwise, on errors, a negated errno value is
* returned (see recvmsg() for the list of appropriate error values).
*
****************************************************************************/
ssize_t pkt_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
int flags)
{
FAR struct sockaddr_ll *from = msg->msg_name;
FAR socklen_t *fromlen = &msg->msg_namelen;
FAR struct pkt_conn_s *conn = psock->s_conn;
FAR struct net_driver_s *dev;
struct pkt_recvfrom_s state;
ssize_t ret = 0;
* enough to hold this address family.
*/
if (from != NULL && *fromlen < sizeof(struct sockaddr_ll))
{
return -EINVAL;
}
if (msg->msg_iovlen != 1)
{
return -ENOTSUP;
}
if (psock->s_type != SOCK_DGRAM && psock->s_type != SOCK_RAW)
{
nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);
return -ENOSYS;
}
dev = pkt_find_device(conn);
if (dev == NULL)
{
return -ENODEV;
}
* locked because we don't want anything to happen until we are ready.
*/
pkt_recvfrom_initialize(conn, msg, &state, psock->s_type);
conn_dev_lock(&conn->sconn, dev);
#ifdef CONFIG_NET_TIMESTAMPING
if (flags & MSG_ERRQUEUE)
{
if (!IOB_QEMPTY(&conn->errahead))
{
ret = pkt_readdata(&state, &conn->errahead, append_timestamping);
}
else
{
ret = -EAGAIN;
}
}
else
#endif
* already received the response to previous command.
*/
if (!IOB_QEMPTY(&conn->readahead))
{
ret = pkt_readdata(&state, &conn->readahead, append_timestamp);
}
else if (_SS_ISNONBLOCK(conn->sconn.s_flags) ||
(flags & MSG_DONTWAIT) != 0)
{
ret = -EAGAIN;
}
else
{
* sockaddr_in, but in our case is sockaddr_ll
*/
#if 0
ret = pkt_connect(conn, NULL);
if (ret < 0)
{
goto errout_with_state;
}
#endif
state.pr_cb = pkt_callback_alloc(dev, conn);
if (state.pr_cb)
{
state.pr_cb->flags = (PKT_NEWDATA | PKT_POLL);
state.pr_cb->priv = (FAR void *)&state;
state.pr_cb->event = pkt_recvfrom_eventhandler;
* to occur. NOTES: (1) conn_dev_sem_timedwait will also terminate
* if a signal is received, (2) the network is locked! It will be
* un-locked while the task sleeps and automatically re-locked when
* the task restarts.
*/
ret = conn_dev_sem_timedwait(&state.pr_sem, true, UINT_MAX,
&conn->sconn, dev);
pkt_callback_free(dev, conn, state.pr_cb);
ret = pkt_recvfrom_result(ret, &state);
}
else
{
ret = -EBUSY;
}
}
conn_dev_unlock(&conn->sconn, dev);
pkt_recvfrom_uninitialize(&state);
return ret;
}
#endif