local_recvutils.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /****************************************************************************
  2. * net/local/local_recvpacket.c
  3. *
  4. * Copyright (C) 2015 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 <stdint.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <errno.h>
  44. #include <assert.h>
  45. #include <debug.h>
  46. #include <nuttx/fs/fs.h>
  47. #include "local/local.h"
  48. #if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: local_fifo_read
  54. *
  55. * Description:
  56. * Read a data from the read-only FIFO.
  57. *
  58. * Input Parameters:
  59. * filep - File structure of write-only FIFO.
  60. * buf - Local to store the received data
  61. * len - Length of data to receive [in]
  62. * Length of data actually received [out]
  63. *
  64. * Returned Value:
  65. * Zero is returned on success; a negated errno value is returned on any
  66. * failure. If -ECONNRESET is received, then the sending side has closed
  67. * the FIFO. In this case, the returned data may still be valid (if the
  68. * returned len > 0).
  69. *
  70. ****************************************************************************/
  71. int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, size_t *len)
  72. {
  73. ssize_t remaining;
  74. ssize_t nread;
  75. int ret;
  76. DEBUGASSERT(buf && len);
  77. remaining = *len;
  78. while (remaining > 0)
  79. {
  80. nread = file_read(filep, buf, remaining);
  81. if (nread < 0)
  82. {
  83. if (nread != -EINTR)
  84. {
  85. ret = (int)nread;
  86. nerr("ERROR: nx_read() failed: %d\n", ret);
  87. goto errout;
  88. }
  89. ninfo("Ignoring signal\n");
  90. }
  91. else if (nread == 0)
  92. {
  93. /* The FIFO returns zero if the sending side of the connection
  94. * has closed the FIFO.
  95. */
  96. ret = -ECONNRESET;
  97. goto errout;
  98. }
  99. else
  100. {
  101. DEBUGASSERT(nread <= remaining);
  102. remaining -= nread;
  103. buf += nread;
  104. }
  105. }
  106. ret = OK;
  107. errout:
  108. *len -= remaining;
  109. return ret;
  110. }
  111. /****************************************************************************
  112. * Name: local_sync
  113. *
  114. * Description:
  115. * Read a sync bytes until the start of the packet is found.
  116. *
  117. * Input Parameters:
  118. * filep - File structure of write-only FIFO.
  119. *
  120. * Returned Value:
  121. * The non-zero size of the following packet is returned on success; a
  122. * negated errno value is returned on any failure.
  123. *
  124. ****************************************************************************/
  125. int local_sync(FAR struct file *filep)
  126. {
  127. size_t readlen;
  128. uint16_t pktlen;
  129. uint8_t sync;
  130. int ret;
  131. /* Loop until a valid pre-amble is encountered: SYNC bytes followed
  132. * by one END byte.
  133. */
  134. do
  135. {
  136. /* Read until we encounter a sync byte */
  137. do
  138. {
  139. readlen = sizeof(uint8_t);
  140. ret = local_fifo_read(filep, &sync, &readlen);
  141. if (ret < 0)
  142. {
  143. nerr("ERROR: Failed to read sync bytes: %d\n", ret);
  144. return ret;
  145. }
  146. }
  147. while (sync != LOCAL_SYNC_BYTE);
  148. /* Then read to the end of the SYNC sequence */
  149. do
  150. {
  151. readlen = sizeof(uint8_t);
  152. ret = local_fifo_read(filep, &sync, &readlen);
  153. if (ret < 0)
  154. {
  155. nerr("ERROR: Failed to read sync bytes: %d\n", ret);
  156. return ret;
  157. }
  158. }
  159. while (sync == LOCAL_SYNC_BYTE);
  160. }
  161. while (sync != LOCAL_END_BYTE);
  162. /* Then read the packet length */
  163. readlen = sizeof(uint16_t);
  164. ret = local_fifo_read(filep, (FAR uint8_t *)&pktlen, &readlen);
  165. return ret < 0 ? ret : pktlen;
  166. }
  167. /****************************************************************************
  168. * Name: local_getaddr
  169. *
  170. * Description:
  171. * Return the Unix domain address of a connection.
  172. *
  173. * Input Parameters:
  174. * conn - The connection
  175. * addr - The location to return the address
  176. * addrlen - The size of the memory allocated by the caller to receive the
  177. * address.
  178. *
  179. * Returned Value:
  180. * Zero (OK) on success; a negated errno value on failure.
  181. *
  182. ****************************************************************************/
  183. int local_getaddr(FAR struct local_conn_s *conn, FAR struct sockaddr *addr,
  184. FAR socklen_t *addrlen)
  185. {
  186. FAR struct sockaddr_un *unaddr;
  187. int totlen;
  188. int pathlen;
  189. DEBUGASSERT(conn && addr && addrlen && *addrlen >= sizeof(sa_family_t));
  190. /* Get the length of the path (minus the NUL terminator) and the length
  191. * of the whole Unix domain address.
  192. */
  193. pathlen = strnlen(conn->lc_path, UNIX_PATH_MAX - 1);
  194. totlen = sizeof(sa_family_t) + pathlen + 1;
  195. /* If the length of the whole Unix domain address is larger than the
  196. * buffer provided by the caller, then truncate the address to fit.
  197. */
  198. if (totlen > *addrlen)
  199. {
  200. pathlen -= (totlen - *addrlen);
  201. totlen = *addrlen;
  202. }
  203. /* Copy the Unix domain address */
  204. unaddr = (FAR struct sockaddr_un *)addr;
  205. unaddr->sun_family = AF_LOCAL;
  206. memcpy(unaddr->sun_path, conn->lc_path, pathlen);
  207. unaddr->sun_path[pathlen] = '\0';
  208. /* Return the Unix domain address size */
  209. *addrlen = totlen;
  210. return OK;
  211. }
  212. #endif /* CONFIG_NET && CONFIG_NET_LOCAL */