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 <queue.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-12 15:47:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_generate_instance_id
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static 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
|
|
|
/****************************************************************************
|
|
|
|
* Name: _local_semtake() and _local_semgive()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take/give semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void _local_semtake(sem_t *sem)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
net_lockedwait_uninterruptible(sem);
|
2015-01-25 21:16:44 +01:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define _local_semgive(sem) nxsem_post(sem)
|
2015-01-25 21:16:44 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
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
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
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
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
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;
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/* Add ourself to the list of waiting connections and notify the server. */
|
|
|
|
|
|
|
|
dq_addlast(&client->lc_node, &server->u.server.lc_waiters);
|
|
|
|
client->lc_state = LOCAL_STATE_ACCEPT;
|
2015-05-12 15:41:12 +02:00
|
|
|
local_accept_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
|
|
|
{
|
|
|
|
_local_semgive(&server->lc_waitsem);
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-25 21:16:44 +01:00
|
|
|
|
|
|
|
/* Wait for the server to accept the connections */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
_local_semtake(&client->lc_waitsem);
|
|
|
|
ret = client->u.client.lc_result;
|
|
|
|
}
|
|
|
|
while (ret == -EBUSY);
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Did we successfully connect? */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to connect: %d\n", ret);
|
2015-01-26 20:30:27 +01:00
|
|
|
goto errout_with_outfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2015-01-26 20:30:27 +01:00
|
|
|
client->lc_state = LOCAL_STATE_CONNECTED;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
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;
|
2021-04-19 12:06:14 +02:00
|
|
|
FAR struct local_conn_s *conn = NULL;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2015-01-27 23:39:30 +01:00
|
|
|
/* Anything in the listener list should be a stream socket in the
|
2021-02-25 13:48:46 +01:00
|
|
|
* listening state
|
2015-01-25 21:16:44 +01:00
|
|
|
*/
|
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
DEBUGASSERT(conn->lc_state == LOCAL_STATE_LISTENING &&
|
|
|
|
conn->lc_proto == SOCK_STREAM);
|
2015-01-25 21:16:44 +01:00
|
|
|
|
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 */
|
|
|
|
case LOCAL_TYPE_ABSTRACT: /* lc_path is length zero */
|
|
|
|
{
|
|
|
|
#warning Missing logic
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-25 21:16:44 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOCAL_TYPE_PATHNAME: /* lc_path holds a null terminated string */
|
|
|
|
{
|
2019-03-11 19:48:17 +01:00
|
|
|
if (strncmp(conn->lc_path, unaddr->sun_path, UNIX_PATH_MAX - 1)
|
|
|
|
== 0)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
2015-01-28 15:39:48 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
/* Bind the address and protocol */
|
|
|
|
|
|
|
|
client->lc_proto = conn->lc_proto;
|
2020-05-17 16:47:40 +02:00
|
|
|
strncpy(client->lc_path, unaddr->sun_path,
|
|
|
|
UNIX_PATH_MAX - 1);
|
2019-03-11 19:48:17 +01:00
|
|
|
client->lc_path[UNIX_PATH_MAX - 1] = '\0';
|
2015-05-12 15:47:32 +02:00
|
|
|
client->lc_instance_id = local_generate_instance_id();
|
2015-01-27 23:39:30 +01:00
|
|
|
|
2015-01-28 15:39:48 +01:00
|
|
|
/* The client is now bound to an address */
|
|
|
|
|
|
|
|
client->lc_state = LOCAL_STATE_BOUND;
|
|
|
|
|
2015-01-25 21:16:44 +01:00
|
|
|
/* We have to do more for the SOCK_STREAM family */
|
|
|
|
|
2015-01-27 23:39:30 +01:00
|
|
|
if (conn->lc_proto == SOCK_STREAM)
|
2015-01-25 21:16:44 +01:00
|
|
|
{
|
2020-05-17 16:47:40 +02:00
|
|
|
ret =
|
|
|
|
local_stream_connect(client, conn,
|
|
|
|
_SS_ISNONBLOCK(psock->s_flags));
|
2015-01-25 21:16:44 +01:00
|
|
|
}
|
2015-05-12 15:28:26 +02:00
|
|
|
else
|
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-05-12 15:28:26 +02:00
|
|
|
}
|
2015-01-25 21:16:44 +01:00
|
|
|
|
2015-01-28 15:39:48 +01:00
|
|
|
return ret;
|
2015-01-25 21:16:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* Bad, memory must be corrupted */
|
|
|
|
DEBUGPANIC(); /* PANIC if debug on, else fall through */
|
|
|
|
|
|
|
|
case LOCAL_TYPE_UNTYPED: /* Type is not determined until the socket is bound */
|
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-25 21:16:44 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 */
|