From: Rainer Weikusat on
"ultr" <piotrdabrowski.ultr(a)gmail.com> writes:
> I need to add timeout to gethostbyname function in my program.

I assume that you want to do a DNS lookup. DNS is a UDP-based
protocol, which means the the library implementation will do a limited
amount of query retries to available servers (I am additionally
assuming a stub-resolver intended to consult a recursive server),
timeout on its own and return with h_errno == TRY_AGAIN. It is often
not sensible to use a shorter timeout than the default (for instance,
an interactive application should rather offer the possibility to
interrupt long running task than to just give up, effectively
dictating a user how patient he has to be).

The only portable and reliable way to implement such a timeout would
be using a killable child process to do the lookup and to kill it
after 'too much' time has expired.
From: Rick Jones on
Rainer Weikusat <rainer.weikusat(a)sncag.com> wrote:
> I assume that you want to do a DNS lookup. DNS is a UDP-based
> protocol,

It would be more accurate to say "DNS queries are generally carried in
UDP datagrams"

DNS queries _can_ be carried over TCP connections, it just doesn't
happen all that often. And other "DNS things" regularly happen over
TCP connections.

rick jones
--
firebug n, the idiot who tosses a lit cigarette out his car window
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...
From: Rainer Weikusat on
Rick Jones <rick.jones2(a)hp.com> writes:
> Rainer Weikusat <rainer.weikusat(a)sncag.com> wrote:
>> I assume that you want to do a DNS lookup. DNS is a UDP-based
>> protocol,
>
> It would be more accurate to say "DNS queries are generally carried in
> UDP datagrams"
>
> DNS queries _can_ be carried over TCP connections, it just doesn't
> happen all that often. And other "DNS things" regularly happen over
> TCP connections.

RFC1035 says:

The DNS assumes that messages will be transmitted as datagrams
or in a byte stream carried by a virtual circuit. While
virtual circuits can be used for any DNS activity, datagrams
are preferred for queries due to their lower overhead and
better performance.

[...]

UDP is not acceptable for zone transfers, but is the
recommended method for standard queries in the Internet.

IMO, calling the DNS protocol 'UDP-based' is appropriate because of
this and because simplifications tend to make things simpler to
explain (and understand).
From: David Schwartz on
On Feb 1, 10:20 am, Rainer Weikusat <rainer.weiku...(a)sncag.com> wrote:

> RFC1035 says:
>
> The DNS assumes that messages will be transmitted as datagrams
> or in a byte stream carried by a virtual circuit. While
> virtual circuits can be used for any DNS activity, datagrams
> are preferred for queries due to their lower overhead and
> better performance.

I think you may be misunderstanding what this paragraph is saying.
It's saying that this assumption is made in the design of the
protocol, that is, the protocol is designed to always correctly handle
loss of packets. It's not saying that in the real world DNS will
actually be transmitted as datagrams. It's simply giving one of the
assumptions that the protocol design proceeded with.

> UDP is not acceptable for zone transfers, but is the
> recommended method for standard queries in the Internet.

> IMO, calling the DNS protocol 'UDP-based' is appropriate because of
> this and because simplifications tend to make things simpler to
> explain (and understand).

I think it's dangerous. TCP is supposed to be equally supported and
saying things like this may mislead people into thinking they are not
requires to support queries over TCP in a server or are not allowed to
perform such queries in a client.

DS

From: Rainer Weikusat on
"David Schwartz" <davids(a)webmaster.com> writes:
> On Feb 1, 10:20 am, Rainer Weikusat <rainer.weiku...(a)sncag.com> wrote:
>> RFC1035 says:
>>
>> The DNS assumes that messages will be transmitted as datagrams
>> or in a byte stream carried by a virtual circuit. While
>> virtual circuits can be used for any DNS activity, datagrams
>> are preferred for queries due to their lower overhead and
>> better performance.
>
> I think you may be misunderstanding what this paragraph is saying.
> It's saying that this assumption is made in the design of the
> protocol, that is, the protocol is designed to always correctly handle
> loss of packets.

That what be an not entirely unreasonable guess based on the first
sentence of the cited text. But the DNS protocol does not deal with
packet loss and the actually important part is in the second sentence:

[...], datagrams are preferred for queries due [...]

> It's not saying that in the real world DNS will
> actually be transmitted as datagrams.

Actually, no. It demands the DNS queries/ responses should be
transmitted as datagrams and 'usual implementation' do this.

>> UDP is not acceptable for zone transfers, but is the
>> recommended method for standard queries in the Internet.
>
>> IMO, calling the DNS protocol 'UDP-based' is appropriate because of
>> this and because simplifications tend to make things simpler to
>> explain (and understand).
>
> I think it's dangerous. TCP is supposed to be equally supported and
> saying things like this may mislead people into thinking they are not
> requires to support queries over TCP in a server or are not allowed to
> perform such queries in a client.

It is likely that implementations of anything done without knowing the
specification behave strangely in various respects, although this has
not yet stopped anyone from implementing them or prevented those
implementations from being 'fairly successful' (busybox) or
'commercially viable' (eg what Conexant-DSL-modems call 'telnet' --
the most original idea being the use of LFCR as line terminator).

But this is not really relevant in the context of understanding the
behaviour of a 'usual' C-library stub resolver, called via one the
various defined frontends (like gethostbyname).