2007-09-07 08:10:10 +08:00
|
|
|
/****************************************************************************
|
2014-06-29 07:25:18 +08:00
|
|
|
* net/socket/setsockopt.c
|
2007-09-07 08:10:10 +08:00
|
|
|
*
|
2020-01-11 23:40:32 +08:00
|
|
|
* Copyright (C) 2007, 2008, 2011-2012, 2014-2015, 2017-2018, 2020 Gregory
|
|
|
|
* Nutt. All rights reserved.
|
2012-03-03 03:57:52 +08:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-07 08:10:10 +08:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-09-09 03:50:59 +08:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_SOCKOPTS)
|
2007-09-07 08:10:10 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2015-02-16 05:18:35 +08:00
|
|
|
#include <sys/time.h>
|
2007-09-07 08:10:10 +08:00
|
|
|
#include <errno.h>
|
2017-03-31 22:58:14 +08:00
|
|
|
#include <debug.h>
|
|
|
|
#include <assert.h>
|
2007-09-09 06:41:00 +08:00
|
|
|
#include <arch/irq.h>
|
2007-09-07 08:10:10 +08:00
|
|
|
|
2014-07-05 06:38:51 +08:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2014-06-29 07:25:18 +08:00
|
|
|
#include "socket/socket.h"
|
2018-11-01 05:59:05 +08:00
|
|
|
#include "inet/inet.h"
|
2018-03-13 00:59:46 +08:00
|
|
|
#include "tcp/tcp.h"
|
2018-06-26 02:41:28 +08:00
|
|
|
#include "udp/udp.h"
|
2017-03-31 22:58:14 +08:00
|
|
|
#include "usrsock/usrsock.h"
|
2014-06-29 07:25:18 +08:00
|
|
|
#include "utils/utils.h"
|
2020-06-16 01:57:13 +08:00
|
|
|
#include "can/can.h"
|
2007-09-07 08:10:10 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-20 06:02:56 +08:00
|
|
|
* Public Functions
|
2007-09-07 08:10:10 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-03-13 00:59:46 +08:00
|
|
|
* Name: psock_socketlevel_option
|
2007-09-07 08:10:10 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2018-03-13 00:59:46 +08:00
|
|
|
* psock_socketlevel_option() sets the socket-level option specified by the
|
|
|
|
* 'option' argument to the value pointed to by the 'value' argument for
|
|
|
|
* the socket specified by the 'psock' argument.
|
2007-09-07 08:10:10 +08:00
|
|
|
*
|
2018-03-13 00:59:46 +08:00
|
|
|
* See <sys/socket.h> a complete list of values for the socket level
|
|
|
|
* 'option' argument.
|
2007-09-07 08:10:10 +08:00
|
|
|
*
|
2018-03-13 00:59:46 +08:00
|
|
|
* Input Parameters:
|
2012-03-03 03:57:52 +08:00
|
|
|
* psock Socket structure of socket to operate on
|
2007-09-07 08:10:10 +08:00
|
|
|
* option identifies the option to set
|
|
|
|
* value Points to the argument value
|
|
|
|
* value_len The length of the argument value
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-03-13 00:59:46 +08:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error. See psock_setcockopt() for
|
|
|
|
* the list of possible error values.
|
2007-09-07 08:10:10 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-13 00:59:46 +08:00
|
|
|
static int psock_socketlevel_option(FAR struct socket *psock, int option,
|
2020-05-14 17:45:29 +08:00
|
|
|
FAR const void *value,
|
|
|
|
socklen_t value_len)
|
2007-09-07 08:10:10 +08:00
|
|
|
{
|
2007-09-09 03:50:59 +08:00
|
|
|
/* Verify that the socket option if valid (but might not be supported ) */
|
|
|
|
|
|
|
|
if (!_SO_SETVALID(option) || !value)
|
|
|
|
{
|
2017-09-30 22:18:08 +08:00
|
|
|
return -EINVAL;
|
2007-09-09 03:50:59 +08:00
|
|
|
}
|
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
/* Process the option */
|
|
|
|
|
|
|
|
switch (option)
|
2017-03-31 22:58:14 +08:00
|
|
|
{
|
2020-12-01 20:03:00 +08:00
|
|
|
/* The following options take a pointer to an integer boolean value.
|
|
|
|
* We will blindly set the bit here although the implementation
|
|
|
|
* is outside of the scope of setsockopt.
|
|
|
|
*/
|
2017-03-31 22:58:14 +08:00
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
case SO_RCVTIMEO:
|
|
|
|
case SO_SNDTIMEO:
|
|
|
|
{
|
|
|
|
FAR struct timeval *tv = (FAR struct timeval *)value;
|
|
|
|
socktimeo_t timeo;
|
2017-03-31 22:58:14 +08:00
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
/* Verify that option is the size of an 'struct timeval'. */
|
2017-03-31 22:58:14 +08:00
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
if (tv == NULL || value_len != sizeof(struct timeval))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the timeout value. Any microsecond remainder will be
|
|
|
|
* forced to the next larger, whole decisecond value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
timeo = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
|
|
|
|
|
|
|
|
/* Save the timeout value */
|
2017-03-31 22:58:14 +08:00
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
if (option == SO_RCVTIMEO)
|
2017-03-31 22:58:14 +08:00
|
|
|
{
|
2020-12-01 20:03:00 +08:00
|
|
|
psock->s_rcvtimeo = timeo;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
psock->s_sndtimeo = timeo;
|
2017-03-31 22:58:14 +08:00
|
|
|
}
|
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
/* Set/clear the corresponding enable/disable bit */
|
2014-01-14 01:58:45 +08:00
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
if (timeo)
|
|
|
|
{
|
|
|
|
_SO_CLROPT(psock->s_options, option);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_SO_SETOPT(psock->s_options, option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_USRSOCK
|
2007-09-09 03:50:59 +08:00
|
|
|
|
|
|
|
case SO_BROADCAST: /* Permits sending of broadcast messages */
|
2018-03-13 22:07:03 +08:00
|
|
|
case SO_DEBUG: /* Enables recording of debugging information */
|
|
|
|
case SO_DONTROUTE: /* Requests outgoing messages bypass standard routing */
|
2018-03-13 00:59:46 +08:00
|
|
|
#ifndef CONFIG_NET_TCPPROTO_OPTIONS
|
|
|
|
case SO_KEEPALIVE: /* Verifies TCP connections active by enabling the
|
|
|
|
* periodic transmission of probes */
|
|
|
|
#endif
|
2007-09-09 03:50:59 +08:00
|
|
|
case SO_OOBINLINE: /* Leaves received out-of-band data inline */
|
2018-03-13 22:07:03 +08:00
|
|
|
case SO_REUSEADDR: /* Allow reuse of local addresses */
|
2007-09-09 03:50:59 +08:00
|
|
|
{
|
|
|
|
int setting;
|
|
|
|
|
|
|
|
/* Verify that option is the size of an 'int'. Should also check
|
|
|
|
* that 'value' is properly aligned for an 'int'
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (value_len != sizeof(int))
|
|
|
|
{
|
2017-09-30 22:18:08 +08:00
|
|
|
return -EINVAL;
|
2013-04-05 20:40:40 +08:00
|
|
|
}
|
2007-09-09 03:50:59 +08:00
|
|
|
|
|
|
|
/* Get the value. Is the option being set or cleared? */
|
|
|
|
|
2015-10-09 05:10:04 +08:00
|
|
|
setting = *(FAR int *)value;
|
2007-09-09 03:50:59 +08:00
|
|
|
|
2018-06-24 05:03:01 +08:00
|
|
|
/* Lock the network so that we have exclusive access to the socket
|
|
|
|
* options.
|
2007-09-09 03:50:59 +08:00
|
|
|
*/
|
|
|
|
|
2016-12-04 06:28:19 +08:00
|
|
|
net_lock();
|
2007-09-09 03:50:59 +08:00
|
|
|
|
|
|
|
/* Set or clear the option bit */
|
|
|
|
|
|
|
|
if (setting)
|
|
|
|
{
|
|
|
|
_SO_SETOPT(psock->s_options, option);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_SO_CLROPT(psock->s_options, option);
|
|
|
|
}
|
2014-01-14 01:58:45 +08:00
|
|
|
|
2016-12-04 06:28:19 +08:00
|
|
|
net_unlock();
|
2007-09-09 03:50:59 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-03-13 00:59:46 +08:00
|
|
|
#ifdef CONFIG_NET_TCPPROTO_OPTIONS
|
|
|
|
/* Any connection-oriented protocol could potentially support
|
|
|
|
* SO_KEEPALIVE. However, this option is currently only available for
|
|
|
|
* TCP/IP.
|
|
|
|
*
|
|
|
|
* NOTE: SO_KEEPALIVE is not really a socket-level option; it is a
|
|
|
|
* protocol-level option. A given TCP connection may service multiple
|
|
|
|
* sockets (via dup'ing of the socket). There is, however, still only
|
|
|
|
* one connection to be monitored and that is a global attribute across
|
|
|
|
* all of the clones that may use the underlying connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case SO_KEEPALIVE: /* Verifies TCP connections active by enabling the
|
|
|
|
* periodic transmission of probes */
|
|
|
|
return tcp_setsockopt(psock, option, value, value_len);
|
|
|
|
#endif
|
|
|
|
|
2014-01-14 01:58:45 +08:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2018-03-13 22:07:03 +08:00
|
|
|
case SO_LINGER: /* Lingers on a close() if data is present */
|
2014-01-14 01:58:45 +08:00
|
|
|
{
|
|
|
|
FAR struct linger *setting;
|
|
|
|
|
|
|
|
/* Verify that option is at least the size of an 'struct linger'. */
|
|
|
|
|
|
|
|
if (value_len < sizeof(FAR struct linger))
|
|
|
|
{
|
2017-09-30 22:18:08 +08:00
|
|
|
return -EINVAL;
|
2014-01-14 01:58:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the value. Is the option being set or cleared? */
|
|
|
|
|
|
|
|
setting = (FAR struct linger *)value;
|
|
|
|
|
2018-06-24 05:03:01 +08:00
|
|
|
/* Lock the network so that we have exclusive access to the socket
|
|
|
|
* options.
|
2014-01-14 01:58:45 +08:00
|
|
|
*/
|
|
|
|
|
2018-06-24 05:03:01 +08:00
|
|
|
net_lock();
|
2014-01-14 01:58:45 +08:00
|
|
|
|
2020-05-14 17:45:29 +08:00
|
|
|
/* Set or clear the linger option bit and linger time
|
|
|
|
* (in deciseconds)
|
|
|
|
*/
|
2014-01-14 01:58:45 +08:00
|
|
|
|
|
|
|
if (setting->l_onoff)
|
|
|
|
{
|
|
|
|
_SO_SETOPT(psock->s_options, option);
|
|
|
|
psock->s_linger = 10 * setting->l_linger;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_SO_CLROPT(psock->s_options, option);
|
|
|
|
psock->s_linger = 0;
|
|
|
|
}
|
|
|
|
|
2016-12-04 06:28:19 +08:00
|
|
|
net_unlock();
|
2014-01-14 01:58:45 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2020-06-15 16:23:25 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TIMESTAMP
|
|
|
|
case SO_TIMESTAMP: /* Generates a timestamp for each incoming packet */
|
|
|
|
{
|
|
|
|
/* Verify that option is at least the size of an integer. */
|
|
|
|
|
|
|
|
if (value_len < sizeof(FAR int32_t))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock the network so that we have exclusive access to the socket
|
|
|
|
* options.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
psock->s_timestamp = *((FAR int32_t *)value);
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2007-09-09 03:50:59 +08:00
|
|
|
/* The following are not yet implemented */
|
|
|
|
|
|
|
|
case SO_RCVBUF: /* Sets receive buffer size */
|
|
|
|
case SO_RCVLOWAT: /* Sets the minimum number of bytes to input */
|
2018-03-13 22:07:03 +08:00
|
|
|
case SO_SNDBUF: /* Sets send buffer size */
|
2007-09-09 03:50:59 +08:00
|
|
|
case SO_SNDLOWAT: /* Sets the minimum number of bytes to output */
|
|
|
|
|
|
|
|
/* There options are only valid when used with getopt */
|
|
|
|
|
|
|
|
case SO_ACCEPTCONN: /* Reports whether socket listening is enabled */
|
|
|
|
case SO_ERROR: /* Reports and clears error status. */
|
|
|
|
case SO_TYPE: /* Reports the socket type */
|
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
#endif
|
2007-09-09 03:50:59 +08:00
|
|
|
default:
|
2017-09-30 22:18:08 +08:00
|
|
|
return -ENOPROTOOPT;
|
2007-09-09 03:50:59 +08:00
|
|
|
}
|
2013-04-05 20:40:40 +08:00
|
|
|
|
2007-09-09 03:50:59 +08:00
|
|
|
return OK;
|
2007-09-07 08:10:10 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 00:59:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: psock_setsockopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* psock_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 socket specified by the
|
|
|
|
* 'psock' argument.
|
|
|
|
*
|
|
|
|
* 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 socket level
|
|
|
|
* 'option' argument.
|
|
|
|
*
|
2020-05-14 17:45:29 +08:00
|
|
|
* Protocol level options, such as SOL_TCP, are defined in
|
|
|
|
* protocol-specific header files, for example include/netinet/tcp.h
|
2018-03-13 00:59:46 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock Socket structure of socket to operate on
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error:
|
|
|
|
*
|
|
|
|
* EDOM
|
|
|
|
* The send and receive timeout values are too big to fit into the
|
|
|
|
* timeout fields in the socket structure.
|
|
|
|
* EINVAL
|
|
|
|
* The specified option is invalid at the specified socket 'level' or the
|
|
|
|
* socket has been shut down.
|
|
|
|
* EISCONN
|
|
|
|
* The socket is already connected, and a specified option cannot be set
|
|
|
|
* while the socket is connected.
|
|
|
|
* ENOPROTOOPT
|
|
|
|
* The 'option' is not supported by the protocol.
|
|
|
|
* ENOTSOCK
|
|
|
|
* The 'sockfd' argument does not refer to a socket.
|
|
|
|
* ENOMEM
|
|
|
|
* There was insufficient memory available for the operation to complete.
|
|
|
|
* ENOBUFS
|
|
|
|
* Insufficient resources are available in the system to complete the
|
|
|
|
* call.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int psock_setsockopt(FAR struct socket *psock, int level, int option,
|
|
|
|
FAR const void *value, socklen_t value_len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-01-31 17:48:46 +08:00
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (psock == NULL || psock->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:59:46 +08:00
|
|
|
/* Handle setting of the socket option according to the level at which
|
|
|
|
* option should be applied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case SOL_SOCKET: /* Socket-level options (see include/sys/socket.h) */
|
2018-11-01 05:59:05 +08:00
|
|
|
ret = psock_socketlevel_option(psock, option, value, value_len);
|
|
|
|
break;
|
2018-03-13 00:59:46 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCPPROTO_OPTIONS
|
2020-12-01 15:51:12 +08:00
|
|
|
case IPPROTO_TCP:/* TCP protocol socket options (see include/netinet/tcp.h) */
|
2018-11-01 05:59:05 +08:00
|
|
|
ret = tcp_setsockopt(psock, option, value, value_len);
|
|
|
|
break;
|
2018-03-13 00:59:46 +08:00
|
|
|
#endif
|
|
|
|
|
2018-06-26 02:41:28 +08:00
|
|
|
#ifdef CONFIG_NET_UDPPROTO_OPTIONS
|
2020-12-01 15:51:12 +08:00
|
|
|
case IPPROTO_UDP:/* UDP protocol socket options (see include/netinet/udp.h) */
|
2018-11-01 05:59:05 +08:00
|
|
|
ret = udp_setsockopt(psock, option, value, value_len);
|
|
|
|
break;
|
2018-06-26 02:41:28 +08:00
|
|
|
#endif
|
|
|
|
|
2018-11-01 05:59:05 +08:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2020-06-29 14:05:38 +08:00
|
|
|
case IPPROTO_IP:/* TCP protocol socket options (see include/netinet/in.h) */
|
2018-11-01 05:59:05 +08:00
|
|
|
ret = ipv4_setsockopt(psock, option, value, value_len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2020-06-29 14:05:38 +08:00
|
|
|
case IPPROTO_IPV6:/* TCP protocol socket options (see include/netinet/in.h) */
|
2018-11-01 05:59:05 +08:00
|
|
|
ret = ipv6_setsockopt(psock, option, value, value_len);
|
|
|
|
break;
|
|
|
|
#endif
|
2018-03-13 00:59:46 +08:00
|
|
|
|
2020-06-15 16:23:25 +08:00
|
|
|
#ifdef CONFIG_NET_CANPROTO_OPTIONS
|
|
|
|
case SOL_CAN_RAW: /* CAN protocol socket options (see include/netpacket/can.h) */
|
|
|
|
ret = can_setsockopt(psock, option, value, value_len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2018-03-13 00:59:46 +08:00
|
|
|
default: /* The provided level is invalid */
|
2020-12-01 15:51:12 +08:00
|
|
|
ret = -ENOPROTOOPT;
|
2018-11-01 05:59:05 +08:00
|
|
|
break;
|
2018-03-13 00:59:46 +08:00
|
|
|
}
|
|
|
|
|
2020-12-01 20:03:00 +08:00
|
|
|
#ifdef CONFIG_NET_USRSOCK
|
|
|
|
/* Try usrsock further if the protocol not available */
|
|
|
|
|
|
|
|
if (ret == -ENOPROTOOPT && psock->s_type == SOCK_USRSOCK_TYPE)
|
|
|
|
{
|
|
|
|
ret = usrsock_setsockopt(psock->s_conn, level,
|
|
|
|
option, value, value_len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-13 00:59:46 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-03 03:57:52 +08:00
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: setsockopt
|
2012-03-03 03:57:52 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* 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 socket associated with the
|
|
|
|
* file descriptor specified by the 'sockfd' argument.
|
|
|
|
*
|
2018-03-13 00:59:46 +08:00
|
|
|
* The 'level' argument specifies the protocol level of the option. To set
|
2012-03-03 03:57:52 +08:00
|
|
|
* options at the socket level, specify the level argument as SOL_SOCKET.
|
|
|
|
*
|
2018-03-13 00:59:46 +08:00
|
|
|
* See <sys/socket.h> a complete list of values for the socket level
|
|
|
|
* 'option' argument.
|
2012-03-03 03:57:52 +08:00
|
|
|
*
|
2020-05-14 17:45:29 +08:00
|
|
|
* Protocol level options, such as SOL_TCP, are defined in
|
|
|
|
* protocol-specific header files, for example include/netinet/tcp.h
|
2018-03-13 00:59:46 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2012-03-03 03:57:52 +08:00
|
|
|
* sockfd Socket descriptor of socket
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-03-13 00:59:46 +08:00
|
|
|
* 0 on success; -1 on failure
|
2012-03-03 03:57:52 +08:00
|
|
|
*
|
2018-03-13 00:59:46 +08:00
|
|
|
* EBADF
|
|
|
|
* The 'sockfd' argument is not a valid socket descriptor.
|
|
|
|
* EDOM
|
|
|
|
* The send and receive timeout values are too big to fit into the
|
|
|
|
* timeout fields in the socket structure.
|
|
|
|
* EINVAL
|
|
|
|
* The specified option is invalid at the specified socket 'level' or the
|
|
|
|
* socket has been shut down.
|
|
|
|
* EISCONN
|
|
|
|
* The socket is already connected, and a specified option cannot be set
|
|
|
|
* while the socket is connected.
|
|
|
|
* ENOPROTOOPT
|
|
|
|
* The 'option' is not supported by the protocol.
|
|
|
|
* ENOTSOCK
|
|
|
|
* The 'sockfd' argument does not refer to a socket.
|
|
|
|
* ENOMEM
|
|
|
|
* There was insufficient memory available for the operation to complete.
|
|
|
|
* ENOBUFS
|
|
|
|
* Insufficient resources are available in the system to complete the
|
|
|
|
* call.
|
2012-03-03 03:57:52 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-11 23:40:32 +08:00
|
|
|
int setsockopt(int sockfd, int level, int option, const void *value,
|
|
|
|
socklen_t value_len)
|
2012-03-03 03:57:52 +08:00
|
|
|
{
|
|
|
|
FAR struct socket *psock;
|
2017-09-30 22:18:08 +08:00
|
|
|
int ret;
|
2012-03-03 03:57:52 +08:00
|
|
|
|
|
|
|
/* Get the underlying socket structure */
|
|
|
|
|
|
|
|
psock = sockfd_socket(sockfd);
|
|
|
|
|
|
|
|
/* Then let psock_setockopt() do all of the work */
|
|
|
|
|
2017-09-30 22:18:08 +08:00
|
|
|
ret = psock_setsockopt(psock, level, option, value, value_len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-13 12:08:32 +08:00
|
|
|
set_errno(-ret);
|
2017-09-30 22:18:08 +08:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2012-03-03 03:57:52 +08:00
|
|
|
}
|
|
|
|
|
2007-09-09 03:50:59 +08:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */
|