@@ -169,34 +169,52 @@ 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 ], * p ;
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 , 0 ) < 0 ) {
180+ return zend_string_init (ip , strlen (ip ), 0 );
181+ }
182+ p = strchr (out , '%' ); /* demangle numeric host with %name suffix */
183+ if (p ) {
184+ * p = 0 ;
185+ }
186+ return zend_string_init (out , strlen (out ), 0 );
187+ } else if (inet_pton (AF_INET , ip , & sa4 .sin_addr )) {
188+ sa4 .sin_family = AF_INET ;
189+
190+ if (getnameinfo ((struct sockaddr * )& sa4 , sizeof (sa4 ), out , sizeof (out ), NULL , 0 , 0 ) < 0 ) {
191+ return zend_string_init (ip , strlen (ip ), 0 );
192+ }
193+ p = strchr (out , '%' ); /* demangle numeric host with %name suffix */
194+ if (p ) {
195+ * p = 0 ;
196+ }
197+ return zend_string_init (out , strlen (out ), 0 );
184198 }
199+ return NULL ; /* not a valid IP */
185200#else
201+ struct in_addr addr ;
202+ struct hostent * hp ;
203+
186204 addr .s_addr = inet_addr (ip );
187205
188206 if (addr .s_addr == -1 ) {
189207 return NULL ;
190208 }
191209
192210 hp = gethostbyaddr ((char * ) & addr , sizeof (addr ), AF_INET );
193- #endif
194211
195212 if (!hp || hp -> h_name == NULL || hp -> h_name [0 ] == '\0' ) {
196213 return zend_string_init (ip , strlen (ip ), 0 );
197214 }
198215
199216 return zend_string_init (hp -> h_name , strlen (hp -> h_name ), 0 );
217+ #endif
200218}
201219/* }}} */
202220
0 commit comments