From: Marco Wang on
Hello,

I am working on writing code for a networking device. The device is
linux based. Now I encounter a problem:

1. The device is powered on and application started, but network cable
is unplugged;
2. Application on device calls gethostbyname() or something like that
to get IP address;
3. As network cable is unplugged, the device failed to acquire DHCP IP
address (including DNS info);
4. So gethostbyname() failed to resolve remote host name;
5. Plug network cable into device, the device will get IP/DNS info
sooner or later;
6. But gethostbyname() still won't work unless the process is
restarted;

I know gethostbyname() retrieves host/network info only once when it
is invoked the first time, so I use res_init() to solve this issue,
then gethostbyname() will return correct information, but it takes too
long: around 30 seconds. Anyone has any idea why such 30 seconds
occurs? TIA.

Thanks,
Marco Wang

From: Rick Jones on
It isn't really an answer to your question, but new code really should
be using getaddrinfo() rather than gethostbyname().

As for the rest of it, perhaps some system call tracing would help -
run your application under strace and look at the system calls being
made - that may help tell you want is going-on/wrong.

If I were to guess, it would be that gethostbyname() and the resolver
libraries were caching bogus DNS server information - if DHCP didn't
provide an IP address to the NIC, it likely didn't provide DNS server
information to go into /etc/resolv.conf either.

rick jones
--
a wide gulf separates "what if" from "if only"
these opinions are mine, all mine; HP might not want them anyway... :)
feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...