2015-01-25 14:36:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_conn.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 14:36:16 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-25 14:36:16 +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 14:36:16 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2015-01-25 15:33:39 +01:00
|
|
|
#include <string.h>
|
2015-01-25 14:36:16 +01:00
|
|
|
#include <assert.h>
|
2015-01-25 19:37:40 +01:00
|
|
|
#include <errno.h>
|
2015-01-25 14:36:16 +01:00
|
|
|
#include <debug.h>
|
2022-11-22 14:18:12 +01:00
|
|
|
#include <unistd.h>
|
2015-01-25 14:36:16 +01:00
|
|
|
|
2015-01-25 15:33:39 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2015-01-25 15:33:39 +01:00
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
#include "local/local.h"
|
|
|
|
|
2021-09-13 09:06:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* A list of all allocated packet socket connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_local_connections;
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-19 12:06:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_nextconn
|
|
|
|
*
|
|
|
|
* Description:
|
2021-09-13 09:06:15 +02:00
|
|
|
* Traverse the list of local connections
|
2021-04-19 12:06:14 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct local_conn_s *local_nextconn(FAR struct local_conn_s *conn)
|
|
|
|
{
|
|
|
|
if (!conn)
|
|
|
|
{
|
2021-09-13 09:06:15 +02:00
|
|
|
return (FAR struct local_conn_s *)g_local_connections.head;
|
2021-04-19 12:06:14 +02:00
|
|
|
}
|
2021-09-13 09:06:15 +02:00
|
|
|
|
2022-02-07 04:44:52 +01:00
|
|
|
return (FAR struct local_conn_s *)conn->lc_conn.node.flink;
|
2021-04-19 12:06:14 +02:00
|
|
|
}
|
|
|
|
|
2024-01-31 11:50:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_findconn
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the connections list to find the local connection
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct local_conn_s *
|
|
|
|
local_findconn(FAR const struct local_conn_s *local_conn,
|
|
|
|
FAR const struct sockaddr_un *unaddr)
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *conn = NULL;
|
|
|
|
|
|
|
|
int index = unaddr->sun_path[0] == '\0' ? 1 : 0;
|
|
|
|
|
|
|
|
while ((conn = local_nextconn(conn)) != NULL)
|
|
|
|
{
|
|
|
|
if (local_conn->lc_proto == conn->lc_proto &&
|
|
|
|
strncmp(conn->lc_path, &unaddr->sun_path[index],
|
|
|
|
UNIX_PATH_MAX - 1) == 0)
|
|
|
|
{
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-09-14 09:21:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_peerconn
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Traverse the connections list to find the peer
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct local_conn_s *local_peerconn(FAR struct local_conn_s *conn)
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *peer = NULL;
|
|
|
|
|
|
|
|
while ((peer = local_nextconn(peer)) != NULL)
|
|
|
|
{
|
|
|
|
if (conn->lc_proto == peer->lc_proto && conn != peer &&
|
|
|
|
!strncmp(conn->lc_path, peer->lc_path, UNIX_PATH_MAX - 1))
|
|
|
|
{
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_alloc()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new, uninitialized Unix domain socket connection structure.
|
|
|
|
* This is normally something done by the implementation of the socket()
|
|
|
|
* API
|
|
|
|
*
|
2024-06-05 09:29:04 +02:00
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
2015-01-25 14:36:16 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct local_conn_s *local_alloc(void)
|
|
|
|
{
|
2015-01-25 15:33:39 +01:00
|
|
|
FAR struct local_conn_s *conn =
|
2023-08-28 09:39:47 +02:00
|
|
|
kmm_zalloc(sizeof(struct local_conn_s));
|
2015-01-25 15:33:39 +01:00
|
|
|
|
2019-09-01 19:23:40 +02:00
|
|
|
if (conn != NULL)
|
2015-01-25 15:33:39 +01:00
|
|
|
{
|
2019-09-01 19:23:40 +02:00
|
|
|
/* Initialize non-zero elements the new connection structure. Since
|
|
|
|
* Since the memory was allocated with kmm_zalloc(), it is not
|
|
|
|
* necessary to zerio-ize any structure elements.
|
|
|
|
*/
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2024-02-28 03:47:17 +01:00
|
|
|
conn->lc_crefs = 1;
|
2024-08-14 14:41:23 +02:00
|
|
|
conn->lc_rcvsize = CONFIG_DEV_FIFO_SIZE;
|
2024-02-28 03:47:17 +01:00
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&conn->lc_waitsem, 0, 0);
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif
|
2021-09-13 09:06:15 +02:00
|
|
|
|
2022-08-08 12:20:41 +02:00
|
|
|
/* This semaphore is used for sending safely in multithread.
|
|
|
|
* Make sure data will not be garbled when multi-thread sends.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&conn->lc_sendlock);
|
2023-07-27 12:34:23 +02:00
|
|
|
nxmutex_init(&conn->lc_polllock);
|
2022-08-08 12:20:41 +02:00
|
|
|
|
2022-11-22 14:18:12 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
2023-02-01 11:05:58 +01:00
|
|
|
conn->lc_cred.pid = nxsched_getpid();
|
2022-11-22 14:18:12 +01:00
|
|
|
conn->lc_cred.uid = getuid();
|
|
|
|
conn->lc_cred.gid = getgid();
|
|
|
|
#endif
|
|
|
|
|
2021-09-13 09:06:15 +02:00
|
|
|
/* Add the connection structure to the list of listeners */
|
|
|
|
|
2022-02-07 04:44:52 +01:00
|
|
|
dq_addlast(&conn->lc_conn.node, &g_local_connections);
|
2015-01-25 15:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
2015-01-25 14:36:16 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_alloc_accept
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when a client calls connect and can find the appropriate
|
|
|
|
* connection in LISTEN. In that case, this function will create
|
|
|
|
* a new connection and initialize it.
|
|
|
|
*
|
2024-06-05 09:29:04 +02:00
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
2023-12-19 10:37:08 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int local_alloc_accept(FAR struct local_conn_s *server,
|
|
|
|
FAR struct local_conn_s *client,
|
|
|
|
FAR struct local_conn_s **accept)
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *conn;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create a new connection structure for the server side of the
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn = local_alloc();
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to allocate new connection structure\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->lc_proto = SOCK_STREAM;
|
|
|
|
conn->lc_type = LOCAL_TYPE_PATHNAME;
|
|
|
|
conn->lc_state = LOCAL_STATE_CONNECTED;
|
|
|
|
conn->lc_peer = client;
|
|
|
|
client->lc_peer = conn;
|
|
|
|
|
|
|
|
strlcpy(conn->lc_path, client->lc_path, sizeof(conn->lc_path));
|
|
|
|
conn->lc_instance_id = client->lc_instance_id;
|
|
|
|
|
|
|
|
/* Open the server-side write-only FIFO. This should not
|
|
|
|
* block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = local_open_server_tx(conn, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
|
|
|
|
conn->lc_path, ret);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we have a connection? Is the write-side FIFO opened? */
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->lc_outfile.f_inode != NULL);
|
|
|
|
|
|
|
|
/* Open the server-side read-only FIFO. This should not
|
|
|
|
* block because the client side has already opening it
|
|
|
|
* for writing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = local_open_server_rx(conn, false);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
|
|
|
|
conn->lc_path, ret);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do we have a connection? Are the FIFOs opened? */
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->lc_infile.f_inode != NULL);
|
|
|
|
*accept = conn;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
err:
|
|
|
|
local_free(conn);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_free()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a packet Unix domain connection structure that is no longer in use.
|
|
|
|
* This should be done by the implementation of close().
|
|
|
|
*
|
2024-06-05 09:29:04 +02:00
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
2015-01-25 14:36:16 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void local_free(FAR struct local_conn_s *conn)
|
|
|
|
{
|
2021-09-15 17:22:18 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
int i;
|
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2015-01-25 19:37:40 +01:00
|
|
|
|
2021-09-13 09:06:15 +02:00
|
|
|
/* Remove the server from the list of listeners. */
|
|
|
|
|
2022-02-07 04:44:52 +01:00
|
|
|
dq_rem(&conn->lc_conn.node, &g_local_connections);
|
2021-09-15 17:22:18 +02:00
|
|
|
|
|
|
|
if (local_peerconn(conn) && conn->lc_peer)
|
|
|
|
{
|
|
|
|
conn->lc_peer->lc_peer = NULL;
|
2021-10-15 12:36:30 +02:00
|
|
|
conn->lc_peer = NULL;
|
2021-09-15 17:22:18 +02:00
|
|
|
}
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Make sure that the read-only FIFO is closed */
|
2015-01-25 19:37:40 +01:00
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
if (conn->lc_infile.f_inode != NULL)
|
2015-01-25 19:37:40 +01:00
|
|
|
{
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&conn->lc_infile);
|
2017-11-02 15:23:38 +01:00
|
|
|
conn->lc_infile.f_inode = NULL;
|
2015-01-25 19:37:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Make sure that the write-only FIFO is closed */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
if (conn->lc_outfile.f_inode != NULL)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&conn->lc_outfile);
|
2017-11-02 15:23:38 +01:00
|
|
|
conn->lc_outfile.f_inode = NULL;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
2021-09-15 17:22:18 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
/* Free the pending control file pointer */
|
|
|
|
|
|
|
|
for (i = 0; i < conn->lc_cfpcount; i++)
|
|
|
|
{
|
|
|
|
if (conn->lc_cfps[i])
|
|
|
|
{
|
|
|
|
file_close(conn->lc_cfps[i]);
|
|
|
|
kmm_free(conn->lc_cfps[i]);
|
|
|
|
conn->lc_cfps[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_LOCAL_SCM */
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Destroy all FIFOs associted with the connection */
|
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
local_release_fifos(conn);
|
2023-09-27 10:17:16 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&conn->lc_waitsem);
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif
|
2015-01-25 19:37:40 +01:00
|
|
|
|
2022-08-08 12:20:41 +02:00
|
|
|
/* Destory sem associated with the connection */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&conn->lc_sendlock);
|
2023-07-27 12:34:23 +02:00
|
|
|
nxmutex_destroy(&conn->lc_polllock);
|
2022-08-08 12:20:41 +02:00
|
|
|
|
2015-01-25 19:37:40 +01:00
|
|
|
/* And free the connection structure */
|
|
|
|
|
|
|
|
kmm_free(conn);
|
2015-01-25 14:36:16 +01:00
|
|
|
}
|
|
|
|
|
2023-09-22 07:33:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_addref
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Increment the reference count on the underlying connection structure.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void local_addref(FAR struct local_conn_s *conn)
|
|
|
|
{
|
2024-03-27 12:21:45 +01:00
|
|
|
DEBUGASSERT(conn->lc_crefs < 255);
|
2023-09-22 07:33:42 +02:00
|
|
|
conn->lc_crefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_subref
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Subtract the reference count on the underlying connection structure.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void local_subref(FAR struct local_conn_s *conn)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(conn->lc_crefs > 0 && conn->lc_crefs < 255);
|
|
|
|
|
|
|
|
conn->lc_crefs--;
|
|
|
|
if (conn->lc_crefs == 0)
|
|
|
|
{
|
|
|
|
local_release(conn);
|
|
|
|
}
|
|
|
|
}
|