getaddrinfo() — Get address information

Standards

Standards / Extensions C or C++ Dependencies
RFC2553 both z/OS® V1R4

Format

#define _OPEN_SYS_SOCK_IPV6
#include <sys/socket.h>
#include <netdb.h>

int getaddrinfo(const char *nodename,
                const char *servname,
                const struct addrinfo *hints,
                struct addrinfo **res);
SUSV3:
#define _POSIX_C_SOURCE 200112L
#include <sys/socket.h>
#include <netdb.h>
int getaddrinfo(const char *__restrict__ nodename, 
           const char *__restrict__ servname, 
           const struct addrinfo *__restrict__ hints, 
           struct addrinfo **__restrict__ res);

General description

The getaddrinfo() function translates the name of a service location (for example, a host name) and/or service name and returns a set of socket addresses and associated information to be used in creating a socket with which to address the specified service.

The nodename and servname arguments are either pointers to null-terminated strings or null pointers. One or both of these two arguments must be specified as a non-null pointer.

The format of a valid name depends on the protocol family or families. If a specific family is not given and the name could be interpreted as valid within multiple supported families, the function attempts to resolve the name in all supported families. When no errors are detected, all successful results will be returned.

If the nodename argument is not null, it can be a descriptive name or it can be an address string. If the specified address family is AF_INET, AF_INET6, or AF_UNSPEC, valid descriptive names include host names. If the specified address family is AF_INET or AF_UNSPEC, address strings using standard dot notation as specified in inet_addr() are valid. If the specified address family is AF_INET6 or AF_UNSPEC, standard IPv6 text forms described in inet_pton() are valid. In addition, scope information can be appended to the descriptive name or the address string using the format nodename%scope information. Scope information can be either an interface name or the numeric representation of an interface index suitable for use on this system.

If nodename is not null, the requested service location is named by nodename; otherwise, the requested service location is local to the caller.

If servname is null, the call returns network-level addresses for the specified nodename. If servname is not null, it is a null-terminated character string identifying the requested service. This can be either a descriptive name or a numeric representation suitable for use with the address family or families. If the specified address family is AF_INET, AF_INET6, or AF_UNSPEC, the service can be specified as a string specifying a decimal port number.

If the argument hints is not null, it refers to a structure containing input values that may direct the operation by providing options and by limiting the returned information to a specific socket type, address family and/or protocol. In the hints structure every member other than ai_flags, ai_family, ai_socktype, and ai_protocol must be zero or a null pointer. A value of AF_UNSPEC for ai_family means that the caller will accept any protocol family. A value of zero for ai_socktype means that the caller will accept any socket type. A value of zero for ai_protocol means that the caller will accept any protocol. If hints is a null pointer, the behavior must be as if it referred to a structure containing the value zero for the ai_flags, ai_socktype, and ai_protocol fields, and AF_UNSPEC for the ai_family field.

The ai_flags member to which the hints argument points can be set to 0 or be the bitwise inclusive OR of one or more of the following values:
  • AI_PASSIVE
  • AI_CANONNAME
  • AI_NUMERICHOST
  • AI_NUMERICSERV
  • AI_V4MAPPED
  • AI_ALL
  • AI_ADDRCONFIG
  • AI_EXTFLAGS

If the AI_PASSIVE bit is set in the ai_flags member of the hints structure, then the caller plans to use the returned socket address structure in a call to bind(). In this case, if the nodename argument is a null pointer, then the IP address portion of the socket address structure will be set to INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6 address. If the AI_PASSIVE bit is not set in the ai_flags member of the hints structure, then the returned socket address structure will be ready for a call to connect() (for a connection-oriented protocol) or either connect(), sendto(), or sendmsg() (for a connectionless protocol). In this case, if the nodename argument is a null pointer, then the IP address portion of the socket address structure will be set to the loopback address.

If the AI_CANONNAME bit is set in the ai_flags member of the hints structure, then upon successful return the ai_canonname member of the first addrinfo structure in the linked list will point to a null-terminated string containing the canonical name of the specified nodename.

If the AI_NUMERICHOST bit is set in the ai_flags member of the hints structure, then a non-null nodename string must be a numeric host address string. Otherwise an error code of EAI_NONAME is returned. This flag prevents any type of name resolution service (for example, the DNS) from being called.

