You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1352 lines
56 KiB

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