2018-03-12 17:59:46 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* net/tcp/tcp_getsockopt.c
|
2018-03-12 17:59:46 +01:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
2018-03-12 17:59:46 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-12 17:59:46 +01: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.
|
2018-03-12 17:59:46 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-01-11 21:10:29 +01:00
|
|
|
#include <sys/time.h>
|
2018-03-12 17:59:46 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/tcp.h>
|
|
|
|
|
|
|
|
#include "socket/socket.h"
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "tcp/tcp.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCPPROTO_OPTIONS
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_getsockopt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* tcp_getsockopt() retrieves the value for the option specified by the
|
|
|
|
* 'option' argument for the socket specified by the 'psock' 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; to retrieve options at the TCP-protocol level, the level
|
2020-12-13 14:58:02 +01:00
|
|
|
* argument is SOL_TCP.
|
2018-03-12 17:59:46 +01:00
|
|
|
*
|
|
|
|
* See <sys/socket.h> a complete list of values for the socket-level
|
|
|
|
* 'option' argument. Protocol-specific options are are protocol specific
|
|
|
|
* header files (such as netinet/tcp.h for the case of the TCP protocol).
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-03-12 17:59:46 +01:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error. See psock_getsockopt() for
|
|
|
|
* the complete list of appropriate return error codes.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int tcp_getsockopt(FAR struct socket *psock, int option,
|
|
|
|
FAR void *value, FAR socklen_t *value_len)
|
|
|
|
{
|
|
|
|
FAR struct tcp_conn_s *conn;
|
|
|
|
int ret;
|
|
|
|
|
2023-08-30 08:46:41 +02:00
|
|
|
DEBUGASSERT(value != NULL && value_len != NULL);
|
2023-05-08 16:11:04 +02:00
|
|
|
conn = psock->s_conn;
|
2018-03-12 17:59:46 +01:00
|
|
|
|
|
|
|
/* All of the TCP protocol options apply only TCP sockets. The sockets
|
|
|
|
* do not have to be connected.. that might occur later with the KeepAlive
|
|
|
|
* already configured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (psock->s_type != SOCK_STREAM)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Not a TCP socket\n");
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the Keep-Alive option */
|
|
|
|
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
/* Handle the SO_KEEPALIVE socket-level option.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-03-08 16:43:38 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_KEEPALIVE
|
2018-03-12 17:59:46 +01:00
|
|
|
case SO_KEEPALIVE: /* Verifies TCP connections active by enabling the
|
|
|
|
* periodic transmission of probes */
|
|
|
|
if (*value_len < sizeof(int))
|
|
|
|
{
|
|
|
|
/* REVISIT: POSIX says that we should truncate the value if it
|
|
|
|
* is larger than value_len. That just doesn't make sense
|
|
|
|
* to me in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR int *keepalive = (FAR int *)value;
|
2022-05-18 07:32:56 +02:00
|
|
|
*keepalive = conn->keepalive;
|
2018-03-12 17:59:46 +01:00
|
|
|
*value_len = sizeof(int);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TCP_KEEPIDLE: /* Start keepalives after this IDLE period */
|
|
|
|
case TCP_KEEPINTVL: /* Interval between keepalives */
|
2021-05-11 14:20:05 +02:00
|
|
|
{
|
|
|
|
int dsecs;
|
|
|
|
|
|
|
|
if (option == TCP_KEEPIDLE)
|
|
|
|
{
|
|
|
|
dsecs = conn->keepidle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dsecs = conn->keepintvl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else if (*value_len == sizeof(struct timeval))
|
|
|
|
{
|
|
|
|
FAR struct timeval *tv = (FAR struct timeval *)value;
|
|
|
|
|
|
|
|
/* Convert the KeepIdle time from deciseconds to struct
|
|
|
|
* timeval.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_dsec2timeval(dsecs, tv);
|
|
|
|
*value_len = sizeof(struct timeval);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else if (*value_len == sizeof(int))
|
|
|
|
{
|
|
|
|
FAR int *pdsecs = (FAR int *)value;
|
|
|
|
*pdsecs = dsecs;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* REVISIT: POSIX says that we should truncate the value if it
|
|
|
|
* is larger than value_len. That just doesn't make sense
|
|
|
|
* to me in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 17:59:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TCP_KEEPCNT: /* Number of keepalives before death */
|
|
|
|
if (*value_len < sizeof(int))
|
|
|
|
{
|
|
|
|
/* REVISIT: POSIX says that we should truncate the value if it
|
|
|
|
* is larger than value_len. That just doesn't make sense
|
|
|
|
* to me in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR int *keepcnt = (FAR int *)value;
|
2022-05-18 07:32:56 +02:00
|
|
|
*keepcnt = conn->keepcnt;
|
2018-03-12 17:59:46 +01:00
|
|
|
*value_len = sizeof(int);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
2023-03-08 16:43:38 +01:00
|
|
|
#endif /* CONFIG_NET_TCP_KEEPALIVE */
|
|
|
|
|
2024-04-29 09:10:08 +02:00
|
|
|
case TCP_NODELAY: /* Avoid coalescing of small segments. */
|
|
|
|
if (*value_len < sizeof(int))
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR int *nodelay = (FAR int *)value;
|
|
|
|
|
|
|
|
/* Always true here since we do not support Nagle. */
|
|
|
|
|
|
|
|
*nodelay = 1;
|
|
|
|
*value_len = sizeof(int);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-03-08 16:43:38 +01:00
|
|
|
case TCP_MAXSEG: /* The maximum segment size */
|
|
|
|
if (*value_len < sizeof(int))
|
|
|
|
{
|
|
|
|
/* REVISIT: POSIX says that we should truncate the value if it
|
|
|
|
* is larger than value_len. That just doesn't make sense
|
|
|
|
* to me in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR int *mss = (FAR int *)value;
|
|
|
|
*mss = conn->mss;
|
|
|
|
*value_len = sizeof(int);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
break;
|
2018-03-12 17:59:46 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
nerr("ERROR: Unrecognized TCP option: %d\n", option);
|
|
|
|
ret = -ENOPROTOOPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_TCPPROTO_OPTIONS */
|