recvmsg.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /****************************************************************************
  2. * net/socket/recvmsg.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2017, 2019 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <nuttx/cancelpt.h>
  43. #include <nuttx/net/net.h>
  44. #include "socket/socket.h"
  45. #ifdef CONFIG_NET_CMSG
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: psock_recvmsg
  51. *
  52. * Description:
  53. * psock_recvfrom() receives messages from a socket, and may be used to
  54. * receive data on a socket whether or not it is connection-oriented.
  55. * This is an internal OS interface. It is functionally equivalent to
  56. * recvfrom() except that:
  57. *
  58. * - It is not a cancellation point,
  59. * - It does not modify the errno variable, and
  60. * - I accepts the internal socket structure as an input rather than an
  61. * task-specific socket descriptor.
  62. *
  63. * Input Parameters:
  64. * psock - A pointer to a NuttX-specific, internal socket structure
  65. * msg Buffer to receive msg
  66. * len - Length of buffer
  67. * flags - Receive flags
  68. *
  69. * Returned Value:
  70. * On success, returns the number of characters sent. If no data is
  71. * available to be received and the peer has performed an orderly shutdown,
  72. * recv() will return 0. Otherwise, on any failure, a negated errno value
  73. * is returned (see comments with send() for a list of appropriate errno
  74. * values).
  75. *
  76. ****************************************************************************/
  77. ssize_t psock_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
  78. int flags)
  79. {
  80. /* Verify that non-NULL pointers were passed */
  81. if (msg == NULL)
  82. {
  83. return -EINVAL;
  84. }
  85. if (msg->msg_iovlen != 1)
  86. {
  87. return -ENOTSUP;
  88. }
  89. /* Verify that the sockfd corresponds to valid, allocated socket */
  90. if (psock == NULL || psock->s_crefs <= 0)
  91. {
  92. return -EBADF;
  93. }
  94. /* Let logic specific to this address family handle the recvfrom()
  95. * operation.
  96. */
  97. DEBUGASSERT(psock->s_sockif != NULL &&
  98. (psock->s_sockif->si_recvmsg != NULL ||
  99. psock->s_sockif->si_recvfrom != NULL));
  100. if (psock->s_sockif->si_recvmsg != NULL)
  101. {
  102. return psock->s_sockif->si_recvmsg(psock, msg, flags);
  103. }
  104. else
  105. {
  106. /* Socket doesn't implement si_recvmsg fallback to si_recvfrom */
  107. FAR void *buf = msg->msg_iov->iov_base;
  108. FAR struct sockaddr *from = msg->msg_name;
  109. FAR socklen_t *fromlen = (FAR socklen_t *)&msg->msg_namelen;
  110. size_t len = msg->msg_iov->iov_len;
  111. return psock->s_sockif->si_recvfrom(psock, buf, len, flags, from,
  112. fromlen);
  113. }
  114. }
  115. /****************************************************************************
  116. * Name: nx_recvfrom
  117. *
  118. * Description:
  119. * nx_recvfrom() receives messages from a socket, and may be used to
  120. * receive data on a socket whether or not it is connection-oriented.
  121. * This is an internal OS interface. It is functionally equivalent to
  122. * recvfrom() except that:
  123. *
  124. * - It is not a cancellation point, and
  125. * - It does not modify the errno variable.
  126. *
  127. * Input Parameters:
  128. * sockfd - Socket descriptor of socket
  129. * msg Buffer to receive msg
  130. * len - Length of buffer
  131. * flags - Receive flags
  132. *
  133. * Returned Value:
  134. * On success, returns the number of characters sent. If no data is
  135. * available to be received and the peer has performed an orderly shutdown,
  136. * recv() will return 0. Otherwise, on any failure, a negated errno value
  137. * is returned (see comments with send() for a list of appropriate errno
  138. * values).
  139. *
  140. ****************************************************************************/
  141. ssize_t nx_recvmsg(int sockfd, FAR struct msghdr *msg, int flags)
  142. {
  143. FAR struct socket *psock;
  144. /* Get the underlying socket structure */
  145. psock = sockfd_socket(sockfd);
  146. /* Then let psock_recvmsg() do all of the work */
  147. return psock_recvmsg(psock, msg, flags);
  148. }
  149. /****************************************************************************
  150. * Function: recvmsg
  151. *
  152. * Description:
  153. * The recvmsg() call is identical to recvfrom() with a NULL from
  154. * parameter.
  155. *
  156. * Parameters:
  157. * sockfd Socket descriptor of socket
  158. * msg Buffer to receive msg
  159. * len Length of buffer
  160. * flags Receive flags
  161. *
  162. * Returned Value:
  163. * On success, returns the number of characters received. On error,
  164. * -1 is returned, and errno is set appropriately:
  165. *
  166. * EAGAIN
  167. * The socket is marked non-blocking and the receive operation would
  168. * block, or a receive timeout had been set and the timeout expired
  169. * before data was received.
  170. * EBADF
  171. * The argument sockfd is an invalid descriptor.
  172. * ECONNREFUSED
  173. * A remote host refused to allow the network connection (typically
  174. * because it is not running the requested service).
  175. * EFAULT
  176. * The receive buffer pointer(s) point outside the process's address
  177. * space.
  178. * EINTR
  179. * The receive was interrupted by delivery of a signal before any data
  180. * were available.
  181. * EINVAL
  182. * Invalid argument passed.
  183. * ENOMEM
  184. * Could not allocate memory.
  185. * ENOTCONN
  186. * The socket is associated with a connection-oriented protocol and has
  187. * not been connected.
  188. * ENOTSOCK
  189. * The argument sockfd does not refer to a socket.
  190. *
  191. ****************************************************************************/
  192. ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags)
  193. {
  194. FAR struct socket *psock;
  195. ssize_t ret;
  196. /* recvfrom() is a cancellation point */
  197. enter_cancellation_point();
  198. /* Get the underlying socket structure */
  199. psock = sockfd_socket(sockfd);
  200. /* Let psock_recvfrom() do all of the work */
  201. ret = psock_recvmsg(psock, msg, flags);
  202. if (ret < 0)
  203. {
  204. set_errno(-ret);
  205. ret = ERROR;
  206. }
  207. leave_cancellation_point();
  208. return ret;
  209. }
  210. #endif /* CONFIG_NET_CMSG */