#include <sys/socket.h> #include <netdb.h> int getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hints, struct addrinfo **res);
The getaddrinfo() function translates the name of a service location (for example, a host name) and/or a 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 parameters are either null pointers or pointers to null-terminated strings. One or both of these two parameters must be a non-null pointer.
The format of a valid name depends on the address family or families. If a specific family is not given and the name could be interpreted as valid within multiple supported families, the implementation will attempt to resolve the name in all supported families and, in the absence of errors, one or more results shall be returned.
The structure struct addrinfo is defined in <netdb.h>.
struct addrinfo { int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST, .. */ int ai_family; /* AF_xxx */ int ai_socktype; /* SOCK_xxx */ int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ socklen_t ai_addrlen; /* length of ai_addr */ char *ai_canonname; /* canonical name for nodename */ struct sockaddr *ai_addr; /* binary address */ struct addrinfo *ai_next; /* next structure in linked list */ int ai_eflags; /* Extended flags for special usage */ };
A value of AF_UNSPEC for ai_family means that the caller will accept any address 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 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.
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.
The ai_flags field to which hints parameter points must have the value zero or be the bitwise OR of one or more of the values AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST, AI_NUMERICSERV, AI_V4MAPPED, AI_ALL, AI_ADDRCONFIG, and AI_EXTFLAGS.
The AI_PASSIVE flag in the ai_flags member of the hints structure specifies how to fill in the IP address portion of the socket address structure. If the AI_PASSIVE flag is specified, then the returned address information will be suitable for use in binding a socket for accepting incoming connections for the specified service (that is, a call to bind()). In this case, if the nodename parameter is null, 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, the returned address information will be suitable for a call to connect() (for a connection-oriented protocol) or for a call to connect(), sendto(), or sendmsg() (for a connectionless protocol). In this case, if the nodename parameter is null, then the IP address portion of the socket address structure will be set to the loopback address. This flag is ignored if the nodename parameter is not null.
If the flag AI_CANONNAME is specified and the nodename parameter is not null, the function attempts to determine the canonical name corresponding to nodename (for example, if nodename is an alias or shorthand notation for a complete name).
If the flag AI_NUMERICHOST is specified then a non-null nodename string must be a numeric host address string. Otherwise an error of [EAI_NONAME] is returned. This flag prevents any type of name resolution service (for example, the DNS) from being called.
If the flag AI_NUMERICSERV is specified then a non-null servname string must be a numeric port string. Otherwise an error [EAI_NONAME] is returned. This flag prevents any type of name resolution service (for example, NIS+) from being called.
If the AI_V4MAPPED flag is specified along with an ai_family of AF_INET6, then the caller will accept IPv4-mapped IPv6 addresses. That is, if no IPv6 addresses are found then a query is made for A records and an IPv4 local host table lookup is performed and any results are returned as IPv4-mapped IPv6 addresses (ai_addrlen will be 28). The AI_V4MAPPED flag is ignored unless ai_family equals AF_INET6.
The AI_ALL flag is used in conjunction with the AI_V4MAPPED flag, and is only used with an ai_family of AF_INET6. When AI_ALL is logically or'd with AI_V4MAPPED flag then the caller will accept all addresses: IPv6 and IPv4-mapped IPv6. A query is first made for AAAA records and if successful, the IPv6 addresses are returned. Another query is then made for A records and any found are returned as IPv4-mapped IPv6 addresses (ai_addrlen will be 28). This flag is ignored unless ai_family equals AF_INET6.
If the AI_ADDRCONFIG flag is specified then a family of addresses will only
be returned if at least one source address is configured for that family. That is,
a query for AAAA records and an IPv6 local host table lookup will
occur only if the node has at least one IPv6 source address configured.
Similarly, a query for A records and an IPv4 local host table lookup will occur only
if the node has at least one IPv4 source
address configured. If the AI_V4MAPPED flag is also specified, then IPv4-mapped IPv6
addresses are returned only if at least one IPv4 source address is configured.
The loopback address is not considered as a valid
configured source address.
If the AI_EXTFLAGS flag is specified then the system should look for the "ai_eflags" extended flag-set field in the addrinfo structure. That is if AI_EXTFLAGS is set, getaddrinfo will look for the values stored in the extended flag-set field, "ai_eflags", of the addrinfo data structure to determine source address selection preferences.
The ai_socktype field to which argument hints points specifies the socket type for the service. If a specific socket type is not given (for example, a value of zero) and the service name could be interpreted as valid with multiple supported socket types, the implementation will attempt to resolve the service name for all supported socket types and, all successful results will be returned. A non-zero socket type value will limit the returned information to values with the specified socket type.
The ai_eflags member of the hints structure specifies the preferred address selection rules. This field must have a value of zero or a valid bitwise OR combination of the values defined in <netdb.h>: IPV6_PREFER_SRC_HOME, IPV6_PREFER_SRC_COA, IPV6_PREFER_SRC_TMP, IPV6_PREFER_SRC_PUBLIC, IPV6_PREFER_SRC_CGA, and IPV6_PREFER_SRC_NONCGA. If ai_eflags is zero, then the default address selection rules will be used.
If the IPV6_PREFER_SRC_HOME (0x00000001) flag is specified then home addresses will be preferred as the source address. If this flag is set then IPV6_PREFER_SRC_COA must not be set.
If the IPV6_PREFER_SRC_COA (0x00000002) flag is specified then care-of addresses will be preferred as the source address. If this flag is set then IPV6_PREFER_SRC_HOME must not be set.
If the IPV6_PREFER_SRC_TMP (0x00000004) flag is specified then temporary addresses will be preferred as the source address. If this flag is set then IPV6_PREFER_SRC_PUBLIC must not be set.
If the IPV6_PREFER_SRC_PUBLIC (0x00000008) flag is specified then public addresses will be preferred as the source address. If this flag is set then IPV6_PREFER_SRC_TMP must not be set.
If the IPV6_PREFER_SRC_CGA (0x00000010) flag is specified then CGA addresses will be preferred as the source address. If this flag is set then IPV6_PREFER_SRC_NONCGA must not be set.
If the IPV6_PREFER_SRC_NONCGA (0x00000020) flag is specified then non-CGA addresses will be preferred as the source address. If this flag is set then IPV6_PREFER_SRC_CGA must not be set.
If nodename is not null, and if requested by the AI_CANONNAME flag, the ai_canonname field of the first returned addrinfo structure points to a null-terminated string containing the canonical name corresponding to the input nodename; if the canonical name is not available, then ai_canonname refers to the argument nodename or a string with the same contents. The contents of the ai_flags field of the returned structures is undefined.
All fields in socket address structures returned by getaddrinfo() that are not filled in through an explicit argument (for example, sin6_flowinfo and sin_zero) will be set to zero.
Note: This makes it easier to compare socket address structures.
Authorization of *R (allow access to the object) to the host aliases file specified by the HOSTALIASES environment variable.
You also need *X authority to each directory in the path of the host aliases file.
getaddrinfo() returns an integer. Possible values are:
When getaddrinfo() fails, the error return value can be set to one of the following:
The name could not be resolved at this time. Future attempts may succeed.
The flags parameter had an invalid value.
A non-recoverable error occurred when attempting to resolve the name.
The address family was not recognized.
There was a memory allocation failure when trying to allocate storage for the return value.
The name does not resolve for the supplied parameters. Neither nodename nor servname were passed. At least one of these must be passed.
The service passed was not recognized for the specified socket type.
The intended socket type was not recognized.
A system error occurred; the error code can be found in errno
The extended flags parameter had an invalid value.
If the address information is retrieved from the domain name server, sockets converts the address information specified by the nodename and servname parameters from the default (CCSID) to ASCII before communicating with the domain name server. If the address information is retrieved from the local host table, no conversion is done on the node and service names specified by the nodename and servname parameters unless the CCSID of the job is something other than 65535.
In addition, the canonical names for nodename returned in the addrinfo structures will be returned in the default CCSID of the job if they are obtained from the domain name server. For conversion to occur for the canonical names returned in the addrinfo structures when they are obtained from the local host table, you must use a job CCSID of something other than 65535.
www.ibm.com%8 or www.ibm.com%ethline FE80::1%8 or FE80::1%ethlineIf appended, the sin6_scope_id field in the sockaddr_in6 structure pointed to by ai_addr will be set to the integer value associated with the appended value.
Top | UNIX-Type APIs | APIs by category |