1GETADDRINFO(3) BSD Library Functions Manual GETADDRINFO(3)
2
4 getaddrinfo freeaddrinfo, gai_strerror — nodename-to-address translation
5 in protocol-independent manner
6
8 #include <sys/socket.h>
9 #include <netdb.h>
10
11 int
12 getaddrinfo(const char *nodename, const char *servname,
13 const struct addrinfo *hints, struct addrinfo **res);
14
15 void
16 freeaddrinfo(struct addrinfo *ai);
17
18 char *
19 gai_strerror(int ecode);
20
22 The getaddrinfo() function is defined for protocol-independent nodename-
23 to-address translation. It performs functionality of gethostbyname(3)
24 and getservbyname(3), in more sophisticated manner.
25
26 The addrinfo structure is defined as a result of including the <netdb.h>
27 header:
28
29 struct addrinfo { *
30 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
31 int ai_family; /* PF_xxx */
32 int ai_socktype; /* SOCK_xxx */
33 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
34 size_t ai_addrlen; /* length of ai_addr */
35 char *ai_canonname; /* canonical name for nodename */
36 struct sockaddr *ai_addr; /* binary address */
37 struct addrinfo *ai_next; /* next structure in linked list */
38 };
39
40 The nodename and servname arguments are pointers to null-terminated
41 strings or NULL. One or both of these two arguments must be a non-NULL
42 pointer. In the normal client scenario, both the nodename and servname
43 are specified. In the normal server scenario, only the servname is spec‐
44 ified. A non-NULL nodename string can be either a node name or a numeric
45 host address string (i.e., a dotted-decimal IPv4 address or an IPv6 hex
46 address). A non-NULL servname string can be either a service name or a
47 decimal port number.
48
49 The caller can optionally pass an addrinfo structure, pointed to by the
50 third argument, to provide hints concerning the type of socket that the
51 caller supports. In this hints structure all members other than
52 ai_flags, ai_family, ai_socktype, and ai_protocol must be zero or a NULL
53 pointer. A value of PF_UNSPEC for ai_family means the caller will accept
54 any protocol family. A value of 0 for ai_socktype means the caller will
55 accept any socket type. A value of 0 for ai_protocol means the caller
56 will accept any protocol. For example, if the caller handles only TCP
57 and not UDP, then the ai_socktype member of the hints structure should be
58 set to SOCK_STREAM when getaddrinfo() is called. If the caller handles
59 only IPv4 and not IPv6, then the ai_family member of the hints structure
60 should be set to PF_INET when getaddrinfo() is called. If the third
61 argument to getaddrinfo() is a NULL pointer, this is the same as if the
62 caller had filled in an addrinfo structure initialized to zero with
63 ai_family set to PF_UNSPEC.
64
65 Upon successful return a pointer to a linked list of one or more addrinfo
66 structures is returned through the final argument. The caller can
67 process each addrinfo structure in this list by following the ai_next
68 pointer, until a NULL pointer is encountered. In each returned addrinfo
69 structure the three members ai_family, ai_socktype, and ai_protocol are
70 the corresponding arguments for a call to the socket() function. In each
71 addrinfo structure the ai_addr member points to a filled-in socket
72 address structure whose length is specified by the ai_addrlen member.
73
74 If the AI_PASSIVE bit is set in the ai_flags member of the hints struc‐
75 ture, then the caller plans to use the returned socket address structure
76 in a call to bind(). In this case, if the nodename argument is a NULL
77 pointer, then the IP address portion of the socket address structure will
78 be set to INADDR_ANY for an IPv4 address or IN6ADDR_ANY_INIT for an IPv6
79 address.
80
81 If the AI_PASSIVE bit is not set in the ai_flags member of the hints
82 structure, then the returned socket address structure will be ready for a
83 call to connect() (for a connection-oriented protocol) or either
84 connect(), sendto(), or sendmsg() (for a connectionless protocol). In
85 this case, if the nodename argument is a NULL pointer, then the IP
86 address portion of the socket address structure will be set to the loop‐
87 back address.
88
89 If the AI_CANONNAME bit is set in the ai_flags member of the hints struc‐
90 ture, then upon successful return the ai_canonname member of the first
91 addrinfo structure in the linked list will point to a null-terminated
92 string containing the canonical name of the specified nodename.
93
94 If the AI_NUMERICHOST bit is set in the ai_flags member of the hints
95 structure, then a non-NULL nodename string must be a numeric host address
96 string. Otherwise an error of EAI_NONAME is returned. This flag pre‐
97 vents any type of name resolution service (e.g., the DNS) from being
98 called.
99
100 All of the information returned by getaddrinfo() is dynamically allo‐
101 cated: the addrinfo structures, and the socket address structures and
102 canonical node name strings pointed to by the addrinfo structures. To
103 return this information to the system the function Fn freeaddrinfo is
104 called. The addrinfo structure pointed to by the ai argument is freed,
105 along with any dynamic storage pointed to by the structure. This opera‐
106 tion is repeated until a NULL ai_next pointer is encountered.
107
108 To aid applications in printing error messages based on the EAI_xxx codes
109 returned by getaddrinfo(), gai_strerror() is defined. The argument is
110 one of the EAI_xxx values defined earlier and the return value points to
111 a string describing the error. If the argument is not one of the EAI_xxx
112 values, the function still returns a pointer to a string whose contents
113 indicate an unknown error.
114
116 /etc/hosts
117 /etc/host.conf
118 /etc/resolv.conf
119
121 Error return status from getaddrinfo() is zero on success and non-zero on
122 errors. Non-zero error codes are defined in <netdb.h>, and as follows:
123
124 EAI_ADDRFAMILY address family for nodename not supported
125 EAI_AGAIN temporary failure in name resolution
126 EAI_BADFLAGS invalid value for ai_flags
127 EAI_FAIL non-recoverable failure in name resolution
128 EAI_FAMILY ai_family not supported
129 EAI_MEMORY memory allocation failure
130 EAI_NODATA no address associated with nodename
131 EAI_NONAME nodename nor servname provided, or not known
132 EAI_SERVICE servname not supported for ai_socktype
133 EAI_SOCKTYPE ai_socktype not supported
134 EAI_SYSTEM system error returned in errno
135
136 If called with proper argument, gai_strerror() returns a pointer to a
137 string describing the given error code. If the argument is not one of
138 the EAI_xxx values, the function still returns a pointer to a string
139 whose contents indicate an unknown error.
140
142 getnameinfo(3), gethostbyname(3), getservbyname(3), hosts(5),
143 services(5), hostname(7)
144
145 R. Gilligan, S. Thomson, J. Bound, and W. Stevens, ``Basic Socket Inter‐
146 face Extensions for IPv6,'' RFC2133, April 1997.
147
149 The implementation first appeared in WIDE Hydrangea IPv6 protocol stack
150 kit.
151
153 The getaddrinfo() function is defined IEEE POSIX 1003.1g draft specifica‐
154 tion, and documented in ``Basic Socket Interface Extensions for IPv6''
155 (RFC2133).
156
158 The text was shamelessly copied from RFC2133.
159
160KAME May 25, 1995 KAME