diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2009-10-09 07:49:59 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2009-10-09 08:02:53 (GMT) |
commit | 52e22e7b9313233dd248753b25f56cd912382981 (patch) | |
tree | c6fbce025152f021701307758ea3e0dd267b63fc /src/network/kernel | |
parent | eb7daff1548510002031140ceb7fc601d064f670 (diff) | |
download | Qt-52e22e7b9313233dd248753b25f56cd912382981.zip Qt-52e22e7b9313233dd248753b25f56cd912382981.tar.gz Qt-52e22e7b9313233dd248753b25f56cd912382981.tar.bz2 |
Fix QHostInfo IP resolution when there's no reverse for the IP.
If you try to resolve 10.3.4.6, you're probably going to get that it
doesn't exist. On some systems, getnameinfo will return the IP address
in string form (Linux, without NI_NAMEREQD). On some others, it will
fail (Mac, Windows).
So harmonise by gracefully handling the case in which getnameinfo
fails.
Possible behaviour change: we don't try the forward resolution any
more, after completing the reverse one.
Reviewed-by: Peter Hartmann <peter.hartmann@nokia.com>
Diffstat (limited to 'src/network/kernel')
-rw-r--r-- | src/network/kernel/qhostinfo_unix.cpp | 40 | ||||
-rw-r--r-- | src/network/kernel/qhostinfo_win.cpp | 40 |
2 files changed, 30 insertions, 50 deletions
diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index b4ac3d2..7e6e522 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -173,38 +173,28 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) #endif char hbuf[NI_MAXHOST]; - if (!sa || getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) != 0) { - results.setError(QHostInfo::HostNotFound); - results.setErrorString(tr("Host not found")); - return results; - } - results.setHostName(QString::fromLatin1(hbuf)); + if (sa && getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0) + results.setHostName(QString::fromLatin1(hbuf)); #else in_addr_t inetaddr = qt_safe_inet_addr(hostName.toLatin1().constData()); struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET); - if (!ent) { - results.setError(QHostInfo::HostNotFound); - results.setErrorString(tr("Host not found")); - return results; - } - results.setHostName(QString::fromLatin1(ent->h_name)); + if (ent) + results.setHostName(QString::fromLatin1(ent->h_name)); #endif + + if (results.hostName().isEmpty()) + results.setHostName(address.toString()); + results.setAddresses(QList<QHostAddress>() << address); + return results; } // IDN support - QByteArray aceHostname; - if (results.hostName().isEmpty()) { - // it's a hostname resolution - aceHostname = QUrl::toAce(hostName); - results.setHostName(hostName); - if (aceHostname.isEmpty()) { - results.setError(QHostInfo::HostNotFound); - results.setErrorString(hostName.isEmpty() ? QObject::tr("No host name given") : QObject::tr("Invalid hostname")); - return results; - } - } else { - // it's an IP reverse resolution - aceHostname = results.hostName().toLatin1(); + QByteArray aceHostname = QUrl::toAce(hostName); + results.setHostName(hostName); + if (aceHostname.isEmpty()) { + results.setError(QHostInfo::HostNotFound); + results.setErrorString(hostName.isEmpty() ? QObject::tr("No host name given") : QObject::tr("Invalid hostname")); + return results; } #if !defined (QT_NO_GETADDRINFO) diff --git a/src/network/kernel/qhostinfo_win.cpp b/src/network/kernel/qhostinfo_win.cpp index d9d7234..720aaa5 100644 --- a/src/network/kernel/qhostinfo_win.cpp +++ b/src/network/kernel/qhostinfo_win.cpp @@ -160,38 +160,28 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) } char hbuf[NI_MAXHOST]; - if (local_getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) != 0) { - results.setError(QHostInfo::HostNotFound); - results.setErrorString(tr("Host not found")); - return results; - } - results.setHostName(QString::fromLatin1(hbuf)); + if (local_getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0) + results.setHostName(QString::fromLatin1(hbuf)); } else { unsigned long addr = inet_addr(hostName.toLatin1().constData()); struct hostent *ent = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET); - if (!ent) { - results.setError(QHostInfo::HostNotFound); - results.setErrorString(tr("Host not found")); - return results; - } - results.setHostName(QString::fromLatin1(ent->h_name)); + if (ent) + results.setHostName(QString::fromLatin1(ent->h_name)); } + + if (results.hostName().isEmpty()) + results.setHostName(address.toString()); + results.setAddresses(QList<QHostAddress>() << address); + return results; } // IDN support - QByteArray aceHostname; - if (results.hostName().isEmpty()) { - // it's a hostname resolution - aceHostname = QUrl::toAce(hostName); - results.setHostName(hostName); - if (aceHostname.isEmpty()) { - results.setError(QHostInfo::HostNotFound); - results.setErrorString(hostName.isEmpty() ? tr("No host name given") : tr("Invalid hostname")); - return results; - } - } else { - // it's an IP reverse resolution - aceHostname = results.hostName().toLatin1(); + QByteArray aceHostname = QUrl::toAce(hostName); + results.setHostName(hostName); + if (aceHostname.isEmpty()) { + results.setError(QHostInfo::HostNotFound); + results.setErrorString(hostName.isEmpty() ? tr("No host name given") : tr("Invalid hostname")); + return results; } if (local_getaddrinfo && local_freeaddrinfo) { |