| 252 | } |
| 253 | |
| 254 | bool SFTPClient::connect() { |
| 255 | if (connected_) { |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | /* Setting up curl request */ |
| 260 | std::stringstream uri_ss; |
| 261 | uri_ss << hostname_ << ":" << port_; |
| 262 | auto uri = uri_ss.str(); |
| 263 | if (curl_easy_setopt(easy_, CURLOPT_URL, uri.c_str()) != CURLE_OK) { |
| 264 | return false; |
| 265 | } |
| 266 | if (curl_easy_setopt(easy_, CURLOPT_ERRORBUFFER, curl_errorbuffer_.data()) != CURLE_OK) { |
| 267 | return false; |
| 268 | } |
| 269 | if (curl_easy_setopt(easy_, CURLOPT_NOSIGNAL, 1L) != CURLE_OK) { |
| 270 | return false; |
| 271 | } |
| 272 | if (curl_easy_setopt(easy_, CURLOPT_CONNECT_ONLY, 1L) != CURLE_OK) { |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | /* Connecting to proxy, if needed, then to the host */ |
| 277 | CURLcode curl_res = curl_easy_perform(easy_); |
| 278 | if (curl_res != CURLE_OK) { |
| 279 | logger_->log_error("Failed to connect to %s, curl error code: %s, detailed error message: %s", |
| 280 | uri.c_str(), |
| 281 | curl_easy_strerror(curl_res), |
| 282 | curl_errorbuffer_.data()); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | /* Getting socket from curl */ |
| 287 | #ifdef WIN32 |
| 288 | curl_socket_t sockfd; |
| 289 | /* Only CURLINFO_ACTIVESOCKET works on Win64 */ |
| 290 | curl_res = curl_easy_getinfo(easy_, CURLINFO_ACTIVESOCKET, &sockfd); |
| 291 | #else |
| 292 | long sockfd; |
| 293 | /* Some older cURL versions only support CURLINFO_LASTSOCKET */ |
| 294 | curl_res = curl_easy_getinfo(easy_, CURLINFO_LASTSOCKET, &sockfd); |
| 295 | #endif |
| 296 | if (curl_res != CURLE_OK) { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | /* Establishing SSH connection */ |
| 301 | if (libssh2_session_handshake(ssh_session_, sockfd) != 0) { |
| 302 | char *err_msg = nullptr; |
| 303 | libssh2_session_last_error(ssh_session_, &err_msg, nullptr, 0); |
| 304 | logger_->log_info("Failed to establish SSH connection, error: %s", err_msg); |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | /* Checking remote host */ |
| 309 | if (ssh_known_hosts_ != nullptr) { |
| 310 | size_t hostkey_len = 0U; |
| 311 | int type = LIBSSH2_HOSTKEY_TYPE_UNKNOWN; |