static void resolveLibrary() { #ifndef QT_NO_LIBRARY QLibrary lib(QLatin1String("resolv")); if (!lib.load()) return; local_res_init = res_init_proto(lib.resolve("__res_init")); if (!local_res_init) local_res_init = res_init_proto(lib.resolve("res_init")); local_res_ninit = res_ninit_proto(lib.resolve("__res_ninit")); if (!local_res_ninit) local_res_ninit = res_ninit_proto(lib.resolve("res_ninit")); if (!local_res_ninit) { // if we can't get a thread-safe context, we have to use the global _res state local_res = res_state_ptr(lib.resolve("_res")); } else { local_res_nclose = res_nclose_proto(lib.resolve("res_nclose")); if (!local_res_nclose) local_res_nclose = res_nclose_proto(lib.resolve("__res_nclose")); if (!local_res_nclose) local_res_ninit = 0; } #endif }
static void resolveLibrary() { #if !defined(QT_NO_LIBRARY) && !defined(Q_OS_QNX) QLibrary lib(QLatin1String("resolv")); lib.setLoadHints(QLibrary::ImprovedSearchHeuristics); if (!lib.load()) return; local_res_init = res_init_proto(lib.resolve("__res_init")); if (!local_res_init) local_res_init = res_init_proto(lib.resolve("res_init")); local_res_ninit = res_ninit_proto(lib.resolve("__res_ninit")); if (!local_res_ninit) local_res_ninit = res_ninit_proto(lib.resolve("res_ninit")); if (!local_res_ninit) { // if we can't get a thread-safe context, we have to use the global _res state local_res = res_state_ptr(lib.resolve("_res")); } else { local_res_nclose = res_nclose_proto(lib.resolve("res_nclose")); if (!local_res_nclose) local_res_nclose = res_nclose_proto(lib.resolve("__res_nclose")); if (!local_res_nclose) local_res_ninit = 0; } #endif }
QString QHostInfo::localDomainName() { #if !defined(Q_OS_VXWORKS) && !defined(Q_OS_ANDROID) resolveLibrary(); if (local_res_ninit) { // using thread-safe version res_state_ptr state = res_state_ptr(malloc(sizeof(*state))); Q_CHECK_PTR(state); memset(state, 0, sizeof(*state)); local_res_ninit(state); QString domainName = QUrl::fromAce(state->defdname); if (domainName.isEmpty()) domainName = QUrl::fromAce(state->dnsrch[0]); local_res_nclose(state); free(state); return domainName; } if (local_res_init && local_res) { // using thread-unsafe version local_res_init(); QString domainName = QUrl::fromAce(local_res->defdname); if (domainName.isEmpty()) domainName = QUrl::fromAce(local_res->dnsrch[0]); return domainName; } #endif // nothing worked, try doing it by ourselves: QFile resolvconf; #if defined(_PATH_RESCONF) resolvconf.setFileName(QFile::decodeName(_PATH_RESCONF)); #else resolvconf.setFileName(QLatin1String("/etc/resolv.conf")); #endif if (!resolvconf.open(QIODevice::ReadOnly)) return QString(); // failure QString domainName; while (!resolvconf.atEnd()) { QByteArray line = resolvconf.readLine().trimmed(); if (line.startsWith("domain ")) return QUrl::fromAce(line.mid(sizeof "domain " - 1).trimmed()); // in case there's no "domain" line, fall back to the first "search" entry if (domainName.isEmpty() && line.startsWith("search ")) { QByteArray searchDomain = line.mid(sizeof "search " - 1).trimmed(); int pos = searchDomain.indexOf(' '); if (pos != -1) searchDomain.truncate(pos); domainName = QUrl::fromAce(searchDomain); } } // return the fallen-back-to searched domain return domainName; }
static bool resolveLibraryInternal() { #if QT_CONFIG(library) && !defined(Q_OS_QNX) QLibrary lib; #ifdef LIBRESOLV_SO lib.setFileName(QStringLiteral(LIBRESOLV_SO)); if (!lib.load()) #endif { lib.setFileName(QLatin1String("resolv")); if (!lib.load()) return false; } local_res_init = res_init_proto(lib.resolve("__res_init")); if (!local_res_init) local_res_init = res_init_proto(lib.resolve("res_init")); local_res_ninit = res_ninit_proto(lib.resolve("__res_ninit")); if (!local_res_ninit) local_res_ninit = res_ninit_proto(lib.resolve("res_ninit")); if (!local_res_ninit) { // if we can't get a thread-safe context, we have to use the global _res state local_res = res_state_ptr(lib.resolve("_res")); } else { local_res_nclose = res_nclose_proto(lib.resolve("res_nclose")); if (!local_res_nclose) local_res_nclose = res_nclose_proto(lib.resolve("__res_nclose")); if (!local_res_nclose) local_res_ninit = 0; } #endif return true; }
QString QHostInfo::localDomainName() { #if !defined(Q_OS_VXWORKS) resolveLibrary(); if (local_res_ninit) { // using thread-safe version res_state_ptr state = res_state_ptr(qMalloc(sizeof(*state))); Q_CHECK_PTR(state); memset(state, 0, sizeof(*state)); local_res_ninit(state); QString domainName = QUrl::fromAce(state->defdname); if (domainName.isEmpty()) domainName = QUrl::fromAce(state->dnsrch[0]); local_res_nclose(state); qFree(state); return domainName; } if (local_res_init && local_res) { // using thread-unsafe version #if defined(QT_NO_GETADDRINFO) // We have to call res_init to be sure that _res was initialized // So, for systems without getaddrinfo (which is thread-safe), we lock the mutex too QMutexLocker locker(::getHostByNameMutex()); #endif local_res_init(); QString domainName = QUrl::fromAce(local_res->defdname); if (domainName.isEmpty()) domainName = QUrl::fromAce(local_res->dnsrch[0]); return domainName; } #endif // nothing worked, try doing it by ourselves: QFile resolvconf; #if defined(_PATH_RESCONF) resolvconf.setFileName(QFile::decodeName(_PATH_RESCONF)); #else resolvconf.setFileName(QLatin1String("/etc/resolv.conf")); #endif if (!resolvconf.open(QIODevice::ReadOnly)) return QString(); // failure QString domainName; while (!resolvconf.atEnd()) { QByteArray line = resolvconf.readLine().trimmed(); if (line.startsWith("domain ")) return QUrl::fromAce(line.mid(sizeof "domain " - 1).trimmed()); // in case there's no "domain" line, fall back to the first "search" entry if (domainName.isEmpty() && line.startsWith("search ")) { QByteArray searchDomain = line.mid(sizeof "search " - 1).trimmed(); int pos = searchDomain.indexOf(' '); if (pos != -1) searchDomain.truncate(pos); domainName = QUrl::fromAce(searchDomain); } } // return the fallen-back-to searched domain return domainName; }