8#include <qplatformdefs.h>
10#include "QtCore/qapplicationstatic.h"
11#include "QtCore/qscopedpointer.h"
25# include <netinet/in.h>
26# if defined(AI_ADDRCONFIG) && !defined(Q_OS_WASM)
27# define Q_ADDRCONFIG AI_ADDRCONFIG
32# define QT_SOCKLEN_T int
44struct ToBeLookedUpEquals {
45 typedef bool result_type;
46 explicit ToBeLookedUpEquals(
const QString &toBeLookedUp) noexcept : m_toBeLookedUp(toBeLookedUp) {}
49 return m_toBeLookedUp == lookup->toBeLookedUp;
55template <
typename InputIt,
typename OutputIt1,
typename OutputIt2,
typename UnaryPredicate>
56std::pair<OutputIt1, OutputIt2> separate_if(InputIt
first, InputIt last, OutputIt1 dest1, OutputIt2 dest2, UnaryPredicate
p)
58 while (
first != last) {
68 return std::make_pair(dest1, dest2);
94 if (withContextObject && !receiver)
97 static const int signal_index = []() ->
int {
98 auto senderMetaObject = &QHostInfoResult::staticMetaObject;
100 int signal_index = -1;
123 auto args = metaCallEvent->args();
125 if (!withContextObject || receiver)
196 return 1 +
counter.fetchAndAddRelaxed(1);
230 return QHostInfo::lookupHostImpl(
name, receiver,
nullptr, member);
317 theHostInfoLookupManager()->abortLookup(
id);
335#if defined QHOSTINFO_DEBUG
336 qDebug(
"QHostInfo::fromName(\"%s\")",
name.toLatin1().constData());
356 sockaddr *sa =
nullptr;
359 sa =
reinterpret_cast<sockaddr *
>(&sa4);
360 saSize =
sizeof(sa4);
361 memset(&sa4, 0,
sizeof(sa4));
362 sa4.sin_family = AF_INET;
363 sa4.sin_addr.s_addr = htonl(
address.toIPv4Address());
365 sa =
reinterpret_cast<sockaddr *
>(&sa6);
366 saSize =
sizeof(sa6);
367 memset(&sa6, 0,
sizeof(sa6));
369 memcpy(&sa6.sin6_addr,
address.toIPv6Address().c,
sizeof(sa6.sin6_addr));
373 if (sa && getnameinfo(sa, saSize, hbuf,
sizeof(hbuf),
nullptr, 0, 0) == 0)
401 addrinfo *
res =
nullptr;
402 struct addrinfo hints;
403 memset(&hints, 0,
sizeof(hints));
404 hints.ai_family = PF_UNSPEC;
406 hints.ai_flags = Q_ADDRCONFIG;
411 if (
result == EAI_BADFLAGS) {
419 addrinfo *node =
res;
422#ifdef QHOSTINFO_DEBUG
423 qDebug() <<
"getaddrinfo node: flags:" << node->ai_flags <<
"family:" << node->ai_family
424 <<
"ai_socktype:" << node->ai_socktype <<
"ai_protocol:" << node->ai_protocol
425 <<
"ai_addrlen:" << node->ai_addrlen;
427 switch (node->ai_family) {
430 addr.setAddress(ntohl(((sockaddr_in *) node->ai_addr)->sin_addr.s_addr));
437 sockaddr_in6 *sa6 = (sockaddr_in6 *) node->ai_addr;
438 addr.setAddress(sa6->sin6_addr.s6_addr);
439 if (sa6->sin6_scope_id)
449 node = node->ai_next;
458 results.setAddresses(addresses);
463 case WSAHOST_NOT_FOUND:
487#if defined(QHOSTINFO_DEBUG)
489 qDebug(
"QHostInfoAgent::fromName(): error #%d %s",
494 for (
int i = 0;
i < addresses.
count(); ++
i) {
495 if (
i != 0) tmp +=
", "_L1;
496 tmp += addresses.
at(
i).toString();
498 qDebug(
"QHostInfoAgent::fromName(): found %i entries for \"%s\": {%s}",
562 *d_ptr = *
other.d_ptr;
737#if defined QHOSTINFO_DEBUG
738 qDebug(
"QHostInfo::lookupHostImpl(\"%s\", %p, %p, %s)",
739 name.toLatin1().constData(), receiver, slotObj, member ? member + 1 : 0);
745 qWarning(
"QHostInfo::lookupHost() called with no event dispatcher");
749 qRegisterMetaType<QHostInfo>();
759 if (receiver && member)
762 result.postResultsReady(hostInfo);
776 if (receiver && member)
779 result.postResultsReady(hostInfo);
790 info.setLookupId(
id);
792 if (receiver && member)
802 if (receiver && member)
805 manager->scheduleLookup(runnable);
813 toBeLookedUp(hn),
id(
i), resultEmitter(receiver, slotObj)
858 const auto partitionBegin = std::stable_partition(
manager->postponedLookups.rbegin(),
manager->postponedLookups.rend(),
860 const auto partitionEnd =
manager->postponedLookups.end();
861 for (
auto it = partitionBegin;
it != partitionEnd; ++
it) {
868 manager->postponedLookups.erase(partitionBegin, partitionEnd);
879 &threadPool, [&](
QObject *) { threadPool.waitForDone(); },
881 threadPool.setMaxThreadCount(20);
903 postponedLookups.clear();
910 threadPool.waitForDone();
916void QHostInfoLookupManager::rescheduleWithMutexHeld()
936 return std::any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(lookup->toBeLookedUp));
940 postponedLookups.erase(separate_if(postponedLookups.begin(),
941 postponedLookups.end(),
942 postponedLookups.begin(),
944 isAlreadyRunning).first,
945 postponedLookups.end());
950 std::back_inserter(postponedLookups),
952 isAlreadyRunning).second,
955 const int availableThreads = threadPool.maxThreadCount() - currentLookups.size();
956 if (availableThreads > 0) {
959 while (readyToStartCount--) {
961 threadPool.start(*
it);
962 currentLookups.push_back(std::move(*
it));
982 rescheduleWithMutexHeld();
995 for (
int i = 0;
i < postponedLookups.size();
i++) {
996 if (postponedLookups.at(
i)->id ==
id) {
997 delete postponedLookups.takeAt(
i);
1034#if QT_CONFIG(thread)
1035 currentLookups.removeOne(
r);
1038 rescheduleWithMutexHeld();
1057 *
id = QHostInfo::lookupHostImpl(
name, receiver,
nullptr, member);
1071#ifdef QT_BUILD_INTERNAL
1094#ifdef QT_QHOSTINFO_CACHE_DISABLED_BY_DEFAULT
1095 enabled.store(
false, std::memory_order_relaxed);
1104 if (QHostInfoCacheElement *element =
cache.object(
name)) {
1105 if (element->age.elapsed() <
max_age*1000)
1107 return element->info;
1123 QHostInfoCacheElement* element =
new QHostInfoCacheElement();
1124 element->info =
info;
1126 element->age.start();
1129 cache.insert(name, element);
1140#include "moc_qhostinfo_p.cpp"
static QAbstractEventDispatcher * instance(QThread *thread=nullptr)
Returns a pointer to the event dispatcher object for the specified thread.
static constexpr auto IPv4Protocol
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
The QHostAddress class provides an IP address.
static QHostInfo lookup(const QString &hostName)
static QHostInfo reverseLookup(const QHostAddress &address)
static QHostInfo fromName(const QString &hostName)
QHostInfo get(const QString &name, bool *valid)
void put(const QString &name, const QHostInfo &info)
~QHostInfoLookupManager()
QQueue< QHostInfoRunnable * > scheduledLookups
void lookupFinished(QHostInfoRunnable *r)
void scheduleLookup(QHostInfoRunnable *r)
QList< int > abortedLookups
QList< QHostInfoRunnable * > finishedLookups
void resultsReady(const QHostInfo &info)
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
void postResultsReady(const QHostInfo &info)
void run() override
Implement this pure virtual function in your subclass.
QHostInfoRunnable(const QString &hn, int i, const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
QHostInfoResult resultEmitter
The QHostInfo class provides static functions for host name lookups.
void setHostName(const QString &name)
Sets the host name of this QHostInfo to hostName.
static int lookupHost(const QString &name, QObject *receiver, const char *member)
Looks up the IP address(es) associated with host name name, and returns an ID for the lookup.
static void abortHostLookup(int lookupId)
Aborts the host lookup with the ID id, as returned by lookupHost().
void setAddresses(const QList< QHostAddress > &addresses)
Sets the list of addresses in this QHostInfo to addresses.
QHostInfo & operator=(const QHostInfo &d)
Assigns the data of the other object to this host info object, and returns a reference to it.
static QString localHostName()
Returns this machine's host name, if one is configured.
HostInfoError
This enum describes the various errors that can occur when trying to resolve a host name.
HostInfoError error() const
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError.
void setError(HostInfoError error)
Sets the error type of this QHostInfo to error.
QHostInfo(int lookupId=-1)
Constructs an empty host info object with lookup ID id.
QString hostName() const
Returns the name of the host whose IP addresses were looked up.
QString errorString() const
If the lookup failed, this function returns a human readable description of the error; otherwise "Unk...
static QHostInfo fromName(const QString &name)
Looks up the IP address(es) for the given host name.
void setErrorString(const QString &errorString)
Sets the human readable description of the error that occurred to str if the lookup failed.
QList< QHostAddress > addresses() const
Returns the list of IP addresses associated with hostName().
int lookupId() const
Returns the ID of this lookup.
void setLookupId(int id)
Sets the ID of this lookup to id.
~QHostInfo()
Destroys the host info object.
qsizetype size() const noexcept
const_pointer constData() const noexcept
bool isEmpty() const noexcept
iterator erase(const_iterator begin, const_iterator end)
const_reference at(qsizetype i) const noexcept
qsizetype removeAll(const AT &t)
qsizetype count() const noexcept
void append(parameter_type t)
void unlock() noexcept
Unlocks this mutex locker.
QAbstractNetworkCache * cache() const
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
virtual bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void deleteLater()
\threadsafe
void enqueue(const T &t)
Adds value t to the tail of the queue.
void setAutoDelete(bool autoDelete)
Enables auto-deletion if autoDelete is true; otherwise auto-deletion is disabled.
\macro QT_RESTRICTED_CAST_FROM_ASCII
QByteArray toLatin1() const &
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
static QString fromWCharArray(const wchar_t *string, qsizetype size=-1)
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
static QString machineHostName()
static QThread * currentThread()
static QByteArray toAce(const QString &domain, AceProcessingOptions options={})
void destroyIfLastRef() noexcept
void call(QObject *r, void **a)
qDeleteAll(list.begin(), list.end())
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_APPLICATION_STATIC(TYPE, NAME,...)
#define Q_BASIC_ATOMIC_INITIALIZER(a)
DBusConnection const char DBusError * error
void qt_qhostinfo_clear_cache()
QHostInfo qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)
void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e)
void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution)
constexpr const T & qMin(const T &a, const T &b)
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLuint GLuint64EXT address
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
#define Q_AUTOTEST_EXPORT
Q_CHECK_PTR(a=new int[80])
QFileInfo info(fileName)
[8]
QNetworkAccessManager manager
bool contains(const AT &t) const noexcept