OSDN Git Service

set result error if lookup by address fails in QHostInfoAgent::fromName()
authorIvailo Monev <xakepa10@laimg.moc>
Sat, 11 Jan 2020 00:28:27 +0000 (00:28 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Sat, 11 Jan 2020 00:28:27 +0000 (00:28 +0000)
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/network/kernel/qhostinfo_unix.cpp

index ecedabf..9d9b91f 100644 (file)
@@ -106,13 +106,33 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
 #endif
 
         char hbuf[NI_MAXHOST];
-        if (sa && getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) == 0)
+        int result = (sa ? ::getnameinfo(sa, saSize, hbuf, sizeof(hbuf), 0, 0, 0) : EAI_NONAME);
+        if (result == 0) {
             results.setHostName(QString::fromLatin1(hbuf));
+        } else if (result == EAI_NONAME || result == EAI_FAIL
+#ifdef EAI_NODATA
+               // EAI_NODATA is deprecated in RFC 3493
+               || result == EAI_NODATA
+#endif
+               ) {
+            results.setError(QHostInfo::HostNotFound);
+            results.setErrorString(tr("Host not found"));
+        } else {
+            results.setError(QHostInfo::UnknownError);
+            results.setErrorString(QString::fromLocal8Bit(gai_strerror(result)));
+        }
 #else
         in_addr_t inetaddr = ::inet_addr(hostName.toLatin1().constData());
         struct hostent *ent = gethostbyaddr((const char *)&inetaddr, sizeof(inetaddr), AF_INET);
-        if (ent)
+        if (ent) {
             results.setHostName(QString::fromLatin1(ent->h_name));
+        } else if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA || h_errno == NO_ADDRESS) {
+            results.setError(QHostInfo::HostNotFound);
+            results.setErrorString(tr("Host not found"));
+        } else {
+            results.setError(QHostInfo::UnknownError);
+            results.setErrorString(tr("Unknown error"));
+        }
 #endif
 
         if (results.hostName().isEmpty())
@@ -187,13 +207,12 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
 
         results.setAddresses(addresses);
         freeaddrinfo(res);
-    } else if (result == EAI_NONAME
-               || result ==  EAI_FAIL
+    } else if (result == EAI_NONAME || result ==  EAI_FAIL
 #ifdef EAI_NODATA
-              // EAI_NODATA is deprecated in RFC 3493
-              || result == EAI_NODATA
+                // EAI_NODATA is deprecated in RFC 3493
+                || result == EAI_NODATA
 #endif
-              ) {
+                ) {
         results.setError(QHostInfo::HostNotFound);
         results.setErrorString(tr("Host not found"));
     } else {