2015-01-25 16:27:25 -06:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_accept.c
|
|
|
|
*
|
2017-10-04 15:22:27 -06:00
|
|
|
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
|
2015-01-25 16:27:25 -06:00
|
|
|
* 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>
|
2015-01-31 07:58:51 -06:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL_STREAM)
|
2015-01-25 16:27:25 -06:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <queue.h>
|
2015-01-26 13:30:27 -06:00
|
|
|
#include <debug.h>
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2017-10-08 13:31:03 -06:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-01-25 16:27:25 -06:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "local/local.h"
|
|
|
|
|
2015-05-12 07:41:12 -06:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-21 16:33:14 -06:00
|
|
|
* Name: local_waitlisten
|
2015-05-12 07:41:12 -06:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_waitlisten(FAR struct local_conn_s *server)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Loop until a connection is requested or we receive a signal */
|
|
|
|
|
|
|
|
while (dq_empty(&server->u.server.lc_waiters))
|
|
|
|
{
|
|
|
|
/* No.. wait for a connection or a signal */
|
|
|
|
|
2017-10-04 15:22:27 -06:00
|
|
|
ret = nxsem_wait(&server->lc_waitsem);
|
2015-05-12 07:41:12 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-02-20 18:24:53 +00:00
|
|
|
DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
|
2017-10-04 15:22:27 -06:00
|
|
|
return ret;
|
2015-05-12 07:41:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is a client waiting for the connection */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-01-25 16:27:25 -06:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-13 11:15:00 -06:00
|
|
|
* Name: local_accept
|
2015-01-25 16:27:25 -06:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements accept() for Unix domain sockets. See the
|
|
|
|
* description of accept() for further information.
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2015-01-25 16:27:25 -06:00
|
|
|
* psock The listening Unix domain socket structure
|
|
|
|
* addr Receives the address of the connecting client
|
|
|
|
* addrlen Input: allocated size of 'addr', Return: returned size of 'addr'
|
|
|
|
* newconn The new, accepted Unix domain connection structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns zero (OK) on success or a negated errno value on failure.
|
|
|
|
* See the description of accept of the possible errno values in the
|
|
|
|
* description of accept().
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Network is NOT locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-13 11:15:00 -06:00
|
|
|
int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
|
2017-07-31 07:36:54 -06:00
|
|
|
FAR socklen_t *addrlen, FAR struct socket *newsock)
|
2015-01-25 16:27:25 -06:00
|
|
|
{
|
|
|
|
FAR struct local_conn_s *server;
|
|
|
|
FAR struct local_conn_s *client;
|
2015-01-26 13:30:27 -06:00
|
|
|
FAR struct local_conn_s *conn;
|
2015-01-25 17:53:01 -06:00
|
|
|
int ret;
|
2015-01-25 16:27:25 -06:00
|
|
|
|
|
|
|
/* Some sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(psock && psock->s_conn);
|
2017-07-31 07:36:54 -06:00
|
|
|
DEBUGASSERT(newsock && !newsock->s_conn);
|
2017-07-13 11:15:00 -06:00
|
|
|
|
|
|
|
/* Is the socket a stream? */
|
|
|
|
|
|
|
|
if (psock->s_domain != PF_LOCAL || psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that a valid memory block has been provided to receive the
|
|
|
|
* address
|
|
|
|
*/
|
|
|
|
|
2015-01-25 16:27:25 -06:00
|
|
|
server = (FAR struct local_conn_s *)psock->s_conn;
|
|
|
|
|
2015-01-27 16:39:30 -06:00
|
|
|
if (server->lc_proto != SOCK_STREAM ||
|
2015-01-25 16:27:25 -06:00
|
|
|
server->lc_state != LOCAL_STATE_LISTENING ||
|
2015-01-27 16:39:30 -06:00
|
|
|
server->lc_type != LOCAL_TYPE_PATHNAME)
|
2015-01-25 16:27:25 -06:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2015-01-25 17:53:01 -06:00
|
|
|
/* Loop as necessary if we have to wait for a connection */
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-10-08 15:10:04 -06:00
|
|
|
for (; ; )
|
2015-01-25 17:53:01 -06:00
|
|
|
{
|
|
|
|
/* Are there pending connections. Remove the client from the
|
|
|
|
* head of the waiting list.
|
|
|
|
*/
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-25 17:53:01 -06:00
|
|
|
client = (FAR struct local_conn_s *)
|
|
|
|
dq_remfirst(&server->u.server.lc_waiters);
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-25 17:53:01 -06:00
|
|
|
if (client)
|
|
|
|
{
|
|
|
|
/* Decrement the number of pending clients */
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-25 17:53:01 -06:00
|
|
|
DEBUGASSERT(server->u.server.lc_pending > 0);
|
|
|
|
server->u.server.lc_pending--;
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
/* Create a new connection structure for the server side of the
|
|
|
|
* connection.
|
|
|
|
*/
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
conn = local_alloc();
|
|
|
|
if (!conn)
|
|
|
|
{
|
2016-06-11 15:50:49 -06:00
|
|
|
nerr("ERROR: Failed to allocate new connection structure\n");
|
2015-01-26 13:30:27 -06:00
|
|
|
ret = -ENOMEM;
|
|
|
|
}
|
|
|
|
else
|
2015-01-25 17:53:01 -06:00
|
|
|
{
|
2015-01-26 13:30:27 -06:00
|
|
|
/* Initialize the new connection structure */
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
conn->lc_crefs = 1;
|
2015-01-27 16:39:30 -06:00
|
|
|
conn->lc_proto = SOCK_STREAM;
|
2015-01-26 13:30:27 -06:00
|
|
|
conn->lc_type = LOCAL_TYPE_PATHNAME;
|
2015-01-27 16:39:30 -06:00
|
|
|
conn->lc_state = LOCAL_STATE_CONNECTED;
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2019-03-11 12:48:17 -06:00
|
|
|
strncpy(conn->lc_path, client->lc_path, UNIX_PATH_MAX - 1);
|
|
|
|
conn->lc_path[UNIX_PATH_MAX - 1] = '\0';
|
2015-05-12 07:47:32 -06:00
|
|
|
conn->lc_instance_id = client->lc_instance_id;
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
/* Open the server-side write-only FIFO. This should not
|
|
|
|
* block.
|
2015-01-25 17:53:01 -06:00
|
|
|
*/
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-30 12:43:37 -06:00
|
|
|
ret = local_open_server_tx(conn,
|
|
|
|
_SS_ISNONBLOCK(psock->s_flags));
|
2015-01-26 13:30:27 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 15:50:49 -06:00
|
|
|
nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
|
2015-01-26 13:30:27 -06:00
|
|
|
conn->lc_path, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 17:28:36 -06:00
|
|
|
/* Do we have a connection? Is the write-side FIFO opened? */
|
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-11-02 08:23:38 -06:00
|
|
|
DEBUGASSERT(conn->lc_outfile.f_inode != NULL);
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
/* Open the server-side read-only FIFO. This should not
|
|
|
|
* block because the client side has already opening it
|
|
|
|
* for writing.
|
2015-01-25 17:53:01 -06:00
|
|
|
*/
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-30 12:43:37 -06:00
|
|
|
ret = local_open_server_rx(conn,
|
|
|
|
_SS_ISNONBLOCK(psock->s_flags));
|
2015-01-26 13:30:27 -06:00
|
|
|
if (ret < 0)
|
2015-01-25 17:53:01 -06:00
|
|
|
{
|
2016-06-11 15:50:49 -06:00
|
|
|
nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
|
2015-01-26 13:30:27 -06:00
|
|
|
conn->lc_path, ret);
|
2015-01-25 17:53:01 -06:00
|
|
|
}
|
2015-01-26 13:30:27 -06:00
|
|
|
}
|
|
|
|
|
2015-01-26 17:28:36 -06:00
|
|
|
/* Do we have a connection? Are the FIFOs opened? */
|
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-11-02 08:23:38 -06:00
|
|
|
DEBUGASSERT(conn->lc_infile.f_inode != NULL);
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
/* Return the address family */
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2017-07-13 11:15:00 -06:00
|
|
|
if (addr != NULL)
|
2015-01-26 13:30:27 -06:00
|
|
|
{
|
2015-01-27 11:51:24 -06:00
|
|
|
ret = local_getaddr(client, addr, addrlen);
|
2015-01-26 13:30:27 -06:00
|
|
|
}
|
2015-01-27 11:51:24 -06:00
|
|
|
}
|
2015-01-26 13:30:27 -06:00
|
|
|
|
2015-01-27 11:51:24 -06:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-07-31 07:36:54 -06:00
|
|
|
/* Setup the client socket structure */
|
2015-01-26 13:30:27 -06:00
|
|
|
|
2017-07-31 07:36:54 -06:00
|
|
|
newsock->s_domain = psock->s_domain;
|
|
|
|
newsock->s_type = SOCK_STREAM;
|
|
|
|
newsock->s_sockif = psock->s_sockif;
|
|
|
|
newsock->s_conn = (FAR void *)conn;
|
2015-01-25 17:53:01 -06:00
|
|
|
}
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
/* Signal the client with the result of the connection */
|
2015-01-25 16:27:25 -06:00
|
|
|
|
2015-01-26 13:30:27 -06:00
|
|
|
client->u.client.lc_result = ret;
|
2017-10-03 15:35:24 -06:00
|
|
|
nxsem_post(&client->lc_waitsem);
|
2015-01-26 13:30:27 -06:00
|
|
|
return ret;
|
2015-01-25 17:53:01 -06:00
|
|
|
}
|
2015-01-25 16:27:25 -06:00
|
|
|
|
|
|
|
/* No.. then there should be no pending connections */
|
|
|
|
|
|
|
|
DEBUGASSERT(server->u.server.lc_pending == 0);
|
|
|
|
|
|
|
|
/* Was the socket opened non-blocking? */
|
|
|
|
|
|
|
|
if (_SS_ISNONBLOCK(psock->s_flags))
|
|
|
|
{
|
|
|
|
/* Yes.. return EAGAIN */
|
|
|
|
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, listen for a connection and try again. */
|
|
|
|
|
2015-05-12 07:41:12 -06:00
|
|
|
ret = local_waitlisten(server);
|
2015-01-25 16:27:25 -06:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 07:58:51 -06:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL_STREAM */
|