2015-01-25 23:27:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_accept.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.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>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.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 */
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_wait(&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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/* There is an accept conn waiting to be processed */
|
2015-05-12 15:41:12 +02:00
|
|
|
|
|
|
|
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,
|
2023-03-06 18:38:39 +01:00
|
|
|
FAR socklen_t *addrlen, FAR struct socket *newsock,
|
|
|
|
int flags)
|
2015-01-25 23:27:25 +01:00
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
FAR struct local_conn_s *server = psock->s_conn;
|
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;
|
2023-03-06 18:38:39 +01:00
|
|
|
bool nonblock = !!(flags & SOCK_NONBLOCK);
|
2023-12-19 10:37:08 +01:00
|
|
|
int ret = OK;
|
2015-01-25 23:27:25 +01:00
|
|
|
|
|
|
|
/* Some sanity checks */
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
if (server->lc_proto != SOCK_STREAM ||
|
2023-06-07 06:11:44 +02:00
|
|
|
server->lc_state != LOCAL_STATE_LISTENING)
|
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
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
/* Are there pending connections. Remove the accpet from the
|
2015-01-26 00:53:01 +01:00
|
|
|
* 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);
|
|
|
|
if (waiter)
|
2015-01-26 00:53:01 +01:00
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
conn = container_of(waiter, struct local_conn_s,
|
|
|
|
u.accept.lc_waiter);
|
2023-09-22 07:33:42 +02:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/* Decrement the number of pending accpets */
|
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
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/* Setup the accpet socket structure */
|
2015-01-25 23:27:25 +01:00
|
|
|
|
2023-12-19 10:37:08 +01: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-27 00:28:36 +01:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/* Return the address family */
|
2015-01-27 00:28:36 +01:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
if (addr != NULL)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
ret = local_getaddr(conn, addr, addrlen);
|
2015-01-27 18:51:24 +01:00
|
|
|
}
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
if (ret == OK && nonblock)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
ret = local_set_nonblocking(conn);
|
2021-08-13 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|