net/local: fix the return address is incorrect when accept

The argument addr is a pointer to a sockaddr structure.
This structure is filled in with the address of the peer socket,
as known to the communications layer.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2024-09-29 15:32:39 +08:00 committed by Xiang Xiao
parent ec691cc5e4
commit e4129d7f70
6 changed files with 62 additions and 41 deletions

View file

@ -613,7 +613,8 @@ int local_release_halfduplex(FAR struct local_conn_s *conn);
*
****************************************************************************/
int local_open_client_rx(FAR struct local_conn_s *client, bool nonblock);
int local_open_client_rx(FAR struct local_conn_s *client,
FAR struct local_conn_s *server, bool nonblock);
/****************************************************************************
* Name: local_open_client_tx
@ -623,7 +624,8 @@ int local_open_client_rx(FAR struct local_conn_s *client, bool nonblock);
*
****************************************************************************/
int local_open_client_tx(FAR struct local_conn_s *client, bool nonblock);
int local_open_client_tx(FAR struct local_conn_s *client,
FAR struct local_conn_s *server, bool nonblock);
/****************************************************************************
* Name: local_open_server_rx

View file

@ -151,9 +151,9 @@ int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
/* Return the address family */
if (addr != NULL)
if (addr != NULL && conn->lc_peer != NULL)
{
ret = local_getaddr(conn, addr, addrlen);
ret = local_getaddr(conn->lc_peer, addr, addrlen);
}
if (ret == OK && nonblock)

View file

