aboutsummaryrefslogtreecommitdiff
path: root/src/or/buffers.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2008-02-06 19:34:32 +0000
committerNick Mathewson <nickm@torproject.org>2008-02-06 19:34:32 +0000
commit4d70094b6ebf9557cb7b22ebad6037ab4e221ff8 (patch)
tree96a71a2eb9425018b691401430f4b58dbe922370 /src/or/buffers.c
parent6e2946fb5d59595278bd0f0ef0aa4e092905dc97 (diff)
downloadtor-4d70094b6ebf9557cb7b22ebad6037ab4e221ff8.tar.gz
tor-4d70094b6ebf9557cb7b22ebad6037ab4e221ff8.zip
r17951@catbus: nickm | 2008-02-06 14:34:13 -0500
Add more documentation; change the behavior of read_to_buf_tls to be more consistent. Note a longstanding problem with current read/write interfaces. svn:r13407
Diffstat (limited to 'src/or/buffers.c')
-rw-r--r--src/or/buffers.c41
1 files changed, 24 insertions, 17 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 940c6ccb46..2a89505b92 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -548,7 +548,10 @@ buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
return chunk;
}
-/** DOCDOC */
+/** Read up to <b>at_most</b> bytes from the socket <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, int fd, size_t at_most,
int *reached_eof)
@@ -580,7 +583,8 @@ read_to_chunk(buf_t *buf, chunk_t *chunk, int fd, size_t at_most,
}
}
-/** DOCDOC */
+/** As read_to_chunk(), but return (negative) error code on error, blocking,
+ * or TLS, and the number of bytes read otherwise. */
static INLINE int
read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
size_t at_most)
@@ -597,16 +601,17 @@ read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
}
/** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
- * <b>at_most</b> bytes, resizing the buffer as necessary. If recv()
- * returns 0, set *<b>reached_eof</b> to 1 and return 0. Return -1 on error;
- * else return the number of bytes read. Return 0 if recv() would
- * block.
- *
- * DOCDOC revise
+ * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
+ * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
+ * error; else return the number of bytes read.
*/
+/* XXXX020 indicate "read blocked" somehow? */
int
read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof)
{
+ /* XXXX020 It's stupid to overload the return values for these functions:
+ * "error status" and "number of bytes read" are not mutually exclusive.
+ */
int r = 0;
size_t total_read = 0;
@@ -639,7 +644,8 @@ read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof)
return r;
}
-/** As read_to_buf, but reads from a TLS connection.
+/** As read_to_buf, but reads from a TLS connection, and returns a TLS
+ * status value rather than the number of bytes read.
*
* Using TLS on OR connections complicates matters in two ways.
*
@@ -657,8 +663,6 @@ read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof)
* Second, the TLS stream's events do not correspond directly to network
* events: sometimes, before a TLS stream can read, the network must be
* ready to write -- or vice versa.
- *
- * DOCDOC revise
*/
int
read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
@@ -686,20 +690,20 @@ read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
if (r < 0)
return r; /* Error */
else if ((size_t)r < readlen) /* eof, block, or no more to read. */
- return r + total_read;
+ return r;
total_read += r;
}
return r;
}
-/** Helper for flush_buf(): try to write <b>sz</b> bytes from buffer
- * <b>buf</b> onto socket <b>s</b>. On success, deduct the bytes written
- * from *<b>buf_flushlen</b>.
- * Return the number of bytes written on success, -1 on failure.
+/** Helper for flush_buf(): 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
+ * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes
+ * written on success, 0 on blocking, -1 on failure.
*/
static INLINE int
flush_chunk(int s, buf_t *buf, chunk_t *chunk, size_t sz,
- size_t *buf_flushlen)
+ size_t *buf_flushlen)
{
int write_result;
@@ -764,6 +768,9 @@ flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
int
flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
{
+ /* XXXX020 It's stupid to overload the return values for these functions:
+ * "error status" and "number of bytes flushed" are not mutually exclusive.
+ */
int r;
size_t flushed = 0;
tor_assert(buf_flushlen);