tcp_setsockopt.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /****************************************************************************
  2. * net/tcp/tcp_setsockopt.c
  3. *
  4. * Copyright (C) 2018 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/clock.h>
  46. #include <nuttx/net/net.h>
  47. #include <nuttx/net/tcp.h>
  48. #include "socket/socket.h"
  49. #include "utils/utils.h"
  50. #include "tcp/tcp.h"
  51. #ifdef CONFIG_NET_TCPPROTO_OPTIONS
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: tcp_setsockopt
  57. *
  58. * Description:
  59. * tcp_setsockopt() sets the TCP-protocol option specified by the
  60. * 'option' argument to the value pointed to by the 'value' argument for
  61. * the socket specified by the 'psock' argument.
  62. *
  63. * See <netinet/tcp.h> for the a complete list of values of TCP protocol
  64. * options.
  65. *
  66. * Input Parameters:
  67. * psock Socket structure of socket to operate on
  68. * option identifies the option to set
  69. * value Points to the argument value
  70. * value_len The length of the argument value
  71. *
  72. * Returned Value:
  73. * Returns zero (OK) on success. On failure, it returns a negated errno
  74. * value to indicate the nature of the error. See psock_setcockopt() for
  75. * the list of possible error values.
  76. *
  77. ****************************************************************************/
  78. int tcp_setsockopt(FAR struct socket *psock, int option,
  79. FAR const void *value, socklen_t value_len)
  80. {
  81. #ifdef CONFIG_NET_TCP_KEEPALIVE
  82. /* Keep alive options are the only TCP protocol socket option currently
  83. * supported.
  84. */
  85. FAR struct tcp_conn_s *conn;
  86. int ret;
  87. DEBUGASSERT(psock != NULL && value != NULL && psock->s_conn != NULL);
  88. conn = (FAR struct tcp_conn_s *)psock->s_conn;
  89. /* All of the TCP protocol options apply only TCP sockets. The sockets
  90. * do not have to be connected.. that might occur later with the KeepAlive
  91. * already configured.
  92. */
  93. if (psock->s_type != SOCK_STREAM)
  94. {
  95. nerr("ERROR: Not a TCP socket\n");
  96. return -ENOTCONN;
  97. }
  98. /* Handle the Keep-Alive option */
  99. switch (option)
  100. {
  101. /* Handle the SO_KEEPALIVE socket-level option.
  102. *
  103. * NOTE: SO_KEEPALIVE is not really a socket-level option; it is a
  104. * protocol-level option. A given TCP connection may service multiple
  105. * sockets (via dup'ing of the socket). There is, however, still only
  106. * one connection to be monitored and that is a global attribute across
  107. * all of the clones that may use the underlying connection.
  108. */
  109. case SO_KEEPALIVE: /* Verifies TCP connections active by enabling the
  110. * periodic transmission of probes */
  111. if (value_len != sizeof(int))
  112. {
  113. ret = -EDOM;
  114. }
  115. else
  116. {
  117. int keepalive = *(FAR int *)value;
  118. if (keepalive != 0 && keepalive != 1)
  119. {
  120. nerr("ERROR: SO_KEEPALIVE value out of range: %d\n",
  121. keepalive);
  122. return -EDOM;
  123. }
  124. else
  125. {
  126. conn->keepalive = (bool)keepalive;
  127. conn->keeptime = clock_systimer(); /* Reset start time */
  128. ret = OK;
  129. }
  130. }
  131. break;
  132. case TCP_NODELAY: /* Avoid coalescing of small segments. */
  133. nerr("ERROR: TCP_NODELAY not supported\n");
  134. ret = -ENOSYS;
  135. break;
  136. case TCP_KEEPIDLE: /* Start keepalives after this IDLE period */
  137. if (value_len != sizeof(struct timeval))
  138. {
  139. ret = -EDOM;
  140. }
  141. else
  142. {
  143. FAR struct timeval *tv = (FAR struct timeval *)value;
  144. if (tv == NULL)
  145. {
  146. ret = -EINVAL;
  147. }
  148. else
  149. {
  150. unsigned int dsecs;
  151. /* Get the IDLE time value. Any microsecond remainder will
  152. * be forced to the next larger, whole decisecond value.
  153. */
  154. dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
  155. if (dsecs > UINT16_MAX)
  156. {
  157. nwarn("WARNING: TCP_KEEPIDLE value out of range: %u\n",
  158. dsecs);
  159. ret = -EDOM;
  160. }
  161. else
  162. {
  163. conn->keepidle = (uint16_t)dsecs;
  164. conn->keeptime = clock_systimer(); /* Reset start time */
  165. ret = OK;
  166. }
  167. }
  168. }
  169. break;
  170. case TCP_KEEPINTVL: /* Interval between keepalives */
  171. if (value_len != sizeof(struct timeval))
  172. {
  173. ret = -EDOM;
  174. }
  175. else
  176. {
  177. FAR struct timeval *tv = (FAR struct timeval *)value;
  178. if (tv == NULL)
  179. {
  180. ret = -EINVAL;
  181. }
  182. else
  183. {
  184. unsigned int dsecs;
  185. /* Get the IDLE time value. Any microsecond remainder will
  186. * be forced to the next larger, whole decisecond value.
  187. */
  188. dsecs = (socktimeo_t)net_timeval2dsec(tv, TV2DS_CEIL);
  189. if (dsecs > UINT16_MAX)
  190. {
  191. nwarn("WARNING: TCP_KEEPINTVL value out of range: %u\n",
  192. dsecs);
  193. ret = -EDOM;
  194. }
  195. else
  196. {
  197. conn->keepintvl = (uint16_t)dsecs;
  198. conn->keeptime = clock_systimer(); /* Reset start time */
  199. ret = OK;
  200. }
  201. }
  202. }
  203. break;
  204. case TCP_KEEPCNT: /* Number of keepalives before death */
  205. if (value_len != sizeof(int))
  206. {
  207. ret = -EDOM;
  208. }
  209. else
  210. {
  211. int keepcnt = *(FAR int *)value;
  212. if (keepcnt < 0 || keepcnt > UINT8_MAX)
  213. {
  214. nerr("ERROR: TCP_KEEPCNT value out of range: %d\n", keepcnt);
  215. return -EDOM;
  216. }
  217. else
  218. {
  219. conn->keepcnt = (uint8_t)keepcnt;
  220. conn->keeptime = clock_systimer(); /* Reset start time */
  221. ret = OK;
  222. }
  223. }
  224. break;
  225. default:
  226. nerr("ERROR: Unrecognized TCP option: %d\n", option);
  227. ret = -ENOPROTOOPT;
  228. break;
  229. }
  230. return ret;
  231. #else
  232. return -ENOPROTOOPT;
  233. #endif /* CONFIG_NET_TCP_KEEPALIVE */
  234. }
  235. #endif /* CONFIG_NET_TCPPROTO_OPTIONS */