getaddrinfo_a (3) - Linux Manuals
getaddrinfo_a: asynchronous
NAME
getaddrinfo_a, gai_suspend, gai_error, gai_cancel - asynchronous network address and service translation
SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <netdb.h> int getaddrinfo_a(int mode, struct gaicb *list[], int nitems, struct sigevent *sevp); int gai_suspend(const struct gaicb * const list[], int nitems, const struct timespec *timeout); int gai_error(struct gaicb *req); int gai_cancel(struct gaicb *req); Link with -lanl.
DESCRIPTION
The getaddrinfo_a() function performs the same task as getaddrinfo(3), but allows multiple name look-ups to be performed asynchronously, with optional notification on completion of look-up operations.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.
- GAI_NOWAIT
- Perform the look-ups asynchronously. The call returns immediately, and the requests are resolved in the background. See the discussion of the sevp argument below.
The array list specifies the look-up requests to process. The nitems 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 {
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(7).
The
sevp->sigev_notify
field can have the following values:
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
nitems
argument specifies the size of the array
list.
The call blocks until one of the following 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.
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.
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:
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:
The
gai_strerror(3)
function translates these error codes to a human readable string,
suitable for error reporting.
$ ./a.out ftp.us.kernel.org enoent.linuxfoundation.org gnu.cz
ftp.us.kernel.org: 128.30.2.36
enoent.linuxfoundation.org: Name or service not known
gnu.cz: 87.236.197.13
Here is the program source code
#define _GNU_SOURCE
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
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:
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
Interface Attribute Value
getaddrinfo_a(),
gai_suspend(),
gai_error(),
gai_cancel()
Thread safety MT-Safe CONFORMING TO
These functions are GNU extensions;
they first appeared in glibc in version 2.2.3.
NOTES
The interface of
getaddrinfo_a()
was modeled after the
lio_listio(3)
interface.
EXAMPLES
Two examples are provided: a simple example that resolves
several requests in parallel synchronously, and a complex example
showing some of the asynchronous capabilities.
Synchronous example
The program below simply resolves several hostnames in parallel,
giving a speed-up compared to resolving the hostnames sequentially using
getaddrinfo(3).
The program might be used like this: