GETADDRINFO

Section: C Library Functions (3)
Index Return to Main Contents

BSD mandoc
KAME  

NAME

getaddrinfo freeaddrinfo gai_strerror - nodename-to-address translation in protocol-independent manner  

SYNOPSIS

Fd #include <sys/socket.h> Fd #include <netdb.h> Ft int Fn getaddrinfo const char *nodename const char *servname const struct addrinfo *hints struct addrinfo **res Ft void Fn freeaddrinfo struct addrinfo *ai Ft char * Fn gai_strerror int ecode  

DESCRIPTION

The Fn getaddrinfo function is defined for protocol-independent nodename-to-address translation. It performs functionality of gethostbyname(3) and getservbyname(3), in more sophisticated manner.

The addrinfo structure is defined as a result of including the <netdb.h> header:

struct addrinfo {                                                  *
     int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
     int     ai_family;    /* PF_xxx */
     int     ai_socktype;  /* SOCK_xxx */
     int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
     size_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 */
};

The Fa nodename and Fa servname arguments are pointers to null-terminated strings or NULL One or both of these two arguments must be a non -NULL pointer. In the normal client scenario, both the Fa nodename and Fa servname are specified. In the normal server scenario, only the Fa servname is specified. A non -NULL Fa nodename string can be either a node name or a numeric host address string (i.e., a dotted-decimal IPv4 address or an IPv6 hex address). A non -NULL Fa servname string can be either a service name or a decimal port number.

The caller can optionally pass an addrinfo structure, pointed to by the third argument, to provide hints concerning the type of socket that the caller supports. In this Fa hints structure all members other than Fa ai_flags , Fa ai_family , Fa ai_socktype , and Fa ai_protocol must be zero or a NULL pointer. A value of PF_UNSPEC for Fa ai_family means the caller will accept any protocol family. A value of 0 for Fa ai_socktype means the caller will accept any socket type. A value of 0 for Fa ai_protocol means the caller will accept any protocol. For example, if the caller handles only TCP and not UDP, then the Fa ai_socktype member of the hints structure should be set to SOCK_STREAM when Fn getaddrinfo is called. If the caller handles only IPv4 and not IPv6, then the Fa ai_family member of the Fa hints structure should be set to PF_INET when Fn getaddrinfo is called. If the third argument to Fn getaddrinfo is a NULL pointer, this is the same as if the caller had filled in an addrinfo structure initialized to zero with Fa ai_family set to PF_UNSPEC.

Upon successful return a pointer to a linked list of one or more addrinfo structures is returned through the final argument. The caller can process each addrinfo structure in this list by following the Fa ai_next pointer, until a NULL pointer is encountered. In each returned addrinfo structure the three members Fa ai_family , Fa ai_socktype , and Fa ai_protocol are the corresponding arguments for a call to the Fn socket function. In each addrinfo structure the Fa ai_addr member points to a filled-in socket address structure whose length is specified by the Fa ai_addrlen member.

If the AI_PASSIVE bit is set in the Fa ai_flags member of the Fa hints structure, then the caller plans to use the returned socket address structure in a call to Fn bind . In this case, if the Fa 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 Fa ai_flags member of the Fa hints structure, then the returned socket address structure will be ready for a call to Fn connect (for a connection-oriented protocol) or either Fn connect , Fn sendto , or Fn sendmsg (for a connectionless protocol) In this case, if the Fa 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 Fa ai_flags member of the Fa hints structure, then upon successful return the Fa 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 Fa nodename .

If the AI_NUMERICHOST bit is set in the Fa ai_flags member of the Fa hints structure, then a non -NULL Fa 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 (e.g., the DNS) from being called.

All of the information returned by Fn 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 Fn freeaddrinfo is called. The Fa addrinfo structure pointed to by the Fa ai argument is freed, along with any dynamic storage pointed to by the structure. This operation is repeated until a NULL Fa ai_next pointer is encountered.

To aid applications in printing error messages based on the EAI_xxx codes returned by Fn getaddrinfo , Fn gai_strerror is defined. The argument is one of the EAI_xxx values defined earlier and the return value points to a string describing the error. If the argument is not one of the EAI_xxx values, the function still returns a pointer to a string whose contents indicate an unknown error.  

FILES

/etc/hosts
/etc/host.conf
/etc/resolv.conf

 

DIAGNOSTICS

Error return status from Fn getaddrinfo is zero on success and non-zero on errors. Non-zero error codes are defined in <netdb.h> and as follows:

EAI_ADDRFAMILY
address family for nodename not supported
EAI_AGAIN
temporary failure in name resolution
EAI_BADFLAGS
invalid value for ai_flags
EAI_FAIL
non-recoverable failure in name resolution
EAI_FAMILY
ai_family not supported
EAI_MEMORY
memory allocation failure
EAI_NODATA
no address associated with nodename
EAI_NONAME
nodename nor servname provided, or not known
EAI_SERVICE
servname not supported for ai_socktype
EAI_SOCKTYPE
ai_socktype not supported
EAI_SYSTEM
system error returned in errno

If called with proper argument, Fn gai_strerror returns a pointer to a string describing the given error code. If the argument is not one of the EAI_xxx values, the function still returns a pointer to a string whose contents indicate an unknown error.  

SEE ALSO

getnameinfo(3), gethostbyname(3), getservbyname(3), hosts(5), services(5), hostname(7), named(8)

R. Gilligan, S. Thomson, J. Bound, and W. Stevens, ``Basic Socket Interface Extensions for IPv6,'' RFC2133, April 1997.  

HISTORY

The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.  

STANDARDS

The Fn getaddrinfo function is defined IEEE POSIX 1003.1g draft specification, and documented in ``Basic Socket Interface Extensions for IPv6'' (RFC2133).  

BUGS

The text was shamelessly copied from RFC2133.


 

Index

NAME
SYNOPSIS
DESCRIPTION
FILES
DIAGNOSTICS
SEE ALSO
HISTORY
STANDARDS
BUGS

linux.jgfs.net manual pages