buffers_net.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #define BUFFERS_PRIVATE
  7. #include "lib/net/buffers_net.h"
  8. #include "lib/container/buffers.h"
  9. #include "lib/log/torlog.h"
  10. #include "lib/log/util_bug.h"
  11. #include <stdlib.h>
  12. #ifdef PARANOIA
  13. /** Helper: If PARANOIA is defined, assert that the buffer in local variable
  14. * <b>buf</b> is well-formed. */
  15. #define check() STMT_BEGIN buf_assert_ok(buf); STMT_END
  16. #else
  17. #define check() STMT_NIL
  18. #endif /* defined(PARANOIA) */
  19. /** Read up to <b>at_most</b> bytes from the socket <b>fd</b> into
  20. * <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
  21. * *<b>reached_eof</b> to 1. Return -1 on error, 0 on eof or blocking,
  22. * and the number of bytes read otherwise. */
  23. static inline int
  24. read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most,
  25. int *reached_eof, int *socket_error)
  26. {
  27. ssize_t read_result;
  28. if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
  29. at_most = CHUNK_REMAINING_CAPACITY(chunk);
  30. read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
  31. if (read_result < 0) {
  32. int e = tor_socket_errno(fd);
  33. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  34. #ifdef _WIN32
  35. if (e == WSAENOBUFS)
  36. log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
  37. #endif
  38. *socket_error = e;
  39. return -1;
  40. }
  41. return 0; /* would block. */
  42. } else if (read_result == 0) {
  43. log_debug(LD_NET,"Encountered eof on fd %d", (int)fd);
  44. *reached_eof = 1;
  45. return 0;
  46. } else { /* actually got bytes. */
  47. buf->datalen += read_result;
  48. chunk->datalen += read_result;
  49. log_debug(LD_NET,"Read %ld bytes. %d on inbuf.", (long)read_result,
  50. (int)buf->datalen);
  51. tor_assert(read_result < INT_MAX);
  52. return (int)read_result;
  53. }
  54. }
  55. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  56. * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
  57. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  58. * error; else return the number of bytes read.
  59. */
  60. /* XXXX indicate "read blocked" somehow? */
  61. int
  62. buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most,
  63. int *reached_eof,
  64. int *socket_error)
  65. {
  66. /* XXXX It's stupid to overload the return values for these functions:
  67. * "error status" and "number of bytes read" are not mutually exclusive.
  68. */
  69. int r = 0;
  70. size_t total_read = 0;
  71. check();
  72. tor_assert(reached_eof);
  73. tor_assert(SOCKET_OK(s));
  74. if (BUG(buf->datalen >= INT_MAX))
  75. return -1;
  76. if (BUG(buf->datalen >= INT_MAX - at_most))
  77. return -1;
  78. while (at_most > total_read) {
  79. size_t readlen = at_most - total_read;
  80. chunk_t *chunk;
  81. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  82. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  83. if (readlen > chunk->memlen)
  84. readlen = chunk->memlen;
  85. } else {
  86. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  87. chunk = buf->tail;
  88. if (cap < readlen)
  89. readlen = cap;
  90. }
  91. r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error);
  92. check();
  93. if (r < 0)
  94. return r; /* Error */
  95. tor_assert(total_read+r < INT_MAX);
  96. total_read += r;
  97. if ((size_t)r < readlen) { /* eof, block, or no more to read. */
  98. break;
  99. }
  100. }
  101. return (int)total_read;
  102. }
  103. /** Helper for buf_flush_to_socket(): try to write <b>sz</b> bytes from chunk
  104. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. On success, deduct
  105. * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  106. * written on success, 0 on blocking, -1 on failure.
  107. */
  108. static inline int
  109. flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz,
  110. size_t *buf_flushlen)
  111. {
  112. ssize_t write_result;
  113. if (sz > chunk->datalen)
  114. sz = chunk->datalen;
  115. write_result = tor_socket_send(s, chunk->data, sz, 0);
  116. if (write_result < 0) {
  117. int e = tor_socket_errno(s);
  118. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  119. #ifdef _WIN32
  120. if (e == WSAENOBUFS)
  121. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  122. #endif
  123. return -1;
  124. }
  125. log_debug(LD_NET,"write() would block, returning.");
  126. return 0;
  127. } else {
  128. *buf_flushlen -= write_result;
  129. buf_drain(buf, write_result);
  130. tor_assert(write_result < INT_MAX);
  131. return (int)write_result;
  132. }
  133. }
  134. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  135. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  136. * the number of bytes actually written, and remove the written bytes
  137. * from the buffer. Return the number of bytes written on success,
  138. * -1 on failure. Return 0 if write() would block.
  139. */
  140. int
  141. buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz,
  142. size_t *buf_flushlen)
  143. {
  144. /* XXXX It's stupid to overload the return values for these functions:
  145. * "error status" and "number of bytes flushed" are not mutually exclusive.
  146. */
  147. int r;
  148. size_t flushed = 0;
  149. tor_assert(buf_flushlen);
  150. tor_assert(SOCKET_OK(s));
  151. if (BUG(*buf_flushlen > buf->datalen)) {
  152. *buf_flushlen = buf->datalen;
  153. }
  154. if (BUG(sz > *buf_flushlen)) {
  155. sz = *buf_flushlen;
  156. }
  157. check();
  158. while (sz) {
  159. size_t flushlen0;
  160. tor_assert(buf->head);
  161. if (buf->head->datalen >= sz)
  162. flushlen0 = sz;
  163. else
  164. flushlen0 = buf->head->datalen;
  165. r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen);
  166. check();
  167. if (r < 0)
  168. return r;
  169. flushed += r;
  170. sz -= r;
  171. if (r == 0 || (size_t)r < flushlen0) /* can't flush any more now. */
  172. break;
  173. }
  174. tor_assert(flushed < INT_MAX);
  175. return (int)flushed;
  176. }