Skip to content

Commit dd07c84

Browse files
committed
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>
1 parent 4117f75 commit dd07c84

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

gossipd/gossip.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ struct daemon {
147147
/* Automatically reconnect. */
148148
bool reconnect;
149149

150-
struct wireaddr *tor_proxyaddr;
150+
struct addrinfo *tor_proxyaddr;
151151
bool use_tor_proxy_always;
152152
};
153153

@@ -1733,14 +1733,15 @@ static struct io_plan *gossip_init(struct daemon_conn *master,
17331733
struct bitcoin_blkid chain_hash;
17341734
u32 update_channel_interval;
17351735
bool dev_allow_localhost;
1736+
struct wireaddr *tor_proxyaddr;
17361737

17371738
if (!fromwire_gossipctl_init(
17381739
daemon, msg, &daemon->broadcast_interval, &chain_hash,
17391740
&daemon->id, &daemon->globalfeatures,
17401741
&daemon->localfeatures, &daemon->proposed_wireaddr,
17411742
&daemon->proposed_listen_announce, daemon->rgb,
17421743
daemon->alias, &update_channel_interval, &daemon->reconnect,
1743-
&daemon->tor_proxyaddr, &daemon->use_tor_proxy_always,
1744+
&tor_proxyaddr, &daemon->use_tor_proxy_always,
17441745
&dev_allow_localhost)) {
17451746
master_badmsg(WIRE_GOSSIPCTL_INIT, msg);
17461747
}
@@ -1749,6 +1750,15 @@ static struct io_plan *gossip_init(struct daemon_conn *master,
17491750
update_channel_interval * 2,
17501751
dev_allow_localhost);
17511752

1753+
/* Resolve Tor proxy address if any */
1754+
if (tor_proxyaddr) {
1755+
status_trace("Tor proxyaddr : %s",
1756+
fmt_wireaddr(tmpctx, tor_proxyaddr));
1757+
daemon->tor_proxyaddr = wireaddr_to_addrinfo(daemon,
1758+
tor_proxyaddr);
1759+
} else
1760+
daemon->tor_proxyaddr = NULL;
1761+
17521762
/* Load stored gossip messages */
17531763
gossip_store_load(daemon->rstate, daemon->rstate->store);
17541764

gossipd/tor.c

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -150,46 +150,18 @@ static struct io_plan *io_tor_connect_do_req(struct io_conn *conn,
150150

151151
// called when we want to connect to TOR SOCKS5
152152
struct io_plan *io_tor_connect(struct io_conn *conn,
153-
const struct wireaddr *tor_proxyaddrs,
153+
const struct addrinfo *tor_proxyaddr,
154154
const struct wireaddr *addr,
155155
struct reaching *reach)
156156
{
157-
struct addrinfo *ai_tor = tal(reach, struct addrinfo);
158-
char *port_addr = tal(reach, char);
159-
struct io_plan *plan;
160157
struct reaching_socks *reach_tor = tal(reach, struct reaching_socks);
161158

162159
reach_tor->port = htons(addr->port);
163-
port_addr = tal_fmt(reach, "%u", tor_proxyaddrs->port);
164-
getaddrinfo((char *)
165-
fmt_wireaddr_without_port(tmpctx,
166-
tor_proxyaddrs),
167-
port_addr, NULL, &ai_tor);
168-
status_trace("Tor proxyaddr : %s",
169-
fmt_wireaddr(reach, tor_proxyaddrs));
170-
reach_tor->host = tal_strdup(reach, "");
171-
172-
if (addr->type == ADDR_TYPE_TOR_V3)
173-
reach_tor->host =
174-
tal_fmt(reach, "%.62s",
175-
fmt_wireaddr_without_port(tmpctx, addr));
176-
else if (addr->type == ADDR_TYPE_TOR_V2)
177-
reach_tor->host =
178-
tal_fmt(reach, "%.22s",
179-
fmt_wireaddr_without_port(tmpctx, addr));
180-
else if (addr->type == ADDR_TYPE_IPV4)
181-
reach_tor->host =
182-
tal_fmt(reach, "%s",
183-
fmt_wireaddr_without_port(tmpctx, addr));
184-
else if (addr->type == ADDR_TYPE_IPV6)
185-
reach_tor->host =
186-
tal_fmt(reach, "%s",
187-
fmt_wireaddr_without_port(tmpctx, addr));
160+
reach_tor->host = fmt_wireaddr_without_port(reach_tor, addr);
188161
reach_tor->reach = reach;
189162

190-
plan = io_connect(conn, ai_tor, &io_tor_connect_do_req, reach_tor);
191-
192-
return plan;
163+
return io_connect(conn, tor_proxyaddr,
164+
&io_tor_connect_do_req, reach_tor);
193165
}
194166

195167
bool do_we_use_tor_addr(const struct wireaddr *wireaddr)

gossipd/tor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
#include "config.h"
44
#include <stdbool.h>
55

6+
struct addrinfo;
67
struct wireaddr;
78
struct io_conn;
89
struct reaching;
910

1011
bool do_we_use_tor_addr(const struct wireaddr *wireaddrs);
1112

1213
struct io_plan *io_tor_connect(struct io_conn *conn,
13-
const struct wireaddr *tor_proxyaddr,
14+
const struct addrinfo *tor_proxyaddr,
1415
const struct wireaddr *addr,
1516
struct reaching *reach);
1617

0 commit comments

Comments
 (0)