diff --git a/src/blackmisc/dbusserver.cpp b/src/blackmisc/dbusserver.cpp index 8b9b413b5..378fbb9b3 100644 --- a/src/blackmisc/dbusserver.cpp +++ b/src/blackmisc/dbusserver.cpp @@ -291,17 +291,34 @@ namespace BlackMisc QString CDBusServer::p2pAddress(const QString &host, const QString &port) { - QString h = host.isEmpty() ? "127.0.0.1" : host.trimmed(); - QString p = port; + QString h = host.trimmed().toLower().remove(' '); + if (h.isEmpty()) { h = "127.0.0.1"; } + + // check port + bool ok = false; + QString p = port.toLower().trimmed(); + if (!p.isEmpty()) + { + p.toInt(&ok); + if (!ok) + { + p = ""; // was not a number + } + } // can handle host and port separately or combined, e.g. "myhost:1234" if (port.isEmpty()) { + if (h.startsWith("tcp:") && h.contains("host=") && h.contains("port=")) + { + // looks we already got a full string + return h; + } + + // 192.168.5.3:9300 style if (h.contains(":")) { QStringList parts = h.split(":"); - // todo: Replace assert with input validation - Q_ASSERT_X(parts.length() == 2, "p2pAdress", "Wrong IP string split"); h = parts.at(0).trimmed(); p = parts.at(1).trimmed(); } @@ -318,7 +335,16 @@ namespace BlackMisc QString CDBusServer::normalizeAddress(const QString &address) { - if (address.isEmpty() || address == sessionBusAddress() || address == systemBusAddress()) { return address; } + QString lc(address.toLower().trimmed()); + + if (lc.isEmpty()) { return sessionBusAddress(); } + if (lc == sessionBusAddress() || lc == systemBusAddress()) { return lc; } + + // some aliases + if (lc.startsWith("sys")) { return systemBusAddress(); } + if (lc.startsWith("ses")) { return sessionBusAddress(); } + + // Qt / p2p if (isQtDBusAddress(address)) { return address; } return p2pAddress(address); } diff --git a/src/blackmisc/dbusserver.h b/src/blackmisc/dbusserver.h index 3c0d86841..1a9023dfe 100644 --- a/src/blackmisc/dbusserver.h +++ b/src/blackmisc/dbusserver.h @@ -85,9 +85,12 @@ namespace BlackMisc //! Address denoting a P2P server at the given host and port. //! \remarks Port number may be embedding in the host string after a colon. - static QString p2pAddress(const QString &host = "127.0.0.1", const QString &port = ""); + //! \return p2p address like "tcp:host=foo.bar.com,port=1234" + static QString p2pAddress(const QString &host, const QString &port = ""); //! Turn something like 127.0.0.1:45000 into "tcp:host=127.0.0.1,port=45000" + //! \note Handles also "session" and "system" as valid address while CDBusServer::p2pAddress is for + //! P2P addresses only. static QString normalizeAddress(const QString &address); //! Return the server mode of the given address diff --git a/src/xbus/plugin.cpp b/src/xbus/plugin.cpp index f8dbbad14..ce386f629 100644 --- a/src/xbus/plugin.cpp +++ b/src/xbus/plugin.cpp @@ -26,7 +26,7 @@ namespace XBus { m_startServerMenuItems.push_back(m_menu.item("Start server on session bus", [this]{ startServer(BlackMisc::CDBusServer::sessionBusAddress()); })); m_startServerMenuItems.push_back(m_menu.item("Start server on system bus", [this]{ startServer(BlackMisc::CDBusServer::systemBusAddress()); })); - m_startServerMenuItems.push_back(m_menu.item("Start server on localhost P2P", [this]{ startServer(BlackMisc::CDBusServer::p2pAddress()); })); + m_startServerMenuItems.push_back(m_menu.item("Start server on localhost P2P", [this]{ startServer(BlackMisc::CDBusServer::p2pAddress("localhost")); })); } void CPlugin::startServer(const QString &address)