2015-01-27 00:28:36 +01:00
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* net/local/local_recvmsg.c
|
2015-01-27 00:28:36 +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
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-27 00:28:36 +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.
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2020-11-20 03:56:57 +01:00
|
|
|
#include <inttypes.h>
|
2015-01-29 14:47:09 +01:00
|
|
|
#include <unistd.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
#include <errno.h>
|
2015-01-27 18:51:24 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2023-10-22 10:53:58 +02:00
|
|
|
#include <fcntl.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
#include "socket/socket.h"
|
2015-01-27 00:28:36 +01:00
|
|
|
#include "local/local.h"
|
2023-04-20 06:15:24 +02:00
|
|
|
#include "utils/utils.h"
|
2015-01-27 00:28:36 +01:00
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
/****************************************************************************
|
2015-01-29 14:47:09 +01:00
|
|
|
* Private Functions
|
2015-01-27 18:51:24 +01:00
|
|
|
****************************************************************************/
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_fifo_read
|
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* A thin layer around local_fifo_read that handles socket-related loss-of-
|
2015-01-27 18:51:24 +01:00
|
|
|
* connection events.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int psock_fifo_read(FAR struct socket *psock, FAR void *buf,
|
2024-01-31 12:00:33 +01:00
|
|
|
size_t offset, FAR size_t *readlen,
|
|
|
|
int flags, bool once)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2015-01-27 18:51:24 +01:00
|
|
|
int ret;
|
|
|
|
|
2023-06-29 05:35:25 +02:00
|
|
|
if (flags & MSG_PEEK)
|
|
|
|
{
|
|
|
|
struct pipe_peek_s peek =
|
|
|
|
{
|
|
|
|
buf,
|
2024-01-31 12:00:33 +01:00
|
|
|
offset,
|
2023-06-29 05:35:25 +02:00
|
|
|
*readlen
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = file_ioctl(&conn->lc_infile, PIPEIOC_PEEK,
|
|
|
|
(unsigned long)((uintptr_t)&peek));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = local_fifo_read(&conn->lc_infile, buf, readlen, once);
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* -ECONNRESET is a special case. We may or not have received
|
|
|
|
* data, then the peer closed the connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == -ECONNRESET)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Lost connection: %d\n", ret);
|
2015-01-27 18:51:24 +01:00
|
|
|
|
|
|
|
/* Report an ungraceful loss of connection. This should
|
|
|
|
* eventually be reported as ENOTCONN.
|
|
|
|
*/
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
conn->lc_conn.s_flags &= ~(_SF_CONNECTED | _SF_CLOSED);
|
|
|
|
conn->lc_state = LOCAL_STATE_DISCONNECTED;
|
2015-01-27 18:51:24 +01:00
|
|
|
|
|
|
|
/* Did we receive any data? */
|
|
|
|
|
|
|
|
if (*readlen <= 0)
|
|
|
|
{
|
|
|
|
/* No.. return the ECONNRESET error now. Otherwise,
|
|
|
|
* process the received data and return ENOTCONN the
|
2020-02-23 09:50:23 +01:00
|
|
|
* next time that psock_recvfrom() is called.
|
2015-01-27 18:51:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-03 06:53:35 +02:00
|
|
|
if (ret != -EAGAIN)
|
2023-05-16 13:48:02 +02:00
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to read packet: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 23:28:54 +02:00
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_recvctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the socket message conntrol field
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn Local connection instance
|
|
|
|
* msg Message to send
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
static void local_recvctl(FAR struct local_conn_s *conn,
|
2023-01-30 15:12:30 +01:00
|
|
|
FAR struct msghdr *msg, int flags)
|
2021-09-15 17:22:18 +02:00
|
|
|
{
|
|
|
|
FAR struct local_conn_s *peer;
|
|
|
|
int count;
|
|
|
|
int *fds;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
if (conn->lc_peer == NULL)
|
|
|
|
{
|
|
|
|
peer = local_peerconn(conn);
|
|
|
|
if (peer == NULL)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
peer = conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer->lc_cfpcount == 0)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-04-20 06:15:24 +02:00
|
|
|
fds = cmsg_append(msg, SOL_SOCKET, SCM_RIGHTS, NULL,
|
|
|
|
sizeof(int) * peer->lc_cfpcount);
|
|
|
|
if (fds == NULL)
|
|
|
|
{
|
|
|
|
goto out;
|
|
|
|
}
|
2021-09-15 17:22:18 +02:00
|
|
|
|
2023-04-20 06:15:24 +02:00
|
|
|
count = peer->lc_cfpcount;
|
2021-09-15 17:22:18 +02:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
2023-10-22 10:53:58 +02:00
|
|
|
fds[i] = file_dup(peer->lc_cfps[i], 0,
|
|
|
|
flags & MSG_CMSG_CLOEXEC ? O_CLOEXEC : 0);
|
2021-09-15 17:22:18 +02:00
|
|
|
file_close(peer->lc_cfps[i]);
|
|
|
|
kmm_free(peer->lc_cfps[i]);
|
|
|
|
peer->lc_cfps[i] = NULL;
|
|
|
|
peer->lc_cfpcount--;
|
|
|
|
if (fds[i] < 0)
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
if (peer->lc_cfpcount)
|
|
|
|
{
|
|
|
|
memmove(peer->lc_cfps[0], peer->lc_cfps[i],
|
|
|
|
sizeof(FAR void *) * peer->lc_cfpcount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
net_unlock();
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
|
|
|
|
2015-01-27 00:28:36 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: psock_stream_recvfrom
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 14:47:09 +01:00
|
|
|
* psock_stream_recvfrom() receives messages from a local stream socket.
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
2015-01-29 14:47:09 +01:00
|
|
|
* Input Parameters:
|
2015-01-27 00:28:36 +01:00
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
|
|
|
* buf Buffer to receive data
|
|
|
|
* len Length of buffer
|
|
|
|
* flags Receive flags
|
|
|
|
* from Address of source (may be NULL)
|
|
|
|
* fromlen The length of the address structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-01-29 14:47:09 +01:00
|
|
|
* On success, returns the number of characters received. If no data is
|
2015-01-27 00:28:36 +01:00
|
|
|
* available to be received and the peer has performed an orderly shutdown,
|
|
|
|
* recv() will return 0. Otherwise, on errors, -1 is returned, and errno
|
2015-01-27 18:51:24 +01:00
|
|
|
* is set appropriately (see receive from for the complete list).
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-29 14:47:09 +01:00
|
|
|
static inline ssize_t
|
|
|
|
psock_stream_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
|
|
|
|
int flags, FAR struct sockaddr *from,
|
|
|
|
FAR socklen_t *fromlen)
|
2015-01-27 00:28:36 +01:00
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2021-07-21 09:41:49 +02:00
|
|
|
size_t readlen = len;
|
2015-01-27 18:51:24 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
/* Verify that this is a connected peer socket */
|
2015-01-27 18:51:24 +01:00
|
|
|
|
2023-11-24 13:12:49 +01:00
|
|
|
if (conn->lc_state != LOCAL_STATE_CONNECTED)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: not connected\n");
|
2015-01-27 18:51:24 +01:00
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2023-11-24 13:12:49 +01:00
|
|
|
/* Check shutdown state */
|
|
|
|
|
|
|
|
if (conn->lc_infile.f_inode == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-01 13:40:28 +02:00
|
|
|
/* If it is non-blocking mode, the data in fifo is 0 and
|
|
|
|
* returns directly
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (flags & MSG_DONTWAIT)
|
|
|
|
{
|
|
|
|
int data_len = 0;
|
|
|
|
ret = file_ioctl(&conn->lc_infile, FIONREAD, &data_len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data_len == 0)
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
/* Read the packet */
|
|
|
|
|
2024-01-31 12:00:33 +01:00
|
|
|
ret = psock_fifo_read(psock, buf, 0, &readlen, flags, true);
|
2015-01-27 18:51:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
/* Return the address family */
|
|
|
|
|
|
|
|
if (from)
|
|
|
|
{
|
|
|
|
ret = local_getaddr(conn, from, fromlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return readlen;
|
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2024-01-31 12:00:33 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_fifo_discard
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_fifo_discard() discard buffer from a local socket.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
|
|
|
* len Remaining length of buffer
|
|
|
|
* flags Receive flags
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns OK. Otherwise, on errors, errno is returned
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
static int psock_fifo_discard(FAR struct socket *psock, size_t len,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
uint8_t bitbucket[256];
|
|
|
|
size_t tmplen;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (flags & MSG_PEEK)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
/* Read 256 bytes into the bit bucket */
|
|
|
|
|
|
|
|
tmplen = MIN(len, sizeof(bitbucket));
|
|
|
|
ret = psock_fifo_read(psock, bitbucket, 0, &tmplen, flags, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to get bitbucket : ret %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust the number of bytes len to be read from the packet */
|
|
|
|
|
|
|
|
DEBUGASSERT(tmplen <= len);
|
|
|
|
len -= tmplen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: psock_dgram_recvfrom
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_dgram_recvfrom() receives messages from a local datagram socket.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
|
|
|
* buf Buffer to receive data
|
|
|
|
* len Length of buffer
|
|
|
|
* flags Receive flags
|
|
|
|
* from Address of source (may be NULL)
|
|
|
|
* fromlen The length of the address structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters received. Otherwise, on
|
|
|
|
* errors, -1 is returned, and errno is set appropriately (see receive
|
|
|
|
* from for the complete list).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline ssize_t
|
|
|
|
psock_dgram_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
|
|
|
|
int flags, FAR struct sockaddr *from,
|
|
|
|
FAR socklen_t *fromlen)
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2015-01-29 14:47:09 +01:00
|
|
|
size_t readlen;
|
2024-01-31 12:00:33 +01:00
|
|
|
size_t pathlen;
|
2023-06-27 10:28:09 +02:00
|
|
|
bool bclose = false;
|
2024-08-26 08:48:55 +02:00
|
|
|
lc_size_t addrlen;
|
|
|
|
lc_size_t pktlen;
|
2024-01-31 12:00:33 +01:00
|
|
|
int offset = 0;
|
|
|
|
int ret;
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Verify that this is a bound, un-connected peer socket */
|
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
if (conn->lc_state != LOCAL_STATE_BOUND &&
|
|
|
|
conn->lc_state != LOCAL_STATE_CONNECTED)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
|
|
|
/* Either not bound to address or it is connected */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Connected or not bound\n");
|
2015-01-29 14:47:09 +01:00
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
if (conn->lc_infile.f_inode == NULL)
|
|
|
|
{
|
|
|
|
bclose = true;
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
/* Make sure that half duplex FIFO has been created */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2024-08-14 14:41:23 +02:00
|
|
|
ret = local_create_halfduplex(conn, conn->lc_path, conn->lc_rcvsize);
|
2023-06-27 10:28:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to create FIFO for %s: %d\n",
|
|
|
|
conn->lc_path, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
/* Open the receiving side of the transfer */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
ret = local_open_receiver(conn, (flags & MSG_DONTWAIT) != 0 ||
|
|
|
|
_SS_ISNONBLOCK(conn->lc_conn.s_flags));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to open FIFO for %s: %d\n",
|
|
|
|
conn->lc_path, ret);
|
|
|
|
goto errout_with_halfduplex;
|
|
|
|
}
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
|
|
|
|
2024-01-31 12:00:33 +01:00
|
|
|
readlen = sizeof(addrlen);
|
|
|
|
ret = psock_fifo_read(psock, &addrlen, offset, &readlen, flags, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to get path length: ret %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
/* Sync to the start of the next packet in the stream and get the size of
|
|
|
|
* the next packet.
|
2015-01-27 18:51:24 +01:00
|
|
|
*/
|
|
|
|
|
2024-01-31 12:00:33 +01:00
|
|
|
readlen = sizeof(pktlen);
|
|
|
|
offset += sizeof(addrlen);
|
|
|
|
ret = psock_fifo_read(psock, &pktlen, offset, &readlen, flags, false);
|
|
|
|
if (ret < 0)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2024-01-31 12:00:33 +01:00
|
|
|
nerr("ERROR: Failed to get packet length: ret %d\n", ret);
|
|
|
|
goto errout_with_infd;
|
|
|
|
}
|
|
|
|
|
|
|
|
readlen = addrlen;
|
|
|
|
offset += sizeof(pktlen);
|
|
|
|
|
|
|
|
if (from && fromlen && *fromlen)
|
|
|
|
{
|
|
|
|
pathlen = MIN(*fromlen - 1, readlen);
|
|
|
|
ret = psock_fifo_read(psock, from->sa_data, offset,
|
|
|
|
&pathlen, flags, false);
|
2023-06-29 05:35:25 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-01-31 12:00:33 +01:00
|
|
|
nerr("ERROR: Failed to get path : ret %d\n", ret);
|
2023-06-29 05:35:25 +02:00
|
|
|
goto errout_with_infd;
|
|
|
|
}
|
2024-01-31 12:00:33 +01:00
|
|
|
|
|
|
|
from->sa_family = AF_LOCAL;
|
|
|
|
from->sa_data[pathlen] = '\0';
|
|
|
|
*fromlen = pathlen;
|
|
|
|
readlen -= pathlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readlen)
|
|
|
|
{
|
|
|
|
ret = psock_fifo_discard(psock, readlen, flags);
|
|
|
|
if (ret < 0)
|
2023-06-29 05:35:25 +02:00
|
|
|
{
|
2024-01-31 12:00:33 +01:00
|
|
|
nerr("ERROR: Failed to discard redunance address: ret %d\n", ret);
|
2023-06-29 05:35:25 +02:00
|
|
|
goto errout_with_infd;
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Read the packet */
|
|
|
|
|
2024-01-31 12:00:33 +01:00
|
|
|
readlen = MIN(pktlen, len);
|
|
|
|
offset += addrlen;
|
|
|
|
ret = psock_fifo_read(psock, buf, offset, &readlen, flags, false);
|
2015-01-29 14:47:09 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-01-31 12:00:33 +01:00
|
|
|
nerr("ERROR: Failed to get packet : ret %d\n", ret);
|
2015-01-29 14:47:09 +01:00
|
|
|
goto errout_with_infd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are unread bytes remaining in the packet, flush the remainder
|
|
|
|
* of the packet to the bit bucket.
|
|
|
|
*/
|
|
|
|
|
2024-01-31 12:00:33 +01:00
|
|
|
DEBUGASSERT(readlen <= pktlen);
|
|
|
|
if (readlen < pktlen)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2024-01-31 12:00:33 +01:00
|
|
|
ret = psock_fifo_discard(psock, pktlen - readlen, flags);
|
2015-01-27 18:51:24 +01:00
|
|
|
}
|
|
|
|
|
2015-01-29 14:47:09 +01:00
|
|
|
errout_with_infd:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
/* Close the read-only file descriptor */
|
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
if (bclose)
|
|
|
|
{
|
|
|
|
/* Now we can close the read-only file descriptor */
|
|
|
|
|
|
|
|
file_close(&conn->lc_infile);
|
|
|
|
conn->lc_infile.f_inode = NULL;
|
2015-01-30 16:28:55 +01:00
|
|
|
|
|
|
|
errout_with_halfduplex:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
/* Release our reference to the half duplex FIFO */
|
|
|
|
|
|
|
|
local_release_halfduplex(conn);
|
|
|
|
}
|
2015-01-30 16:28:55 +01:00
|
|
|
|
2023-06-27 10:28:09 +02:00
|
|
|
return ret < 0 ? ret : readlen;
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2024-02-28 06:14:19 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: local_recvmsg
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2020-12-01 07:55:16 +01:00
|
|
|
* recvmsg() receives messages from a local socket and may be used to
|
|
|
|
* receive data on a socket whether or not it is connection-oriented.
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
* If from is not NULL, and the underlying protocol provides the source
|
|
|
|
* address, this source address is filled in. The argument fromlen
|
|
|
|
* initialized to the size of the buffer associated with from, 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
|
2020-12-01 07:55:16 +01:00
|
|
|
* msg Buffer to receive the message
|
|
|
|
* flags Receive flags (ignored for now)
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-12-01 07:55:16 +01:00
|
|
|
* On success, returns the number of characters received. If no data is
|
2015-01-29 14:47:09 +01:00
|
|
|
* available to be received and the peer has performed an orderly shutdown,
|
2020-12-01 07:55:16 +01:00
|
|
|
* recvmsg() will return 0. Otherwise, on errors, a negated errno value is
|
|
|
|
* returned (see recvmsg() for the list of appropriate error values).
|
2015-01-29 14:47:09 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
ssize_t local_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2021-09-15 17:22:18 +02:00
|
|
|
FAR socklen_t *fromlen = &msg->msg_namelen;
|
|
|
|
FAR struct sockaddr *from = msg->msg_name;
|
2020-12-01 07:55:16 +01:00
|
|
|
FAR void *buf = msg->msg_iov->iov_base;
|
|
|
|
size_t len = msg->msg_iov->iov_len;
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(buf);
|
2015-01-29 14:47:09 +01:00
|
|
|
|
|
|
|
/* Check for a stream socket */
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-29 14:47:09 +01:00
|
|
|
if (psock->s_type == SOCK_STREAM)
|
|
|
|
{
|
2021-09-15 17:22:18 +02:00
|
|
|
len = psock_stream_recvfrom(psock, buf, len, flags, from, fromlen);
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
if (psock->s_type == SOCK_DGRAM)
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
2021-09-15 17:22:18 +02:00
|
|
|
len = psock_dgram_recvfrom(psock, buf, len, flags, from, fromlen);
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
|
|
|
else
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif
|
2015-01-29 14:47:09 +01:00
|
|
|
{
|
|
|
|
DEBUGPANIC();
|
2020-11-20 03:56:57 +01:00
|
|
|
nerr("ERROR: Unrecognized socket type: %" PRIu8 "\n", psock->s_type);
|
2021-09-15 17:22:18 +02:00
|
|
|
len = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
/* Receive the control message */
|
|
|
|
|
|
|
|
if (len >= 0 && msg->msg_control &&
|
|
|
|
msg->msg_controllen > sizeof(struct cmsghdr))
|
|
|
|
{
|
2023-01-30 15:12:30 +01:00
|
|
|
local_recvctl(psock->s_conn, msg, flags);
|
2015-01-29 14:47:09 +01:00
|
|
|
}
|
2021-09-15 17:22:18 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
|
|
|
|
|
|
|
return len;
|
2015-01-27 00:28:36 +01:00
|
|
|
}
|