Code to do a direct DNS lookup

2019-01-27 01:44发布

问题:

I'm thinking of running an experiment to track DNS values in different ways (like how often they change and whatnot). To do this I will need to be able to make a DNS request directly to a server so that 1) I known what server it came from, 2) I can request responses from several servers and 3) I can avoid the local OS run cache.

Does anyone know of a library (c#, D, C, C++ in that order of preference) that will let me directly query a DNS server? Failing that, does anyone know of a easy to understand description of the DNS protocol that I could implement such a system from?

回答1:

I have experience only with C, so here is my list:

  • libresolv is the old, traditional and standard way. It is available on every Unix (type man 3 resolver) and includes routines like res_query which does more or less what you want. To query a specific name server, you typically update the global variable _res.nsaddr_list (do note that, apparently, it does not work with IPv6).

  • ldns is the modern and shiny solution. You have good documentation online.

  • a very common library, but apparently unmaintained, is adns.



回答2:

For C, I'd go with http://cr.yp.to/djbdns/blurb/library.html (the low-level parts if you need total control, i.e. dns_transmit* and friends) -- for C#, maybe http://www.c-sharpcorner.com/UploadFile/ivxivx/DNSClient12122005234612PM/DNSClient.aspx (can't test that one right now, whence the "maybe"!).



回答3:

The DNS specification is spread over many RFC (see a nice graph) and I would strongly advise not to implement a stub resolver from scratch. There are many opportunities to get it wrong. The DNS evolved a lot in the last years. If you are brave and crazy, here are the most important RFC:

  • RFC 1034, concepts
  • RFC 1035, format
  • RFC 2181, update to the specification, to fix many errors or ambiguities
  • RFC 2671, EDNS (mandatory today)
  • RFC 3597, handling the unknown resource record types
  • and many others...


回答4:

libdns (I think it's part of bind). There's a cygwin port which may be useful for windows environments.

http://rpm2html.osmirror.nl/libdns.so.21.html