ARES_QUERY(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_QUERY β€” bestie, it's giving DNS πŸ’–

okay so this is ares_query(3), a libc-ares page from the Library Functions Manual, and it's literally about initiating a single-question DNS query. one question. one vibe. let's go πŸ¦‹


✨ SYNOPSIS ✨

#include <ares.h>

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

ares_status_t ares_query_dnsrec(ares_channel_t      *channel,
                                const char          *name,
                                ares_dns_class_t     dnsclass,
                                ares_dns_rec_type_t  type,
                                ares_callback_dnsrec callback,
                                void                *arg,
                                unsigned short      *qid);

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

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

two whole functions, two whole callbacks, one iconic mission πŸ’…


🌸 DESCRIPTION 🌸

so ares_query_dnsrec(3) and ares_query(3) both do the same main character thing: they initiate a single-question DNS query on the name service channel you hand them in channel. that's it. one question, no bundling, no drama πŸŽ€

the parameter name is your query name β€” a NUL-terminated C string of period-separated labels, and it may optionally end with a period. but heads up bestie: any periods and backslashes inside a label have to be escaped with a backslash. fr fr, the parser is picky and we respect that.

the parameters dnsclass and type give the class and the type of the query. periodt.

here's the tea on the two flavors though πŸ“:

  • ares_query_dnsrec(3) uses the c-ares ares_dns_class_t and ares_dns_rec_type_t defined types β€” the fancy new-school way.
  • ares_query(3) uses the values defined in <arpa/nameser.h> β€” the old-school classic.

when the query is complete or has failed, the ares library will invoke callback. and this can happen immediately (like, even before the return of the function call, no cap 😭), or it may happen during a later call to ares_process(3) or ares_destroy(3).

⚠️ thread tea: if this is called from a thread other than the one the main program event loop is running on, you need to take care to ensure any file descriptor lists are updated immediately within the eventloop. and when the associated callback is called, it is called with a channel lock β€” so keep your processing minimal, otherwise you'll stall the DNS channel and that's not the vibe πŸ™ˆ

🫢 the callback args 🫢

the callback argument arg is copied straight from the ares_query_dnsrec(3) or ares_query(3) argument arg. cute, consistent, iconic.

the callback argument status tells you whether the query succeeded and, if it didn't, how it failed. it may have any of these values, and bestie I am listing every single one because we don't skip πŸ’–:

  • ARES_SUCCESS β€” the query completed successfully. slay! ✨
  • ARES_ENODATA β€” the query completed but contains no answers. it's giving ghost town πŸ‘»
  • ARES_EFORMERR β€” the query completed but the server claims that the query was malformatted. rude of the server honestly.
  • ARES_ESERVFAIL β€” the query completed but the server claims 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_dnsrec(3) level.)
  • ARES_ENOTFOUND β€” the query completed but the queried-for domain name was not found. she's not here πŸ’€
  • ARES_ENOTIMP β€” the 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_dnsrec(3) level.)
  • ARES_EREFUSED β€” the query completed but the server refused 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_dnsrec(3) level.)
  • ARES_EBADNAME β€” the query name name could not be encoded as a domain name, either because it contained a zero-length label or because it contained a label of more than 63 characters. 63! that's the number! don't push it!
  • ARES_ETIMEOUT β€” no name servers responded within the timeout period. the silence was loud 😭
  • ARES_ECONNREFUSED β€” no name servers could be contacted.
  • ARES_ENOMEM β€” memory was exhausted. rip to the RAM πŸͺ¦
  • ARES_ECANCELLED β€” the query was cancelled.
  • ARES_EDESTRUCTION β€” the name service channel channel is being destroyed; the query will not be completed.
  • ARES_ENOSERVER β€” the query will not be completed because 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. receipts, basically 🧾

if the query completed β€” even if there was something wrong with it, as indicated by some of the above error codes β€” the callback argument dnsrec or abuf will be non-NULL, otherwise they will be NULL.

πŸ’€ big important one, do NOT skip this: 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 or free it, and must not retain a reference to it after returning β€” treat it as read-only. the parameter is intentionally not declared const solely to preserve API/ABI compatibility with existing applications. it's not const because ~legacy reasons~, not because you're allowed to touch it. hands off 🫢

🌈 AVAILABILITY 🌈

ares_query_dnsrec(3) was introduced in c-ares 1.28.0. new girl on the block, and she ate πŸ’…


πŸ‘‘ SEE ALSO πŸ‘‘


πŸŽ€ the cute sign-off πŸŽ€

this page lives in the Library Functions Manual, source/version vibes dated 24 July 1998 (yes bestie, 1998, she's a legend and she's still serving), and the page name is ARES_QUERY(3). mwah, go query something single and question-y πŸ’–βœ¨πŸ¦‹