How chrome handle /etc/hosts while doing DNS lookups

2,347 views
Skip to first unread message

Prashant Agrawal

unread,
Dec 16, 2016, 8:20:58 AM12/16/16
to Chromium-discuss
I added an IpV6 entry to my /etc/hosts file for example 

now when I go to chrome and search for google.com. The website does not open.

But when I do
 ping google.com

 from my terminal, it returns me the public IP of google.com.
Also other browser like safari also behave the same way and hence open up google.com.

So I want to know how chrome is handling this.

Chris Bentzel

unread,
Dec 16, 2016, 8:22:00 AM12/16/16
to prashanta...@gmail.com, Chromium-discuss
Which OS

--
--
Chromium Discussion mailing list: chromium...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-discuss

PhistucK

unread,
Dec 16, 2016, 8:23:16 AM12/16/16
to prashanta...@gmail.com, Chromium-discuss
On Windows, I imagine ping by default tries the IPv4 mapping.
Use ping -6 google.com and it should go to localhost, I assume.


PhistucK

--
--
Chromium Discussion mailing list: chromium...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-discuss

---
You received this message because you are subscribed to the Google Groups "Chromium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discuss+unsubscribe@chromium.org.

Message has been deleted

Prashant Agrawal

unread,
Dec 16, 2016, 9:14:07 AM12/16/16
to Chromium-discuss
MacOS Sierra

Prashant Agrawal

unread,
Dec 16, 2016, 9:16:31 AM12/16/16
to Chromium-discuss, prashanta...@gmail.com
Yeah, that's why I was interested to know how chrome overrides this behavior


PhistucK

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-discu...@chromium.org.

Christian Biesinger

unread,
Dec 16, 2016, 9:23:36 AM12/16/16
to prashanta...@gmail.com, Chromium-discuss
It's likely just that ping uses ipv4-only host lookups and chrome doesn't...

Christian

Prashant Agrawal

unread,
Dec 16, 2016, 10:15:32 AM12/16/16
to Chromium-discuss, prashanta...@gmail.com
I tried node's DNS module and did dns.lookup('google.com') and still got the same result as that of ping. And according to node's documentation too dns.lookup is the direct mapping to OS's getaddrinfo(). So therefore chrome's behavior seems to be a bit off (or that of custom implementation of dns lookup). 

Jon Perryman

unread,
Dec 16, 2016, 3:24:01 PM12/16/16
to prashanta...@gmail.com, Chromium-discuss
Phistuck gave you the answer above. What machine do you expect ::1 to address in IPV6? Even more important is why are you overriding google.com? What do you want to accomplish?

Use "nslookup google.com" to display both the IPV4 & IPV6 addresses. Any program using the IPV4 version of the IP macro will work (just as you see). On the other hand, those programs using IPV6 version will now fail because the IPV6 address takes precedence over the IPV4 address (chrome). Chrome does not override TCP behavior.  

Jon

On Fri, Dec 16, 2016 at 7:15 AM, Prashant Agrawal <prashanta...@gmail.com> wrote:
I tried node's DNS module and did dns.lookup('google.com') and still got the same result as that of ping. And according to node's documentation too dns.lookup is the direct mapping to OS's getaddrinfo(). So therefore chrome's behavior seems to be a bit off (or that of custom implementation of dns lookup). 

On Friday, December 16, 2016 at 7:53:36 PM UTC+5:30, Christian Biesinger wrote:
It's likely just that ping uses ipv4-only host lookups and chrome doesn't...

Christian
On Dec 16, 2016 9:16 AM, "Prashant Agrawal" <prashanta...@gmail.com> wrote:
Yeah, that's why I was interested to know how chrome overrides this behavior

Prashant Agrawal

unread,
Dec 16, 2016, 3:39:47 PM12/16/16
to Chromium-discuss, prashanta...@gmail.com
Ok So I have an app on production. Now to test out  some code I added my testing staging Ipv6 entry in my etc/hosts file. Now when I open this app in chrome it points to my chrome staging, but when I use a rest client to make a request to this staging it goes to my production instance. 

After some investigations I found out that the rest Client was using node's dns.lookup which was returning Ipv4 of my production environment (despite the fact that I have an Ipv6 entry in my etc.hosts).

Also when I opened this app in other browsers like safari it was also pointing to my production environment.

So I was curious to know how chrome is doing dns lookup here.

Just to reproduce this try this out

Add this to your etc hosts
::1 nodejs.org

Now try visiting nodejs.org on chrome (it will fail because we added this entry)

Now either try opening this website in Safari or try
ping nodejs.org

It will work.

Thanks

Jon Perryman

unread,
Dec 16, 2016, 7:09:19 PM12/16/16
to prashanta...@gmail.com, Chromium-discuss
Please ask the Safari group why Safari is broken. IPV4 is old and outdated but still supported to be compatible. Safari should have used the IPV6 address because you made it available (IPV6 is preferred over IPV4). Be sure to ask them why after 10 years, they are still forcing the IPV4 address to be used. By the way, there are several methods to force the use of IPV4 (some in operating system and some in the program). 

