Subject: Re: gethostbyname & gethostbyaddr in libc thread-safe yet?
To: Alicia da Conceicao <alicia@engine.ca>
From: Christos Zoulas <christos@zoulas.com>
List: tech-net
Date: 05/25/2004 17:52:30
On May 25,  2:54pm, alicia@engine.ca (Alicia da Conceicao) wrote:
-- Subject: Re: gethostbyname & gethostbyaddr in libc thread-safe yet?

| Dear Christos:
| 
| I had previously looked into "getaddrinfo" & "getnameinfo" as alternatives
| to "gethostbyname" & "gethostbyaddr", but not only are these much more
| bloated (since I only wants to resolve domain names & IP addresses and do
| not want to connect or bind any sockets, nor do I care about IPv6 support)
| these are still not thread-safe:
| 
| ============================================================================
| GETADDRINFO(3)          NetBSD Library Functions Manual         GETADDRINFO(3)
| BUGS
|      The current implementation is not thread-safe.
| ============================================================================
| GETNAMEINFO(3)          NetBSD Library Functions Manual         GETNAMEINFO(3)
| BUGS
|      The current implementation is not thread-safe.
| ============================================================================
| 
| Do you have any suggestions on what to use for a thread-safe alternative to
| "gethostbyname" & "gethostbyaddr", so that no one thread would block or
| clobber the data of another thread when resolving.  This is for a cross-
| platform anti-spam/virus filter which I am coding up from scratch, so it
| only needs to resolve and does not use sockets, socket names, or IPv6
| support.  I personally receive over 2 million virus & spam messages a day,
| since I have my 5 root CA certs preloaded into Netscape/AOL, KDE, OpenSSL,
| etc., so having a cross-platform, thread-safe, and light-weight method to
| resolve domain names and IP addresses with minimal overhead is critical.

The manual page of getaddrinfo is out of date (it is thread-safe in
NetBSD-current). I would suggest to look into linking with the lwres_
library that comes with bind9. Other than that, you can use mutexes
to protect the current gethostbyfoo(), but that will make it really
slow. Another approach is to fork, which is worse.

christos