sendto.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /****************************************************************************
  2. * net/socket/sendto.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2015, 2017 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/types.h>
  40. #include <sys/socket.h>
  41. #include <stdint.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/cancelpt.h>
  46. #include <nuttx/net/net.h>
  47. #include "socket/socket.h"
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: psock_sendto
  53. *
  54. * Description:
  55. * If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
  56. * socket, the parameters to and 'tolen' are ignored (and the error EISCONN
  57. * may be returned when they are not NULL and 0), and the error ENOTCONN is
  58. * returned when the socket was not actually connected.
  59. *
  60. * Input Parameters:
  61. * psock A pointer to a NuttX-specific, internal socket structure
  62. * buf Data to send
  63. * len Length of data to send
  64. * flags Send flags
  65. * to Address of recipient
  66. * tolen The length of the address structure
  67. *
  68. * Returned Value:
  69. * On success, returns the number of characters sent. On a negated errno
  70. * value is returned. One of:
  71. *
  72. * EAGAIN or EWOULDBLOCK
  73. * The socket is marked non-blocking and the requested operation
  74. * would block.
  75. * EBADF
  76. * An invalid descriptor was specified.
  77. * ECONNRESET
  78. * Connection reset by peer.
  79. * EDESTADDRREQ
  80. * The socket is not connection-mode, and no peer address is set.
  81. * EFAULT
  82. * An invalid user space address was specified for a parameter.
  83. * EINTR
  84. * A signal occurred before any data was transmitted.
  85. * EINVAL
  86. * Invalid argument passed.
  87. * EISCONN
  88. * The connection-mode socket was connected already but a recipient
  89. * was specified. (Now either this error is returned, or the recipient
  90. * specification is ignored.)
  91. * EMSGSIZE
  92. * The socket type requires that message be sent atomically, and the
  93. * size of the message to be sent made this impossible.
  94. * ENOBUFS
  95. * The output queue for a network interface was full. This generally
  96. * indicates that the interface has stopped sending, but may be
  97. * caused by transient congestion.
  98. * ENOMEM
  99. * No memory available.
  100. * ENOTCONN
  101. * The socket is not connected, and no target has been given.
  102. * ENOTSOCK
  103. * The argument s is not a socket.
  104. * EOPNOTSUPP
  105. * Some bit in the flags argument is inappropriate for the socket
  106. * type.
  107. * EPIPE
  108. * The local end has been shut down on a connection oriented socket.
  109. * In this case the process will also receive a SIGPIPE unless
  110. * MSG_NOSIGNAL is set.
  111. *
  112. ****************************************************************************/
  113. ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf,
  114. size_t len, int flags, FAR const struct sockaddr *to,
  115. socklen_t tolen)
  116. {
  117. ssize_t nsent;
  118. /* Verify that non-NULL pointers were passed */
  119. #ifdef CONFIG_DEBUG_FEATURES
  120. if (buf == NULL)
  121. {
  122. return -EINVAL;
  123. }
  124. #endif
  125. /* If to is NULL or tolen is zero, then this function is same as send (for
  126. * connected socket types)
  127. */
  128. if (to == NULL || tolen <= 0)
  129. {
  130. #if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_LOCAL_STREAM) || \
  131. defined(CONFIG_NET_USRSOCK)
  132. return psock_send(psock, buf, len, flags);
  133. #else
  134. nerr("ERROR: No 'to' address\n");
  135. return -EINVAL;
  136. #endif
  137. }
  138. /* Verify that the psock corresponds to valid, allocated socket */
  139. if (psock == NULL || psock->s_crefs <= 0)
  140. {
  141. nerr("ERROR: Invalid socket\n");
  142. return -EBADF;
  143. }
  144. /* Let the address family's sendto() method handle the operation */
  145. DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_sendto != NULL);
  146. nsent = psock->s_sockif->si_sendto(psock, buf, len, flags, to, tolen);
  147. /* Check if the domain-specific sendto() logic failed */
  148. if (nsent < 0)
  149. {
  150. nerr("ERROR: Family-specific send failed: %ld\n", (long)nsent);
  151. return nsent;
  152. }
  153. return nsent;
  154. }
  155. /****************************************************************************
  156. * Name: sendto
  157. *
  158. * Description:
  159. * If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
  160. * socket, the parameters to and 'tolen' are ignored (and the error EISCONN
  161. * may be returned when they are not NULL and 0), and the error ENOTCONN is
  162. * returned when the socket was not actually connected.
  163. *
  164. * Input Parameters:
  165. * sockfd Socket descriptor of socket
  166. * buf Data to send
  167. * len Length of data to send
  168. * flags Send flags
  169. * to Address of recipient
  170. * tolen The length of the address structure
  171. *
  172. * Returned Value:
  173. * On success, returns the number of characters sent. On any failure, a
  174. * negated errno value is returned. One of:
  175. *
  176. * EAGAIN or EWOULDBLOCK
  177. * The socket is marked non-blocking and the requested operation
  178. * would block.
  179. * EBADF
  180. * An invalid descriptor was specified.
  181. * ECONNRESET
  182. * Connection reset by peer.
  183. * EDESTADDRREQ
  184. * The socket is not connection-mode, and no peer address is set.
  185. * EFAULT
  186. * An invalid user space address was specified for a parameter.
  187. * EINTR
  188. * A signal occurred before any data was transmitted.
  189. * EINVAL
  190. * Invalid argument passed.
  191. * EISCONN
  192. * The connection-mode socket was connected already but a recipient
  193. * was specified. (Now either this error is returned, or the recipient
  194. * specification is ignored.)
  195. * EMSGSIZE
  196. * The socket type requires that message be sent atomically, and the
  197. * size of the message to be sent made this impossible.
  198. * ENOBUFS
  199. * The output queue for a network interface was full. This generally
  200. * indicates that the interface has stopped sending, but may be
  201. * caused by transient congestion.
  202. * ENOMEM
  203. * No memory available.
  204. * ENOTCONN
  205. * The socket is not connected, and no target has been given.
  206. * ENOTSOCK
  207. * The argument s is not a socket.
  208. * EOPNOTSUPP
  209. * Some bit in the flags argument is inappropriate for the socket
  210. * type.
  211. * EPIPE
  212. * The local end has been shut down on a connection oriented socket.
  213. * In this case the process will also receive a SIGPIPE unless
  214. * MSG_NOSIGNAL is set.
  215. *
  216. ****************************************************************************/
  217. ssize_t sendto(int sockfd, FAR const void *buf, size_t len, int flags,
  218. FAR const struct sockaddr *to, socklen_t tolen)
  219. {
  220. FAR struct socket *psock;
  221. ssize_t ret;
  222. /* sendto() is a cancellation point */
  223. (void)enter_cancellation_point();
  224. /* Get the underlying socket structure */
  225. psock = sockfd_socket(sockfd);
  226. /* And let psock_sendto do all of the work */
  227. ret = psock_sendto(psock, buf, len, flags, to, tolen);
  228. if (ret < 0)
  229. {
  230. set_errno((int)-ret);
  231. ret = ERROR;
  232. }
  233. leave_cancellation_point();
  234. return ret;
  235. }