xref: /freebsd-14.2/lib/libc/net/resolver.3 (revision 5a70219b)
1.\" Copyright (c) 1985, 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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)resolver.3	8.1 (Berkeley) 6/4/93
29.\"
30.Dd January 8, 2024
31.Dt RESOLVER 3
32.Os
33.Sh NAME
34.Nm res_query ,
35.Nm res_search ,
36.Nm res_mkquery ,
37.Nm res_send ,
38.Nm res_init ,
39.Nm dn_comp ,
40.Nm dn_expand ,
41.Nm dn_skipname ,
42.Nm ns_get16 ,
43.Nm ns_get32 ,
44.Nm ns_put16 ,
45.Nm ns_put32
46.Nd resolver routines
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In resolv.h
51.Ft int
52.Fo res_query
53.Fa "const char *dname"
54.Fa "int class"
55.Fa "int type"
56.Fa "u_char *answer"
57.Fa "int anslen"
58.Fc
59.Ft int
60.Fo res_search
61.Fa "const char *dname"
62.Fa "int class"
63.Fa "int type"
64.Fa "u_char *answer"
65.Fa "int anslen"
66.Fc
67.Ft int
68.Fo res_mkquery
69.Fa "int op"
70.Fa "const char *dname"
71.Fa "int class"
72.Fa "int type"
73.Fa "const u_char *data"
74.Fa "int datalen"
75.Fa "const u_char *newrr_in"
76.Fa "u_char *buf"
77.Fa "int buflen"
78.Fc
79.Ft int
80.Fo res_send
81.Fa "const u_char *msg"
82.Fa "int msglen"
83.Fa "u_char *answer"
84.Fa "int anslen"
85.Fc
86.Ft int
87.Fn res_init void
88.Ft int
89.Fo dn_comp
90.Fa "const char *exp_dn"
91.Fa "u_char *comp_dn"
92.Fa "int length"
93.Fa "u_char **dnptrs"
94.Fa "u_char **lastdnptr"
95.Fc
96.Ft int
97.Fo dn_expand
98.Fa "const u_char *msg"
99.Fa "const u_char *eomorig"
100.Fa "const u_char *comp_dn"
101.Fa "char *exp_dn"
102.Fa "int length"
103.Fc
104.Ft int
105.Fn dn_skipname "const u_char *comp_dn" "const u_char *eom"
106.Ft u_int
107.Fn ns_get16 "const u_char *src"
108.Ft u_long
109.Fn ns_get32 "const u_char *src"
110.Ft void
111.Fn ns_put16 "u_int src" "u_char *dst"
112.Ft void
113.Fn ns_put32 "u_long src" "u_char *dst"
114.Sh DESCRIPTION
115These routines are used for making, sending and interpreting
116query and reply messages with Internet domain name servers.
117.Pp
118Global configuration and state information that is used by the
119resolver routines is kept in the structure
120.Va _res .
121Most of the values have reasonable defaults and can be ignored.
122Options
123stored in
124.Va _res.options
125are defined in
126.In resolv.h
127and are as follows.
128Options are stored as a simple bit mask containing the bitwise ``or''
129of the options enabled.
130.Bl -tag -width RES_USE_INET6
131.It Dv RES_INIT
132True if the initial name server address and default domain name are
133initialized (i.e.,
134.Fn res_init
135has been called).
136.It Dv RES_DEBUG
137Print debugging messages.
138.It Dv RES_AAONLY
139Accept authoritative answers only.
140With this option,
141.Fn res_send
142should continue until it finds an authoritative answer or finds an error.
143Currently this is not implemented.
144.It Dv RES_USEVC
145Use
146.Tn TCP
147connections for queries instead of
148.Tn UDP
149datagrams.
150.It Dv RES_STAYOPEN
151Used with
152.Dv RES_USEVC
153to keep the
154.Tn TCP
155connection open between
156queries.
157This is useful only in programs that regularly do many queries.
158.Tn UDP
159should be the normal mode used.
160.It Dv RES_IGNTC
161Unused currently (ignore truncation errors, i.e., do not retry with
162.Tn TCP ) .
163.It Dv RES_RECURSE
164Set the recursion-desired bit in queries.
165This is the default.
166.Pf ( Fn res_send
167does not do iterative queries and expects the name server
168to handle recursion.)
169.It Dv RES_DEFNAMES
170If set,
171.Fn res_search
172will append the default domain name to single-component names
173(those that do not contain a dot).
174This option is enabled by default.
175.It Dv RES_DNSRCH
176If this option is set,
177.Fn res_search
178will search for host names in the current domain and in parent domains; see
179.Xr hostname 7 .
180This is used by the standard host lookup routine
181.Xr gethostbyname 3 .
182This option is enabled by default.
183.It Dv RES_NOALIASES
184This option turns off the user level aliasing feature controlled by the
185.Dq Ev HOSTALIASES
186environment variable.
187Network daemons should set this option.
188.It Dv RES_USE_INET6
189Enables support for IPv6-only applications.
190This causes IPv4 addresses to be returned as an IPv4 mapped address.
191For example,
192.Li 10.1.1.1
193will be returned as
194.Li ::ffff:10.1.1.1 .
195The option is meaningful with certain kernel configuration only.
196.It Dv RES_USE_EDNS0
197Enables support for OPT pseudo-RR for EDNS0 extension.
198With the option, resolver code will attach OPT pseudo-RR into DNS queries,
199to inform of our receive buffer size.
200The option will allow DNS servers to take advantage of non-default receive
201buffer size, and to send larger replies.
202DNS query packets with EDNS0 extension is not compatible with
203non-EDNS0 DNS servers.
204.El
205.Pp
206The
207.Fn res_init
208routine
209reads the configuration file (if any; see
210.Xr resolver 5 )
211to get the default domain name,
212search list and
213the Internet address of the local name server(s).
214If no server is configured, the host running
215the resolver is tried.
216The current domain name is defined by the hostname
217if not specified in the configuration file;
218it can be overridden by the environment variable
219.Ev LOCALDOMAIN .
220This environment variable may contain several blank-separated
221tokens if you wish to override the
222.Em "search list"
223on a per-process basis.
224This is similar to the
225.Ic search
226command in the configuration file.
227Another environment variable
228.Dq Ev RES_OPTIONS
229can be set to
230override certain internal resolver options which are otherwise
231set by changing fields in the
232.Va _res
233structure or are inherited from the configuration file's
234.Ic options
235command.
236The syntax of the
237.Dq Ev RES_OPTIONS
238environment variable is explained in
239.Xr resolver 5 .
240Initialization normally occurs on the first call
241to one of the following routines.
242.Pp
243The
244.Fn res_query
245function provides an interface to the server query mechanism.
246It constructs a query, sends it to the local server,
247awaits a response, and makes preliminary checks on the reply.
248The query requests information of the specified
249.Fa type
250and
251.Fa class
252for the specified fully-qualified domain name
253.Fa dname .
254The reply message is left in the
255.Fa answer
256buffer with length
257.Fa anslen
258supplied by the caller.
259.Pp
260The
261.Fn res_search
262routine makes a query and awaits a response like
263.Fn res_query ,
264but in addition, it implements the default and search rules
265controlled by the
266.Dv RES_DEFNAMES
267and
268.Dv RES_DNSRCH
269options.
270It returns the first successful reply.
271.Pp
272The remaining routines are lower-level routines used by
273.Fn res_query .
274The
275.Fn res_mkquery
276function
277constructs a standard query message and places it in
278.Fa buf .
279It returns the size of the query, or \-1 if the query is
280larger than
281.Fa buflen .
282The query type
283.Fa op
284is usually
285.Dv QUERY ,
286but can be any of the query types defined in
287.In arpa/nameser.h .
288The domain name for the query is given by
289.Fa dname .
290The
291.Fa newrr_in
292argument
293is currently unused but is intended for making update messages.
294.Pp
295The
296.Fn res_send
297routine
298sends a pre-formatted query and returns an answer.
299It will call
300.Fn res_init
301if
302.Dv RES_INIT
303is not set, send the query to the local name server, and
304handle timeouts and retries.
305The length of the reply message is returned, or
306\-1 if there were errors.
307.Pp
308The
309.Fn dn_comp
310function
311compresses the domain name
312.Fa exp_dn
313and stores it in
314.Fa comp_dn .
315The size of the compressed name is returned or \-1 if there were errors.
316The size of the array pointed to by
317.Fa comp_dn
318is given by
319.Fa length .
320The compression uses
321an array of pointers
322.Fa dnptrs
323to previously-compressed names in the current message.
324The first pointer points to
325the beginning of the message and the list ends with
326.Dv NULL .
327The limit to the array is specified by
328.Fa lastdnptr .
329A side effect of
330.Fn dn_comp
331is to update the list of pointers for
332labels inserted into the message
333as the name is compressed.
334If
335.Fa dnptr
336is
337.Dv NULL ,
338names are not compressed.
339If
340.Fa lastdnptr
341is
342.Dv NULL ,
343the list of labels is not updated.
344.Pp
345The
346.Fn dn_expand
347entry
348expands the compressed domain name
349.Fa comp_dn
350to a full domain name
351The compressed name is contained in a query or reply message;
352.Fa msg
353is a pointer to the beginning of the message.
354The uncompressed name is placed in the buffer indicated by
355.Fa exp_dn
356which is of size
357.Fa length .
358The size of compressed name is returned or \-1 if there was an error.
359.Pp
360The
361.Fn dn_skipname
362function skips over a compressed domain name, which starts at a location
363pointed to by
364.Fa comp_dn .
365The compressed name is contained in a query or reply message;
366.Fa eom
367is a pointer to the end of the message.
368The size of compressed name is returned or \-1 if there was
369an error.
370.Pp
371The
372.Fn ns_get16
373function gets a 16-bit quantity from a buffer pointed to by
374.Fa src .
375.Pp
376The
377.Fn ns_get32
378function gets a 32-bit quantity from a buffer pointed to by
379.Fa src .
380.Pp
381The
382.Fn ns_put16
383function puts a 16-bit quantity
384.Fa src
385to a buffer pointed to by
386.Fa dst .
387.Pp
388The
389.Fn ns_put32
390function puts a 32-bit quantity
391.Fa src
392to a buffer pointed to by
393.Fa dst .
394.Sh IMPLEMENTATION NOTES
395This implementation of the resolver is thread-safe, but it will not
396function properly if the programmer attempts to declare his or her own
397.Va _res
398structure in an attempt to replace the per-thread version referred to
399by that macro.
400.Pp
401The following compile-time option can be specified to change the default
402behavior of resolver routines when necessary.
403.Bl -tag -width RES_ENFORCE_RFC1034
404.It Dv RES_ENFORCE_RFC1034
405If this symbol is defined during compile-time,
406.Fn res_search
407will enforce RFC 1034 check, namely, disallow using of underscore character
408within host names.
409This is used by the standard host lookup routines like
410.Xr gethostbyname 3 .
411For compatibility reasons this option is not enabled by default.
412.El
413.Sh RETURN VALUES
414The
415.Fn res_init
416function will return 0 on success, or \-1 in a threaded program if
417per-thread storage could not be allocated.
418.Pp
419The
420.Fn res_mkquery ,
421.Fn res_search ,
422and
423.Fn res_query
424functions return the size of the response on success, or \-1 if an
425error occurs.
426The integer
427.Vt h_errno
428may be checked to determine the reason for error.
429See
430.Xr gethostbyname 3
431for more information.
432.Sh FILES
433.Bl -tag -width /etc/resolv.conf
434.It Pa /etc/resolv.conf
435The configuration file,
436see
437.Xr resolver 5 .
438.El
439.Sh SEE ALSO
440.Xr gethostbyname 3 ,
441.Xr resolver 5 ,
442.Xr hostname 7
443.Pp
444.%T RFC1032 ,
445.%T RFC1033 ,
446.%T RFC1034 ,
447.%T RFC1035 ,
448.%T RFC974
449.Sh HISTORY
450The
451.Nm
452function appeared in
453.Bx 4.3 .
454