Skip to content

Commit 1c046bb

Browse files
committed
Merge bitcoin/bitcoin#22288: Resolve Tor control plane address
cdd51e8 torcontrol: Resolve Tor control plane address (Adrian-Stefan Mares) Pull request description: Closes bitcoin/bitcoin#22236 This PR forces the Tor control plane address to be resolved before a connection attempt is made, similar to how the `-proxy` / `-onion` address is resolved. The use case for this change is that the control plane may not have a stable address - in a containerized environment perhaps. ACKs for top commit: jonatack: ACK cdd51e8 tested various configurations on signet with this branch versus master laanwj: LGTM ACK cdd51e8 theStack: ACK cdd51e8 🪐 prayank23: ACK bitcoin/bitcoin@cdd51e8 Tree-SHA512: 5335cfcb89089a2acd6d02b88c2022dec60bb74388a99187c901c1c35d32896814d5f81df55c053953276c51fcec263c6ddadd068316f8e428b841bd599fc21e
2 parents 10eb000 + cdd51e8 commit 1c046bb

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/torcontrol.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,35 @@ void TorControlConnection::eventcb(struct bufferevent *bev, short what, void *ct
132132

133133
bool TorControlConnection::Connect(const std::string& tor_control_center, const ConnectionCB& _connected, const ConnectionCB& _disconnected)
134134
{
135-
if (b_conn)
135+
if (b_conn) {
136136
Disconnect();
137-
// Parse tor_control_center address:port
138-
struct sockaddr_storage connect_to_addr;
139-
int connect_to_addrlen = sizeof(connect_to_addr);
140-
if (evutil_parse_sockaddr_port(tor_control_center.c_str(),
141-
(struct sockaddr*)&connect_to_addr, &connect_to_addrlen)<0) {
137+
}
138+
139+
CService control_service;
140+
if (!Lookup(tor_control_center, control_service, 9051, fNameLookup)) {
141+
LogPrintf("tor: Failed to look up control center %s\n", tor_control_center);
142+
return false;
143+
}
144+
145+
struct sockaddr_storage control_address;
146+
socklen_t control_address_len = sizeof(control_address);
147+
if (!control_service.GetSockAddr(reinterpret_cast<struct sockaddr*>(&control_address), &control_address_len)) {
142148
LogPrintf("tor: Error parsing socket address %s\n", tor_control_center);
143149
return false;
144150
}
145151

146152
// Create a new socket, set up callbacks and enable notification bits
147153
b_conn = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
148-
if (!b_conn)
154+
if (!b_conn) {
149155
return false;
156+
}
150157
bufferevent_setcb(b_conn, TorControlConnection::readcb, nullptr, TorControlConnection::eventcb, this);
151158
bufferevent_enable(b_conn, EV_READ|EV_WRITE);
152159
this->connected = _connected;
153160
this->disconnected = _disconnected;
154161

155162
// Finally, connect to tor_control_center
156-
if (bufferevent_socket_connect(b_conn, (struct sockaddr*)&connect_to_addr, connect_to_addrlen) < 0) {
163+
if (bufferevent_socket_connect(b_conn, reinterpret_cast<struct sockaddr*>(&control_address), control_address_len) < 0) {
157164
LogPrintf("tor: Error connecting to address %s\n", tor_control_center);
158165
return false;
159166
}

0 commit comments

Comments
 (0)