buffers_net.c 5.8 KB

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