PING uses the IPV4 as a default because it is a diagnostic tool (not an end user command). How hard would it be to ask a user if they are using IPV4 or IPV6? Diagnostic commands need to reduce confusion instead of increasing it.

Jon.  

Christian Biesinger

unread,
Dec 16, 2016, 9:37:29 PM12/16/16
to jon.pe...@gmail.com, prashanta...@gmail.com, Chromium-discuss
If you don't have a webserver listening on ::1, it is possible that
Safari is falling back to IPv4 because v6 failed (coiuld be their
version of "happy eyeballs").

Christian

Prashant Agrawal

unread,
Dec 16, 2016, 11:03:45 PM12/16/16
to Chromium-discuss, prashanta...@gmail.com
Yeah but that's also the behaviour OS is also showing (returing public Ipv4 even when there is Ipv6 avaiable)

Jon Perryman

unread,
Dec 16, 2016, 11:35:39 PM12/16/16
to Christian Biesinger, Prashant Agrawal, Chromium-discuss
I doubt that Safari does a retry on refusal. The browser received a response but was told no. Why try IPV4 when you know it will respond no again.

Jon.

Jon Perryman

unread,
Dec 18, 2016, 10:05:34 PM12/18/16
to Prashant Agrawal, Chromium-discuss
What do you mean this is OS behavior? Chrome is using the OS behavior and getting the correct IP address (IPV6).  Are you saying that Chrome should be using the IPV4 even though the IPV6 address is available? I repeat, Chrome is working correctly. If you disagree, then ask the TCP group. TCP controls address resolution rules and will be the definitive answer to this behavior.

Jon.

Prashant Agrawal

unread,
Dec 18, 2016, 10:23:40 PM12/18/16
to Chromium-discuss, prashanta...@gmail.com
I agree with you that what chrome is doing is the correct behaviour but if you can just try one thing and help me explain the output.

add
::1 nodejs.org

in your /etc/hosts file

after that 
run this code in node
const dns = require('dns');


dns
.lookup('nodejs.org' ,(err, addresses, family) => {
  console
.log('addresses:', addresses);
});

The above code will return public Ipv4 address of the given website despite the fact that I have added a local Ipv6 entry in /etc/hosts/

and in node's  dns module documentation https://nodejs.org/api/dns.html#dns_dns_lookup , they say " dns.lookup() uses the same operating system facilities as most other programs".(ping nodejs.org also gives Ipv4)

So if that is the case how chrome is getting Ipv6

Jonathan Garbee

unread,
Dec 19, 2016, 6:09:01 AM12/19/16
to prashanta...@gmail.com, Chromium-discuss

Have you cleared your DNS cache as well? If v4 is cached then it would be returned.


--

Jon Perryman

unread,
Dec 19, 2016, 3:17:44 PM12/19/16
to Prashant Agrawal, Chromium-discuss
You need to ask this question on the nodejs newsgroup but ask them why you don't get the IPV6 address.

Their documentation also says that it gets the first IPV4 or IPV6 address. They also are getting address info instead of a bind. This is similar to the nslookup command.

Add the following to your hosts file:

127.0.0.2 junkipv4
::1       junkipv6
127.0.0.2 junkip
::1       junkip
::1       junkiprev
127.0.0.2 junkiprev

When I run ping against each of these, it uses the IPV6 except for junkipv4 because it doesn't have an IPV6 address defined.

When I run nslookup against these names, it can't find the name. My guess is that it ignores the host file.

Run you program against each of these names and you will see the IPV6 is only returned when there isn't an IPV4 address defined (junkipv6).

Nodejs is not associated with chrome. It starts chromes javascript (no browser) and adds extensions such as dns. 

Jon. 

Prashant Agrawal

unread,
Dec 19, 2016, 10:26:02 PM12/19/16
to Chromium-discuss, prashanta...@gmail.com
Yeah, you got that all right, this is exactly what I found out.

Also I did filed an issue https://github.com/nodejs/node/issues/10284 . But they closed it saying that is how OS behaves. :/ (maybe you could talk some sense to them, I have got very less experience in OS)

Ok, So lets say I want to do dns lookup, is there any way I could prioritise entries in hosts file. (Just Like chrome does)

Thanks

Jon Perryman

unread,
Dec 20, 2016, 11:15:45 AM12/20/16
to Prashant Agrawal, Chromium-discuss
There is no way for you to prioritize DNS entries. The developers chose what they felt was best for each call type. This may not be a bad thing because I don't know the usage intent for lookup.

Ask yourself if you really need the IP address. Also ask yourself, why are you using lookup (DNS query) rather than getting the IP address directly from the connection you open. I suspect what you want to do and what you chose to do it (dns.lookup) are different. Ask the nodejs your design question instead of asking about your chosen solution. They may already have something that does what you need or maybe you are overthinking the problem.

Jon.
Reply all
Reply to author
Forward
0 new messages