--- a/sources/interface.cpp Wed Jan 27 14:16:58 2021 +0200 +++ b/sources/interface.cpp Wed Jan 27 18:55:03 2021 +0200 @@ -132,7 +132,7 @@ { case INPUTSTATE_ADDRESS: if (m_remoteAddress.host != 0) - getEditableInput() = m_remoteAddress.to_string(IPAddress::WITH_PORT); + getEditableInput() = net::ip_address_to_string(m_remoteAddress); break; default: @@ -488,7 +488,7 @@ case RCON_CONNECTING: case RCON_AUTHENTICATING: - text = "Connecting to " + m_session.address().to_string(IPAddress::WITH_PORT) + "..."; + text = "Connecting to " + net::ip_address_to_string(m_session.address()) + "..."; break; case RCON_CONNECTED: @@ -506,7 +506,7 @@ } text = zfc::sprintf("%s | %s | %s", - m_session.address().to_string(IPAddress::WITH_PORT).data(), + net::ip_address_to_string(m_session.address()).data(), m_session.getLevel().data(), adminText.data()); } @@ -606,6 +606,26 @@ m_needInputRender = true; } +bool Interface::tryResolveAddress(const std::string &address_string, net::ip_address* target) +{ + std::stringstream errors; + const std::optional<net::ip_address> address_opt = net::ip_resolve(address_string, errors); + if (address_opt.has_value()) + { + *target = address_opt.value(); + if (target->port == 0) + { + target->port = 10666; + } + return true; + } + else + { + this->printError("%s\n", errors.str().data()); + return false; + } +} + // ------------------------------------------------------------------------------------------------- // void Interface::handleInput() @@ -794,20 +814,10 @@ break; // handled above case INPUTSTATE_ADDRESS: - try - { - m_remoteAddress = IPAddress::from_string(getCurrentInput()); - } - catch (std::exception& e) + if (this->tryResolveAddress(this->getCurrentInput(), &this->m_remoteAddress)) { - print("%s\n", e.what()); - return; + setInputState(INPUTSTATE_PASSWORD); } - - if (m_remoteAddress.port == 0) - m_remoteAddress.port = 10666; - - setInputState(INPUTSTATE_PASSWORD); break; case INPUTSTATE_PASSWORD: @@ -996,24 +1006,14 @@ // ------------------------------------------------------------------------------------------------- // -void Interface::connect(std::string address, std::string password) +void Interface::connect(std::string address_string, std::string password) { - try - { - m_remoteAddress = IPAddress::from_string(address); - } - catch (std::exception& e) + if (this->tryResolveAddress(address_string, &this->m_remoteAddress)) { - print("%s\n", e.what()); - return; + m_session.disconnect(); + m_session.setPassword(password); + m_session.connect(m_remoteAddress); } - - if (m_remoteAddress.port == 0) - m_remoteAddress.port = 10666; - - m_session.disconnect(); - m_session.setPassword(password); - m_session.connect(m_remoteAddress); } // ------------------------------------------------------------------------------------------------- @@ -1069,24 +1069,7 @@ } else { - IPAddress address; - - try - { - address = IPAddress::from_string(args[0]); - } - catch (std::exception& e) - { - printError("%s\n", e.what()); - return; - } - - if (address.port == 0) - address.port = 10666; - - m_session.setPassword(args[1]); - m_session.disconnect(); - m_session.connect(m_remoteAddress = address); + this->connect(args[0], args[1]); } } else if (command == "disconnect") @@ -1107,7 +1090,7 @@ // void Interface::disconnected() { - print("Disconnected from %s\n", m_session.address().to_string(IPAddress::WITH_PORT).data()); + print("Disconnected from %s\n", net::ip_address_to_string(m_session.address()).data()); resetTitle(); renderFull(); }