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
|
|
|
*
|
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.
|
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,
|
|
|
|
FAR socklen_t *addrlen, FAR struct socket *newsock)
|
2007-09-23 18:58:09 +02:00
|
|
|
{
|
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? */
|
|
|
|
|
|
|
|
if (!_SS_ISLISTENING(psock->s_flags))
|
|
|
|
{
|
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
|
|
|
|
2017-07-13 19:15:00 +02:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_accept != NULL);
|
2017-08-31 15:23:19 +02:00
|
|
|
|
|
|
|
net_lock();
|
2017-07-13 19:15:00 +02:00
|
|
|
ret = psock->s_sockif->si_accept(psock, addr, addrlen, newsock);
|
|
|
|
if (ret < 0)
|
2015-01-25 22:46:05 +01:00
|
|
|
{
|
2017-07-13 19:15:00 +02:00
|
|
|
nerr("ERROR: si_accept failed: %d\n", ret);
|
2017-08-31 18:49:10 +02:00
|
|
|
goto errout_with_lock;
|
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
|
|
|
|
2017-09-30 16:18:08 +02:00
|
|
|
ret = OK;
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2017-08-31 18:49:10 +02:00
|
|
|
errout_with_lock:
|
|
|
|
net_unlock();
|
2017-09-30 16:18:08 +02:00
|
|
|
return ret;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: accept
|
2016-04-14 16:39:48 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2016-04-14 16:39:48 +02:00
|
|
|
* sockfd The listening socket descriptor
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* 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;
|
2021-03-16 13:13:02 +01:00
|
|
|
FAR struct file *filep;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2021-06-11 06:16:24 +02:00
|
|
|
int newfd;
|
2016-04-14 16:39:48 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-12-09 22:17:58 +01:00
|
|
|
/* accept() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
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 */
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock == NULL || psock->s_conn == NULL)
|
2016-04-14 16:39:48 +02:00
|
|
|
{
|
|
|
|
/* It is not a valid socket description. Distinguish between the cases
|
2021-02-19 15:01:46 +01:00
|
|
|
* where sockfd is a just valid and when it is a valid file descriptor
|
|
|
|
* used in the wrong context.
|
2016-04-14 16:39:48 +02:00
|
|
|
*/
|
|
|
|
|
2021-03-16 13:13:02 +01:00
|
|
|
if (fs_getfilep(sockfd, &filep) == 0)
|
2016-04-14 16:39:48 +02:00
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENOTSOCK;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2021-06-11 06:16:24 +02:00
|
|
|
newsock = kmm_zalloc(sizeof(*newsock));
|
|
|
|
if (newsock == NULL)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2021-06-11 06:24:37 +02:00
|
|
|
ret = psock_accept(psock, addr, addrlen, newsock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
|
|
|
|
2016-04-14 16:39:48 +02:00
|
|
|
/* Allocate a socket descriptor for the new connection now (so that it
|
|
|
|
* cannot fail later)
|
|
|
|
*/
|
|
|
|
|
2021-06-11 06:16:24 +02:00
|
|
|
newfd = sockfd_allocate(newsock, O_RDWR);
|
2016-04-14 16:39:48 +02:00
|
|
|
if (newfd < 0)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = ENFILE;
|
2021-06-11 06:24:37 +02:00
|
|
|
goto errout_with_psock;
|
2016-04-14 16:39:48 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-06-11 06:24:37 +02:00
|
|
|
errout_with_psock:
|
|
|
|
psock_close(newsock);
|
2007-09-23 22:45:30 +02:00
|
|
|
|
2021-06-11 06:16:24 +02:00
|
|
|
errout_with_alloc:
|
|
|
|
kmm_free(newsock);
|
|
|
|
|
2007-09-23 22:45:30 +02:00
|
|
|
errout:
|
2016-12-09 22:17:58 +01:00
|
|
|
leave_cancellation_point();
|
2017-09-30 16:18:08 +02:00
|
|
|
|
2019-12-24 15:09:55 +01:00
|
|
|
_SO_SETERRNO(psock, errcode);
|
2007-09-23 18:58:09 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|