buffers_net.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file buffers_net.c
  8. * \brief Read and write data on a buf_t object.
  9. **/
  10. #define BUFFERS_PRIVATE
  11. #include "lib/net/buffers_net.h"
  12. #include "lib/buf/buffers.h"
  13. #include "lib/log/log.h"
  14. #include "lib/log/util_bug.h"
  15. #include "lib/net/nettypes.h"
  16. #ifdef _WIN32
  17. #include <winsock2.h>
  18. #endif
  19. #include <stdlib.h>
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #ifdef PARANOIA
  24. /** Helper: If PARANOIA is defined, assert that the buffer in local variable
  25. * <b>buf</b> is well-formed. */
  26. #define check() STMT_BEGIN buf_assert_ok(buf); STMT_END
  27. #else
  28. #define check() STMT_NIL
  29. #endif /* defined(PARANOIA) */
  30. /** Read up to <b>at_most</b> bytes from the file descriptor <b>fd</b> into
  31. * <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
  32. * *<b>reached_eof</b> to 1. Uses <b>tor_socket_recv()</b> iff <b>is_socket</b>
  33. * is true, otherwise it uses <b>read()</b>. Return -1 on error (and sets
  34. * *<b>error</b> to errno), 0 on eof or blocking, and the number of bytes read
  35. * otherwise. */
  36. static inline int
  37. read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most,
  38. int *reached_eof, int *error, bool is_socket)
  39. {
  40. ssize_t read_result;
  41. if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
  42. at_most = CHUNK_REMAINING_CAPACITY(chunk);
  43. if (is_socket)
  44. read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
  45. else
  46. read_result = read(fd, CHUNK_WRITE_PTR(chunk), at_most);
  47. if (read_result < 0) {
  48. int e = is_socket ? tor_socket_errno(fd) : errno;
  49. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  50. #ifdef _WIN32
  51. if (e == WSAENOBUFS)
  52. log_warn(LD_NET, "%s() failed: WSAENOBUFS. Not enough ram?",
  53. is_socket ? "recv" : "read");
  54. #endif
  55. if (error)
  56. *error = e;
  57. return -1;
  58. }
  59. return 0; /* would block. */
  60. } else if (read_result == 0) {
  61. log_debug(LD_NET,"Encountered eof on fd %d", (int)fd);
  62. *reached_eof = 1;
  63. return 0;
  64. } else { /* actually got bytes. */
  65. buf->datalen += read_result;
  66. chunk->datalen += read_result;
  67. log_debug(LD_NET,"Read %ld bytes. %d on inbuf.", (long)read_result,
  68. (int)buf->datalen);
  69. tor_assert(read_result < INT_MAX);
  70. return (int)read_result;
  71. }
  72. }
  73. /** Read from file descriptor <b>fd</b>, writing onto end of <b>buf</b>. Read
  74. * at most <b>at_most</b> bytes, growing the buffer as necessary. If recv()
  75. * returns 0 (because of EOF), set *<b>reached_eof</b> to 1 and return 0.
  76. * Return -1 on error; else return the number of bytes read.
  77. */
  78. /* XXXX indicate "read blocked" somehow? */
  79. static int
  80. buf_read_from_fd(buf_t *buf, int fd, size_t at_most,
  81. int *reached_eof,
  82. int *socket_error,
  83. bool is_socket)
  84. {
  85. /* XXXX It's stupid to overload the return values for these functions:
  86. * "error status" and "number of bytes read" are not mutually exclusive.
  87. */
  88. int r = 0;
  89. size_t total_read = 0;
  90. check();
  91. tor_assert(reached_eof);
  92. tor_assert(SOCKET_OK(fd));
  93. if (BUG(buf->datalen >= INT_MAX))
  94. return -1;
  95. if (BUG(buf->datalen >= INT_MAX - at_most))
  96. return -1;
  97. while (at_most > total_read) {
  98. size_t readlen = at_most - total_read;
  99. chunk_t *chunk;
  100. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  101. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  102. if (readlen > chunk->memlen)
  103. readlen = chunk->memlen;
  104. } else {
  105. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  106. chunk = buf->tail;
  107. if (cap < readlen)
  108. readlen = cap;
  109. }
  110. r = read_to_chunk(buf, chunk, fd, readlen,
  111. reached_eof, socket_error, is_socket);
  112. check();
  113. if (r < 0)
  114. return r; /* Error */
  115. tor_assert(total_read+r < INT_MAX);
  116. total_read += r;
  117. if ((size_t)r < readlen) { /* eof, block, or no more to read. */
  118. break;
  119. }
  120. }
  121. return (int)total_read;
  122. }
  123. /** Helper for buf_flush_to_socket(): try to write <b>sz</b> bytes from chunk
  124. * <b>chunk</b> of buffer <b>buf</b> onto file descriptor <b>fd</b>. On
  125. * success, deduct the bytes written from *<b>buf_flushlen</b>. Return the
  126. * number of bytes written on success, 0 on blocking, -1 on failure.
  127. */
  128. static inline int
  129. flush_chunk(tor_socket_t fd, buf_t *buf, chunk_t *chunk, size_t sz,
  130. size_t *buf_flushlen, bool is_socket)
  131. {
  132. ssize_t write_result;
  133. if (sz > chunk->datalen)
  134. sz = chunk->datalen;
  135. if (is_socket)
  136. write_result = tor_socket_send(fd, chunk->data, sz, 0);
  137. else
  138. write_result = write(fd, chunk->data, sz);
  139. if (write_result < 0) {
  140. int e = is_socket ? tor_socket_errno(fd) : errno;
  141. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  142. #ifdef _WIN32
  143. if (e == WSAENOBUFS)
  144. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  145. #endif
  146. return -1;
  147. }
  148. log_debug(LD_NET,"write() would block, returning.");
  149. return 0;
  150. } else {
  151. *buf_flushlen -= write_result;
  152. buf_drain(buf, write_result);
  153. tor_assert(write_result < INT_MAX);
  154. return (int)write_result;
  155. }
  156. }
  157. /** Write data from <b>buf</b> to the file descriptor <b>fd</b>. Write at most
  158. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  159. * the number of bytes actually written, and remove the written bytes
  160. * from the buffer. Return the number of bytes written on success,
  161. * -1 on failure. Return 0 if write() would block.
  162. */
  163. static int
  164. buf_flush_to_fd(buf_t *buf, int fd, size_t sz,
  165. size_t *buf_flushlen, bool is_socket)
  166. {
  167. /* XXXX It's stupid to overload the return values for these functions:
  168. * "error status" and "number of bytes flushed" are not mutually exclusive.
  169. */
  170. int r;
  171. size_t flushed = 0;
  172. tor_assert(buf_flushlen);
  173. tor_assert(SOCKET_OK(fd));
  174. if (BUG(*buf_flushlen > buf->datalen)) {
  175. *buf_flushlen = buf->datalen;
  176. }
  177. if (BUG(sz > *buf_flushlen)) {
  178. sz = *buf_flushlen;
  179. }
  180. check();
  181. while (sz) {
  182. size_t flushlen0;
  183. tor_assert(buf->head);
  184. if (buf->head->datalen >= sz)
  185. flushlen0 = sz;
  186. else
  187. flushlen0 = buf->head->datalen;
  188. r = flush_chunk(fd, buf, buf->head, flushlen0, buf_flushlen, is_socket);
  189. check();
  190. if (r < 0)
  191. return r;
  192. flushed += r;
  193. sz -= r;
  194. if (r == 0 || (size_t)r < flushlen0) /* can't flush any more now. */
  195. break;
  196. }
  197. tor_assert(flushed < INT_MAX);
  198. return (int)flushed;
  199. }
  200. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  201. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  202. * the number of bytes actually written, and remove the written bytes
  203. * from the buffer. Return the number of bytes written on success,
  204. * -1 on failure. Return 0 if write() would block.
  205. */
  206. int
  207. buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz,
  208. size_t *buf_flushlen)
  209. {
  210. return buf_flush_to_fd(buf, s, sz, buf_flushlen, true);
  211. }
  212. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  213. * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
  214. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  215. * error; else return the number of bytes read.
  216. */
  217. int
  218. buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most,
  219. int *reached_eof,
  220. int *socket_error)
  221. {
  222. return buf_read_from_fd(buf, s, at_most, reached_eof, socket_error, true);
  223. }
  224. /** Write data from <b>buf</b> to the pipe <b>fd</b>. Write at most
  225. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  226. * the number of bytes actually written, and remove the written bytes
  227. * from the buffer. Return the number of bytes written on success,
  228. * -1 on failure. Return 0 if write() would block.
  229. */
  230. int
  231. buf_flush_to_pipe(buf_t *buf, int fd, size_t sz,
  232. size_t *buf_flushlen)
  233. {
  234. return buf_flush_to_fd(buf, fd, sz, buf_flushlen, false);
  235. }
  236. /** Read from pipe <b>fd</b>, writing onto end of <b>buf</b>. Read at most
  237. * <b>at_most</b> bytes, growing the buffer as necessary. If read() returns 0
  238. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  239. * error; else return the number of bytes read.
  240. */
  241. int
  242. buf_read_from_pipe(buf_t *buf, int fd, size_t at_most,
  243. int *reached_eof,
  244. int *socket_error)
  245. {
  246. return buf_read_from_fd(buf, fd, at_most, reached_eof, socket_error, false);
  247. }