2015-01-25 22:45:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/tcp/tcp_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 22:45:51 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-25 22:45:51 +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 22:45:51 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2019-02-11 22:47:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-11-03 19:17:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-01-25 22:45:51 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "tcp/tcp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct accept_s
|
|
|
|
{
|
2017-09-02 18:27:03 +02:00
|
|
|
sem_t acpt_sem; /* Wait for driver event */
|
2015-01-25 22:45:51 +01:00
|
|
|
FAR struct sockaddr *acpt_addr; /* Return connection address */
|
|
|
|
FAR socklen_t *acpt_addrlen; /* Return length of address */
|
|
|
|
FAR struct tcp_conn_s *acpt_newconn; /* The accepted connection */
|
|
|
|
int acpt_result; /* The result of the wait */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: accept_tcpsender
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the sender's address from the UDP packet
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2022-11-24 08:45:09 +01:00
|
|
|
* listener - The connection structure of the listener
|
|
|
|
* conn - The newly accepted TCP connection
|
|
|
|
* pstate - the recvfrom state structure
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-08-29 23:08:38 +02:00
|
|
|
* The network is locked
|
2015-10-08 23:10:04 +02:00
|
|
|
*
|
2015-01-25 22:45:51 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
static inline void accept_tcpsender(FAR struct tcp_conn_s *listener,
|
2015-01-25 22:45:51 +01:00
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
if (addr)
|
|
|
|
{
|
2015-01-25 23:27:25 +01:00
|
|
|
/* If an address is provided, then the length must also be provided. */
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(addrlen);
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* If both IPv4 and IPv6 support are enabled, then we will need to
|
|
|
|
* select which one to use when obtaining the sender's IP address.
|
|
|
|
*/
|
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
if (listener->domain == PF_INET)
|
2015-01-25 22:45:51 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in *inaddr = (FAR struct sockaddr_in *)addr;
|
|
|
|
|
|
|
|
inaddr->sin_family = AF_INET;
|
|
|
|
inaddr->sin_port = conn->rport;
|
|
|
|
net_ipv4addr_copy(inaddr->sin_addr.s_addr, conn->u.ipv4.raddr);
|
2020-02-25 15:53:39 +01:00
|
|
|
memset(inaddr->sin_zero, 0, sizeof(inaddr->sin_zero));
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
*addrlen = sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Otherwise, this the IPv6 address is needed */
|
|
|
|
|
|
|
|
else
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in6 *inaddr = (FAR struct sockaddr_in6 *)addr;
|
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
DEBUGASSERT(listener->domain == PF_INET6);
|
|
|
|
#endif
|
2015-01-25 22:45:51 +01:00
|
|
|
inaddr->sin6_family = AF_INET6;
|
|
|
|
inaddr->sin6_port = conn->rport;
|
|
|
|
net_ipv6addr_copy(inaddr->sin6_addr.s6_addr, conn->u.ipv6.raddr);
|
|
|
|
|
|
|
|
*addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: accept_eventhandler
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2017-09-02 18:27:03 +02:00
|
|
|
* Receive event callbacks when connections occur
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-01-25 23:27:25 +01:00
|
|
|
* listener The connection structure of the listener
|
|
|
|
* conn The connection structure that was just accepted
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2017-08-29 23:08:38 +02:00
|
|
|
* The network is locked
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static int accept_eventhandler(FAR struct tcp_conn_s *listener,
|
|
|
|
FAR struct tcp_conn_s *conn)
|
2015-01-25 22:45:51 +01:00
|
|
|
{
|
|
|
|
struct accept_s *pstate = (struct accept_s *)listener->accept_private;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
if (pstate)
|
|
|
|
{
|
|
|
|
/* Get the connection address */
|
|
|
|
|
2022-11-24 08:45:09 +01:00
|
|
|
accept_tcpsender(listener, conn, pstate->acpt_addr,
|
2015-01-25 22:45:51 +01:00
|
|
|
pstate->acpt_addrlen);
|
|
|
|
|
|
|
|
/* Save the connection structure */
|
|
|
|
|
|
|
|
pstate->acpt_newconn = conn;
|
|
|
|
pstate->acpt_result = OK;
|
|
|
|
|
|
|
|
/* There should be a reference of one on the new connection */
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 1);
|
|
|
|
|
|
|
|
/* Wake-up the waiting caller thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&pstate->acpt_sem);
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
/* Stop any further callbacks */
|
|
|
|
|
|
|
|
listener->accept_private = NULL;
|
|
|
|
listener->accept = NULL;
|
|
|
|
ret = OK;
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_accept
|
2015-01-25 22:45:51 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements accept() for TCP/IP sockets. See the
|
|
|
|
* description of accept() for further information.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-01-25 22:45:51 +01:00
|
|
|
* psock The listening TCP socket structure
|
|
|
|
* addr Receives the address of the connecting client
|
2015-05-28 16:23:51 +02:00
|
|
|
* addrlen Input: allocated size of 'addr', Return: returned size of
|
|
|
|
* 'addr'
|
2015-01-25 22:45:51 +01:00
|
|
|
* newconn The new, accepted TCP 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 locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int psock_tcp_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen, FAR void **newconn)
|
|
|
|
{
|
|
|
|
FAR struct tcp_conn_s *conn;
|
|
|
|
struct accept_s state;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check the backlog to see if there is a connection already pending for
|
|
|
|
* this listener.
|
|
|
|
*/
|
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCPBACKLOG
|
|
|
|
state.acpt_newconn = tcp_backlogremove(conn);
|
|
|
|
if (state.acpt_newconn)
|
|
|
|
{
|
|
|
|
/* Yes... get the address of the connected client */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("Pending conn=%p\n", state.acpt_newconn);
|
2022-11-24 08:45:09 +01:00
|
|
|
accept_tcpsender(conn, state.acpt_newconn, addr, addrlen);
|
2015-01-25 22:45:51 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 17:31:44 +02:00
|
|
|
/* In general, this implementation will not support non-blocking socket
|
|
|
|
* operations... except in a few cases: Here for TCP accept with
|
2015-05-28 16:23:51 +02:00
|
|
|
* backlog enabled. If this socket is configured as non-blocking then
|
|
|
|
* return EAGAIN if there is no pending connection in the backlog.
|
2015-01-25 22:45:51 +01:00
|
|
|
*/
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
else if (_SS_ISNONBLOCK(conn->sconn.s_flags))
|
2015-01-25 22:45:51 +01:00
|
|
|
{
|
2015-08-25 15:15:21 +02:00
|
|
|
return -EAGAIN;
|
2015-01-25 22:45:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Perform the TCP accept operation */
|
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Initialize the state structure. This is done with the network
|
|
|
|
* locked because we don't want anything to happen until we are
|
|
|
|
* ready.
|
2015-01-25 22:45:51 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
state.acpt_addr = addr;
|
|
|
|
state.acpt_addrlen = addrlen;
|
|
|
|
state.acpt_newconn = NULL;
|
|
|
|
state.acpt_result = OK;
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&state.acpt_sem, 0, 0);
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
conn->accept_private = (FAR void *)&state;
|
2017-08-29 22:08:04 +02:00
|
|
|
conn->accept = accept_eventhandler;
|
2015-01-25 22:45:51 +01:00
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Wait for the send to complete or an error to occur: NOTES:
|
2023-01-13 07:51:38 +01:00
|
|
|
* net_sem_wait will also terminate if a signal is received.
|
2015-01-25 22:45:51 +01:00
|
|
|
*/
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_wait(&state.acpt_sem);
|
2015-01-25 22:45:51 +01:00
|
|
|
|
2017-09-02 18:27:03 +02:00
|
|
|
/* Make sure that no further events are processed */
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
conn->accept_private = NULL;
|
|
|
|
conn->accept = NULL;
|
|
|
|
|
2020-05-14 10:42:18 +02:00
|
|
|
nxsem_destroy(&state.acpt_sem);
|
2015-01-25 22:45:51 +01:00
|
|
|
|
|
|
|
/* Check for a errors. Errors are signalled by negative errno values
|
|
|
|
* for the send length.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (state.acpt_result != 0)
|
|
|
|
{
|
2015-08-25 15:15:21 +02:00
|
|
|
DEBUGASSERT(state.acpt_result > 0);
|
|
|
|
return -state.acpt_result;
|
2015-01-25 22:45:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
/* If net_sem_wait failed, then we were probably reawakened by a
|
|
|
|
* signal. In this case, net_sem_wait will have returned negated
|
2017-09-04 14:55:28 +02:00
|
|
|
* errno appropriately.
|
2015-01-25 22:45:51 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2015-08-25 15:15:21 +02:00
|
|
|
return ret;
|
2015-01-25 22:45:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*newconn = (FAR void *)state.acpt_newconn;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-02-11 22:47:25 +01:00
|
|
|
#endif /* CONFIG_NET_TCP */
|