Makes up queries.
[adns] / src / adns.h
CommitLineData
a17d3a1d 1/**/
2
3#ifndef ADNS_H_INCLUDED
4#define ADNS_H_INCLUDED
5
964344fc 6#include <sys/socket.h>
7#include <netinet/in.h>
8
9typedef struct adns__state *adns_state;
10typedef struct adns__query *adns_query;
a17d3a1d 11
12typedef enum {
13 adns_if_noenv= 0x0001, /* do not look at environment */
14 adns_if_noerrprint= 0x0002, /* never print output to stderr */
15 adns_if_debug= 0x0004, /* print debugging output to stderr */
e8c505a5 16 adns_if_noautosys= 0x0008, /* do not do full flow-of-control whenever we can */
a17d3a1d 17} adns_initflags;
18
19typedef enum {
20 adns_f_search= 0x0001, /* use the searchlist */
21 adns_f_usevc= 0x0002, /* use a virtual circuit (TCP connection) */
964344fc 22 adns_f_anyquote= 0x0004,
a17d3a1d 23} adns_queryflags;
24
25typedef enum {
350d37d9 26 adns__rrt_typemask= 0x0ffff,
27 adns__qtf_deref= 0x10000, /* dereference domains and produce extra data */
28 adns__qtf_mailconv= 0x20000, /* put @ between first and second labels */
29 adns_r_none= 0,
30 adns_r_a= 1,
31 adns_r_ns_raw= 2,
32 adns_r_ns= adns_r_ns_raw|adns__qtf_deref,
33 adns_r_cname= 5,
34 adns_r_soa_raw= 6,
35 adns_r_soa= adns_r_soa_raw|adns__qtf_mailconv,
36 adns_r_null= 10,
37 adns_r_ptr_raw= 12,
38 adns_r_ptr= adns_r_ptr_raw|adns__qtf_deref,
39 adns_r_hinfo= 13,
40 adns_r_mx_raw= 15,
41 adns_r_mx= adns_r_mx_raw|adns__qtf_deref,
42 adns_r_txt= 16,
43 adns_r_rp_raw= 17,
44 adns_r_rp= adns_r_rp_raw|adns__qtf_mailconv
a17d3a1d 45} adns_rrtype;
46
964344fc 47/* In queries without qtf_anyquote, all domains must have standard
48 * legal syntax. In queries _with_ qtf_anyquote, domains in the query
49 * or response may contain any characters, quoted according to
50 * RFC1035 5.1. On input to adns, the char* is a pointer to the
51 * interior of a " delimited string, except that " may appear in it,
52 * and on output, the char* is a pointer to a string which would be
53 * legal either inside or outside " delimiters, and any characters
54 * not usually legal in domain names will be quoted as \X
55 * (if the character is 33-126 except \ and ") or \DDD.
56 *
57 * Do not ask for records containing mailboxes without
58 * specifying qtf_mailconv or qtf_anyquote.
59 */
60
a17d3a1d 61typedef enum {
62 adns_s_ok,
63 adns_s_notresponding,
64 adns_s_serverfailure,
65 adns_s_unknownqtype,
66 adns_s_remoteerror,
350d37d9 67 adns_s_nolocalmem,
a17d3a1d 68 adns_s_max_tempfail= 99,
69 adns_s_nxdomain,
70 adns_s_norecord,
71 adns_s_invaliddomain
72} adns_status;
73
74/* In dereferenced answers, multiple addresses show up as multiple
75 * answers with all the dm pointers being the same. If no
76 * address is available (permanent failure) then INADDR_NONE is
964344fc 77 * used.
78 */
a17d3a1d 79
e8c505a5 80typedef struct {
a17d3a1d 81 adns_status status;
82 char *cname; /* always NULL if query was for CNAME records */
83 adns_rrtype type;
84 int nrrs;
85 union {
86 struct in_addr inaddr[1]; /* a */
87 char (*str)[1]; /* ns_raw, cname, ptr, ptr_raw, txt */
88 struct { char *dm; struct in_addr addr; } dmaddr; /* ns */
89 struct { char *a, *b; } strpair[1]; /* hinfo, rp, rp_raw */
90 struct { int pref; char *dm; struct in_addr addr; } intdmaddr[1]; /* mx */
91 struct { int pref; char *str; } intstr[1]; /* mx_raw */
92 struct {
93 char *ns0, *rp;
94 unsigned long serial, refresh, retry, expire, minimum;
95 } soa[1]; /* soa, soa_raw */
96 /* NULL is empty */
97 } rrs;
e8c505a5 98} adns_answer;
a17d3a1d 99
100/* Memory management:
101 * adns_state and adns_query are actually pointers to malloc'd state;
102 * On submission questions are copied, including the owner domain;
103 * Answers are malloc'd as a single piece of memory.
104 * query_io:
105 * Must always be non-null pointer;
106 * If *query_io is 0 to start with then any query may be returned;
107 * If *query_io is !0 adns_query then only that query may be returned.
108 * Errors:
109 * Return values are 0 or an errno value;
110 * Seriously fatal system errors (eg, failure to create sockets,
111 * malloc failure, etc.) return errno values;
112 * Other errors (nameserver failure, timed out connections, &c)
113 * are returned in the status field of the answer. If status is
114 * nonzero then nrrs will be 0, otherwise it will be >0.
115 * type will always be the type requested;
e8c505a5 116 * If no (appropriate) requests are done adns_check returns EWOULDBLOCK;
117 * If no (appropriate) requests are outstanding adns_query and adns_wait return ESRCH;
a17d3a1d 118 * If malloc failure occurs during internal allocation or processing
e8c505a5 119 * ands_check and _wait set *answer to 0.
a17d3a1d 120 */
121
964344fc 122int adns_init(adns_state *newstate_r, adns_initflags flags);
a17d3a1d 123
124int adns_synchronous(adns_state ads,
125 const char *owner,
126 adns_rrtype type,
72934832 127 adns_queryflags flags,
e8c505a5 128 adns_answer *answer);
72934832 129/* Will not return EINTR. */
e8c505a5 130
131/* NB: if you set adns_if_noautosys then _submit and _check do not
132 * make any system calls; you must use adns_callback (possibly after
133 * adns_interest) to actually get things to happen.
134 */
a17d3a1d 135
136int adns_submit(adns_state ads,
137 const char *owner,
138 adns_rrtype type,
72934832 139 adns_queryflags flags,
a17d3a1d 140 void *context,
964344fc 141 adns_query *query_r);
a17d3a1d 142
964344fc 143int adns_check(adns_state ads,
a17d3a1d 144 adns_query *query_io,
e8c505a5 145 adns_answer *answer,
a17d3a1d 146 void *context_r);
147
148int adns_wait(adns_state ads,
149 adns_query *query_io,
e8c505a5 150 adns_answer *answer,
964344fc 151 void *context_r);
72934832 152/* Might return EINTR - if so, try again */
a17d3a1d 153
e8c505a5 154void adns_cancel(adns_state ads, adns_query query);
a17d3a1d 155
156int adns_finish(adns_state);
157
e8c505a5 158int adns_callback(adns_state, int maxfd, const fd_set *readfds, const fd_set *writefds,
159 const fd_set *exceptfds);
160/* Gives adns flow-of-control for a bit. This will never block.
161 * If maxfd == -1 then adns will check (make nonblocking system calls on)
162 * all of its own filedescriptors; otherwise it will only use those
163 * < maxfd and specified in the fd_set's, as if select had returned them.
164 * Other fd's may be in the fd_sets, and will be ignored.
165 * _callback returns how many adns fd's were in the various sets, so
166 * you can tell if your select handling code has missed something and is going awol.
964344fc 167 */
168
e8c505a5 169void adns_interest(adns_state, int *maxfd_io, fd_set *readfds_io,
170 fd_set *writefds_io, fd_set *exceptfds_io,
171 struct timeval **tv_mod, struct timeval *tv_buf);
172/* Find out file descriptors adns is interested in, and when it
173 * would like the opportunity to time something out. If you do not plan to
174 * block then tv_mod may be 0. Otherwise, tv_mod may point to 0 meaning
175 * you have no timeout of your own, in which case tv_buf must be non-null and
176 * _interest may fill it in and set *tv_mod=tv_buf.
177 * readfds, writefds, exceptfds and maxfd may not be 0.
964344fc 178 */
a17d3a1d 179
180/* Example expected/legal calling sequences:
181 * adns_init
182 * adns_submit 1
183 * adns_submit 2
184 * adns_submit 3
185 * adns_wait 1
964344fc 186 * adns_check 3 -> EWOULDBLOCK
a17d3a1d 187 * adns_wait 2
188 * adns_wait 3
189 * ....
190 * adns_finish
191 *
e8c505a5 192 * adns_init _noautosys
a17d3a1d 193 * loop {
a17d3a1d 194 * adns_interest
195 * select
196 * adns_callback
e8c505a5 197 * ...
198 * adns_submit / adns_check
199 * ...
a17d3a1d 200 * }
201 */
202
203#endif