2007-09-23 18:58:09 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/accept.c
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
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
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-09-23 18:58:09 +02: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.
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2007-11-16 19:48:39 +01:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
#include <unistd.h>
|
2007-09-23 18:58:09 +02:00
|
|
|
#include <errno.h>
|
2010-07-08 03:48:16 +02:00
|
|
|
#include <assert.h>
|
2007-11-16 19:48:39 +01:00
|
|
|
#include <debug.h>
|
2021-02-23 11:04:13 +01:00
|
|
|
#include <fcntl.h>
|
2007-11-16 19:48:39 +01:00
|
|
|
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2021-02-23 11:04:13 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2021-06-11 06:16:24 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-01-28 21:56:06 +01:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-06-29 01:25:18 +02:00
|
|
|
#include "socket/socket.h"
|
2007-09-23 18:58:09 +02:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-09-23 18:58:09 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: psock_accept
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2016-04-14 16:39:48 +02:00
|
|
|
* The psock_accept function is used with connection-based socket types
|
2007-09-23 18:58:09 +02:00
|
|
|
* (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first
|
|
|
|
* connection request on the queue of pending connections, creates a new
|
|
|
|
* connected socket with mostly the same properties as 'sockfd', and
|
|
|
|
* allocates a new socket descriptor for the socket, which is returned. The
|
|
|
|
* newly created socket is no longer in the listening state. The original
|
|
|
|
* socket 'sockfd' is unaffected by this call. Per file descriptor flags
|
2016-04-14 16:39:48 +02:00
|
|
|
* are not inherited across an psock_accept.
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
* The 'sockfd' argument is a socket descriptor that has been created with
|
|
|
|
* socket(), bound to a local address with bind(), and is listening for
|
|
|
|
* connections after a call to listen().
|
|
|
|
*
|
|
|
|
* On return, the 'addr' structure is filled in with the address of the
|
|
|
|
* connecting entity. The 'addrlen' argument initially contains the size
|
|
|
|
* of the structure pointed to by 'addr'; on return it will contain the
|
|
|
|
* actual length of the address returned.
|
|
|
|
*
|
|
|
|
* If no pending connections are present on the queue, and the socket is
|
2016-04-14 16:39:48 +02:00
|
|
|
* not marked as non-blocking, psock_accept blocks the caller until a
|
|
|
|
* connection is present. If the socket is marked non-blocking and no
|
|
|
|
* pending connections are present on the queue, psock_accept returns
|
|
|
|
* EAGAIN.
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-04-14 16:39:48 +02:00
|
|
|
* psock Reference to the listening socket structure
|
2007-09-23 18:58:09 +02:00
|
|
|
* 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'
|
2016-04-14 16:39:48 +02:00
|
|
|
* newsock Location to return the accepted socket information.
|
2022-11-18 11:16:19 +01:00
|
|
|
* flags The flags used for initialization
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-09-30 16:18:08 +02:00
|
|
|
* Returns 0 (OK) on success. On failure, it returns a negated errno value
|
|
|
|
* to indicate the nature of the error.
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
* EAGAIN or EWOULDBLOCK
|
|
|
|
* The socket is marked non-blocking and no connections are present to
|
|
|
|
* be accepted.
|
|
|
|
* EOPNOTSUPP
|
|
|
|
* The referenced socket is not of type SOCK_STREAM.
|
|
|
|
* EINTR
|
|
|
|
* The system call was interrupted by a signal that was caught before
|
|
|
|
* a valid connection arrived.
|
|
|
|
* ECONNABORTED
|
|
|
|
* A connection has been aborted.
|
|
|
|
* EINVAL
|
|
|
|
* Socket is not listening for connections.
|
|
|
|
* EMFILE
|
|
|
|
* The per-process limit of open file descriptors has been reached.
|
|
|
|
* ENFILE
|
|
|
|
* The system maximum for file descriptors has been reached.
|
|
|
|
* EFAULT
|
2017-07-13 19:15:00 +02:00
|
|
|
* The addr parameter is not in a writable part of the user address space.
|
2007-09-23 18:58:09 +02:00
|
|
|
* ENOBUFS or ENOMEM
|
|
|
|
* Not enough free memory.
|
|
|
|
* EPROTO
|
|
|
|
* Protocol error.
|
|
|
|
* EPERM
|
|
|
|
* Firewall rules forbid connection.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
|
2022-11-18 11:16:19 +01:00
|
|
|
FAR socklen_t *addrlen, FAR struct socket *newsock,
|
|
|
|
int flags)
|
2007-09-23 18:58:09 +02:00
|
|
|
{
|
2022-02-08 06:18:01 +01:00
|
|
|
FAR struct socket_conn_s *conn;
|
2007-09-23 22:45:30 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-07-13 19:15:00 +02:00
|
|
|
DEBUGASSERT(psock != NULL && psock->s_conn != NULL && newsock != NULL);
|
2015-01-22 13:51:31 +01:00
|
|
|
|
2017-07-13 19:15:00 +02:00
|
|
|
/* May sure that the socket has been opened with socket() */
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2017-07-13 19:15:00 +02:00
|
|
|
if (psock == NULL || psock->s_conn == NULL)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
2017-07-13 19:15:00 +02:00
|
|
|
nerr("ERROR: Socket invalid or not opened\n");
|
2017-09-30 16:18:08 +02:00
|
|
|
return -EINVAL;
|
2007-09-23 22:45:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the socket listening for a connection? */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
conn = psock->s_conn;
|
|
|
|
if (!_SS_ISLISTENING(conn->s_flags))
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
2017-08-31 18:49:10 +02:00
|
|
|
nerr("ERROR: Socket is not listening for a connection.\n");
|
2017-09-30 16:18:08 +02:00
|
|
|
return -EINVAL;
|
2007-09-23 22:45:30 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 19:15:00 +02:00
|
|
|
/* Let the address family's accept() method handle the operation */
|
2009-08-03 01:35:27 +02:00
|
|
|
|
2023-03-06 06:16:26 +01:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL);
|
|
|
|
if (psock->s_sockif->si_accept == NULL)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2017-08-31 15:23:19 +02:00
|
|
|
|
|
|
|
net_lock();
|
2023-03-06 18:38:39 +01:00
|
|
|
ret = psock->s_sockif->si_accept(psock, addr, addrlen, newsock, flags);
|
2022-02-08 06:18:01 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Mark the new socket as connected. */
|
|
|
|
|
|
|
|
conn = newsock->s_conn;
|
|
|
|
conn->s_flags |= _SF_CONNECTED;
|
|
|
|
conn->s_flags &= ~_SF_CLOSED;
|
2022-11-18 11:16:19 +01:00
|
|
|
if (flags & SOCK_NONBLOCK)
|
|
|
|
{
|
|
|
|
conn->s_flags |= _SF_NONBLOCK;
|
|
|
|
}
|
2022-02-08 06:18:01 +01:00
|
|
|
}
|
|
|
|
else
|
2015-01-25 22:46:05 +01:00
|
|
|
{
|
2017-07-13 19:15:00 +02:00
|
|
|
nerr("ERROR: si_accept failed: %d\n", ret);
|
2007-09-23 22:45:30 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 18:49:10 +02:00
|
|
|
net_unlock();
|
2017-09-30 16:18:08 +02:00
|
|
|
return ret;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|