chiark / gitweb /
70e743ba9a9649b6aa3017d72623aee479e1ba2e
[adns.git] / src / adns.h
1 /**/
2
3 #ifndef ADNS_H_INCLUDED
4 #define ADNS_H_INCLUDED
5
6 #include <sys/socket.h>
7 #include <netinet/in.h>
8
9 typedef struct adns__state *adns_state;
10 typedef struct adns__query *adns_query;
11
12 typedef 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 */
16   adns_if_noautosys=  0x0008, /* do not do full flow-of-control whenever we can */
17 } adns_initflags;
18
19 typedef enum {
20   adns_f_search=     0x0001, /* use the searchlist */
21   adns_f_usevc=      0x0002, /* use a virtual circuit (TCP connection) */
22   adns_f_anyquote=   0x0004,
23 } adns_queryflags;
24
25 typedef enum {
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
45 } adns_rrtype;
46
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
61 typedef enum {
62   adns_s_ok,
63   adns_s_notresponding,
64   adns_s_serverfailure,
65   adns_s_unknownqtype,
66   adns_s_remoteerror,
67   adns_s_nolocalmem,
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
77  * used.
78  */
79
80 typedef struct {
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;
98 } adns_answer;
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;
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;
118  *  If malloc failure occurs during internal allocation or processing
119  *  ands_check and _wait set *answer to 0.
120  */
121
122 int adns_init(adns_state *newstate_r, adns_initflags flags);
123
124 int adns_synchronous(adns_state ads,
125                      const char *owner,
126                      adns_rrtype type,
127                      adns_queryflags flags,
128                      adns_answer *answer);
129 /* Will not return EINTR. */
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  */
135
136 int adns_submit(adns_state ads,
137                 const char *owner,
138                 adns_rrtype type,
139                 adns_queryflags flags,
140                 void *context,
141                 adns_query *query_r);
142
143 int adns_check(adns_state ads,
144                adns_query *query_io,
145                adns_answer *answer,
146                void *context_r);
147
148 int adns_wait(adns_state ads,
149               adns_query *query_io,
150               adns_answer *answer,
151               void *context_r);
152 /* Might return EINTR - if so, try again */
153
154 void adns_cancel(adns_state ads, adns_query query);
155
156 int adns_finish(adns_state);
157
158 int 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.
167  */
168
169 void 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.
178  */
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
186  *  adns_check 3 -> EWOULDBLOCK
187  *  adns_wait 2
188  *  adns_wait 3
189  *  ....
190  *  adns_finish
191  *
192  *  adns_init _noautosys
193  *  loop {
194  *   adns_interest
195  *   select
196  *   adns_callback
197  *   ...
198  *   adns_submit / adns_check
199  *   ...
200  *  }
201  */
202
203 #endif