Reputation: 1579
I'm currently implementing a tiny and fast dns-resolver in C. Normally, a dns-resolver would ask the local cache and then the cache of its router and so on, before it sends it's request to a root-nameserver, but in my case, I just want to resolve names which are probably not saved locally. So whats the fastest way to resolve a hostname?
Firing the packets to my local nameserver (in my case my router), or sending the directly to a root nameserver? Does it matter which root-nameserver I am going to choose? Shall I choose them randomly (from the list of the 13 root-dns-servers)?
Upvotes: 1
Views: 541
Reputation: 22252
Resolving names is a tricky business. It looks easy from the outset, but it really is rife with some things that make it "not so easy". Thus, the best thing to do would actually be to use an existing implementation rather than roll your own, because there are lots out there to choose from and most of them are pretty good. If your local nameserver (router) has a resolver in it, just use it because it'll take care of the complexity (but it will have a cache itself).
If you're intent on resolving yourself, you'll need to do a lot more than fire the request at a root name server. You'll need to fire the request to one at random, deal with what happens if you don't get a response (try another), and then when you get the answer back it won't be the answer you're looking for. If you send it a request for "www.example.com" it's simply going to list all the name servers for "com" (in NS records) and tell you to go ask them next. Then you'll need to go ask com, who will tell you to go ask the name servers for example.com and tell you to go ask them. Meanwhile, the NS records for example.com may in fact point you to name servers somewhere else, such as "dns1.example.net" which means you'll need to go start at the root again to see how to get to example.net's name servers.
Anyway, the above is actually a simplification, believe it or not. So, yes: you should ask the local router or your ISP's resolvers rather than do it yourself. If you do want to do it locally to your application, pick one of the main C implementations for doing it for you rather than rewriting. Unless you want to learn a lot (which is always a good thing).
Upvotes: 1