getaddrinfo_a(3)
π getaddrinfo_a(3) β async network address & service translation π
okay bestie, this page is from the Library Functions Manual and it's giving asynchronous name lookup realness. no cap, it's a whole vibe when you don't wanna block your thread.
πΈ NAME πΈ
getaddrinfo_a, gai_suspend, gai_error, gai_cancel β asynchronous network address and service translation π¦
β¨ LIBRARY β¨
Asynchronous name lookup library (libanl, -lanl) π
π SYNOPSIS π
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <netdb.h>
int getaddrinfo_a(int mode, struct gaicb *list[restrict n],
int n, struct sigevent *restrict sevp);
int gai_suspend(const struct gaicb *const list[n], int n,
const struct timespec *timeout);
int gai_error(struct gaicb *req);
int gai_cancel(struct gaicb *req);
π DESCRIPTION π
getaddrinfo_a() does the same task as getaddrinfo(3), bestie, but it lets multiple name look-ups be performed asynchronously, with optional notification on completion of look-up operations. It's giving multitasking queen energy. π
The mode argument has one of the following values:
GAI_WAITβ Perform the look-ups synchronously. The call blocks until the look-ups have completed. yes, it's gonna sit there and wait, no cap. π΄GAI_NOWAITβ Perform the look-ups asynchronously. The call returns immediately, and the requests are resolved in the background. See the discussion of thesevpargument below. bye bestie, we'll ping you. π¦
The array list specifies the look-up requests to process. The n argument specifies the number of elements in list. The requested look-up operations are started in parallel. NULL elements in list are ignored. Each request is described by a gaicb structure, defined as follows:
struct gaicb {
const char *ar_name;
const char *ar_service;
const struct addrinfo *ar_request;
struct addrinfo *ar_result;
};
The elements of this structure correspond to the arguments of getaddrinfo(3). Thus, ar_name corresponds to the node argument and ar_service to the service argument, identifying an Internet host and a service. The ar_request element corresponds to the hints argument, specifying the criteria for selecting the returned socket address structures. Finally, ar_result corresponds to the res argument; you do not need to initialize this element, it will be automatically set when the request is resolved. The addrinfo structure referenced by the last two elements is described in getaddrinfo(3).
When mode is specified as GAI_NOWAIT, notifications about resolved requests can be obtained by employing the sigevent structure pointed to by the sevp argument. For the definition and general details of this structure, see sigevent(3type). The sevp->sigev_notify field can have the following values:
SIGEV_NONEβ Don't provide any notification. silent treatment, but make it intentional. π€«SIGEV_SIGNALβ When a look-up completes, generate the signalsigev_signofor the process. Seesigevent(3type)for general details. Thesi_codefield of thesiginfo_tstructure will be set toSI_ASYNCNL. π£SIGEV_THREADβ When a look-up completes, invokesigev_notify_functionas if it were the start function of a new thread. Seesigevent(3type)for details. π§΅
For SIGEV_SIGNAL and SIGEV_THREAD, it may be useful to point sevp->sigev_value.sival_ptr to list.
The gai_suspend() function suspends execution of the calling thread, waiting for the completion of one or more requests in the array list. The n argument specifies the size of the array list. The call blocks until one of the following occurs:
- One or more of the operations in
listcompletes. β - The call is interrupted by a signal that is caught. π¨
- The time interval specified in
timeoutelapses. This argument specifies a timeout in seconds plus nanoseconds (seenanosleep(2)for details of thetimespecstructure). IftimeoutisNULL, then the call blocks indefinitely (until one of the events above occurs). β³
No explicit indication of which request was completed is given; you must determine which request(s) have completed by iterating with gai_error() over the list of requests. detective mode, bestie. π
The gai_error() function returns the status of the request req: either EAI_INPROGRESS if the request was not completed yet, 0 if it was handled successfully, or an error code if the request could not be resolved. π
The gai_cancel() function cancels the request req. If the request has been canceled successfully, the error status of the request will be set to EAI_CANCELED and normal asynchronous notification will be performed. The request cannot be canceled if it is currently being processed; in that case, it will be handled as if gai_cancel() has never been called. If req is NULL, an attempt is made to cancel all outstanding requests that the process has made. π
π₯ RETURN VALUE π₯
The getaddrinfo_a() function returns 0 if all of the requests have been enqueued successfully, or one of the following nonzero error codes:
EAI_AGAINβ The resources necessary to enqueue the look-up requests were not available. The application may check the error status of each request to determine which ones failed. πEAI_MEMORYβ Out of memory. πEAI_SYSTEMβmodeis invalid. π
The gai_suspend() function returns 0 if at least one of the listed requests has been completed. Otherwise, it returns one of the following nonzero error codes:
EAI_AGAINβ The giventimeoutexpired before any of the requests could be completed. β°EAI_ALLDONEβ There were no actual requests given to the function. π«EAI_INTRβ A signal has interrupted the function. Note that this interruption might have been caused by signal notification of some completed look-up request. π¬
The gai_error() function can return EAI_INPROGRESS for an unfinished look-up request, 0 for a successfully completed look-up (as described above), one of the error codes that could be returned by getaddrinfo(3), or the error code EAI_CANCELED if the request has been canceled explicitly before it could be finished.
The gai_cancel() function can return one of these values:
EAI_CANCELEDβ The request has been canceled successfully. πEAI_NOTCANCELEDβ The request has not been canceled. π«EAI_ALLDONEβ The request has already completed. β
The gai_strerror(3) function translates these error codes to a human readable string, suitable for error reporting. π£οΈ
𧬠ATTRIBUTES π§¬
For an explanation of the terms used in this section, see attributes(7).
getaddrinfo_a(),gai_suspend(),gai_error(),gai_cancel()β Thread safety:MT-Safeπ
π STANDARDS π
GNU. π
π°οΈ HISTORY π°οΈ
glibc 2.2.3. π
The interface of getaddrinfo_a() was modeled after the lio_listio(3) interface. π‘
π EXAMPLES π
okay bestie, buckle up π the page gives you two examples: one simple lil guy that resolves several requests in parallel synchronously, and one complex one that flexes some of the asynchronous capabilities. both are iconic in their own way, fr fr.
π§Έ Synchronous example
so this program below just resolves several hostnames in parallel, which gives a speed-up compared to resolving the hostnames sequentially using getaddrinfo(3). you'd use it like this:
$ ./a.out mirrors.kernel.org enoent.linuxfoundation.org gnu.org;
mirrors.kernel.org: 139.178.88.99
enoent.linuxfoundation.org: Name or service not known
gnu.org: 209.51.188.116
here's the program source code, no edits, no cap π«Ά
#define _GNU_SOURCE
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MALLOC(n, type) ((type *) reallocarray(NULL, n, sizeof(type)))
int
main(int argc, char *argv[])
{
int ret;
struct gaicb *reqs[argc - 1];
char host[NI_MAXHOST];
struct addrinfo *res;
if (argc < 2) {
fprintf(stderr, "Usage: %s HOST...\n", argv[0]);
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < argc - 1; i++) {
reqs[i] = MALLOC(1, struct gaicb);
if (reqs[i] == NULL)
err(EXIT_FAILURE, "malloc");
memset(reqs[i], 0, sizeof(*reqs[0]));
reqs[i]->ar_name = argv[i + 1];
}
ret = getaddrinfo_a(GAI_WAIT, reqs, argc - 1, NULL);
if (ret != 0) {
fprintf(stderr, "getaddrinfo_a() failed: %s\n",
gai_strerror(ret));
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < argc - 1; i++) {
printf("%s: ", reqs[i]->ar_name);
ret = gai_error(reqs[i]);
if (ret == 0) {
res = reqs[i]->ar_result;
ret = getnameinfo(res->ai_addr, res->ai_addrlen,
host, sizeof(host),
NULL, 0, NI_NUMERICHOST);
if (ret != 0) {
fprintf(stderr, "getnameinfo() failed: %s\n",
gai_strerror(ret));
exit(EXIT_FAILURE);
}
puts(host);
} else {
puts(gai_strerror(ret));
}
}
exit(EXIT_SUCCESS);
}
β¨ Asynchronous example
this next one is a simple interactive getaddrinfo_a() front-end π
heads up tho: the notification facility is not demonstrated here.
an example session might look like this:
$ ./a.out;
> a mirrors.kernel.org enoent.linuxfoundation.org gnu.org
> c 2
[2] gnu.org: Request not canceled
> w 0 1
[00] mirrors.kernel.org: Finished
> l
[00] mirrors.kernel.org: 139.178.88.99
[01] enoent.linuxfoundation.org: Processing request in progress
[02] gnu.org: 209.51.188.116
> l
[00] mirrors.kernel.org: 139.178.88.99
[01] enoent.linuxfoundation.org: Name or service not known
[02] gnu.org: 209.51.188.116
and the program source is as follows, bestie:
#define _GNU_SOURCE
#include <assert.h>
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CALLOC(n, type) ((type *) calloc(n, sizeof(type)))
#define REALLOCF(ptr, n, type) \
({ \
static_assert(__builtin_types_compatible_p(typeof(ptr), type *)); \
\
(type *) reallocarrayf(ptr, n, sizeof(type)); \
})
static struct gaicb **reqs = NULL;
static size_t nreqs = 0;
static inline void *
reallocarrayf(void *p, size_t n, size_t size)
{
void *q;
q = reallocarray(p, n, size);
if (q == NULL && n != 0 && size != 0)
free(p);
return q;
}
static char *
getcmd(void)
{
static char buf[256];
fputs("> ", stdout);
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) == NULL)
return NULL;
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
return buf;
}
/* Add requests for specified hostnames. */
static void
add_requests(void)
{
size_t nreqs_base = nreqs;
char *host;
int ret;
while ((host = strtok(NULL, " "))) {
nreqs++;
reqs = REALLOCF(reqs, nreqs, struct gaicb *);
if (reqs == NULL)
err(EXIT_FAILURE, "reallocf");
reqs[nreqs - 1] = CALLOC(1, struct gaicb);
if (reqs[nreqs - 1] == NULL)
err(EXIT_FAILURE, "calloc");
reqs[nreqs - 1]->ar_name = strdup(host);
}
/* Queue nreqs_base..nreqs requests. */
ret = getaddrinfo_a(GAI_NOWAIT, &reqs[nreqs_base],
nreqs - nreqs_base, NULL);
if (ret) {
fprintf(stderr, "getaddrinfo_a() failed: %s\n",
gai_strerror(ret));
exit(EXIT_FAILURE);
}
}
/* Wait until at least one of specified requests completes. */
static void
wait_requests(void)
{
char *id;
int ret;
size_t n;
struct gaicb const **wait_reqs;
wait_reqs = CALLOC(nreqs, const struct gaicb *);
if (wait_reqs == NULL)
err(EXIT_FAILURE, "calloc");
/* NULL elements are ignored by gai_suspend(). */
while ((id = strtok(NULL, " ")) != NULL) {
n = atoi(id);
if (n >= nreqs) {
printf("Bad request number: %s\n", id);
return;
}
wait_reqs[n] = reqs[n];
}
ret = gai_suspend(wait_reqs, nreqs, NULL);
if (ret) {
printf("gai_suspend(): %s\n", gai_strerror(ret));
return;
}
for (size_t i = 0; i < nreqs; i++) {
if (wait_reqs[i] == NULL)
continue;
ret = gai_error(reqs[i]);
if (ret == EAI_INPROGRESS)
continue;
printf("[%02zu] %s: %s\n", i, reqs[i]->ar_name,
ret == 0 ? "Finished" : gai_strerror(ret));
}
}
/* Cancel specified requests. */
static void
cancel_requests(void)
{
char *id;
int ret;
size_t n;
while ((id = strtok(NULL, " ")) != NULL) {
n = atoi(id);
if (n >= nreqs) {
printf("Bad request number: %s\n", id);
return;
}
ret = gai_cancel(reqs[n]);
printf("[%s] %s: %s\n", id, reqs[atoi(id)]->ar_name,
gai_strerror(ret));
}
}
/* List all requests. */
static void
list_requests(void)
{
int ret;
char host[NI_MAXHOST];
struct addrinfo *res;
for (size_t i = 0; i < nreqs; i++) {
printf("[%02zu] %s: ", i, reqs[i]->ar_name);
ret = gai_error(reqs[i]);
if (!ret) {
res = reqs[i]->ar_result;
ret = getnameinfo(res->ai_addr, res->ai_addrlen,
host, sizeof(host),
NULL, 0, NI_NUMERICHOST);
if (ret) {
fprintf(stderr, "getnameinfo() failed: %s\n",
gai_strerror(ret));
exit(EXIT_FAILURE);
}
puts(host);
} else {
puts(gai_strerror(ret));
}
}
}
int
main(void)
{
char *cmdline;
char *cmd;
while ((cmdline = getcmd()) != NULL) {
cmd = strtok(cmdline, " ");
if (cmd == NULL) {
list_requests();
} else {
switch (cmd[0]) {
case 'a':
add_requests();
break;
case 'w':
wait_requests();
break;
case 'c':
cancel_requests();
break;
case 'l':
list_requests();
break;
default:
fprintf(stderr, "Bad command: %c\n", cmd[0]);
break;
}
}
}
exit(EXIT_SUCCESS);
}
π SEE ALSO π
and that's the whole page bestie, we ate π β¨ this one came straight from Linux man-pages 6.20, dated 2026-02-08 β go forth and resolve all the things π¦π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2009, Petr Baudis <pasky@ucw.cz>
- Copyright 2010, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms