From 92100c85f78f1e3ea3d4689cd64a2d40a2dbafe6 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Thu, 16 Feb 2017 16:30:52 +0100 Subject: [PATCH] refs #883, added utility function ping --- src/blackmisc/network/networkutils.cpp | 42 +++++++++++++++++--------- src/blackmisc/network/networkutils.h | 4 +++ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/blackmisc/network/networkutils.cpp b/src/blackmisc/network/networkutils.cpp index a3d07bb1b..bb27ab90a 100644 --- a/src/blackmisc/network/networkutils.cpp +++ b/src/blackmisc/network/networkutils.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -49,32 +51,43 @@ namespace BlackMisc { // http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4 const QList interfaces = QNetworkInterface::allInterfaces(); - bool result = false; - - for (int i = 0; i < interfaces.count(); i++) + for (const QNetworkInterface &interface : interfaces) { - QNetworkInterface interface = interfaces.at(i); - // details of connection if (withDebugOutput) qDebug() << "name:" << interface.name() << endl << "ip addresses:" << endl << "mac:" << interface.hardwareAddress() << endl; if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack)) { - // this loop is important - for (int j = 0; j < interface.addressEntries().count(); j++) + // this loop is important to check if there are addresses + for (const QNetworkAddressEntry &entry : interface.addressEntries()) { - if (withDebugOutput) qDebug() << interface.addressEntries().at(j).ip().toString() << " / " << interface.addressEntries().at(j).netmask().toString() << endl; + if (withDebugOutput) qDebug() << entry.ip().toString() << " / " << entry.netmask().toString() << endl; // we have an interface that is up, and has an ip address, therefore the link is present // we will only enable this check on first positive, all later results are incorrect - if (!result) - { - result = true; - break; - } + return true; } } } - return result; + return false; + } + + bool CNetworkUtils::canPing(const QString &hostAddress) + { + if (hostAddress.isEmpty()) { return false; } + QStringList params; + if (CBuildConfig::isRunningOnWindowsNtPlatform()) + { + params << "-n" << "1"; + } + else + { + // all UNIX alike + params << "-c" << "1"; + } + params << hostAddress; + + const int exitCode = QProcess::execute("ping", params); + return exitCode == 0; } QStringList CNetworkUtils::getKnownLocalIpV4Addresses() @@ -93,6 +106,7 @@ namespace BlackMisc } } } + ips.sort(); return ips; } diff --git a/src/blackmisc/network/networkutils.h b/src/blackmisc/network/networkutils.h index 47b6bbdaf..5610bee02 100644 --- a/src/blackmisc/network/networkutils.h +++ b/src/blackmisc/network/networkutils.h @@ -54,6 +54,10 @@ namespace BlackMisc //! \param withDebugOutput enables some debugging output static bool hasConnectedInterface(bool withDebugOutput = false); + //! Can ping the address? + //! \note uses OS ping + static bool canPing(const QString &hostAddress); + //! Can connect? //! \param hostAddress 130.4.20.3, or myserver.com //! \param port 80, 1234