Name

getaddrinfo, freeaddrinfo
- network address and service translation

Synopsis

#include <sys/types.h> #include <sys/socket.h> #include <netdb.h>
  int
.Fo getaddrinfo const char *hostname const char *servname const struct addrinfo *hints struct addrinfo **res
.Fc
  void freeaddrinfo (struct addrinfo *ai);

Return values

getaddrinfo returns zero on success or one of the error codes listed in gai_strerror if an error occurs.

Detailed description

The getaddrinfo function is used to get a list of IP addresses and port numbers for host hostname and service servname. It is a replacement for and provides more flexibility than the gethostbyname and getservbyname functions.

The hostname and servname arguments are either pointers to NUL-terminated strings or the null pointer. An acceptable value for hostname is either a valid host name or a numeric host address string consisting of a dotted decimal IPv4 address. The servname is either a decimal port number or a service name listed in services. At least one of hostname and servname must be non-null.

hints is an optional pointer to a struct addrinfo, as defined by <netdb.h:>

struct addrinfo {
        int ai_flags;           /* input flags */
        int ai_family;          /* protocol family for socket */
        int ai_socktype;        /* socket type */
        int ai_protocol;        /* protocol for socket */
        socklen_t ai_addrlen;   /* length of socket-address */
        struct sockaddr *ai_addr; /* socket-address for socket */
        char *ai_canonname;     /* canonical name for service location */
        struct addrinfo *ai_next; /* pointer to next in list */
};

         

This structure can be used to provide hints concerning the type of socket that the caller supports or wishes to use. The caller can supply the following structure elements in hints:

ai_family The protocol family that should be used. When ai_family is set to PF_UNSPEC, it means the caller will accept any protocol family supported by the operating system.
ai_socktype Denotes the type of socket that is wanted: SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW. When ai_socktype is zero the caller will accept any socket type.
ai_protocol Indicates which transport protocol is desired, IPPROTO_UDP or IPPROTO_TCP. If ai_protocol is zero the caller will accept any protocol.
ai_flags ai_flags is formed by OR ’ing the following values:
AI_CANONNAME If the AI_CANONNAME bit is set, a successful call to getaddrinfo will return a NUL-terminated string containing the canonical name of the specified hostname in the ai_canonname element of the first addrinfo structure returned.
AI_NUMERICHOST
  If the AI_NUMERICHOST bit is set, it indicates that hostname should be treated as a numeric string defining an IPv4 address and no name resolution should be attempted.
AI_PASSIVE If the AI_PASSIVE bit is set it indicates that the returned socket address structure is intended for use in a call to bind. In this case, if the hostname argument is the null pointer, then the IP address portion of the socket address structure will be set to INADDR_ANY for an IPv4 address

If the AI_PASSIVE bit is not set, the returned socket address structure will be ready for use in a call to connect for a connection-oriented protocol or connect if a connectionless protocol was chosen. The IP address portion of the socket address structure will be set to the loopback address if hostname is the null pointer and AI_PASSIVE is not set.

All other elements of the addrinfo structure passed via hints must be zero or the null pointer.

If hints is the null pointer, getaddrinfo behaves as if the caller provided a struct addrinfo with ai_family set to PF_UNSPEC and all other elements set to zero or NULL.

After a successful call to getaddrinfo, *res is a pointer to a linked list of one or more addrinfo structures. The list can be traversed by following the ai_next pointer in each addrinfo structure until a null pointer is encountered. The three members ai_family, ai_socktype, and ai_protocol in each returned addrinfo structure are suitable for a call to socket For each addrinfo structure in the list, the ai_addr member points to a filled-in socket address structure of length ai_addrlen.

This implementation of

The current implementation assumes a one-to-one relationship between the interface and link, which is not necessarily true from the specification.

All of the information returned by getaddrinfo is dynamically allocated: the addrinfo structures themselves as well as the socket address structures and the canonical host name strings included in the addrinfo structures.

Memory allocated for the dynamically allocated structures created by a successful call to getaddrinfo is released by the freeaddrinfo function. The ai pointer should be a addrinfo structure created by a call to getaddrinfo.


Examples

#include <stdio.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.
int main()
{
int error;
char *hostname=”www,yahoo.com”;
char *servname=”http”;
struct addrinfo hint;
struct addrinfo *res;
hint.ai_flags=AI_CANONNAME];
hint.ai_family=AF_INET;
hint.ai_socktype=SOCK_STREAM;
hint.ai_protocol=6];
error=getaddrinfo(hostname,servname,&hint,&res);
if (error)
printf(“getaddrinfo failed”);
else
printf(“getaddrinfo passed”);
freeaddrinfo(res);
return 0;
}

         

See also

bind, connect, send, socket, gai_strerror, gethostbyname, getnameinfo, getservent
Protocol Independence Using the Sockets API
The getaddrinfo function is defined by the -p1003.1g-2000 draft specification and documented in RFC 3493, "Basic Socket Interface Extensions for IPv6."

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top