Improving parsing code, and changing memory management. On its way,
[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 (*get_fn)(adns_state ads, adns_query qu, int serv,
65 const byte *dgram, int dglen,
66 int *cbyte_io, int max,
67 int nsstart, int arcount, int *arstart_io,
68 int roff, int *rcount_io);
69 } typeinfo;
70
71 struct adns__query {
72 /* FIXME: make sure this is all init'd properly */
73 enum { query_udp, query_tcpwait, query_tcpsent, query_child, query_done } state;
74 adns_query back, next, parent;
75 struct { adns_query head, tail; } children;
76 struct { adns_query back, next; } siblings;
77
78 const typeinfo *typei;
79 vbuf ansbuf; /* Used for answer RRs */
80 char *cname;
81 int id, flags, udpretries;
82 int udpnextserver;
83 unsigned long udpsent, tcpfailed; /* bitmap indexed by server */
84 struct timeval timeout;
85 byte *querymsg;
86 int querylen;
87 qcontext context;
88 char owner[1];
89 /* After the owner name and nul comes the query message, pointed to by querymsg */
90
91 /* Possible states:
92 *
93 * state Queue child id answer nextudpserver sentudp failedtcp
94 *
95 * udp NONE null >=0 null 0 zero zero
96 * udp timew null >=0 null any nonzero zero
97 * udp NONE null >=0 null any nonzero zero
98 *
99 * tcpwait timew null >=0 null irrelevant zero any
100 * tcpsent timew null >=0 null irrelevant zero any
101 *
102 * child childw set >=0 partial irrelevant irrelevant irrelevant
103 * done output null -1 set/null irrelevant irrelevant irrelevant
104 *
105 * +------------------------+
106 * START -----> | udp/NONE |
107 * +------------------------+
108 * / |\ \
109 * too big for UDP / UDP timeout \ \ send via UDP
110 * do this ASAP! / more retries \ \ do this ASAP!
111 * |_ desired \ _|
112 * +---------------+ +-----------+
113 * | tcpwait/timew | ____ | udp/timew |
114 * +---------------+ \ +-----------+
115 * | ^ | | |
116 * TCP conn'd; | | TCP died | | |
117 * send via TCP | | more | UDP timeout | |
118 * do this ASAP! | | servers | no more | |
119 * v | to try | retries | |
120 * +---------------+ | desired | |
121 * | tcpsent/timew | ____ | | |
122 * +---------------+ \| | |
123 * \ \ TCP died | TCP | |
124 * \ \ no more | timeout / |
125 * \ \ servers | / |
126 * \ \ to try | / |
127 * got \ \ v |_ / got
128 * reply \ _| +------------------+ / reply
129 * \ | done/output FAIL | /
130 * \ +------------------+ /
131 * \ /
132 * _| |_
133 * (..... got reply ....)
134 * / \
135 * need child query/ies / \ no child query
136 * / \
137 * |_ _|
138 * +--------------+ +----------------+
139 * | child/childw | ----------------> | done/output OK |
140 * +--------------+ children done +----------------+
141 */
142 };
143
144 struct adns__state {
145 adns_initflags iflags;
146 FILE *diagfile;
147 struct { adns_query head, tail; } timew, childw, output;
148 int nextid, udpsocket, tcpsocket;
149 vbuf rqbuf, tcpsend, tcprecv;
150 int nservers, tcpserver;
151 enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate;
152 struct timeval tcptimeout;
153 struct server {
154 struct in_addr addr;
155 } servers[MAXSERVERS];
156 };
157
158 /* From setup.c: */
159
160 void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent,
161 int serv, const char *fmt, va_list al);
162 void adns__debug(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
163 void adns__warn(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
164 void adns__diag(adns_state ads, int serv, const char *fmt, ...) PRINTFFORMAT(3,4);
165
166 int adns__vbuf_ensure(vbuf *vb, int want);
167 int adns__vbuf_append(vbuf *vb, const byte *data, int len);
168 /* 1=>success, 0=>realloc failed */
169 void adns__vbuf_appendq(vbuf *vb, const byte *data, int len);
170 void adns__vbuf_init(vbuf *vb);
171
172 int adns__setnonblock(adns_state ads, int fd); /* => errno value */
173
174 /* From submit.c: */
175
176 void adns__query_nomem(adns_state ads, adns_query qu);
177 void adns__query_finish(adns_state ads, adns_query qu, adns_status stat);
178 void adns__query_fail(adns_state ads, adns_query qu, adns_status stat);
179
180 /* From query.c: */
181
182 void adns__query_udp(adns_state ads, adns_query qu, struct timeval now);
183 void adns__query_tcp(adns_state ads, adns_query qu, struct timeval now);
184 adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id,
185 const typeinfo *typei, adns_queryflags flags);
186
187 /* From reply.c: */
188
189 void adns__procdgram(adns_state ads, const byte *dgram, int len,
190 int serv, struct timeval now);
191
192 /* From types.c: */
193
194 const typeinfo *adns__findtype(adns_rrtype type);
195
196 /* From parse.c: */
197
198 typedef struct {
199 adns_state ads, int serv;
200 const byte *dgram;
201 int dglen, max, cbyte, namelen;
202 int *dmend_rlater, *namelen_rlater;
203 } findlabel_state;
204
205 void adns__findlabel_start(findlabel_state *fls,
206 adns_state ads, int serv,
207 const byte *dgram, int dglen, int max,
208 int dmbegin, int *dmend_rlater);
209 /* Finds labels in a domain in a datagram.
210 *
211 * Call this routine first.
212 * endpoint_rlater may be null.
213 */
214
215 adns_status adns__findlabel_next(findlabel_state *fls,
216 int *lablen_r, int *labstart_r);
217 /* Then, call this one repeatedly.
218 *
219 * It will return adns_s_ok if all is well, and tell you the length
220 * and start of successive labels. labstart_r may be null, but
221 * lablen_r must not be.
222 *
223 * After the last label, it will return with *lablen_r zero.
224 * Do not then call it again; instead, just throw away the findlabel_state.
225 *
226 * *dmend_rlater will have been set to point to the next part of
227 * the datagram after the label (or after the uncompressed part,
228 * if compression was used). *namelen_rlater will have been set
229 * to the length of the domain name (total length of labels plus
230 * 1 for each intervening dot).
231 *
232 * If the datagram appears to be truncated, *lablen_r will be -1.
233 * *dmend_rlater, *labstart_r and *namelen_r may contain garbage.
234 * Do not call _next again.
235 *
236 * There may also be errors, in which case *dmend_rlater,
237 * *namelen_rlater, *lablen_r and *labstart_r may contain garbage.
238 * Do not then call findlabel_next again.
239 */
240
241 adns_status adns__parse_domain(adns_state ads, int serv, vbuf *vb,
242 const byte *dgram, int dglen,
243 int *cbyte_io, int max);
244 /* vb must already have been initialised; it will be reset if necessary.
245 * If there is truncation, vb->used will be set to 0; otherwise
246 * (if there is no error) vb will be null-terminated.
247 * If there is an error vb and *cbyte_io may be left indeterminate.
248 */
249
250 adns_status adns__findrr(adns_state ads, int serv,
251 const byte *dgram, int dglen, int *cbyte_io,
252 int *type_r, int *class_r, int *rdlen_r, int *rdstart_r,
253 const byte *eo_dgram, int eo_dglen, int eo_cbyte,
254 int *eo_matched_r);
255 /* Finds the extent and some of the contents of an RR in a datagram
256 * and does some checks. The datagram is *dgram, length dglen, and
257 * the RR starts at *cbyte_io (which is updated afterwards to point
258 * to the end of the RR).
259 *
260 * The type, class and RRdata length and start are returned iff
261 * the corresponding pointer variables are not null. type_r and
262 * class_r may not be null.
263 *
264 * If the caller thinks they know what the owner of the RR ought to
265 * be they can pass in details in eo_*: this is another (or perhaps
266 * the same datagram), and a pointer to where the putative owner
267 * starts in that datagram. In this case *eo_matched_r will be set
268 * to 1 if the datagram matched or 0 if it did not. Either
269 * both eo_dgram and eo_matched_r must both be non-null, or they
270 * must both be null (in which case eo_dglen and eo_cbyte will be ignored).
271 * The eo datagram and contained owner domain MUST be valid and
272 * untruncated.
273 *
274 * If there is truncation then *type_r will be set to -1 and
275 * *cbyte_io, *class_r, *rdlen_r, *rdstart_r and *eo_matched_r will be
276 * undefined.
277 *
278 * If an error is returned then *type_r will be undefined too.
279 */
280
281 int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len);
282
283 /* From event.c: */
284
285 void adns__tcp_broken(adns_state ads, const char *what, const char *why);
286 void adns__tcp_tryconnect(adns_state ads, struct timeval now);
287 void adns__autosys(adns_state ads, struct timeval now);
288
289 /* Useful static inline functions: */
290
291 static inline void timevaladd(struct timeval *tv_io, long ms) {
292 struct timeval tmp;
293 assert(ms>=0);
294 tmp= *tv_io;
295 tmp.tv_usec += (ms%1000)*1000000;
296 tmp.tv_sec += ms/1000;
297 if (tmp.tv_usec >= 1000000) { tmp.tv_sec++; tmp.tv_usec -= 1000; }
298 *tv_io= tmp;
299 }
300
301 static inline int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; }
302 static inline int ctype_digit(int c) { return c>='0' && c<='9'; }
303 static inline int ctype_alpha(int c) {
304 return (c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z');
305 }
306
307 /* Useful macros */
308
309 #define LIST_INIT(list) ((list).head= (list).tail= 0)
310
311 #define LIST_UNLINK_PART(list,node,part) \
312 do { \
313 if ((node)->back) (node)->back->part next= (node)->part next; \
314 else (list).head= (node)->part next; \
315 if ((node)->next) (node)->next->part back= (node)->part back; \
316 else (list).tail= (node)->part back; \
317 } while(0)
318
319 #define LIST_LINK_TAIL_PART(list,node,part) \
320 do { \
321 (node)->part back= 0; \
322 (node)->part next= (list).tail; \
323 if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \
324 (list).tail= (node); \
325 } while(0)
326
327 #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,)
328 #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,)
329
330 #define GETIL_B(cb) (dgram[(cb)++])
331 #define GET_B(cb,tv) ((tv)= GETIL_B((cb)))
332 #define GET_W(cb,tv) ((tv)=0, (tv)|=(GETIL_B((cb))<<8), (tv)|=GETIL_B(cb), (tv))
333
334 #endif