Merge branch 'tor-gitlab/mr/54'

This commit is contained in:
David Goulet 2020-07-16 12:55:26 -04:00
commit f6fc062119
16 changed files with 168 additions and 19 deletions

3
changes/ticket40046 Normal file
View File

@ -0,0 +1,3 @@
o Code simplification and refactoring:
- Add and use a set of functions to perform downcasts on constant
connection and channel pointers. Closes ticket 40046.

View File

@ -220,8 +220,12 @@ static smartlist_t *outgoing_addrs = NULL;
/**************************************************************/
/** Convert a connection_t* to an listener_connection_t*; assert if the cast
* is invalid. */
/**
* Cast a `connection_t *` to a `listener_connection_t *`.
*
* Exit with an assertion failure if the input is not a
* `listener_connection_t`.
**/
listener_connection_t *
TO_LISTENER_CONN(connection_t *c)
{
@ -229,6 +233,18 @@ TO_LISTENER_CONN(connection_t *c)
return DOWNCAST(listener_connection_t, c);
}
/**
* Cast a `const connection_t *` to a `const listener_connection_t *`.
*
* Exit with an assertion failure if the input is not a
* `listener_connection_t`.
**/
const listener_connection_t *
CONST_TO_LISTENER_CONN(const connection_t *c)
{
return TO_LISTENER_CONN((connection_t *)c);
}
size_t
connection_get_inbuf_len(connection_t *conn)
{
@ -417,7 +433,7 @@ connection_describe_peer_internal(const connection_t *conn,
address = conn->address ? conn->address : "unix socket";
} else if (conn->type == CONN_TYPE_OR) {
/* For OR connections, we have a lot to do. */
const or_connection_t *or_conn = TO_OR_CONN((connection_t *)conn);
const or_connection_t *or_conn = CONST_TO_OR_CONN(conn);
/* we report 'real_addr' as the address we're talking with, if it's set.
*
* TODO: Eventually we should have 'addr' always mean the address on the
@ -2241,7 +2257,7 @@ connection_connect_log_client_use_ip_version(const connection_t *conn)
/* OR conns keep the original address in real_addr, as addr gets overwritten
* with the descriptor address */
if (conn->type == CONN_TYPE_OR) {
const or_connection_t *or_conn = TO_OR_CONN((connection_t *)conn);
const or_connection_t *or_conn = CONST_TO_OR_CONN(conn);
tor_addr_copy(&real_addr, &or_conn->real_addr);
} else if (conn->type == CONN_TYPE_DIR) {
tor_addr_copy(&real_addr, &conn->addr);

View File

@ -31,6 +31,8 @@ struct tor_addr_t;
struct or_options_t;
struct listener_connection_t *TO_LISTENER_CONN(struct connection_t *);
const struct listener_connection_t *CONST_TO_LISTENER_CONN(
const struct connection_t *);
struct buf_t;

View File

@ -389,6 +389,25 @@ channel_tls_from_base(channel_t *chan)
return (channel_tls_t *)(chan);
}
/**
* Cast a const channel_tls_t to a const channel_t.
*/
const channel_t *
channel_tls_to_base_const(const channel_tls_t *tlschan)
{
return channel_tls_to_base((channel_tls_t*) tlschan);
}
/**
* Cast a const channel_t to a const channel_tls_t, with appropriate
* type-checking asserts.
*/
const channel_tls_t *
channel_tls_from_base_const(const channel_t *chan)
{
return channel_tls_from_base((channel_t *)chan);
}
/********************************************
* Method implementations for channel_tls_t *
*******************************************/
@ -519,7 +538,7 @@ static int
channel_tls_get_remote_addr_method(const channel_t *chan,
tor_addr_t *addr_out)
{
const channel_tls_t *tlschan = BASE_CHAN_TO_TLS((channel_t*) chan);
const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan);
tor_assert(tlschan);
tor_assert(addr_out);
@ -574,7 +593,7 @@ channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
static const char *
channel_tls_describe_peer_method(const channel_t *chan)
{
const channel_tls_t *tlschan = BASE_CHAN_TO_TLS((channel_t*)chan);
const channel_tls_t *tlschan = CONST_BASE_CHAN_TO_TLS(chan);
tor_assert(tlschan);
if (tlschan->conn) {

View File

@ -19,6 +19,8 @@ struct curve25519_public_key_t;
#define BASE_CHAN_TO_TLS(c) (channel_tls_from_base((c)))
#define TLS_CHAN_TO_BASE(c) (channel_tls_to_base((c)))
#define CONST_BASE_CHAN_TO_TLS(c) (channel_tls_from_base_const((c)))
#define CONST_TLS_CHAN_TO_BASE(c) (channel_tls_to_base_const((c)))
#define TLS_CHAN_MAGIC 0x8a192427U
@ -44,6 +46,8 @@ channel_t * channel_tls_handle_incoming(or_connection_t *orconn);
channel_t * channel_tls_to_base(channel_tls_t *tlschan);
channel_tls_t * channel_tls_from_base(channel_t *chan);
const channel_t * channel_tls_to_base_const(const channel_tls_t *tlschan);
const channel_tls_t * channel_tls_from_base_const(const channel_t *chan);
/* Things for connection_or.c to call back into */
void channel_tls_handle_cell(cell_t *cell, or_connection_t *conn);

View File

@ -166,8 +166,12 @@ static int connection_exit_connect_dir(edge_connection_t *exitconn);
static int consider_plaintext_ports(entry_connection_t *conn, uint16_t port);
static int connection_ap_supports_optimistic_data(const entry_connection_t *);
/** Convert a connection_t* to an edge_connection_t*; assert if the cast is
* invalid. */
/**
* Cast a `connection_t *` to an `edge_connection_t *`.
*
* Exit with an assertion failure if the input is not an
* `edge_connection_t`.
**/
edge_connection_t *
TO_EDGE_CONN(connection_t *c)
{
@ -176,6 +180,24 @@ TO_EDGE_CONN(connection_t *c)
return DOWNCAST(edge_connection_t, c);
}
/**
* Cast a `const connection_t *` to a `const edge_connection_t *`.
*
* Exit with an assertion failure if the input is not an
* `edge_connection_t`.
**/
const edge_connection_t *
CONST_TO_EDGE_CONN(const connection_t *c)
{
return TO_EDGE_CONN((connection_t *)c);
}
/**
* Cast a `connection_t *` to an `entry_connection_t *`.
*
* Exit with an assertion failure if the input is not an
* `entry_connection_t`.
**/
entry_connection_t *
TO_ENTRY_CONN(connection_t *c)
{
@ -183,6 +205,24 @@ TO_ENTRY_CONN(connection_t *c)
return (entry_connection_t*) SUBTYPE_P(c, entry_connection_t, edge_.base_);
}
/**
* Cast a `const connection_t *` to a `const entry_connection_t *`.
*
* Exit with an assertion failure if the input is not an
* `entry_connection_t`.
**/
const entry_connection_t *
CONST_TO_ENTRY_CONN(const connection_t *c)
{
return TO_ENTRY_CONN((connection_t*) c);
}
/**
* Cast an `edge_connection_t *` to an `entry_connection_t *`.
*
* Exit with an assertion failure if the input is not an
* `entry_connection_t`.
**/
entry_connection_t *
EDGE_TO_ENTRY_CONN(edge_connection_t *c)
{
@ -190,6 +230,18 @@ EDGE_TO_ENTRY_CONN(edge_connection_t *c)
return (entry_connection_t*) SUBTYPE_P(c, entry_connection_t, edge_);
}
/**
* Cast a `const edge_connection_t *` to a `const entry_connection_t *`.
*
* Exit with an assertion failure if the input is not an
* `entry_connection_t`.
**/
const entry_connection_t *
CONST_EDGE_TO_ENTRY_CONN(const edge_connection_t *c)
{
return EDGE_TO_ENTRY_CONN((edge_connection_t*)c);
}
/** An AP stream has failed/finished. If it hasn't already sent back
* a socks reply, send one now (based on endreason). Also set
* has_sent_end to 1, and mark the conn.

View File

@ -20,6 +20,10 @@ edge_connection_t *TO_EDGE_CONN(connection_t *);
entry_connection_t *TO_ENTRY_CONN(connection_t *);
entry_connection_t *EDGE_TO_ENTRY_CONN(edge_connection_t *);
const edge_connection_t *CONST_TO_EDGE_CONN(const connection_t *);
const entry_connection_t *CONST_TO_ENTRY_CONN(const connection_t *);
const entry_connection_t *CONST_EDGE_TO_ENTRY_CONN(const edge_connection_t *);
#define EXIT_CONN_STATE_MIN_ 1
/** State for an exit connection: waiting for response from DNS farm. */
#define EXIT_CONN_STATE_RESOLVING 1

View File

@ -99,8 +99,11 @@ static void connection_or_check_canonicity(or_connection_t *conn,
/**************************************************************/
/** Convert a connection_t* to an or_connection_t*; assert if the cast is
* invalid. */
/**
* Cast a `connection_t *` to an `or_connection_t *`.
*
* Exit with an assertion failure if the input is not an `or_connnection_t`.
**/
or_connection_t *
TO_OR_CONN(connection_t *c)
{
@ -108,6 +111,17 @@ TO_OR_CONN(connection_t *c)
return DOWNCAST(or_connection_t, c);
}
/**
* Cast a `const connection_t *` to a `const or_connection_t *`.
*
* Exit with an assertion failure if the input is not an `or_connnection_t`.
**/
const or_connection_t *
CONST_TO_OR_CONN(const connection_t *c)
{
return TO_OR_CONN((connection_t *)c);
}
/** Clear clear conn->identity_digest and update other data
* structures as appropriate.*/
void

View File

@ -16,6 +16,7 @@ struct ed25519_public_key_t;
struct ed25519_keypair_t;
or_connection_t *TO_OR_CONN(connection_t *);
const or_connection_t *CONST_TO_OR_CONN(const connection_t *);
#include "core/or/orconn_event.h"

View File

@ -713,7 +713,7 @@ scheduler_bug_occurred(const channel_t *chan)
if (chan != NULL) {
const size_t outbuf_len =
buf_datalen(TO_CONN(BASE_CHAN_TO_TLS((channel_t *) chan)->conn)->outbuf);
buf_datalen(TO_CONN(CONST_BASE_CHAN_TO_TLS(chan)->conn)->outbuf);
tor_snprintf(buf, sizeof(buf),
"Channel %" PRIu64 " in state %s and scheduler state %s."
" Num cells on cmux: %d. Connection outbuf len: %lu.",

View File

@ -203,7 +203,7 @@ update_socket_info_impl, (socket_table_ent_t *ent))
tor_assert(ent);
tor_assert(ent->chan);
const tor_socket_t sock =
TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn)->s;
TO_CONN(CONST_BASE_CHAN_TO_TLS(ent->chan)->conn)->s;
struct tcp_info tcp;
socklen_t tcp_info_len = sizeof(tcp);

View File

@ -61,8 +61,12 @@
#include <sys/stat.h>
#endif
/** Convert a connection_t* to an control_connection_t*; assert if the cast is
* invalid. */
/**
* Cast a `connection_t *` to a `control_connection_t *`.
*
* Exit with an assertion failure if the input is not a
* `control_connection_t`.
**/
control_connection_t *
TO_CONTROL_CONN(connection_t *c)
{
@ -70,6 +74,18 @@ TO_CONTROL_CONN(connection_t *c)
return DOWNCAST(control_connection_t, c);
}
/**
* Cast a `const connection_t *` to a `const control_connection_t *`.
*
* Exit with an assertion failure if the input is not a
* `control_connection_t`.
**/
const control_connection_t *
CONST_TO_CONTROL_CONN(const connection_t *c)
{
return TO_CONTROL_CONN((connection_t*)c);
}
/** Create and add a new controller connection on <b>sock</b>. If
* <b>CC_LOCAL_FD_IS_OWNER</b> is set in <b>flags</b>, this Tor process should
* exit when the connection closes. If <b>CC_LOCAL_FD_IS_AUTHENTICATED</b>

View File

@ -13,6 +13,7 @@
#define TOR_CONTROL_H
control_connection_t *TO_CONTROL_CONN(connection_t *);
const control_connection_t *CONST_TO_CONTROL_CONN(const connection_t *);
#define CONTROL_CONN_STATE_MIN_ 1
/** State for a control connection: Authenticated and accepting v1 commands. */

View File

@ -274,7 +274,7 @@ control_event_bootstrap_problem(const char *warn, const char *reason,
const char *recommendation = "ignore";
int severity;
char *or_id = NULL, *hostaddr = NULL;
or_connection_t *or_conn = NULL;
const or_connection_t *or_conn = NULL;
/* bootstrap_percent must not be in "undefined" state here. */
tor_assert(status >= 0);
@ -301,7 +301,7 @@ control_event_bootstrap_problem(const char *warn, const char *reason,
if (conn && conn->type == CONN_TYPE_OR) {
/* XXX TO_OR_CONN can't deal with const */
or_conn = TO_OR_CONN((connection_t *)conn);
or_conn = CONST_TO_OR_CONN(conn);
or_id = tor_strdup(hex_str(or_conn->identity_digest, DIGEST_LEN));
} else {
or_id = tor_strdup("?");

View File

@ -79,8 +79,12 @@
* connection_finished_connecting() in connection.c
*/
/** Convert a connection_t* to a dir_connection_t*; assert if the cast is
* invalid. */
/**
* Cast a `connection_t *` to a `dir_connection_t *`.
*
* Exit with an assertion failure if the input is not a
* `dir_connection_t`.
**/
dir_connection_t *
TO_DIR_CONN(connection_t *c)
{
@ -88,6 +92,18 @@ TO_DIR_CONN(connection_t *c)
return DOWNCAST(dir_connection_t, c);
}
/**
* Cast a `const connection_t *` to a `const dir_connection_t *`.
*
* Exit with an assertion failure if the input is not a
* `dir_connection_t`.
**/
const dir_connection_t *
CONST_TO_DIR_CONN(const connection_t *c)
{
return TO_DIR_CONN((connection_t *)c);
}
/** Return false if the directory purpose <b>dir_purpose</b>
* does not require an anonymous (three-hop) connection.
*
@ -217,7 +233,7 @@ connection_dir_is_anonymous(const dir_connection_t *dir_conn)
return false;
}
edge_conn = TO_EDGE_CONN((connection_t *) linked_conn);
edge_conn = CONST_TO_EDGE_CONN(linked_conn);
circ = edge_conn->on_circuit;
/* Can't be a circuit we initiated and without a circuit, no channel. */

View File

@ -13,6 +13,7 @@
#define TOR_DIRECTORY_H
dir_connection_t *TO_DIR_CONN(connection_t *c);
const dir_connection_t *CONST_TO_DIR_CONN(const connection_t *c);
#define DIR_CONN_STATE_MIN_ 1
/** State for connection to directory server: waiting for connect(). */