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