@@ -132,28 +132,35 @@ void TorControlConnection::eventcb(struct bufferevent *bev, short what, void *ct
132
132
133
133
bool TorControlConnection::Connect (const std::string& tor_control_center, const ConnectionCB& _connected, const ConnectionCB& _disconnected)
134
134
{
135
- if (b_conn)
135
+ if (b_conn) {
136
136
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)) {
142
148
LogPrintf (" tor: Error parsing socket address %s\n " , tor_control_center);
143
149
return false ;
144
150
}
145
151
146
152
// Create a new socket, set up callbacks and enable notification bits
147
153
b_conn = bufferevent_socket_new (base, -1 , BEV_OPT_CLOSE_ON_FREE);
148
- if (!b_conn)
154
+ if (!b_conn) {
149
155
return false ;
156
+ }
150
157
bufferevent_setcb (b_conn, TorControlConnection::readcb, nullptr , TorControlConnection::eventcb, this );
151
158
bufferevent_enable (b_conn, EV_READ|EV_WRITE);
152
159
this ->connected = _connected;
153
160
this ->disconnected = _disconnected;
154
161
155
162
// 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 ) {
157
164
LogPrintf (" tor: Error connecting to address %s\n " , tor_control_center);
158
165
return false ;
159
166
}
0 commit comments