Getaddrinfo


In C programming, the functions ' and ' 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.
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. 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.

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:

struct addrinfo ;

The structure contains a member and a member pointing to a object with its own field. These are set to the same value when the structure is created with function in some implementations.

converts human-readable text strings representing hostnames or IP addresses into a list of struct addrinfo structures.
The function prototype is:

int getaddrinfo;

; '
; '

; '
; '

The function returns 0 upon success and non-zero error value if it fails, which can be mapped to a human-readable string using
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 frees the memory allocated by function. Given a linked list of objects starting at , yielded by getaddrinfo, freeaddrinfo frees every object in that list.

void freeaddrinfo;

The function converts the internal binary representation of an IP address in the form of a pointer to a 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:

int getnameinfo;

Example

The following example uses to resolve the domain name into its list of addresses and then calls 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.

  1. include
  2. include
  3. include
  4. include
  5. include
  6. include
  7. ifndef NI_MAXHOST
  8. define NI_MAXHOST 1025
  9. endif
int main