From: Alexander Færøy Date: Mon, 10 Sep 2018 12:42:29 +0000 (+0200) Subject: Add buf_flush_to_pipe() and buf_read_from_pipe(). X-Git-Tag: tor-0.4.0.1-alpha~70^2~4^2~28 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=31b3a6577c89492e94836f6e3b4bfc7051a3dc7a;p=thirdparty%2Ftor.git Add buf_flush_to_pipe() and buf_read_from_pipe(). This patch adds two new functions: buf_flush_to_pipe() and buf_read_from_pipe(), which makes use of our new buf_flush_to_fd() and buf_read_from_fd() functions. See: https://bugs.torproject.org/28179 --- diff --git a/src/lib/net/buffers_net.c b/src/lib/net/buffers_net.c index 7c81096e38..1b65819dbb 100644 --- a/src/lib/net/buffers_net.c +++ b/src/lib/net/buffers_net.c @@ -239,3 +239,29 @@ buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most, { return buf_read_from_fd(buf, s, at_most, reached_eof, socket_error, true); } + +/** Write data from buf to the pipe fd. Write at most + * sz bytes, decrement *buf_flushlen by + * the number of bytes actually written, and remove the written bytes + * from the buffer. Return the number of bytes written on success, + * -1 on failure. Return 0 if write() would block. + */ +int +buf_flush_to_pipe(buf_t *buf, int fd, size_t sz, + size_t *buf_flushlen) +{ + return buf_flush_to_fd(buf, fd, sz, buf_flushlen, false); +} + +/** Read from pipe fd, writing onto end of buf. Read at most + * at_most bytes, growing the buffer as necessary. If read() returns 0 + * (because of EOF), set *reached_eof to 1 and return 0. Return -1 on + * error; else return the number of bytes read. + */ +int +buf_read_from_pipe(buf_t *buf, int fd, size_t at_most, + int *reached_eof, + int *socket_error) +{ + return buf_read_from_fd(buf, fd, at_most, reached_eof, socket_error, false); +} diff --git a/src/lib/net/buffers_net.h b/src/lib/net/buffers_net.h index 417f6f9413..8911b082a2 100644 --- a/src/lib/net/buffers_net.h +++ b/src/lib/net/buffers_net.h @@ -24,4 +24,11 @@ int buf_read_from_socket(struct buf_t *buf, tor_socket_t s, size_t at_most, int buf_flush_to_socket(struct buf_t *buf, tor_socket_t s, size_t sz, size_t *buf_flushlen); +int buf_read_from_pipe(struct buf_t *buf, int fd, size_t at_most, + int *reached_eof, + int *socket_error); + +int buf_flush_to_pipe(struct buf_t *buf, int fd, size_t sz, + size_t *buf_flushlen); + #endif /* !defined(TOR_BUFFERS_H) */