-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewallchecker.cpp
More file actions
88 lines (63 loc) · 2.44 KB
/
Copy pathfirewallchecker.cpp
File metadata and controls
88 lines (63 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "firewallchecker.h"
FirewallChecker::FirewallChecker(QObject *parent):
QObject(parent),
target_list()
{
}
bool FirewallChecker::inProgress() const
{
return !target_list.isEmpty();
}
void FirewallChecker::checkTargets(const QStringList & targets)
{
target_list = targets;
for (const QString & target: target_list) {
QTcpSocket * sock = new QTcpSocket(this);
sock->setProperty("target", target);
connect(sock, &QTcpSocket::readyRead, this, &FirewallChecker::checkAnswer);
connect(sock, &QTcpSocket::connected, this, &FirewallChecker::checkConnection);
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(checkError(QAbstractSocket::SocketError)));
qDebug() << "FirewallChecker: Checking availability of target" << target;
sock->connectToHost(QHostAddress(target.split(":").first()), target.split(":").last().toInt());
}
}
void FirewallChecker::checkConnection()
{
QTcpSocket * sock = qobject_cast<QTcpSocket *> (sender());
qDebug() << "FirewallChecker: Connection to" << sock->property("target").toString() << "eastablished, waiting for status";
sock->write(QString("CHECK\n").toUtf8());
sock->waitForBytesWritten();
}
void FirewallChecker::checkAnswer()
{
QTcpSocket * sock = qobject_cast<QTcpSocket *> (sender());
if (sock->canReadLine()) {
QString answer = QString::fromUtf8(sock->readLine()).trimmed();
QString target = sock->property("target").toString();
if (answer == QString("UP")) {
qDebug() << "FirewallChecker: Target" << target << "reported to be UP";
emit targetAvailable(target);
} else {
qDebug() << "FirewallChecker: Target" << target << "reported to be DOWN";
emit targetUnavailable(target);
}
sock->disconnectFromHost();
sock->deleteLater();
target_list.removeAll(target);
if (target.isEmpty()) {
emit allTargetsChecked();
}
}
}
void FirewallChecker::checkError(QAbstractSocket::SocketError error)
{
QTcpSocket * sock = qobject_cast<QTcpSocket *> (sender());
QString target = sock->property("target").toString();
qDebug() << "FirewallChecker: An error" << error << "occured while connecting to" << target;
emit targetUnavailable(target);
sock->deleteLater();
target_list.removeAll(target);
if (target.isEmpty()) {
emit allTargetsChecked();
}
}