From 003cd2973300369e2d5207aa2b021223a6cfb1a5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 10 May 2018 08:48:24 +0930 Subject: [PATCH] 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 --- gossipd/gossip.c | 14 ++++++++++++-- gossipd/tor.c | 36 ++++-------------------------------- gossipd/tor.h | 3 ++- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 8f2b6179c..350240a70 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -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); diff --git a/gossipd/tor.c b/gossipd/tor.c index be466ca8e..4305d8c91 100644 --- a/gossipd/tor.c +++ b/gossipd/tor.c @@ -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) diff --git a/gossipd/tor.h b/gossipd/tor.h index d4e4cd56f..222ca3172 100644 --- a/gossipd/tor.h +++ b/gossipd/tor.h @@ -3,6 +3,7 @@ #include "config.h" #include +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);