udp_setsockopt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /****************************************************************************
  2. * net/udp/udp_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/udp.h>
  45. #include <nuttx/clock.h>
  46. #include <nuttx/net/net.h>
  47. #include <nuttx/net/udp.h>
  48. #include "socket/socket.h"
  49. #include "utils/utils.h"
  50. #include "netdev/netdev.h"
  51. #include "udp/udp.h"
  52. #ifdef CONFIG_NET_UDPPROTO_OPTIONS
  53. /****************************************************************************
  54. * Public Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: udp_setsockopt
  58. *
  59. * Description:
  60. * udp_setsockopt() sets the UDP-protocol option specified by the
  61. * 'option' argument to the value pointed to by the 'value' argument for
  62. * the socket specified by the 'psock' argument.
  63. *
  64. * See <netinet/udp.h> for the a complete list of values of UDP protocol
  65. * options.
  66. *
  67. * Input Parameters:
  68. * psock Socket structure of socket to operate on
  69. * option identifies the option to set
  70. * value Points to the argument value
  71. * value_len The length of the argument value
  72. *
  73. * Returned Value:
  74. * Returns zero (OK) on success. On failure, it returns a negated errno
  75. * value to indicate the nature of the error. See psock_setcockopt() for
  76. * the list of possible error values.
  77. *
  78. ****************************************************************************/
  79. int udp_setsockopt(FAR struct socket *psock, int option,
  80. FAR const void *value, socklen_t value_len)
  81. {
  82. #ifdef CONFIG_NET_UDP_BINDTODEVICE
  83. /* Keep alive options are the only UDP protocol socket option currently
  84. * supported.
  85. */
  86. FAR struct udp_conn_s *conn;
  87. int ret;
  88. DEBUGASSERT(psock != NULL && value != NULL && psock->s_conn != NULL);
  89. conn = (FAR struct udp_conn_s *)psock->s_conn;
  90. /* All of the UDP protocol options apply only UDP sockets. The sockets
  91. * do not have to be connected.. that might occur later with the KeepAlive
  92. * already configured.
  93. */
  94. if (psock->s_type != SOCK_DGRAM)
  95. {
  96. nerr("ERROR: Not a UDP socket\n");
  97. return -ENOTCONN;
  98. }
  99. /* Handle the UDP-protocol options */
  100. switch (option)
  101. {
  102. #ifdef CONFIG_NET_UDP_BINDTODEVICE
  103. /* Handle the UDP_BINDTODEVICE socket-level option.
  104. *
  105. * NOTE: UDP_BINDTODEVICE is declared in linux as SO_BINDTODEVICE,
  106. * but this option only makes sense for UDP sockets trying to broadcast
  107. * while their local address is not set, eg, with DHCP requests.
  108. * The problem is that we are not able to determine the interface to be
  109. * used for sending packets when multiple interfaces do not have a local
  110. * address yet. This option can be used to "force" the interface used to
  111. * send the UDP traffic in this connection. Note that it does NOT only
  112. * apply to broadcast packets.
  113. */
  114. case UDP_BINDTODEVICE: /* Bind socket to a specific network device */
  115. if (value == NULL || value_len == 0 ||
  116. (value_len > 0 && ((FAR char *)value)[0] == 0))
  117. {
  118. conn->boundto = 0; /* This interface is no longer bound */
  119. ret = OK;
  120. }
  121. else
  122. {
  123. int ifindex;
  124. /* Get the interface index corresponding to the interface name */
  125. ifindex = netdev_nametoindex(value);
  126. if (ifindex >= 0)
  127. {
  128. DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
  129. conn->boundto = ifindex;
  130. ret = OK;
  131. }
  132. else
  133. {
  134. ret = ifindex;
  135. }
  136. }
  137. break;
  138. #endif
  139. default:
  140. nerr("ERROR: Unrecognized UDP option: %d\n", option);
  141. ret = -ENOPROTOOPT;
  142. break;
  143. }
  144. return ret;
  145. #else
  146. return -ENOPROTOOPT;
  147. #endif /* CONFIG_NET_UDP_BINDTODEVICE */
  148. }
  149. #endif /* CONFIG_NET_UDPPROTO_OPTIONS */