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