ARES_GETADDRINFO(3)
π ARES_GETADDRINFO β a.k.a. the "bestie, who is this host and what service" function π
okay so we're in the ares_getaddrinfo(3) manual page, Library Functions Manual edition, and honestly? this one's a whole vibe. sit down, grab a strawberry, let's get into it. πβ¨
β¨ NAME β¨
ares_getaddrinfo β kick off a host query by name and service. that's it, that's the job. π
π¦ SYNOPSIS π¦
#include <ares.h>
typedef void (*ares_addrinfo_callback)(void *arg, int status,
int timeouts,
struct ares_addrinfo *result)
void ares_getaddrinfo(ares_channel_t *channel, const char *name,
const char* service,
const struct ares_addrinfo_hints *hints,
ares_addrinfo_callback callback, void *arg)
π DESCRIPTION π
so ares_getaddrinfo(3) starts a host query by name on the name service channel that channel points at. the name and service params are the hostname and the service, both as NULL-terminated C strings.
here's the tea tho: either name or service may be NULL, but not both. literally not both, bestie. if name is NULL, the addresses you get back are synthesized from service: the wildcard address (0.0.0.0 or ::) comes back when ARES_AI_PASSIVE is set in ai_flags, otherwise you get the loopback address (127.0.0.1 or ::1).
the hints parameter is a ares_addrinfo_hints structure, and she's built like this:
struct ares_addrinfo_hints {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
};
let's go field by field, no skipping, i don't play that π«Ά
ai_familyβ says which address family you want.AF_UNSPECmeans give me bothAF_INETandAF_INET6. indecisive queen behavior, love that for us.ai_socktypeβ which socket type you want, likeSOCK_STREAMorSOCK_DGRAM. setting it to0means any type, we're flexible.ai_protocolβ setting this to0means any protocol. again: flexible.ai_flagsβ extra options, and they're all listed right down below. scroll, babe.
πΈ the flags, one by one, no crumbs left πΈ
ARES_AI_NUMERICSERVβ if this is set, theservicefield gets treated as a numeric value.ARES_AI_PASSIVEβ if this is set andnameisNULL, the returned addresses will use the wildcard address (0.0.0.0or::), which is the move forbind(2)ing a socket that's gonna accept connections. if it's not set andnameisNULL, you get the loopback address (127.0.0.1or::1) instead.ARES_AI_CANONNAMEβ theares_addrinfostructure will return a canonical names list. cute, we love a glow-up name.ARES_AI_NOSORTβ result addresses will not be sorted, and no connections to resolved addresses will be attempted. chaotic? maybe. valid? absolutely.ARES_AI_ENVHOSTSβ read the hosts file path from the environment variableCARES_HOSTS.
when the query is complete or has failed, the ares library invokes callback. and heads up: completion or failure 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. π
when the associated callback is called, it is called with a channel lock, so care must be taken to make sure any processing is minimal, to prevent DNS channel stalls. keep it short in there, bestie, we're holding the lock. π
also: the callback may be triggered from a different thread than the one which called ares_getaddrinfo(3). yes, different thread. not the same one. we don't make the rules. π
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. like, immediately immediately. no "one sec". β°
π the callback arguments, decoded π
the callback argument arg is copied from the ares_getaddrinfo(3) argument arg. matching set, as it should be. β¨
the callback argument status tells you whether the query succeeded and, if it didn't, how it went wrong. 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 find addresses of that type family.ARES_ENOTFOUNDβ the name was not found. she's not here, girl. πARES_ENOMEMβ memory was exhausted.ARES_ESERVICEβ the textual service name provided could not be dereferenced into a port.ARES_ECANCELLEDβ the query was cancelled.ARES_EDESTRUCTIONβ the name service channelchannelis being destroyed; the query will not be completed. rip. π
π on success: the result π
on successful completion of the query, the callback argument result points to a struct ares_addrinfo, and she contains two linked lists β one with resolved addresses, and another with canonical names. also included is the official name of the host (analogous to gethostbyname() h_name). main character energy fr fr.
struct ares_addrinfo {
struct ares_addrinfo_cname *cnames;
struct ares_addrinfo_node *nodes;
char *name;
};
the ares_addrinfo_node structure is similar to RFC3493 addrinfo, but without canonname and with an extra ttl field. so like, a glow-up, but she's doing her own thing:
struct ares_addrinfo_node {
int ai_ttl;
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
ares_socklen_t ai_addrlen;
struct sockaddr *ai_addr;
struct ares_addrinfo_node *ai_next;
};
the ares_addrinfo_cname structure is a linked list of CNAME records, where ttl is a time to live, alias is a label of the resource record, and name is a value (canonical name) of the resource record. see RFC2181 10.1.1. CNAME terminology. very much iykyk.
struct ares_addrinfo_cname {
int ttl;
char *alias;
char *name;
struct ares_addrinfo_cname *next;
};
the reserved memory has to be deleted by ares_freeaddrinfo(3). do NOT forget this one, bestie. clean up after yourself, it's giving responsible. π§
β the sorting rules β
the result is sorted according to RFC6724 except:
- Rule 3 (Avoid deprecated addresses)
- Rule 4 (Prefer home addresses)
- Rule 7 (Prefer native transport)
please note that the function will attempt a connection on each of the resolved addresses as per RFC6724. she's checking in on every single one of them. thorough queen.
πΈ AVAILABILITY πΈ
this function was added in c-ares 1.16.0, released in March 2020. vintage, but not that vintage. π
π SEE ALSO π
and that's the whole page, bestie!! this one shipped from the Library Functions Manual and was dated 4 November 2018 β yeah, 2018, we been knew. mwah, go forth and resolve responsibly. πβ¨ππ¦
π¦ where this page comes from
- alpine 3.22: c-ares-doc 1.34.9-r0 Β· MIT
- alpine 3.23: c-ares-doc 1.34.9-r0 Β· MIT
- alpine 3.24: c-ares-doc 1.34.9-r0 Β· MIT
- arch rolling: c-ares 1.34.9-1 Β· MIT
- centos-stream 10: c-ares-devel 1.34.9-1.el10 Β· MIT
- fedora 43: c-ares-devel 1.34.8-1.fc43 Β· MIT
- fedora 43: c-ares-devel 1.34.8-1.fc43 Β· MIT
- fedora 44: c-ares-devel 1.34.8-1.fc44 Β· MIT
- fedora 44: c-ares-devel 1.34.8-1.fc44 Β· MIT
- nixos 26.05: c-ares 1.34.9
- nixpkgs unstable: c-ares 1.34.8
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- opensuse-leap 16.0: c-ares-devel 1.34.8-160000.1.1 Β· MIT
- ubuntu stonking: libc-ares-dev 1.34.8-1 Β· MIT
- π the page's license: MIT
- Copyright 1998 by the Massachusetts Institute of Technology
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms