Manual Pages for UNIX Darwin command on man freeaddrinfo
MyWebUniversity

Manual Pages for UNIX Darwin command on man freeaddrinfo

GETADDRINFO(3) BSD Library Functions Manual GETADDRINFO(3)

NAME

ggeettaaddddrriinnffoo, ffrreeeeaaddddrriinnffoo - socket address structure to host and service

name

SYNOPSIS

##iinncclluuddee <>

##iinncclluuddee <>

##iinncclluuddee <>

int ggeettaaddddrriinnffoo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res); void ffrreeeeaaddddrriinnffoo(struct addrinfo *ai);

DESCRIPTION

The ggeettaaddddrriinnffoo() 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(3) and getservbyname(3) 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 or an IPv6 address. The servname is either a deci-

mal port number or a service name listed in services(5). At least one of

hostname and servname must be non-null.

hints is an optional pointer to a struct addrinfo, as defined by : struct addrinfo { int aiflags; /* input flags */ int aifamily; /* protocol family for socket */ int aisocktype; /* socket type */ int aiprotocol; /* protocol for socket */

socklent aiaddrlen; /* length of socket-address */

struct sockaddr *aiaddr; /* socket-address for socket */

char *aicanonname; /* canonical name for service location */ struct addrinfo *ainext; /* 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: aifamily The protocol family that should be used. When aifamily is set to PFUNSPEC, it means the caller will accept any protocol family supported by the operating system. aisocktype Denotes the type of socket that is wanted: SOCKSTREAM, SOCKDGRAM, or SOCKRAW. When aisocktype is zero the caller will accept any socket type. aiprotocol Indicates which transport protocol is desired, IPPROTOUDP or IPPROTOTCP. If aiprotocol is zero the caller will accept any protocol. aiflags aiflags is formed by OR'ing the following values:

AICANONNAME If the AICANONNAME bit is set, a success-

ful call to ggeettaaddddrriinnffoo() will return a

NUL-terminated string containing the

canonical name of the specified hostname in the aicanonname element of the first addrinfo structure returned.

AINUMERICHOST If the AINUMERICHOST bit is set, it indi-

cates that hostname should be treated as a numeric string defining an IPv4 or IPv6 address and no name resolution should be attempted. AIPASSIVE If the AIPASSIVE bit is set it indicates that the returned socket address structure is intended for use in a call to bind(2). In this case, if the hostname argument is

the null pointer, then the IP address por-

tion of the socket address structure will be set to INADDRANY for an IPv4 address or IN6ADDRANYINIT for an IPv6 address. If the AIPASSIVE bit is not set, the returned socket address structure will be ready for use in a call to connect(2) for

a connection-oriented protocol or

connect(2), sendto(2), or sendmsg(2) 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 AIPASSIVE 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, ggeettaaddddrriinnffoo() behaves as if the caller pro-

vided a struct addrinfo with aifamily set to PFUNSPEC and all other elements set to zero or NULL. After a successful call to ggeettaaddddrriinnffoo(), *res is a pointer to a linked list of one or more addrinfo structures. The list can be traversed by following the ainext pointer in each addrinfo structure until a null pointer is encountered. The three members aifamily, aisocktype, and aiprotocol in each returned addrinfo structure are suitable for a call

to socket(2). For each addrinfo structure in the list, the aiaddr mem-

ber points to a filled-in socket address structure of length aiaddrlen.

This implementation of ggeettaaddddrriinnffoo() allows numeric IPv6 address notation

with scope identifier, as documented in chapter 11 of draft-ietf-

ipv6-scoping-arch-02.txt. By appending the percent character and scope

identifier to addresses, one can fill the sin6scopeid field for addresses. This would make management of scoped addresses easier and

allows cut-and-paste input of scoped addresses.

At this moment the code supports only link-local addresses with the for-

mat. The scope identifier is hardcoded to the name of the hardware interface associated with the link (such as ne0). An example is

``fe80::1%ne0'', which means ``fe80::1 on the link associated with the

ne0 interface''.

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 ggeettaaddddrriinnffoo() is dynamically allo-

cated: 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 ggeettaaddddrriinnffoo() is released by the ffrreeeeaaddddrriinnffoo() func-

tion. The ai pointer should be a addrinfo structure created by a call to ggeettaaddddrriinnffoo().

RETURN VALUES

ggeettaaddddrriinnffoo() returns zero on success or one of the error codes listed in gaistrerror(3) if an error occurs. EEXXAAMMPPLLEESS The following code tries to connect to ``www.kame.net'' service ``http'' via a stream socket. It loops through all the addresses available, regardless of address family. If the destination resolves to an IPv4 address, it will use an AFINET socket. Similarly, if it resolves to IPv6, an AFINET6 socket is used. Observe that there is no hardcoded reference to a particular address family. The code works even if ggeettaaddddrriinnffoo() returns addresses that are not IPv4/v6. struct addrinfo hints, *res, *res0; int error; int s; const char *cause = NULL; memset(&hints, 0, sizeof(hints)); hints.aifamily = PFUNSPEC; hints.aisocktype = SOCKSTREAM; error = getaddrinfo("www.kame.net", "http", &hints, &res0); if (error) {

errx(1, "%s", gaistrerror(error));

/*NOTREACHED*/ }

