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
|
|
|
*
|
2012-02-18 19:13:30 +01:00
|
|
|
* Copyright (C) 2007-2012 Gregory Nutt. All rights reserved.
|
|
|
|
* 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>
|
2007-11-22 19:36:46 +01:00
|
|
|
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0 && defined(CONFIG_NET_TCP)
|
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 22:45:30 +02:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <string.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>
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
#include <arch/irq.h>
|
2007-09-23 18:58:09 +02:00
|
|
|
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
|
|
|
|
2014-06-29 01:25:18 +02:00
|
|
|
#include "socket/socket.h"
|
2007-09-23 18:58:09 +02:00
|
|
|
|
2007-11-23 20:25:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-23 18:58:09 +02:00
|
|
|
/****************************************************************************
|
2007-09-23 22:45:30 +02:00
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct accept_s
|
|
|
|
{
|
2007-12-02 19:18:59 +01:00
|
|
|
sem_t acpt_sem; /* Wait for interrupt event */
|
2007-09-23 22:45:30 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2007-12-02 19:18:59 +01:00
|
|
|
FAR struct sockaddr_in6 *acpt_addr; /* Return connection adress */
|
2007-09-23 22:45:30 +02:00
|
|
|
#else
|
2007-12-02 19:18:59 +01:00
|
|
|
FAR struct sockaddr_in *acpt_addr; /* Return connection adress */
|
2007-09-23 22:45:30 +02:00
|
|
|
#endif
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *acpt_newconn; /* The accepted connection */
|
2007-12-02 19:18:59 +01:00
|
|
|
int acpt_result; /* The result of the wait */
|
2007-09-23 22:45:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-23 20:25:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: accept_tcpsender
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Getting the sender's address from the UDP packet
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* conn - The newly accepted TCP connection
|
2012-02-22 17:03:10 +01:00
|
|
|
* pstate - the recvfrom state structure
|
2007-11-23 20:25:39 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Running at the interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP
|
2008-11-20 22:50:55 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-25 02:12:49 +02:00
|
|
|
static inline void accept_tcpsender(FAR struct tcp_conn_s *conn,
|
2008-11-20 22:50:55 +01:00
|
|
|
FAR struct sockaddr_in6 *addr)
|
2009-08-02 18:31:50 +02:00
|
|
|
{
|
|
|
|
if (addr)
|
|
|
|
{
|
|
|
|
addr->sin_family = AF_INET6;
|
|
|
|
addr->sin_port = conn->rport;
|
2012-06-08 00:00:19 +02:00
|
|
|
uip_ipaddr_copy(addr->sin6_addr.s6_addr, conn->ripaddr);
|
2009-08-02 18:31:50 +02:00
|
|
|
}
|
|
|
|
}
|
2007-11-23 20:25:39 +01:00
|
|
|
#else
|
2014-06-25 02:12:49 +02:00
|
|
|
static inline void accept_tcpsender(FAR struct tcp_conn_s *conn,
|
2008-11-20 22:50:55 +01:00
|
|
|
FAR struct sockaddr_in *addr)
|
|
|
|
{
|
2007-11-23 20:25:39 +01:00
|
|
|
if (addr)
|
|
|
|
{
|
|
|
|
addr->sin_family = AF_INET;
|
|
|
|
addr->sin_port = conn->rport;
|
|
|
|
uip_ipaddr_copy(addr->sin_addr.s_addr, conn->ripaddr);
|
|
|
|
}
|
|
|
|
}
|
2009-08-02 18:31:50 +02:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
#endif /* CONFIG_NET_TCP */
|
2007-11-23 20:25:39 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Function: accept_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Receive interrupt level callbacks when connections occur
|
|
|
|
*
|
2007-11-08 17:01:18 +01:00
|
|
|
* Parameters:
|
|
|
|
* listener The conection stucture of the listener
|
|
|
|
* conn The connection stucture that was just accepted
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Running at the interrupt level
|
|
|
|
*
|
2007-09-23 22:45:30 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
static int accept_interrupt(FAR struct tcp_conn_s *listener,
|
|
|
|
FAR struct tcp_conn_s *conn)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
2007-11-08 17:01:18 +01:00
|
|
|
struct accept_s *pstate = (struct accept_s *)listener->accept_private;
|
2007-09-23 22:45:30 +02:00
|
|
|
int ret = -EINVAL;
|
2007-11-16 19:48:39 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
if (pstate)
|
|
|
|
{
|
|
|
|
/* Get the connection address */
|
2007-11-23 20:25:39 +01:00
|
|
|
|
2008-11-20 22:50:55 +01:00
|
|
|
accept_tcpsender(conn, pstate->acpt_addr);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
|
|
|
/* Save the connection structure */
|
2007-11-08 17:01:18 +01:00
|
|
|
|
|
|
|
pstate->acpt_newconn = conn;
|
|
|
|
pstate->acpt_result = OK;
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2009-12-18 23:21:20 +01:00
|
|
|
/* There should be a reference of one on the new connection */
|
2009-06-15 20:58:22 +02:00
|
|
|
|
2009-12-18 23:21:20 +01:00
|
|
|
DEBUGASSERT(conn->crefs == 1);
|
2009-06-15 20:58:22 +02:00
|
|
|
|
|
|
|
/* Wake-up the waiting caller thread */
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
sem_post(&pstate->acpt_sem);
|
2007-11-08 17:01:18 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
/* Stop any further callbacks */
|
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
listener->accept_private = NULL;
|
|
|
|
listener->accept = NULL;
|
|
|
|
ret = OK;
|
2007-09-23 22:45:30 +02:00
|
|
|
}
|
2007-11-16 19:48:39 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-09-23 18:58:09 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 descriptior
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
|
|
|
|
{
|
2007-09-23 22:45:30 +02:00
|
|
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
|
|
|
FAR struct socket *pnewsock;
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2007-09-23 22:45:30 +02:00
|
|
|
struct accept_s state;
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2007-12-11 15:27:11 +01:00
|
|
|
FAR struct sockaddr_in6 *inaddr = (struct sockaddr_in6 *)addr;
|
2007-09-23 22:45:30 +02:00
|
|
|
#else
|
2007-12-11 15:27:11 +01:00
|
|
|
FAR struct sockaddr_in *inaddr = (struct sockaddr_in *)addr;
|
2007-09-23 22:45:30 +02:00
|
|
|
#endif
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t save;
|
2007-09-23 22:45:30 +02:00
|
|
|
int newfd;
|
|
|
|
int err;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (!psock || 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)
|
|
|
|
{
|
|
|
|
err = ENOTSOCK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = EBADF;
|
|
|
|
}
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have a socket descriptor, but it is a stream? */
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
if (psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
|
|
|
err = EOPNOTSUPP;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the socket listening for a connection? */
|
|
|
|
|
|
|
|
if (!_SS_ISLISTENING(psock->s_flags))
|
|
|
|
{
|
|
|
|
err = EINVAL;
|
|
|
|
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)
|
|
|
|
{
|
2007-09-23 22:45:30 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2009-08-02 18:31:50 +02:00
|
|
|
if (*addrlen < sizeof(struct sockaddr_in6))
|
2007-09-23 22:45:30 +02:00
|
|
|
#else
|
2009-08-02 18:31:50 +02:00
|
|
|
if (*addrlen < sizeof(struct sockaddr_in))
|
2007-09-23 22:45:30 +02:00
|
|
|
#endif
|
2008-11-20 22:50:55 +01:00
|
|
|
{
|
|
|
|
err = EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
/* Allocate a socket descriptor for the new connection now (so that it
|
|
|
|
* cannot fail later)
|
2008-11-20 20:24:06 +01:00
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2009-07-19 02:14:46 +02:00
|
|
|
newfd = sockfd_allocate(0);
|
2007-09-23 22:45:30 +02:00
|
|
|
if (newfd < 0)
|
|
|
|
{
|
|
|
|
err = ENFILE;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
pnewsock = sockfd_socket(newfd);
|
2007-11-16 19:48:39 +01:00
|
|
|
if (!pnewsock)
|
2007-09-23 22:45:30 +02:00
|
|
|
{
|
|
|
|
err = ENFILE;
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
2007-11-16 19:48:39 +01:00
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
/* Check the backlog to see if there is a connection already pending for
|
|
|
|
* this listener.
|
2008-11-20 20:24:06 +01:00
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
save = net_lock();
|
2014-06-25 02:12:49 +02:00
|
|
|
conn = (struct tcp_conn_s *)psock->s_conn;
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
#ifdef CONFIG_NET_TCPBACKLOG
|
2014-06-25 02:12:49 +02:00
|
|
|
state.acpt_newconn = tcp_backlogremove(conn);
|
2008-11-20 22:50:55 +01:00
|
|
|
if (state.acpt_newconn)
|
|
|
|
{
|
|
|
|
/* Yes... get the address of the connected client */
|
|
|
|
|
|
|
|
nvdbg("Pending conn=%p\n", state.acpt_newconn);
|
|
|
|
accept_tcpsender(state.acpt_newconn, inaddr);
|
|
|
|
}
|
2009-08-03 01:35:27 +02:00
|
|
|
|
|
|
|
/* In general, this uIP-based implementation will not support non-blocking
|
|
|
|
* socket operations... except in a few cases: Here for TCP accept with backlog
|
|
|
|
* enabled. If this socket is configured as non-blocking then return EAGAIN
|
|
|
|
* if there is no pending connection in the backlog.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (_SS_ISNONBLOCK(psock->s_flags))
|
|
|
|
{
|
|
|
|
err = EAGAIN;
|
2011-03-15 21:26:34 +01:00
|
|
|
goto errout_with_lock;
|
2009-08-03 01:35:27 +02:00
|
|
|
}
|
2008-11-20 22:50:55 +01:00
|
|
|
else
|
2008-11-20 20:24:06 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Set the socket state to accepting */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_ACCEPT);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Perform the TCP accept operation */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Initialize the state structure. This is done with interrupts
|
|
|
|
* disabled because we don't want anything to happen until we
|
|
|
|
* are ready.
|
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
state.acpt_addr = inaddr;
|
|
|
|
state.acpt_newconn = NULL;
|
|
|
|
state.acpt_result = OK;
|
|
|
|
sem_init(&state.acpt_sem, 0, 0);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Set up the callback in the connection */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
conn->accept_private = (void*)&state;
|
|
|
|
conn->accept = accept_interrupt;
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Wait for the send to complete or an error to occur: NOTES: (1)
|
2014-06-26 22:23:21 +02:00
|
|
|
* net_lockedwait will also terminate if a signal is received, (2) interrupts
|
2011-03-15 21:26:34 +01:00
|
|
|
* may be disabled! They will be re-enabled while the task sleeps and
|
2008-11-20 20:24:06 +01:00
|
|
|
* automatically re-enabled when the task restarts.
|
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
ret = net_lockedwait(&state.acpt_sem);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Make sure that no further interrupts are processed */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
conn->accept_private = NULL;
|
|
|
|
conn->accept = NULL;
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
sem_destroy(&state. acpt_sem);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Set the socket state to idle */
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
/* Check for a errors. Errors are signaled by negative errno values
|
2012-02-22 17:03:10 +01:00
|
|
|
* for the send length.
|
2008-11-20 20:24:06 +01:00
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2008-11-20 20:24:06 +01:00
|
|
|
if (state.acpt_result != 0)
|
|
|
|
{
|
|
|
|
err = state.acpt_result;
|
2011-03-15 21:26:34 +01:00
|
|
|
goto errout_with_lock;
|
2008-11-20 20:24:06 +01:00
|
|
|
}
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
/* If net_lockedwait failed, then we were probably reawakened by a signal. In
|
|
|
|
* this case, net_lockedwait will have set errno appropriately.
|
2008-11-20 20:24:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
err = -ret;
|
2011-03-15 21:26:34 +01:00
|
|
|
goto errout_with_lock;
|
2008-11-20 20:24:06 +01:00
|
|
|
}
|
2007-09-23 22:45:30 +02:00
|
|
|
}
|
|
|
|
|
2009-06-15 20:58:22 +02:00
|
|
|
/* Initialize the socket structure and mark the socket as connected.
|
|
|
|
* (The reference count on the new connection structure was set in the
|
|
|
|
* interrupt handler).
|
|
|
|
*/
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2007-11-16 19:48:39 +01:00
|
|
|
pnewsock->s_type = SOCK_STREAM;
|
|
|
|
pnewsock->s_conn = state.acpt_newconn;
|
|
|
|
pnewsock->s_flags |= _SF_CONNECTED;
|
2012-02-18 19:13:30 +01:00
|
|
|
pnewsock->s_flags &= ~_SF_CLOSED;
|
2012-02-22 17:03:10 +01:00
|
|
|
|
|
|
|
/* Begin monitoring for TCP connection events on the newly connected socket */
|
|
|
|
|
|
|
|
net_startmonitor(pnewsock);
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(save);
|
2007-09-23 22:45:30 +02:00
|
|
|
return newfd;
|
|
|
|
|
2011-03-15 21:26:34 +01:00
|
|
|
errout_with_lock:
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(save);
|
2008-11-20 20:24:06 +01:00
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
errout_with_socket:
|
|
|
|
sockfd_release(newfd);
|
|
|
|
|
|
|
|
errout:
|
2008-11-20 22:50:55 +01:00
|
|
|
errno = err;
|
2007-09-23 18:58:09 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2007-12-02 16:11:32 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS && CONFIG_NET_TCP */
|