mirror of
https://github.com/apache/nuttx.git
synced 2026-06-11 05:03:47 +08:00
9866f01908
MIRTOS-495 Return correct length now Signed-off-by: Peter Bee <bijunda1@xiaomi.com> Change-Id: I32e3bfc44d8aabd3c19963486cb43962a38f927f
320 lines
9.4 KiB
C
320 lines
9.4 KiB
C
/****************************************************************************
|
|
* net/local/local_sendmsg.c
|
|
*
|
|
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in
|
|
* the documentation and/or other materials provided with the
|
|
* distribution.
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
* used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <assert.h>
|
|
#include <debug.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include "socket/socket.h"
|
|
#include "local/local.h"
|
|
|
|
/****************************************************************************
|
|
* Private Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* 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 void *buf,
|
|
size_t len, int flags)
|
|
{
|
|
FAR struct local_conn_s *peer;
|
|
ssize_t ret;
|
|
|
|
switch (psock->s_type)
|
|
{
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
|
case SOCK_STREAM:
|
|
{
|
|
/* 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)
|
|
{
|
|
nerr("ERROR: not connected\n");
|
|
return -ENOTCONN;
|
|
}
|
|
|
|
/* Send the packet */
|
|
|
|
ret = local_send_packet(&peer->lc_outfile, buf, len);
|
|
}
|
|
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
|
|
*
|
|
* Description:
|
|
* This function implements the Unix domain-specific logic of the
|
|
* standard sendto() socket operation.
|
|
*
|
|
* Input Parameters:
|
|
* 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
|
|
*
|
|
* NOTE: All input parameters were verified by sendto() before this
|
|
* function was called.
|
|
*
|
|
* Returned Value:
|
|
* On success, returns the number of characters sent. On error,
|
|
* a negated errno value is returned. See the description in
|
|
* net/socket/sendto.c for the list of appropriate return value.
|
|
*
|
|
****************************************************************************/
|
|
|
|
static ssize_t local_sendto(FAR struct socket *psock, FAR const void *buf,
|
|
size_t len, int flags,
|
|
FAR const struct sockaddr *to, socklen_t tolen)
|
|
{
|
|
FAR struct local_conn_s *conn = (FAR struct local_conn_s *)psock->s_conn;
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)to;
|
|
ssize_t nsent;
|
|
int ret;
|
|
|
|
/* 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;
|
|
}
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
/* If this is a connected socket, then return EISCONN */
|
|
|
|
if (psock->s_type != SOCK_DGRAM)
|
|
{
|
|
nerr("ERROR: Connected socket\n");
|
|
return -EISCONN;
|
|
}
|
|
|
|
/* Now handle the local UDP sendto() operation */
|
|
|
|
/* We keep packet sizes in a uint16_t, so there is a upper limit to the
|
|
* 'len' that can be supported.
|
|
*/
|
|
|
|
DEBUGASSERT(buf && len <= UINT16_MAX);
|
|
|
|
/* 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.
|
|
*/
|
|
|
|
if (conn->lc_state != LOCAL_STATE_UNBOUND &&
|
|
conn->lc_state != LOCAL_STATE_BOUND)
|
|
{
|
|
/* Either not bound to address or it is connected */
|
|
|
|
nerr("ERROR: Connected state\n");
|
|
return -EISCONN;
|
|
}
|
|
|
|
/* The outgoing FIFO should not be open */
|
|
|
|
DEBUGASSERT(conn->lc_outfile.f_inode == 0);
|
|
|
|
/* At present, only standard pathname type address are support */
|
|
|
|
if (tolen < sizeof(sa_family_t) + 2)
|
|
{
|
|
/* EFAULT - An invalid user space address was specified for
|
|
* a parameter
|
|
*/
|
|
|
|
return -EFAULT;
|
|
}
|
|
|
|
/* Make sure that half duplex FIFO has been created.
|
|
* REVISIT: Or should be just make sure that it already exists?
|
|
*/
|
|
|
|
ret = local_create_halfduplex(conn, unaddr->sun_path);
|
|
if (ret < 0)
|
|
{
|
|
nerr("ERROR: Failed to create FIFO for %s: %d\n",
|
|
conn->lc_path, ret);
|
|
return ret;
|
|
}
|
|
|
|
/* Open the sending side of the transfer */
|
|
|
|
ret = local_open_sender(conn, unaddr->sun_path,
|
|
_SS_ISNONBLOCK(psock->s_flags) ||
|
|
(flags & MSG_DONTWAIT) != 0);
|
|
if (ret < 0)
|
|
{
|
|
nerr("ERROR: Failed to open FIFO for %s: %d\n",
|
|
unaddr->sun_path, ret);
|
|
|
|
nsent = ret;
|
|
goto errout_with_halfduplex;
|
|
}
|
|
|
|
/* Send the packet */
|
|
|
|
nsent = local_send_packet(&conn->lc_outfile, buf, len);
|
|
if (nsent < 0)
|
|
{
|
|
nerr("ERROR: Failed to send the packet: %d\n", ret);
|
|
}
|
|
else
|
|
{
|
|
/* local_send_packet returns 0 if all 'len' bytes were sent */
|
|
|
|
nsent = len;
|
|
}
|
|
|
|
/* Now we can close the write-only socket descriptor */
|
|
|
|
file_close(&conn->lc_outfile);
|
|
conn->lc_outfile.f_inode = NULL;
|
|
|
|
errout_with_halfduplex:
|
|
|
|
/* Release our reference to the half duplex FIFO */
|
|
|
|
local_release_halfduplex(conn);
|
|
|
|
#else
|
|
nsent = -EISCONN;
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
|
|
|
return nsent;
|
|
}
|
|
|
|
/****************************************************************************
|
|
* 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 void *buf = msg->msg_iov;
|
|
size_t len = msg->msg_iovlen;
|
|
FAR 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);
|
|
}
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|