aboutsummaryrefslogtreecommitdiff
path: root/src/lib/net
diff options
context:
space:
mode:
authorAlexander Færøy <ahf@torproject.org>2018-09-10 13:23:59 +0200
committerAlexander Færøy <ahf@torproject.org>2018-11-27 19:31:08 +0100
commit5f26ae833eea79e56cb8cb8133b16a9cb0944ae4 (patch)
treea72c2c2f97d6052186cf2e14dba6617ccc4bdcfa /src/lib/net
parent2a3eef4404415f1bb90a86c9cc396b6dee89039c (diff)
downloadtor-5f26ae833eea79e56cb8cb8133b16a9cb0944ae4.tar.gz
tor-5f26ae833eea79e56cb8cb8133b16a9cb0944ae4.zip
Refactor read_to_chunk() such that it supports both pipes and sockets.
See: https://bugs.torproject.org/28179
Diffstat (limited to 'src/lib/net')
-rw-r--r--src/lib/net/buffers_net.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/lib/net/buffers_net.c b/src/lib/net/buffers_net.c
index c52ea2784e..bd420510ab 100644
--- a/src/lib/net/buffers_net.c
+++ b/src/lib/net/buffers_net.c
@@ -21,6 +21,7 @@
#endif
#include <stdlib.h>
+#include <unistd.h>
#ifdef PARANOIA
/** Helper: If PARANOIA is defined, assert that the buffer in local variable
@@ -30,27 +31,33 @@
#define check() STMT_NIL
#endif /* defined(PARANOIA) */
-/** Read up to <b>at_most</b> bytes from the socket <b>fd</b> into
+/** Read up to <b>at_most</b> bytes from the file descriptor <b>fd</b> into
* <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
* *<b>reached_eof</b> to 1. Return -1 on error, 0 on eof or blocking,
* and the number of bytes read otherwise. */
static inline int
read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most,
- int *reached_eof, int *socket_error)
+ int *reached_eof, int *error, bool is_socket)
{
ssize_t read_result;
if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
at_most = CHUNK_REMAINING_CAPACITY(chunk);
- read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
+
+ if (is_socket)
+ read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
+ else
+ read_result = read(fd, CHUNK_WRITE_PTR(chunk), at_most);
if (read_result < 0) {
int e = tor_socket_errno(fd);
if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
#ifdef _WIN32
if (e == WSAENOBUFS)
- log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
+ log_warn(LD_NET, "%s() failed: WSAENOBUFS. Not enough ram?",
+ is_socket ? "recv" : "read");
#endif
- *socket_error = e;
+ if (error)
+ *error = e;
return -1;
}
return 0; /* would block. */
@@ -108,7 +115,7 @@ buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most,
readlen = cap;
}
- r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error);
+ r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error, true);
check();
if (r < 0)
return r; /* Error */