@@ -169,34 +169,44 @@ PHP_FUNCTION(gethostbyaddr)
169169static zend_string * php_gethostbyaddr (char * ip )
170170{
171171#if HAVE_IPV6 && HAVE_INET_PTON
172- struct in6_addr addr6 ;
173- #endif
174- struct in_addr addr ;
175- struct hostent * hp ;
172+ struct sockaddr_in sa4 ;
173+ struct sockaddr_in6 sa6 ;
174+ char out [NI_MAXHOST ];
176175
177- #if HAVE_IPV6 && HAVE_INET_PTON
178- if (inet_pton (AF_INET6 , ip , & addr6 )) {
179- hp = gethostbyaddr ((char * ) & addr6 , sizeof (addr6 ), AF_INET6 );
180- } else if (inet_pton (AF_INET , ip , & addr )) {
181- hp = gethostbyaddr ((char * ) & addr , sizeof (addr ), AF_INET );
182- } else {
183- return NULL ;
176+ if (inet_pton (AF_INET6 , ip , & sa6 .sin6_addr )) {
177+ sa6 .sin6_family = AF_INET6 ;
178+
179+ if (getnameinfo ((struct sockaddr * )& sa6 , sizeof (sa6 ), out , sizeof (out ), NULL , 0 , NI_NAMEREQD ) < 0 ) {
180+ return zend_string_init (ip , strlen (ip ), 0 );
181+ }
182+ return zend_string_init (out , strlen (out ), 0 );
183+ } else if (inet_pton (AF_INET , ip , & sa4 .sin_addr )) {
184+ sa4 .sin_family = AF_INET ;
185+
186+ if (getnameinfo ((struct sockaddr * )& sa4 , sizeof (sa4 ), out , sizeof (out ), NULL , 0 , NI_NAMEREQD ) < 0 ) {
187+ return zend_string_init (ip , strlen (ip ), 0 );
188+ }
189+ return zend_string_init (out , strlen (out ), 0 );
184190 }
191+ return NULL ; /* not a valid IP */
185192#else
193+ struct in_addr addr ;
194+ struct hostent * hp ;
195+
186196 addr .s_addr = inet_addr (ip );
187197
188198 if (addr .s_addr == -1 ) {
189199 return NULL ;
190200 }
191201
192202 hp = gethostbyaddr ((char * ) & addr , sizeof (addr ), AF_INET );
193- #endif
194203
195204 if (!hp || hp -> h_name == NULL || hp -> h_name [0 ] == '\0' ) {
196205 return zend_string_init (ip , strlen (ip ), 0 );
197206 }
198207
199208 return zend_string_init (hp -> h_name , strlen (hp -> h_name ), 0 );
209+ #endif
200210}
201211/* }}} */
202212
0 commit comments