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

On Windows, dns resolves using the secondary nameserver #42676

Open
spazmodius opened this issue Apr 10, 2022 · 3 comments
Open

On Windows, dns resolves using the secondary nameserver #42676

spazmodius opened this issue Apr 10, 2022 · 3 comments
Labels
dns Issues and PRs related to the dns subsystem. windows Issues and PRs related to the Windows platform.

Comments

@spazmodius
Copy link

spazmodius commented Apr 10, 2022

Version

v16.14.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

dns

What steps will reproduce the bug?

My Primary DNS server configures for 127.0.0.1, a local instance of SmartDns. My secondary DNS server is my ISP's.

However, Node resolves using the secondary DNS server, not the primary:

$ node -e 'dns.resolve4("www.google.com", { ttl: true }, console.log);'
null [
  { address: '142.250.138.105', ttl: 63 },
  { address: '142.250.138.106', ttl: 63 },
  { address: '142.250.138.99', ttl: 63 },
  { address: '142.250.138.147', ttl: 63 },
  { address: '142.250.138.104', ttl: 63 },
  { address: '142.250.138.103', ttl: 63 }
]

I can tell because of the short TTLs, whereas my SmartDns server is configured with a minimum TTL of 2400. Thus:

$ node -e 'dns.setServers(["127.0.0.1"]); dns.resolve4("www.google.com", { ttl: true }, console.log);'
null [ { address: '172.217.13.4', ttl: 2397 } ]

And as confirmation:

$ node -e 'console.log(dns.getServers());'
[ '12.127.16.67', '127.0.0.1' ]

How often does it reproduce? Is there a required condition?

Always sorts local addresses after remote addresses. If both Primary and Secondary are remote, then re-ordering does not happen.

What is the expected behavior?

I'd expect dns.getServers() to return [ 'primary', 'secondary' ], and for name resolution to prefer the Primary DNS server.

What do you see instead?

dns.getServers() reverse the primary and secondary DNS servers, and resolution prefers the secondary server.

Additional information

image

@VoltrexKeyva VoltrexKeyva added dns Issues and PRs related to the dns subsystem. windows Issues and PRs related to the Windows platform. labels Apr 10, 2022
@bnoordhuis
Copy link
Member

It's a feature of c-ares, the resolver library: it sorts the DNS servers according to routing weight.

/* For each interface, find any associated DNS servers as IPv4 or IPv6
* addresses. For each found address, find the best route to that DNS
* server address _on_ _that_ _interface_ (at this moment in time) and
* compute the resulting total metric, just as Windows routing will do.
* Then, sort all the addresses found by the metric.
*/

There's no way to disable it but if it really bothers you, you could comment out the qsort() call a few lines below and recompile.

@spazmodius
Copy link
Author

spazmodius commented Apr 11, 2022

I suspected the same. But I replicated c-ares system calls and logic, and did not reproduce the reversal.

On my system, those DNS servers are returned (from GetAdaptersAddresses) in correct order, on the same interface, have the same metric (0, from GetBestRoute2), and qsort does not switch their order.

@spazmodius
Copy link
Author

🤦🏼‍♂️ never mind. I am able to reproduce by calling into c-ares instead of (imperfectly, obviously) reconstructing the logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dns Issues and PRs related to the dns subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

3 participants