[svlug] gethostbyname()

Seth David Schoen schoen at loyalty.org
Sat Jan 26 21:18:02 PST 2002


Erik Fears writes:

> Anyway one of the clients for the database checks remote hosts connecting
> to a server to determine whether or not they use an open proxy. One of the 
> checks uses the unix gethostbyname() function to resolve the host against
> a DNS blacklist. 
> 
> The problem I found is that gethostbyname() blocks ( I have no idea what the
> timeout is ). I'm wondering if any linux C programmers (or UNIX programmers)
> on this list know a way to resolve a host in a way that will not block 
> (without using a seperate thread, or writing the DNS client from scratch).

I know there are many standard solutions for this, but I don't happen
to remember any of them offhand.

A generic thing -- short of writing your own resolver -- is to use
alarm(2) and SIGALRM.

It's a substitute for forking or threads in some cases; the kernel can
interrupt you after a designated amount of time.  There's some
discussion of it in Stevens, _Advanced Programming in the Unix
Environment_, among other places.  You call alarm and tell the kernel
when you want to be interrupted; after that among of time, you get
interrupted by a signal.

If you can use another language, you may be able to get someone else's
resolver library with built-in timeouts.  I recall that there's one of
those in Perl, for example.

-- 
Seth David Schoen <schoen at loyalty.org> | Reading is a right, not a feature!
     http://www.loyalty.org/~schoen/   |                 -- Kathryn Myronuk
     http://vitanuova.loyalty.org/     |




More information about the svlug mailing list