2015-01-27 07:28:36 +08:00
|
|
|
/****************************************************************************
|
2020-12-01 14:55:16 +08:00
|
|
|
* net/local/local_sendmsg.c
|
2015-01-27 07:28:36 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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 07:28:36 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-27 07:28:36 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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 07:28:36 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2020-12-01 14:55:16 +08:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
2015-01-27 07:28:36 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-01-30 00:40:10 +08:00
|
|
|
#include <unistd.h>
|
2015-01-27 07:28:36 +08:00
|
|
|
#include <errno.h>
|
2015-01-30 00:40:10 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2015-01-27 07:28:36 +08:00
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2015-01-31 02:43:37 +08:00
|
|
|
#include "socket/socket.h"
|
2015-01-27 07:28:36 +08:00
|
|
|
#include "local/local.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 14:55:16 +08:00
|
|
|
* Private Functions
|
2015-01-27 07:28:36 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 14:55:16 +08: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:
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *peer;
|
|
|
|
|
|
|
|
/* Local TCP packet send */
|
|
|
|
|
|
|
|
DEBUGASSERT(psock && psock->s_conn && buf);
|
|
|
|
peer = (FAR struct local_conn_s *)psock->s_conn;
|
|
|
|
|
|
|
|
/* Verify that this is a connected peer socket and that it has
|
|
|
|
* opened the outgoing FIFO for write-only access.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (peer->lc_state != LOCAL_STATE_CONNECTED ||
|
|
|
|
peer->lc_outfile.f_inode == NULL)
|
|
|
|
{
|
2021-08-13 20:53:38 +08:00
|
|
|
if (peer->lc_state == LOCAL_STATE_CONNECTING)
|
|
|
|
{
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
2020-12-01 14:55:16 +08:00
|
|
|
nerr("ERROR: not connected\n");
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the packet */
|
|
|
|
|
2021-07-21 15:41:49 +08:00
|
|
|
ret = local_send_packet(&peer->lc_outfile, buf, len, false);
|
2020-12-01 14:55:16 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
{
|
|
|
|
/* Local UDP packet send */
|
|
|
|
|
|
|
|
#warning Missing logic
|
|
|
|
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
|
|
|
|
|
|
|
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 07:28:36 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2015-01-29 03:41:24 +08:00
|
|
|
* This function implements the Unix domain-specific logic of the
|
|
|
|
* standard sendto() socket operation.
|
2015-01-27 07:28:36 +08:00
|
|
|
*
|
2015-01-29 03:41:24 +08:00
|
|
|
* Input Parameters:
|
2015-01-27 07:28:36 +08: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-29 03:41:24 +08:00
|
|
|
* NOTE: All input parameters were verified by sendto() before this
|
|
|
|
* function was called.
|
|
|
|
*
|
2015-01-27 07:28:36 +08:00
|
|
|
* Returned Value:
|
|
|
|
* On success, returns the number of characters sent. On error,
|
2015-01-29 03:41:24 +08: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 07:28:36 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 14:55:16 +08: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 07:28:36 +08:00
|
|
|
{
|
2020-12-01 14:55:16 +08:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2015-01-30 00:40:10 +08:00
|
|
|
FAR struct local_conn_s *conn = (FAR struct local_conn_s *)psock->s_conn;
|
|
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)to;
|
2021-03-31 18:21:02 +08:00
|
|
|
ssize_t ret;
|
2015-01-30 00:40:10 +08:00
|
|
|
|
2020-12-01 14:55:16 +08: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-30 00:40:10 +08:00
|
|
|
|
2020-12-01 14:55:16 +08: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-30 00:40:10 +08:00
|
|
|
|
2015-01-30 01:05:04 +08: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-30 00:40:10 +08:00
|
|
|
|
2015-01-30 01:05:04 +08:00
|
|
|
if (conn->lc_state != LOCAL_STATE_UNBOUND &&
|
|
|
|
conn->lc_state != LOCAL_STATE_BOUND)
|
2015-01-30 00:40:10 +08:00
|
|
|
{
|
|
|
|
/* Either not bound to address or it is connected */
|
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
nerr("ERROR: Connected state\n");
|
2015-01-30 00:40:10 +08:00
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The outgoing FIFO should not be open */
|
|
|
|
|
2017-11-02 22:23:38 +08:00
|
|
|
DEBUGASSERT(conn->lc_outfile.f_inode == 0);
|
2015-01-30 00:40:10 +08:00
|
|
|
|
|
|
|
/* At present, only standard pathname type address are support */
|
|
|
|
|
|
|
|
if (tolen < sizeof(sa_family_t) + 2)
|
|
|
|
{
|
2021-02-19 22:01:46 +08:00
|
|
|
/* EFAULT
|
|
|
|
* - An invalid user space address was specified for a parameter
|
|
|
|
*/
|
2015-01-30 00:40:10 +08:00
|
|
|
|
2019-07-03 08:02:23 +08:00
|
|
|
return -EFAULT;
|
2015-01-30 00:40:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that half duplex FIFO has been created.
|
|
|
|
* REVISIT: Or should be just make sure that it already exists?
|
|
|
|
*/
|
|
|
|
|
2015-01-30 01:05:04 +08:00
|
|
|
ret = local_create_halfduplex(conn, unaddr->sun_path);
|
2015-01-30 00:40:10 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-31 18:21:02 +08:00
|
|
|
nerr("ERROR: Failed to create FIFO for %s: %zd\n",
|
2015-01-30 00:40:10 +08:00
|
|
|
conn->lc_path, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the sending side of the transfer */
|
|
|
|
|
2015-01-31 02:43:37 +08:00
|
|
|
ret = local_open_sender(conn, unaddr->sun_path,
|
2020-02-20 02:21:28 +08:00
|
|
|
_SS_ISNONBLOCK(psock->s_flags) ||
|
|
|
|
(flags & MSG_DONTWAIT) != 0);
|
2015-01-30 00:40:10 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-31 18:21:02 +08:00
|
|
|
nerr("ERROR: Failed to open FIFO for %s: %zd\n",
|
2015-01-30 00:40:10 +08:00
|
|
|
unaddr->sun_path, ret);
|
2015-01-30 23:28:55 +08:00
|
|
|
|
|
|
|
goto errout_with_halfduplex;
|
2015-01-30 00:40:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the packet */
|
|
|
|
|
2021-07-21 15:41:49 +08:00
|
|
|
ret = local_send_packet(&conn->lc_outfile, buf, len, true);
|
2021-03-31 18:21:02 +08:00
|
|
|
if (ret < 0)
|
2015-01-30 00:40:10 +08:00
|
|
|
{
|
2021-03-31 18:21:02 +08:00
|
|
|
nerr("ERROR: Failed to send the packet: %zd\n", ret);
|
2015-01-30 01:05:04 +08:00
|
|
|
}
|
2015-01-30 00:40:10 +08:00
|
|
|
|
|
|
|
/* Now we can close the write-only socket descriptor */
|
|
|
|
|
2018-09-16 03:04:56 +08:00
|
|
|
file_close(&conn->lc_outfile);
|
2017-11-02 22:23:38 +08:00
|
|
|
conn->lc_outfile.f_inode = NULL;
|
2015-01-30 00:40:10 +08:00
|
|
|
|
2015-01-30 23:28:55 +08:00
|
|
|
errout_with_halfduplex:
|
2019-10-26 01:31:42 +08:00
|
|
|
|
2015-10-09 05:10:04 +08:00
|
|
|
/* Release our reference to the half duplex FIFO */
|
2015-01-30 23:28:55 +08:00
|
|
|
|
2020-01-03 00:49:34 +08:00
|
|
|
local_release_halfduplex(conn);
|
2021-03-31 18:21:02 +08:00
|
|
|
|
|
|
|
return ret;
|
2020-12-01 14:55:16 +08: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 iovec *buf = msg->msg_iov;
|
|
|
|
size_t len = msg->msg_iovlen;
|
|
|
|
FAR const struct sockaddr *to = msg->msg_name;
|
|
|
|
socklen_t tolen = msg->msg_namelen;
|
|
|
|
|
|
|
|
return to ? local_sendto(psock, buf, len, flags, to, tolen) :
|
|
|
|
local_send(psock, buf, len, flags);
|
2015-01-27 07:28:36 +08:00
|
|
|
}
|
|
|
|
|
2020-12-01 14:55:16 +08:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|