From 3859dcef4f9b892322b964120090d53e1f60586d Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 20 Jan 2014 13:05:11 +0100 Subject: [PATCH] Helper class to check whether a network connection can be established ref #101 --- src/blackmisc/networkchecks.cpp | 85 +++++++++++++++++++++++++++++++++ src/blackmisc/networkchecks.h | 57 ++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 src/blackmisc/networkchecks.cpp create mode 100644 src/blackmisc/networkchecks.h diff --git a/src/blackmisc/networkchecks.cpp b/src/blackmisc/networkchecks.cpp new file mode 100644 index 000000000..3a31528ca --- /dev/null +++ b/src/blackmisc/networkchecks.cpp @@ -0,0 +1,85 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "networkchecks.h" +#include +#include +#include + +namespace BlackMisc +{ + /* + * Connected interface? + */ + bool CNetworkChecks::hasConnectedInterface(bool withDebugOutput) + { + // http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4 + QList interfaces = QNetworkInterface::allInterfaces(); + bool result = false; + + for (int i = 0; i < interfaces.count(); i++) + { + QNetworkInterface iface = interfaces.at(i); + if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack)) + { + + // details of connection + if (withDebugOutput) qDebug() << "name:" << iface.name() << endl << "ip addresses:" << endl << "mac:" << iface.hardwareAddress() << endl; + + // this loop is important + for (int j = 0; j < iface.addressEntries().count(); j++) + { + if (withDebugOutput) qDebug() << iface.addressEntries().at(j).ip().toString() << " / " << iface.addressEntries().at(j).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 result; + } + + /* + * Can connect to IP/port? + */ + bool CNetworkChecks::canConnect(const QString &hostAddress, quint16 port, QString &message, int timeoutMs) + { + if (!CNetworkChecks::hasConnectedInterface(false)) + { + message = QCoreApplication::translate("BlackMisc", "No connected network interface"); + return false; + } + + // http://qt-project.org/forums/viewthread/9346 + QTcpSocket socket; + bool connected = false; + socket.connectToHost(hostAddress, port); + if (!socket.waitForConnected(timeoutMs)) + { + message = QCoreApplication::translate("BlackMisc", "Connection failed : %1").arg(socket.errorString()); + connected = false; + } + else + { + message = QCoreApplication::translate("BlackMisc", "OK, connected"); + connected = true; + } + socket.close(); + return connected; + } + + /* + * Can connect server? + */ + bool CNetworkChecks::canConnect(const Network::CServer &server, QString &message, int timeoutMs) + { + return CNetworkChecks::canConnect(server.getAddress(), server.getPort(), message, timeoutMs); + } +} // namespace diff --git a/src/blackmisc/networkchecks.h b/src/blackmisc/networkchecks.h new file mode 100644 index 000000000..c4d3c6f16 --- /dev/null +++ b/src/blackmisc/networkchecks.h @@ -0,0 +1,57 @@ +/* Copyright (C) 2013 VATSIM Community / authors + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BLACKMISC_NETWORKCHECKS_H +#define BLACKMISC_NETWORKCHECKS_H + +#include "nwserver.h" +#include + +namespace BlackMisc +{ + + /*! + * \brief Utilities checking whether a network connection can be established + */ + class CNetworkChecks + { + private: + /*! + * \brief Constructor + */ + CNetworkChecks() {} + + public: + /*! + * \brief Is a connected interface available? + * \param withDebugOutput enables some debugging output + * \return + */ + static bool hasConnectedInterface(bool withDebugOutput = false); + + /*! + * \brief Can connect? + * \param hostAddress 130.4.20.3, or myserver.com + * \param port 80, 1234 + * \param timeoutMs + * \param message human readable message + * \return + */ + static bool canConnect(const QString &hostAddress, quint16 port, QString &message, int timeoutMs = 1500); + + /*! + * \brief Can connect to server? + * \param server + * \param message human readable message + * \param timeoutMs + * \return + */ + static bool canConnect(const BlackMisc::Network::CServer &server, QString &message, int timeoutMs = 1500); + + }; +} // namespace + +#endif // guard +