Parsing code revamped, not compiled yet.
[adns] / src / internal.h
CommitLineData
37e28fde 1/**/
2
3#ifndef ADNS_INTERNAL_H_INCLUDED
4#define ADNS_INTERNAL_H_INCLUDED
5
4353a5c4 6#define PRINTFFORMAT(a,b) __attribute__((format(printf,a,b)))
7typedef unsigned char byte;
8
9#include <stdarg.h>
10#include <assert.h>
11#include <unistd.h>
12
37e28fde 13#include <sys/time.h>
14
15#include "adns.h"
16
17/* Configuration and constants */
18
19#define MAXSERVERS 5
b9de380c 20#define UDPMAXRETRIES /*15*/5
37e28fde 21#define UDPRETRYMS 2000
22#define TCPMS 30000
23#define LOCALRESOURCEMS 20
b9de380c 24
98a3f706 25#define DNS_PORT 53
b9de380c 26#define DNS_MAXUDP 512
27#define DNS_MAXDOMAIN 255
28#define DNS_HDRSIZE 12
29#define DNS_CLASS_IN 1
37e28fde 30
98a3f706 31typedef enum {
32 rcode_noerror,
33 rcode_formaterror,
34 rcode_servfail,
35 rcode_nxdomain,
36 rcode_notimp,
37 rcode_refused
38} dns_rcode;
39
37e28fde 40/* Shared data structures */
41
4353a5c4 42typedef union {
37e28fde 43 adns_status status;
44 char *cp;
45 adns_rrtype type;
4353a5c4 46 int i;
37e28fde 47 struct in_addr ia;
48 unsigned long ul;
4353a5c4 49} rr_align;
50
51typedef struct {
52 int used, avail;
53 byte *buf;
54} vbuf;
55
56typedef union {
57 void *ext;
58 int dmaddr_index;
59} qcontext;
37e28fde 60
0ba0614a 61typedef struct {
0ba0614a 62 adns_rrtype type;
98a3f706 63 int rrsz;
31144a72 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 */
0ba0614a 77} typeinfo;
78
37e28fde 79struct adns__query {
80 /* FIXME: make sure this is all init'd properly */
ddfda861 81 enum { query_udp, query_tcpwait, query_tcpsent, query_child, query_done } state;
4353a5c4 82 adns_query back, next, parent;
37e28fde 83 struct { adns_query head, tail; } children;
84 struct { adns_query back, next; } siblings;
0ba0614a 85
86 const typeinfo *typei;
31144a72 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
f1e474dd 93 vbuf ansbuf; /* Used for answer RRs */
31144a72 94 char *cname_str;
95 byte *cname_dgram;
96 int cname_dglen, cname_begin;
97
ddfda861 98 int id, flags, udpretries;
4353a5c4 99 int udpnextserver;
100 unsigned long udpsent, tcpfailed; /* bitmap indexed by server */
37e28fde 101 struct timeval timeout;
4353a5c4 102 qcontext context;
37e28fde 103 char owner[1];
4353a5c4 104 /* After the owner name and nul comes the query message, pointed to by querymsg */
ddfda861 105
37e28fde 106 /* Possible states:
ddfda861 107 *
31144a72 108 * state Queue child id ansbuf nextudpserver sentudp failedtcp
ddfda861 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 +----------------+
37e28fde 156 */
157};
158
37e28fde 159struct adns__state {
37e28fde 160 adns_initflags iflags;
161 FILE *diagfile;
ddfda861 162 struct { adns_query head, tail; } timew, childw, output;
4353a5c4 163 int nextid, udpsocket, tcpsocket;
164 vbuf rqbuf, tcpsend, tcprecv;
37e28fde 165 int nservers, tcpserver;
8402e34c 166 enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
37e28fde 167 struct timeval tcptimeout;
168 struct server {
169 struct in_addr addr;
170 } servers[MAXSERVERS];
171};
172
173/* From setup.c: */
174
4353a5c4 175void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
37e28fde 176 int serv, const char *fmt, va_list al);
31144a72 177
178void adns__debug(adns_state ads, int serv, adns_query qu,
179 const char *fmt, ...) PRINTFFORMAT(3,4);
180void adns__warn(adns_state ads, int serv, adns_query qu,
181 const char *fmt, ...) PRINTFFORMAT(3,4);
182void adns__diag(adns_state ads, int serv, adns_query qu,
183 const char *fmt, ...) PRINTFFORMAT(3,4);
37e28fde 184
4353a5c4 185int adns__vbuf_ensure(vbuf *vb, int want);
31144a72 186int adns__vbuf_appendstr(vbuf *vb, const char *data);
4353a5c4 187int adns__vbuf_append(vbuf *vb, const byte *data, int len);
ddfda861 188/* 1=>success, 0=>realloc failed */
4353a5c4 189void adns__vbuf_appendq(vbuf *vb, const byte *data, int len);
190void adns__vbuf_init(vbuf *vb);
191
192int adns__setnonblock(adns_state ads, int fd); /* => errno value */
ddfda861 193
37e28fde 194/* From submit.c: */
195
4353a5c4 196void adns__query_nomem(adns_state ads, adns_query qu);
98a3f706 197void adns__query_finish(adns_state ads, adns_query qu, adns_status stat);
37e28fde 198void adns__query_fail(adns_state ads, adns_query qu, adns_status stat);
199
200/* From query.c: */
201
ddfda861 202void adns__query_udp(adns_state ads, adns_query qu, struct timeval now);
203void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now);
4bec51a4 204adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id,
98a3f706 205 const typeinfo *typei, adns_queryflags flags);
4353a5c4 206
207/* From reply.c: */
208
98a3f706 209void adns__procdgram(adns_state ads, const byte *dgram, int len,
210 int serv, struct timeval now);
211
212/* From types.c: */
213
214const typeinfo *adns__findtype(adns_rrtype type);
215
216/* From parse.c: */
217
f1e474dd 218typedef 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
225void 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
235adns_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
261adns_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
270adns_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 */
98a3f706 300
f1e474dd 301int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len);
8402e34c 302
303/* From event.c: */
4353a5c4 304
8402e34c 305void adns__tcp_broken(adns_state ads, const char *what, const char *why);
4353a5c4 306void adns__tcp_tryconnect(adns_state ads, struct timeval now);
307void adns__autosys(adns_state ads, struct timeval now);
37e28fde 308
309/* Useful static inline functions: */
310
311static inline void timevaladd(struct timeval *tv_io, long ms) {
312 struct timeval tmp;
313 assert(ms>=0);
314 tmp= *tv_io;
94436798 315 tmp.tv_usec += (ms%1000)*1000000;
37e28fde 316 tmp.tv_sec += ms/1000;
94436798 317 if (tmp.tv_usec >= 1000000) { tmp.tv_sec++; tmp.tv_usec -= 1000; }
37e28fde 318 *tv_io= tmp;
ddfda861 319}
37e28fde 320
321static inline int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
322static inline int ctype_digit(int c) { return c>='0' && c<='9'; }
98a3f706 323static inline int ctype_alpha(int c) {
324 return (c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z');
325}
37e28fde 326
327/* Useful macros */
328
4353a5c4 329#define LIST_INIT(list) ((list).head= (list).tail= 0)
330
37e28fde 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,)
4353a5c4 348#define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)
37e28fde 349
98a3f706 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
37e28fde 354#endif