s = -1;

for (res = res0; res; res = res->ainext) {

s = socket(res->aifamily, res->aisocktype,

res->aiprotocol);

if (s < 0) { cause = "socket"; continue; }

if (connect(s, res->aiaddr, res->aiaddrlen) < 0) {

cause = "connect"; close(s);

s = -1;

continue; } break; /* okay we got one */ } if (s < 0) {

err(1, "%s", cause);

/*NOTREACHED*/ }

freeaddrinfo(res0);

The following example tries to open a wildcard listening socket onto ser-

vice ``http'', for all the address families available. struct addrinfo hints, *res, *res0; int error; int s[MAXSOCK]; int nsock; const char *cause = NULL; memset(&hints, 0, sizeof(hints)); hints.aifamily = PFUNSPEC; hints.aisocktype = SOCKSTREAM; hints.aiflags = AIPASSIVE; error = getaddrinfo(NULL, "http", &hints, &res0); if (error) {

errx(1, "%s", gaistrerror(error));

/*NOTREACHED*/ } nsock = 0;

for (res = res0; res && nsock < MAXSOCK; res = res->ainext) {

s[nsock] = socket(res->aifamily, res->aisocktype,

res->aiprotocol);

if (s[nsock] < 0) { cause = "socket"; continue; }

if (bind(s[nsock], res->aiaddr, res->aiaddrlen) < 0) {

cause = "bind"; close(s[nsock]); continue; } (void) listen(s[nsock], 5); nsock++; } if (nsock == 0) {

err(1, "%s", cause);

/*NOTREACHED*/ }

freeaddrinfo(res0);

SEE ALSO

bind(2), connect(2), send(2), socket(2), gaistrerror(3), gethostbyname(3), getnameinfo(3), getservbyname(3), resolver(3), hosts(5), resolv.conf(5), services(5), hostname(7), named(8) R. Gilligan, S. Thomson, J. Bound, J. McCann, and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC 3493, February 2003. S. Deering, B. Haberman, T. Jinmei, E. Nordmark, and B. Zill, IPv6 Scoped

Address Architecture, internet draft, draft-ietf-ipv6-scoping-

arch-02.txt, work in progress material.

Craig Metz, "Protocol Independence Using the Sockets API", Proceedings of the freenix track: 2000 USENIX annual technical conference, June 2000. STANDARDS

The ggeettaaddddrriinnffoo() function is defined by the IEEE Std 1003.1g-2000

(``POSIX.1'') draft specification and documented in RFC 3493, ``Basic Socket Interface Extensions for IPv6''.

BUGS

The implementation of ggeettaaddddrriinnffoo() is not thread-safe.

BSD December 20, 2004 BSD




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™