25 #include <QCoreApplication>
26 #include <QHostAddress>
29 #include <QNetworkInterface>
30 #include <QNetworkAddressEntry>
38 #define LOC QString("PortChecker::%1(): ").arg(__func__)
75 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"host %1 port %2 timeLimit %3 linkLocalOnly %4")
76 .arg(host).arg(port).arg(timeLimit.count()).arg(linkLocalOnly));
79 bool isIPAddress = addr.setAddress(host);
80 bool islinkLocal =
false;
85 && addr.protocol() == QAbstractSocket::IPv6Protocol
86 && addr.isInSubnet(QHostAddress::parseSubnet(
"fe80::/10")))
96 host = addr.toString();
105 QList<QNetworkInterface> cards = QNetworkInterface::allInterfaces();
107 QListIterator<QNetworkInterface> iCard = cards;
110 QTcpSocket socket(
this);
111 QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState;
114 bool testedAll =
false;
115 while (state != QAbstractSocket::ConnectedState
116 && (timer.
elapsed() < timeLimit))
118 if (state == QAbstractSocket::UnconnectedState)
126 addr.setScopeId(QString());
127 while (addr.scopeId().isEmpty() && iCardsEnd<2)
132 QNetworkInterface card = iCard.next();
133 LOG(VB_GENERAL, LOG_DEBUG, QString(
"Trying interface %1").arg(card.name()));
134 unsigned int flags = card.flags();
135 if ((flags & QNetworkInterface::IsLoopBack)
136 || !(flags & QNetworkInterface::IsRunning))
139 QList<QNetworkAddressEntry> addresses = card.addressEntries();
140 bool foundv6 =
false;
141 for (
const auto& ae : std::as_const(addresses))
143 if (ae.ip().protocol() == QAbstractSocket::IPv6Protocol)
152 addr.setScopeId(scope);
160 cards = QNetworkInterface::allInterfaces();
170 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"There is no IPV6 compatible interface for %1")
177 dest=addr.toString();
180 socket.connectToHost(
dest, port);
195 std::this_thread::sleep_for(500ms);
196 state = socket.state();
197 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"socket state %1")
200 && state == QAbstractSocket::UnconnectedState
204 if (state == QAbstractSocket::ConnectedState
205 && islinkLocal && !scope.isEmpty())
208 host = addr.toString();
212 return (state == QAbstractSocket::ConnectedState);
233 return checker.
checkPort(host,port,timeLimit,
true);
238 qApp->processEvents(QEventLoop::AllEvents, 250);
239 qApp->processEvents(QEventLoop::AllEvents, 250);