mirror of
https://github.com/apache/nuttx.git
synced 2026-09-28 15:54:57 +08:00
Networking: Add local Unix domain socket listen logic
This commit is contained in:
+2
-1
@@ -37,7 +37,8 @@
|
||||
|
||||
ifeq ($(CONFIG_NET_LOCAL),y)
|
||||
|
||||
NET_CSRCS += local_conn.c
|
||||
NET_CSRCS += local_conn.c local_connect.c local_release.c local_bind.c
|
||||
NET_CSRCS += local_listen.c
|
||||
|
||||
# Include UDP build support
|
||||
|
||||
|
||||
+31
-1
@@ -44,6 +44,7 @@
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <semaphore.h>
|
||||
#include <queue.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -88,9 +89,15 @@ enum local_state_s
|
||||
|
||||
struct local_conn_s
|
||||
{
|
||||
/* Fields common to SOCK_STREAM and SOCK_DGRAM */
|
||||
/* lc_node supports a doubly linked list: Listening SOCK_STREAM servers
|
||||
* will be linked into a list of listeners; SOCK_STREAM clients will be
|
||||
* linked to the lc_waiters and lc_conn lists.
|
||||
*/
|
||||
|
||||
dq_entry_t lc_node; /* Supports a doubly linked list */
|
||||
|
||||
/* Fields common to SOCK_STREAM and SOCK_DGRAM */
|
||||
|
||||
uint8_t lc_crefs; /* Reference counts on this instance */
|
||||
uint8_t lc_family; /* SOCK_STREAM or SOCK_DGRAM */
|
||||
uint8_t lc_type; /* See enum local_type_e */
|
||||
@@ -138,6 +145,10 @@ extern "C"
|
||||
# define EXTERN extern
|
||||
#endif
|
||||
|
||||
/* A list of all SOCK_STREAM listener connections */
|
||||
|
||||
EXTERN dq_queue_t g_local_listeners;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
@@ -212,6 +223,25 @@ int local_bind(FAR struct local_conn_s *conn,
|
||||
int local_connect(FAR struct local_conn_s *client,
|
||||
FAR const struct sockaddr *addr);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: local_listen
|
||||
*
|
||||
* Description:
|
||||
* Listen for a new connection of a SOCK_STREAM Unix domain socket.
|
||||
*
|
||||
* This function is called as part of the implementation of listen();
|
||||
*
|
||||
* Input Parameters:
|
||||
* server - A reference to the server-side local connection structure
|
||||
* backlog - Maximum number of pending connections.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negated errno value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int local_listen(FAR struct local_conn_s *server, int backlog);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: local_release
|
||||
*
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/****************************************************************************
|
||||
* net/local/local_bind.c
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "local/local.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: local_bind
|
||||
*
|
||||
* Description:
|
||||
* This function implements the low-level parts of the standard local
|
||||
* bind()operation.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int local_bind(FAR struct local_conn_s *conn,
|
||||
FAR const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
FAR const struct sockaddr_un *unaddr =
|
||||
(FAR const struct sockaddr_un *)addr;
|
||||
int namelen;
|
||||
|
||||
DEBUGASSERT(conn && unaddr && unaddr->sun_family == AF_LOCAL &&
|
||||
addrlen >= sizeof(sa_family_t));
|
||||
|
||||
/* Save the address family */
|
||||
|
||||
conn->lc_family = unaddr->sun_family;
|
||||
|
||||
/* No determine the type of the Unix domain socket by comparing the size
|
||||
* of the address description.
|
||||
*/
|
||||
|
||||
if (addrlen == sizeof(sa_family_t))
|
||||
{
|
||||
/* No sun_path... This is an un-named Unix domain socket */
|
||||
|
||||
conn->lc_type = LOCAL_TYPE_UNNAMED;
|
||||
}
|
||||
else
|
||||
{
|
||||
namelen = strnlen(unaddr->sun_path, UNIX_PATH_MAX-1);
|
||||
if (namelen <= 0)
|
||||
{
|
||||
/* Zero-length sun_path... This is an abstract Unix domain socket */
|
||||
|
||||
conn->lc_type = LOCAL_TYPE_ABSTRACT;
|
||||
conn->lc_path[0] = '\0';
|
||||
}
|
||||
else
|
||||
{
|
||||
/* This is an normal, pathname Unix domain socket */
|
||||
|
||||
conn->lc_type = LOCAL_TYPE_PATHNAME;
|
||||
|
||||
/* Copy the path into the connection structure */
|
||||
|
||||
(void)strncpy(conn->lc_path, unaddr->sun_path, UNIX_PATH_MAX-1);
|
||||
conn->lc_path[UNIX_PATH_MAX-1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
conn->lc_state = LOCAL_STATE_BOUND;
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,146 @@
|
||||
/****************************************************************************
|
||||
* net/local/local_listen.c
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
||||
|
||||
#include <assert.h>
|
||||
#include <queue.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <nuttx/net/net.h>
|
||||
|
||||
#include "local/local.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/* A list of all allocated packet socket connections */
|
||||
|
||||
dq_queue_t g_local_listeners;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: local_listen
|
||||
*
|
||||
* Description:
|
||||
* Listen for a new connection of a SOCK_STREAM Unix domain socket.
|
||||
*
|
||||
* This function is called as part of the implementation of listen();
|
||||
*
|
||||
* Input Parameters:
|
||||
* server - A reference to the server-side local connection structure
|
||||
* backlog - Maximum number of pending connections.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) on success; a negated errno value on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int local_listen(FAR struct local_conn_s *server, int backlog)
|
||||
{
|
||||
net_lock_t state;
|
||||
int ret;
|
||||
|
||||
/* Some sanity checks */
|
||||
|
||||
DEBUGASSERT(server);
|
||||
|
||||
if (server->lc_family != SOCK_STREAM ||
|
||||
server->lc_state == LOCAL_STATE_UNBOUND ||
|
||||
server->lc_type != LOCAL_TYPE_PATHNAME)
|
||||
{
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
|
||||
DEBUGASSERT(server->lc_state == LOCAL_STATE_BOUND ||
|
||||
server->lc_state == LOCAL_STATE_LISTENING);
|
||||
|
||||
/* Set the backlog value */
|
||||
|
||||
DEBUGASSERT((unsigned)backlog < 256);
|
||||
server->u.server.lc_backlog = backlog;
|
||||
|
||||
/* Is this the first time since being bound to an address and that
|
||||
* listen() was called? If so, the state should be LOCAL_STATE_BOUND.
|
||||
*/
|
||||
|
||||
if (server->lc_state == LOCAL_STATE_BOUND)
|
||||
{
|
||||
/* The connection should not reside in any other list */
|
||||
|
||||
DEBUGASSERT(server->lc_node.flink == NULL &&
|
||||
server->lc_node.flink == NULL);
|
||||
|
||||
/* Add the connection structure to the list of listeners */
|
||||
|
||||
state = net_lock();
|
||||
dq_addlast(&server->lc_node, &g_local_listeners);
|
||||
net_unlock(state);
|
||||
|
||||
/* And change the server state to listing */
|
||||
|
||||
server->lc_state = LOCAL_STATE_LISTENING;
|
||||
}
|
||||
|
||||
/* Loop until a connection requested or we receive a signal */
|
||||
|
||||
while (dq_empty(&server->u.server.lc_waiters))
|
||||
{
|
||||
/* No.. wait for a connection or a signal */
|
||||
|
||||
ret = sem_wait(&server->lc_waitsem);
|
||||
if (ret < 0)
|
||||
{
|
||||
int errval = errno;
|
||||
DEBUGASSERT(errval == EINTR);
|
||||
return -errval;
|
||||
}
|
||||
}
|
||||
|
||||
/* There is a client waiting for the connection */
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|
||||
@@ -0,0 +1,183 @@
|
||||
/****************************************************************************
|
||||
* net/local/local_release.c
|
||||
*
|
||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
||||
|
||||
#include <semaphore.h>
|
||||
#include <errno.h>
|
||||
#include <queue.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <nuttx/net/net.h>
|
||||
|
||||
#include "local/local.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: local_release
|
||||
*
|
||||
* Description:
|
||||
* If the local, Unix domain socket is in the connected state, then
|
||||
* disconnect it. Release the local connection structure in any event
|
||||
*
|
||||
* Input Parameters:
|
||||
* conn - A reference to local connection structure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int local_release(FAR struct local_conn_s *conn)
|
||||
{
|
||||
net_lock_t state;
|
||||
|
||||
/* There should be no references on this structure */
|
||||
|
||||
DEBUGASSERT(conn->lc_crefs == 0);
|
||||
state = net_lock();
|
||||
|
||||
/* We should not bet here with states LOCAL_STATE_CLOSED or with
|
||||
* LOCAL_STATE_ACCEPT. Those are internal states that should be atomic
|
||||
* with respect to socket operations.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(conn->lc_state != LOCAL_STATE_CLOSED &&
|
||||
conn->lc_state != LOCAL_STATE_ACCEPT);
|
||||
|
||||
/* If the socket is connected (SOCK_STREAM client), then disconnect it */
|
||||
|
||||
if (conn->lc_state == LOCAL_STATE_CONNECTED ||
|
||||
conn->lc_state == LOCAL_STATE_DISCONNECTED)
|
||||
{
|
||||
FAR struct local_conn_s *server;
|
||||
|
||||
DEBUGASSERT(conn->lc_family == SOCK_STREAM);
|
||||
|
||||
server = conn->u.client.lc_server;
|
||||
DEBUGASSERT(server &&
|
||||
(server->lc_state == LOCAL_STATE_LISTENING ||
|
||||
server->lc_state == LOCAL_STATE_CLOSED) &&
|
||||
!dq_empty(&server->u.server.lc_conns));
|
||||
|
||||
/* Remove ourself from the list of connections */
|
||||
|
||||
dq_rem(&conn->lc_node, &server->u.server.lc_conns);
|
||||
|
||||
/* Is the list of pending connections now empty? Was the connection
|
||||
* already closed?
|
||||
*/
|
||||
|
||||
if (dq_empty(&server->u.server.lc_waiters) &&
|
||||
server->lc_state == LOCAL_STATE_CLOSED)
|
||||
{
|
||||
/* Yes, free the server connection as well */
|
||||
|
||||
local_free(server);
|
||||
}
|
||||
|
||||
/* Now we can free this connection structure */
|
||||
|
||||
local_free(conn);
|
||||
}
|
||||
|
||||
/* Is the socket is listening socket (SOCK_STREAM server) */
|
||||
|
||||
else if (conn->lc_state == LOCAL_STATE_LISTENING)
|
||||
{
|
||||
FAR struct local_conn_s *client;
|
||||
FAR struct local_conn_s *next;
|
||||
|
||||
DEBUGASSERT(conn->lc_family == SOCK_STREAM);
|
||||
|
||||
/* Are there still clients waiting for a connection to the server? */
|
||||
|
||||
for (client = (FAR struct local_conn_s *)conn->u.server.lc_waiters.head;
|
||||
client;
|
||||
client = (FAR struct local_conn_s *)dq_next(&client->lc_node))
|
||||
{
|
||||
client->u.client.lc_result = -ENETUNREACH;
|
||||
sem_post(&client->lc_waitsem);
|
||||
conn->lc_state = LOCAL_STATE_CLOSED;
|
||||
}
|
||||
|
||||
conn->u.server.lc_pending = 0;
|
||||
|
||||
/* Disconnect any previous client connections */
|
||||
|
||||
for (client = (FAR struct local_conn_s *)conn->u.server.lc_conns.head;
|
||||
client;
|
||||
client = next)
|
||||
{
|
||||
next = (FAR struct local_conn_s *)dq_next(&client->lc_node);
|
||||
dq_rem(&client->lc_node, &conn->u.server.lc_conns);
|
||||
client->lc_state = LOCAL_STATE_DISCONNECTED;
|
||||
}
|
||||
|
||||
/* Remove the server from the list of listeners */
|
||||
|
||||
dq_rem(&conn->lc_node, &g_local_listeners);
|
||||
|
||||
/* Can we free the connection structure now? We cannot
|
||||
* if there are still pending connection requested to
|
||||
* be resolved.
|
||||
*/
|
||||
|
||||
conn->u.server.lc_backlog = 0;
|
||||
if (conn->lc_state == LOCAL_STATE_CLOSED)
|
||||
{
|
||||
local_free(conn);
|
||||
}
|
||||
}
|
||||
|
||||
/* For the remaining states (LOCAL_STATE_UNBOUND and LOCAL_STATE_UNBOUND),
|
||||
* we simply free the connection structure.
|
||||
*/
|
||||
|
||||
else
|
||||
{
|
||||
local_free(conn);
|
||||
}
|
||||
|
||||
net_unlock(state);
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|
||||
@@ -43,6 +43,13 @@ SOCK_CSRCS += net_clone.c net_poll.c net_vfcntl.c
|
||||
|
||||
ifeq ($(CONFIG_NET_TCP),y)
|
||||
SOCK_CSRCS += send.c listen.c accept.c net_monitor.c
|
||||
else
|
||||
|
||||
# Local Unix domain support
|
||||
|
||||
ifeq ($(CONFIG_NET_LOCAL),y)
|
||||
SOCK_CSRCS += send.c listen.c accept.c
|
||||
endif
|
||||
endif
|
||||
|
||||
# Socket options
|
||||
|
||||
+50
-16
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
* net/socket/listen.c
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007-2009, 2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -45,6 +45,7 @@
|
||||
#include <debug.h>
|
||||
|
||||
#include "tcp/tcp.h"
|
||||
#include "local/local.h"
|
||||
#include "socket/socket.h"
|
||||
|
||||
/****************************************************************************
|
||||
@@ -89,7 +90,6 @@
|
||||
int listen(int sockfd, int backlog)
|
||||
{
|
||||
FAR struct socket *psock = sockfd_socket(sockfd);
|
||||
struct tcp_conn_s *conn;
|
||||
int err;
|
||||
|
||||
/* Verify that the sockfd corresponds to valid, allocated socket */
|
||||
@@ -111,39 +111,73 @@ int listen(int sockfd, int backlog)
|
||||
{
|
||||
err = EBADF;
|
||||
}
|
||||
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Verify that the sockfd corresponds to a connected SOCK_STREAM */
|
||||
|
||||
conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
||||
if (psock->s_type != SOCK_STREAM || !psock->s_conn || conn->lport <= 0)
|
||||
if (psock->s_type != SOCK_STREAM || !psock->s_conn)
|
||||
{
|
||||
err = EOPNOTSUPP;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Set up the backlog for this connection */
|
||||
#ifdef CONFIG_NET_LOCAL
|
||||
#ifdef CONFIG_NET_TCP
|
||||
if (psock->s_domain == PF_LOCAL)
|
||||
#endif
|
||||
{
|
||||
FAR struct local_conn_s *conn =
|
||||
(FAR struct local_conn_s *)psock->s_conn;
|
||||
|
||||
err = local_listen(conn, backlog);
|
||||
if (err < 0)
|
||||
{
|
||||
err = -err;
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_NET_LOCAL */
|
||||
|
||||
#ifdef CONFIG_NET_TCP
|
||||
#ifdef CONFIG_NET_LOCAL
|
||||
else
|
||||
#endif
|
||||
{
|
||||
FAR struct tcp_conn_s *conn =
|
||||
(FAR struct tcp_conn_s *)psock->s_conn;
|
||||
|
||||
if (conn->lport <= 0)
|
||||
{
|
||||
err = EOPNOTSUPP;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Set up the backlog for this connection */
|
||||
|
||||
#ifdef CONFIG_NET_TCPBACKLOG
|
||||
err = tcp_backlogcreate(conn, backlog);
|
||||
if (err < 0)
|
||||
{
|
||||
err = -err;
|
||||
goto errout;
|
||||
}
|
||||
err = tcp_backlogcreate(conn, backlog);
|
||||
if (err < 0)
|
||||
{
|
||||
err = -err;
|
||||
goto errout;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Start listening to the bound port. This enables callbacks when accept()
|
||||
* is called and enables poll()/select() logic.
|
||||
*/
|
||||
/* Start listening to the bound port. This enables callbacks when
|
||||
* accept() is called and enables poll()/select() logic.
|
||||
*/
|
||||
|
||||
tcp_listen(conn);
|
||||
}
|
||||
#endif /* CONFIG_NET_TCP */
|
||||
|
||||
tcp_listen(conn);
|
||||
psock->s_flags |= _SF_LISTENING;
|
||||
return OK;
|
||||
|
||||
errout:
|
||||
errno = err;
|
||||
set_errno(err);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user