buffers_net.c 8.4 KB

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