mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-17 02:45:33 +08:00
refs #205, renamed to CNetworkUtils as more methods for IP-addresses are introduced, added CNetworkUtils::getKnownIpAddresses()
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
#include "blackcore/dbus_server.h"
|
#include "blackcore/dbus_server.h"
|
||||||
#include "blackcore/context_network.h"
|
#include "blackcore/context_network.h"
|
||||||
#include "blackmisc/hwkeyboardkey.h"
|
#include "blackmisc/hwkeyboardkey.h"
|
||||||
#include "blackmisc/networkchecks.h"
|
#include "blackmisc/networkutils.h"
|
||||||
#include "blacksim/fsx/simconnectutilities.h"
|
#include "blacksim/fsx/simconnectutilities.h"
|
||||||
|
|
||||||
using namespace BlackCore;
|
using namespace BlackCore;
|
||||||
@@ -160,19 +160,19 @@ void MainWindow::testSimConnectConnection()
|
|||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "no address or port"));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "no address or port"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!CNetworkChecks::isValidIPv4Address(address))
|
if (!CNetworkUtils::isValidIPv4Address(address))
|
||||||
{
|
{
|
||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "IPv4 address invalid"));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "IPv4 address invalid"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!CNetworkChecks::isValidPort(port))
|
if (!CNetworkUtils::isValidPort(port))
|
||||||
{
|
{
|
||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "invalid port"));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "invalid port"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
quint16 p = port.toUInt();
|
quint16 p = port.toUInt();
|
||||||
QString msg;
|
QString msg;
|
||||||
if (!CNetworkChecks::canConnect(address, p, msg))
|
if (!CNetworkUtils::canConnect(address, p, msg))
|
||||||
{
|
{
|
||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, msg));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, msg));
|
||||||
return;
|
return;
|
||||||
@@ -192,12 +192,12 @@ void MainWindow::saveSimConnectCfg()
|
|||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "no address or port"));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "no address or port"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!CNetworkChecks::isValidIPv4Address(address))
|
if (!CNetworkUtils::isValidIPv4Address(address))
|
||||||
{
|
{
|
||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "IPv4 address invalid"));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "IPv4 address invalid"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!CNetworkChecks::isValidPort(port))
|
if (!CNetworkUtils::isValidPort(port))
|
||||||
{
|
{
|
||||||
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "invalid port"));
|
this->displayStatusMessage(CStatusMessage(CStatusMessage::TypeTrafficNetwork, CStatusMessage::SeverityWarning, "invalid port"));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
#include "vatsimbookingreader.h"
|
#include "vatsimbookingreader.h"
|
||||||
#include "vatsimdatafilereader.h"
|
#include "vatsimdatafilereader.h"
|
||||||
|
|
||||||
#include "blackmisc/networkchecks.h"
|
#include "blackmisc/networkutils.h"
|
||||||
#include "blackmisc/avatcstationlist.h"
|
#include "blackmisc/avatcstationlist.h"
|
||||||
|
|
||||||
#include <QtXml/QDomElement>
|
#include <QtXml/QDomElement>
|
||||||
@@ -128,7 +128,7 @@ namespace BlackCore
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
QString msg;
|
QString msg;
|
||||||
if (CNetworkChecks::canConnect(currentServer, msg, 2000))
|
if (CNetworkUtils::canConnect(currentServer, msg, 2000))
|
||||||
{
|
{
|
||||||
INetwork::LoginMode mode = static_cast<INetwork::LoginMode>(loginMode);
|
INetwork::LoginMode mode = static_cast<INetwork::LoginMode>(loginMode);
|
||||||
this->m_ownAircraft.setPilot(currentServer.getUser()); // still needed?
|
this->m_ownAircraft.setPilot(currentServer.getUser()); // still needed?
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#include "networkchecks.h"
|
#include "networkutils.h"
|
||||||
#include <QtNetwork/QNetworkInterface>
|
#include <QtNetwork/QNetworkInterface>
|
||||||
#include <QtNetwork/QTcpSocket>
|
#include <QtNetwork/QTcpSocket>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
@@ -15,7 +15,7 @@ namespace BlackMisc
|
|||||||
/*
|
/*
|
||||||
* Connected interface?
|
* Connected interface?
|
||||||
*/
|
*/
|
||||||
bool CNetworkChecks::hasConnectedInterface(bool withDebugOutput)
|
bool CNetworkUtils::hasConnectedInterface(bool withDebugOutput)
|
||||||
{
|
{
|
||||||
// http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4
|
// http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4
|
||||||
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
||||||
@@ -24,12 +24,11 @@ namespace BlackMisc
|
|||||||
for (int i = 0; i < interfaces.count(); i++)
|
for (int i = 0; i < interfaces.count(); i++)
|
||||||
{
|
{
|
||||||
QNetworkInterface iface = interfaces.at(i);
|
QNetworkInterface iface = interfaces.at(i);
|
||||||
|
|
||||||
|
// details of connection
|
||||||
|
if (withDebugOutput) qDebug() << "name:" << iface.name() << endl << "ip addresses:" << endl << "mac:" << iface.hardwareAddress() << endl;
|
||||||
if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
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
|
// this loop is important
|
||||||
for (int j = 0; j < iface.addressEntries().count(); j++)
|
for (int j = 0; j < iface.addressEntries().count(); j++)
|
||||||
{
|
{
|
||||||
@@ -48,12 +47,31 @@ namespace BlackMisc
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* my IP
|
||||||
|
*/
|
||||||
|
QStringList CNetworkUtils::getKnownIpAddresses()
|
||||||
|
{
|
||||||
|
QStringList ips;
|
||||||
|
if (!CNetworkUtils::hasConnectedInterface(false)) return ips;
|
||||||
|
foreach(const QHostAddress & address, QNetworkInterface::allAddresses())
|
||||||
|
{
|
||||||
|
if (address.isLoopback() || address.isNull()) continue;
|
||||||
|
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
|
||||||
|
{
|
||||||
|
QString a = address.toString();
|
||||||
|
if (CNetworkUtils::isValidIPv4Address(a)) ips.append(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ips;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Can connect to IP/port?
|
* Can connect to IP/port?
|
||||||
*/
|
*/
|
||||||
bool CNetworkChecks::canConnect(const QString &hostAddress, quint16 port, QString &message, int timeoutMs)
|
bool CNetworkUtils::canConnect(const QString &hostAddress, quint16 port, QString &message, int timeoutMs)
|
||||||
{
|
{
|
||||||
if (!CNetworkChecks::hasConnectedInterface(false))
|
if (!CNetworkUtils::hasConnectedInterface(false))
|
||||||
{
|
{
|
||||||
message = QObject::tr("No connected network interface", "BlackMisc");
|
message = QObject::tr("No connected network interface", "BlackMisc");
|
||||||
return false;
|
return false;
|
||||||
@@ -80,15 +98,15 @@ namespace BlackMisc
|
|||||||
/*
|
/*
|
||||||
* Can connect server?
|
* Can connect server?
|
||||||
*/
|
*/
|
||||||
bool CNetworkChecks::canConnect(const Network::CServer &server, QString &message, int timeoutMs)
|
bool CNetworkUtils::canConnect(const Network::CServer &server, QString &message, int timeoutMs)
|
||||||
{
|
{
|
||||||
return CNetworkChecks::canConnect(server.getAddress(), server.getPort(), message, timeoutMs);
|
return CNetworkUtils::canConnect(server.getAddress(), server.getPort(), message, timeoutMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Valid IPv4 address
|
* Valid IPv4 address
|
||||||
*/
|
*/
|
||||||
bool CNetworkChecks::isValidIPv4Address(const QString &candidate)
|
bool CNetworkUtils::isValidIPv4Address(const QString &candidate)
|
||||||
{
|
{
|
||||||
QHostAddress address(candidate);
|
QHostAddress address(candidate);
|
||||||
return (QAbstractSocket::IPv4Protocol == address.protocol());
|
return (QAbstractSocket::IPv4Protocol == address.protocol());
|
||||||
@@ -97,7 +115,7 @@ namespace BlackMisc
|
|||||||
/*
|
/*
|
||||||
* Valid IPv6 address
|
* Valid IPv6 address
|
||||||
*/
|
*/
|
||||||
bool CNetworkChecks::isValidIPv6Address(const QString &candidate)
|
bool CNetworkUtils::isValidIPv6Address(const QString &candidate)
|
||||||
{
|
{
|
||||||
QHostAddress address(candidate);
|
QHostAddress address(candidate);
|
||||||
return (QAbstractSocket::IPv6Protocol == address.protocol());
|
return (QAbstractSocket::IPv6Protocol == address.protocol());
|
||||||
@@ -106,7 +124,7 @@ namespace BlackMisc
|
|||||||
/*
|
/*
|
||||||
* Valid port?
|
* Valid port?
|
||||||
*/
|
*/
|
||||||
bool CNetworkChecks::isValidPort(const QString &port)
|
bool CNetworkUtils::isValidPort(const QString &port)
|
||||||
{
|
{
|
||||||
bool success;
|
bool success;
|
||||||
int p = port.toInt(&success);
|
int p = port.toInt(&success);
|
||||||
@@ -3,25 +3,23 @@
|
|||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#ifndef BLACKMISC_NETWORKCHECKS_H
|
#ifndef BLACKMISC_NETWORKUTILS_H
|
||||||
#define BLACKMISC_NETWORKCHECKS_H
|
#define BLACKMISC_NETWORKUTILS_H
|
||||||
|
|
||||||
#include "nwserver.h"
|
#include "nwserver.h"
|
||||||
#include <QString>
|
#include <QStringList>
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Utilities checking whether a network connection can be established
|
* \brief Utilities, e.g. checking whether a network connection can be established
|
||||||
*/
|
*/
|
||||||
class CNetworkChecks
|
class CNetworkUtils
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
/*!
|
// Constructor
|
||||||
* \brief Constructor
|
CNetworkUtils() {}
|
||||||
*/
|
|
||||||
CNetworkChecks() {}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/*!
|
/*!
|
||||||
@@ -50,13 +48,16 @@ namespace BlackMisc
|
|||||||
*/
|
*/
|
||||||
static bool canConnect(const BlackMisc::Network::CServer &server, QString &message, int timeoutMs = 1500);
|
static bool canConnect(const BlackMisc::Network::CServer &server, QString &message, int timeoutMs = 1500);
|
||||||
|
|
||||||
//! \brief Valid IPv4 address
|
//! Find out my IPv4 address, empty if not possible
|
||||||
|
static QStringList getKnownIpAddresses();
|
||||||
|
|
||||||
|
//! Valid IPv4 address
|
||||||
static bool isValidIPv4Address(const QString &candidate);
|
static bool isValidIPv4Address(const QString &candidate);
|
||||||
|
|
||||||
//! \brief Valid IPv6 address
|
//! Valid IPv6 address
|
||||||
static bool isValidIPv6Address(const QString &candidate);
|
static bool isValidIPv6Address(const QString &candidate);
|
||||||
|
|
||||||
//! \brief Valid port
|
//! Valid port
|
||||||
static bool isValidPort(const QString &port);
|
static bool isValidPort(const QString &port);
|
||||||
|
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user