ARES_GETNAMEINFO(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_GETNAMEINFO πŸ’–

okay bestie, gather round, because this one is literally the glow-up of address-to-name translation and it lives in the Library Functions Manual section of your man pages. no cap. πŸŽ€


✨ NAME ✨

ares_getnameinfo β€” address-to-nodename translation in protocol-independent manner πŸ¦‹


🌸 SYNOPSIS 🌸

#include <ares.h>

typedef void (*ares_nameinfo_callback)(void *arg, int status,
                                       int timeouts, char *node,
                                       char *service)

void ares_getnameinfo(ares_channel_t *channel, const struct sockaddr *sa,
                      ares_socklen_t salen, int flags,
                      ares_nameinfo_callback callback, void *arg)

πŸ’œ DESCRIPTION πŸ’œ

So ares_getnameinfo is your protocol-independent address translation girlie. she's literally a combo platter of ares_gethostbyaddr(3) and getservbyport(3) β€” the way that she does two jobs at once is giving main character energy fr fr. πŸ‘‘

she'll translate the address either by running a host query on the name service channel identified by channel, or she'll just try to resolve it locally if that's possible. lowkey efficient.

the parameters sa and len give the address as a sockaddr structure, and flags gives the options the function will use. here are the valid flags, no gatekeeping:

  • ARES_NI_NOFQDN β€” only the nodename portion of the FQDN gets returned, and only for local hosts.
  • ARES_NI_NUMERICHOST β€” you get the numeric form of the hostname instead of the name. numbers only, periodt.
  • ARES_NI_NAMEREQD β€” if the hostname can't be found in the DNS, you get an error. no name, no vibes. 😭
  • ARES_NI_NUMERICSERV β€” the numeric form of the service is returned rather than the name.
  • ARES_NI_TCP β€” the service name is to be looked up for the TCP protocol.
  • ARES_NI_UDP β€” the service name is to be looked up for the UDP protocol.
  • ARES_NI_SCTP β€” the service name is to be looked up for the SCTP protocol.
  • ARES_NI_DCCP β€” the service name is to be looked up for the DCCP protocol.
  • ARES_NI_NUMERICSCOPE β€” the numeric form of the scope ID is returned rather than the name.
  • ARES_NI_LOOKUPHOST β€” a hostname lookup is being requested. 🏠
  • ARES_NI_LOOKUPSERVICE β€” a service name lookup is being requested. πŸ›ŽοΈ

🫢 when the callback shows up 🫢

when the query is complete or has failed, the ares library will invoke callback. and heads up β€” 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 like, don't assume it's instant, ok? ✨

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 it cute and keep it quick bestie, no essay-writing in there. πŸ’…

the callback may be triggered from a different thread than the one which called ares_getnameinfo(3). plot twist. πŸ™ˆ

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. like, immediately immediately. no procrastinating.


πŸŽ€ the callback args, explained πŸŽ€

the callback argument arg is copied from the ares_getnameinfo argument arg. yes, same name, she's consistent.

the callback argument status tells you whether the query succeeded and, if not, how it failed. it may have any of these values:

  • ARES_SUCCESS β€” the host lookup completed successfully. slay! πŸŽ‰
  • ARES_ENOTIMP β€” the ares library does not know how to look up addresses of type family. not implemented, not her problem.
  • ARES_ENOTFOUND β€” the address addr was not found. πŸ’€
  • ARES_ENOMEM β€” memory was exhausted. rip.
  • ARES_ECANCELLED β€” the query was cancelled.
  • ARES_EDESTRUCTION β€” the name service channel channel is being destroyed; the query will not be completed. it's over, it's giving demolition day.
  • ARES_EBADFLAGS β€” the flags parameter contains an illegal value. 🚩

the callback argument timeouts reports how many times a query timed out during the execution of the given request. yes, it's counting. it always counts. ⏰

on successful completion of the query, the callback argument node contains a string representing the hostname (assuming ARES_NI_LOOKUPHOST was specified). additionally, service contains a string representing the service name (assuming ARES_NI_LOOKUPSERVICE was specified). if the query did not complete successfully, or one of the values was not requested, node or service will be NULL. no string, just void. πŸ•³οΈ


🚨 IMPORTANT, DON'T SKIP THIS ONE 🚨

the node and service strings are owned by the c-ares library and are only valid for the duration of the callback. the callback must not modify or free them, and must not retain references to them after returning; treat them as read-only. bestie, do NOT keep receipts. πŸ“΅

the parameters are intentionally not declared const solely to preserve API/ABI compatibility with existing applications. it's not a typo, it's a legacy thing, iykyk. 🌈


πŸ‘‘ SEE ALSO πŸ‘‘


πŸ’– sign-off πŸ’–

that's the whole page, babe β€” straight from the Library Functions Manual, dated 1 May 2009, and you ate it. go forth and translate some addresses responsibly. mwah. πŸ’…βœ¨πŸ¦‹