/////////////////////////////////////////////////////// Entry point of application \return Application exit code ///////////////////////////////////////////////////////
| 94 | /// |
| 95 | //////////////////////////////////////////////////////////// |
| 96 | int main() |
| 97 | { |
| 98 | // Install SIGINT handler to allow the user to interrupt SFTP operations |
| 99 | std::signal(SIGINT, [](int) { interrupt = true; }); |
| 100 | |
| 101 | // Timeout predicate to check whether the ongoing operation has to be interrupted, poll at 1ms |
| 102 | sf::TimeoutWithPredicate timeout([]() { return !interrupt; }, sf::milliseconds(1)); |
| 103 | |
| 104 | // Choose the server address |
| 105 | auto address = sf::IpAddress::Any; |
| 106 | while (true) |
| 107 | { |
| 108 | std::string addressString; |
| 109 | std::cout << "Enter the SFTP server hostname or IP address: "; |
| 110 | std::cin >> addressString; |
| 111 | if (const auto addresses = sf::Dns::resolve(addressString); addresses && !addresses->empty()) |
| 112 | { |
| 113 | address = addresses->front(); |
| 114 | break; |
| 115 | } |
| 116 | std::cout << "The host could not be resolved to an IP address" << std::endl; |
| 117 | } |
| 118 | |
| 119 | // Choose the server port |
| 120 | std::optional<unsigned short> port; |
| 121 | do |
| 122 | { |
| 123 | try |
| 124 | { |
| 125 | std::cout << "Enter the SFTP server port (empty to use default 22): "; |
| 126 | std::string line; |
| 127 | std::cin.ignore(10'000, '\n'); |
| 128 | std::getline(std::cin, line); |
| 129 | port = static_cast<unsigned short>(line.empty() ? 22ul : std::stoul(line)); |
| 130 | } catch (const std::exception&) |
| 131 | { |
| 132 | continue; |
| 133 | } |
| 134 | } while (!port.has_value()); |
| 135 | |
| 136 | // Connect to the server |
| 137 | sf::Sftp server; |
| 138 | if (const auto result = server.connect(address, port.value(), timeout); !checkResult(result)) |
| 139 | return EXIT_FAILURE; |
| 140 | std::cout << "Connecting to SFTP server at " << address.toString() << " successful" << std::endl; |
| 141 | |
| 142 | // Print session information |
| 143 | const auto sessionInfo = server.getSessionInfo(); |
| 144 | |
| 145 | if (!sessionInfo.has_value()) |
| 146 | { |
| 147 | std::cout << "Failed to retrieve session information" << std::endl; |
| 148 | return EXIT_FAILURE; |
| 149 | } |
| 150 | |
| 151 | std::cout << "\nHost key SHA256: " << std::hex << std::setw(2) << std::setfill('0'); |
| 152 | for (const auto b : sessionInfo->hostKey.sha256) |
| 153 | std::cout << static_cast<int>(b); |
nothing calls this directly
no test coverage detected