Skip to content

use getnameinfo instead of gethostbyaddr #6952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions ext/standard/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,34 +169,44 @@ PHP_FUNCTION(gethostbyaddr)
static zend_string *php_gethostbyaddr(char *ip)
{
#if HAVE_IPV6 && HAVE_INET_PTON
struct in6_addr addr6;
#endif
struct in_addr addr;
struct hostent *hp;
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
char out[NI_MAXHOST];

#if HAVE_IPV6 && HAVE_INET_PTON
if (inet_pton(AF_INET6, ip, &addr6)) {
hp = gethostbyaddr((char *) &addr6, sizeof(addr6), AF_INET6);
} else if (inet_pton(AF_INET, ip, &addr)) {
hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
} else {
return NULL;
if (inet_pton(AF_INET6, ip, &sa6.sin6_addr)) {
sa6.sin6_family = AF_INET6;

if (getnameinfo((struct sockaddr *)&sa6, sizeof(sa6), out, sizeof(out), NULL, 0, NI_NAMEREQD) < 0) {
return zend_string_init(ip, strlen(ip), 0);
}
return zend_string_init(out, strlen(out), 0);
} else if (inet_pton(AF_INET, ip, &sa4.sin_addr)) {
sa4.sin_family = AF_INET;

if (getnameinfo((struct sockaddr *)&sa4, sizeof(sa4), out, sizeof(out), NULL, 0, NI_NAMEREQD) < 0) {
return zend_string_init(ip, strlen(ip), 0);
}
return zend_string_init(out, strlen(out), 0);
}
return NULL; /* not a valid IP */
#else
struct in_addr addr;
struct hostent *hp;

addr.s_addr = inet_addr(ip);

if (addr.s_addr == -1) {
return NULL;
}

hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
#endif

if (!hp || hp->h_name == NULL || hp->h_name[0] == '\0') {
return zend_string_init(ip, strlen(ip), 0);
}

return zend_string_init(hp->h_name, strlen(hp->h_name), 0);
#endif
}
/* }}} */

Expand Down