2017-07-31 17:49:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/usrsock/usrsock_sockif.c
|
|
|
|
*
|
2021-11-15 07:53:26 +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-31 17:49:19 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-31 17:49:19 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +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-31 17:49:19 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_USRSOCK)
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
|
|
|
#include "usrsock/usrsock.h"
|
|
|
|
#include "socket/socket.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
static int usrsock_sockif_setup(FAR struct socket *psock,
|
|
|
|
int protocol);
|
2017-07-31 17:49:19 +02:00
|
|
|
static sockcaps_t usrsock_sockif_sockcaps(FAR struct socket *psock);
|
|
|
|
static void usrsock_sockif_addref(FAR struct socket *psock);
|
|
|
|
static int usrsock_sockif_close(FAR struct socket *psock);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
const struct sock_intf_s g_usrsock_sockif =
|
|
|
|
{
|
|
|
|
usrsock_sockif_setup, /* si_setup */
|
|
|
|
usrsock_sockif_sockcaps, /* si_sockcaps */
|
|
|
|
usrsock_sockif_addref, /* si_addref */
|
|
|
|
usrsock_bind, /* si_bind */
|
|
|
|
usrsock_getsockname, /* si_getsockname */
|
2018-08-26 23:15:48 +02:00
|
|
|
usrsock_getpeername, /* si_getpeername */
|
|
|
|
usrsock_listen, /* si_listen */
|
2017-07-31 17:49:19 +02:00
|
|
|
usrsock_connect, /* si_connect */
|
2018-08-26 23:15:48 +02:00
|
|
|
usrsock_accept, /* si_accept */
|
2017-07-31 17:49:19 +02:00
|
|
|
usrsock_poll, /* si_poll */
|
2020-12-01 07:55:16 +01:00
|
|
|
usrsock_sendmsg, /* si_sendmsg */
|
|
|
|
usrsock_recvmsg, /* si_recvmsg */
|
|
|
|
usrsock_sockif_close, /* si_close */
|
2022-11-22 14:15:06 +01:00
|
|
|
usrsock_ioctl, /* si_ioctl */
|
2023-01-18 11:28:32 +01:00
|
|
|
NULL, /* si_socketpair */
|
2023-01-29 05:32:19 +01:00
|
|
|
usrsock_shutdown /* si_shutdown */
|
2022-11-22 14:15:06 +01:00
|
|
|
#ifdef CONFIG_NET_SOCKOPTS
|
|
|
|
, usrsock_getsockopt /* si_getsockopt */
|
|
|
|
, usrsock_setsockopt /* si_setsockopt */
|
|
|
|
#endif
|
2017-07-31 17:49:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-13 14:58:02 +01:00
|
|
|
* Name: usrsock_sockif_setup
|
2017-07-31 17:49:19 +02:00
|
|
|
*
|
|
|
|
* 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:
|
2019-03-11 19:48:17 +01:00
|
|
|
* psock - A pointer to a user allocated socket structure to be
|
|
|
|
* initialized.
|
|
|
|
* protocol - (see sys/socket.h)
|
2017-07-31 17:49:19 +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-31 17:49:19 +02:00
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usrsock_sockif_setup(FAR struct socket *psock, int protocol)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Let the user socket logic handle the setup...
|
|
|
|
*
|
|
|
|
* A return value of zero means that the operation was
|
|
|
|
* successfully handled by usrsock. A negative value means that
|
|
|
|
* an error occurred. The special error value -ENETDOWN means
|
|
|
|
* that usrsock daemon is not running. The caller should attempt
|
|
|
|
* to open socket with kernel networking stack in this case.
|
|
|
|
*/
|
|
|
|
|
2022-11-23 14:44:11 +01:00
|
|
|
ret = usrsock_socket(psock->s_domain, psock->s_type, protocol, psock);
|
2017-07-31 17:49:19 +02:00
|
|
|
if (ret == -ENETDOWN)
|
|
|
|
{
|
|
|
|
nwarn("WARNING: usrsock daemon is not running\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsock_sockif_sockcaps
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the bit encoded capabilities of this socket.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-31 17:49:19 +02:00
|
|
|
* psock - Socket structure of the socket whose capabilities are being
|
|
|
|
* queried.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The non-negative set of socket cababilities is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static sockcaps_t usrsock_sockif_sockcaps(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
return SOCKCAP_NONBLOCKING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsock_sockif_addref
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Increment the reference count on the underlying connection structure.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-31 17:49:19 +02:00
|
|
|
* psock - Socket structure of the socket whose reference count will be
|
|
|
|
* incremented.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void usrsock_sockif_addref(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct usrsock_conn_s *conn;
|
|
|
|
|
|
|
|
DEBUGASSERT(psock != NULL && psock->s_conn != NULL);
|
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
DEBUGASSERT(conn->crefs > 0 && conn->crefs < 255);
|
|
|
|
conn->crefs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usrsock_sockif_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Performs the close operation on an USRSOCK socket instance
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-31 17:49:19 +02:00
|
|
|
* psock Socket instance
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usrsock_sockif_close(FAR struct socket *psock)
|
|
|
|
{
|
|
|
|
FAR struct usrsock_conn_s *conn = psock->s_conn;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Perform some pre-close operations for the USRSOCK socket type. */
|
|
|
|
|
|
|
|
/* Is this the last reference to the connection structure (there
|
|
|
|
* could be more if the socket was dup'ed).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->crefs <= 1)
|
|
|
|
{
|
|
|
|
/* Yes... inform user-space daemon of socket close. */
|
|
|
|
|
|
|
|
ret = usrsock_close(conn);
|
|
|
|
|
|
|
|
/* Free the connection structure */
|
|
|
|
|
|
|
|
conn->crefs = 0;
|
|
|
|
usrsock_free(psock->s_conn);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Return with error code, but free resources. */
|
|
|
|
|
|
|
|
nerr("ERROR: usrsock_close failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. Just decrement the reference count */
|
|
|
|
|
|
|
|
conn->crefs--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_USRSOCK */
|