Fix more remaining users of inbuf/outbuf to handle bufferevents instead.

This commit is contained in:
Nick Mathewson 2009-08-09 11:40:28 -07:00
parent 5279036148
commit ddcb59bb70
8 changed files with 69 additions and 8 deletions

View File

@ -1988,6 +1988,27 @@ peek_buf_has_control0_command(buf_t *buf)
return 0; return 0;
} }
#ifdef USE_BUFFEREVENTS
int
peek_evbuffer_has_control0_command(struct evbuffer *buf)
{
int result = 0;
if (evbuffer_get_length(buf) >= 4) {
int free_out = 0;
char *data = NULL;
size_t n = inspect_evbuffer(buf, &data, 4, &free_out);
uint16_t cmd;
tor_assert(n >= 4);
cmd = ntohs(get_uint16(data+2));
if (cmd <= 0x14)
result = 1;
if (free_out)
tor_free(data);
}
return result;
}
#endif
/** Return the index within <b>buf</b> at which <b>ch</b> first appears, /** Return the index within <b>buf</b> at which <b>ch</b> first appears,
* or -1 if <b>ch</b> does not appear on buf. */ * or -1 if <b>ch</b> does not appear on buf. */
static off_t static off_t

View File

@ -57,6 +57,7 @@ int fetch_from_evbuffer_http(struct evbuffer *buf,
char **headers_out, size_t max_headerlen, char **headers_out, size_t max_headerlen,
char **body_out, size_t *body_used, size_t max_bodylen, char **body_out, size_t *body_used, size_t max_bodylen,
int force_complete); int force_complete);
int peek_evbuffer_has_control0_command(struct evbuffer *buf);
#endif #endif
void assert_buf_ok(buf_t *buf); void assert_buf_ok(buf_t *buf);

View File

@ -2785,6 +2785,24 @@ connection_fetch_from_buf_line(connection_t *conn, char *data,
} }
} }
/** As fetch_from_buf_http, but fetches from a conncetion's input buffer_t or
* its bufferevent as appropriate. */
int
connection_fetch_from_buf_http(connection_t *conn,
char **headers_out, size_t max_headerlen,
char **body_out, size_t *body_used,
size_t max_bodylen, int force_complete)
{
IF_HAS_BUFFEREVENT(conn, {
struct evbuffer *input = bufferevent_get_input(conn->bufev);
return fetch_from_evbuffer_http(input, headers_out, max_headerlen,
body_out, body_used, max_bodylen, force_complete);
}) ELSE_IF_NO_BUFFEREVENT {
return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
body_out, body_used, max_bodylen, force_complete);
}
}
/** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush /** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush
* from its outbuf. */ * from its outbuf. */
int int

View File

@ -65,6 +65,10 @@ int connection_handle_read(connection_t *conn);
int connection_fetch_from_buf(char *string, size_t len, connection_t *conn); int connection_fetch_from_buf(char *string, size_t len, connection_t *conn);
int connection_fetch_from_buf_line(connection_t *conn, char *data, int connection_fetch_from_buf_line(connection_t *conn, char *data,
size_t *data_len); size_t *data_len);
int connection_fetch_from_buf_http(connection_t *conn,
char **headers_out, size_t max_headerlen,
char **body_out, size_t *body_used,
size_t max_bodylen, int force_complete);
int connection_wants_to_flush(connection_t *conn); int connection_wants_to_flush(connection_t *conn);
int connection_outbuf_too_full(connection_t *conn); int connection_outbuf_too_full(connection_t *conn);

View File

