|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73518 gethostbyaddr unnecessarily attempts to resolve the domain name back to IP addr
Submitted: 2016-11-14 20:32 UTC Modified: 2016-12-04 04:22 UTC
From: teo8976 at gmail dot com Assigned:
Status: No Feedback Package: HTTP related
PHP Version: 5.6.28 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: teo8976 at gmail dot com
New email:
PHP Version: OS:


 [2016-11-14 20:32 UTC] teo8976 at gmail dot com
When you call gethostbyaddr($someip), it does 2 DNS lookups:
1 - looks up the IP to find the corresponding domain name, i.e. does its job
2 - then looks up the domain name, as if it needed to find the corresponding IP address

The second is completely unnecessary, especially considering that, if it times out, it nonetheless returns the host name found at step 1 (if, in this case, it returned false or the IP address, one may perhaps argue that it makes sense in order to verify that the host name is correct or something). 

Test script:
echo gethostbyaddr('');

Expected result:
should immediately print:

At you can verify that it takes a fraction of a second to do the reverse lookup

Actual result:
From most servers, it takes more than 15 seconds (I guess it depends on how long it takes for the DNS resolver to time out), and then it prints the expected domain name.

Apparently, there is something wrong with this domain which prevents the name from being resolved to an IP:

However, as explained above, the name->IP lookup is completely unnecessary.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-20 20:32 UTC] php at duncanc dot co dot uk
I don't think this is a PHP issue. As far as I'm aware this functionality comes from a C library (libc)
 [2016-11-20 20:55 UTC]
-Status: Open +Status: Feedback
 [2016-11-20 20:55 UTC]
As you can see PHP calls gethostbyaddr(3) and only does so once. Do you have any evidence to support the claim that there are two lookups? Is there something on your system, or in an upstream DNS server, that is responsible instead of PHP?
 [2016-11-20 21:12 UTC]
It is easy enough to verify that there aren't 2 lookups:

strace -Ff -tt -T php -r "echo gethostbyaddr('');"

13:09:27.142617 stat("/etc/resolv.conf", {st_mode=S_IFREG|0644, st_size=55, ...}) = 0 <0.000006>
13:09:27.142644 socket(AF_INET, SOCK_DGRAM|SOCK_NONBLOCK, IPPROTO_IP) = 3 <0.000006>
13:09:27.142660 connect(3, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("")}, 16) = 0 <0.000008>
13:09:27.142688 poll([{fd=3, events=POLLOUT}], 1, 0) = 1 ([{fd=3, revents=POLLOUT}]) <0.000004>
13:09:27.142703 sendto(3, "\224!\1\0\0\1\0\0\0\0\0\0\003106\003181\003232\00295\7in-a"..., 45, MSG_NOSIGNAL, NULL, 0) = 45 <0.000014>
13:09:27.142731 poll([{fd=3, events=POLLIN}], 1, 5000) = 1 ([{fd=3, revents=POLLIN}]) <0.000634>
13:09:27.143377 ioctl(3, FIONREAD, [111]) = 0 <0.000005>
13:09:27.143394 recvfrom(3, "\224!\201\200\0\1\0\1\0\0\0\0\003106\003181\003232\00295\7in-a"..., 1024, 0, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("")}, [28->16]) = 111 <0.000006>
13:09:27.143420 close(3)                = 0 <0.000007>
13:09:27.143450 write(1, "host106-181-dynamic.232-95-r.ret"..., = 52 <0.000005>

PHP calls the standard POSIX library function gethostbyaddr() just once. The above strace shows what that function does below the covers. It checks to see if resolv.conf has changed, then connects to the dns server
( in my case), sends the query, polls to wait for the reply, receives the reply and writes the result. That's all. If you reverse lookup is taking a long time look at your resolver. It is not PHP getting in the way. You can see from the syscall timing in the strace that in my case it took 0.000833 seconds from the resolv.conf stat to writing the answer. A lot less than 15 seconds.
 [2016-12-04 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 19:01:30 2024 UTC