2015-01-25 21:16:44 +01:00
|
|
|
/****************************************************************************
|
2019-09-20 02:19:18 +02:00
|
|
|
* net/local/local_connect.c
|
2015-01-25 21:16:44 +01:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-05-17 16:47:40 +02: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 21:16:44 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-25 21:16:44 +01:00
|
|
|
*
|
2020-05-17 16:47:40 +02: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 21:16:44 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2015-01-26 20:30:27 +01:00
|
|
|
#include <unistd.h>
|
2015-01-25 21:16:44 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2015-01-25 21:16:44 +01:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
#include <arch/irq.h>
|
2022-12-27 08:12:38 +01:00
|
|
|
#include <sys/stat.h>
|
2024-08-14 14:41:23 +02:00
|
|
|
#include <sys/param.h>
|
2015-01-30 19:43:37 +01:00
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
#include "utils/utils.h"
|
2015-01-30 19:43:37 +01:00
|
|
|
#include "socket/socket.h"
|
2015-01-25 21:16:44 +01:00
|
|
|
#include "local/local.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_stream_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a local connection structure that is the appropriate "server"
|
|
|
|
* connection to be used with the provided "client" connection.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-25 21:16:44 +01:00
|
|
|
* Zero (OK) returned on success; A negated errno value is returned on a
|
|
|
|
* failure. Possible failures include:
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The network is locked on entry, unlocked on return. This logic is
|
|
|
|
* an integral part of the lock_connect() implementation and was
|
|
|
|
* separated out only to improve readability.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-03-30 14:54:59 +02:00
|
|
|
static int inline local_stream_connect(FAR struct local_conn_s *client,
|
|
|
|
FAR struct local_conn_s *server,
|
|
|
|
bool nonblock)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
FAR struct local_conn_s *conn;
|
2015-01-25 21:16:44 +01:00
|
|
|
int ret;
|
2017-09-01 15:47:29 +02:00
|
|
|
int sval;
|
2015-01-25 21:16:44 +01:00
|
|
|
|
|
|
|
/* Has server backlog been reached?
|
|
|
|
* NOTE: The backlog will be zero if listen() has never been called by the
|
|
|
|
* server.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (server->lc_state != LOCAL_STATE_LISTENING ||
|
|
|
|
server->u.server.lc_pending >= server->u.server.lc_backlog)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Server is not listening: lc_state=%d\n",
|
2015-01-26 20:30:27 +01:00
|
|
|
server->lc_state);
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr(" OR: The backlog limit was reached: %d or %d\n",
|
2015-01-26 20:30:27 +01:00
|
|
|
server->u.server.lc_pending, server->u.server.lc_backlog);
|
2015-01-25 21:16:44 +01:00
|
|
|
return -ECONNREFUSED;
|
|
|
|
}
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Create the FIFOs needed for the connection */
|
|
|
|
|
2024-08-26 08:48:55 +02:00
|
|
|
ret = local_create_fifos(client, server->lc_rcvsize, client->lc_rcvsize);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to create FIFOs for %s: %d\n",
|
2015-01-26 20:30:27 +01:00
|
|
|
client->lc_path, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the client-side write-only FIFO. This should not block and should
|
|
|
|
* prevent the server-side from blocking as well.
|
|
|
|
*/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_open_client_tx(client, nonblock);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
|
2015-01-26 20:30:27 +01:00
|
|
|
client->lc_path, ret);
|
|
|
|
goto errout_with_fifos;
|
|
|
|
}
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
DEBUGASSERT(client->lc_outfile.f_inode != NULL);
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2024-06-05 09:29:04 +02:00
|
|
|
net_lock();
|
2023-12-19 10:37:08 +01:00
|
|
|
ret = local_alloc_accept(server, client, &conn);
|
2024-06-05 09:29:04 +02:00
|
|
|
net_unlock();
|
2023-12-19 10:37:08 +01:00
|
|
|
if (ret < 0)
|
2017-09-01 15:47:29 +02:00
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
nerr("ERROR: Failed to alloc accept conn %s: %d\n",
|
|
|
|
client->lc_path, ret);
|
|
|
|
goto errout_with_outfd;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
client->lc_state = LOCAL_STATE_ACCEPT;
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Yes.. open the read-only FIFO */
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
ret = local_open_client_rx(client, nonblock);
|
2015-01-26 20:30:27 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
|
2015-01-26 20:30:27 +01:00
|
|
|
client->lc_path, ret);
|
2023-12-19 10:37:08 +01:00
|
|
|
goto errout_with_conn;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
DEBUGASSERT(client->lc_infile.f_inode != NULL);
|
2021-08-13 14:53:38 +02:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
/* Increment the number of pending server connections */
|
2022-05-27 18:24:35 +02:00
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
server->u.server.lc_pending++;
|
|
|
|
DEBUGASSERT(server->u.server.lc_pending != 0);
|
|
|
|
|
|
|
|
/* Add ourself to the list of waiting connections and notify the server. */
|
|
|
|
|
|
|
|
dq_addlast(&conn->u.accept.lc_waiter, &server->u.server.lc_waiters);
|
|
|
|
local_event_pollnotify(server, POLLIN);
|
|
|
|
|
|
|
|
if (nxsem_get_value(&server->lc_waitsem, &sval) >= 0 && sval < 1)
|
2021-08-13 14:53:38 +02:00
|
|
|
{
|
2023-12-19 10:37:08 +01:00
|
|
|
nxsem_post(&server->lc_waitsem);
|
2021-08-13 14:53:38 +02:00
|
|
|
}
|
|
|
|
|
2023-12-19 10:37:08 +01:00
|
|
|
client->lc_state = LOCAL_STATE_CONNECTED;
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout_with_conn:
|
2024-06-05 09:29:04 +02:00
|
|
|
net_lock();
|
2023-12-19 10:37:08 +01:00
|
|
|
local_free(conn);
|
2024-06-05 09:29:04 +02:00
|
|
|
net_unlock();
|
2015-01-26 20:30:27 +01:00
|
|
|
|
|
|
|
errout_with_outfd:
|
2020-01-02 17:49:34 +01:00
|
|
|
file_close(&client->lc_outfile);
|
2017-11-02 15:23:38 +01:00
|
|
|
client->lc_outfile.f_inode = NULL;
|
2015-01-25 21:16:44 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
errout_with_fifos:
|
2020-01-02 17:49:34 +01:00
|
|
|
local_release_fifos(client);
|
2015-01-26 20:30:27 +01:00
|
|
|
client->lc_state = LOCAL_STATE_BOUND;
|
2015-01-25 21:16:44 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-08 16:16:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_generate_instance_id
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Generate instance ID for stream
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int32_t local_generate_instance_id(void)
|
|
|
|
{
|
|
|
|
static int32_t g_next_instance_id = 0;
|
|
|
|
int32_t id;
|
|
|
|
|
|
|
|
/* Called from local_connect with net_lock held. */
|
|
|
|
|
|
|
|
id = g_next_instance_id++;
|
|
|
|
if (g_next_instance_id < 0)
|
|
|
|
{
|
|
|
|
g_next_instance_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/****************************************************************************
|
2015-01-30 19:43:37 +01:00
|
|
|
* Name: psock_local_connect
|
2015-01-25 21:16:44 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a local connection structure that is the appropriate "server"
|
|
|
|
* connection to be used with the provided "client" connection.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-25 21:16:44 +01:00
|
|
|
* Zero (OK) returned on success; A negated errno value is returned on a
|
|
|
|
* failure. Possible failures include:
|
|
|
|
*
|
|
|
|
* EISCONN - The specified socket is connection-mode and is already
|
|
|
|
* connected.
|
|
|
|
* EADDRNOTAVAIL - The specified address is not available from the
|
|
|
|
* local machine.
|
|
|
|
* ECONNREFUSED - The target address was not listening for connections or
|
|
|
|
* refused the connection request because the connection backlog has
|
|
|
|
* been exceeded.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
int psock_local_connect(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
FAR struct local_conn_s *client = psock->s_conn;
|
2015-01-27 23:39:30 +01:00
|
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)addr;
|
2022-12-18 08:18:56 +01:00
|
|
|
FAR const char *unpath = unaddr->sun_path;
|
2021-04-19 12:06:14 +02:00
|
|
|
FAR struct local_conn_s *conn = NULL;
|
2022-12-18 08:18:56 +01:00
|
|
|
uint8_t type = LOCAL_TYPE_PATHNAME;
|
2022-12-27 08:12:38 +01:00
|
|
|
struct stat buf;
|
2024-02-28 06:14:19 +01:00
|
|
|
int ret = OK;
|
2015-01-25 21:16:44 +01:00
|
|
|
|
|
|
|
if (client->lc_state == LOCAL_STATE_ACCEPT ||
|
|
|
|
client->lc_state == LOCAL_STATE_CONNECTED)
|
|
|
|
{
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
2022-12-18 08:18:56 +01:00
|
|
|
if (unpath[0] == '\0')
|
|
|
|
{
|
|
|
|
type = LOCAL_TYPE_ABSTRACT;
|
|
|
|
unpath++;
|
|
|
|
}
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/* Find the matching server connection */
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2021-04-19 12:06:14 +02:00
|
|
|
while ((conn = local_nextconn(conn)) != NULL)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Self found, continue */
|
2022-05-30 08:38:24 +02:00
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
if (conn == client)
|
2022-05-30 08:38:24 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
/* Handle according to the server connection type */
|
2015-01-25 21:16:44 +01:00
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
switch (conn->lc_type)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
|
|
|
case LOCAL_TYPE_UNNAMED: /* A Unix socket that is not bound to any name */
|
2022-12-18 07:49:02 +01:00
|
|
|
break;
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
case LOCAL_TYPE_ABSTRACT: /* lc_path is length zero */
|
|
|
|
case LOCAL_TYPE_PATHNAME: /* lc_path holds a null terminated string */
|
2022-12-18 08:18:56 +01:00
|
|
|
|
|
|
|
/* Anything in the listener list should be a stream socket in the
|
|
|
|
* listening state
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->lc_state == LOCAL_STATE_LISTENING &&
|
|
|
|
conn->lc_type == type && conn->lc_proto == SOCK_STREAM &&
|
|
|
|
strncmp(conn->lc_path, unpath, UNIX_PATH_MAX - 1) == 0)
|
|
|
|
{
|
|
|
|
/* Bind the address and protocol */
|
|
|
|
|
|
|
|
client->lc_type = conn->lc_type;
|
|
|
|
client->lc_proto = conn->lc_proto;
|
|
|
|
strlcpy(client->lc_path, unpath, sizeof(client->lc_path));
|
|
|
|
client->lc_instance_id = local_generate_instance_id();
|
|
|
|
|
|
|
|
/* The client is now bound to an address */
|
|
|
|
|
|
|
|
client->lc_state = LOCAL_STATE_BOUND;
|
|
|
|
|
|
|
|
/* We have to do more for the SOCK_STREAM family */
|
|
|
|
|
2024-02-28 06:21:41 +01:00
|
|
|
ret = local_stream_connect(client, conn,
|
2022-12-18 08:18:56 +01:00
|
|
|
_SS_ISNONBLOCK(client->lc_conn.s_flags));
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
break;
|
|
|
|
|
2022-12-18 07:49:02 +01:00
|
|
|
default: /* Bad, memory must be corrupted */
|
|
|
|
DEBUGPANIC(); /* PANIC if debug on */
|
|
|
|
net_unlock();
|
|
|
|
return -EINVAL;
|
2015-01-25 21:16:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2022-12-27 08:12:38 +01:00
|
|
|
ret = nx_stat(unpath, &buf, 1);
|
|
|
|
return ret < 0 ? ret : -ECONNREFUSED;
|
2015-01-25 21:16:44 +01:00
|
|
|
}
|