Parsing code revamped, not compiled yet.
[adns] / src / internal.h
1 /**/
2
3 #ifndef ADNS_INTERNAL_H_INCLUDED
4 #define ADNS_INTERNAL_H_INCLUDED
5
6 #define PRINTFFORMAT(a,b) __attribute__((format(printf,a,b)))
7 typedef unsigned char byte;
8
9 #include <stdarg.h>
10 #include <assert.h>
11 #include <unistd.h>
12
13 #include <sys/time.h>
14
15 #include "adns.h"
16
17 /* Configuration and constants */
18
19 #define MAXSERVERS 5
20 #define UDPMAXRETRIES /*15*/5
21 #define UDPRETRYMS 2000
22 #define TCPMS 30000
23 #define LOCALRESOURCEMS 20
24
25 #define DNS_PORT 53
26 #define DNS_MAXUDP 512
27 #define DNS_MAXDOMAIN 255
28 #define DNS_HDRSIZE 12
29 #define DNS_CLASS_IN 1
30
31 typedef enum {
32 rcode_noerror,
33 rcode_formaterror,
34 rcode_servfail,
35 rcode_nxdomain,
36 rcode_notimp,
37 rcode_refused
38 } dns_rcode;
39
40 /* Shared data structures */
41
42 typedef union {
43 adns_status status;
44 char *cp;
45 adns_rrtype type;
46 int i;
47 struct in_addr ia;
48 unsigned long ul;
49 } rr_align;
50
51 typedef struct {
52 int used, avail;
53 byte *buf;
54 } vbuf;
55
56 typedef union {
57 void *ext;
58 int dmaddr_index;
59 } qcontext;
60
61 typedef struct {
62 adns_rrtype type;
63 int rrsz;
64 adns_status (*parse)(adns_state ads, adns_query qu, int serv, vbuf *vb,
65 const byte *dgram, int dglen, int cbyte, int max,
66 void *store_r);
67 /* Parse one RR, in dgram of length dglen, starting at cbyte and
68 * extending until at most max.
69 *
70 * The RR should be stored at *store_r, of length qu->typei->rrsz.
71 *
72 * If there is an overrun which might indicate truncation, it should set
73 * *rdstart to -1; otherwise it may set it to anything else positive.
74 *
75 * This function may use vb (which has been initialised) however it likes.
76 */
77 } typeinfo;
78
79 struct adns__query {
80 /* FIXME: make sure this is all init'd properly */
81 enum { query_udp, query_tcpwait, query_tcpsent, query_child, query_done } state;
82 adns_query back, next, parent;
83 struct { adns_query head, tail; } children;
84 struct { adns_query back, next; } siblings;
85
86 const typeinfo *typei;
87
88 #error make sure all this is init'd properly
89 #error make sure all this is freed properly
90 byte *querymsg;
91 int querylen;
92
93 vbuf ansbuf; /* Used for answer RRs */
94 char *cname_str;
95 byte *cname_dgram;
96 int cname_dglen, cname_begin;
97
98 int id, flags, udpretries;
99 int udpnextserver;
100 unsigned long udpsent, tcpfailed; /* bitmap indexed by server */
101 struct timeval timeout;
102 qcontext context;
103 char owner[1];
104 /* After the owner name and nul comes the query message, pointed to by querymsg */
105
106 /* Possible states:
107 *
108 * state Queue child id ansbuf nextudpserver sentudp failedtcp
109 *
110 * udp NONE null >=0 null 0 zero zero
111 * udp timew null >=0 null any nonzero zero
112 * udp NONE null >=0 null any nonzero zero
113 *
114 * tcpwait timew null >=0 null irrelevant zero any
115 * tcpsent timew null >=0 null irrelevant zero any
116 *
117 * child childw set >=0 partial irrelevant irrelevant irrelevant
118 * done output null -1 set/null irrelevant irrelevant irrelevant
119 *
120 * +------------------------+
121 * START -----> | udp/NONE |
122 * +------------------------+
123 * / |\ \
124 * too big for UDP / UDP timeout \ \ send via UDP
125 * do this ASAP! / more retries \ \ do this ASAP!
126 * |_ desired \ _|
127 * +---------------+ +-----------+
128 * | tcpwait/timew | ____ | udp/timew |
129 * +---------------+ \ +-----------+
130 * | ^ | | |
131 * TCP conn'd; | | TCP died | | |
132 * send via TCP | | more | UDP timeout | |
133 * do this ASAP! | | servers | no more | |
134 * v | to try | retries | |
135 * +---------------+ | desired | |
136 * | tcpsent/timew | ____ | | |
137 * +---------------+ \| | |
138 * \ \ TCP died | TCP | |
139 * \ \ no more | timeout / |
140 * \ \ servers | / |
141 * \ \ to try | / |
142 * got \ \ v |_ / got
143 * reply \ _| +------------------+ / reply
144 * \ | done/output FAIL | /
145 * \ +------------------+ /
146 * \ /
147 * _| |_
148 * (..... got reply ....)
149 * / \
150 * need child query/ies / \ no child query
151 * / \
152 * |_ _|
153 * +--------------+ +----------------+
154 * | child/childw | ----------------> | done/output OK |
155 * +--------------+ children done +----------------+
156 */
157 };
158
159 struct adns__state {
160 adns_initflags iflags;
161 FILE *diagfile;
162 struct { adns_query head, tail; } timew, childw, output;
163 int nextid, udpsocket, tcpsocket;
164 vbuf rqbuf, tcpsend, tcprecv;
165 int nservers, tcpserver;
166 enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
167 struct timeval tcptimeout;
168 struct server {
169 struct in_addr addr;
170 } servers[MAXSERVERS];
171 };
172
173 /* From setup.c: */
174
175 void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
176 int serv, const char *fmt, va_list al);
177
178 void adns__debug(adns_state ads, int serv, adns_query qu,
179 const char *fmt, ...) PRINTFFORMAT(3,4);
180 void adns__warn(adns_state ads, int serv, adns_query qu,
181 const char *fmt, ...) PRINTFFORMAT(3,4);
182 void adns__diag(adns_state ads, int serv, adns_query qu,
183 const char *fmt, ...) PRINTFFORMAT(3,4);
184
185 int adns__vbuf_ensure(vbuf *vb, int want);
186 int adns__vbuf_appendstr(vbuf *vb, const char *data);
187 int adns__vbuf_append(vbuf *vb, const byte *data, int len);
188 /* 1=>success, 0=>realloc failed */
189 void adns__vbuf_appendq(vbuf *vb, const byte *data, int len);
190 void adns__vbuf_init(vbuf *vb);
191
192 int adns__setnonblock(adns_state ads, int fd); /* => errno value */
193
194 /* From submit.c: */
195
196 void adns__query_nomem(adns_state ads, adns_query qu);
197 void adns__query_finish(adns_state ads, adns_query qu, adns_status stat);
198 void adns__query_fail(adns_state ads, adns_query qu, adns_status stat);
199
200 /* From query.c: */
201
202 void adns__query_udp(adns_state ads, adns_query qu, struct timeval now);
203 void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now);
204 adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id,
205 const typeinfo *typei, adns_queryflags flags);
206
207 /* From reply.c: */
208
209 void adns__procdgram(adns_state ads, const byte *dgram, int len,
210 int serv, struct timeval now);
211
212 /* From types.c: */
213
214 const typeinfo *adns__findtype(adns_rrtype type);
215
216 /* From parse.c: */
217
218 typedef struct {
219 adns_state ads, int serv;
220 const byte *dgram;
221 int dglen, max, cbyte, namelen;
222 int *dmend_rlater, *namelen_rlater;
223 } findlabel_state;
224
225 void adns__findlabel_start(findlabel_state *fls,
226 adns_state ads, int serv,
227 const byte *dgram, int dglen, int max,
228 int dmbegin, int *dmend_rlater);
229 /* Finds labels in a domain in a datagram.
230 *
231 * Call this routine first.
232 * endpoint_rlater may be null.
233 */
234
235 adns_status adns__findlabel_next(findlabel_state *fls,
236 int *lablen_r, int *labstart_r);
237 /* Then, call this one repeatedly.
238 *
239 * It will return adns_s_ok if all is well, and tell you the length
240 * and start of successive labels. labstart_r may be null, but
241 * lablen_r must not be.
242 *
243 * After the last label, it will return with *lablen_r zero.
244 * Do not then call it again; instead, just throw away the findlabel_state.
245 *
246 * *dmend_rlater will have been set to point to the next part of
247 * the datagram after the label (or after the uncompressed part,
248 * if compression was used). *namelen_rlater will have been set
249 * to the length of the domain name (total length of labels plus
250 * 1 for each intervening dot).
251 *
252 * If the datagram appears to be truncated, *lablen_r will be -1.
253 * *dmend_rlater, *labstart_r and *namelen_r may contain garbage.
254 * Do not call _next again.
255 *
256 * There may also be errors, in which case *dmend_rlater,
257 * *namelen_rlater, *lablen_r and *labstart_r may contain garbage.
258 * Do not then call findlabel_next again.
259 */
260
261 adns_status adns__parse_domain(adns_state ads, int serv, vbuf *vb,
262 const byte *dgram, int dglen,
263 int *cbyte_io, int max);
264 /* vb must already have been initialised; it will be reset if necessary.
265 * If there is truncation, vb->used will be set to 0; otherwise
266 * (if there is no error) vb will be null-terminated.
267 * If there is an error vb and *cbyte_io may be left indeterminate.
268 */
269
270 adns_status adns__findrr(adns_state ads, int serv,
271 const byte *dgram, int dglen, int *cbyte_io,
272 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
273 const byte *eo_dgram, int eo_dglen, int eo_cbyte,
274 int *eo_matched_r);
275 /* Finds the extent and some of the contents of an RR in a datagram
276 * and does some checks. The datagram is *dgram, length dglen, and
277 * the RR starts at *cbyte_io (which is updated afterwards to point
278 * to the end of the RR).
279 *
280 * The type, class and RRdata length and start are returned iff
281 * the corresponding pointer variables are not null. type_r and
282 * class_r may not be null.
283 *
284 * If the caller thinks they know what the owner of the RR ought to
285 * be they can pass in details in eo_*: this is another (or perhaps
286 * the same datagram), and a pointer to where the putative owner
287 * starts in that datagram. In this case *eo_matched_r will be set
288 * to 1 if the datagram matched or 0 if it did not. Either
289 * both eo_dgram and eo_matched_r must both be non-null, or they
290 * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
291 * The eo datagram and contained owner domain MUST be valid and
292 * untruncated.
293 *
294 * If there is truncation then *type_r will be set to -1 and
295 * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
296 * undefined.
297 *
298 * If an error is returned then *type_r will be undefined too.
299 */
300
301 int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len);
302
303 /* From event.c: */
304
305 void adns__tcp_broken(adns_state ads, const char *what, const char *why);
306 void adns__tcp_tryconnect(adns_state ads, struct timeval now);
307 void adns__autosys(adns_state ads, struct timeval now);
308
309 /* Useful static inline functions: */
310
311 static inline void timevaladd(struct timeval *tv_io, long ms) {
312 struct timeval tmp;
313 assert(ms>=0);
314 tmp= *tv_io;
315 tmp.tv_usec += (ms%1000)*1000000;
316 tmp.tv_sec += ms/1000;
317 if (tmp.tv_usec >= 1000000) { tmp.tv_sec++; tmp.tv_usec -= 1000; }
318 *tv_io= tmp;
319 }
320
321 static inline int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
322 static inline int ctype_digit(int c) { return c>='0' && c<='9'; }
323 static inline int ctype_alpha(int c) {
324 return (c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z');
325 }
326
327 /* Useful macros */
328
329 #define LIST_INIT(list) ((list).head= (list).tail= 0)
330
331 #define LIST_UNLINK_PART(list,node,part) \
332 do { \
333 if ((node)->back) (node)->back->part next= (node)->part next; \
334 else (list).head= (node)->part next; \
335 if ((node)->next) (node)->next->part back= (node)->part back; \
336 else (list).tail= (node)->part back; \
337 } while(0)
338
339 #define LIST_LINK_TAIL_PART(list,node,part) \
340 do { \
341 (node)->part back= 0; \
342 (node)->part next= (list).tail; \
343 if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
344 (list).tail= (node); \
345 } while(0)
346
347 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
348 #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)
349
350 #define GETIL_B(cb) (dgram[(cb)++])
351 #define GET_B(cb,tv) ((tv)= GETIL_B((cb)))
352 #define GET_W(cb,tv) ((tv)=0, (tv)|=(GETIL_B((cb))<<8), (tv)|=GETIL_B(cb), (tv))
353
354 #endif