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
|
|
|
*
|
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>
|
|
|
|
|
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"
|
|
|
|
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
{
|
|
|
|
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-10-08 23:10:04 +02:00
|
|
|
/* Increment the number of pending server connection s */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
server->u.server.lc_pending++;
|
|
|
|
DEBUGASSERT(server->u.server.lc_pending != 0);
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Create the FIFOs needed for the connection */
|
|
|
|
|
|
|
|
ret = local_create_fifos(client);
|
|
|
|
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);
|
2015-05-12 15:28:26 +02:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
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);
|
2015-05-12 15:28:26 +02:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
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
|
|
|
|
2019-03-19 16:52:40 +01:00
|
|
|
/* Set the busy "result" before giving the semaphore. */
|
|
|
|
|
|
|
|
client->u.client.lc_result = -EBUSY;
|
2021-08-13 14:53:38 +02:00
|
|
|
client->lc_state = LOCAL_STATE_ACCEPT;
|
2019-03-19 16:52:40 +01:00
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/* Add ourself to the list of waiting connections and notify the server. */
|
|
|
|
|
2021-09-16 07:40:02 +02:00
|
|
|
dq_addlast(&client->u.client.lc_waiter, &server->u.server.lc_waiters);
|
2021-08-13 14:53:38 +02:00
|
|
|
local_event_pollnotify(server, POLLIN);
|
2017-09-01 15:47:29 +02:00
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
if (nxsem_get_value(&server->lc_waitsem, &sval) >= 0 && sval < 1)
|
2017-09-01 15:47:29 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(&server->lc_waitsem);
|
2017-09-01 15:47:29 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/* Wait for the server to accept the connections */
|
|
|
|
|
2021-08-13 14:53:38 +02:00
|
|
|
if (!nonblock)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
2021-08-13 14:53:38 +02:00
|
|
|
do
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
net_lockedwait_uninterruptible(&client->lc_waitsem);
|
2021-08-13 14:53:38 +02:00
|
|
|
ret = client->u.client.lc_result;
|
|
|
|
}
|
|
|
|
while (ret == -EBUSY);
|
2015-01-25 21:16:44 +01:00
|
|
|
|
2021-08-13 14:53:38 +02:00
|
|
|
/* Did we successfully connect? */
|
2015-01-26 20:30:27 +01:00
|
|
|
|
2021-08-13 14:53:38 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to connect: %d\n", ret);
|
|
|
|
goto errout_with_outfd;
|
|
|
|
}
|
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)
|
|
|
|
{
|
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_outfd;
|
|
|
|
}
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
DEBUGASSERT(client->lc_infile.f_inode != NULL);
|
2021-08-13 14:53:38 +02:00
|
|
|
|
2022-05-27 18:24:35 +02:00
|
|
|
nxsem_post(&client->lc_donesem);
|
|
|
|
|
2021-08-13 14:53:38 +02:00
|
|
|
if (!nonblock)
|
|
|
|
{
|
|
|
|
client->lc_state = LOCAL_STATE_CONNECTED;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
client->lc_state = LOCAL_STATE_CONNECTING;
|
|
|
|
return -EINPROGRESS;
|
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
|
|
|
{
|
2015-01-30 19:43:37 +01:00
|
|
|
FAR struct local_conn_s *client;
|
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;
|
2015-01-25 21:16:44 +01:00
|
|
|
|
2015-01-30 19:43:37 +01:00
|
|
|
DEBUGASSERT(psock && psock->s_conn);
|
|
|
|
client = (FAR struct local_conn_s *)psock->s_conn;
|
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
|
|
|
{
|
2022-05-30 08:38:24 +02:00
|
|
|
/* Slef found, continue */
|
|
|
|
|
|
|
|
if (conn == psock->s_conn)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
if (conn->lc_proto == SOCK_STREAM)
|
|
|
|
{
|
|
|
|
ret = local_stream_connect(client, conn,
|
|
|
|
_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();
|
2015-01-25 21:16:44 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
|
|
|
}
|
|
|
|
|
2017-07-14 20:36:54 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|