@ -1253,12 +1253,18 @@ connection_or_write_var_cell_to_buf(const var_cell_t *cell,
conn->timestamp_last_added_nonpadding = approx_time(); conn->timestamp_last_added_nonpadding = approx_time();
} }
/** See whether there's a variable-length cell waiting on <b>conn</b>'s /** See whether there's a variable-length cell waiting on <b>or_conn</b>'s
* inbuf. Return values as for fetch_var_cell_from_buf(). */ * inbuf. Return values as for fetch_var_cell_from_buf(). */
static int static int
connection_fetch_var_cell_from_buf(or_connection_t *conn, var_cell_t **out) connection_fetch_var_cell_from_buf(or_connection_t *or_conn, var_cell_t **out)
{ {
return fetch_var_cell_from_buf(conn->_base.inbuf, out, conn->link_proto); connection_t *conn = TO_CONN(or_conn);
IF_HAS_BUFFEREVENT(conn, {
struct evbuffer *input = bufferevent_get_input(conn->bufev);
return fetch_var_cell_from_evbuffer(input, out, or_conn->link_proto);
}) ELSE_IF_NO_BUFFEREVENT {
return fetch_var_cell_from_buf(conn->inbuf, out, or_conn->link_proto);
}
} }
/** Process cells from <b>conn</b>'s inbuf. /** Process cells from <b>conn</b>'s inbuf.

View File

@ -2753,6 +2753,17 @@ is_valid_initial_command(control_connection_t *conn, const char *cmd)
* interfaces is broken. */ * interfaces is broken. */
#define MAX_COMMAND_LINE_LENGTH (1024*1024) #define MAX_COMMAND_LINE_LENGTH (1024*1024)
static int
peek_connection_has_control0_command(connection_t *conn)
{
IF_HAS_BUFFEREVENT(conn, {
struct evbuffer *input = bufferevent_get_input(conn->bufev);
return peek_evbuffer_has_control0_command(input);
}) ELSE_IF_NO_BUFFEREVENT {
return peek_buf_has_control0_command(conn->inbuf);
}
}
/** Called when data has arrived on a v1 control connection: Try to fetch /** Called when data has arrived on a v1 control connection: Try to fetch
* commands from conn->inbuf, and execute them. * commands from conn->inbuf, and execute them.
*/ */
@ -2775,7 +2786,7 @@ connection_control_process_inbuf(control_connection_t *conn)
} }
if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH && if (conn->_base.state == CONTROL_CONN_STATE_NEEDAUTH &&
peek_buf_has_control0_command(conn->_base.inbuf)) { peek_connection_has_control0_command(TO_CONN(conn))) {
/* Detect v0 commands and send a "no more v0" message. */ /* Detect v0 commands and send a "no more v0" message. */
size_t body_len; size_t body_len;
char buf[128]; char buf[128];

View File

@ -144,9 +144,9 @@ connection_cpu_process_inbuf(connection_t *conn)
return 0; return 0;
if (conn->state == CPUWORKER_STATE_BUSY_ONION) { if (conn->state == CPUWORKER_STATE_BUSY_ONION) {
if (buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* answer available? */ if (connection_get_inbuf_len(conn) < LEN_ONION_RESPONSE)
return 0; /* not yet */ return 0; /* not yet */
tor_assert(buf_datalen(conn->inbuf) == LEN_ONION_RESPONSE); tor_assert(connection_get_inbuf_len(conn) == LEN_ONION_RESPONSE);
connection_fetch_from_buf(&success,1,conn); connection_fetch_from_buf(&success,1,conn);
connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn); connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn);

View File

@ -1466,7 +1466,7 @@ connection_dir_client_reached_eof(dir_connection_t *conn)
int was_compressed=0; int was_compressed=0;
time_t now = time(NULL); time_t now = time(NULL);
switch (fetch_from_buf_http(conn->_base.inbuf, switch (connection_fetch_from_buf_http(TO_CONN(conn),
&headers, MAX_HEADERS_SIZE, &headers, MAX_HEADERS_SIZE,
&body, &body_len, MAX_DIR_DL_SIZE, &body, &body_len, MAX_DIR_DL_SIZE,
allow_partial)) { allow_partial)) {
@ -3299,7 +3299,7 @@ directory_handle_command(dir_connection_t *conn)
tor_assert(conn); tor_assert(conn);
tor_assert(conn->_base.type == CONN_TYPE_DIR); tor_assert(conn->_base.type == CONN_TYPE_DIR);
switch (fetch_from_buf_http(conn->_base.inbuf, switch (connection_fetch_from_buf_http(TO_CONN(conn),
&headers, MAX_HEADERS_SIZE, &headers, MAX_HEADERS_SIZE,
&body, &body_len, MAX_DIR_UL_SIZE, 0)) { &body, &body_len, MAX_DIR_UL_SIZE, 0)) {
case -1: /* overflow */ case -1: /* overflow */