Skip to content

Looking up ip for localhost should return 127.0.0.1 #24593

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

Open
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions src/lib/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ addToLibrary({
$DNS: {
address_map: {
id: 1,
addrs: {},
names: {}
addrs: { localhost: "127.0.0.1" },
names: { "127.0.0.1": "localhost" },
},

lookup_name(name) {
Expand Down
16 changes: 12 additions & 4 deletions test/sockets/test_gethostbyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include <emscripten.h>
#endif

int main() {

void do_ip_check(char* name) {
char str[INET_ADDRSTRLEN];
struct hostent *host = NULL;
struct hostent hostData;
Expand All @@ -30,7 +31,7 @@ int main() {
// gethostbyname_r calls the same stuff as gethostbyname, so we'll test the
// more complicated one.
// resolve the hostname to an actual address
gethostbyname_r("slashdot.org", &hostData, buffer, sizeof(buffer), &host, &err);
gethostbyname_r(name, &hostData, buffer, sizeof(buffer), &host, &err);
assert(host->h_addrtype == AF_INET);
assert(host->h_length == sizeof(uint32_t));

Expand All @@ -48,10 +49,17 @@ int main() {
// do a reverse lookup on the ip address
struct hostent *host1 = gethostbyaddr(&addr, sizeof(addr), host->h_addrtype);
printf("gethostbyaddr -> %s\n", host1->h_name);
assert(strstr(host1->h_name, "slashdot.org"));
assert(strstr(host1->h_name, name));
}

int main() {
printf("slashdot.org\n");
do_ip_check("slashdot.org");

printf("localhost\n");
do_ip_check("localhost");

puts("success");

return EXIT_SUCCESS;
}

4 changes: 4 additions & 0 deletions test/sockets/test_gethostbyname.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
slashdot.org
gethostbyname -> 172.29.1.0
gethostbyaddr -> slashdot.org
localhost
gethostbyname -> 127.0.0.1
gethostbyaddr -> localhost
success