2015-01-27 00:28:36 +01:00
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* net/local/local_sendmsg.c
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
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>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-01-29 17:40:10 +01:00
|
|
|
#include <unistd.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
#include <errno.h>
|
2015-01-29 17:40:10 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-01-27 00:28:36 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
#include "socket/socket.h"
|
2015-01-27 00:28:36 +01:00
|
|
|
#include "local/local.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Private Functions
|
2015-01-27 00:28:36 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_sendctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle the socket message conntrol field
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn Local connection instance
|
|
|
|
* msg Message to send
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On any failure, a negated errno value is returned
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
2024-02-28 07:05:59 +01:00
|
|
|
static void local_freectl(FAR struct local_conn_s *conn, int count)
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *peer = conn->lc_peer;
|
|
|
|
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
|
|
|
file_close(peer->lc_cfps[--peer->lc_cfpcount]);
|
|
|
|
kmm_free(peer->lc_cfps[peer->lc_cfpcount]);
|
|
|
|
peer->lc_cfps[peer->lc_cfpcount] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
static int local_sendctl(FAR struct local_conn_s *conn,
|
|
|
|
FAR struct msghdr *msg)
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *peer;
|
|
|
|
FAR struct file *filep2;
|
|
|
|
FAR struct file *filep;
|
|
|
|
struct cmsghdr *cmsg;
|
2023-02-07 10:01:31 +01:00
|
|
|
int count = 0;
|
2021-09-15 17:22:18 +02:00
|
|
|
int *fds;
|
|
|
|
int ret;
|
2023-02-07 10:01:31 +01:00
|
|
|
int i = 0;
|
2021-09-15 17:22:18 +02:00
|
|
|
|
|
|
|
net_lock();
|
|
|
|
peer = conn->lc_peer;
|
|
|
|
if (peer == NULL)
|
|
|
|
{
|
|
|
|
peer = conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
for_each_cmsghdr(cmsg, msg)
|
|
|
|
{
|
|
|
|
if (!CMSG_OK(msg, cmsg) ||
|
|
|
|
cmsg->cmsg_level != SOL_SOCKET ||
|
|
|
|
cmsg->cmsg_type != SCM_RIGHTS)
|
|
|
|
{
|
|
|
|
ret = -EOPNOTSUPP;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fds = (int *)CMSG_DATA(cmsg);
|
|
|
|
count = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
|
|
|
|
|
2023-10-12 08:50:17 +02:00
|
|
|
if (count + peer->lc_cfpcount >= LOCAL_NCONTROLFDS)
|
2021-09-15 17:22:18 +02:00
|
|
|
{
|
|
|
|
ret = -EMFILE;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
ret = fs_getfilep(fds[i], &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
filep2 = kmm_zalloc(sizeof(*filep2));
|
2021-09-15 17:22:18 +02:00
|
|
|
if (!filep2)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = file_dup2(filep, filep2);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(filep2);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->lc_cfps[peer->lc_cfpcount++] = filep2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
return count;
|
|
|
|
|
|
|
|
fail:
|
2024-02-28 07:05:59 +01:00
|
|
|
local_freectl(conn, i);
|
2021-09-15 17:22:18 +02:00
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
|
|
|
|
2015-01-27 00:28:36 +01:00
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: local_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Send a local packet as a stream.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock An instance of the internal socket structure.
|
|
|
|
* buf Data to send
|
|
|
|
* len Length of data to send
|
|
|
|
* flags Send flags (ignored for now)
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
|
|
|
* -1 is returned, and errno is set appropriately (see send() for the
|
|
|
|
* list of errno numbers).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t local_send(FAR struct socket *psock,
|
|
|
|
FAR const struct iovec *buf,
|
|
|
|
size_t len, int flags)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
|
|
|
case SOCK_STREAM:
|
2023-06-27 10:28:09 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2020-12-01 07:55:16 +01:00
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2020-12-01 07:55:16 +01:00
|
|
|
|
|
|
|
/* Local TCP packet send */
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(buf);
|
2020-12-01 07:55:16 +01:00
|
|
|
|
|
|
|
/* Verify that this is a connected peer socket and that it has
|
|
|
|
* opened the outgoing FIFO for write-only access.
|
|
|
|
*/
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
if (conn->lc_state != LOCAL_STATE_CONNECTED)
|
2020-12-01 07:55:16 +01:00
|
|
|
{
|
|
|
|
nerr("ERROR: not connected\n");
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2023-11-24 13:12:49 +01:00
|
|
|
/* Check shutdown state */
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
if (conn->lc_outfile.f_inode == NULL)
|
2023-11-24 13:12:49 +01:00
|
|
|
{
|
|
|
|
return -EPIPE;
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Send the packet */
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
ret = nxmutex_lock(&conn->lc_sendlock);
|
2022-08-08 12:20:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* May fail because the task was canceled. */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
ret = local_send_packet(&conn->lc_outfile, buf, len,
|
2023-06-27 10:28:09 +02:00
|
|
|
psock->s_type == SOCK_DGRAM);
|
2024-02-28 06:14:19 +01:00
|
|
|
nxmutex_unlock(&conn->lc_sendlock);
|
2020-12-01 07:55:16 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* EDESTADDRREQ. Signifies that the socket is not connection-mode
|
|
|
|
* and no peer address is set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -EDESTADDRREQ;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_sendto
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-28 20:41:24 +01:00
|
|
|
* This function implements the Unix domain-specific logic of the
|
|
|
|
* standard sendto() socket operation.
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
2015-01-28 20:41:24 +01:00
|
|
|
* Input Parameters:
|
2015-01-27 00:28:36 +01:00
|
|
|
* 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
|
|
|
|
*
|
2015-01-28 20:41:24 +01:00
|
|
|
* NOTE: All input parameters were verified by sendto() before this
|
|
|
|
* function was called.
|
|
|
|
*
|
2015-01-27 00:28:36 +01:00
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
2015-01-28 20:41:24 +01:00
|
|
|
* a negated errno value is returned. See the description in
|
|
|
|
* net/socket/sendto.c for the list of appropriate return value.
|
2015-01-27 00:28:36 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
static ssize_t local_sendto(FAR struct socket *psock,
|
|
|
|
FAR const struct iovec *buf,
|
|
|
|
size_t len, int flags,
|
|
|
|
FAR const struct sockaddr *to,
|
|
|
|
socklen_t tolen)
|
2015-01-27 00:28:36 +01:00
|
|
|
{
|
2020-12-01 07:55:16 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2015-01-29 17:40:10 +01:00
|
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)to;
|
2021-03-31 12:21:02 +02:00
|
|
|
ssize_t ret;
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
|
|
|
if (to->sa_family != AF_LOCAL || tolen < sizeof(sa_family_t))
|
|
|
|
{
|
|
|
|
nerr("ERROR: Unrecognized address family: %d\n",
|
|
|
|
to->sa_family);
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* If this is a connected socket, then return EISCONN */
|
|
|
|
|
|
|
|
if (psock->s_type != SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Connected socket\n");
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2015-01-29 18:05:04 +01:00
|
|
|
/* Verify that this is not a connected peer socket. It need not be
|
|
|
|
* bound, however. If unbound, recvfrom will see this as a nameless
|
|
|
|
* connection.
|
|
|
|
*/
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2015-01-29 18:05:04 +01:00
|
|
|
if (conn->lc_state != LOCAL_STATE_UNBOUND &&
|
|
|
|
conn->lc_state != LOCAL_STATE_BOUND)
|
2015-01-29 17:40:10 +01:00
|
|
|
{
|
|
|
|
/* Either not bound to address or it is connected */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Connected state\n");
|
2015-01-29 17:40:10 +01:00
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The outgoing FIFO should not be open */
|
|
|
|
|
2023-05-30 07:28:05 +02:00
|
|
|
DEBUGASSERT(conn->lc_outfile.f_inode == NULL);
|
2015-01-29 17:40:10 +01:00
|
|
|
|
|
|
|
/* At present, only standard pathname type address are support */
|
|
|
|
|
|
|
|
if (tolen < sizeof(sa_family_t) + 2)
|
|
|
|
{
|
2021-02-19 15:01:46 +01:00
|
|
|
/* EFAULT
|
|
|
|
* - An invalid user space address was specified for a parameter
|
|
|
|
*/
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
return -EFAULT;
|
2015-01-29 17:40:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that half duplex FIFO has been created.
|
|
|
|
* REVISIT: Or should be just make sure that it already exists?
|
|
|
|
*/
|
|
|
|
|
2015-01-29 18:05:04 +01:00
|
|
|
ret = local_create_halfduplex(conn, unaddr->sun_path);
|
2015-01-29 17:40:10 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-31 12:21:02 +02:00
|
|
|
nerr("ERROR: Failed to create FIFO for %s: %zd\n",
|
2015-01-29 17:40:10 +01:00
|
|
|
conn->lc_path, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the sending side of the transfer */
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_open_sender(conn, unaddr->sun_path,
|
2022-02-08 06:18:01 +01:00
|
|
|
_SS_ISNONBLOCK(conn->lc_conn.s_flags) ||
|
2020-02-19 19:21:28 +01:00
|
|
|
(flags & MSG_DONTWAIT) != 0);
|
2015-01-29 17:40:10 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-31 12:21:02 +02:00
|
|
|
nerr("ERROR: Failed to open FIFO for %s: %zd\n",
|
2015-01-29 17:40:10 +01:00
|
|
|
unaddr->sun_path, ret);
|
2015-01-30 16:28:55 +01:00
|
|
|
|
|
|
|
goto errout_with_halfduplex;
|
2015-01-29 17:40:10 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 12:20:41 +02:00
|
|
|
/* Make sure that dgram is sent safely */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&conn->lc_sendlock);
|
2022-08-08 12:20:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* May fail because the task was canceled. */
|
|
|
|
|
|
|
|
goto errout_with_sender;
|
|
|
|
}
|
|
|
|
|
2015-01-29 17:40:10 +01:00
|
|
|
/* Send the packet */
|
|
|
|
|
2021-07-21 09:41:49 +02:00
|
|
|
ret = local_send_packet(&conn->lc_outfile, buf, len, true);
|
2021-03-31 12:21:02 +02:00
|
|
|
if (ret < 0)
|
2015-01-29 17:40:10 +01:00
|
|
|
{
|
2021-03-31 12:21:02 +02:00
|
|
|
nerr("ERROR: Failed to send the packet: %zd\n", ret);
|
2015-01-29 18:05:04 +01:00
|
|
|
}
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&conn->lc_sendlock);
|
2022-08-08 12:20:41 +02:00
|
|
|
|
|
|
|
errout_with_sender:
|
|
|
|
|
2015-01-29 17:40:10 +01:00
|
|
|
/* Now we can close the write-only socket descriptor */
|
|
|
|
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&conn->lc_outfile);
|
2017-11-02 15:23:38 +01:00
|
|
|
conn->lc_outfile.f_inode = NULL;
|
2015-01-29 17:40:10 +01:00
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
errout_with_halfduplex:
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
/* Release our reference to the half duplex FIFO */
|
2015-01-30 16:28:55 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
local_release_halfduplex(conn);
|
2021-03-31 12:21:02 +02:00
|
|
|
return ret;
|
2020-12-01 07:55:16 +01:00
|
|
|
#else
|
|
|
|
return -EISCONN;
|
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_sendmsg
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Implements the sendmsg() operation for the case of the local Unix socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock A pointer to a NuttX-specific, internal socket structure
|
|
|
|
* msg msg to send
|
|
|
|
* flags Send flags
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error, a negated
|
|
|
|
* errno value is returned (see sendmsg() for the list of appropriate error
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t local_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
FAR const struct sockaddr *to = msg->msg_name;
|
2021-09-15 17:22:18 +02:00
|
|
|
FAR const struct iovec *buf = msg->msg_iov;
|
2020-12-01 07:55:16 +01:00
|
|
|
socklen_t tolen = msg->msg_namelen;
|
2021-09-15 17:22:18 +02:00
|
|
|
size_t len = msg->msg_iovlen;
|
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2023-02-07 10:01:31 +01:00
|
|
|
int count = 0;
|
2021-09-15 17:22:18 +02:00
|
|
|
|
|
|
|
if (msg->msg_control &&
|
|
|
|
msg->msg_controllen > sizeof(struct cmsghdr))
|
|
|
|
{
|
|
|
|
count = local_sendctl(conn, msg);
|
|
|
|
if (count < 0)
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
|
|
|
|
|
|
|
len = to ? local_sendto(psock, buf, len, flags, to, tolen) :
|
|
|
|
local_send(psock, buf, len, flags);
|
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
if (len < 0 && count > 0)
|
|
|
|
{
|
|
|
|
net_lock();
|
2024-02-28 07:05:59 +01:00
|
|
|
local_freectl(conn, count);
|
2021-09-15 17:22:18 +02:00
|
|
|
net_unlock();
|
|
|
|
}
|
|
|
|
#endif
|
2020-12-01 07:55:16 +01:00
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
return len;
|
2015-01-27 00:28:36 +01:00
|
|
|
}
|