Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit support for localhost DNS lookup #22641

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,27 +913,24 @@ addToLibrary({
$DNS: {
address_map: {
id: 1,
addrs: {},
names: {}
addrs: {'localhost': '127.0.0.1'},
names: {'127.0.0.1': 'localhost'}
},

lookup_name(name) {
// If the name is already a valid ipv4 / ipv6 address, don't generate a fake one.
var res = inetPton4(name);
if (res !== null) {
if (inetPton4(name) != null) {
return name;
}
res = inetPton6(name);
if (res !== null) {
// Unlike the inetPton4 above we don't need and explict null comparison
// here since there are no valie v6 addresses that are falsey.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// here since there are no valie v6 addresses that are falsey.
// here since there are no valid v6 addresses that are falsey.

if (inetPton6(name)) {
return name;
}

// See if this name is already mapped.
var addr;

if (DNS.address_map.addrs[name]) {
addr = DNS.address_map.addrs[name];
} else {
var addr = DNS.address_map.addrs[name];
if (!addr) {
var id = DNS.address_map.id++;
assert(id < 65535, 'exceeded max address mappings of 65535');

Expand All @@ -947,11 +944,8 @@ addToLibrary({
},

lookup_addr(addr) {
if (DNS.address_map.names[addr]) {
return DNS.address_map.names[addr];
}

return null;
// Returns `undefined` if that address is not in the map.
return DNS.address_map.names[addr];
}
},

Expand Down
14 changes: 14 additions & 0 deletions test/sockets/test_getaddrinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ int main() {
assert(sa4->sin_port == ntohs(80));
freeaddrinfo(servinfo);

// test loopback address
err = getaddrinfo("localhost", "89", &hints, &servinfo);
assert(!err);
print_addrinfo(servinfo);
sa4 = ((struct sockaddr_in*)servinfo->ai_addr);
assert(servinfo->ai_family == AF_INET);
assert(servinfo->ai_socktype == SOCK_STREAM);
assert(servinfo->ai_protocol == IPPROTO_TCP);
assert(sa4->sin_port == ntohs(89));
struct in_addr addr;
inet_aton("127.0.0.1", &addr);
assert(sa4->sin_addr.s_addr == addr.s_addr);
freeaddrinfo(servinfo);

#ifdef __EMSCRIPTEN__
// test gai_strerror
CHECK_ERR(0, "Unknown error");
Expand Down
Loading