tcp_setsockopt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /****************************************************************************
  2. * net/tcp/tcp_setsockopt.c
  3. *
  4. * Copyright (C) 2018, 2020 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/time.h>
  40. #include <stdint.h>
  41. #include <errno.h>
  42. #include <assert.h>
  43. #include <debug.h>
  44. #include <netinet/tcp.h>
  45. #include <nuttx/net/net.h>
  46. #include <nuttx/net/tcp.h>
  47. #include "socket/socket.h"
  48. #include "utils/utils.h"
  49. #include "tcp/tcp.h"
  50. #ifdef CONFIG_NET_TCPPROTO_OPTIONS
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: tcp_setsockopt
  56. *
  57. * Description:
  58. * tcp_setsockopt() sets the TCP-protocol option specified by the
  59. * 'option' argument to the value pointed to by the 'value' argument for
  60. * the socket specified by the 'psock' argument.
  61. *
  62. * See <netinet/tcp.h> for the a complete list of values of TCP protocol
  63. * options.
  64. *
  65. * Input Parameters:
  66. * psock Socket structure of socket to operate on
  67. * option identifies the option to set
  68. * value Points to the argument value
  69. * value_len The length of the argument value
  70. *
  71. * Returned Value:
  72. * Returns zero (OK) on success. On failure, it returns a negated errno
  73. * value to indicate the nature of the error. See psock_setcockopt() for
  74. * the list of possible error values.
  75. *
  76. ****************************************************************************/
  77. int tcp_setsockopt(FAR struct socket *psock, int option,
  78. FAR const void *value, socklen_t value_len)
  79. {
  80. #ifdef CONFIG_NET_TCP_KEEPALIVE
  81. /* Keep alive options are the only TCP protocol socket option currently
  82. * supported.
  83. */
  84. FAR struct tcp_conn_s *conn;
  85. int ret;
  86. DEBUGASSERT(psock != NULL && value != NULL && psock->s_conn != NULL);
  87. conn = (FAR struct tcp_conn_s *)psock->s_conn;
  88. /* All of the TCP protocol options apply only TCP sockets. The sockets
  89. * do not have to be connected.. that might occur later with the KeepAlive
  90. * already configured.
  91. */
  92. if (psock->s_type != SOCK_STREAM)
  93. {
  94. nerr("ERROR: Not a TCP socket\n");
  95. return -ENOTCONN;
  96. }
  97. /* Handle the Keep-Alive option */
  98. switch (option)
  99. {
  100. /* Handle the SO_KEEPALIVE socket-level option.
  101. *
  102. * NOTE: SO_KEEPALIVE is not really a socket-level option; it is a
  103. * protocol-level option. A given TCP connection may service multiple
  104. * sockets (via dup'ing of the socket). There is, however, still only
  105. * one connection to be monitored and that is a global attribute across
  106. * all of the clones that may use the underlying connection.
  107. */
  108. case SO_KEEPALIVE: /* Verifies TCP connections active by enabling the
  109. * periodic transmission of probes */
  110. if (value_len != sizeof(int))
  111. {
  112. ret = -EDOM;
  113. }
  114. else
  115. {
  116. int keepalive = *(FAR int *)value;
  117. if (keepalive != 0 && keepalive != 1)
  118. {
  119. nerr("ERROR: SO_KEEPALIVE value out of range: %d\n",
  120. keepalive);
  121. return -EDOM;
  122. }
  123. else
  124. {
  125. conn->keepalive = (bool)keepalive;
  126. conn->keeptime = clock_systime_ticks(); /* Reset start time */
  127. ret = OK;
  128. }
  129. }
  130. break;
  131. case TCP_NODELAY: /* Avoid coalescing of small segments. */
  132. nerr("ERROR: TCP_NODELAY not supported\n");
  133. ret = -ENOSYS;
  134. break;
  135. case TCP_KEEPIDLE: /* Start keepalives after this IDLE period */
  136. if (value_len != sizeof(struct timeval))
  137. {
  138. ret = -EDOM;
  139. }
  140. else
  141. {
  142. FAR struct timeval *tv = (FAR struct timeval *)value;
  143. if (tv == NULL)
  144. {
  145. ret = -EINVAL;
  146. }
  147. else
  148. {
  149. unsigned int dsecs;
  150. /* Get the IDLE time value. Any microsecond remainder will
  151. * be forced to the next larger, whole decisecond value.
  152. */
  153. dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
  154. if (dsecs > UINT16_MAX)
  155. {
  156. nwarn("WARNING: TCP_KEEPIDLE value out of range: %u\n",
  157. dsecs);
  158. ret = -EDOM;
  159. }
  160. else
  161. {
  162. conn->keepidle = (uint16_t)dsecs;
  163. conn->keeptime = clock_systime_ticks(); /* Reset start time */
  164. ret = OK;
  165. }
  166. }
  167. }
  168. break;
  169. case TCP_KEEPINTVL: /* Interval between keepalives */
  170. if (value_len != sizeof(struct timeval))
  171. {
  172. ret = -EDOM;
  173. }
  174. else
  175. {
  176. FAR struct timeval *tv = (FAR struct timeval *)value;
  177. if (tv == NULL)
  178. {
  179. ret = -EINVAL;
  180. }
  181. else
  182. {
  183. unsigned int dsecs;
  184. /* Get the IDLE time value. Any microsecond remainder will
  185. * be forced to the next larger, whole decisecond value.
  186. */
  187. dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
  188. if (dsecs > UINT16_MAX)
  189. {
  190. nwarn("WARNING: TCP_KEEPINTVL value out of range: %u\n",
  191. dsecs);
  192. ret = -EDOM;
  193. }
  194. else
  195. {
  196. conn->keepintvl = (uint16_t)dsecs;
  197. conn->keeptime = clock_systime_ticks(); /* Reset start time */
  198. ret = OK;
  199. }
  200. }
  201. }
  202. break;
  203. case TCP_KEEPCNT: /* Number of keepalives before death */
  204. if (value_len != sizeof(int))
  205. {
  206. ret = -EDOM;
  207. }
  208. else
  209. {
  210. int keepcnt = *(FAR int *)value;
  211. if (keepcnt < 0 || keepcnt > UINT8_MAX)
  212. {
  213. nerr("ERROR: TCP_KEEPCNT value out of range: %d\n", keepcnt);
  214. return -EDOM;
  215. }
  216. else
  217. {
  218. conn->keepcnt = (uint8_t)keepcnt;
  219. conn->keeptime = clock_systime_ticks(); /* Reset start time */
  220. ret = OK;
  221. }
  222. }
  223. break;
  224. default:
  225. nerr("ERROR: Unrecognized TCP option: %d\n", option);
  226. ret = -ENOPROTOOPT;
  227. break;
  228. }
  229. return ret;
  230. #else
  231. return -ENOPROTOOPT;
  232. #endif /* CONFIG_NET_TCP_KEEPALIVE */
  233. }
  234. #endif /* CONFIG_NET_TCPPROTO_OPTIONS */