1getaddrinfo(3) Linux Programmer's Manual getaddrinfo(3)
2
3
4
6 getaddrinfo, freeaddrinfo, gai_strerror - network address and service
7 translation
8
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netdb.h>
13
14 int getaddrinfo(const char *node, const char *service,
15 const struct addrinfo *hints,
16 struct addrinfo **res);
17
18 void freeaddrinfo(struct addrinfo *res);
19
20 const char *gai_strerror(int errcode);
21
23 The getaddrinfo(3) function combines the functionality provided by the
24 getipnodebyname(3), getipnodebyaddr(3), getservbyname(3), and get‐
25 servbyport(3) functions into a single interface. The thread-safe
26 getaddrinfo(3) function creates one or more socket address structures
27 that can be used by the bind(2) and connect(2) system calls to create a
28 client or a server socket.
29
30 The getaddrinfo(3) function is not limited to creating IPv4 socket
31 address structures; IPv6 socket address structures can be created if
32 IPv6 support is available. These socket address structures can be used
33 directly by bind(2) or connect(2), to prepare a client or a server
34 socket.
35
36 The addrinfo structure used by this function contains the following
37 members:
38
39 struct addrinfo {
40 int ai_flags;
41 int ai_family;
42 int ai_socktype;
43 int ai_protocol;
44 size_t ai_addrlen;
45 struct sockaddr *ai_addr;
46 char *ai_canonname;
47 struct addrinfo *ai_next;
48 };
49
50 getaddrinfo(3) sets res to point to a dynamically-allocated linked list
51 of addrinfo structures, linked by the ai_next member. There are sev‐
52 eral reasons why the linked list may have more than one addrinfo struc‐
53 ture, including: if the network host is multi-homed; or if the same
54 service is available from multiple socket protocols (one SOCK_STREAM
55 address and another SOCK_DGRAM address, for example).
56
57 The members ai_family, ai_socktype, and ai_protocol have the same mean‐
58 ing as the corresponding parameters in the socket(2) system call. The
59 getaddrinfo(3) function returns socket addresses in either IPv4 or IPv6
60 address family, (ai_family will be set to either AF_INET or AF_INET6).
61
62 The hints parameter specifies the preferred socket type, or protocol.
63 A NULL hints specifies that any network address or protocol is accept‐
64 able. If this parameter is not NULL it points to an addrinfo structure
65 whose ai_family, ai_socktype, and ai_protocol members specify the pre‐
66 ferred socket type. AF_UNSPEC in ai_family specifies any protocol fam‐
67 ily (either IPv4 or IPv6, for example). 0 in ai_socktype or ai_proto‐
68 col specifies that any socket type or protocol is acceptable as well.
69 The ai_flags member specifies additional options, defined below. Mul‐
70 tiple flags are specified by logically OR-ing them together. All the
71 other members in the hints parameter must contain either 0, or a null
72 pointer.
73
74 The node or service parameter, but not both, may be NULL. node speci‐
75 fies either a numerical network address (dotted-decimal format for
76 IPv4, hexadecimal format for IPv6) or a network hostname, whose network
77 addresses are looked up and resolved. If hints.ai_flags contains the
78 AI_NUMERICHOST flag then the node parameter must be a numerical network
79 address. The AI_NUMERICHOST flag suppresses any potentially lengthy
80 network host address lookups.
81
82 The getaddrinfo(3) function creates a linked list of addrinfo struc‐
83 tures, one for each network address subject to any restrictions imposed
84 by the hints parameter. The ai_canonname field of the first of these
85 addrinfo structures is set to point to the official name of the host,
86 if hints.ai_flags includes the AI_CANONNAME flag. ai_family, ai_sock‐
87 type, and ai_protocol specify the socket creation parameters. A
88 pointer to the socket address is placed in the ai_addr member, and the
89 length of the socket address, in bytes, is placed in the ai_addrlen
90 member.
91
92 If node is NULL, the network address in each socket structure is ini‐
93 tialized according to the AI_PASSIVE flag, which is set in
94 hints.ai_flags. The network address in each socket structure will be
95 left unspecified if AI_PASSIVE flag is set. This is used by server
96 applications, which intend to accept client connections on any network
97 address. The network address will be set to the loopback interface
98 address if the AI_PASSIVE flag is not set. This is used by client
99 applications, which intend to connect to a server running on the same
100 network host.
101
102 If hints.ai_flags includes the AI_ADDRCONFIG flag, then IPv4 addresses
103 are returned in the list pointed to by result only if the local system
104 has at least one IPv4 address configured, and IPv6 addresses are only
105 returned if the local system has at least one IPv6 address configured.
106
107 If hint.ai_flags specifies the AI_V4MAPPED flag, and hints.ai_family
108 was specified as AF_INET6, and no matching IPv6 addresses could be
109 found, then return IPv4-mapped IPv6 addresses in the list pointed to by
110 result. If both AI_V4MAPPED and AI_ALL are specified in hints.ai_fam‐
111 ily, then return both IPv6 and IPv4-mapped IPv6 addresses in the list
112 pointed to by result. AI_ALL is ignored if AI_V4MAPPED is not also
113 specified.
114
115 service sets the port number in the network address of each socket
116 structure. If service is NULL the port number will be left uninitial‐
117 ized. If AI_NUMERICSERV is specified in hints.ai_flags and service is
118 not NULL, then service must point to a string containing a numeric port
119 number. This flag is used to inhibit the invocation of a name resolu‐
120 tion service in cases where it is known not to be required.
121
122 The freeaddrinfo(3) function frees the memory that was allocated for
123 the dynamically allocated linked list res.
124
125 Extensions to getaddrinfo() for Internationalized Domain Names
126 Starting with glibc 2.3.4, getaddrinfo() has been extended to selec‐
127 tively allow the incoming and outgoing host names to be transparently
128 converted to and from the Internationalized Domain Name (IDN) format
129 (see RFC 3490, Internationalizing Domain Names in Applications (IDNA)).
130 Four new flags are defined:
131
132 AI_IDN If this flag is specified, then the node name given in node is
133 converted to IDN format if necessary. The source encoding is
134 that of the current locale.
135
136 If the input name contains non-ASCII characters, then the IDN
137 encoding is used. Those parts of the node name (delimited by
138 dots) that contain non-ASCII characters are encoded using ASCII
139 Compatible Encoding (ACE) before being passed to the name reso‐
140 lution functions.
141
142 AI_CANONIDN
143 After a successful name lookup, and if the AI_CANONNAME flag was
144 specified, getaddrinfo() will return the canonical name of the
145 node corresponding to the addrinfo structure value passed back.
146 The return value is an exact copy of the value returned by the
147 name resolution function.
148
149 If the name is encoded using ACE, then it will contain the xn--
150 prefix for one or more components of the name. To convert these
151 components into a readable form the AI_CANONIDN flag can be
152 passed in addition to AI_CANONNAME. The resulting string is
153 encoded using the current locale's encoding.
154
155 AI_IDN_ALLOW_UNASSIGNED, AI_IDN_USE_STD3_ASCII_RULES
156 Setting these flags will enable the IDNA_ALLOW_UNASSIGNED (allow
157 unassigned Unicode code points) and IDNA_USE_STD3_ASCII_RULES
158 (check output to make sure it is a STD3 conforming host name)
159 flags respectively to be used in the IDNA handling.
160
162 getaddrinfo(3) returns 0 if it succeeds, or one of the following non-
163 zero error codes:
164
165 EAI_ADDRFAMILY
166 The specified network host does not have any network addresses
167 in the requested address family.
168
169 EAI_AGAIN
170 The name server returned a temporary failure indication. Try
171 again later.
172
173 EAI_BADFLAGS
174 ai_flags contains invalid flags.
175
176 EAI_FAIL
177 The name server returned a permanent failure indication.
178
179 EAI_FAMILY
180 The requested address family is not supported at all.
181
182 EAI_MEMORY
183 Out of memory.
184
185 EAI_NODATA
186 The specified network host exists, but does not have any network
187 addresses defined.
188
189 EAI_NONAME
190 The node or service is not known; or both node and service are
191 NULL; or AI_NUMERICSERV was specified in hints.ai_flags and ser‐
192 vice was not a numeric port-number string.
193
194 EAI_SERVICE
195 The requested service is not available for the requested socket
196 type. It may be available through another socket type.
197
198 EAI_SOCKTYPE
199 The requested socket type is not supported at all.
200
201 EAI_SYSTEM
202 Other system error, check errno for details.
203
204 The gai_strerror(3) function translates these error codes to a human
205 readable string, suitable for error reporting.
206
208 POSIX.1-2001. The getaddrinfo() function is documented in RFC 2553.
209
211 AI_ADDRCONFIG, AI_ALL, and AI_V4MAPPED are available since glibc 2.3.3.
212 AI_NUMERICSERV is available since glibc 2.3.4.
213
215 getipnodebyaddr(3), getipnodebyname(3)
216
217
218
219Linux Man Page 2000-12-18 getaddrinfo(3)