简体   繁体   English

gethostbyname_r() 调用不可解析的主机,总是返回 h_errnop 设置为 TRY_AGAIN(在 Ubuntu 19.04 和 20.04 上)

[英]gethostbyname_r() calls for non-resolvable hosts always returning with h_errnop set to TRY_AGAIN (on Ubuntu 19.04 and 20.04)

    while(do_again) {

       rc = gethostbyname_r(host, &hbuf, tmp, TMPLEN, &hp, &my_h_errno);
       if (rc != 0) {
          if(my_h_errno == TRY_AGAIN) {
             printf("HOST NOT FOUND: got a TRY_AGAIN response from gethostbyname_r()\n");
             continue;
          } else if (my_h_errno == HOST_NOT_FOUND) {
             printf("HOST NOT FOUND: Got an authoritative answer\n");
             exit(0);
          } else {
             printf("Other errors..\n");
             exit(0);
          }
       }
       do_again = false;
    }

Above code always returning with my_h_errno set to TRY_AGAIN for non-resolvable hostnames on Ubuntu 19.04 and 20.04.对于 Ubuntu 19.04 和 20.04 上不可解析的主机名,上面的代码总是返回my_h_errno设置为TRY_AGAIN On previous versions of the OS or on other Linux flavors, it usually returns a more authoritative version, HOST_NOT_FOUND .在以前版本的操作系统或其他 Linux 版本上,它通常返回一个更权威的版本, HOST_NOT_FOUND

How does the call( gethostbyname_r ) exactly work? call( gethostbyname_r ) 是如何工作的? And are there any changes on the dns or lookup mechanism on the latest versions of Ubuntu which is causing such behavior? dns 或 Ubuntu 的最新版本的查找机制是否有任何更改导致此类行为?

[Update] Installing libnss-myhostname which kind of acts as a fallback mechanism for hostname resolving, fixed the issue. [更新] 安装 libnss-myhostname 作为主机名解析的后备机制,解决了这个问题。

Debugging on the glibc code got me to the point where there was a call to the above library functions, if exsits, where the h_errno was set to HOST_NOT_FOUND for non-resolvable hosts.对 glibc 代码的调试使我到达了调用上述库函数的地步,如果存在,则 h_errno 被设置为 HOST_NOT_FOUND 对于不可解析的主机。 That was initially being set to TRY_AGAIN in the glibc's gethostbyname_r functionality for such cases.对于这种情况,最初在 glibc 的 gethostbyname_r 功能中将其设置为 TRY_AGAIN。

From the manual从手册

The gethostbyname*(), gethostbyaddr*(), herror(), and hstrerror() functions are obsolete. gethostbyname*()、gethostbyaddr*()、herror() 和 hstrerror() 函数已过时。 Applications should use getaddrinfo(3), getnameinfo(3), and gai_strerror(3) instead.应用程序应该改用 getaddrinfo(3)、getnameinfo(3) 和 gai_strerror(3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM