Getaddrinfo
Template:Lowercase title Template:Short description In C programming, the functions <syntaxhighlight lang="text" class="" style="" inline="1">getaddrinfo()</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">getnameinfo()</syntaxhighlight> convert domain names, hostnames, and IP addresses between human-readable text representations and structured binary formats for the operating system's networking API. Both functions are contained in the POSIX standard application programming interface (API).[1]
getaddrinfo and getnameinfo are inverse functions of each other. They are network protocol agnostic, and support both IPv4 and IPv6. It is the recommended interface for name resolution in building protocol independent applications and for transitioning legacy IPv4 code to the IPv6 Internet.
Internally, the functions may use a variety of resolution methods not limited to the Domain Name System (DNS). The Name Service Switch is commonly used on Unix-like systems and affects most implementation of this pair as it did with their BSD-socket era predecessors.[2]
The C data structure used to represent results of address address information queries for use in the networking API might be defined as follows, possibly with members in another order or additional members:[3]Template:Refn
<syntaxhighlight lang="c"> struct addrinfo {
int ai_flags; int ai_family; int ai_socktype; int ai_protocol; socklen_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next;
}; </syntaxhighlight>
The structure contains a member <syntaxhighlight lang="text" class="" style="" inline="1">ai_family</syntaxhighlight> and a member <syntaxhighlight lang="text" class="" style="" inline="1">ai_addr</syntaxhighlight> pointing to a <syntaxhighlight lang="text" class="" style="" inline="1">struct sockaddr</syntaxhighlight> object with its own <syntaxhighlight lang="text" class="" style="" inline="1">sa_family</syntaxhighlight> field. These are set to the same value when the structure is created with function <syntaxhighlight lang="text" class="" style="" inline="1">getaddrinfo()</syntaxhighlight> in some implementations.Template:Cn
<syntaxhighlight lang="text" class="" style="" inline="1">getaddrinfo()</syntaxhighlight> converts human-readable text strings representing hostnames or IP addresses into a list of struct addrinfo structures. The function prototype is:[1]
<syntaxhighlight lang="c"> int getaddrinfo(const char *restrict hostname,
const char *restrict service,
const struct addrinfo *restrict hints,
struct addrinfo **restrict res);
</syntaxhighlight>
- Template:Mono
- can be either a domain name, such as "example.com", an address string, such as "Template:IPaddr", or <syntaxhighlight lang="text" class="" style="" inline="1">NULL</syntaxhighlight>, in which case the address Template:IPaddr or Template:IPaddr is assigned depending on the hints flags.
- Template:Mono
- can be a port number passed as string, such as Template:Mono, or a service name, e.g. Template:Mono. In the latter case a typical implementation uses <syntaxhighlight lang="text" class="" style="" inline="1">getservbyname()</syntaxhighlight> to query the file Template:Mono to resolve the service to a port number.
- Template:Mono
- can be either <syntaxhighlight lang="text" class="" style="" inline="1">NULL</syntaxhighlight> or an <syntaxhighlight lang="text" class="" style="" inline="1">addrinfo</syntaxhighlight> structure with the type of service requested.
- Template:Mono
- is a pointer that will be assigned to point to a new dynamically allocated linked list of <syntaxhighlight lang="text" class="" style="" inline="1">addrinfo</syntaxhighlight> structures with the information requested after successful completion of the function, to be freed with <syntaxhighlight lang="text" class="" style="" inline="1">freeaddrinfo()</syntaxhighlight>.[4]
The function returns 0 upon success and non-zero error value if it fails,[1] which can be mapped to a human-readable string using <syntaxhighlight lang="text" class="" style="" inline="1">gai_strerror()</syntaxhighlight>[5]
Implementations vary among platforms, but often the function will look up hostnames in the DNS if not, and look up services among a well-known service database. The exact set of sources queried for hostname and service resolution is often controlled by the system's Name Service Switch configuration.
The function <syntaxhighlight lang="text" class="" style="" inline="1">freeaddrinfo()</syntaxhighlight> frees the memory allocated by function <syntaxhighlight lang="text" class="" style="" inline="1">getaddrinfo()</syntaxhighlight>. Given a linked list of <syntaxhighlight lang="text" class="" style="" inline="1">struct addrinfo</syntaxhighlight> objects starting at Template:Mono, yielded by getaddrinfo(..., &ai), freeaddrinfo(ai) frees every <syntaxhighlight lang="text" class="" style="" inline="1">struct addrinfo</syntaxhighlight> object in that list.
<syntaxhighlight lang="c">
void freeaddrinfo(struct addrinfo *ai);
</syntaxhighlight>
The function <syntaxhighlight lang="text" class="" style="" inline="1">getnameinfo()</syntaxhighlight> converts the internal binary representation of an IP address in the form of a pointer to a <syntaxhighlight lang="text" class="" style="" inline="1">struct sockaddr</syntaxhighlight> into text strings consisting of the hostname or, if the address cannot be resolved into a name, a textual IP address representation, as well as the service port name or number. The function prototype is specified as follows: <syntaxhighlight lang="c"> int getnameinfo(const struct sockaddr *restrict sa, socklen_t salen,
char *restrict host, socklen_t hostlen,
char *restrict service, socklen_t servicelen,
int flags);
</syntaxhighlight>
Example
The following example uses <syntaxhighlight lang="text" class="" style="" inline="1">getaddrinfo()</syntaxhighlight> to resolve the domain name Template:Mono into its list of addresses and then calls <syntaxhighlight lang="text" class="" style="" inline="1">getnameinfo()</syntaxhighlight> on each result to return the canonical name for the address. In general, this produces the original hostname, unless the particular address has multiple names, in which case the canonical name is returned. In this example, the domain name is printed three times, once for each of the three results obtained. <syntaxhighlight lang="c">
- include <errno.h>
- include <stdio.h>
- include <stdlib.h>
- include <string.h>
- include <netdb.h>
- include <netinet/in.h>
- include <sys/socket.h>
- ifndef NI_MAXHOST
- define NI_MAXHOST 1025
- endif
int main(void) {
const char query_host[] = "www.example.com"; struct addrinfo *result;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM
};
// Resolve the domain name into a list of addresses
int gai_rc = getaddrinfo(query_host, NULL, &hints, &result);
switch (gai_rc) {
case 0:
break;
case EAI_SYSTEM:
fprintf(stderr, "Failed to resolve server address '%s': system error %s\n",
query_host, strerror(errno));
return EXIT_FAILURE;
default:
fprintf(stderr, "Failed to resolve server address '%s': %s\n",
query_host, gai_strerror(gai_rc));
return EXIT_FAILURE;
}
// Perform reverse DNS lookup for each resolved address
for (const struct addrinfo *ai = result; ai != NULL; ai = ai->ai_next) {
char hostname[NI_MAXHOST];
char addr[NI_MAXHOST];
int gni_rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, addr,
sizeof(addr), NULL, 0, NI_NUMERICHOST);
if (gni_rc != 0) {
fprintf(stderr, "Failed to format address: %s\n", gai_strerror(gni_rc));
continue;
}
gni_rc = getnameinfo(ai->ai_addr, ai->ai_addrlen,
hostname, sizeof(hostname), NULL, 0, 0);
switch (gni_rc) {
case 0:
printf("Hostname for %s: %s\n", addr, hostname);
break;
case EAI_SYSTEM:
fprintf(stderr, "Reverse DNS lookup for %s: system error %s\n",
addr, strerror(errno));
break;
default:
fprintf(stderr, "Reverse DNS lookup failed for %s: %s\n",
addr, gai_strerror(gni_rc));
}
}
freeaddrinfo(result); return 0;
} </syntaxhighlight>
See also
Notes
References
External links
- freeaddrinfo and getaddrinfo specifications in POSIX.1-2024. The Open Group Base Specifications Issue 8, 2024 edition
- RFC 3493, Basic Socket Interface Extensions for IPv6
- ↑ 1.0 1.1 1.2 {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ Stevens R., Fenner, Rudoff [2003] UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API. Publisher: Addison-Wesley Professional. Pub. Date: November 14, 2003 p. 256
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}