From: ultr on
Hello,

I need to add timeout to gethostbyname function in my program. I
googled and searched archives, found some examples with alert(), but
can't make it work :/ I would be grateful of someone could tell me how
to do it.

From: William Ahern on
On Wed, 31 Jan 2007 15:15:42 -0800, ultr wrote:

> Hello,
>
> I need to add timeout to gethostbyname function in my program. I
> googled and searched archives, found some examples with alert(), but
> can't make it work :/ I would be grateful of someone could tell me how
> to do it.

You can't. Or rather, you shouldn't.

The trick with alarm(2) (not alert()), is to longjmp(3) from the signal
handler to a context created with setjmp(3) before calling gethostbyname(3).
This is, unfortunately, very common in Perl code (in Perl die() actually
uses longjmp() internally to a context set from an eval statement).

However, gethostbyname() keeps internal state, and afterward its unsafe
to call the function again. gethostbyname_r() might be relatively safer,
but you've probably also leaked a file descriptor and memory,
meaning you could only do it so many times from a single process before it
won't work anymore. (And from a strict C perspective, jumping from the
signal handler itself is questionable.)

You do have options.

1) Multiple processes. Use child processes and the
gethostbyname()+alarm() trick, and return the answer down a pipe to
the parent. The child will just kill itself if it timeouts (or
alternatively the parent will set the alarm and kill the child), since it
cannot reliably do it's job afterward.

2) Use threads in conjunction with gethostbyname_r(), or preferably
getaddrinfo(3). Still, you cannot interrupt these functions within the
thread, and so you have a few more decisions to make in terms of how you
handle timeouts.

3) Use a third-party asynchronous DNS library: ADNS, C-Ares and UDNS are
the first ones which comes to mind, the former two probably being the most
popular.
From: David Schwartz on
On Jan 31, 3:15 pm, "ultr" <piotrdabrowski.u...(a)gmail.com> wrote:

> I need to add timeout to gethostbyname function in my program. I
> googled and searched archives, found some examples with alert(), but
> can't make it work :/ I would be grateful of someone could tell me how
> to do it.

It's hard to imagine this is really what you need. Either you have
something else to do while waiting for the resolve to complete or you
don't. If you don't have anything to do, what good is a timeout before
the resolve attempt actually times out? If you do have something else
to do, why wait until the timeout to do it?

DS

From: Christopher Layne on
David Schwartz wrote:

> It's hard to imagine this is really what you need. Either you have
> something else to do while waiting for the resolve to complete or you
> don't. If you don't have anything to do, what good is a timeout before
> the resolve attempt actually times out? If you do have something else
> to do, why wait until the timeout to do it?
>
> DS

Good points. He should also be aware that depending on the TTL of records he
is requesting, after the first lookup they will remain cached by the local
resolver until the TTL decays.

If this is for a gui or interactive code, it most definitely should be using
some kind of threading or async library like C-Ares.
From: Christopher Layne on
Also, stay away from gethostbyname() and any of it's devil child variants. Use
getaddrinfo() and getnameinfo().