2 files changed +24
-16
lines changed Original file line number Diff line number Diff line change @@ -913,27 +913,24 @@ addToLibrary({
913
913
$DNS : {
914
914
address_map : {
915
915
id : 1 ,
916
- addrs : { } ,
917
- names : { }
916
+ addrs : { ' localhost ': ' 127.0 .0 .1 ' } ,
917
+ names : { ' 127.0 .0 .1 ': ' localhost ' }
918
918
} ,
919
919
920
920
lookup_name ( name ) {
921
921
// If the name is already a valid ipv4 / ipv6 address, don't generate a fake one.
922
- var res = inetPton4 ( name ) ;
923
- if ( res !== null ) {
922
+ if ( inetPton4 ( name ) != null ) {
924
923
return name ;
925
924
}
926
- res = inetPton6 ( name ) ;
927
- if ( res !== null ) {
925
+ // Unlike the inetPton4 above we don't need and explict null comparison
926
+ // here since there are no valie v6 addresses that are falsey.
927
+ if ( inetPton6 ( name ) ) {
928
928
return name ;
929
929
}
930
930
931
931
// See if this name is already mapped.
932
- var addr ;
933
-
934
- if ( DNS . address_map . addrs [ name ] ) {
935
- addr = DNS . address_map . addrs [ name ] ;
936
- } else {
932
+ var addr = DNS . address_map . addrs [ name ] ;
933
+ if ( ! addr ) {
937
934
var id = DNS . address_map . id ++ ;
938
935
assert ( id < 65535 , 'exceeded max address mappings of 65535' ) ;
939
936
@@ -947,11 +944,8 @@ addToLibrary({
947
944
} ,
948
945
949
946
lookup_addr ( addr ) {
950
- if ( DNS . address_map . names [ addr ] ) {
951
- return DNS . address_map . names [ addr ] ;
952
- }
953
-
954
- return null ;
947
+ // Returns `undefined` if that address is not in the map.
948
+ return DNS . address_map . names [ addr ] ;
955
949
}
956
950
} ,
957
951
Original file line number Diff line number Diff line change @@ -293,6 +293,20 @@ int main() {
293
293
assert (sa4 -> sin_port == ntohs (80 ));
294
294
freeaddrinfo (servinfo );
295
295
296
+ // test loopback address
297
+ err = getaddrinfo ("localhost" , "89" , & hints , & servinfo );
298
+ assert (!err );
299
+ print_addrinfo (servinfo );
300
+ sa4 = ((struct sockaddr_in * )servinfo -> ai_addr );
301
+ assert (servinfo -> ai_family == AF_INET );
302
+ assert (servinfo -> ai_socktype == SOCK_STREAM );
303
+ assert (servinfo -> ai_protocol == IPPROTO_TCP );
304
+ assert (sa4 -> sin_port == ntohs (89 ));
305
+ struct in_addr addr ;
306
+ inet_aton ("127.0.0.1" , & addr );
307
+ assert (sa4 -> sin_addr .s_addr == addr .s_addr );
308
+ freeaddrinfo (servinfo );
309
+
296
310
#ifdef __EMSCRIPTEN__
297
311
// test gai_strerror
298
312
CHECK_ERR (0 , "Unknown error" );
0 commit comments