bobbybee
bobbybee

Reputation: 1788

In Linux, what program is responsible for DNS lookups?

As a way to test out some of my ideas for DNS stuff, I wrote a very simple server that basically emulates a DNS-like behavior by basically responding to key-value pairs.

I could easily write a client to this server in C that would perhaps take in a hostname as a parameter and print out an IP (or something of the sort)

My real question is in a linux scenario, what piece of code is ultimately responsible for lookups. How does this program operate? Would it be trivial to overwrite this program with a custom "toy" client (in a VM, of course)?

Of course, this is all theoretical, I have no plans of using this outside of a virtual machine, and more so, I don't want to implement the default DNS protocol (so simply setting the DNS server setting to localhost or 127.0.0.1 wouldn't be of use to me)

Some sample code on the kind of behavior I'd like to implement (psuedo)

int main(int argc, char** argv){
    if(strcmp(argv[0], "localhost") == 0){
           printf("127.0.0.1");
    }
    return 0;
}

Upvotes: 0

Views: 723

Answers (1)

Celada
Celada

Reputation: 22261

The thing that is responsible for performing name lookups is not a program as such, it's a library: the name service switch, which is a component of libc.

The name service switch supports pluggable backend modules, so if you don't want to use DNS, you can write your own name service switch (NSS) backend. Under Linux and glibc, NSS backend modules are named something like /lib/nss_<name>.so.2. You configure the NSS to tell it what backend you want to use by editing /etc/nsswitch.conf. Typically two backend are used for hosts: files (looks up names in /etc/hosts) and dns (looks up names in DNS).

There is some quite sparse documentation on how to write an NSS module in glibc's manual but it is probably possible to find a better tutorial.

Upvotes: 2

Related Questions