If the AI_NUMERICSERV flag is specified then a non-null servname string must be a numeric port string. Otherwise an error code EAI_NONAME is returned. This flag prevents any type of name resolution service (for example, NIS+ from being invoked.

If the AI_V4MAPPED flag is specified along with the AF field with the value of AF_INET6, or a value of AF_UNSPEC when IPv6 is supported on the system, then the caller will accept IPv4-mapped IPv6 addresses. When the AI_ALL flag is not also specified and no IPv6 addresses are found, then a query is made for IPv4 addresses. If any IPv4 addresses are found, they are returned as IPv4-mapped IPv6 addresses.

If the AF field does not have a value of AF_INET6 or the AF field contains AF_UNSPEC but IPv6 is not supported on the system, this flag is ignored.

When the AF field has a value of AF_INET6 and AI_ALL is set, the AI_V4MAPPED flag must also be set to indicate that the caller will accept all addresses (IPv6 and IPv4-mapped IPv6 addresses). When the AF field has a value of AF_UNSPEC when the system supports IPv6 and AI_ALL is set, the caller accepts IPv6 addresses and either IPv4 (if AI_V4MAPPED is not set) or IPv4-mapped IPv6 (if AI_V4MAPPED is set) addresses. A query is first made for IPv6 addresses and if successful, the IPv6 addresses are returned. Another query is then made for IPv4 addresses and any found are returned as IPv4 addresses (if AI_V4MAPPED was not set) or as IPv4-mapped IPv6 addresses (if AI_V4MAPPED was set). If the AF field does not have the value of AF_INET6, or the value of AF_UNSPEC when the system supports IPv6, the flag is ignored.

If the AI_ADDRCONFIG flag is specified then a query for IPv6 address records should occur only if the node has at least one IPv6 source address configured. A query for IPv4 address records will always occur, whether or not any IPv4 addresses are configured. The loopback address is not considered for this case as valid as a configured sources address.

If the AI_EXTFLAGS flag is specified then getaddrinfo() will look for the values stored in the extended flags field called ai_eflags in the addrinfo structure. The flags stored in the ai_eflags field are only meaningful if the AI_EXTFLAGS flag is set in the ai_flags field of the addrinfo data structure. By default, AI_EXTFLAGS is not set in the ai_flags field. If AI_EXTFLAGS is set in the ai_flags field, and the ai_eflags extended flags field is 0 (zero) or undefined, then AI_EXTFLAGS is ignored. The ai_eflags field can be set to any of the following:

These flags can be combined into a flag set to express complex address preferences, but some can result in a contradictory flag set. For example, the following flags are mutually exclusive:

All of the information returned by getaddrinfo() is dynamically allocated: the addrinfo structures, and the socket address structures and canonical node name strings pointed to by the addrinfo structures. To return this information to the system the function freeaddrinfo() is called.

Usage notes

  1. If the caller handles only TCP and not UDP, for example, then the ai_protocol member of the hints structure should be set to IPPROTO_TCP when getaddrinfo() is called.
  2. If the caller handles only IPV4 and not IPv6, then the ai_family member of the hints structure should be set to AF_INET when getaddrinfo() is called.
  3. Scope information is only pertinent to IPv6 link-local addresses. It is ignored for resolved IPv4 addresses and IPv6 addresses that are not link-local addresses.

Returned value

When successful, getaddrinfo() returns 0 and a pointer to a linked list of one or more addrinfo structures through the res argument. The caller can process each addrinfo structure in this list by following the ai_next pointer, until a null pointer is encountered. In each returned addrinfo structure the three members ai_family, ai_socktype, and ai_protocol are the corresponding arguments for a call to the socket() function. In each addrinfo structure the ai_addr member points to a filled-in socket address structure whose length is specified by the ai_addrlen member. Upon failure, getaddrinfo() returns a non-zero error code. The error codes are as follows:

Error Code
Description
EAI_AGAIN
The name specified by the Node_Name or Service_Name parameter could not be resolved within the configured time interval, or the resolver address space has not been started. The request can be retried later.
EAI_BADEXTFLAGS
The extended flags parameter had an incorrect setting.
EAI_BADFLAGS
The flags parameter had an incorrect setting.
EAI_FAIL
An unrecoverable error occurred.
EAI_FAMILY
The family parameter had an incorrect setting.
EAI_MEMORY
A memory allocation failure occurred during an attempt to acquire an Addr_Info structure.
EAI_NONAME
One of the following conditions occurred:
  1. The name does not resolve for the specified parameters. At least one of the Name or Service operands must be specified.
  2. The request name parameter is valid, but it does not have a record at the name server.
EAI_SERVICE
The service that was passed was not recognized for the specified socket type.
EAI_SOCKTYPE
The intended socket type was not recognized.
EAI_SYSTEM
A system error occurred.

For more information about the above return codes, see z/OS V2R1.0 Communications Server: IP and SNA Codes.

Related information