xref: /freebsd/lib/libc/net/getipnodebyname.3 (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1.\" Copyright (c) 1983, 1987, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     From: @(#)gethostbyname.3	8.4 (Berkeley) 5/25/95
33.\"     $Id: getipnodebyname.3,v 1.2 1999/09/13 16:04:51 itojun Exp $
34.\	$FreeBSD$
35.\"
36.Dd May 25, 1995
37.Dt GETIPNODEBYNAME 3
38.Os KAME
39.\"
40.Sh NAME
41.Nm getipnodebyname ,
42.Nm getipnodebyaddr ,
43.Nm freehostent
44.Nd nodename-to-address and address-to-nodename translation
45.\"
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.Fd #include <sys/types.h>
50.Fd #include <sys/socket.h>
51.Fd #include <netdb.h>
52.Ft "struct hostent *"
53.Fn getipnodebyname "const char *name" "int af" "int flags" "int *error_num"
54.Ft "struct hostent *"
55.Fn getipnodebyaddr "const void *src" "size_t len" "int af" "int *error_num"
56.Ft void
57.Fn freehostent "struct hostent *ptr"
58.\"
59.Sh DESCRIPTION
60.Fn getipnodebyname
61and
62.Fn getipnodebyaddr
63functions are very similar to
64.Xr gethostbyname 3 ,
65.Xr gethostbyname2 3
66and
67.Xr gethostbyaddr 3 .
68The functions cover all the functionalities provided by the older ones,
69and provide better interface to programmers.
70The functions require additional arguments,
71.Ar af ,
72and
73.Ar flags ,
74for specifying address family and operation mode.
75The additional arguments allow programmer to get address for a nodename,
76for specific address family
77.Po
78such as
79.Dv AF_INET
80or
81.Dv AF_INET6
82.Pc .
83The functions also require an additional pointer argument,
84.Ar error_num
85to return the appropriate error code,
86to support thread safe error code returns.
87.Pp
88The type and usage of the return value,
89.Li "struct hostent"
90is described in
91.Xr gethostbyname 3 .
92.Pp
93For
94.Fn getipnodebyname ,
95the
96.Ar name
97argument can be either a node name or a numeric address
98string
99.Po
100i.e., a dotted-decimal IPv4 address or an IPv6 hex address
101.Pc .
102The
103.Ar af
104argument specifies the address family, either
105.Dv AF_INET
106or
107.Dv AF_INET6 .
108The
109.Ar flags
110argument specifies the types of addresses that are searched for,
111and the types of addresses that are returned.
112We note that a special flags value of
113.Dv AI_DEFAULT
114.Pq defined below
115should handle most applications.
116That is, porting simple applications to use IPv6 replaces the call
117.Bd -literal -offset
118   hptr = gethostbyname(name);
119.Ed
120.Pp
121with
122.Bd -literal -offset
123   hptr = getipnodebyname(name, AF_INET6, AI_DEFAULT, &error_num);
124.Ed
125.Pp
126Applications desiring finer control over the types of addresses
127searched for and returned, can specify other combinations of the
128.Ar flags
129argument.
130.Pp
131A
132.Ar flags
133of
134.Li 0
135implies a strict interpretation of the
136.Ar af
137argument:
138.Bl -bullet
139.It
140If
141.Ar flags
142is 0 and
143.Ar af
144is
145.Dv AF_INET ,
146then the caller wants only IPv4 addresses.
147A query is made for
148.Li A
149records.
150If successful, the IPv4 addresses are returned and the
151.Li h_length
152member of the
153.Li hostent
154structure will be 4, else the function returns a
155.Dv NULL
156pointer.
157.It
158If
159.Ar flags
160is 0 and if
161.Ar af
162is
163.Li AF_INET6 ,
164then the caller wants only IPv6 addresses.
165A query is made for
166.Li AAAA
167records.
168If successful, the IPv6 addresses are returned and the
169.Li h_length
170member of the
171.Li hostent
172structure will be 16, else the function returns a
173.Dv NULL
174pointer.
175.El
176.Pp
177Other constants can be logically-ORed into the
178.Ar flags
179argument, to modify the behavior of the function.
180.Bl -bullet
181.It
182If the
183.Dv AI_V4MAPPED
184flag is specified along with an
185.Ar af
186of
187.Dv AF_INET6 ,
188then the caller will accept IPv4-mapped IPv6 addresses.
189That is, if no
190.Li AAAA
191records are found then a query is made for
192.Li A
193records and any found are returned as IPv4-mapped IPv6 addresses
194.Po
195.Li h_length
196will be 16
197.Pc .
198The
199.Dv AI_V4MAPPED
200flag is ignored unless
201.Ar af
202equals
203.Dv AF_INET6 .
204.It
205The
206.Dv AI_V4MAPPED_CFG
207flag is exact same as the
208.Dv AI_V4MAPPED
209flag only if the kernel supports IPv4-mapped IPv6 address.
210.It
211If the
212.Dv AI_ALL
213flag is used in conjunction with the
214.Dv AI_V4MAPPED
215flag, and only used with the IPv6 address family.
216When
217.Dv AI_ALL
218is logically or'd with
219.Dv AI_V4MAPPED
220flag then the caller wants all addresses: IPv6 and IPv4-mapped IPv6.
221A query is first made for
222.Li AAAA
223records and if successful, the
224IPv6 addresses are returned.  Another query is then made for
225.Li A
226records and any found are returned as IPv4-mapped IPv6 addresses.
227.Li h_length
228will be 16.  Only if both queries fail does the function
229return a
230.Dv NULL
231pointer.  This flag is ignored unless af equals
232AF_INET6.  If both
233.Dv AI_ALL
234and
235.Dv AI_V4MAPPED
236are specified,
237.Dv AI_ALL
238takes precedence.
239.It
240The
241.Dv AI_ADDRCONFIG
242flag specifies that a query for
243.Li AAAA
244records
245should occur only if the node has at least one IPv6 source
246address configured and a query for
247.Li A
248records should occur only if the node has at least one IPv4 source address
249configured.
250.Pp
251For example, if the node has no IPv6 source addresses configured,
252and
253.Ar af
254equals AF_INET6, and the node name being looked up has both
255.Li AAAA
256and
257.Li A
258records, then:
259(a) if only
260.Dv AI_ADDRCONFIG
261is
262specified, the function returns a
263.Dv NULL
264pointer;
265(b) if
266.Dv AI_ADDRCONFIG
267|
268.Dv AI_V4MAPPED
269is specified, the
270.Li A
271records are returned as IPv4-mapped IPv6 addresses;
272.El
273.Pp
274The special flags value of
275.Dv AI_DEFAULT
276is defined as
277.Bd -literal -offset
278   #define  AI_DEFAULT  (AI_V4MAPPED_CFG | AI_ADDRCONFIG)
279.Ed
280.Pp
281We noted that the
282.Fn getipnodebyname
283function must allow the
284.Ar name
285argument to be either a node name or a literal address string
286.Po
287i.e., a dotted-decimal IPv4 address or an IPv6 hex address
288.Pc .
289This saves applications from having to call
290.Xr inet_pton 3
291to handle literal address strings.
292When the
293.Ar name
294argument is a literal address string,
295the
296.Ar flags
297argument is always ignored.
298.Pp
299There are four scenarios based on the type of literal address string
300and the value of the
301.Ar af
302argument.
303The two simple cases are when
304.Ar name
305is a dotted-decimal IPv4 address and
306.Ar af
307equals
308.Dv AF_INET ,
309or when
310.Ar name
311is an IPv6 hex address and
312.Ar af
313equals
314.Dv AF_INET6 .
315The members of the
316returned hostent structure are:
317.Li h_name
318points to a copy of the
319.Ar name
320argument,
321.Li h_aliases
322is a
323.Dv NULL
324pointer,
325.Li h_addrtype
326is a copy of the
327.Ar af
328argument,
329.Li h_length
330is either 4
331.Po
332for
333.Dv AF_INET
334.Pc
335or 16
336.Po
337for
338.Dv AF_INET6
339.Pc ,
340.Li h_addr_list[0]
341is a pointer to the 4-byte or 16-byte binary address,
342and
343.Li h_addr_list[1]
344is a
345.Dv NULL
346pointer.
347.Pp
348When
349.Ar name
350is a dotted-decimal IPv4 address and
351.Ar af
352equals
353.Dv AF_INET6 ,
354and
355.Dv AI_V4MAPPED
356is specified,
357an IPv4-mapped IPv6 address is returned:
358.Li h_name
359points to an IPv6 hex address containing the IPv4-mapped IPv6 address,
360.Li h_aliases
361is a
362.Dv NULL
363pointer,
364.Li h_addrtype
365is
366.Dv AF_INET6 ,
367.Li h_length
368is 16,
369.Li h_addr_list[0]
370is a pointer to the 16-byte binary address, and
371.Li h_addr_list[1]
372is a
373.Dv NULL
374pointer.
375.Pp
376It is an error when
377.Ar name
378is an IPv6 hex address and
379.Ar af
380equals
381.Dv AF_INET .
382The function's return value is a
383.Dv NULL
384pointer and the value pointed to by
385.Ar error_num
386equals
387.Dv HOST_NOT_FOUND .
388.Pp
389.Fn getipnodebyaddr
390takes almost the same argument as
391.Xr gethostbyaddr 3 ,
392but adds a pointer to return an error number.
393Additionally it takes care of IPv4-mapped IPv6 addresses,
394and IPv4-compatible IPv6 addresses.
395.Pp
396.Fn getipnodebyname
397and
398.Fn getipnodebyaddr
399dynamically allocate the structure to be returned to the caller.
400.Fn freehostent
401reclaims memory region allocated and returned by
402.Fn getipnodebyname
403or
404.Fn getipnodebyaddr .
405.\"
406.Sh FILES
407.Bl -tag -width /etc/resolv.conf -compact
408.It Pa /etc/hosts
409.It Pa /etc/host.conf
410.It Pa /etc/resolv.conf
411.El
412.\"
413.Sh DIAGNOSTICS
414.Nm getipnodebyname
415and
416.Nm getipnodebyaddr
417returns
418.Dv NULL
419on errors.
420The integer values pointed to by
421.Ar error_num
422may then be checked to see whether this is a temporary failure
423or an invalid or unknown host.
424The meanings of each error code are described in
425.Xr gethostbyname 3 .
426.\"
427.Sh SEE ALSO
428.Xr gethostbyname 3 ,
429.Xr gethostbyaddr 3 ,
430.Xr hosts 5 ,
431.Xr services 5 ,
432.Xr hostname 7 ,
433.Xr named 8
434.Pp
435R. Gilligan, S.  Thomson, J. Bound, and W. Stevens,
436``Basic Socket Interface Extensions for IPv6,'' RFC2553, March 1999.
437.\"
438.Sh HISTORY
439The implementation first appeared in KAME advanced networking kit.
440.\"
441.Sh STANDARDS
442.Fn getipnodebyname
443and
444.Fn getipnodebyaddr
445are documented in ``Basic Socket Interface Extensions for IPv6''
446.Pq RFC2553 .
447.\"
448.Sh BUGS
449The text was shamelessly copied from RFC2553.
450