Subject: Re: Adding TTL information to gethostbyname() and friends
To: Ian Lance Taylor <ian@airs.com>
From: Robert Elz <kre@munnari.OZ.AU>
List: tech-net
Date: 06/03/2003 20:20:15
    Date:        1 Jun 2003 18:41:14 -0700
    From:        Ian Lance Taylor <ian@airs.com>
    Message-ID:  <20030602014114.14965.qmail@gossamer.airs.com>

  | One of the long standing drawbacks to the BSD sockets API is that
  | there is no reasonable way to get the TTL of a DNS entry.

Yes, this is a serious problem that should have been fixed a long
time ago (the history of upgrading a function intended just to
read /etc/hosts to instead fetch DNS information, invisibly to
most applications, can be seen here).

But ...

  | For gethostbyname() and similar functions, the TTL is returned in a
  | new global variable named h_ttl.

That isn't worth the bother.

Instead ...

  | For getaddrinfo(), the TTL is returned in a new field added to the
  | addrinfo struct.

is all that is needed.   Any application planning to make use of the
extra information is going to require code updates anyway.   Simply
having it switch to getaddrinfo() and drop all uses of gethostby*()
is going to be a minor change - assuming it hasn't already been done
for IPv6 support.

The only reason to keep using gethostby*() at all is for legacy systems
where getaddrinfo() doesn't exist - but those systems also can't be
assumed to have the new TTL availability - so really, any application
that finds itself ever needing to use gethostby*() cannot make use of
the extra info anyway.

  | If the TTL is not defined, as is the case when the answer comes from
  | /etc/hosts or from NIS, then the returned TTL is set to -1, with
  | appropriate macros to be used by calling code.

I'm not sure I like that part - just pick a defined value and set it
to that - 0 may be OK (/etc/hosts lookups are always going to be
pretty cheap, NIS, well, who cares?) but perhaps some other small
value would be just as good (30 seconds) ?

That avoids the need to special case code to deal with this.

kre

ps: ignore the troll.