tor: clean up io_tor_connect.

Instead of storing a wireaddr and converting to an addrinfo every
time, just convert once (which also avoids the memory leak in the
current code).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-05-10 08:48:24 +09:30
parent d87a6c3a48
commit 003cd29733
3 changed files with 18 additions and 35 deletions

View File

@ -147,7 +147,7 @@ struct daemon {
/* Automatically reconnect. */
bool reconnect;
struct wireaddr *tor_proxyaddr;
struct addrinfo *tor_proxyaddr;
bool use_tor_proxy_always;
};
@ -1739,6 +1739,7 @@ static struct io_plan *gossip_init(struct daemon_conn *master,
struct bitcoin_blkid chain_hash;
u32 update_channel_interval;
bool dev_allow_localhost;
struct wireaddr *tor_proxyaddr;
if (!fromwire_gossipctl_init(
daemon, msg, &daemon->broadcast_interval, &chain_hash,
@ -1746,7 +1747,7 @@ static struct io_plan *gossip_init(struct daemon_conn *master,
&daemon->localfeatures, &daemon->proposed_wireaddr,
&daemon->proposed_listen_announce, daemon->rgb,
daemon->alias, &update_channel_interval, &daemon->reconnect,
&daemon->tor_proxyaddr, &daemon->use_tor_proxy_always,
&tor_proxyaddr, &daemon->use_tor_proxy_always,
&dev_allow_localhost)) {
master_badmsg(WIRE_GOSSIPCTL_INIT, msg);
}
@ -1755,6 +1756,15 @@ static struct io_plan *gossip_init(struct daemon_conn *master,
update_channel_interval * 2,
dev_allow_localhost);
/* Resolve Tor proxy address if any */
if (tor_proxyaddr) {
status_trace("Tor proxyaddr : %s",
fmt_wireaddr(tmpctx, tor_proxyaddr));
daemon->tor_proxyaddr = wireaddr_to_addrinfo(daemon,
tor_proxyaddr);
} else
daemon->tor_proxyaddr = NULL;
/* Load stored gossip messages */
gossip_store_load(daemon->rstate, daemon->rstate->store);

View File

@ -150,46 +150,18 @@ static struct io_plan *io_tor_connect_do_req(struct io_conn *conn,
// called when we want to connect to TOR SOCKS5
struct io_plan *io_tor_connect(struct io_conn *conn,
const struct wireaddr *tor_proxyaddrs,
const struct addrinfo *tor_proxyaddr,
const struct wireaddr *addr,
struct reaching *reach)
{
struct addrinfo *ai_tor = tal(reach, struct addrinfo);
char *port_addr = tal(reach, char);
struct io_plan *plan;
struct reaching_socks *reach_tor = tal(reach, struct reaching_socks);
reach_tor->port = htons(addr->port);
port_addr = tal_fmt(reach, "%u", tor_proxyaddrs->port);
getaddrinfo((char *)
fmt_wireaddr_without_port(tmpctx,
tor_proxyaddrs),
port_addr, NULL, &ai_tor);
status_trace("Tor proxyaddr : %s",
fmt_wireaddr(reach, tor_proxyaddrs));
reach_tor->host = tal_strdup(reach, "");
if (addr->type == ADDR_TYPE_TOR_V3)
reach_tor->host =
tal_fmt(reach, "%.62s",
fmt_wireaddr_without_port(tmpctx, addr));
else if (addr->type == ADDR_TYPE_TOR_V2)
reach_tor->host =
tal_fmt(reach, "%.22s",
fmt_wireaddr_without_port(tmpctx, addr));
else if (addr->type == ADDR_TYPE_IPV4)
reach_tor->host =
tal_fmt(reach, "%s",
fmt_wireaddr_without_port(tmpctx, addr));
else if (addr->type == ADDR_TYPE_IPV6)
reach_tor->host =
tal_fmt(reach, "%s",
fmt_wireaddr_without_port(tmpctx, addr));
reach_tor->host = fmt_wireaddr_without_port(reach_tor, addr);
reach_tor->reach = reach;
plan = io_connect(conn, ai_tor, &io_tor_connect_do_req, reach_tor);
return plan;
return io_connect(conn, tor_proxyaddr,
&io_tor_connect_do_req, reach_tor);
}
bool do_we_use_tor_addr(const struct wireaddr *wireaddr)

View File

@ -3,6 +3,7 @@
#include "config.h"
#include <stdbool.h>
struct addrinfo;
struct wireaddr;
struct io_conn;
struct reaching;
@ -10,7 +11,7 @@ struct reaching;
bool do_we_use_tor_addr(const struct wireaddr *wireaddrs);
struct io_plan *io_tor_connect(struct io_conn *conn,
const struct wireaddr *tor_proxyaddr,
const struct addrinfo *tor_proxyaddr,
const struct wireaddr *addr,
struct reaching *reach);