2017-07-12 23:07:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_sockif.c
|
|
|
|
*
|
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
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-12 23:07:32 +02: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.
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2017-07-14 18:57:38 +02:00
|
|
|
#include <stdbool.h>
|
2021-06-08 16:16:56 +02:00
|
|
|
#include <stdio.h>
|
2017-07-14 19:34:34 +02:00
|
|
|
#include <string.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2021-06-07 12:06:44 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2017-07-14 14:02:49 +02:00
|
|
|
#include <socket/socket.h>
|
2017-07-12 23:07:32 +02:00
|
|
|
|
|
|
|
#include "local/local.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int local_setup(FAR struct socket *psock);
|
2017-07-13 21:36:18 +02:00
|
|
|
static sockcaps_t local_sockcaps(FAR struct socket *psock);
|
2023-09-22 07:33:42 +02:00
|
|
|
static void local_sockaddref(FAR struct socket *psock);
|
2017-07-13 21:36:18 +02:00
|
|
|
static int local_bind(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
2017-07-14 17:04:19 +02:00
|
|
|
static int local_getsockname(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen);
|
2018-07-19 15:16:30 +02:00
|
|
|
static int local_getpeername(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen);
|
2017-07-13 21:36:18 +02:00
|
|
|
static int local_connect(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen);
|
2017-07-14 18:57:38 +02:00
|
|
|
static int local_poll(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
FAR struct pollfd *fds, bool setup);
|
2017-07-13 21:36:18 +02:00
|
|
|
static int local_close(FAR struct socket *psock);
|
2022-08-30 20:18:33 +02:00
|
|
|
static int local_ioctl(FAR struct socket *psock,
|
2023-11-19 12:19:53 +01:00
|
|
|
int cmd, unsigned long arg);
|
2021-06-08 16:16:56 +02:00
|
|
|
static int local_socketpair(FAR struct socket *psocks[2]);
|
2023-01-18 11:28:32 +01:00
|
|
|
static int local_shutdown(FAR struct socket *psock, int how);
|
2022-11-22 14:18:12 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
static int local_getsockopt(FAR struct socket *psock, int level,
|
2023-11-19 12:19:53 +01:00
|
|
|
int option, FAR void *value,
|
|
|
|
FAR socklen_t *value_len);
|
2022-11-22 14:18:12 +01:00
|
|
|
static int local_setsockopt(FAR struct socket *psock, int level,
|
2023-11-19 12:19:53 +01:00
|
|
|
int option, FAR const void *value,
|
|
|
|
socklen_t value_len);
|
2022-11-22 14:18:12 +01:00
|
|
|
#endif
|
2017-07-12 23:07:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const struct sock_intf_s g_local_sockif =
|
|
|
|
{
|
2017-07-14 17:04:19 +02:00
|
|
|
local_setup, /* si_setup */
|
|
|
|
local_sockcaps, /* si_sockcaps */
|
2023-09-22 07:33:42 +02:00
|
|
|
local_sockaddref, /* si_addref */
|
2017-07-14 17:04:19 +02:00
|
|
|
local_bind, /* si_bind */
|
|
|
|
local_getsockname, /* si_getsockname */
|
2018-07-19 15:16:30 +02:00
|
|
|
local_getpeername, /* si_getpeername */
|
2023-03-06 06:08:24 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-07-14 17:04:19 +02:00
|
|
|
local_listen, /* si_listen */
|
2023-03-06 06:08:24 +01:00
|
|
|
#else
|
|
|
|
NULL, /* si_listen */
|
|
|
|
#endif
|
2017-07-14 17:04:19 +02:00
|
|
|
local_connect, /* si_connect */
|
2023-03-06 06:16:26 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-07-14 17:04:19 +02:00
|
|
|
local_accept, /* si_accept */
|
2023-03-06 06:16:26 +01:00
|
|
|
#else
|
|
|
|
NULL, /* si_accept */
|
|
|
|
#endif
|
2017-07-14 18:57:38 +02:00
|
|
|
local_poll, /* si_poll */
|
2020-12-01 07:55:16 +01:00
|
|
|
local_sendmsg, /* si_sendmsg */
|
|
|
|
local_recvmsg, /* si_recvmsg */
|
2021-06-07 12:06:44 +02:00
|
|
|
local_close, /* si_close */
|
2021-06-08 16:16:56 +02:00
|
|
|
local_ioctl, /* si_ioctl */
|
2023-01-18 11:28:32 +01:00
|
|
|
local_socketpair, /* si_socketpair */
|
|
|
|
local_shutdown /* si_shutdown */
|
2022-11-22 14:18:12 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
, local_getsockopt /* si_getsockopt */
|
|
|
|
, local_setsockopt /* si_setsockopt */
|
|
|
|
#endif
|
2017-07-12 23:07:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_sockif_alloc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate and attach a local, Unix domain connection structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_sockif_alloc(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
/* Allocate the local connection structure */
|
|
|
|
|
2024-06-05 09:29:04 +02:00
|
|
|
FAR struct local_conn_s *conn;
|
|
|
|
net_lock();
|
|
|
|
conn = local_alloc();
|
|
|
|
net_unlock();
|
2017-07-12 23:07:32 +02:00
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to reserve a connection structure */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the pre-allocated connection in the socket structure */
|
|
|
|
|
2024-04-18 12:40:47 +02:00
|
|
|
conn->lc_proto = psock->s_type;
|
2017-07-12 23:07:32 +02:00
|
|
|
psock->s_conn = conn;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called for socket() to verify that the provided socket type and
|
|
|
|
* protocol are usable by this address family. Perform any family-
|
|
|
|
* specific socket fields.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2020-06-15 10:23:25 +02:00
|
|
|
* psock A pointer to a user allocated socket structure
|
|
|
|
* to be initialized.
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-14 14:55:45 +02:00
|
|
|
* Zero (OK) is returned on success. Otherwise, a negated errno value is
|
2017-07-12 23:07:32 +02:00
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
static int local_setup(FAR struct socket *psock)
|
2017-07-12 23:07:32 +02:00
|
|
|
{
|
|
|
|
/* Allocate the appropriate connection structure. This reserves the
|
2021-01-15 10:01:00 +01:00
|
|
|
* connection structure, it is unallocated at this point. It will not
|
|
|
|
* actually be initialized until the socket is connected.
|
2017-07-12 23:07:32 +02:00
|
|
|
*
|
2021-12-20 15:49:48 +01:00
|
|
|
* REVISIT: Only SOCK_STREAM and SOCK_DGRAM are supported. Should also
|
2018-06-23 14:20:25 +02:00
|
|
|
* support SOCK_RAW.
|
2017-07-12 23:07:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-07-12 23:07:32 +02:00
|
|
|
case SOCK_STREAM:
|
2023-02-06 18:55:44 +01:00
|
|
|
if (psock->s_proto != 0 && psock->s_proto != IPPROTO_TCP)
|
2017-07-12 23:07:32 +02:00
|
|
|
{
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and attach the local connection structure */
|
|
|
|
|
|
|
|
return local_sockif_alloc(psock);
|
2017-07-14 20:36:54 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
2017-07-12 23:07:32 +02:00
|
|
|
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2017-07-12 23:07:32 +02:00
|
|
|
case SOCK_DGRAM:
|
2023-02-06 18:55:44 +01:00
|
|
|
if (psock->s_proto != 0 && psock->s_proto != IPPROTO_UDP)
|
2017-07-12 23:07:32 +02:00
|
|
|
{
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and attach the local connection structure */
|
|
|
|
|
|
|
|
return local_sockif_alloc(psock);
|
2017-07-14 20:36:54 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2017-07-12 23:07:32 +02:00
|
|
|
|
2023-01-18 06:42:42 +01:00
|
|
|
case SOCK_CTRL:
|
|
|
|
if (psock->s_proto == 0 || psock->s_proto == IPPROTO_TCP ||
|
|
|
|
psock->s_proto == IPPROTO_UDP)
|
|
|
|
{
|
|
|
|
/* Allocate and attach the local connection structure */
|
|
|
|
|
|
|
|
return local_sockif_alloc(psock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
default:
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:36:18 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_sockcaps
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the bit encoded capabilities of this socket.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 21:36:18 +02:00
|
|
|
* psock - Socket structure of the socket whose capabilities are being
|
|
|
|
* queried.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The set of socket cababilities is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static sockcaps_t local_sockcaps(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
return SOCKCAP_NONBLOCKING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2023-09-22 07:33:42 +02:00
|
|
|
* Name: local_sockaddref
|
2017-07-13 21:36:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-02-22 19:31:14 +01:00
|
|
|
* Increment the reference count on the underlying connection structure.
|
2017-07-13 21:36:18 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 21:36:18 +02:00
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-22 07:33:42 +02:00
|
|
|
static void local_sockaddref(FAR struct socket *psock)
|
2017-07-13 21:36:18 +02:00
|
|
|
{
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(psock->s_domain == PF_LOCAL);
|
2023-09-22 07:33:42 +02:00
|
|
|
local_addref(psock->s_conn);
|
2017-07-13 21:36:18 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* local_bind() gives the socket 'psock' the local address 'addr'. 'addr'
|
|
|
|
* is 'addrlen' bytes long. Traditionally, this is called "assigning a
|
|
|
|
* name to a socket." When a socket is created with socket(), it exists
|
|
|
|
* in a name space (address family) but has no name assigned.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 17:27:56 +02:00
|
|
|
* psock Socket structure of the socket to bind
|
|
|
|
* addr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; A negated errno value is returned on failure. See
|
|
|
|
* bind() for a list a appropriate error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_bind(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
2024-02-28 06:14:19 +01:00
|
|
|
if (addrlen < sizeof(sa_family_t) || addr->sa_family != AF_LOCAL)
|
2017-07-13 17:27:56 +02:00
|
|
|
{
|
2020-11-20 03:59:06 +01:00
|
|
|
nerr("ERROR: Invalid address length: %d < %zu\n",
|
2017-07-13 17:27:56 +02:00
|
|
|
addrlen, sizeof(sa_family_t));
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the binding depending on the protocol type */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
|
|
|
/* Bind a local TCP/IP stream or datagram socket */
|
|
|
|
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-07-13 17:27:56 +02:00
|
|
|
case SOCK_STREAM:
|
|
|
|
#endif
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2017-07-13 17:27:56 +02:00
|
|
|
case SOCK_DGRAM:
|
|
|
|
#endif
|
2023-01-18 06:42:42 +01:00
|
|
|
case SOCK_CTRL:
|
2017-07-13 17:27:56 +02:00
|
|
|
{
|
|
|
|
/* Bind the Unix domain connection structure */
|
|
|
|
|
2017-07-14 14:02:49 +02:00
|
|
|
ret = psock_local_bind(psock, addr, addrlen);
|
2017-07-13 17:27:56 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -EBADF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-14 17:04:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_getsockname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The local_getsockname() function retrieves the locally-bound name of
|
|
|
|
* the specified local socket, stores this address in the sockaddr
|
|
|
|
* structure pointed to by the 'addr' argument, and stores the length of
|
|
|
|
* this address in the object pointed to by the 'addrlen' argument.
|
|
|
|
*
|
|
|
|
* If the actual length of the address is greater than the length of the
|
|
|
|
* supplied sockaddr structure, the stored address will be truncated.
|
|
|
|
*
|
|
|
|
* If the socket has not been bound to a local name, the value stored in
|
|
|
|
* the object pointed to by address is unspecified.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-14 17:04:19 +02:00
|
|
|
* psock Socket structure of the socket to be queried
|
|
|
|
* addr sockaddr structure to receive data [out]
|
|
|
|
* addrlen Length of sockaddr structure [in/out]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, 0 is returned, the 'addr' argument points to the address
|
|
|
|
* of the socket, and the 'addrlen' argument points to the length of the
|
|
|
|
* address. Otherwise, a negated errno value is returned. See
|
|
|
|
* getsockname() for the list of appropriate error numbers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_getsockname(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
2017-07-14 19:34:34 +02:00
|
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)addr;
|
2024-02-28 06:14:19 +01:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2017-07-14 17:04:19 +02:00
|
|
|
|
|
|
|
if (*addrlen < sizeof(sa_family_t))
|
|
|
|
{
|
|
|
|
/* This is apparently not an error */
|
|
|
|
|
|
|
|
*addrlen = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the address family */
|
|
|
|
|
|
|
|
unaddr->sun_family = AF_LOCAL;
|
|
|
|
if (*addrlen > sizeof(sa_family_t))
|
|
|
|
{
|
|
|
|
/* Now copy the address description. */
|
|
|
|
|
|
|
|
if (conn->lc_type == LOCAL_TYPE_UNNAMED)
|
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Zero-length sun_path... */
|
2017-07-14 17:04:19 +02:00
|
|
|
|
|
|
|
*addrlen = sizeof(sa_family_t);
|
|
|
|
}
|
2024-02-28 06:14:19 +01:00
|
|
|
else
|
2017-07-14 17:04:19 +02:00
|
|
|
{
|
2020-06-15 14:09:26 +02:00
|
|
|
/* Get the full length of the socket name (incl. null terminator) */
|
2017-07-14 19:34:34 +02:00
|
|
|
|
2022-12-18 08:18:56 +01:00
|
|
|
size_t namelen = strlen(conn->lc_path) + 1 +
|
|
|
|
(conn->lc_type == LOCAL_TYPE_ABSTRACT);
|
2017-07-14 19:34:34 +02:00
|
|
|
|
2017-07-14 17:04:19 +02:00
|
|
|
/* Get the available length in the user-provided buffer. */
|
|
|
|
|
2022-12-18 08:18:56 +01:00
|
|
|
size_t pathlen = *addrlen - sizeof(sa_family_t);
|
2017-07-14 17:04:19 +02:00
|
|
|
|
2017-07-14 19:34:34 +02:00
|
|
|
/* Clip the socket name size so that if fits in the user buffer */
|
|
|
|
|
|
|
|
if (pathlen < namelen)
|
|
|
|
{
|
|
|
|
namelen = pathlen;
|
|
|
|
}
|
|
|
|
|
2017-07-14 17:04:19 +02:00
|
|
|
/* Copy the path into the user address structure */
|
|
|
|
|
2022-12-18 08:18:56 +01:00
|
|
|
if (conn->lc_type == LOCAL_TYPE_ABSTRACT)
|
|
|
|
{
|
|
|
|
unaddr->sun_path[0] = '\0';
|
|
|
|
strlcpy(&unaddr->sun_path[1], conn->lc_path, namelen - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strlcpy(unaddr->sun_path, conn->lc_path, namelen);
|
|
|
|
}
|
2017-07-14 17:04:19 +02:00
|
|
|
|
|
|
|
*addrlen = sizeof(sa_family_t) + namelen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-07-19 15:16:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_getpeername
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The local_getpeername() function retrieves the remote-connected name of
|
|
|
|
* the specified local socket, stores this address in the sockaddr
|
|
|
|
* structure pointed to by the 'addr' argument, and stores the length of
|
|
|
|
* this address in the object pointed to by the 'addrlen' argument.
|
|
|
|
*
|
|
|
|
* If the actual length of the address is greater than the length of the
|
|
|
|
* supplied sockaddr structure, the stored address will be truncated.
|
|
|
|
*
|
|
|
|
* If the socket has not been bound to a local name, the value stored in
|
|
|
|
* the object pointed to by address is unspecified.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock Socket structure of the socket to be queried
|
|
|
|
* addr sockaddr structure to receive data [out]
|
|
|
|
* addrlen Length of sockaddr structure [in/out]
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, 0 is returned, the 'addr' argument points to the address
|
|
|
|
* of the socket, and the 'addrlen' argument points to the length of the
|
|
|
|
* address. Otherwise, a negated errno value is returned. See
|
|
|
|
* getpeername() for the list of appropriate error numbers.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_getpeername(FAR struct socket *psock,
|
|
|
|
FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
net/local/local_sockif.c:add local_getpeername function implementation
this testcast
TEST_IMPL(udp_send_unix) {
/* Test that "uv_udp_send()" supports sending over
a "sockaddr_un" address. */
struct sockaddr_un addr;
uv_udp_t handle;
uv_udp_send_t req;
uv_loop_t* loop;
uv_buf_t buf = uv_buf_init("PING", 4);
int fd;
int r;
loop = uv_default_loop();
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT(fd >= 0);
unlink(TEST_PIPENAME);
ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
ASSERT(0 == listen(fd, 1));
r = uv_udp_init(loop, &handle);
ASSERT(r == 0);
r = uv_udp_open(&handle, fd);
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_udp_send(&req,
&handle,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
uv_run(loop, UV_RUN_DEFAULT);
close(fd);
unlink(TEST_PIPENAME);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-03 05:28:28 +02:00
|
|
|
FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)addr;
|
2024-02-28 06:14:19 +01:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
|
|
|
FAR struct local_conn_s *peer = conn->lc_peer;
|
net/local/local_sockif.c:add local_getpeername function implementation
this testcast
TEST_IMPL(udp_send_unix) {
/* Test that "uv_udp_send()" supports sending over
a "sockaddr_un" address. */
struct sockaddr_un addr;
uv_udp_t handle;
uv_udp_send_t req;
uv_loop_t* loop;
uv_buf_t buf = uv_buf_init("PING", 4);
int fd;
int r;
loop = uv_default_loop();
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT(fd >= 0);
unlink(TEST_PIPENAME);
ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
ASSERT(0 == listen(fd, 1));
r = uv_udp_init(loop, &handle);
ASSERT(r == 0);
r = uv_udp_open(&handle, fd);
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_udp_send(&req,
&handle,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
uv_run(loop, UV_RUN_DEFAULT);
close(fd);
unlink(TEST_PIPENAME);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-03 05:28:28 +02:00
|
|
|
|
|
|
|
if (*addrlen < sizeof(sa_family_t))
|
|
|
|
{
|
|
|
|
/* This is apparently not an error */
|
|
|
|
|
|
|
|
*addrlen = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the socket has been connected */
|
|
|
|
|
|
|
|
if (conn->lc_state != LOCAL_STATE_CONNECTED)
|
|
|
|
{
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the address family */
|
|
|
|
|
|
|
|
unaddr->sun_family = AF_LOCAL;
|
|
|
|
if (*addrlen > sizeof(sa_family_t))
|
|
|
|
{
|
|
|
|
/* Now copy the address description. */
|
|
|
|
|
|
|
|
if (peer->lc_type == LOCAL_TYPE_UNNAMED)
|
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
/* Zero-length sun_path... */
|
net/local/local_sockif.c:add local_getpeername function implementation
this testcast
TEST_IMPL(udp_send_unix) {
/* Test that "uv_udp_send()" supports sending over
a "sockaddr_un" address. */
struct sockaddr_un addr;
uv_udp_t handle;
uv_udp_send_t req;
uv_loop_t* loop;
uv_buf_t buf = uv_buf_init("PING", 4);
int fd;
int r;
loop = uv_default_loop();
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT(fd >= 0);
unlink(TEST_PIPENAME);
ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
ASSERT(0 == listen(fd, 1));
r = uv_udp_init(loop, &handle);
ASSERT(r == 0);
r = uv_udp_open(&handle, fd);
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_udp_send(&req,
&handle,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
uv_run(loop, UV_RUN_DEFAULT);
close(fd);
unlink(TEST_PIPENAME);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-03 05:28:28 +02:00
|
|
|
|
|
|
|
*addrlen = sizeof(sa_family_t);
|
|
|
|
}
|
2024-02-28 06:14:19 +01:00
|
|
|
else
|
net/local/local_sockif.c:add local_getpeername function implementation
this testcast
TEST_IMPL(udp_send_unix) {
/* Test that "uv_udp_send()" supports sending over
a "sockaddr_un" address. */
struct sockaddr_un addr;
uv_udp_t handle;
uv_udp_send_t req;
uv_loop_t* loop;
uv_buf_t buf = uv_buf_init("PING", 4);
int fd;
int r;
loop = uv_default_loop();
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT(fd >= 0);
unlink(TEST_PIPENAME);
ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
ASSERT(0 == listen(fd, 1));
r = uv_udp_init(loop, &handle);
ASSERT(r == 0);
r = uv_udp_open(&handle, fd);
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_udp_send(&req,
&handle,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
uv_run(loop, UV_RUN_DEFAULT);
close(fd);
unlink(TEST_PIPENAME);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-03 05:28:28 +02:00
|
|
|
{
|
|
|
|
/* Get the full length of the socket name (incl. null terminator) */
|
|
|
|
|
|
|
|
size_t namelen = strlen(peer->lc_path) + 1 +
|
|
|
|
(peer->lc_type == LOCAL_TYPE_ABSTRACT);
|
|
|
|
|
|
|
|
/* Get the available length in the user-provided buffer. */
|
|
|
|
|
|
|
|
size_t pathlen = *addrlen - sizeof(sa_family_t);
|
|
|
|
|
|
|
|
/* Clip the socket name size so that if fits in the user buffer */
|
|
|
|
|
|
|
|
if (pathlen < namelen)
|
|
|
|
{
|
|
|
|
namelen = pathlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the path into the user address structure */
|
|
|
|
|
|
|
|
if (peer->lc_type == LOCAL_TYPE_ABSTRACT)
|
|
|
|
{
|
|
|
|
unaddr->sun_path[0] = '\0';
|
2024-02-28 06:14:19 +01:00
|
|
|
strlcpy(&unaddr->sun_path[1], peer->lc_path, namelen - 1);
|
net/local/local_sockif.c:add local_getpeername function implementation
this testcast
TEST_IMPL(udp_send_unix) {
/* Test that "uv_udp_send()" supports sending over
a "sockaddr_un" address. */
struct sockaddr_un addr;
uv_udp_t handle;
uv_udp_send_t req;
uv_loop_t* loop;
uv_buf_t buf = uv_buf_init("PING", 4);
int fd;
int r;
loop = uv_default_loop();
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT(fd >= 0);
unlink(TEST_PIPENAME);
ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
ASSERT(0 == listen(fd, 1));
r = uv_udp_init(loop, &handle);
ASSERT(r == 0);
r = uv_udp_open(&handle, fd);
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_udp_send(&req,
&handle,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
uv_run(loop, UV_RUN_DEFAULT);
close(fd);
unlink(TEST_PIPENAME);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-03 05:28:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
strlcpy(unaddr->sun_path, peer->lc_path, namelen);
|
net/local/local_sockif.c:add local_getpeername function implementation
this testcast
TEST_IMPL(udp_send_unix) {
/* Test that "uv_udp_send()" supports sending over
a "sockaddr_un" address. */
struct sockaddr_un addr;
uv_udp_t handle;
uv_udp_send_t req;
uv_loop_t* loop;
uv_buf_t buf = uv_buf_init("PING", 4);
int fd;
int r;
loop = uv_default_loop();
memset(&addr, 0, sizeof addr);
addr.sun_family = AF_UNIX;
ASSERT(strlen(TEST_PIPENAME) < sizeof(addr.sun_path));
memcpy(addr.sun_path, TEST_PIPENAME, strlen(TEST_PIPENAME));
fd = socket(AF_UNIX, SOCK_STREAM, 0);
ASSERT(fd >= 0);
unlink(TEST_PIPENAME);
ASSERT(0 == bind(fd, (const struct sockaddr*)&addr, sizeof addr));
ASSERT(0 == listen(fd, 1));
r = uv_udp_init(loop, &handle);
ASSERT(r == 0);
r = uv_udp_open(&handle, fd);
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
r = uv_udp_send(&req,
&handle,
&buf,
1,
(const struct sockaddr*) &addr,
NULL);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
uv_run(loop, UV_RUN_DEFAULT);
close(fd);
unlink(TEST_PIPENAME);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-03 05:28:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*addrlen = sizeof(sa_family_t) + namelen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2018-07-19 15:16:30 +02:00
|
|
|
}
|
|
|
|
|
2022-11-22 14:18:12 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_getsockopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* local_getsockopt() retrieve the value for the option specified by the
|
|
|
|
* 'option' argument at the protocol level specified by the 'level'
|
|
|
|
* argument. If the size of the option value is greater than 'value_len',
|
|
|
|
* the value stored in the object pointed to by the 'value' argument will
|
|
|
|
* be silently truncated. Otherwise, the length pointed to by the
|
|
|
|
* 'value_len' argument will be modified to indicate the actual length
|
|
|
|
* of the 'value'.
|
|
|
|
*
|
|
|
|
* The 'level' argument specifies the protocol level of the option. To
|
|
|
|
* retrieve options at the socket level, specify the level argument as
|
|
|
|
* SOL_SOCKET.
|
|
|
|
*
|
|
|
|
* See <sys/socket.h> a complete list of values for the 'option' argument.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of the socket to query
|
|
|
|
* level Protocol level to set the option
|
|
|
|
* option identifies the option to get
|
|
|
|
* value Points to the argument value
|
|
|
|
* value_len The length of the argument value
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_getsockopt(FAR struct socket *psock, int level, int option,
|
|
|
|
FAR void *value, FAR socklen_t *value_len)
|
|
|
|
{
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(psock->s_domain == PF_LOCAL);
|
2022-11-22 14:18:12 +01:00
|
|
|
|
2023-10-24 11:41:14 +02:00
|
|
|
if (level == SOL_SOCKET)
|
2022-11-22 14:18:12 +01:00
|
|
|
{
|
2023-10-24 11:41:14 +02:00
|
|
|
switch (option)
|
2022-11-22 14:18:12 +01:00
|
|
|
{
|
2023-10-24 11:41:14 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_SCM
|
|
|
|
case SO_PEERCRED:
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
|
|
|
if (*value_len != sizeof(struct ucred))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(value, &conn->lc_peer->lc_cred, sizeof(struct ucred));
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
2022-11-22 14:18:12 +01:00
|
|
|
|
2023-10-24 11:41:14 +02:00
|
|
|
case SO_SNDBUF:
|
|
|
|
{
|
|
|
|
if (*value_len != sizeof(int))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(FAR int *)value = LOCAL_SEND_LIMIT;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 14:18:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_setsockopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* local_setsockopt() sets the option specified by the 'option' argument,
|
|
|
|
* at the protocol level specified by the 'level' argument, to the value
|
|
|
|
* pointed to by the 'value' argument for the usrsock connection.
|
|
|
|
*
|
|
|
|
* The 'level' argument specifies the protocol level of the option. To set
|
|
|
|
* options at the socket level, specify the level argument as SOL_SOCKET.
|
|
|
|
*
|
|
|
|
* See <sys/socket.h> a complete list of values for the 'option' argument.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of the socket to query
|
|
|
|
* level Protocol level to set the option
|
|
|
|
* option identifies the option to set
|
|
|
|
* value Points to the argument value
|
|
|
|
* value_len The length of the argument value
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_setsockopt(FAR struct socket *psock, int level, int option,
|
|
|
|
FAR const void *value, socklen_t value_len)
|
|
|
|
{
|
|
|
|
return -ENOPROTOOPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* local_connect() connects the local socket referred to by the structure
|
|
|
|
* 'psock' to the address specified by 'addr'. The addrlen argument
|
|
|
|
* specifies the size of 'addr'. The format of the address in 'addr' is
|
|
|
|
* determined by the address space of the socket 'psock'.
|
|
|
|
*
|
|
|
|
* If the socket 'psock' is of type SOCK_DGRAM then 'addr' is the address
|
|
|
|
* to which datagrams are sent by default, and the only address from which
|
|
|
|
* datagrams are received. If the socket is of type SOCK_STREAM or
|
|
|
|
* SOCK_SEQPACKET, this call attempts to make a connection to the socket
|
|
|
|
* that is bound to the address specified by 'addr'.
|
|
|
|
*
|
|
|
|
* Generally, connection-based protocol sockets may successfully
|
|
|
|
* local_connect() only once; connectionless protocol sockets may use
|
|
|
|
* local_connect() multiple times to change their association.
|
|
|
|
* Connectionless sockets may dissolve the association by connecting to
|
|
|
|
* an address with the sa_family member of sockaddr set to AF_UNSPEC.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 17:27:56 +02:00
|
|
|
* psock Pointer to a socket structure initialized by psock_socket()
|
|
|
|
* addr Server address (form depends on type of socket)
|
|
|
|
* addrlen Length of actual 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-02-23 09:50:23 +01:00
|
|
|
* 0 on success; a negated errno value on failure. See connect() for the
|
2017-07-13 17:27:56 +02:00
|
|
|
* list of appropriate errno values to be returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_connect(FAR struct socket *psock,
|
|
|
|
FAR const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
|
|
|
if (addr->sa_family != AF_LOCAL || addrlen < sizeof(sa_family_t))
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the connection depending on the protocol type */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-07-13 17:27:56 +02:00
|
|
|
case SOCK_STREAM:
|
|
|
|
{
|
2022-02-08 06:18:01 +01:00
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
/* Verify that the socket is not already connected */
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
if (_SS_ISCONNECTED(conn->s_flags))
|
2017-07-13 17:27:56 +02:00
|
|
|
{
|
|
|
|
return -EISCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's not... Connect to the local Unix domain server */
|
|
|
|
|
|
|
|
return psock_local_connect(psock, addr);
|
|
|
|
}
|
|
|
|
break;
|
2017-07-14 20:36:54 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_STREAM */
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2017-07-13 17:27:56 +02:00
|
|
|
case SOCK_DGRAM:
|
|
|
|
{
|
|
|
|
/* Perform the datagram connection logic */
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-07-14 20:36:54 +02:00
|
|
|
return -ENOSYS;
|
2017-07-13 17:27:56 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-07-14 20:36:54 +02:00
|
|
|
#endif /* CONFIG_NET_LOCAL_DGRAM */
|
2017-07-13 17:27:56 +02:00
|
|
|
|
2023-01-18 06:42:42 +01:00
|
|
|
case SOCK_CTRL:
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
default:
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:57:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_poll
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on socket descriptors
|
|
|
|
* to local_poll which, indiectly, calls to function.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_poll(FAR struct socket *psock, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
/* Check if we are setting up or tearing down the poll */
|
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Perform the TCP/IP poll() setup */
|
|
|
|
|
|
|
|
return local_pollsetup(psock, fds);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Perform the TCP/IP poll() teardown */
|
|
|
|
|
2017-07-22 17:00:36 +02:00
|
|
|
return local_pollteardown(psock, fds);
|
2017-07-14 18:57:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 20:15:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on a local, Unix socket instance
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-13 20:15:15 +02:00
|
|
|
* psock Socket instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; a negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
/* Perform some pre-close operations for the local address type */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2017-07-13 20:15:15 +02:00
|
|
|
case SOCK_STREAM:
|
|
|
|
#endif
|
2017-07-14 20:36:54 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
2017-07-13 20:15:15 +02:00
|
|
|
case SOCK_DGRAM:
|
|
|
|
#endif
|
2023-01-18 06:42:42 +01:00
|
|
|
case SOCK_CTRL:
|
2017-07-13 20:15:15 +02:00
|
|
|
{
|
|
|
|
/* Is this the last reference to the connection structure (there
|
|
|
|
* could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
2023-09-22 07:33:42 +02:00
|
|
|
local_subref(psock->s_conn);
|
2017-07-13 20:15:15 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 12:06:44 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_ioctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function performs local device specific operations.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock A reference to the socket structure of the socket
|
|
|
|
* cmd The ioctl command
|
|
|
|
* arg The argument of the ioctl cmd
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-30 20:18:33 +02:00
|
|
|
static int local_ioctl(FAR struct socket *psock, int cmd, unsigned long arg)
|
2021-06-07 12:06:44 +02:00
|
|
|
{
|
2024-02-28 06:14:19 +01:00
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
2021-06-07 12:06:44 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2021-12-08 16:27:39 +01:00
|
|
|
case FIONBIO:
|
|
|
|
if (conn->lc_infile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&conn->lc_infile, cmd, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0 && conn->lc_outfile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&conn->lc_outfile, cmd, arg);
|
|
|
|
}
|
|
|
|
break;
|
2021-06-07 12:06:44 +02:00
|
|
|
case FIONREAD:
|
|
|
|
if (conn->lc_infile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&conn->lc_infile, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
break;
|
2022-11-24 08:26:27 +01:00
|
|
|
case FIONWRITE:
|
2021-08-12 08:48:05 +02:00
|
|
|
case FIONSPACE:
|
|
|
|
if (conn->lc_outfile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&conn->lc_outfile, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-21 06:02:23 +02:00
|
|
|
case PIPEIOC_POLLINTHRD:
|
|
|
|
if (conn->lc_infile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&conn->lc_infile, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PIPEIOC_POLLOUTTHRD:
|
|
|
|
if (conn->lc_outfile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
ret = file_ioctl(&conn->lc_outfile, cmd, arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOTCONN;
|
|
|
|
}
|
|
|
|
break;
|
2023-08-02 09:11:52 +02:00
|
|
|
case FIOC_FILEPATH:
|
|
|
|
snprintf((FAR char *)(uintptr_t)arg, PATH_MAX, "local:[%s]",
|
|
|
|
conn->lc_path);
|
|
|
|
break;
|
2023-01-16 16:59:41 +01:00
|
|
|
case BIOC_FLUSH:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
2021-06-07 12:06:44 +02:00
|
|
|
default:
|
|
|
|
ret = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:16:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_socketpair
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a pair of connected sockets between psocks[2]
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psocks A reference to the socket structure of the socket pair
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_socketpair(FAR struct socket *psocks[2])
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *conns[2];
|
|
|
|
bool nonblock;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
conns[i] = psocks[i]->s_conn;
|
|
|
|
snprintf(conns[i]->lc_path,
|
|
|
|
sizeof(conns[i]->lc_path), "socketpair%p", psocks[0]);
|
|
|
|
|
|
|
|
conns[i]->lc_proto = psocks[i]->s_type;
|
|
|
|
conns[i]->lc_type = LOCAL_TYPE_PATHNAME;
|
|
|
|
conns[i]->lc_state = LOCAL_STATE_BOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
conns[0]->lc_instance_id = conns[1]->lc_instance_id
|
2023-06-27 10:28:09 +02:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2021-06-08 16:16:56 +02:00
|
|
|
= local_generate_instance_id();
|
2023-06-27 10:28:09 +02:00
|
|
|
#else
|
|
|
|
= -1;
|
|
|
|
#endif
|
2021-06-08 16:16:56 +02:00
|
|
|
|
|
|
|
/* Create the FIFOs needed for the connection */
|
|
|
|
|
|
|
|
ret = local_create_fifos(conns[0]);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
nonblock = _SS_ISNONBLOCK(conns[0]->lc_conn.s_flags);
|
2021-06-08 16:16:56 +02:00
|
|
|
|
|
|
|
/* Open the client-side write-only FIFO. */
|
|
|
|
|
|
|
|
ret = local_open_client_tx(conns[0], nonblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the server-side read-only FIFO. */
|
|
|
|
|
|
|
|
ret = local_open_server_rx(conns[1], nonblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the server-side write-only FIFO. */
|
|
|
|
|
|
|
|
ret = local_open_server_tx(conns[1], nonblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the client-side read-only FIFO */
|
|
|
|
|
|
|
|
ret = local_open_client_rx(conns[0], nonblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
conns[0]->lc_state = conns[1]->lc_state
|
|
|
|
= LOCAL_STATE_CONNECTED;
|
2023-06-27 10:28:09 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
if (psocks[0]->s_type == SOCK_DGRAM)
|
|
|
|
{
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
ret = local_set_pollthreshold(conns[i], sizeof(uint16_t));
|
2024-02-28 06:14:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2023-06-27 10:28:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-08 16:16:56 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
local_release_fifos(conns[0]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:28:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_shutdown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The shutdown() function will cause all or part of a full-duplex
|
|
|
|
* connection on the socket associated with the file descriptor socket to
|
|
|
|
* be shut down.
|
|
|
|
*
|
|
|
|
* The shutdown() function disables subsequent send and/or receive
|
|
|
|
* operations on a socket, depending on the value of the how argument.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sockfd - Specifies the file descriptor of the socket.
|
|
|
|
* how - Specifies the type of shutdown. The values are as follows:
|
|
|
|
*
|
|
|
|
* SHUT_RD - Disables further receive operations.
|
|
|
|
* SHUT_WR - Disables further send operations.
|
|
|
|
* SHUT_RDWR - Disables further send and receive operations.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int local_shutdown(FAR struct socket *psock, int how)
|
|
|
|
{
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(psock->s_domain == PF_LOCAL);
|
2023-01-29 09:34:30 +01:00
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
|
|
|
case SOCK_STREAM:
|
|
|
|
{
|
|
|
|
FAR struct local_conn_s *conn = psock->s_conn;
|
|
|
|
if (how & SHUT_RD)
|
|
|
|
{
|
|
|
|
if (conn->lc_infile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
file_close(&conn->lc_infile);
|
|
|
|
conn->lc_infile.f_inode = NULL;
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 11:28:32 +01:00
|
|
|
|
2023-01-29 09:34:30 +01:00
|
|
|
if (how & SHUT_WR)
|
|
|
|
{
|
|
|
|
if (conn->lc_outfile.f_inode != NULL)
|
|
|
|
{
|
|
|
|
file_close(&conn->lc_outfile);
|
|
|
|
conn->lc_outfile.f_inode = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_LOCAL_DGRAM
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
return -EOPNOTSUPP;
|
2023-01-18 06:42:42 +01:00
|
|
|
#endif
|
|
|
|
case SOCK_CTRL:
|
|
|
|
return -EOPNOTSUPP;
|
2023-01-29 09:34:30 +01:00
|
|
|
default:
|
|
|
|
return -EBADF;
|
|
|
|
}
|
2023-01-18 11:28:32 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|