Fix ping crash if the host address is not valid
This commit is contained in:
parent
61bbd3cbdf
commit
76b7bc1267
@ -33,9 +33,9 @@
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
namespace nymeaserver {
|
||||
Q_DECLARE_LOGGING_CATEGORY(dcNetworkDeviceDiscovery)
|
||||
|
||||
NYMEA_LOGGING_CATEGORY(dcNetworkDeviceDiscovery, "NetworkDeviceDiscovery")
|
||||
namespace nymeaserver {
|
||||
|
||||
NetworkDeviceDiscoveryReplyImpl::NetworkDeviceDiscoveryReplyImpl(QObject *parent) :
|
||||
NetworkDeviceDiscoveryReply(parent)
|
||||
|
||||
@ -71,9 +71,6 @@ bool ArpSocket::sendRequest()
|
||||
if (!m_isOpen)
|
||||
return false;
|
||||
|
||||
// Initially load the arp cache before we start to broadcast
|
||||
//loadArpCache();
|
||||
|
||||
// Send the ARP request trough each network interface
|
||||
qCDebug(dcArpSocket()) << "Sending ARP request to all local network interfaces...";
|
||||
foreach (const QNetworkInterface &networkInterface, QNetworkInterface::allInterfaces()) {
|
||||
@ -88,9 +85,8 @@ bool ArpSocket::sendRequest(const QString &interfaceName)
|
||||
if (!m_isOpen)
|
||||
return false;
|
||||
|
||||
|
||||
// Get the interface
|
||||
qCDebug(dcArpSocket()) << "Sending ARP request to all network interfaces" << interfaceName << "...";
|
||||
qCDebug(dcArpSocket()) << "Sending ARP request to network interface" << interfaceName << "...";
|
||||
QNetworkInterface networkInterface = QNetworkInterface::interfaceFromName(interfaceName);
|
||||
if (!networkInterface.isValid()) {
|
||||
qCWarning(dcArpSocket()) << "Failed to send the ARP request to network interface" << interfaceName << "because the interface is not valid.";
|
||||
@ -109,7 +105,7 @@ bool ArpSocket::sendRequest(const QNetworkInterface &networkInterface)
|
||||
if (networkInterface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
||||
return false;
|
||||
|
||||
// If have no interface indes, we cannot use this network
|
||||
// If the interface index is unknown, we cannot use this network
|
||||
if (networkInterface.index() == 0) {
|
||||
qCDebug(dcArpSocket()) << "Failed to send the ARP request to network interface" << networkInterface.name() << "because the system interface index is unknown.";
|
||||
return false;
|
||||
|
||||
@ -135,7 +135,7 @@ void Ping::sendNextReply()
|
||||
PingReply *reply = m_replyQueue.dequeue();
|
||||
//qCDebug(dcPing()) << "Send next reply," << m_replyQueue.count() << "left in queue";
|
||||
m_queueTimer->start();
|
||||
QTimer::singleShot(0, this, [=]() { performPing(reply); });
|
||||
QTimer::singleShot(0, reply, [=]() { performPing(reply); });
|
||||
}
|
||||
|
||||
void Ping::performPing(PingReply *reply)
|
||||
@ -146,6 +146,13 @@ void Ping::performPing(PingReply *reply)
|
||||
return;
|
||||
}
|
||||
|
||||
if (reply->targetHostAddress().isNull()) {
|
||||
m_error = PingReply::ErrorInvalidHostAddress;
|
||||
qCWarning(dcPing()) << "Cannot send ping request" << m_error;
|
||||
finishReply(reply, m_error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get host ip address
|
||||
struct hostent *hostname = gethostbyname(reply->targetHostAddress().toString().toLocal8Bit().constData());
|
||||
struct sockaddr_in pingAddress;
|
||||
|
||||
@ -57,7 +57,8 @@ public:
|
||||
ErrorPermissionDenied,
|
||||
ErrorSocketError,
|
||||
ErrorTimeout,
|
||||
ErrorHostUnreachable
|
||||
ErrorHostUnreachable,
|
||||
ErrorInvalidHostAddress
|
||||
};
|
||||
Q_ENUM(Error)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user