local_sendto.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. * net/local/local_sendto.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_DGRAM)
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <unistd.h>
  43. #include <errno.h>
  44. #include <assert.h>
  45. #include <debug.h>
  46. #include <nuttx/net/net.h>
  47. #include "socket/socket.h"
  48. #include "local/local.h"
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: psock_local_sendto
  54. *
  55. * Description:
  56. * This function implements the Unix domain-specific logic of the
  57. * standard sendto() socket operation.
  58. *
  59. * Input Parameters:
  60. * psock A pointer to a NuttX-specific, internal socket structure
  61. * buf Data to send
  62. * len Length of data to send
  63. * flags Send flags
  64. * to Address of recipient
  65. * tolen The length of the address structure
  66. *
  67. * NOTE: All input parameters were verified by sendto() before this
  68. * function was called.
  69. *
  70. * Returned Value:
  71. * On success, returns the number of characters sent. On error,
  72. * a negated errno value is returned. See the description in
  73. * net/socket/sendto.c for the list of appropriate return value.
  74. *
  75. ****************************************************************************/
  76. ssize_t psock_local_sendto(FAR struct socket *psock, FAR const void *buf,
  77. size_t len, int flags, FAR const struct sockaddr *to,
  78. socklen_t tolen)
  79. {
  80. FAR struct local_conn_s *conn = (FAR struct local_conn_s *)psock->s_conn;
  81. FAR struct sockaddr_un *unaddr = (FAR struct sockaddr_un *)to;
  82. ssize_t nsent;
  83. int ret;
  84. /* We keep packet sizes in a uint16_t, so there is a upper limit to the
  85. * 'len' that can be supported.
  86. */
  87. DEBUGASSERT(buf && len <= UINT16_MAX);
  88. /* Verify that this is not a connected peer socket. It need not be
  89. * bound, however. If unbound, recvfrom will see this as a nameless
  90. * connection.
  91. */
  92. if (conn->lc_state != LOCAL_STATE_UNBOUND &&
  93. conn->lc_state != LOCAL_STATE_BOUND)
  94. {
  95. /* Either not bound to address or it is connected */
  96. nerr("ERROR: Connected state\n");
  97. return -EISCONN;
  98. }
  99. /* The outgoing FIFO should not be open */
  100. DEBUGASSERT(conn->lc_outfile.f_inode == 0);
  101. /* At present, only standard pathname type address are support */
  102. if (tolen < sizeof(sa_family_t) + 2)
  103. {
  104. /* EFAULT - An invalid user space address was specified for a parameter */
  105. return -EFAULT;
  106. }
  107. /* Make sure that half duplex FIFO has been created.
  108. * REVISIT: Or should be just make sure that it already exists?
  109. */
  110. ret = local_create_halfduplex(conn, unaddr->sun_path);
  111. if (ret < 0)
  112. {
  113. nerr("ERROR: Failed to create FIFO for %s: %d\n",
  114. conn->lc_path, ret);
  115. return ret;
  116. }
  117. /* Open the sending side of the transfer */
  118. ret = local_open_sender(conn, unaddr->sun_path,
  119. _SS_ISNONBLOCK(psock->s_flags));
  120. if (ret < 0)
  121. {
  122. nerr("ERROR: Failed to open FIFO for %s: %d\n",
  123. unaddr->sun_path, ret);
  124. nsent = ret;
  125. goto errout_with_halfduplex;
  126. }
  127. /* Send the packet */
  128. nsent = local_send_packet(&conn->lc_outfile, buf, len);
  129. if (nsent < 0)
  130. {
  131. nerr("ERROR: Failed to send the packet: %d\n", ret);
  132. }
  133. else
  134. {
  135. /* local_send_packet returns 0 if all 'len' bytes were sent */
  136. nsent = len;
  137. }
  138. /* Now we can close the write-only socket descriptor */
  139. file_close(&conn->lc_outfile);
  140. conn->lc_outfile.f_inode = NULL;
  141. errout_with_halfduplex:
  142. /* Release our reference to the half duplex FIFO */
  143. (void)local_release_halfduplex(conn);
  144. return nsent;
  145. }
  146. #endif /* CONFIG_NET && CONFIG_NET_LOCAL_DGRAM */