diff options
author | Nick Mathewson <nickm@torproject.org> | 2017-08-08 15:16:39 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-09-05 13:57:51 -0400 |
commit | 4a7e90adc5828263a5d2dc3744818f2189c04bc7 (patch) | |
tree | 62cac4a9acefb41d9e3b9b6ee7d6501ee64efc01 /src/common | |
parent | 336aa21e37b6c5bd4f1215ceba27c21f6c43dce7 (diff) | |
download | tor-4a7e90adc5828263a5d2dc3744818f2189c04bc7.tar.gz tor-4a7e90adc5828263a5d2dc3744818f2189c04bc7.zip |
Repair buffer API so everything starts with buf_.
Our convention is that functions which manipulate a type T should be
named T_foo. But the buffer functions were super old, and followed
all kinds of conventions. Now they're uniform.
Here's the perl I used to do this:
\#!/usr/bin/perl -w -i -p
s/read_to_buf\(/buf_read_from_socket\(/;
s/flush_buf\(/buf_flush_to_socket\(/;
s/read_to_buf_tls\(/buf_read_from_tls\(/;
s/flush_buf_tls\(/buf_flush_to_tls\(/;
s/write_to_buf\(/buf_add\(/;
s/write_to_buf_compress\(/buf_add_compress\(/;
s/move_buf_to_buf\(/buf_move_to_buf\(/;
s/peek_from_buf\(/buf_peek\(/;
s/fetch_from_buf\(/buf_get_bytes\(/;
s/fetch_from_buf_line\(/buf_get_line\(/;
s/fetch_from_buf_line\(/buf_get_line\(/;
s/buf_remove_from_front\(/buf_drain\(/;
s/peek_buf_startswith\(/buf_peek_startswith\(/;
s/assert_buf_ok\(/buf_assert_ok\(/;
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/buffers.c | 48 | ||||
-rw-r--r-- | src/common/buffers.h | 22 | ||||
-rw-r--r-- | src/common/buffers_tls.c | 10 | ||||
-rw-r--r-- | src/common/buffers_tls.h | 4 |
4 files changed, 42 insertions, 42 deletions
diff --git a/src/common/buffers.c b/src/common/buffers.c index 1a9144e14b..683952ddf2 100644 --- a/src/common/buffers.c +++ b/src/common/buffers.c @@ -36,7 +36,7 @@ #ifdef PARANOIA /** Helper: If PARANOIA is defined, assert that the buffer in local variable * <b>buf</b> is well-formed. */ -#define check() STMT_BEGIN assert_buf_ok(buf); STMT_END +#define check() STMT_BEGIN buf_assert_ok(buf); STMT_END #else #define check() STMT_NIL #endif @@ -282,14 +282,14 @@ buf_new_with_data(const char *cp, size_t sz) /* Allocate a buffer */ buf_t *buf = buf_new_with_capacity(sz); tor_assert(buf); - assert_buf_ok(buf); + buf_assert_ok(buf); tor_assert(!buf->head); /* Allocate a chunk that is sz bytes long */ buf->head = chunk_new_with_alloc_size(CHUNK_ALLOC_SIZE(sz)); buf->tail = buf->head; tor_assert(buf->head); - assert_buf_ok(buf); + buf_assert_ok(buf); tor_assert(buf_allocation(buf) >= sz); /* Copy the data and size the buffers */ @@ -299,7 +299,7 @@ buf_new_with_data(const char *cp, size_t sz) buf->datalen = sz; buf->head->datalen = sz; buf->head->data = &buf->head->mem[0]; - assert_buf_ok(buf); + buf_assert_ok(buf); /* Make sure everything is large enough */ tor_assert(buf_allocation(buf) >= sz); @@ -315,7 +315,7 @@ buf_new_with_data(const char *cp, size_t sz) /** Remove the first <b>n</b> bytes from buf. */ void -buf_remove_from_front(buf_t *buf, size_t n) +buf_drain(buf_t *buf, size_t n) { tor_assert(buf->datalen >= n); while (n) { @@ -551,7 +551,7 @@ read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most, */ /* XXXX indicate "read blocked" somehow? */ int -read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof, +buf_read_from_socket(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof, int *socket_error) { /* XXXX It's stupid to overload the return values for these functions: @@ -596,7 +596,7 @@ read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof, return (int)total_read; } -/** Helper for flush_buf(): 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 * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes * written on success, 0 on blocking, -1 on failure. @@ -624,7 +624,7 @@ flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz, return 0; } else { *buf_flushlen -= write_result; - buf_remove_from_front(buf, write_result); + buf_drain(buf, write_result); tor_assert(write_result < INT_MAX); return (int)write_result; } @@ -637,7 +637,7 @@ flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz, * -1 on failure. Return 0 if write() would block. */ int -flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen) +buf_flush_to_socket(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen) { /* XXXX It's stupid to overload the return values for these functions: * "error status" and "number of bytes flushed" are not mutually exclusive. @@ -677,7 +677,7 @@ flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen) * Return the new length of the buffer on success, -1 on failure. */ int -write_to_buf(const char *string, size_t string_len, buf_t *buf) +buf_add(const char *string, size_t string_len, buf_t *buf) { if (!string_len) return (int)buf->datalen; @@ -712,14 +712,14 @@ write_to_buf(const char *string, size_t string_len, buf_t *buf) * onto <b>string</b>. */ void -peek_from_buf(char *string, size_t string_len, const buf_t *buf) +buf_peek(char *string, size_t string_len, const buf_t *buf) { chunk_t *chunk; tor_assert(string); /* make sure we don't ask for too much */ tor_assert(string_len <= buf->datalen); - /* assert_buf_ok(buf); */ + /* buf_assert_ok(buf); */ chunk = buf->head; while (string_len) { @@ -739,7 +739,7 @@ peek_from_buf(char *string, size_t string_len, const buf_t *buf) * must be \<= the number of bytes on the buffer. */ int -fetch_from_buf(char *string, size_t string_len, buf_t *buf) +buf_get_bytes(char *string, size_t string_len, buf_t *buf) { /* There must be string_len bytes in buf; write them onto string, * then memmove buf back (that is, remove them from buf). @@ -747,8 +747,8 @@ fetch_from_buf(char *string, size_t string_len, buf_t *buf) * Return the number of bytes still on the buffer. */ check(); - peek_from_buf(string, string_len, buf); - buf_remove_from_front(buf, string_len); + buf_peek(string, string_len, buf); + buf_drain(buf, string_len); check(); tor_assert(buf->datalen < INT_MAX); return (int)buf->datalen; @@ -759,7 +759,7 @@ fetch_from_buf(char *string, size_t string_len, buf_t *buf) * Return the number of bytes actually copied. */ int -move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen) +buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen) { /* We can do way better here, but this doesn't turn up in any profiles. */ char b[4096]; @@ -781,8 +781,8 @@ move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen) * it does two copies instead of 1, but I kinda doubt that this will be * critical path. */ size_t n = len > sizeof(b) ? sizeof(b) : len; - fetch_from_buf(b, n, buf_in); - write_to_buf(b, n, buf_out); + buf_get_bytes(b, n, buf_in); + buf_add(b, n, buf_out); len -= n; } *buf_flushlen -= cp; @@ -901,7 +901,7 @@ buf_find_string_offset(const buf_t *buf, const char *s, size_t n) /** Return 1 iff <b>buf</b> starts with <b>cmd</b>. <b>cmd</b> must be a null * terminated string, of no more than PEEK_BUF_STARTSWITH_MAX bytes. */ int -peek_buf_startswith(const buf_t *buf, const char *cmd) +buf_peek_startswith(const buf_t *buf, const char *cmd) { char tmp[PEEK_BUF_STARTSWITH_MAX]; size_t clen = strlen(cmd); @@ -909,7 +909,7 @@ peek_buf_startswith(const buf_t *buf, const char *cmd) return 0; if (buf->datalen < clen) return 0; - peek_from_buf(tmp, clen, buf); + buf_peek(tmp, clen, buf); return fast_memeq(tmp, cmd, clen); } @@ -938,7 +938,7 @@ buf_find_offset_of_char(buf_t *buf, char ch) * length exceeds *<b>data_len</b>. */ int -fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len) +buf_get_line(buf_t *buf, char *data_out, size_t *data_len) { size_t sz; off_t offset; @@ -954,7 +954,7 @@ fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len) *data_len = sz + 2; return -1; } - fetch_from_buf(data_out, sz+1, buf); + buf_get_bytes(data_out, sz+1, buf); data_out[sz+1] = '\0'; *data_len = sz+1; return 1; @@ -965,7 +965,7 @@ fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len) * <b>done</b> is true, flush the data in the state and finish the * compression/uncompression. Return -1 on failure, 0 on success. */ int -write_to_buf_compress(buf_t *buf, tor_compress_state_t *state, +buf_add_compress(buf_t *buf, tor_compress_state_t *state, const char *data, size_t data_len, const int done) { @@ -1033,7 +1033,7 @@ buf_set_to_copy(buf_t **output, /** Log an error and exit if <b>buf</b> is corrupted. */ void -assert_buf_ok(buf_t *buf) +buf_assert_ok(buf_t *buf) { tor_assert(buf); tor_assert(buf->magic == BUFFER_MAGIC); diff --git a/src/common/buffers.h b/src/common/buffers.h index 77cc9ce0fb..73a688cff9 100644 --- a/src/common/buffers.h +++ b/src/common/buffers.h @@ -35,27 +35,27 @@ size_t buf_slack(const buf_t *buf); uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now); size_t buf_get_total_allocation(void); -int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof, +int buf_read_from_socket(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof, int *socket_error); -int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen); +int buf_flush_to_socket(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen); -int write_to_buf(const char *string, size_t string_len, buf_t *buf); -int write_to_buf_compress(buf_t *buf, struct tor_compress_state_t *state, +int buf_add(const char *string, size_t string_len, buf_t *buf); +int buf_add_compress(buf_t *buf, struct tor_compress_state_t *state, const char *data, size_t data_len, int done); -int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen); -void peek_from_buf(char *string, size_t string_len, const buf_t *buf); -void buf_remove_from_front(buf_t *buf, size_t n); -int fetch_from_buf(char *string, size_t string_len, buf_t *buf); -int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len); +int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen); +void buf_peek(char *string, size_t string_len, const buf_t *buf); +void buf_drain(buf_t *buf, size_t n); +int buf_get_bytes(char *string, size_t string_len, buf_t *buf); +int buf_get_line(buf_t *buf, char *data_out, size_t *data_len); #define PEEK_BUF_STARTSWITH_MAX 16 -int peek_buf_startswith(const buf_t *buf, const char *cmd); +int buf_peek_startswith(const buf_t *buf, const char *cmd); int buf_set_to_copy(buf_t **output, const buf_t *input); -void assert_buf_ok(buf_t *buf); +void buf_assert_ok(buf_t *buf); int buf_find_string_offset(const buf_t *buf, const char *s, size_t n); void buf_pullup(buf_t *buf, size_t bytes, diff --git a/src/common/buffers_tls.c b/src/common/buffers_tls.c index e470738698..d22b56b639 100644 --- a/src/common/buffers_tls.c +++ b/src/common/buffers_tls.c @@ -57,7 +57,7 @@ read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls, * ready to write -- or vice versa. */ int -read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf) +buf_read_from_tls(tor_tls_t *tls, size_t at_most, buf_t *buf) { int r = 0; size_t total_read = 0; @@ -94,7 +94,7 @@ read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf) return (int)total_read; } -/** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from chunk +/** Helper for buf_flush_to_tls(): try to write <b>sz</b> bytes from chunk * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write * more if there is a forced pending write size.) On success, deduct the * bytes written from *<b>buf_flushlen</b>. Return the number of bytes @@ -125,17 +125,17 @@ flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk, *buf_flushlen -= r; else *buf_flushlen = 0; - buf_remove_from_front(buf, r); + buf_drain(buf, r); log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.", r,(int)*buf_flushlen,(int)buf->datalen); return r; } -/** As flush_buf(), but writes data to a TLS connection. Can write more than +/** As buf_flush_to_socket(), but writes data to a TLS connection. Can write more than * <b>flushlen</b> bytes. */ int -flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t flushlen, +buf_flush_to_tls(tor_tls_t *tls, buf_t *buf, size_t flushlen, size_t *buf_flushlen) { int r; diff --git a/src/common/buffers_tls.h b/src/common/buffers_tls.h index 4fce6c1130..d6b35fe934 100644 --- a/src/common/buffers_tls.h +++ b/src/common/buffers_tls.h @@ -10,9 +10,9 @@ struct buf_t; struct tor_tls_t; -int read_to_buf_tls(struct tor_tls_t *tls, size_t at_most, +int buf_read_from_tls(struct tor_tls_t *tls, size_t at_most, struct buf_t *buf); -int flush_buf_tls(struct tor_tls_t *tls, struct buf_t *buf, size_t sz, +int buf_flush_to_tls(struct tor_tls_t *tls, struct buf_t *buf, size_t sz, size_t *buf_flushlen); #endif |