0

Pouring over some old network utility code, I found a res_init() call before a getipnodebyname():

void getAddresses(string hostname, set<string> &addresses) {
    int error = 0;
    res_init();
    struct hostent *host = getipnodebyname(hostname.c_str(), AF_INET, AI_DEFAULT, &error);
    insertHostAddresses(host, addresses);
    freehostent(host);
}

Never having run into this call before, I looked up the man page:
https://linux.die.net/man/3/res_init

However this has not exactly helped me to understand when this call would be necessary to make.

I understand this is for preloading a cache? A bit of an explanation would help me out.

I should note - the current call getaddrinfo does not seem to require this?

chub500
  • 712
  • 6
  • 18
  • 1
    Looks like two different name-resolution APIs are mixed up there: http://man7.org/linux/man-pages/man3/resolver.3.html http://man7.org/linux/man-pages/man3/getipnodebyname.3.html – Joseph Sible-Reinstate Monica Apr 28 '20 at 21:04
  • Right - it got me to wondering why. I should mention, this particular code was designed to be portable between linux and freebsd. I'm mostly interested in the linux side. – chub500 Apr 28 '20 at 21:07
  • Where did you find that code? If it's some public program, can you link to it? – Joseph Sible-Reinstate Monica Apr 28 '20 at 21:08
  • Unfortunately no. The code above I wrote as a summary. I will say - the actual example differentiated between Darwin and Freebsd, in that only Freebsd called `res_init()` – chub500 Apr 28 '20 at 21:11
  • There's a good chance that the program didn't actually need to call that, and that the programmer just didn't know any better. – Joseph Sible-Reinstate Monica Apr 28 '20 at 21:12
  • To be honest, the code is a distraction from my question. Really what I'm after is a description of what linux's res_init is for and how it differs from other systems. – chub500 Apr 28 '20 at 21:13
  • 2
    From the own manpage you cite: "res_init() is normally executed by the first call to one of the other functions." So even if you do not do it explicitly, it may be called implicitly. res_init() takes care of reading /etc/resolv.conf and hence among other things to load the list of nameservers to use for all following DNS requests. You can see its source code at https://sourceware.org/git/?p=glibc.git;a=blob;f=resolv/res_libc.c;hb=6d246cb852d3c5ab721dc583112a59ac47dc374e and https://sourceware.org/git/?p=glibc.git;a=blob;f=resolv/res_init.c;hb=6d246cb852d3c5ab721dc583112a59ac47dc374e#l620 – Patrick Mevzek Apr 29 '20 at 00:09

1 Answers1

0

One must call res_init() if one intends to modify any of the internal resolver settings before using any of the other resolver(3) functions (directly or indirectly), e.g. to change the flags set in _res.options.

Greg A. Woods
  • 2,663
  • 29
  • 26