| 130 | } |
| 131 | |
| 132 | bool TorControlConnection::Connect(const std::string& tor_control_center, const ConnectionCB& _connected, const ConnectionCB& _disconnected) |
| 133 | { |
| 134 | if (b_conn) { |
| 135 | Disconnect(); |
| 136 | } |
| 137 | |
| 138 | CService control_service; |
| 139 | if (!Lookup(tor_control_center, control_service, 9051, fNameLookup)) { |
| 140 | LogPrintf("tor: Failed to look up control center %s\n", tor_control_center); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | struct sockaddr_storage control_address; |
| 145 | socklen_t control_address_len = sizeof(control_address); |
| 146 | if (!control_service.GetSockAddr(reinterpret_cast<struct sockaddr*>(&control_address), &control_address_len)) { |
| 147 | LogPrintf("tor: Error parsing socket address %s\n", tor_control_center); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // Create a new socket, set up callbacks and enable notification bits |
| 152 | b_conn = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE); |
| 153 | if (!b_conn) { |
| 154 | return false; |
| 155 | } |
| 156 | bufferevent_setcb(b_conn, TorControlConnection::readcb, nullptr, TorControlConnection::eventcb, this); |
| 157 | bufferevent_enable(b_conn, EV_READ|EV_WRITE); |
| 158 | this->connected = _connected; |
| 159 | this->disconnected = _disconnected; |
| 160 | |
| 161 | // Finally, connect to tor_control_center |
| 162 | if (bufferevent_socket_connect(b_conn, reinterpret_cast<struct sockaddr*>(&control_address), control_address_len) < 0) { |
| 163 | LogPrintf("tor: Error connecting to address %s\n", tor_control_center); |
| 164 | return false; |
| 165 | } |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | void TorControlConnection::Disconnect() |
| 170 | { |
no test coverage detected