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