2015-01-25 23:27:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_accept.c
|
|
|
|
*
|
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-25 23:27:25 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-25 23:27:25 +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-25 23:27:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2015-01-31 14:58:51 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL_STREAM)
|
2015-01-25 23:27:25 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <queue.h>
|
2015-01-26 20:30:27 +01:00
|
|
|
#include <debug.h>
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2021-09-16 07:40:02 +02:00
|
|
|
#include <nuttx/nuttx.h>
|
2015-01-25 23:27:25 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "local/local.h"
|
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: local_waitlisten
|
2015-05-12 15:41:12 +02: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 */
|
|
|
|
|
2019-03-19 16:54:22 +01:00
|
|
|
ret = net_lockedwait(&server->lc_waitsem);
|
2015-05-12 15:41:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2015-05-12 15:41:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There is a client waiting for the connection */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-01-25 23:27:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-13 19:15:00 +02:00
|
|
|
* Name: local_accept
|
2015-01-25 23:27:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements accept() for Unix domain sockets. See the
|
|
|
|
* description of accept() for further information.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-01-25 23:27:25 +01:00
|
|
|
* psock The listening Unix domain socket structure
|
|
|
|
* addr Receives the address of the connecting client
|
2021-02-19 15:01:46 +01:00
|
|
|
* addrlen Input: allocated size of 'addr',
|
|
|
|
* Return: returned size of 'addr'
|
2015-01-25 23:27:25 +01:00
|
|
|
* 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 19:15:00 +02:00
|
|
|
int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
|
2017-07-31 15:36:54 +02:00
|
|
|
FAR socklen_t *addrlen, FAR struct socket *newsock)
|
2015-01-25 23:27:25 +01:00
|
|
|
{
|
|
|
|
FAR struct local_conn_s *server;
|
|
|
|
FAR struct local_conn_s *client;
|
2015-01-26 20:30:27 +01:00
|
|
|
FAR struct local_conn_s *conn;
|
2021-09-16 07:40:02 +02:00
|
|
|
FAR dq_entry_t *waiter;
|
2015-01-26 00:53:01 +01:00
|
|
|
int ret;
|
2015-01-25 23:27:25 +01:00
|
|
|
|
|
|
|
/* Some sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(psock && psock->s_conn);
|
2017-07-31 15:36:54 +02:00
|
|
|
DEBUGASSERT(newsock && !newsock->s_conn);
|
2017-07-13 19:15:00 +02: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 23:27:25 +01:00
|
|
|
server = (FAR struct local_conn_s *)psock->s_conn;
|
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
if (server->lc_proto != SOCK_STREAM ||
|
2015-01-25 23:27:25 +01:00
|
|
|
server->lc_state != LOCAL_STATE_LISTENING ||
|
2015-01-27 23:39:30 +01:00
|
|
|
server->lc_type != LOCAL_TYPE_PATHNAME)
|
2015-01-25 23:27:25 +01:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2015-01-26 00:53:01 +01:00
|
|
|
/* Loop as necessary if we have to wait for a connection */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
for (; ; )
|
2015-01-26 00:53:01 +01:00
|
|
|
{
|
|
|
|
/* Are there pending connections. Remove the client from the
|
|
|
|
* head of the waiting list.
|
|
|
|
*/
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2021-09-16 07:40:02 +02:00
|
|
|
waiter = dq_remfirst(&server->u.server.lc_waiters);
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2021-09-16 07:40:02 +02:00
|
|
|
if (waiter)
|
2015-01-26 00:53:01 +01:00
|
|
|
{
|
2021-09-16 07:40:02 +02:00
|
|
|
client = container_of(waiter, struct local_conn_s,
|
|
|
|
u.client.lc_waiter);
|
|
|
|
|
2015-01-26 00:53:01 +01:00
|
|
|
/* Decrement the number of pending clients */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 00:53:01 +01:00
|
|
|
DEBUGASSERT(server->u.server.lc_pending > 0);
|
|
|
|
server->u.server.lc_pending--;
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Create a new connection structure for the server side of the
|
|
|
|
* connection.
|
|
|
|
*/
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
conn = local_alloc();
|
|
|
|
if (!conn)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to allocate new connection structure\n");
|
2015-01-26 20:30:27 +01:00
|
|
|
ret = -ENOMEM;
|
|
|
|
}
|
|
|
|
else
|
2015-01-26 00:53:01 +01:00
|
|
|
{
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Initialize the new connection structure */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
conn->lc_crefs = 1;
|
2015-01-27 23:39:30 +01:00
|
|
|
conn->lc_proto = SOCK_STREAM;
|
2015-01-26 20:30:27 +01:00
|
|
|
conn->lc_type = LOCAL_TYPE_PATHNAME;
|
2015-01-27 23:39:30 +01:00
|
|
|
conn->lc_state = LOCAL_STATE_CONNECTED;
|
2021-08-13 14:53:38 +02:00
|
|
|
conn->lc_psock = psock;
|
2021-09-15 17:22:18 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
conn->lc_peer = client;
|
2021-10-15 12:36:30 +02:00
|
|
|
client->lc_peer = conn;
|
2021-09-15 17:22:18 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
strncpy(conn->lc_path, client->lc_path, UNIX_PATH_MAX - 1);
|
|
|
|
conn->lc_path[UNIX_PATH_MAX - 1] = '\0';
|
2015-05-12 15:47:32 +02:00
|
|
|
conn->lc_instance_id = client->lc_instance_id;
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Open the server-side write-only FIFO. This should not
|
|
|
|
* block.
|
2015-01-26 00:53:01 +01:00
|
|
|
*/
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
ret = local_open_server_tx(
|
|
|
|
conn, _SS_ISNONBLOCK(conn->lc_conn.s_flags));
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-02-19 15:01:46 +01:00
|
|
|
nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
|
|
|
|
conn->lc_path, ret);
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 00:28:36 +01:00
|
|
|
/* Do we have a connection? Is the write-side FIFO opened? */
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-11-02 15:23:38 +01:00
|
|
|
DEBUGASSERT(conn->lc_outfile.f_inode != NULL);
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Open the server-side read-only FIFO. This should not
|
|
|
|
* block because the client side has already opening it
|
|
|
|
* for writing.
|
2015-01-26 00:53:01 +01:00
|
|
|
*/
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
ret = local_open_server_rx(
|
|
|
|
conn, _SS_ISNONBLOCK(conn->lc_conn.s_flags));
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
2015-01-26 00:53:01 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
|
2015-01-26 20:30:27 +01:00
|
|
|
conn->lc_path, ret);
|
2015-01-26 00:53:01 +01:00
|
|
|
}
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
2015-01-27 00:28:36 +01:00
|
|
|
/* Do we have a connection? Are the FIFOs opened? */
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-11-02 15:23:38 +01:00
|
|
|
DEBUGASSERT(conn->lc_infile.f_inode != NULL);
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Return the address family */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2017-07-13 19:15:00 +02:00
|
|
|
if (addr != NULL)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2015-01-27 18:51:24 +01:00
|
|
|
ret = local_getaddr(client, addr, addrlen);
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
2015-01-27 18:51:24 +01:00
|
|
|
}
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
2017-07-31 15:36:54 +02:00
|
|
|
/* Setup the client socket structure */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2017-07-31 15:36:54 +02: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-26 00:53:01 +01:00
|
|
|
}
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Signal the client with the result of the connection */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
client->u.client.lc_result = ret;
|
2021-08-13 14:53:38 +02:00
|
|
|
if (client->lc_state == LOCAL_STATE_CONNECTING)
|
|
|
|
{
|
|
|
|
client->lc_state = LOCAL_STATE_CONNECTED;
|
|
|
|
_SO_SETERRNO(client->lc_psock, ret);
|
|
|
|
local_event_pollnotify(client, POLLOUT);
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&client->lc_waitsem);
|
2015-01-26 20:30:27 +01:00
|
|
|
return ret;
|
2015-01-26 00:53:01 +01:00
|
|
|
}
|
2015-01-25 23:27:25 +01:00
|
|
|
|
|
|
|
/* No.. then there should be no pending connections */
|
|
|
|
|
|
|
|
DEBUGASSERT(server->u.server.lc_pending == 0);
|
|
|
|
|
|
|
|
/* Was the socket opened non-blocking? */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISNONBLOCK(server->lc_conn.s_flags))
|
2015-01-25 23:27:25 +01:00
|
|
|
{
|
|
|
|
/* Yes.. return EAGAIN */
|
|
|
|
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, listen for a connection and try again. */
|
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
ret = local_waitlisten(server);
|
2015-01-25 23:27:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL_STREAM */
|