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