Refactor flush_chunk() to work on pipes as well as sockets.

See: https://bugs.torproject.org/28179
This commit is contained in:
Alexander Færøy 2018-09-10 13:28:01 +02:00
parent 5f26ae833e
commit 340260281a

View file

@ -129,22 +129,26 @@ buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most,
} }
/** Helper for buf_flush_to_socket(): try to write <b>sz</b> bytes from chunk /** Helper for buf_flush_to_socket(): try to write <b>sz</b> bytes from chunk
* <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. On success, deduct * <b>chunk</b> of buffer <b>buf</b> onto file descriptor <b>fd</b>. On
* the bytes written from *<b>buf_flushlen</b>. Return the number of bytes * success, deduct the bytes written from *<b>buf_flushlen</b>. Return the
* written on success, 0 on blocking, -1 on failure. * number of bytes written on success, 0 on blocking, -1 on failure.
*/ */
static inline int static inline int
flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz, flush_chunk(tor_socket_t fd, buf_t *buf, chunk_t *chunk, size_t sz,
size_t *buf_flushlen) size_t *buf_flushlen, bool is_socket)
{ {
ssize_t write_result; ssize_t write_result;
if (sz > chunk->datalen) if (sz > chunk->datalen)
sz = chunk->datalen; sz = chunk->datalen;
write_result = tor_socket_send(s, chunk->data, sz, 0);
if (is_socket)
write_result = tor_socket_send(fd, chunk->data, sz, 0);
else
write_result = write(fd, chunk->data, sz);
if (write_result < 0) { if (write_result < 0) {
int e = tor_socket_errno(s); int e = tor_socket_errno(fd);
if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */ if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
#ifdef _WIN32 #ifdef _WIN32
if (e == WSAENOBUFS) if (e == WSAENOBUFS)
@ -195,7 +199,7 @@ buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz,
else else
flushlen0 = buf->head->datalen; flushlen0 = buf->head->datalen;
r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen); r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen, true);
check(); check();
if (r < 0) if (r < 0)
return r; return r;