ARES_SEARCH(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

✨ ARES_SEARCH(3) ✨ β€” or, "how to do a DNS query with domain search, bestie" πŸ¦‹

okay so this is ARES_SEARCH(3) from the Library Functions Manual, and it's genuinely one of those pages you're gonna want to actually read because there's a lot going on. grab a snack. πŸ’–


🌸 NAME 🌸

ares_search β€” Initiate a DNS query with domain search


✨ SYNOPSIS ✨

#include <ares.h>

typedef void (*ares_callback_dnsrec)(void *arg,
                                     ares_status_t status,
                                     size_t timeouts,
                                     const ares_dns_record_t *dnsrec);

void ares_search_dnsrec(ares_channel_t *channel,
                        const ares_dns_record_t *dnsrec,
                        ares_callback_dnsrec callback, void *arg);

typedef void (*ares_callback)(void *arg, int status,
                              int timeouts, unsigned char *abuf,
                              int alen);

void ares_search(ares_channel_t *channel, const char *name,
                 int dnsclass, int type,
                 ares_callback callback, void *arg);

πŸ’… DESCRIPTION πŸ’…

so ares_search kicks off a whole series of single-question DNS queries on the name service channel that channel points at, and it uses the channel's search domains plus a host alias file that comes from the HOSTALIAS environment variable. iconic combo, fr fr.

the name parameter is the alias name, or the base of the query name, given as a NUL-terminated C string of period-separated labels. and here's the tea: if it ends with a period, the channel's search domains will not be used. periods and backslashes inside a label have to be escaped with a backslash. the dnsclass and type parameters give the class and type of the query using the values defined in <arpa/nameser.h>.

when the query sequence is done or has failed, the ares library invokes callback. and lowkey important: completion or failure of the query sequence may happen immediately, or it may happen during a later call to ares_process(3) or ares_destroy(3). so don't be surprised. πŸ™ˆ

if this is called from a thread other than the one where the main program event loop is running, care needs to be taken to make sure any file descriptor lists get updated immediately within the eventloop. and when the associated callback is called, it's called with a channel lock, so you gotta keep any processing minimal to prevent DNS channel stalls. no long tasks in there bestie, keep it snappy. πŸ’€

the callback argument arg is copied from the ares_search argument arg. the callback argument status tells you whether the query sequence ended with a successful query and, if not, how it failed. it can be any of these:

  • ARES_SUCCESS β€” A query completed successfully. slay! ✨
  • ARES_ENODATA β€” No query completed successfully; when the query was tried without a search domain appended, a response was returned with no answers.
  • ARES_EFORMERR β€” A query completed but the server claimed that the query was malformatted.
  • ARES_ESERVFAIL β€” No query completed successfully; when the query was tried without a search domain appended, the server claimed to have experienced a failure. (This code can only occur if the ARES_FLAG_NOCHECKRESP flag was specified at channel initialization time; otherwise, such responses are ignored at the ares_send(3) level.)
  • ARES_ENOTFOUND β€” No query completed successfully; when the query was tried without a search domain appended, the server reported that the queried-for domain name was not found.
  • ARES_ENOTIMP β€” A query completed but the server does not implement the operation requested by the query. (This code can only occur if the ARES_FLAG_NOCHECKRESP flag was specified at channel initialization time; otherwise, such responses are ignored at the ares_send(3) level.)
  • ARES_EREFUSED β€” A query completed but the server refused the query. (This code can only occur returned if the ARES_FLAG_NOCHECKRESP flag was specified at channel initialization time; otherwise, such responses are ignored at the ares_send(3) level.)
  • ARES_TIMEOUT β€” No name servers responded to a query within the timeout period.
  • ARES_ECONNREFUSED β€” No name servers could be contacted.
  • ARES_ENOMEM β€” Memory was exhausted. 😭
  • ARES_ECANCELLED β€” The query was cancelled.
  • ARES_EDESTRUCTION β€” The name service channel channel is being destroyed; the query will not be completed.
  • ARES_ENOSERVER β€” No query completed successfully; no DNS servers were configured on the channel.

the callback argument timeouts reports how many times a query timed out during the execution of the given request. very "how many times did we almost lose" energy. ⏳

if a query completed successfully, the callback argument abuf points to a result buffer of length alen. if the query did not complete successfully, abuf will usually be NULL and alen will usually be 0 β€” but in some cases an unsuccessful query result may still get placed in abuf. plot twist.

🚨 the abuf buffer is owned by the c-ares library and is only valid for the duration of the callback. the callback must not modify it, must not free it, and must not keep a reference to it after returning. treat it as read-only, periodt. the parameter is intentionally not declared const solely to preserve API/ABI compatibility with existing applications.

now, the ares_search_dnsrec(3) function behaves identically to ares_search(3), except it takes an initialized and filled DNS record object to use for queries as the second argument, dnsrec, instead of a name, class and type. this object is used as the base for the queries and must itself represent a valid query for a single name.

πŸ’‘ note that the search domains will only be appended to the name in the question section; RRs on the DNS record object will not be affected.

also, its callback argument is of type ares_callback_dnsrec. this callback behaves identically to ares_callback, but it's invoked with a parsed DNS record object dnsrec rather than a raw buffer with length. and heads up: this object is read-only. 🫢

ares_search_dnsrec(3) returns an ares_status_t response code, which is handy for knowing that the query was enqueued properly. key detail bestie: the response code does not reflect the result of the query β€” it only reflects the result of the enqueuing of the query. iykyk. πŸ’…


🌈 AVAILABILITY 🌈

ares_search_dnsrec(3) was introduced in c-ares 1.28.0.


πŸ‘‘ SEE ALSO πŸ‘‘

ares_process(3), ares_dns_record(3)


and that's the whole page, bestie β€” this one's from the c-ares Library Functions Manual, dated 24 July 1998. go forth and resolve responsibly. πŸŽ€πŸ¦‹βœ¨