Skip to content

Commit

Permalink
use getnameinfo instead of gethostbyaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
remicollet committed May 8, 2021
1 parent 05e6f3e commit 99d67d1
Showing 1 changed file with 22 additions and 12 deletions.
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

0 comments on commit 99d67d1

Please sign in to comment.