]> git.mxchange.org Git - simgear.git/blob - 3rdparty/udns/udns.3
Initial commit for a DNS service resolver
[simgear.git] / 3rdparty / udns / udns.3
1 .\" udns.3
2 .\" udns library manpage
3 .\"
4 .\" Copyright (C) 2005-2014  Michael Tokarev <mjt+udns@tls.msk.ru>
5 .\" This file is part of UDNS library, an async DNS stub resolver.
6 .\"
7 .\" This library is free software; you can redistribute it and/or
8 .\" modify it under the terms of the GNU Lesser General Public
9 .\" License as published by the Free Software Foundation; either
10 .\" version 2.1 of the License, or (at your option) any later version.
11 .\"
12 .\" This library is distributed in the hope that it will be useful,
13 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
14 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 .\" Lesser General Public License for more details.
16 .\"
17 .\" You should have received a copy of the GNU Lesser General Public
18 .\" License along with this library, in file named COPYING.LGPL; if not,
19 .\" write to the Free Software Foundation, Inc., 59 Temple Place,
20 .\" Suite 330, Boston, MA  02111-1307  USA
21
22 .TH udns 3 "Jan 2014" "Library Functions"
23
24 .SH NAME
25 udns \- stub DNS resolver library
26
27 .SH SYNOPSYS
28 .nf
29 #include <udns.h>
30 struct \fBdns_ctx\fR;
31 struct \fBdns_query\fR;
32 extern struct dns_ctx \fBdns_defctx\fR;
33 struct dns_ctx *\fIctx\fR;
34 typedef void \fBdns_query_fn\fR(\fIctx\fR, void *\fIresult\fR, void *\fIdata\fR);
35 typedef int
36 \fBdns_parse_fn\fR(const unsigned char *\fIqnd\fR,
37        const unsigned char *\fIpkt\fR,
38        const unsigned char *\fIcur\fR,
39        const unsigned char *\fIend\fR,
40        void **\fIresultp\fR);
41
42 \fBcc\fR ... -l\fBudns\fR
43 .fi
44
45 .SH DESCRIPTION
46
47 .PP
48 The DNS library, \fBudns\fR, implements thread-safe stub DNS resolver
49 functionality, which may be used both traditional, syncronous way
50 and asyncronously, with application-supplied event loop.
51
52 .PP
53 While DNS works with both TCP and UDP, performing UDP query first and
54 if the result does not fit in UDP buffer (512 bytes max for original
55 DNS protocol), retrying the query over TCP, the library uses UDP only,
56 but uses EDNS0 (RFC2671) extensions which allows larger UDP buffers.
57
58 .PP
59 The library uses single UDP socket to perform all operations even when
60 asking multiple nameservers.  This way, it is very simple to use the
61 library in asyncronous event-loop applications: an application should
62 add only single socket to the set of filedescriptors it monitors for I/O.
63
64 .PP
65 The library uses two main objects, \fIresolver context\fR of type
66 \fBstruct\ dns_ctx\fR, and \fIquery structure\fR of type
67 \fBstruct\ dns_query\fR, both are opaque for an application.
68 Resolver context holds global information about the resolver,
69 such as list of nameservers to use, list of active requests and the like.
70 Query objects holds information about a single DNS query in progress and
71 are allocated/processed/freed by the library.   Pointer to query structure
72 may be treated as an identifier of an in-progress query and may be used
73 to cancel the asyncronous query or to wait for it to complete.
74
75 .PP
76 Asyncronous interface works as follows.  An application initializes
77 resolver context, submits any number of queries for it using one of
78 supplied \fBdns_submit_\fIXXX\fR() routines (each return the query
79 identifier as pointer to query structure), waits for input on the
80 UDP socket used by the library, and gives some control to the library
81 by calling \fBdns_ioevent\fR() and \fBdns_timeouts\fR() routines when
82 appropriate.  The library performs all necessary processing and executes
83 application supplied callback routine when a query completes (either
84 successefully or not), giving it the result if any, pointer to the
85 resolver context (from which completion status may be obtained), and
86 the data pointer supplied by an application when the query has been
87 submitted.  When submitting a query, an application requests how to
88 handle the reply -- to either return raw DNS reply packet for its
89 own low-level processing, or it may provide an address of \fIparsing
90 routine\fR of type \fBdns_parse_fn\fR to perform conversion of on-wire
91 format into easy to use data structure (the library provides parsing
92 routines for several commonly used resource record types, as well as
93 type-safe higher-level inteface that requests parsing automatically).
94 The I/O monitoring and timeout handling may be either traditional
95 select() or poll() based, or any callback-driven technique may be
96 used.
97
98 .PP
99 Additionally, the library provides traditional syncronous interface,
100 which may be intermixed with asyncronous calls (during syncronous
101 query processing, other asyncronous queries for the same resolver
102 context continued to be processed as usual).  An application uses
103 one of numerous \fBdns_resolve_\fIXXX\fR() routines provided by the
104 library to perform a query.  As with asyncronous interface, an
105 application may either request to return raw DNS packet or type-specific
106 data structure by providing the parsing routine to handle the reply.
107 Every routine from \fBdns_resolve_\fIXXX\fR() series return pointer
108 to result or NULL in case of any error.  Query completion status
109 (or length of the raw DNS packet) is available from the resolver
110 context using \fBdns_status\fR() routine, the same way as for the
111 asyncronous interface.
112
113 .PP
114 Internally, library uses on-wire format of domain names, referred
115 to as \fIDN format\fR in this manual page.  This is a series of domain
116 \fIlabels\fR whith preceeding length byte, terminated by zero-length
117 label wich is integral part of the DN format.  There are several routines
118 provided to convert from traditional asciiz string to DN and back.
119 Higher-level type-specific query interface hides the DN format from
120 an application.
121
122 .SH "COMMON DEFINITIONS"
123
124 .PP
125 Every DNS Resource Record (RR) has a \fItype\fR and a \fIclass\fR.
126 The library defines several integer constants, \fBDNS_C_\fIXXX\fR and
127 \fBDNS_T_\fIXXX\fR, to use as symbolic names for RR classes and types,
128 such as \fBDNS_C_IN\fR for Internet class, \fBDNS_T_A\fR for IPv4
129 address record type and so on.  See udns.h header file for complete list
130 of all such constants.
131
132 .PP
133 The following constants are defined in udns.h header file:
134 .IP "\fBDNS_MAXDN\fR (255 bytes)"
135 Maximum length of the domain name in internal (on-wire) DN format.
136 .IP "\fBDNS_MAXLABEL\fR (63 bytes)"
137 Maximum length of a single label in DN format.
138 .IP "\fBDNS_MAXNAME\fR (1024 bytes)"
139 Maximum length of asciiz format of a domain name.
140 .IP "\fBDNS_HSIZE\fR (12 bytes)"
141 Size of header in DNS packet.
142 .IP "\fBDNS_PORT\fR (53)"
143 Default port to use when contacting a DNS server.
144 .IP "\fBDNS_MAXSERV\fR (6 servers)"
145 Maximum number of DNS servers to use.
146 .IP "\fBDNS_MAXPACKET\fR (512 bytes)"
147 Maximum length of DNS UDP packet as specified by original DNS protocol
148 .IP "\fBDNS_EDNS0PACKET\fR (4096 bytes)"
149 Default length of DNS UDP packet (with EDNS0 extensions) the library uses.
150 Note that recursive nameservers usually resides near the client asking them
151 to resolve names, e.g. on the same LAN segment or even on the same host, so
152 UDP packet fragmentation isn't a problem in most cases.  Note also that
153 the size of actual packets will be as many bytes as actual reply size requires,
154 which is smaller than this value in almost all cases.
155
156 .PP
157 Additionally, several constants are defined to simplify work with raw DNS
158 packets, such as DNS response codes (\fBDNS_R_\fIXXX\fR), DNS header layout
159 (\fBDNS_H_\fIXXX\fR) and others.  Again, see udns.h for complete list.
160 Library error codes (\fBDNS_E_\fIXXX\fR) are described later in this
161 manual page.
162
163 .SH "RESOLVER CONTEXT"
164
165 .PP
166 Resolver context, of type \fBstruct\ dns_ctx\fR, is an object which is
167 opaque to an application.  Several routines provided by the library
168 to initialize, copy and free resolver contexts.  Most other high-level
169 routines in this library expects a pointer to resolver context, \fIctx\fR,
170 as the first argument.  There is a default resolver context available,
171 named \fBdns_defctx\fR.  When the context pointer \fIctx\fR passed to
172 a routine is NULL, \fBdns_defctx\fR is used.  Several resolver contexts
173 may be active at the same time, for example, when an application is
174 multi-threaded and each thread uses resolver.
175 .PP
176 In order to use the library, an application should initialize and open
177 one or more resolver context objects.  These are two separate actions,
178 performed by \fBdns_init\fR() (or \fBdns_reset\fR()), and \fBdns_open\fR().
179 Between the two calls, an application is free to pefrorm additional
180 initialisation, such as setting custom nameservers, options or domain search
181 lists.  Optionally, in case no additional custom initialisation is required,
182 \fBdns_init\fR() may open the context if \fIdo_open\fR argument (see below)
183 is non-zero.
184 .PP
185 When initializing resolver context, the library uses information from
186 system file /etc/resolv.conf (see \fBresolv.conf\fR(5)), consults
187 environment variables \fB$LOCALDOMAIN\fR, \fB$NSCACHEIP\fR,
188 \fB$NAMESERVERS\fR and \fB$RES_OPTIONS\fR, and local host name to obtain
189 list of local nameservers, domain name search list and various resolver
190 options.
191 .PP
192 The following routines to initialize resolver context are available:
193 .PP
194 .nf
195 void \fBdns_reset\fR(\fIctx\fR)
196 int \fBdns_init\fR(\fIctx\fR, int \fIdo_open\fR)
197 .fi
198 .RS
199 \fBdns_reset\fR() resets a given resolver context to default values,
200 preparing it to be opened by \fBdns_open\fR().
201 It is ok to call this routine against opened and active context - all active
202 queries will be dropped, sockets will be closed and so on.  This routine
203 does not initialize any parameters from system configuration files, use
204 \fBdns_init\fR() for this.  There's no error return - operation always
205 succeeds.  \fBdns_init\fR() does everything \fBdns_reset\fR() does,
206 plus initializes various parameters of the context according to system
207 configuration and process environment variables.  If \fIdo_open\fR is
208 non-zero, \fBdns_init\fR() calls \fIdns_open\fR(), so that the whole
209 library initialisation is performed in a single step.
210 .RE
211 .PP
212 .nf
213 struct dns_ctx *\fBdns_new\fR(struct dns_ctx *\fIcopy\fR)
214 void \fBdns_free\fR(\fIctx\fR)
215 .fi
216 .RS
217 \fBdns_new\fR() allocates new resolver context and copies all parameters
218 for a given resolver context \fIcopy\fR, or default context if \fIcopy\fR
219 is NULL, and returns pointer to the newly allocated context.  The context
220 being copied should be initialized.
221 \fBdns_new\fR() may fail if there's no memory available to make a copy
222 of \fIcopy\fR, in which case the routine will return NULL pointer.
223 \fBdns_free\fR() is used to close assotiated socket and free resolver
224 context resources and cancelling (abandoming) all active queries
225 assotiated with it.  It's an error to free \fBdns_defctx\fR, only
226 dynamically allocated contexts returned by \fBdns_new\fR() are allowed
227 to be freed by \fBdns_free\fR().
228 .RE
229 .PP
230 .nf
231 int \fBdns_add_serv\fR(\fIctx\fR, const char *\fIservaddr\fR)
232 int \fBdns_add_serv_s\fR(\fIctx\fR, const struct sockaddr *\fIsa\fR)
233 int \fBdns_add_srch\fR(\fIctx\fR, const char *\fIsrch\fR)
234 .fi
235 .RS
236 Add an element to list of nameservers (\fBdns_add_serv\fR(), as
237 asciiz-string \fIservaddr\fR with an IP address of the nameserver,
238 and \fBdns_add_serv_s\fR(), as initialized socket address \fIsa\fR),
239 or search list (\fBdns_add_srch\fR(), as a pointer to domain name)
240 for the given context \fIctx\fR.  If the last argument is a NULL
241 pointer, the corresponding list (search or nameserver) is reset
242 instead.  Upon successeful completion, each routine returns new
243 number of elements in the list in question.  On error, negative
244 value is returned and global variable \fBerrno\fR is set appropriately.
245 It is an error to call any of this functions if the context is
246 opened (after \fBdns_open\fR() or \fBdns_init\fR() with non-zero argument).
247 .RE
248 .PP
249 .nf
250 int \fBdns_set_opts\fR(\fIctx\fR, const char *\fIopts\fR)
251 .fi
252 .RS
253 set resolver context options from \fIopts\fR string, in the same way as
254 processing \fBoptions\fR statement in resolv.conf and \fB$RES_OPTIONS\fR
255 environment variable.  Return number of unrecognized/invalid options
256 found (all recognized and valid options gets processed).
257 .RE
258 .PP
259 .nf
260 void \fBdns_set_opt\fR(\fIctx\fR, int \fIopt\fR, \fIval\fR)
261 .fi
262 .RS
263 .B TODO
264 The \fIflags\fR argument is a bitmask with the following bits defined:
265 .IP \fBDNS_NOSRCH\fR
266 do not perform domain name search in search list.
267 .IP \fBDNS_NORD\fR
268 do not request recursion when performing queries
269 (i.e. don't set RD flag in querues).
270 .IP \fBDNS_AAONLY\fR
271 request authoritative answers only (i.e. set AA
272 flag in queries).
273 .RE
274
275 .PP
276 .nf
277 int \fBdns_open\fR(\fIctx\fR)
278 int \fBdns_sock\fR(const \fIctx\fR)
279 void \fBdns_close\fR(\fIctx\fR)
280 .fi
281 .RS
282 \fBdns_open\fR() opens the UDP socket used for queries if not already
283 open, and return assotiated filedescriptor (or negative value in case
284 of error).  Before any query can be submitted, the context should be
285 opened using this routine.  And before opening, the context should be
286 initialized.
287 \fBdns_sock\fR() return the UDP socket if open, or -1 if not.
288 \fBdns_close\fR() closes the UDP socket if it was open, and drops all active
289 queries if any.
290 .RE
291
292 .PP
293 .nf
294 int \fBdns_active\fR(const \fIctx\fR)
295 .fi
296 .RS
297 return number of active queries queued for the given context
298 \fIctx\fR, or zero if none.
299 .RE
300
301 .PP
302 .nf
303 int \fBdns_status\fR(const \fIctx\fR)
304 .fi
305 .RS
306 return status code from last operation.  When using syncronous
307 interface, this is the query completion status of the last query.
308 With asyncronous interface, from within the callback routine,
309 this is the query completion status of the query for which the
310 callback is being called.  When query submission fails, this
311 is the error code indicating failure reason.  All error codes
312 are negative and are represented by \fBDNS_E_\fIXXX\fR constants
313 described below.
314 .RE
315
316 .PP
317 .nf
318 void \fBdns_ioevent\fR(\fIctx\fR, time_t \fInow\fR)
319 .fi
320 .RS
321 this routine may be called by an application to process I/O
322 events on the UDP socket used by the library, as returned
323 by \fBdns_sock\fR().  The routine tries to receive incoming
324 UDP datagram from the socket and process it.  The socket is
325 set up to be non-blocking, so it is safe to call the routine
326 even if there's no data to read.  The routine will process
327 as many datagrams as are queued for the socket, so it is
328 safe to use it with either level-triggered or edge-triggered
329 I/O monitoring model.  The \fInow\fR argument is either a
330 current time as returned by \fBtime\fR(), or 0, in which
331 case the routine will obtain current time by it's own.
332 .RE
333
334 .PP
335 .nf
336 int \fBdns_timeouts\fR(\fIctx\fR, int \fImaxwait\fR, time_t \fInow\fR)
337 .fi
338 .RS
339 process any pending timeouts and return number of secounds
340 from current time (\fInow\fR if it is not 0) to the time when
341 the library wants the application to pass it control to process
342 more queued requests.  In case when there are no requests pending,
343 this time is -1.  The routine will not request a time larger than
344 \fImaxwait\fR secounds if it is greather or equal to zero.  If
345 \fInow\fR is 0, the routine will obtain current time by it's own;
346 when it is not 0, it should contain current time as returned by
347 \fBtime\fR().
348 .RE
349
350 .PP
351 .nf
352 typedef void \fBdns_utm_fn\fR(\fIctx\fR, int \fItimeout\fR, void *\fIdata\fR)
353 void \fBdns_set_tmcbck\fR(\fIctx\fR, dns_utm_fn *\fIutmfn\fR, void *\fIdata\fR)
354 .fi
355 .RS
356 An application may use custom callback-based I/O multiplexing mechanism.
357 Usually such a mechanism have concept of a \fItimer\fR, and an ability
358 to register a timer event in a form of a callback routine which will
359 be executed after certain amount of time.  In order to use such an
360 event mechanism, udns provides an ability to register and de-register
361 timer events necessary for internal processing using whatever event
362 mechanism an application uses.  For this to work, it is possible to
363 assotiate a pointer to a routine that will perform necessary work for
364 (de)registering timer events with a given resolver context, and
365 udns will call that routine at appropriate times.  Prototype of
366 such a routine is shown by \fBdns_utm_fn\fR typedef above.  Libudns
367 assotiates single timer with resolver context.  User-supplied \fIutmfn\fR
368 routine will be called by the library with the following arguments:
369 .IP "\fIctx\fR == NULL"
370 delete user timer, at context free time or when an application changes
371 user timer request routine using \fBdns_set_tmcbck\fR();
372 .IP "\fIctx\fR != NULL, \fItimeout\fR < 0"
373 don't fire timer anymore, when there are no active requests;
374 .IP "\fIctx\fR != NULL, \fItimeout\fR == 0"
375 fire timer at the next possibility, but not immediately;
376 .IP "\fIctx\fR != NULL, \fItimeout\fR > 0"
377 fire timer after \fItimeout\fR seconds after now.
378 .PP
379 The \fIdata\fR argument passed to the routine will be the same
380 as passed to \fBdns_set_tmcbck\fR().
381 .PP
382 When a timer expires, an application should call \fBdns_timeouts\fR()
383 routine (see below).  Non-callback timer usage is provided too.
384 .RE
385
386 .PP
387 .B XXXX TODO: some more resolver context routines, like dns_set_dbgfn() etc.
388
389 .SH "QUERY INTERFACE"
390
391 .PP
392 There are two ways to perform DNS queries: traditional syncronous
393 way, when udns performs all the necessary processing and return
394 control to the application only when the query completes, and
395 asyncronous way, when an application submits one or more queries
396 to the library using given resolver context, and waits for completion
397 by monitoring filedescriptor used by library and calling library
398 routines to process input on that filedescriptor.  Asyncronous mode
399 works with callback routines: an application supplies an address of
400 a routine to execute when the query completes, and a data pointer,
401 which is passed to the callback routine.
402
403 .PP
404 Queries are submitted to the library in a form of \fBstruct\ dns_query\fR.
405 To perform asyncronous query, an application calls one of the
406 \fBdns_submit_\fIXXX\fR() rounines, and provides necessary information
407 for a callback, together with all the query parameters.
408 When the query completes, library will call application-supplied callback
409 routine, giving it the resolver context (wich holds query completion status),
410 dynamically allocated result (which will be either raw DNS packet or, if
411 applicatin requested parsing the result by specifying non-NULL parse routine,
412 ready-to-use type-specific structure), and a data pointer provided by an
413 application when it submitted the query.  It is the application who's
414 responsible for freeing the result memory.
415 .PP
416 Generic query callback routine looks like this:
417 .nf
418 typedef void
419 \fBdns_query_fn\fR(\fIctx\fR, void *\fIresult\fR, void *\fIdata\fR)
420 .fi
421 Type-specific query interface expects similar form of callback
422 routine with the only difference in type of \fBresult\fR argument,
423 which will be pointer to specific data structure (decoded reply)
424 instead of this void pointer to raw DNS packet data.
425
426 .PP
427 Result parsing routine looks like this:
428 .nf
429 typedef int
430 \fBdns_parse_fn\fR(const unsigned char *\fIqdn\fR,
431       const unsigned char *\fIpkt\fR,
432       const unsigned char *\fIcur\fR,
433       const unsigned char *\fIend\fR,
434       void **\fIresultp\fR);
435 .fi
436 When called by the library, the arguments are as follows:
437 \fIpkt\fR points to the start of the packet received;
438 \fIend\fR points past the end of the packet received;
439 \fIcur\fR points past the query DN in the query section of the
440 packet;
441 \fIqdn\fR points to the original query DN.
442 The routine should allocate a single buffer to hold the result,
443 parse the reply filling in the buffer, and return the buffer
444 using \fIresultp\fR argument.  It returns 0 in case of error,
445 or udns error code (\fBDNS_E_\fIXXX\fR constants) in case of
446 error.
447 Note that by the time when the parse routine is called by the
448 library, packet is already verified to be a reply to the
449 original query, by matching query DN, query class and query type.
450
451 .PP
452 Type-specific query inteface supplies necessary parsing routines
453 automatically.
454
455 .PP
456 In case of error, query completion status as returned by
457 \fBdns_status\fR(\fIctx\fR), will contain one of the following values:
458 .IP "positive value"
459 length of raw DNS packet if parsing is not requested.
460 .IP 0
461 the query was successeful and the \fIreply\fR points to type-specific
462 data structure.
463 .IP \fBDNS_E_TEMPFAIL\fR
464 temporary error, the resolver nameserver was not able to
465 process our query or timed out.
466 .IP \fBDNS_E_PROTOCOL\fR
467 protocol error, a nameserver returned malformed reply.
468 .IP \fBDNS_E_NXDOMAIN\fR
469 the domain name does not exist.
470 .IP \fBDNS_E_NODATA\fR
471 there is no data of requested type found.
472 .IP \fBDNS_E_NOMEM\fR
473 out of memory while processing request.
474 .IP \fBDNS_E_BADQUERY\fR
475 some aspect of the query (most common is the domain name in question)
476 is invalid, and the library can't even start a query.
477
478 .PP
479 Library provides two series of routines which uses similar interface --
480 one for asyncronous queries and another for syncronous queries.  There
481 are two general low-level routines in each series to submit (asyncronous
482 interface) and resolve (syncronous interface) queries, as well as several
483 type-specific routines with more easy-to-use interfaces.  To submit
484 an asyncronous query, use one of \fBdns_submit_\fIXXX\fR() routine, each
485 of which accepts query parameters, pointers to callback routine and to
486 callback data, and optional current time hint.  Note type-specific
487 \fBdns_submit_\fIXXX\fR() routines expects specific type of the callback
488 routine as well, which accepts reply as a pointer to corresponding
489 structure, not a void pointer).  Every \fBdns_submit_\fIXXX\fR() routine
490 return pointer to internal query structure of type struct\ dns_query,
491 used as an identifier for the given query.
492
493 .PP
494 To resolve a query syncronously, use one of \fBdns_resolve_\fIXXX\fR()
495 routines, which accepts the same query parameters (but not the
496 callback pointers) as corresponding \fBdns_submit_\fIXXX\fR(), and
497 return the query result, which is the same as passed to the callback
498 routine in case of asyncronous interface.
499
500 .PP
501 In either case, the result memory (if the query completed successefully)
502 is dynamically allocated and should be freed by an application.  If
503 the query failed for any reason, the result will be NULL, and error
504 status will be available from \fBdns_status\fR(\fIctx\fR) routine
505 as shown above.
506
507 .PP
508 .nf
509 struct dns_query *
510 \fBdns_submit_dn\fR(\fIctx\fR,
511      const unsigned char *\fIdn\fR, \fIqcls\fR, \fIqtyp\fR, \fIflags\fR,
512      \fIparse\fR, \fIcbck\fR, \fIdata\fR)
513 struct dns_query *
514 \fBdns_submit_p\fR(\fIctx\fR,
515      const char *\fIname\fR, \fIqcls\fR, \fIqtyp\fR, \fIflags\fR,
516      \fIparse\fR, \fIcbck\fR, \fIdata\fR)
517    enum dns_class \fIqcls\fR;
518    enum dns_type \fIqtyp\fR;
519    int \fIflags\fR;
520    dns_parse_fn *\fIparse\fR;
521    dns_query_fn *\fIcbck\fR;
522    void *\fIdata\fR;
523 .fi
524 .RS
525 submit a query for processing for the given resolver context \fIctx\fR.
526 Two routines differs only in 3rd argument, which is domain name in
527 DN format (\fIdn\fR) or asciiz string (\fIname\fR).  The query will be
528 performed for the given domain name, with type \fIqtyp\fR in class \fIqcls\fR,
529 using option bits in \fIflags\fR, using RR parsing routine pointed by
530 \fIparse\fR if not-NULL, and upon completion, \fIcbck\fR function will
531 be called with the \fIdata\fR argument.
532 In case of successeful query submission,
533 the routine return pointer to internal query structure which may be treated
534 as an identifier of the query as used by the library, and may be used as an
535 argument for \fBdns_cancel\fR() routine.  In case of error, NULL will be
536 returned, and context error status (available using \fIdns_status\fR() routine)
537 will be set to corresponding error code, which in this case may be
538 DNS_E_BADQUERY if the \fIname\fR of \fIdn\fR is invalid, DNS_E_NOMEM if
539 there's no memory available to allocate query structure, or DNS_E_TEMPFAIL
540 if an internal error occured.
541 .RE
542
543 .PP
544 .nf
545 void *\fBdns_resolve_dn\fR(\fIctx\fR,
546      const unsigned char *\fIdn\fR, \fIqcls\fR, \fIqtyp\fR, \fIflags\fR, \fIparse\fR);
547 void *\fBdns_resolve_p\fR(\fIctx\fR,
548      const char *\fIname\fR, \fIqcls\fR, \fIqtyp\fR, \fIflags\fR, \fIparse\fR)
549    enum dns_class \fIqcls\fR;
550    enum dns_type \fIqtyp\fR;
551    int \fIflags\fR;
552    dns_parse_fn *\fIparse\fR;  
553 .fi
554 .RS
555 syncronous interface.  The routines perform all the steps necessary to resolve
556 the given query and return the result.  If there's no positive result for any
557 reason, all the routines return NULL, and set context error status (available 
558 using \fBdns_status\fR() routine) to indicate the error code.  If the query
559 was successeful, context status code will contain either the length of the
560 raw DNS reply packet if \fIparse\fR argument was NULL (in which case the return
561 value is pointer to the reply DNS packet), or 0 (in which case the return value
562 is the result of \fIparse\fR routine).  If the query successeful (return value
563 is not NULL), the memory returned was dynamically allocated by the library
564 and should be free()d by application after use.
565 .RE
566
567 .PP
568 .nf
569 void *\fBdns_resolve\fR(\fIctx\fR, struct dns_query *\fIq\fR)
570 .fi
571 .RS
572 wait for the given query \fIq\fR, as returned by one of
573 \fBdns_submit_\fIXXX\fR() routines, for completion, and
574 return the result.  The callback routine will not be called
575 for this query.  After completion, the query identifier \fIq\fR
576 is not valid. Both \fBdns_resolve_dn\fR() and \fBdns_resolve_p\fR()
577 are just wrappers around corresponding submit routines and this
578 \fBdns_resolve\fR() routine.
579 .RE
580
581 .PP
582 .nf
583 void \fBdns_cancel\fR(\fIctx\fR, struct dns_query *\fIq\fR)
584 .fi
585 .RS
586 cancel an active query \fIq\fR, without calling a callback routine.
587 After completion, the query identifier \fIq\fR is not valid.
588 .RE
589
590 .SH "TYPE-SPECIFIC QUERIES"
591
592 .PP
593 In addition to the generic low-level query interface, the library provides
594 a set of routines to perform specific queries in a type-safe manner, as
595 well as parsers for several well-known resource record types.  The library
596 implements high-level interface for A, AAAA, PTR, MX and TXT records
597 and DNSBL and RHSBL functionality.  These routines returns specific types
598 as result of a query, instead of raw DNS packets.  The following types
599 and routines are available.
600
601 .PP
602 .nf
603 struct \fBdns_rr_null\fR {
604   char *\fBdnsn_qname\fR;     /* original query name */
605   char *\fBdnsn_cname\fR;     /* canonical name */
606   unsigned \fBdnsn_ttl\fR;    /* Time-To-Live (TTL) value */
607   int \fBdnsn_nrr\fR;         /* number of records in the set */
608 };
609 .fi
610 .PP
611 NULL RR set, used as a base for all other RR type structures.
612 Every RR structure as used by the library have four standard
613 fields as in struct\ \fBdns_rr_null\fR.
614
615 .SS "IN A Queries"
616 .PP
617 .nf
618 struct \fBdns_rr_a4\fR {       /* IN A RRset */
619   char *\fBdnsa4_qname\fR;     /* original query name */
620   char *\fBdnsa4_cname\fR;     /* canonical name */
621   unsigned \fBdnsa4_ttl\fR;    /* Time-To-Live (TTL) value */
622   int \fBdnsa4_nrr\fR;         /* number of addresses in the set */
623   struct in_addr \fBdnsa4_addr\fR[]; /* array of addresses */
624 };
625 typedef void
626   \fBdns_query_a4_fn\fR(\fIctx\fR, struct dns_rr_a4 *\fIresult\fR, \fIdata\fR)
627 dns_parse_fn \fBdns_parse_a4\fB;
628 struct dns_query *
629 \fBdns_submit_a4\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR,
630    dns_query_a4_fn *\fIcbck\fR, \fIdata\fR);
631 struct dns_rr_a4 *
632 \fBdns_resolve_a4\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR);
633 .fi
634 .PP
635 The \fBdns_rr_a4\fR structure holds a result of an \fBIN A\fR query,
636 which is an array of IPv4 addresses.  Callback routine for IN A queries
637 expected to be of type \fBdns_query_a4_fn\fR, which expects pointer to
638 \fBdns_rr_a4\fR structure as query result instead of raw DNS packet.
639 The \fBdns_parse_a4\fR() is used to convert raw DNS reply packet into
640 \fBdns_rr_a4\fR structure (it is used internally and may be used directly too
641 with generic query interface).  Routines \fBdns_submit_a4\fR() and
642 \fBdns_resolve_a4\fR() are used to perform A IN queries in a type-safe
643 manner.  The \fIname\fR parameter is the domain name in question, and
644 \fIflags\fR is query flags bitmask, with one bit, DNS_NOSRCH, of practical
645 interest (if the \fIname\fR is absolute, that is, it ends up with a dot,
646 DNS_NOSRCH flag will be set automatically).
647
648 .SS "IN AAAA Queries"
649 .PP
650 .nf
651 struct \fBdns_rr_a6\fR {       /* IN AAAA RRset */
652   char *\fBdnsa6_qname\fR;     /* original query name */
653   char *\fBdnsa6_cname\fR;     /* canonical name */
654   unsigned \fBdnsa6_ttl\fR;    /* Time-To-Live (TTL) value */
655   int \fBdnsa6_nrr\fR;         /* number of addresses in the set */
656   struct in6_addr \fBdnsa6_addr\fR[]; /* array of addresses */
657 };
658 typedef void
659   \fBdns_query_a6_fn\fR(\fIctx\fR, struct dns_rr_a6 *\fIresult\fR, \fIdata\fR)
660 dns_parse_fn \fBdns_parse_a6\fB;
661 struct dns_query *
662 \fBdns_submit_a6\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR,
663    dns_query_a6_fn *\fIcbck\fR, \fIdata\fR);
664 struct dns_rr_a6 *
665 \fBdns_resolve_a6\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR);
666 .fi
667 .PP
668 The \fBdns_rr_a6\fR structure holds a result of an \fBIN AAAA\fR query,
669 which is an array of IPv6 addresses.  Callback routine for IN AAAA queries
670 expected to be of type \fBdns_query_a6_fn\fR, which expects pointer to
671 \fBdns_rr_a6\fR structure as query result instead of raw DNS packet.
672 The \fBdns_parse_a6\fR() is used to convert raw DNS reply packet into
673 \fBdns_rr_a6\fR structure (it is used internally and may be used directly too
674 with generic query interface).  Routines \fBdns_submit_a6\fR() and
675 \fBdns_resolve_a6\fR() are used to perform AAAA IN queries in a type-safe
676 manner.  The \fIname\fR parameter is the domain name in question, and
677 \fIflags\fR is query flags bitmask, with one bit, DNS_NOSRCH, of practical
678 interest (if the \fIname\fR is absolute, that is, it ends up with a dot,
679 DNS_NOSRCH flag will be set automatically).
680
681 .SS "IN PTR Queries"
682 .PP
683 .nf
684 struct \fBdns_rr_ptr\fR {       /* IN PTR RRset */
685   char *\fBdnsptr_qname\fR;     /* original query name */
686   char *\fBdnsptr_cname\fR;     /* canonical name */
687   unsigned \fBdnsptr_ttl\fR;    /* Time-To-Live (TTL) value */
688   int \fBdnsptr_nrr\fR;         /* number of domain name pointers */
689   char *\fBdnsptr_ptr\fR[];     /* array of domain name pointers */
690 };
691 typedef void
692   \fBdns_query_ptr_fn\fR(\fIctx\fR, struct dns_rr_ptr *\fIresult\fR, \fIdata\fR)
693 dns_parse_fn \fBdns_parse_ptr\fB;
694 struct dns_query *
695 \fBdns_submit_a4ptr\fB(\fIctx\fR, const struct in_addr *\fBaddr\fR,
696    dns_query_ptr_fn *\fIcbck\fR, \fIdata\fR);
697 struct dns_rr_ptr *
698 \fBdns_resolve_a4ptr\fB(\fIctx\fR, const struct in_addr *\fBaddr\fR);
699 struct dns_query *
700 \fBdns_submit_a6ptr\fB(\fIctx\fR, const struct in6_addr *\fBaddr\fR,
701    dns_query_ptr_fn *\fIcbck\fR, \fIdata\fR);
702 struct dns_rr_ptr *
703 \fBdns_resolve_a6ptr\fB(\fIctx\fR, const struct in6_addr *\fBaddr\fR);
704 .fi
705 .PP
706 The \fBdns_rr_ptr\fR structure holds a result of an IN PTR query, which
707 is an array of domain name pointers for a given IPv4 or IPv6 address.
708 Callback routine for IN PTR queries expected to be of type
709 \fBdns_query_ptr_fn\fR, which expects pointer to \fBdns_rr_ptr\fR
710 structure as query result instead of raw DNS packet.  The \fBdns_parse_ptr\fR()
711 is used to convert raw DNS reply packet into \fBdns_rr_ptr\fR structure
712 (it is used internally and may be used directly too with generic query
713 interface).  Routines \fBdns_submit_a4ptr\fR() and \fBdns_resolve_a4ptr\fR()
714 are used to perform IN PTR queries for IPv4 addresses in a type-safe
715 manner. Routines \fBdns_submit_a6ptr\fR() and \fBdns_resolve_a6ptr\fR()
716 are used to perform IN PTR queries for IPv6 addresses.
717
718 .SS "IN MX Queries"
719 .PP
720 .nf
721 struct \fBdns_mx\fR {          /* single MX record */
722   int \fBpriority\fR;          /* priority value of this MX */
723   char *\fBname\fR;            /* domain name of this MX */
724 };
725 struct \fBdns_rr_mx\fR {       /* IN MX RRset */
726   char *\fBdnsmx_qname\fR;     /* original query name */
727   char *\fBdnsmx_cname\fR;     /* canonical name */
728   unsigned \fBdnsmx_ttl\fR;    /* Time-To-Live (TTL) value */
729   int \fBdnsmx_nrr\fR;         /* number of mail exchangers in the set */
730   struct dns_mx \fBdnsmx_mx\fR[]; /* array of mail exchangers */
731 };
732 typedef void
733   \fBdns_query_mx_fn\fR(\fIctx\fR, struct dns_rr_mx *\fIresult\fR, \fIdata\fR)
734 dns_parse_fn \fBdns_parse_mx\fB;
735 struct dns_query *
736 \fBdns_submit_mx\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR,
737    dns_query_mx_fn *\fIcbck\fR, \fIdata\fR);
738 struct dns_rr_mx *
739 \fBdns_resolve_mx\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR);
740 .fi
741 .PP
742 The \fBdns_rr_mx\fR structure holds a result of an IN MX query, which
743 is an array of mail exchangers for a given domain.  Callback routine for IN MX
744 queries expected to be of type \fBdns_query_mx_fn\fR, which expects pointer to
745 \fBdns_rr_mx\fR structure as query result instead of raw DNS packet.
746 The \fBdns_parse_mx\fR() is used to convert raw DNS reply packet into
747 \fBdns_rr_mx\fR structure (it is used internally and may be used directly too
748 with generic query interface).  Routines \fBdns_submit_mx\fR() and
749 \fBdns_resolve_mx\fR() are used to perform IN MX queries in a type-safe
750 manner.  The \fIname\fR parameter is the domain name in question, and
751 \fIflags\fR is query flags bitmask, with one bit, DNS_NOSRCH, of practical
752 interest (if the \fIname\fR is absolute, that is, it ends up with a dot,
753 DNS_NOSRCH flag will be set automatically).
754
755 .SS "TXT Queries"
756 .PP
757 .nf
758 struct \fBdns_txt\fR {          /* single TXT record */
759   int \fBlen\fR;                /* length of the text */
760   unsigned char *\fBtxt\fR;     /* pointer to the text */
761 };
762 struct \fBdns_rr_txt\fR {       /* TXT RRset */
763   char *\fBdnstxt_qname\fR;     /* original query name */
764   char *\fBdnstxt_cname\fR;     /* canonical name */
765   unsigned \fBdnstxt_ttl\fR;    /* Time-To-Live (TTL) value */
766   int \fBdnstxt_nrr\fR;         /* number of text records in the set */
767   struct dns_txt \fBdnstxt_txt\fR[]; /* array of TXT records */
768 };
769 typedef void
770   \fBdns_query_txt_fn\fR(\fIctx\fR, struct dns_rr_txt *\fIresult\fR, \fIdata\fR)
771 dns_parse_fn \fBdns_parse_txt\fB;
772 struct dns_query *
773 \fBdns_submit_txt\fB(\fIctx\fR, const char *\fIname\fR, enum dns_class \fIqcls\fR,
774    int \fIflags\fR, dns_query_txt_fn *\fIcbck\fR, \fIdata\fR);
775 struct dns_rr_txt *
776 \fBdns_resolve_txt\fB(\fIctx\fR, const char *\fIname\fR,
777              enum dns_class \fIqcls\fR, int \fIflags\fR);
778 .fi
779 .PP
780 The \fBdns_rr_txt\fR structure holds a result of a TXT query, which is an
781 array of text records for a given domain name.  Callback routine for TXT
782 queries expected to be of type \fBdns_query_txt_fn\fR, which expects pointer
783 to \fBdns_rr_txt\fR structure as query result instead of raw DNS packet.
784 The \fBdns_parse_txt\fR() is used to convert raw DNS reply packet into
785 \fBdns_rr_txt\fR structure (it is used internally and may be used directly too
786 with generic query interface).  Routines \fBdns_submit_txt\fR() and
787 \fBdns_resolve_txt\fR() are used to perform IN MX queries in a type-safe
788 manner.  The \fIname\fR parameter is the domain name in question, and
789 \fIflags\fR is query flags bitmask, with one bit, DNS_NOSRCH, of practical
790 interest (if the \fIname\fR is absolute, that is, it ends up with a dot,
791 DNS_NOSRCH flag will be set automatically).  Note that each TXT string
792 is represented by \fBstruct\ dns_txt\fR, while zero-terminated (and the
793 len field of the structure does not include the terminator), may contain
794 embedded null characters -- content of TXT records is not interpreted
795 by the library in any way.
796
797 .SS "SRV Queries"
798 .PP
799 .nf
800 struct \fBdns_srv\fR {          /* single SRV record */
801   int \fBpriority\fR;           /* priority of the record */
802   int \fBweight\fR;             /* weight of the record */
803   int \fBport\fR;               /* the port number to connect to */
804   char *\fBname\fR;             /* target host name */
805 };
806 struct \fBdns_rr_srv\fR {       /* SRV RRset */
807   char *\fBdnssrv_qname\fR;     /* original query name */
808   char *\fBdnssrv_cname\fR;     /* canonical name */
809   unsigned \fBdnssrv_ttl\fR;    /* Time-To-Live (TTL) value */
810   int \fBdnssrv_nrr\fR;         /* number of text records in the set */
811   struct dns_srv \fBdnssrv_srv\fR[]; /* array of SRV records */
812 };
813 typedef void
814   \fBdns_query_srv_fn\fR(\fIctx\fR, struct dns_rr_srv *\fIresult\fR, \fIdata\fR)
815 dns_parse_fn \fBdns_parse_srv\fB;
816 struct dns_query *
817 \fBdns_submit_srv\fB(\fIctx\fR, const char *\fIname\fR, const char *\fIservice\fR, const char *\fIprotocol\fR,
818    int \fIflags\fR, dns_query_txt_fn *\fIcbck\fR, \fIdata\fR);
819 struct dns_rr_srv *
820 \fBdns_resolve_srv\fB(\fIctx\fR, const char *\fIname\fR, const char *\fIservice\fR, const char *\fIprotocol\fR,
821              int \fIflags\fR);
822 .fi
823 .PP
824 The \fBdns_rr_srv\fR structure holds a result of an IN SRV (rfc2782) query,
825 which is an array of servers (together with port numbers) which are performing
826 operations for a given \fIservice\fR using given \fIprotocol\fR on a target
827 domain \fIname\fR.  Callback routine for IN SRV queries expected to be of type
828 \fBdns_query_srv_fn\fR, which expects pointer to \fBdns_rr_srv\fR structure as
829 query result instead of raw DNS packet.  The \fBdns_parse_srv\fR() is used to
830 convert raw DNS reply packet into \fBdns_rr_srv\fR structure (it is used
831 internally and may be used directly too with generic query interface).
832 Routines \fBdns_submit_srv\fR() and \fBdns_resolve_srv\fR() are used to
833 perform IN SRV queries in a type-safe manner.  The \fIname\fR parameter
834 is the domain name in question, \fIservice\fR and \fRprotocl\fR specifies the
835 service and the protocol in question (the library will construct query DN
836 according to rfc2782 rules) and may be NULL (in this case the library
837 assumes \fIname\fR parameter holds the complete SRV query), and
838 \fIflags\fR is query flags bitmask, with one bit, DNS_NOSRCH, of practical
839 interest (if the \fIname\fR is absolute, that is, it ends up with a dot,
840 DNS_NOSRCH flag will be set automatically).
841
842 .SS "NAPTR Queries"
843 .PP
844 .nf
845 struct \fBdns_naptr\fR {        /* single NAPTR record */
846   int \fBorder\fR;              /* record order */
847   int \fBpreference\fR;         /* preference of this record */
848   char *\fBflags\fR;            /* application-specific flags */
849   char *\fBservice\fR;          /* service parameter */
850   char *\fBregexp\fR;           /* substitutional regular expression */
851   char *\fBreplacement\fR;      /* replacement string */
852 };
853 struct \fBdns_rr_naptr\fR {     /* NAPTR RRset */
854   char *\fBdnsnaptr_qname\fR;   /* original query name */
855   char *\fBdnsnaptr_cname\fR;   /* canonical name */
856   unsigned \fBdnsnaptr_ttl\fR;  /* Time-To-Live (TTL) value */
857   int \fBdnsnaptr_nrr\fR;       /* number of text records in the set */
858   struct dns_naptr \fBdnsnaptr_naptr\fR[]; /* array of NAPTR records */
859 };
860 typedef void
861   \fBdns_query_naptr_fn\fR(\fIctx\fR, struct dns_rr_naptr *\fIresult\fR, \fIdata\fR)
862 dns_parse_fn \fBdns_parse_naptr\fB;
863 struct dns_query *
864 \fBdns_submit_naptr\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR,
865    dns_query_txt_fn *\fIcbck\fR, \fIdata\fR);
866 struct dns_rr_naptr *
867 \fBdns_resolve_naptr\fB(\fIctx\fR, const char *\fIname\fR, int \fIflags\fR);
868 .fi
869 .PP
870 The \fBdns_rr_naptr\fR structure holds a result of an IN NAPTR (rfc3403) query.
871 Callback routine for IN NAPTR queries expected to be of type
872 \fBdns_query_naptr_fn\fR, expects pointer to \fBdns_rr_naptr\fR
873 structure as query result instead of raw DNS packet.
874 The \fBdns_parse_naptr\fR() is used to convert raw DNS reply packet into
875 \fBdns_rr_naptr\fR structure (it is used
876 internally and may be used directly too with generic query interface).
877 Routines \fBdns_submit_naptr\fR() and \fBdns_resolve_naptr\fR() are used to
878 perform IN NAPTR queries in a type-safe manner.  The \fIname\fR parameter
879 is the domain name in question, and \fIflags\fR is query flags bitmask,
880 with one bit, DNS_NOSRCH, of practical interest (if the \fIname\fR is
881 absolute, that is, it ends up with a dot, DNS_NOSRCH flag will be set
882 automatically).
883
884 .SS "DNSBL Interface"
885 .PP
886 A DNS-based blocklists, or a DNSBLs, are in wide use nowadays, especially
887 to protect mailservers from spammers.  The library provides DNSBL interface,
888 a set of routines to perform queries against DNSBLs.  Routines accepts an
889 IP address (IPv4 and IPv6 are both supported) and a base DNSBL zone as
890 query parameters, and returns either \fBdns_rr_a4\fR or \fBdns_rr_txt\fR
891 structure.  Note that IPv6 interface return IPv4 RRset.
892 .PP
893 .nf
894 struct dns_query *
895 \fBdns_submit_a4dnsbl\fR(\fIctx\fR,
896   const struct in_addr *\fIaddr\fR, const char *\fIdnsbl\fR,
897   dns_query_a4_fn *\fIcbck\fR, void *\fIdata\fR);
898 struct dns_query *
899 \fBdns_submit_a4dnsbl_txt\fR(\fIctx\fR,
900   const struct in_addr *\fIaddr\fR, const char *\fIdnsbl\fR,
901   dns_query_txt_fn *\fIcbck\fR, void *\fIdata\fR);
902 struct dns_query *
903 \fBdns_submit_a6dnsbl\fR(\fIctx\fR,
904   const struct in6_addr *\fIaddr\fR, const char *\fIdnsbl\fR,
905   dns_query_a4_fn *\fIcbck\fR, void *\fIdata\fR);
906 struct dns_query *
907 \fBdns_submit_a6dnsbl_txt\fR(\fIctx\fR,
908   const struct in6_addr *\fIaddr\fR, const char *\fIdnsbl\fR,
909   dns_query_txt_fn *\fIcbck\fR, void *\fIdata\fR);
910 struct dns_rr_a4 *\fBdns_resolve_a4dnsbl\fR(\fIctx\fR,
911   const struct in_addr *\fIaddr\fR, const char *\fIdnsbl\fR)
912 struct dns_rr_txt *\fBdns_resolve_a4dnsbl_txt\fR(\fIctx\fR,
913   const struct in_addr *\fIaddr\fR, const char *\fIdnsbl\fR)
914 struct dns_rr_a4 *\fBdns_resolve_a6dnsbl\fR(\fIctx\fR,
915   const struct in6_addr *\fIaddr\fR, const char *\fIdnsbl\fR)
916 struct dns_rr_txt *\fBdns_resolve_a6dnsbl_txt\fR(\fIctx\fR,
917   const struct in6_addr *\fIaddr\fR, const char *\fIdnsbl\fR)
918 .fi
919 Perform (submit or resolve) a DNSBL query for the given \fIdnsbl\fR
920 domain and an IP \fIaddr\fR in question, requesting either A or TXT
921 records.
922
923 .SS "RHSBL Interface"
924 .PP
925 RHSBL is similar to DNSBL, but instead of an IP address, the
926 parameter is a domain name.
927 .PP
928 .nf
929 struct dns_query *
930 \fBdns_submit_rhsbl\fR(\fIctx\fR, const char *\fIname\fR, const char *\fIrhsbl\fR,
931   dns_query_a4_fn *\fIcbck\fR, void *\fIdata\fR);
932 struct dns_query *
933 \fBdns_submit_rhsbl_txt\fR(\fIctx\fR, const char *\fIname\fR, const char *\fIrhsbl\fR,
934   dns_query_txt_fn *\fIcbck\fR, void *\fIdata\fR);
935 struct dns_rr_a4 *
936 \fBdns_resolve_rhsbl\fR(\fIctx\fR, const char *\fIname\fR, const char *\fIrhsbl\fR);
937 struct dns_rr_txt *
938 \fBdns_resolve_rhsbl_txt\fR(\fIctx\fR, const char *\fIname\fR, const char *\fIrhsbl\fR);
939 .fi
940 Perform (submit or resolve) a RHSBL query for the given \fIrhsbl\fR
941 domain and \fIname\fR in question, requesting either A or TXT records.
942
943
944 .SH "LOW-LEVEL INTERFACE"
945
946 .SS "Domain Names (DNs)"
947
948 .PP
949 A DN is a series of domain name labels each starts with length byte,
950 followed by empty label (label with zero length).  The following
951 routines to work with DNs are provided.
952
953 .PP
954 .nf
955 unsigned \fBdns_dnlen\fR(const unsigned char *\fIdn\fR)
956 .fi
957 .RS
958 return length of the domain name \fIdn\fR, including the terminating label.
959 .RE
960
961 .PP
962 .nf
963 unsigned \fBdns_dnlabels\fR(const unsigned char *\fIdn\fR)
964 .fi
965 .RS
966 return number of non-zero labels in domain name \fIdn\fR.
967 .RE
968
969 .PP
970 .nf
971 unsigned \fBdns_dnequal\fR(\fIdn1\fR, \fIdn2\fR)
972   const unsigned char *\fIdn1\fR, *\fIdn2\fR;
973 .fi
974 .RS
975 test whenever the two domain names, \fIdn1\fR and \fIdn2\fR, are
976 equal (case-insensitive).  Return domain name length if equal
977 or 0 if not.
978 .RE
979
980 .PP
981 .nf
982 unsigned \fBdns_dntodn\fR(\fIsdn\fR, \fIddn\fR, \fIdnsiz\fR)
983   const unsigned char *\fIsdn\fR;
984   unsigned char *\fIddn\fR;
985   unsigned \fIdnsiz\fR;
986 .fi
987 .RS
988 copies the source domain name \fIsdn\fR to destination buffer \fIddn\fR
989 of size \fIdnsiz\fR.  Return domain name length or 0 if \fIddn\fR is
990 too small.
991 .RE
992
993 .PP
994 .nf
995 int \fBdns_ptodn\fR(\fIname\fR, \fInamelen\fR, \fIdn\fR, \fIdnsiz\fR, \fIisabs\fR)
996 int \fBdns_sptodn\fR(\fIname\fR, \fIdn\fR, \fIdnsiz\fR)
997   const char *\fIname\fR; unsigned \fInamelen\fR;
998   unsigned char *\fIdn\fR; unsigned \fIdnsiz\fR;
999   int *\fIisabs\fR;
1000 .fi
1001 .RS
1002 convert asciiz name \fIname\fR of length \fInamelen\fR to DN format,
1003 placing result into buffer \fIdn\fR of size \fIdnsiz\fR.  Return
1004 length of the DN if successeful, 0 if the \fIdn\fR buffer supplied is
1005 too small, or negative value if \fIname\fR is invalid.  If \fIisabs\fR
1006 is non-NULL and conversion was successeful, *\fIisabs\fR will be set to
1007 either 1 or 0 depending whenever \fIname\fR was absolute (i.e. ending with
1008 a dot) or not.  Name length, \fInamelength\fR, may be zero, in which case
1009 strlen(\fIname\fR) will be used.  Second form, \fBdns_sptodn\fR(), is a
1010 simplified form of \fBdns_ptodn\fR(), equivalent to
1011 .br
1012 .nf
1013 \fBdns_ptodn\fR(\fIname\fR, 0, \fIdn\fR, \fIdnlen\fR, 0).
1014 .fi
1015 .RE
1016
1017 .PP
1018 .nf
1019 extern const unsigned char \fBdns_inaddr_arpa_dn\fR[]
1020 int \fBdns_a4todn\fR(const struct in_addr *\fIaddr\fR, const unsigned char *\fItdn\fR,
1021       unsigned char *\fIdn\fR, unsigned \fIdnsiz\fR)
1022 int \fBdns_a4ptodn\fR(const struct in_addr *\fIaddr\fR, const char *\fItname\fR,
1023       unsigned char *\fIdn\fR, unsigned \fIdnsiz\fR)
1024 extern const unsigned char \fBdns_ip6_arpa_dn\fR[]
1025 int \fBdns_a6todn\fR(const struct in6_addr *\fIaddr\fR, const unsigned char *\fItdn\fR,
1026       unsigned char *\fIdn\fR, unsigned \fIdnsiz\fR)
1027 int \fBdns_a6ptodn\fR(const struct in6_addr *\fIaddr\fR, const char *\fItname\fR,
1028       unsigned char *\fIdn\fR, unsigned \fIdnsiz\fR)
1029 .fi
1030 .RS
1031 several variants of routines to convert IPv4 and IPv6 address \fIaddr\fR
1032 into reverseDNS-like domain name in DN format, storing result in \fIdn\fR
1033 of size \fIdnsiz\fR.  \fItdn\fR (or \fItname\fR) is the base zone name,
1034 like in-addr.arpa for IPv4 or in6.arpa for IPv6.  If \fItdn\fR (or \fItname\fR)
1035 is NULL, \fBdns_inaddr_arpa_dn\fR (or \fBdns_ip6_arpa_dn\fR) will be used.
1036 The routines may be used to construct a DN for a DNSBL lookup for example.
1037 All routines return length of the resulting DN on success, -1 if resulting
1038 DN is invalid, or 0 if the \fIdn\fR buffer (\fIdnsiz\fR) is too small.
1039 To hold standard rDNS DN, a buffer of size \fBDNS_A4RSIZE\fR (30 bytes) for
1040 IPv4 address, or \fBDNS_A6RSIZE\fR (74 bytes) for IPv6 address, is sufficient.
1041 .RE
1042
1043 .PP
1044 .nf
1045 int \fBdns_dntop\fR(\fIdn\fR, \fIname\fR, \fInamesiz\fR)
1046    const unsigned char *\fIdn\fR;
1047    const char *\fIname\fR; unsigned \fInamesiz\fR;
1048 .fi
1049 .RS
1050 convert domain name \fIdn\fR in DN format to asciiz string, placing result
1051 into \fIname\fR buffer of size \fInamesiz\fR.  Maximum length of asciiz
1052 representation of domain name is \fBDNS_MAXNAME\fR (1024) bytes.  Root
1053 domain is represented as empty string.  Return length of the resulting name
1054 (including terminating character, i.e. strlen(name)+1) on success, 0 if the
1055 \fIname\fR buffer is too small, or negative value if \fIdn\fR is invalid
1056 (last case should never happen since all routines in this library which
1057 produce domain names ensure the DNs generated are valid).
1058 .RE
1059
1060 .PP
1061 .nf
1062 const char *\fBdns_dntosp\fR(const unsigned char *\fIdn\fR)
1063 .fi
1064 .RS
1065 convert domain name \fIdn\fR in DN format to asciiz string using static
1066 buffer.  Return the resulting asciiz string on success or NULL on failure.
1067 Note since this routine uses static buffer, it is not thread-safe.
1068 .RE
1069
1070 .PP
1071 .nf
1072 unsigned \fBdns_dntop_size\fR(const unsigned char *\fIdn\fR)
1073 .fi
1074 .RS
1075 return the buffer size needed to convert the \fIdn\fR domain name
1076 in DN format to asciiz string, for \fBdns_dntop\fR().  The routine
1077 return either the size of buffer required, including the trailing
1078 zero byte, or 0 if \fIdn\fR is invalid.
1079 .RE
1080
1081 .SS "Working with DNS Packets"
1082
1083 .PP
1084 The following routines are provided to encode and decode DNS on-wire
1085 packets.  This is low-level interface.
1086
1087 .PP
1088 DNS response codes (returned by \fBdns_rcode\fR() routine) are
1089 defined as constants prefixed with \fBDNS_R_\fR.  See udns.h
1090 header file for the complete list.  In particular, constants
1091 \fBDNS_R_NOERROR\fR (0), \fBDNS_R_SERVFAIL\fR, \fBDNS_R_NXDOMAIN\fR
1092 may be of interest to an application.
1093
1094 .PP
1095 .nf
1096 unsigned \fBdns_get16\fR(const unsigned char *\fIp\fR)
1097 unsigned \fBdns_get32\fR(const unsigned char *\fIp\fR)
1098 .fi
1099 .RS
1100 helper routines, convert 16-bit or 32-bit integer in on-wire
1101 format pointed to by \fIp\fR to unsigned.
1102 .RE
1103
1104 .PP
1105 .nf
1106 unsigned char *\fBdns_put16\fR(unsigned char *\fId\fR, unsigned \fIn\fR)
1107 unsigned char *\fBdns_put32\fR(unsigned char *\fId\fR, unsigned \fIn\fR)
1108 .fi
1109 .RS
1110 helper routine, convert unsigned 16-bit or 32-bit integer \fIn\fR to
1111 on-wire format to buffer pointed to by \fId\fR, return \fId\fR+2 or
1112 \fId\fR+4.
1113 .RE
1114
1115 .PP
1116 .nf
1117 \fBDNS_HSIZE\fR (12)
1118 .fi
1119 .RS
1120 defines size of DNS header.  Data section
1121 in the DNS packet immediately follows the header.  In the header,
1122 there are query identifier (id), various flags and codes,
1123 and number of resource records in various data sections.
1124 See udns.h header file for complete list of DNS header definitions.
1125 .RE
1126
1127 .PP
1128 .nf
1129 unsigned \fBdns_qid\fR(const unsigned char *\fIpkt\fR)
1130 int \fBdns_rd\fR(const unsigned char *\fIpkt\fR)
1131 int \fBdns_tc\fR(const unsigned char *\fIpkt\fR)
1132 int \fBdns_aa\fR(const unsigned char *\fIpkt\fR)
1133 int \fBdns_qr\fR(const unsigned char *\fIpkt\fR)
1134 int \fBdns_ra\fR(const unsigned char *\fIpkt\fR)
1135 unsigned \fBdns_opcode\fR(const unsigned char *\fIpkt\fR)
1136 unsigned \fBdns_rcode\fR(const unsigned char *\fIpkt\fR)
1137 unsigned \fBdns_numqd\fR(const unsigned char *\fIpkt\fR)
1138 unsigned \fBdns_numan\fR(const unsigned char *\fIpkt\fR)
1139 unsigned \fBdns_numns\fR(const unsigned char *\fIpkt\fR)
1140 unsigned \fBdns_numar\fR(const unsigned char *\fIpkt\fR)
1141 const unsigned char *\fBdns_payload\fR(const unsigned char *\fIpkt\fR)
1142 .fi
1143 .RS
1144 return various parts from the DNS packet header \fIpkt\fR:
1145 query identifier (qid),
1146 recursion desired (rd) flag,
1147 truncation occured (tc) flag,
1148 authoritative answer (aa) flag,
1149 query response (qr) flag,
1150 recursion available (ra) flag,
1151 operation code (opcode),
1152 result code (rcode),
1153 number of entries in question section (numqd),
1154 number of answers (numan),
1155 number of authority records (numns),
1156 number of additional records (numar),
1157 and the pointer to the packet data (payload).
1158 .RE
1159
1160 .PP
1161 .nf
1162 int \fBdns_getdn\fR(\fIpkt\fR, \fIcurp\fR, \fIpkte\fR, \fIdn\fR, \fIdnsiz\fR)
1163 const unsigned char *\fBdns_skipdn\fR(\fIcur\fR, \fIpkte\fR)
1164    const unsigned char *\fIpkt\fR, *\fIpkte\fR, **\fIcurp\fR, *\fIcur\fR;
1165    unsigned char *\fIdn\fR; unsigned \fIdnsiz\fR;
1166 .fi
1167 .RS
1168 \fBdns_getdn\fR() extract DN from DNS packet \fIpkt\fR which ends before
1169 \fIpkte\fR starting at position *\fIcurp\fR into buffer pointed to by
1170 \fIdn\fR of size \fIdnsiz\fR.  Upon successeful completion, *\fIcurp\fR
1171 will point to the next byte in the packet after the extracted domain name.
1172 It return positive number (length of the DN if \fIdn\fR) upon successeful
1173 completion, negative value on error (when the packet contains invalid data),
1174 or zero if the \fIdnsiz\fR is too small (maximum length of a domain name is
1175 \fBDNS_MAXDN\fR).  \fBdns_skipdn\fR() return pointer to the next byte in
1176 DNS packet which ends up before \fIpkte\fR after a domain name which starts
1177 at the \fIcur\fP byte, or NULL if the packet is invalid.  \fBdns_skipdn\fR()
1178 is more or less equivalent to what \fBdns_getdn\fR() does, except it does not
1179 actually extract the domain name in question, and uses simpler interface.
1180 .RE
1181
1182 .PP
1183 .nf
1184 struct \fBdns_rr\fR {
1185   unsigned char \fBdnsrr_dn\fR[DNS_MAXDN]; /* the RR DN name */
1186   enum dns_class \fBdnsrr_cls\fR;          /* class of the RR */
1187   enum dns_type  \fBdnsrr_typ\fR;          /* type of the RR */
1188   unsigned \fBdnsrr_ttl\fR;                /* TTL value */
1189   unsigned \fBdnsrr_dsz\fR;                /* size of data in bytes */
1190   const unsigned char *\fBdnsrr_dptr\fR;   /* pointer to the first data byte */
1191   const unsigned char *\fBdnsrr_dend\fR;   /* next byte after RR */
1192 };
1193 .fi
1194 .RS
1195 The \fBdns_rr\fR structure is used to hold information about
1196 single DNS Resource Record (RR) in an easy to use form.
1197 .RE
1198
1199 .PP
1200 .nf
1201 struct \fBdns_parse\fR {
1202   const unsigned char *\fBdnsp_pkt\fR; /* pointer to the packet being parsed */
1203   const unsigned char *\fBdnsp_end\fR; /* end of the packet pointer */
1204   const unsigned char *\fBdnsp_cur\fR; /* current packet positionn */
1205   const unsigned char *\fBdnsp_ans\fR; /* pointer to the answer section */
1206   int \fBdnsp_rrl\fR;                  /* number of RRs left */
1207   int \fBdnsp_nrr\fR;                  /* number of relevant RRs seen so far */
1208   unsigned \fBdnsp_ttl\fR;             /* TTL value so far */
1209   const unsigned char *\fBdnsp_qdn\fR; /* the domain of interest or NULL */
1210   enum dns_class \fBdnsp_qcls\fR;      /* class of interest or 0 for any */
1211   enum dns_type  \fBdnsp_qtyp\fR;      /* type of interest or 0 for any */
1212   unsigned char \fBdnsp_dnbuf\fR[DNS_MAXDN]; /* domain name buffer */
1213 };
1214 .fi
1215 .RS
1216 The \fBdns_parse\fR structure is used to parse DNS reply packet.
1217 It holds information about the packet being parsed (dnsp_pkt, dnsp_end and
1218 dnsp_cur fields), number of RRs in the current section left to do, and
1219 the information about specific RR which we're looking for (dnsp_qdn,
1220 dnsp_qcls and dnsp_qtyp fields).
1221 .RE
1222
1223 .PP
1224 .nf
1225 int \fBdns_initparse\fR(struct dns_parse *\fIp\fR,
1226   const unsigned char *\fIqdn\fR,
1227   const unsigned char *\fIpkt\fR,
1228   const unsigned char *\fIcur\fR,
1229   const unsigned char *\fIend\fR)
1230 .fi
1231 .RS
1232 initializes the RR parsing structure \fIp\fR.  Arguments \fIpkt\fR, \fIcur\fR
1233 and \fIend\fR should describe the received packet: \fIpkt\fR is the start of
1234 the packet, \fIend\fR points to the next byte after the end of the packet,
1235 and \fIcur\fR points past the query DN in query section (to query class+type
1236 information).  And \fIqdn\fR points to the query DN.  This is the arguments
1237 passed to \fBdns_parse_fn\fR() routine. \fBdns_initparse\fR() initializes
1238 \fBdnsp_pkt\fR, \fBdnsp_end\fR and \fBdnsp_qdn\fR fields to the corresponding
1239 arguments, extracts and initializes \fBdnsp_qcls\fR and \fBdnsp_qtyp\fR
1240 fields to the values found at \fIcur\fR pointer, initializes
1241 \fBdnsp_cur\fR and \fBdnsp_ans\fR fields to be \fIcur\fR+4 (to the start of
1242 answer section), and initializes \fBdnsp_rrl\fR field to be number of entries
1243 in answer section. \fBdnsp_ttl\fR will be set to max TTL value, 0xffffffff,
1244 and \fBdnsp_nrr\fR to 0.
1245 .RE
1246
1247 .PP
1248 .nf
1249 int \fBdns_nextrr\fR(struct dns_parse *\fIp\fR, struct dns_rr *\fIrr\fR);
1250 .fi
1251 .RS
1252 searches for next RR in the packet based on the criteria provided in
1253 the \fIp\fR structure, filling in the \fIrr\fR structure and
1254 advancing \fIp\fR->\fBdnsp_cur\fR to the next RR in the packet.
1255 RR selection is based on dnsp_qdn, dnsp_qcls and dnsp_qtyp fields in
1256 the dns_parse structure.  Any (or all) of the 3 fields may be 0,
1257 which means any actual value from the packet is acceptable.  In case
1258 the field isn't 0 (or NULL for dnsp_qdn), only RRs with corresponding
1259 characteristics are acceptable.  Additionally, when dnsp_qdn is non-NULL,
1260 \fBdns_nextrr\fR() performs automatic CNAME expansion.
1261 Routine will return positive value on success, 0 in case it reached the end
1262 of current section in the packet (\fIp\fR->\fBdnsp_rrl\fR is zero), or
1263 negative value if next RR can not be decoded (packet format is invalid).
1264 The routine updates \fIp\fR->\fBdnsp_qdn\fR automatically when this
1265 field is non-NULL and it encounters appropriate CNAME RRs (saving CNAME
1266 target in \fIp\fR->\fBdnsp_dnbuf\fR), so after end of the process,
1267 \fIp\fR->\fBdnsp_qdn\fR will point to canonical name of the domain
1268 in question.  The routine updates \fIp\fR->\fBdnsp_ttl\fR value to
1269 be the minimum TTL of all RRs found.
1270 .RE
1271
1272 .PP
1273 .nf
1274 void \fBdns_rewind\fR(struct dns_parse *\fIp\fR, const unsigned char *\fIqdn\fR)
1275 .fi
1276 .RS
1277 this routine "rewinds" the packet parse state structure to be at the
1278 same state as after a call to \fBdns_initparse\fR(), i.e. reposition
1279 the parse structure \fIp\fR to the start of answer section and
1280 initialize \fIp\fR->\fBdnsp_rrl\fR to the number of entries in
1281 answer section.
1282 .RE
1283
1284 .PP
1285 .nf
1286 int \fBdns_stdrr_size\fR(const struct dns_parse *\fIp\fR);
1287 .fi
1288 .RS
1289 return size to hold standard RRset structure information, as shown
1290 in \fBdns_rr_null\fR structure (for the query and canonical
1291 names).  Used to calculate amount of memory to allocate for common
1292 part of type-specific RR structures in parsing routines.
1293 .RE
1294
1295 .PP
1296 .nf
1297 void *\fBdns_stdrr_finish\fR(struct dns_rr_null *\fIret\fR, char *\fIcp\fR,
1298   const struct dns_parse *\fIp\fR);
1299 .fi
1300 .RS
1301 initializes standard RRset fields in \fIret\fR structure using buffer
1302 pointed to by \fIcp\fR, which should have at least as many bytes
1303 as \fBdns_stdrr_size\fR(\fIp\fR) returned.  Used to finalize common
1304 part of type-specific RR structures in parsing routines.
1305 .RE
1306
1307 .PP
1308 See library source for usage examples of all the above low-level routines,
1309 especially source of the parsing routines.
1310
1311 .SS "Auxilary Routines"
1312
1313 .PP
1314 .nf
1315 int \fBdns_pton\fR(int \fIaf\fR, const char *\fIsrc\fR, void *\fIdst\fR);
1316 .fi
1317 .RS
1318 privides functionality similar to standard \fBinet_pton\fR() routine,
1319 to convert printable representation of an IP address of family \fIaf\fR
1320 (either \fBAF_INET\fR or \fBAF_INET6\fR) pointed to by \fIsrc\fR into
1321 binary form suitable for socket addresses and transmission over network,
1322 in buffer pointed to by \fIdst\fR.  The destination buffer should be
1323 of size 4 for \fBAF_INET\fR family or 16 for \fBAF_INET6\fR.
1324 The return value is positive on success, 0 if \fIsrc\fR is not a valid text
1325 representation of an address of family \fIaf\fR, or negative if the
1326 given address family is not supported.
1327 .RE
1328
1329 .PP
1330 .nf
1331 const char *\fBdns_ntop\fR(int \fIaf\fR, const void *\fIsrc\fR,
1332     char *\fIdst\fR, int \fIdstsize\fR)
1333 .fi
1334 .RS
1335 privides functionality similar to standard \fBinet_ntop\fR() routine,
1336 to convert binary representation of an IP address of family \fIaf\fR
1337 (either \fBAF_INET\fR or \fBAF_INET6\fR) pointed to by \fIsrc\fR
1338 (either 4 or 16 bytes) into printable form in buffer in buffer pointed
1339 to by \fIdst\fR of size \fIdstsize\fR.  The destination buffer should be
1340 at least of size 16 bytes for \fBAF_INET\fR family or 46 bytes for
1341 \fBAF_INET6\fR.  The return value is either \fIdst\fR, or NULL pointer
1342 if \fIdstsize\fR is too small to hold this address or if the given
1343 address family is not supported.
1344 .RE
1345
1346 .SH AUTHOR
1347 .PP
1348 The \fBudns\fR library has been written by Michael Tokarev, mjt+udns@tls.msk.ru.
1349
1350 .SH VERSION
1351 .PP
1352 This manual page corresponds to udns version 0.4, released Jan-2014.