2 .\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH GETIPNODEBYNAME 3SOCKET "Sep 10, 2013"
8 getipnodebyname, getipnodebyaddr, freehostent \- get IP node entry
12 \fBcc\fR [ \fIflag\fR... ] \fIfile\fR... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR... ]
13 #include <sys/socket.h>
16 \fBstruct hostent *\fR\fBgetipnodebyname\fR(\fBconst char *\fR\fIname\fR, \fBint\fR \fIaf\fR,
17 \fBint\fR \fIflags\fR, \fBint *\fR\fIerror_num\fR);
22 \fBstruct hostent *\fR\fBgetipnodebyaddr\fR(\fBconst void *\fR\fIsrc\fR, \fBsize_t\fR \fIlen\fR,
23 \fBint\fR \fIaf\fR, \fBint *\fR\fIerror_num\fR);
28 \fBvoid\fR \fBfreehostent\fR(\fBstruct hostent *\fR\fIptr\fR);
92 Pointer to \fBhostent\fR structure
98 The \fBgetipnodebyname()\fR function searches the \fBipnodes\fR database from
99 the beginning. The function finds the first \fBh_name\fR member that matches
100 the hostname specified by \fIname\fR. The function takes an \fIaf\fR argument
101 that specifies the address family. The address family can be \fBAF_INET\fR for
102 IPv4 addresses or \fBAF_INET6\fR for IPv6 addresses. The \fIflags\fR argument
103 determines what results are returned based on the value of \fIflags\fR. If the
104 \fIflags\fR argument is set to \fB0\fR (zero), the default operation of the
105 function is specified as follows:
110 If the \fIaf\fR argument is \fBAF_INET\fR, a query is made for an IPv4 address.
111 If successful, IPv4 addresses are returned and the \fBh_length\fR member of the
112 \fBhostent\fR structure is 4. Otherwise, the function returns a \fINULL\fR
119 If the \fIaf\fR argument is \fBAF_INET6\fR, a query is made for an IPv6
120 address. If successful, IPv6 addresses are returned and the \fBh_length\fR
121 member of the \fBhostent\fR structure is 16. Otherwise, the function returns a
126 The \fIflags\fR argument changes the default actions of the function. Set the
127 \fIflags\fR argument with a logical \fBOR\fR operation on any of combination of
128 the following values:
149 The special flags value, \fBAI_DEFAULT\fR, should handle most applications.
150 Porting simple applications to use IPv6 replaces the call
154 hptr = gethostbyname(name);
164 hptr = getipnodebyname(name, AF_INET6, AI_DEFAULT, &error_num);
170 The \fIflags\fR value \fB0\fR (zero) implies a strict interpretation of the
176 If \fIflags\fR is \fB0\fR and \fIaf\fR is \fBAF_INET\fR, the caller wants only
177 IPv4 addresses. A query is made for \fBA\fR records. If successful, IPv4
178 addresses are returned and the \fBh_length\fR member of the \fBhostent\fR
179 structure is 4. Otherwise, the function returns a \fINULL\fR pointer.
185 If \fIflags\fR is \fB0\fR and \fIaf\fR is \fBAF_INET6\fR, the caller wants only
186 IPv6 addresses. A query is made for \fBAAAA\fR records. If successful, IPv6
187 addresses are returned and the \fBh_length\fR member of the \fBhostent\fR
188 structure is 16. Otherwise, the function returns a \fINULL\fR pointer.
192 Logically \fBOR\fR other constants into the \fIflags\fR argument to modify the
193 behavior of the \fBgetipnodebyname()\fR function.
198 If the \fBAI_V4MAPPED\fR flag is specified with \fIaf\fR set to \fBAF_INET6\fR,
199 the caller can accept IPv4-mapped IPv6 addresses. If no \fBAAAA\fR records are
200 found, a query is made for \fBA\fR records. Any \fBA\fR records found are
201 returned as IPv4-mapped IPv6 addresses and the \fBh_length\fR is 16. The
202 \fBAI_V4MAPPED\fR flag is ignored unless \fIaf\fR equals \fBAF_INET6\fR.
208 The \fBAI_ALL\fR flag is used in conjunction with the \fBAI_V4MAPPED\fR flag,
209 exclusively with the IPv6 address family. When \fBAI_ALL\fR is logically
210 \fBOR\fRed with \fBAI_V4MAPPED\fR flag, the caller wants all addresses: IPv6
211 and IPv4-mapped IPv6 addresses. A query is first made for \fBAAAA\fR records
212 and, if successful, IPv6 addresses are returned. Another query is then made for
213 \fBA\fR records. Any \fBA\fR records found are returned as IPv4-mapped IPv6
214 addresses and the \fBh_length\fR is 16. Only when both queries fail does the
215 function return a \fINULL\fR pointer. The \fBAI_ALL\fR flag is ignored unless
216 \fIaf\fR is set to \fBAF_INET6\fR.
222 The \fBAI_ADDRCONFIG\fR flag specifies that a query for \fBAAAA\fR records
223 should occur only when the node is configured with at least one IPv6 source
224 address. A query for \fBA\fR records should occur only when the node is
225 configured with at least one IPv4 source address. For example, if a node is
226 configured with no IPv6 source addresses, \fIaf\fR equals \fBAF_INET6\fR, and
227 the node name queried has both \fBAAAA\fR and \fBA\fR records, then:
232 A \fINULL\fR pointer is returned when only the \fBAI_ADDRCONFIG\fR value is
239 The \fBA\fR records are returned as IPv4-mapped IPv6 addresses when the
240 \fBAI_ADDRCONFIG\fR and \fBAI_V4MAPPED\fR values are specified.
245 The special flags value, \fBAI_DEFAULT\fR, is defined as
249 #define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG)
255 The \fBgetipnodebyname()\fR function allows the \fIname\fR argument to be a
256 node name or a literal address string: a dotted-decimal IPv4 address or an IPv6
257 hex address. Applications do not have to call \fBinet_pton\fR(3SOCKET) to
258 handle literal address strings.
261 Four scenarios arise based on the type of literal address string and the value
262 of the \fIaf\fR argument. The two simple cases occur when \fIname\fR is a
263 dotted-decimal IPv4 address and \fIaf\fR equals \fBAF_INET\fR and when
264 \fIname\fR is an IPv6 hex address and \fIaf\fR equals \fBAF_INET6\fR. The
265 members of the returned \fBhostent\fR structure are:
272 Pointer to a copy of the name argument
278 \fB\fBh_aliases\fR\fR
287 \fB\fBh_addrtype\fR\fR
290 Copy of the \fIaf\fR argument.
299 4 for \fBAF_INET\fR or 16 for \fBAF_INET6\fR.
305 \fB\fBh_addr_list\fR\fR
308 Array of pointers to 4-byte or 16-byte binary addresses. The array is
309 terminated by a \fINULL\fR pointer.
315 Upon successful completion, \fBgetipnodebyname()\fR and \fBgetipnodebyaddr()\fR
316 return a \fBhostent\fR structure. Otherwise they return \fINULL\fR.
319 The \fBhostent\fR structure does not change from the existing definition when
320 used with \fBgethostbyname\fR(3NSL). For example, host entries are represented
321 by the \fBstruct hostent\fR structure defined in <\fBnetdb.h\fR>:
326 char *h_name; /* canonical name of host */
327 char **h_aliases; /* alias list */
328 int h_addrtype; /* host address type */
329 int h_length; /* length of address */
330 char **h_addr_list; /* list of addresses */
337 An error occurs when \fIname\fR is an IPv6 hex address and \fIaf\fR equals
338 \fBAF_INET\fR. The return value of the function is a \fINULL\fR pointer and
339 \fBerror_num\fR equals \fBHOST_NOT_FOUND\fR.
342 The \fBgetipnodebyaddr()\fR function has the same arguments as the existing
343 \fBgethostbyaddr\fR(3NSL) function, but adds an error number. As with
344 \fBgetipnodebyname()\fR, \fBgetipnodebyaddr()\fR is thread-safe. The
345 \fBerror_num\fR value is returned to the caller with the appropriate error code
346 to support thread-safe error code returns. The following error conditions can
347 be returned for \fBerror_num\fR:
351 \fB\fBHOST_NOT_FOUND\fR\fR
363 No address is available for the \fIname\fR specified in the server request.
364 This error is not a soft error. Another type of \fIname\fR server request might
371 \fB\fBNO_RECOVERY\fR\fR
374 An unexpected server failure occurred, which is a non-recoverable error.
380 \fB\fBTRY_AGAIN\fR\fR
383 This error is a soft error that indicates that the local server did not receive
384 a response from an authoritative server. A retry at some later time might be
390 One possible source of confusion is the handling of IPv4-mapped IPv6 addresses
391 and IPv4-compatible IPv6 addresses, but the following logic should apply:
395 If \fIaf\fR is \fBAF_INET6\fR, and if \fIlen\fR equals 16, and if the IPv6
396 address is an IPv4-mapped IPv6 address or an IPv4-compatible IPv6 address, then
397 skip over the first 12 bytes of the IPv6 address, set \fIaf\fR to
398 \fBAF_INET\fR, and set \fIlen\fR to 4.
403 If \fIaf\fR is \fBAF_INET\fR, lookup the \fIname\fR for the given IPv4
409 If \fIaf\fR is \fBAF_INET6\fR, lookup the \fIname\fR for the given IPv6
415 If the function is returning success, then the single address that is
416 returned in the \fBhostent\fR structure is a copy of the first argument to the
417 function with the same address family that was passed as an argument to this
422 All four steps listed are performed in order.
425 This structure, and the information pointed to by this structure, are
426 dynamically allocated by \fBgetipnodebyname()\fR and \fBgetipnodebyaddr()\fR.
427 The \fBfreehostent()\fR function frees this memory.
430 \fBExample 1 \fRGetting the Canonical Name, Aliases, and Internet IP Addresses
434 The following is a sample program that retrieves the canonical name, aliases,
435 and all Internet IP addresses, both version 6 and version 4, for a given
443 #include <sys/types.h>
444 #include <sys/socket.h>
445 #include <netinet/in.h>
446 #include <arpa/inet.h>
449 main(int argc, const char **argv)
451 char abuf[INET6_ADDRSTRLEN];
457 (void) printf("usage: %s hostname\en", argv[0]);
461 /* argv[1] can be a pointer to a hostname or literal IP address */
462 hp = getipnodebyname(argv[1], AF_INET6, AI_ALL | AI_ADDRCONFIG |
463 AI_V4MAPPED, &error_num);
465 if (error_num == TRY_AGAIN) {
466 printf("%s: unknown host or invalid literal address "
467 "(try again later)\en", argv[1]);
469 printf("%s: unknown host or invalid literal address\en",
474 for (p = hp->h_addr_list; *p != 0; p++) {
478 bcopy(*p, (caddr_t)&in6, hp->h_length);
479 (void) printf("%s\t%s", inet_ntop(AF_INET6, (void *)&in6,
480 abuf, sizeof(abuf)), hp->h_name);
481 for (q = hp->h_aliases; *q != 0; q++)
482 (void) printf(" %s", *q);
483 (void) putchar('\en');
494 See \fBattributes\fR(5) for descriptions of the following attributes:
502 ATTRIBUTE TYPE ATTRIBUTE VALUE
504 Interface Stability Committed
512 \fBgetaddrinfo\fR(3SOCKET), \fBgethostbyname\fR(3NSL), \fBhtonl\fR(3SOCKET),
513 \fBinet\fR(3SOCKET), \fBnetdb.h\fR(3HEAD), \fBhosts\fR(4),
514 \fBnsswitch.conf\fR(4), \fBattributes\fR(5)
518 No enumeration functions are provided for IPv6. Existing enumeration functions
519 such as \fBsethostent\fR(3NSL) do not work in combination with the
520 \fBgetipnodebyname()\fR and \fBgetipnodebyaddr()\fR functions.
523 All the functions that return a \fBstruct hostent\fR must always return the
524 canonical in the \fBh_name\fR field. This name, by definition, is the
525 well-known and official hostname shared between all aliases and all addresses.
526 The underlying source that satisfies the request determines the mapping of the
527 input name or address into the set of names and addresses in \fBhostent\fR.
528 Different sources might make such as determination in different ways. If more
529 than one alias and more than one address in \fBhostent\fR exist, no pairing is
530 implied between the alias and address.
533 The current implementations of these functions return or accept only addresses
534 for the Internet address family (type \fBAF_INET\fR) or the Internet address
535 family Version 6 (type \fBAF_INET6\fR).
538 IPv4-mapped addresses are not recommended. The \fBgetaddrinfo\fR(3SOCKET)
539 function is preferred over \fBgetipnodebyaddr()\fR because it allows
540 applications to lookup IPv4 and IPv6 addresses without relying on IPv4-mapped
544 The form for an address of type \fBAF_INET\fR is a \fBstruct in_addr\fR defined
545 in <\fBnetinet/in.h\fR>. The form for an address of type \fBAF_INET6\fR is a
546 \fBstruct in6_addr\fR, also defined in <\fBnetinet/in.h\fR>. The functions
547 described in \fBinet_ntop\fR(3SOCKET) and \fBinet_pton\fR(3SOCKET) that are
548 illustrated in the EXAMPLES section are helpful in constructing and
549 manipulating addresses in either of these forms.