25#include <QCoreApplication>
26#include <QHostAddress>
29#include <QNetworkInterface>
30#include <QNetworkAddressEntry>
39#define LOC QString("PortChecker::%1(): ").arg(__func__)
76 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"host %1 port %2 timeLimit %3 linkLocalOnly %4")
77 .arg(host).arg(port).arg(timeLimit.count()).arg(linkLocalOnly));
80 bool isIPAddress = addr.setAddress(host);
81 bool islinkLocal =
false;
86 && addr.protocol() == QAbstractSocket::IPv6Protocol
87 && addr.isInSubnet(QHostAddress::parseSubnet(
"fe80::/10")))
97 host = addr.toString();
106 QList<QNetworkInterface> cards = QNetworkInterface::allInterfaces();
108 QListIterator<QNetworkInterface> iCard = cards;
111 QTcpSocket socket(
this);
112 QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState;
115 bool testedAll =
false;
116 while (state != QAbstractSocket::ConnectedState
117 && (timer.
elapsed() < timeLimit))
119 if (state == QAbstractSocket::UnconnectedState)
127 addr.setScopeId(QString());
128 while (addr.scopeId().isEmpty() && iCardsEnd<2)
133 QNetworkInterface card = iCard.next();
134 LOG(VB_GENERAL, LOG_DEBUG, QString(
"Trying interface %1").arg(card.name()));
135 unsigned int flags = card.flags();
136 if ((flags & QNetworkInterface::IsLoopBack)
137 || !(flags & QNetworkInterface::IsRunning))
140 QList<QNetworkAddressEntry> addresses = card.addressEntries();
141 bool foundv6 =
false;
142 for (
const auto& ae : std::as_const(addresses))
144 if (ae.ip().protocol() == QAbstractSocket::IPv6Protocol)
153 addr.setScopeId(scope);
161 cards = QNetworkInterface::allInterfaces();
171 LOG(VB_GENERAL, LOG_ERR,
LOC + QString(
"There is no IPV6 compatible interface for %1")
178 dest=addr.toString();
181 socket.connectToHost(
dest, port);
196 std::this_thread::sleep_for(500ms);
197 state = socket.state();
198 LOG(VB_GENERAL, LOG_DEBUG,
LOC + QString(
"socket state %1")
201 && state == QAbstractSocket::UnconnectedState
205 if (state == QAbstractSocket::ConnectedState
206 && islinkLocal && !scope.isEmpty())
209 host = addr.toString();
213 return (state == QAbstractSocket::ConnectedState);
234 return checker.
checkPort(host,port,timeLimit,
true);
239 qApp->processEvents(QEventLoop::AllEvents, 250);
240 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_)