How web browser determines when to use IPv4 or IPv6 to connect to the destination

dnsipv4ipv6web

I saw couple of related posts on this topic:
How do client applications know to use IPv4 or IPv6? and
How to know if your browser is connected to the Ipv4 or Ipv6 address of a website?

The first post is couple years old so I am not sure if it is still relevant and the 2nd one didn't answer my question.

Reason I ask is I installed pi hole to help blocking ads. I setup my router to use pi-hole as my IPv4 DNS server, however I still see advertisements when browsing site like CNN. I then realized that my ISP provider (Comcast) provided two IP addresses, one IPv4 and one IPv6 and my router also supports IPv6.

When I manually disable IPv6 on my network card then advertisements stop showing up when I browse CNN website. Therefore I concluded that I was using IPv6 to connect to the CNN website. My question is when a web browser decides to use IPv4 or IPv6 to connect to the destination? Does is simply try to use IPv6 first and if succeeded then use it, otherwise fall back to IPv4?

A side note, apparently Arris SBG6700-AC modem/router only allow you to configure IPv4 DNS server but not IPv6 DNS server. To specify your own IPv6 DNS server, I must set that up from the computer itself.

Best Answer

First: How you resolve a hostname has nothing to do with what address you can resolve it to. DNS servers are perfectly capable of returning IPv6 AAAA records when asked via IPv4, and vice versa.

As for how a browser chooses which address to try: The first post you linked to is still relevant, the only things that changed are minor priority adjustments.

  • Most modern browsers use the Happy Eyeballs algorithm; they try all addresses at once and stick with the one which replies fastest. This is implemented entirely within the app and cannot be influenced by the OS. (Note that the algorithm is sometimes tweaked to give a head start to IPv6 or IPv4 in case they both give similar results.)

  • Older browsers and many other programs try all addresses one by one, sorted according to a default address selection algorithm, which usually prefers native IPv6 over native IPv4 over automatic v6 tunnels. This ordering is usually implemented in the OS (e.g. gai.conf), and programs don't sort addresses manually.

Related Question