@ -219,9 +219,19 @@ int local_alloc_accept(FAR struct local_conn_s *server,
conn->lc_peer = client;
client->lc_peer = conn;
strlcpy(conn->lc_path, client->lc_path, sizeof(conn->lc_path));
strlcpy(conn->lc_path, server->lc_path, sizeof(conn->lc_path));
conn->lc_instance_id = client->lc_instance_id;
/* Create the FIFOs needed for the connection */
ret = local_create_fifos(conn, server->lc_rcvsize, client->lc_rcvsize);
if (ret < 0)
{
nerr("ERROR: Failed to create FIFOs for %s: %d\n",
client->lc_path, ret);
goto err;
}
/* Open the server-side write-only FIFO. This should not
* block.
*/
@ -231,7 +241,7 @@ int local_alloc_accept(FAR struct local_conn_s *server,
{
nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
conn->lc_path, ret);
goto err;
goto errout_with_fifos;
}
/* Do we have a connection? Is the write-side FIFO opened? */
@ -248,7 +258,7 @@ int local_alloc_accept(FAR struct local_conn_s *server,
{
nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
conn->lc_path, ret);
goto err;
goto errout_with_fifos;
}
/* Do we have a connection? Are the FIFOs opened? */
@ -257,6 +267,9 @@ int local_alloc_accept(FAR struct local_conn_s *server,
*accept = conn;
return OK;
errout_with_fifos:
local_release_fifos(conn);
err:
local_free(conn);
return ret;

View file

@ -88,12 +88,12 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
return -ECONNREFUSED;
}
/* Create the FIFOs needed for the connection */
ret = local_create_fifos(client, server->lc_rcvsize, client->lc_rcvsize);
net_lock();
ret = local_alloc_accept(server, client, &conn);
net_unlock();
if (ret < 0)
{
nerr("ERROR: Failed to create FIFOs for %s: %d\n",
nerr("ERROR: Failed to alloc accept conn %s: %d\n",
client->lc_path, ret);
return ret;
}
@ -102,36 +102,26 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
* prevent the server-side from blocking as well.
*/
ret = local_open_client_tx(client, nonblock);
ret = local_open_client_tx(client, conn, nonblock);
if (ret < 0)
{
nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
client->lc_path, ret);
goto errout_with_fifos;
goto errout_with_conn;
}
DEBUGASSERT(client->lc_outfile.f_inode != NULL);
net_lock();
ret = local_alloc_accept(server, client, &conn);
net_unlock();
if (ret < 0)
{
nerr("ERROR: Failed to alloc accept conn %s: %d\n",
client->lc_path, ret);
goto errout_with_outfd;
}
client->lc_state = LOCAL_STATE_ACCEPT;
/* Yes.. open the read-only FIFO */
ret = local_open_client_rx(client, nonblock);
ret = local_open_client_rx(client, conn, nonblock);
if (ret < 0)
{
nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
client->lc_path, ret);
goto errout_with_conn;
goto errout_with_outfd;
}
DEBUGASSERT(client->lc_infile.f_inode != NULL);
@ -154,18 +144,17 @@ static int inline local_stream_connect(FAR struct local_conn_s *client,
client->lc_state = LOCAL_STATE_CONNECTED;
return ret;
errout_with_conn:
net_lock();
local_free(conn);
net_unlock();
errout_with_outfd:
file_close(&client->lc_outfile);
client->lc_outfile.f_inode = NULL;
errout_with_fifos:
local_release_fifos(client);
errout_with_conn:
local_release_fifos(conn);
client->lc_state = LOCAL_STATE_BOUND;
net_lock();
local_free(conn);
net_unlock();
return ret;
}
@ -275,7 +264,6 @@ int psock_local_connect(FAR struct socket *psock,
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 */

View file

@ -237,10 +237,10 @@ static int local_release_fifo(FAR const char *path)
static int local_rx_open(FAR struct local_conn_s *conn, FAR const char *path,
bool nonblock)
{
int oflags = nonblock ? O_RDONLY | O_NONBLOCK : O_RDONLY;
int ret;
ret = file_open(&conn->lc_infile, path, oflags | O_CLOEXEC);
ret = file_open(&conn->lc_infile, path, O_RDONLY | O_NONBLOCK |
O_CLOEXEC);
if (ret < 0)
{
nerr("ERROR: Failed on open %s for reading: %d\n",
@ -257,6 +257,20 @@ static int local_rx_open(FAR struct local_conn_s *conn, FAR const char *path,
return ret == -ENOENT ? -EFAULT : ret;
}
/* Clear O_NONBLOCK if it's meant to be blocking */
if (nonblock == false)
{
ret = nonblock;
ret = file_ioctl(&conn->lc_infile, FIONBIO, &ret);
if (ret < 0)
{
file_close(&conn->lc_infile);
conn->lc_infile.f_inode = NULL;
return ret;
}
}
return OK;
}
@ -299,6 +313,8 @@ static int local_tx_open(FAR struct local_conn_s *conn, FAR const char *path,
ret = file_ioctl(&conn->lc_outfile, FIONBIO, &ret);
if (ret < 0)
{
file_close(&conn->lc_outfile);
conn->lc_outfile.f_inode = NULL;
return ret;
}
}
@ -543,14 +559,15 @@ int local_release_halfduplex(FAR struct local_conn_s *conn)
*
****************************************************************************/
int local_open_client_rx(FAR struct local_conn_s *client, bool nonblock)
int local_open_client_rx(FAR struct local_conn_s *client,
FAR struct local_conn_s *server, bool nonblock)
{
char path[LOCAL_FULLPATH_LEN];
int ret;
/* Get the server-to-client path name */
local_sc_name(client, path);
local_sc_name(server, path);
/* Then open the file for read-only access */
@ -573,14 +590,15 @@ int local_open_client_rx(FAR struct local_conn_s *client, bool nonblock)
*
****************************************************************************/
int local_open_client_tx(FAR struct local_conn_s *client, bool nonblock)
int local_open_client_tx(FAR struct local_conn_s *client,
FAR struct local_conn_s *server, bool nonblock)
{
char path[LOCAL_FULLPATH_LEN];
int ret;
/* Get the client-to-server path name */
local_cs_name(client, path);
local_cs_name(server, path);
/* Then open the file for write-only access */

View file

@ -1066,7 +1066,7 @@ static int local_socketpair(FAR struct socket *psocks[2])
/* Open the client-side write-only FIFO. */
ret = local_open_client_tx(conns[0], nonblock);
ret = local_open_client_tx(conns[0], conns[1], nonblock);
if (ret < 0)
{
goto errout;
@ -1090,7 +1090,7 @@ static int local_socketpair(FAR struct socket *psocks[2])
/* Open the client-side read-only FIFO */
ret = local_open_client_rx(conns[0], nonblock);
ret = local_open_client_rx(conns[0], conns[1], nonblock);
if (ret < 0)
{
goto errout;