Skip to content

Fix Can not resolve "localhost" if not Network adapter exists #4988#5019

Open
diridari wants to merge 2 commits intopocoproject:mainfrom
diridari:dns
Open

Fix Can not resolve "localhost" if not Network adapter exists #4988#5019
diridari wants to merge 2 commits intopocoproject:mainfrom
diridari:dns

Conversation

@diridari
Copy link
Copy Markdown

Fill HostEntry with localhost and a default loopback address if 'getaddrinfo' returns 'WSANO_DATA'

Comment thread Net/src/DNS.cpp Outdated
Comment thread Net/src/DNS.cpp Outdated
Comment thread Net/src/DNS.cpp
return result;
}
#if defined(_WIN32)
else if(rc == WSANO_DATA && address.isLoopback()) // no data record found and is local host
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it make sense putting this condition first, before checking the "localhost" hostname?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i would say checking the rc value is much cheaper than checking for localhost or loopback and is less common.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I apologise for not being clear enough.

In line 86 (above) it is first checked if the hostname is localhost and if that is not true then in line 145 is is checked if the device is a loopback.

From the logic point of view, what shall be the order? localhost first of loopback first? Or it does not matter?

Copy link
Copy Markdown
Author

@diridari diridari Sep 14, 2025

Choose a reason for hiding this comment

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

@matejk Those are different methods:
First is 'DNS::hostByName()' line 63-114, second is 'DNS::hostByAddress' from line 117-176

Comment thread Net/src/DNS.cpp
#if defined(POCO_OS_FAMILY_WINDOWS)
else if(rc == WSANO_DATA && hostname == "localhost") // no data record found and is local host
{
#if defined(POCO_HAVE_IPv6)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think it's a good idea to default to IPv6 solely based on it being present on the system where poco was compiled. Not even the presence of IPv6 on the runtime system would warrant it being default, given the prevalence of IPv4.

Bottom line, I would prefer this to be decided at runtime, based on an additional function argument, defaulting to IPv4.

Copy link
Copy Markdown
Author

@diridari diridari Sep 14, 2025

Choose a reason for hiding this comment

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

@aleks-f Since this is an edge case i am not sure if a addition argument wouldn't confuse users.
Does an function exists, that determines if ipv6 is available?
Otherwise i would use 127.0.0.1

@obiltschnig
Copy link
Copy Markdown
Member

I don't think Poco should do any name resolution at all outside of that provided by the operating system's network stack.

@diridari
Copy link
Copy Markdown
Author

@obiltschnig i can not respond to you comment.
I agree with your statement, but in this case Linux and Windows do behave differently.
On linux systems 127.0.0.1 and localhost can be resolved on windows an exception is thrown. See #4988

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can not resolve "localhost" if not Network adapter exists

4 participants