in reply to Timout a gethostbyaddr?

On most Unix systems the resolver timeout is configurable in /etc/resolv.conf. For instance, on Linux, man resolv.conf says:
options
  Options allows certain internal resolver variables to
  be  modified. The syntax is

           options option ...

  where option is one of the following:

  ...

    timeout:n
      sets  the amount of time the resolver will wait for a
      response from a remote name server before retrying the
      query via a different name server.  Measured in seconds,
      the default is RES_TIMEOUT (currently 5, see <resolv.h>).
The drawback is that it is a system wide setting that (AFAIK) can not be changed for specific applications.

update: hey!, actually it can be changed in a per process manner...

  The  options keyword of a system's resolv.conf file
  can be amended on a per-process basis by setting the
  environment  variable "RES_OPTIONS" to  a space-separated
  list of resolver options as explained above under
  options.
So, probably doing...
$ENV{RES_OPTIONS} = "timeout:$timeout";
before calling gethostbyname will do what you want!

Replies are listed 'Best First'.
Re^2: Timout a gethostbyaddr?
by almut (Canon) on Mar 23, 2007 at 14:14 UTC
    $ENV{RES_OPTIONS} = "timeout:$timeout";

    It might be worth noting that what you specify here will probably not be the overall timeout, because in a typical scenario the resolver is configured to query multiple servers, with multiple retries per server. The timeout applies to every single query performed. You should be able to control the number of retries by saying something like $ENV{RES_OPTIONS} = "timeout:2 attempts:1";.

    However, when I was just playing with this (on Linux, btw), it didn't quite behave as expected: the attempts setting did not influence the number of retries (as stated in the manpage), but apparently limited the number of nameservers queried. There were still 4 retries/queries to the same nameserver... (confirmed by strace-ing a sample (failing) lookup).