ARES_GETHOSTBYNAME(3)

πŸ“¦
version 1.34.9 Β· 5 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu stonking

✨ NAME ✨

ares_gethostbyname β€” the function that starts a host query by name, and she does it flawlessly πŸ’…

(It lives in the ares library, and this is manual page 3, so you know it's a library functions girlie.)


πŸŽ€ SYNOPSIS πŸŽ€

#include <ares.h>

typedef void (*ares_host_callback)(void *arg, int status,
                                   int timeouts,
                                   struct hostent *hostent)

void ares_gethostbyname(ares_channel_t *channel, const char *name,
                        int family, ares_host_callback callback,
                        void *arg)

πŸ’… DESCRIPTION πŸ’…

okay so ares_gethostbyname initiates a host query by name on the name service channel identified by channel. The parameter name gives the hostname as a NUL-terminated C string β€” yes bestie, NUL-terminated, don't play with me 😭 β€” and family gives the desired type of address for the resulting host entry. When the query is complete or has failed, the ares library will invoke callback.

And here's the part that catches people slipping: completion or failure of the query may happen immediately, or it may happen during a later call to ares_process(3), ares_destroy(3) or ares_cancel(3). So don't assume, okay? ✨

The callback argument arg is copied from the ares_gethostbyname argument arg. The callback argument status tells you whether the query succeeded and, if not, how it failed. It may have any of the following values:

  • ARES_SUCCESS β€” the host lookup completed successfully. she ate πŸ”₯
  • ARES_ENOTIMP β€” the ares library does not know how to find addresses of type family. genuinely not her era.
  • ARES_EBADNAME β€” the hostname name is composed entirely of numbers and periods, but it is not a valid representation of an Internet address. so close πŸ™ˆ
  • ARES_ENODATA β€” there was no data returned to extract a result from. empty. void. nothing.
  • ARES_ENOTFOUND β€” the name name was not found. ghosted fr fr πŸ’€
  • ARES_ENOMEM β€” memory was exhausted. the budget ran out.
  • ARES_ECANCELLED β€” the query was cancelled.
  • ARES_EDESTRUCTION β€” the name service channel channel is being destroyed; the query will not be completed. rip.

The callback argument timeouts reports how many times a query timed out during the execution of the given request. count 'em 🫢

On successful completion of the query, the callback argument hostent points to a struct hostent containing the name of the host returned by the query. Now listen, because this is the important part:

The callback need not and should not attempt to free the memory pointed to by hostent β€” the ares library will free it when the callback returns. The callback must not modify the struct hostent or retain a reference to it after returning; it is owned by c-ares and is only valid for the duration of the callback. blink and it's gone, bestie.

And about that const-less parameter: the parameter is intentionally not declared const solely to preserve API/ABI compatibility with existing applications. it's not a typo, it's a legacy situation. iykyk.

If the query did not complete successfully, hostent will be NULL. periodt.

Also!! When the associated callback is called, it is called with a channel lock, so care must be taken to ensure any processing is minimal to prevent DNS channel stalls. keep your work in there short and cute 🌸

The callback may be triggered from a different thread than the one which called ares_gethostbyname(3). threads. love that for us πŸ¦‹

And for integrators running their own event loops and not using ARES_OPT_EVENT_THREAD: care needs to be taken to ensure any file descriptor lists are updated immediately within the eventloop when notified. no procrastinating on the fd lists, okay? ⭐


πŸ”₯ SEE ALSO πŸ”₯


and that's the whole page, from the Library Functions Manual, dated 25 July 1998 β€” yes, iconic since 1998, ARES_GETHOSTBYNAME(3) forever πŸ‘‘ go resolve some names bestie, mwah πŸ’–πŸŽ€βœ¨