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
|
|
|
*
|
2016-12-09 22:17:58 +01:00
|
|
|
* Copyright (C) 2007-2012, 2015-2016 Gregory Nutt. All rights reserved.
|
2012-02-18 19:13:30 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
* 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-28 00:31:42 +01:00
|
|
|
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 && \
|
2015-01-31 14:58:51 +01:00
|
|
|
(defined(CONFIG_NET_TCP) || defined(CONFIG_NET_LOCAL_STREAM))
|
2007-09-23 18:58:09 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2007-11-16 19:48:39 +01:00
|
|
|
|
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>
|
|
|
|
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2015-01-28 21:56:06 +01:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-07-06 20:34:27 +02:00
|
|
|
#include "tcp/tcp.h"
|
2015-01-25 22:46:05 +01:00
|
|
|
#include "local/local.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
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-04-14 16:39:48 +02:00
|
|
|
* Function: 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
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* 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.
|
2007-09-23 18:58:09 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2016-04-14 16:39:48 +02:00
|
|
|
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with the
|
|
|
|
* errno variable set 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
|
|
|
|
* The addr parameter is not in a writable part of the user address
|
|
|
|
* space.
|
|
|
|
* 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,
|
|
|
|
FAR socklen_t *addrlen, FAR struct socket *newsock)
|
2007-09-23 18:58:09 +02:00
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2007-09-23 22:45:30 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
DEBUGASSERT(psock != NULL);
|
2015-01-22 13:51:31 +01:00
|
|
|
|
2016-12-09 22:17:58 +01:00
|
|
|
/* Treat as a cancellation point */
|
|
|
|
|
2016-12-10 15:36:58 +01:00
|
|
|
(void)enter_cancellation_point();
|
2016-12-09 22:17:58 +01:00
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
/* Is the socket a stream? */
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
if (psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EOPNOTSUPP;
|
2007-09-23 22:45:30 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the socket listening for a connection? */
|
|
|
|
|
|
|
|
if (!_SS_ISLISTENING(psock->s_flags))
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EINVAL;
|
2007-09-23 22:45:30 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
/* Verify that a valid memory block has been provided to receive the
|
|
|
|
* address
|
2008-11-20 20:24:06 +01:00
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 22:50:55 +01:00
|
|
|
if (addr)
|
|
|
|
{
|
2015-01-29 23:30:47 +01:00
|
|
|
/* If an address is provided, then the length must also be provided. */
|
2015-01-25 22:46:05 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(addrlen);
|
|
|
|
|
|
|
|
/* A valid length depends on the address domain */
|
|
|
|
|
2015-01-17 16:27:05 +01:00
|
|
|
switch (psock->s_domain)
|
2008-11-20 22:50:55 +01:00
|
|
|
{
|
2015-01-17 16:27:05 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
case PF_INET:
|
|
|
|
{
|
|
|
|
if (*addrlen < sizeof(struct sockaddr_in))
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2015-01-17 16:27:05 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-01-18 16:23:22 +01:00
|
|
|
case PF_INET6:
|
2015-01-17 16:27:05 +01:00
|
|
|
{
|
|
|
|
if (*addrlen < sizeof(struct sockaddr_in6))
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2015-01-17 16:27:05 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-25 22:46:05 +01:00
|
|
|
case PF_LOCAL:
|
|
|
|
{
|
|
|
|
if (*addrlen < sizeof(sa_family_t))
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2015-01-25 22:46:05 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
2015-01-17 16:27:05 +01:00
|
|
|
default:
|
|
|
|
DEBUGPANIC();
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EINVAL;
|
2008-11-20 22:50:55 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2015-01-30 12:40:28 +01:00
|
|
|
/* Initialize the socket structure. */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
newsock->s_domain = psock->s_domain;
|
|
|
|
newsock->s_type = SOCK_STREAM;
|
2009-08-03 01:35:27 +02:00
|
|
|
|
2015-01-25 22:46:05 +01:00
|
|
|
/* Perform the correct accept operation for this address domain */
|
2009-08-03 01:35:27 +02:00
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-25 22:46:05 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
|
|
|
if (psock->s_domain == PF_LOCAL)
|
2008-11-20 20:24:06 +01:00
|
|
|
#endif
|
|
|
|
{
|
2015-01-25 23:27:25 +01:00
|
|
|
/* Perform the local accept operation (with the network unlocked) */
|
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
ret = psock_local_accept(psock, addr, addrlen, &newsock->s_conn);
|
2015-01-22 13:51:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = -ret;
|
2016-04-14 16:39:48 +02:00
|
|
|
goto errout;
|
2015-01-22 13:51:31 +01:00
|
|
|
}
|
2015-01-25 22:46:05 +01:00
|
|
|
}
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2015-01-25 22:46:05 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-25 22:46:05 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-01-25 23:27:25 +01:00
|
|
|
/* Perform the local accept operation (with the network locked) */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2016-04-14 16:39:48 +02:00
|
|
|
ret = psock_tcp_accept(psock, addr, addrlen, &newsock->s_conn);
|
2015-01-25 22:46:05 +01:00
|
|
|
if (ret < 0)
|
2008-11-20 20:24:06 +01:00
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = -ret;
|
2016-04-14 16:39:48 +02:00
|
|
|
goto errout;
|
2008-11-20 20:24:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-25 22:46:05 +01:00
|
|
|
/* Begin monitoring for TCP connection events on the newly connected
|
|
|
|
* socket
|
2008-11-20 20:24:06 +01:00
|
|
|
*/
|
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
ret = net_startmonitor(newsock);
|
2015-05-28 16:23:51 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* net_startmonitor() can only fail on certain race conditions
|
|
|
|
* where the connection was lost just before this function was
|
|
|
|
* called. Undo everything we have done and return a failure.
|
|
|
|
*/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = -ret;
|
2015-05-28 16:23:51 +02:00
|
|
|
goto errout_after_accept;
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2007-09-23 22:45:30 +02:00
|
|
|
}
|
2015-01-25 22:46:05 +01:00
|
|
|
#endif /* CONFIG_NET_TCP */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2015-01-30 12:40:28 +01:00
|
|
|
/* Mark the new socket as connected. */
|
2015-01-29 23:30:47 +01:00
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
newsock->s_flags |= _SF_CONNECTED;
|
|
|
|
newsock->s_flags &= ~_SF_CLOSED;
|
2016-12-09 22:17:58 +01:00
|
|
|
|
|
|
|
leave_cancellation_point();
|
2016-04-14 16:39:48 +02:00
|
|
|
return OK;
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2015-05-28 16:23:51 +02:00
|
|
|
errout_after_accept:
|
2016-04-14 16:39:48 +02:00
|
|
|
psock_close(newsock);
|
|
|
|
|
|
|
|
errout:
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2016-12-09 22:17:58 +01:00
|
|
|
leave_cancellation_point();
|
2016-04-14 16:39:48 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: accept
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The accept function is used with connection-based socket types
|
|
|
|
* (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
|
|
|
|
* are not inherited across an accept.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* not marked as non-blocking, 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, accept returns EAGAIN.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* sockfd The listening socket descriptor
|
|
|
|
* addr Receives the address of the connecting client
|
|
|
|
* addrlen Input: allocated size of 'addr', Return: returned size of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns -1 on error. If it succeeds, it returns a non-negative integer
|
|
|
|
* that is a descriptor for the accepted socket.
|
|
|
|
*
|
|
|
|
* EAGAIN or EWOULDBLOCK
|
|
|
|
* The socket is marked non-blocking and no connections are present to
|
|
|
|
* be accepted.
|
|
|
|
* EBADF
|
|
|
|
* The descriptor is invalid.
|
|
|
|
* ENOTSOCK
|
|
|
|
* The descriptor references a file, not a socket.
|
|
|
|
* 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
|
|
|
|
* The addr parameter is not in a writable part of the user address
|
|
|
|
* space.
|
|
|
|
* ENOBUFS or ENOMEM
|
|
|
|
* Not enough free memory.
|
|
|
|
* EPROTO
|
|
|
|
* Protocol error.
|
|
|
|
* EPERM
|
|
|
|
* Firewall rules forbid connection.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
|
|
|
FAR struct socket *newsock;
|
|
|
|
int newfd;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2016-04-14 16:39:48 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-12-09 22:17:58 +01:00
|
|
|
/* accept() is a cancellation point */
|
|
|
|
|
2016-12-10 15:36:58 +01:00
|
|
|
(void)enter_cancellation_point();
|
2016-12-09 22:17:58 +01:00
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (psock == NULL || psock->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
/* It is not a valid socket description. Distinguish between the cases
|
|
|
|
* where sockfd is a just valid and when it is a valid file descriptor used
|
|
|
|
* in the wrong context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
if ((unsigned int)sockfd < CONFIG_NFILE_DESCRIPTORS)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOTSOCK;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a socket descriptor for the new connection now (so that it
|
|
|
|
* cannot fail later)
|
|
|
|
*/
|
|
|
|
|
|
|
|
newfd = sockfd_allocate(0);
|
|
|
|
if (newfd < 0)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENFILE;
|
2016-04-14 16:39:48 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
newsock = sockfd_socket(newfd);
|
|
|
|
if (newsock == NULL)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENFILE;
|
2016-04-14 16:39:48 +02:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = psock_accept(psock, addr, addrlen, newsock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* The errno value has already been set */
|
|
|
|
|
|
|
|
sockfd_release(newfd);
|
2016-12-09 22:17:58 +01:00
|
|
|
leave_cancellation_point();
|
2016-04-14 16:39:48 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-09 22:17:58 +01:00
|
|
|
leave_cancellation_point();
|
2016-04-14 16:39:48 +02:00
|
|
|
return newfd;
|
2015-05-28 16:23:51 +02:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
errout_with_socket:
|
|
|
|
sockfd_release(newfd);
|
|
|
|
|
|
|
|
errout:
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2016-12-09 22:17:58 +01:00
|
|
|
leave_cancellation_point();
|
2007-09-23 18:58:09 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS && (CONFIG_NET_TCP || CONFIG_NET_LOCAL_STREAM) */
|