@@ -169,34 +169,44 @@ PHP_FUNCTION(gethostbyaddr)
169
169
static zend_string * php_gethostbyaddr (char * ip )
170
170
{
171
171
#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 ];
176
175
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 );
184
190
}
191
+ return NULL ; /* not a valid IP */
185
192
#else
193
+ struct in_addr addr ;
194
+ struct hostent * hp ;
195
+
186
196
addr .s_addr = inet_addr (ip );
187
197
188
198
if (addr .s_addr == -1 ) {
189
199
return NULL ;
190
200
}
191
201
192
202
hp = gethostbyaddr ((char * ) & addr , sizeof (addr ), AF_INET );
193
- #endif
194
203
195
204
if (!hp || hp -> h_name == NULL || hp -> h_name [0 ] == '\0' ) {
196
205
return zend_string_init (ip , strlen (ip ), 0 );
197
206
}
198
207
199
208
return zend_string_init (hp -> h_name , strlen (hp -> h_name ), 0 );
209
+ #endif
200
210
}
201
211
/* }}} */
202
212
0 commit comments