local_accept.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /****************************************************************************
  2. * net/local/local_accept.c
  3. *
  4. * Copyright (C) 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. #if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL_STREAM)
  40. #include <string.h>
  41. #include <errno.h>
  42. #include <assert.h>
  43. #include <queue.h>
  44. #include <debug.h>
  45. #include <nuttx/semaphore.h>
  46. #include <nuttx/net/net.h>
  47. #include "socket/socket.h"
  48. #include "local/local.h"
  49. /****************************************************************************
  50. * Private Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: local_waitlisten
  54. ****************************************************************************/
  55. static int local_waitlisten(FAR struct local_conn_s *server)
  56. {
  57. int ret;
  58. /* Loop until a connection is requested or we receive a signal */
  59. while (dq_empty(&server->u.server.lc_waiters))
  60. {
  61. /* No.. wait for a connection or a signal */
  62. ret = net_lockedwait(&server->lc_waitsem);
  63. if (ret < 0)
  64. {
  65. DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
  66. return ret;
  67. }
  68. }
  69. /* There is a client waiting for the connection */
  70. return OK;
  71. }
  72. /****************************************************************************
  73. * Public Functions
  74. ****************************************************************************/
  75. /****************************************************************************
  76. * Name: local_accept
  77. *
  78. * Description:
  79. * This function implements accept() for Unix domain sockets. See the
  80. * description of accept() for further information.
  81. *
  82. * Input Parameters:
  83. * psock The listening Unix domain socket structure
  84. * addr Receives the address of the connecting client
  85. * addrlen Input: allocated size of 'addr', Return: returned size of 'addr'
  86. * newconn The new, accepted Unix domain connection structure
  87. *
  88. * Returned Value:
  89. * Returns zero (OK) on success or a negated errno value on failure.
  90. * See the description of accept of the possible errno values in the
  91. * description of accept().
  92. *
  93. * Assumptions:
  94. * Network is NOT locked.
  95. *
  96. ****************************************************************************/
  97. int local_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
  98. FAR socklen_t *addrlen, FAR struct socket *newsock)
  99. {
  100. FAR struct local_conn_s *server;
  101. FAR struct local_conn_s *client;
  102. FAR struct local_conn_s *conn;
  103. int ret;
  104. /* Some sanity checks */
  105. DEBUGASSERT(psock && psock->s_conn);
  106. DEBUGASSERT(newsock && !newsock->s_conn);
  107. /* Is the socket a stream? */
  108. if (psock->s_domain != PF_LOCAL || psock->s_type != SOCK_STREAM)
  109. {
  110. return -EOPNOTSUPP;
  111. }
  112. /* Verify that a valid memory block has been provided to receive the
  113. * address
  114. */
  115. server = (FAR struct local_conn_s *)psock->s_conn;
  116. if (server->lc_proto != SOCK_STREAM ||
  117. server->lc_state != LOCAL_STATE_LISTENING ||
  118. server->lc_type != LOCAL_TYPE_PATHNAME)
  119. {
  120. return -EOPNOTSUPP;
  121. }
  122. /* Loop as necessary if we have to wait for a connection */
  123. for (; ; )
  124. {
  125. /* Are there pending connections. Remove the client from the
  126. * head of the waiting list.
  127. */
  128. client = (FAR struct local_conn_s *)
  129. dq_remfirst(&server->u.server.lc_waiters);
  130. if (client)
  131. {
  132. /* Decrement the number of pending clients */
  133. DEBUGASSERT(server->u.server.lc_pending > 0);
  134. server->u.server.lc_pending--;
  135. /* Create a new connection structure for the server side of the
  136. * connection.
  137. */
  138. conn = local_alloc();
  139. if (!conn)
  140. {
  141. nerr("ERROR: Failed to allocate new connection structure\n");
  142. ret = -ENOMEM;
  143. }
  144. else
  145. {
  146. /* Initialize the new connection structure */
  147. conn->lc_crefs = 1;
  148. conn->lc_proto = SOCK_STREAM;
  149. conn->lc_type = LOCAL_TYPE_PATHNAME;
  150. conn->lc_state = LOCAL_STATE_CONNECTED;
  151. strncpy(conn->lc_path, client->lc_path, UNIX_PATH_MAX - 1);
  152. conn->lc_path[UNIX_PATH_MAX - 1] = '\0';
  153. conn->lc_instance_id = client->lc_instance_id;
  154. /* Open the server-side write-only FIFO. This should not
  155. * block.
  156. */
  157. ret = local_open_server_tx(conn,
  158. _SS_ISNONBLOCK(psock->s_flags));
  159. if (ret < 0)
  160. {
  161. nerr("ERROR: Failed to open write-only FIFOs for %s: %d\n",
  162. conn->lc_path, ret);
  163. }
  164. }
  165. /* Do we have a connection? Is the write-side FIFO opened? */
  166. if (ret == OK)
  167. {
  168. DEBUGASSERT(conn->lc_outfile.f_inode != NULL);
  169. /* Open the server-side read-only FIFO. This should not
  170. * block because the client side has already opening it
  171. * for writing.
  172. */
  173. ret = local_open_server_rx(conn,
  174. _SS_ISNONBLOCK(psock->s_flags));
  175. if (ret < 0)
  176. {
  177. nerr("ERROR: Failed to open read-only FIFOs for %s: %d\n",
  178. conn->lc_path, ret);
  179. }
  180. }
  181. /* Do we have a connection? Are the FIFOs opened? */
  182. if (ret == OK)
  183. {
  184. DEBUGASSERT(conn->lc_infile.f_inode != NULL);
  185. /* Return the address family */
  186. if (addr != NULL)
  187. {
  188. ret = local_getaddr(client, addr, addrlen);
  189. }
  190. }
  191. if (ret == OK)
  192. {
  193. /* Setup the client socket structure */
  194. newsock->s_domain = psock->s_domain;
  195. newsock->s_type = SOCK_STREAM;
  196. newsock->s_sockif = psock->s_sockif;
  197. newsock->s_conn = (FAR void *)conn;
  198. }
  199. /* Signal the client with the result of the connection */
  200. client->u.client.lc_result = ret;
  201. nxsem_post(&client->lc_waitsem);
  202. return ret;
  203. }
  204. /* No.. then there should be no pending connections */
  205. DEBUGASSERT(server->u.server.lc_pending == 0);
  206. /* Was the socket opened non-blocking? */
  207. if (_SS_ISNONBLOCK(psock->s_flags))
  208. {
  209. /* Yes.. return EAGAIN */
  210. return -EAGAIN;
  211. }
  212. /* Otherwise, listen for a connection and try again. */
  213. ret = local_waitlisten(server);
  214. if (ret < 0)
  215. {
  216. return ret;
  217. }
  218. }
  219. }
  220. #endif /* CONFIG_NET && CONFIG_NET_LOCAL_STREAM */