26#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
27#include <QtSystemDetection>
29#include <QCoreApplication>
30#include <QHostAddress>
33#include <QNetworkInterface>
34#include <QNetworkAddressEntry>
43#define LOC QString("PortChecker::%1(): ").arg(__func__)
80 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"host %1 port %2 timeLimit %3 linkLocalOnly %4")
81 .arg(host).arg(port).arg(timeLimit.count()).arg(linkLocalOnly));
84 bool isIPAddress = addr.setAddress(host);
85 bool islinkLocal =
false;
90 && addr.protocol() == QAbstractSocket::IPv6Protocol
91 && addr.isInSubnet(QHostAddress::parseSubnet(
"fe80::/10")))
101 host = addr.toString();
110 QList<QNetworkInterface> cards = QNetworkInterface::allInterfaces();
112 QListIterator<QNetworkInterface> iCard = cards;
115 QTcpSocket socket(
this);
116 QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState;
119 bool testedAll =
false;
120 while (state != QAbstractSocket::ConnectedState
121 && (timer.
elapsed() < timeLimit))
123 if (state == QAbstractSocket::UnconnectedState)
131 addr.setScopeId(QString());
132 while (addr.scopeId().isEmpty() && iCardsEnd<2)
137 QNetworkInterface card = iCard.next();
138 LOG(VB_GENERAL, LOG_DEBUG, QString(
"Trying interface %1").arg(card.name()));
139 unsigned int flags = card.flags();
140 if ((flags & QNetworkInterface::IsLoopBack)
141 || !(flags & QNetworkInterface::IsRunning))
144 QList<QNetworkAddressEntry> addresses = card.addressEntries();
145 bool foundv6 =
false;
146 for (
const auto& ae : std::as_const(addresses))
148 if (ae.ip().protocol() == QAbstractSocket::IPv6Protocol)
157 addr.setScopeId(scope);
165 cards = QNetworkInterface::allInterfaces();
175 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"There is no IPV6 compatible interface for %1")
182 dest=addr.toString();
185 socket.connectToHost(
dest, port);
200 std::this_thread::sleep_for(500ms);
201 state = socket.state();
202 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"socket state %1")
205 && state == QAbstractSocket::UnconnectedState
209 if (state == QAbstractSocket::ConnectedState
210 && islinkLocal && !scope.isEmpty())
213 host = addr.toString();
217 return (state == QAbstractSocket::ConnectedState);
238 return checker.
checkPort(host,port,timeLimit,
true);
243 qApp->processEvents(QEventLoop::AllEvents, 250);
244 qApp->processEvents(QEventLoop::AllEvents, 250);
void SetScopeForAddress(const QHostAddress &addr)
Record the scope Id of the given IP address.
bool GetScopeForAddress(QHostAddress &addr) const
Return the cached scope Id for the given address.
A QElapsedTimer based timer to replace use of QTime as a timer.
std::chrono::milliseconds elapsed(void)
Returns milliseconds elapsed since last start() or restart()
Small class to handle TCP port checking and finding link-local context.
static bool resolveLinkLocal(QString &host, int port, std::chrono::milliseconds timeLimit=30s)
Convenience method to resolve link-local address.
static void processEvents(void)
bool checkPort(QString &host, int port, std::chrono::milliseconds timeLimit=30s, bool linkLocalOnly=false)
Check if a port is open and sort out the link-local scope.
void cancelPortCheck(void)
Cancel the checkPort operation currently in progress.
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)