clinuxsocketsgetaddrinfogethostbyname

getaddrinfo expiration value (Linux hostname entries)


I'm writing a linux userspace application that opens a stateless socket (ICMP/UDP) to an internet host The user specifies a hostname FQDN (www.google.com) and I use getaddrinfo (or the old deprecated gethostbyname) function to resolve to an IPv4 address.

The nature of DNS is that host entries may get updated from time to time but I query it only once. Is there a way I could tell when the entry will expire ? I'm trying to avoid periodically running getaddrinfo() (querying OS DB) since on a large scale will result on unwanted system calls.

Thanks !


Solution

  • As a general rule you should use getaddrinfo with the hostname on each use (e.g. every connection) and rely on the operating system cache being efficient.

    If you are somehow worried about performance or other issues, you could of course keep your own cache in memory of your process. However, you seem to understand this could introduce problems. Caching and keeping state can introduce weird bugs.

    In those cases I would advise to at least implement an option to clear caches by sending a signal (e.g. SIGHUP) or command to the process so it knows to clear buffers and to reconnect, otherwise you will make some system administrators sad.

    But, this adds complexity to the software. This can be avoided by just calling getaddrinfo on each use. The situation you describe does not seem to warrant adding unnecessary complexity.