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))) |
7 | typedef 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 |
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 | |
37e28fde |
40 | /* Shared data structures */ |
41 | |
4353a5c4 |
42 | typedef 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 | |
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; |
37e28fde |
60 | |
0ba0614a |
61 | typedef struct { |
0ba0614a |
62 | adns_rrtype type; |
98a3f706 |
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); |
0ba0614a |
69 | } typeinfo; |
70 | |
37e28fde |
71 | struct adns__query { |
72 | /* FIXME: make sure this is all init'd properly */ |
ddfda861 |
73 | enum { query_udp, query_tcpwait, query_tcpsent, query_child, query_done } state; |
4353a5c4 |
74 | adns_query back, next, parent; |
37e28fde |
75 | struct { adns_query head, tail; } children; |
76 | struct { adns_query back, next; } siblings; |
0ba0614a |
77 | |
78 | const typeinfo *typei; |
f1e474dd |
79 | vbuf ansbuf; /* Used for answer RRs */ |
80 | char *cname; |
ddfda861 |
81 | int id, flags, udpretries; |
4353a5c4 |
82 | int udpnextserver; |
83 | unsigned long udpsent, tcpfailed; /* bitmap indexed by server */ |
37e28fde |
84 | struct timeval timeout; |
4353a5c4 |
85 | byte *querymsg; |
f1e474dd |
86 | int querylen; |
4353a5c4 |
87 | qcontext context; |
37e28fde |
88 | char owner[1]; |
4353a5c4 |
89 | /* After the owner name and nul comes the query message, pointed to by querymsg */ |
ddfda861 |
90 | |
37e28fde |
91 | /* Possible states: |
ddfda861 |
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 +----------------+ |
37e28fde |
141 | */ |
142 | }; |
143 | |
37e28fde |
144 | struct adns__state { |
37e28fde |
145 | adns_initflags iflags; |
146 | FILE *diagfile; |
ddfda861 |
147 | struct { adns_query head, tail; } timew, childw, output; |
4353a5c4 |
148 | int nextid, udpsocket, tcpsocket; |
149 | vbuf rqbuf, tcpsend, tcprecv; |
37e28fde |
150 | int nservers, tcpserver; |
8402e34c |
151 | enum adns__tcpstate { server_disconnected, server_connecting, server_ok } tcpstate; |
37e28fde |
152 | struct timeval tcptimeout; |
153 | struct server { |
154 | struct in_addr addr; |
155 | } servers[MAXSERVERS]; |
156 | }; |
157 | |
158 | /* From setup.c: */ |
159 | |
4353a5c4 |
160 | void adns__vdiag(adns_state ads, const char *pfx, adns_initflags prevent, |
37e28fde |
161 | int serv, const char *fmt, va_list al); |
8402e34c |
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); |
37e28fde |
165 | |
4353a5c4 |
166 | int adns__vbuf_ensure(vbuf *vb, int want); |
167 | int adns__vbuf_append(vbuf *vb, const byte *data, int len); |
ddfda861 |
168 | /* 1=>success, 0=>realloc failed */ |
4353a5c4 |
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 */ |
ddfda861 |
173 | |
37e28fde |
174 | /* From submit.c: */ |
175 | |
4353a5c4 |
176 | void adns__query_nomem(adns_state ads, adns_query qu); |
98a3f706 |
177 | void adns__query_finish(adns_state ads, adns_query qu, adns_status stat); |
37e28fde |
178 | void adns__query_fail(adns_state ads, adns_query qu, adns_status stat); |
179 | |
180 | /* From query.c: */ |
181 | |
ddfda861 |
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); |
4bec51a4 |
184 | adns_status adns__mkquery(adns_state ads, const char *owner, int ol, int id, |
98a3f706 |
185 | const typeinfo *typei, adns_queryflags flags); |
4353a5c4 |
186 | |
187 | /* From reply.c: */ |
188 | |
98a3f706 |
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 | |
f1e474dd |
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 | */ |
98a3f706 |
280 | |
f1e474dd |
281 | int vbuf__append_quoted1035(vbuf *vb, const byte *buf, int len); |
8402e34c |
282 | |
283 | /* From event.c: */ |
4353a5c4 |
284 | |
8402e34c |
285 | void adns__tcp_broken(adns_state ads, const char *what, const char *why); |
4353a5c4 |
286 | void adns__tcp_tryconnect(adns_state ads, struct timeval now); |
287 | void adns__autosys(adns_state ads, struct timeval now); |
37e28fde |
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; |
94436798 |
295 | tmp.tv_usec += (ms%1000)*1000000; |
37e28fde |
296 | tmp.tv_sec += ms/1000; |
94436798 |
297 | if (tmp.tv_usec >= 1000000) { tmp.tv_sec++; tmp.tv_usec -= 1000; } |
37e28fde |
298 | *tv_io= tmp; |
ddfda861 |
299 | } |
37e28fde |
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'; } |
98a3f706 |
303 | static inline int ctype_alpha(int c) { |
304 | return (c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z'); |
305 | } |
37e28fde |
306 | |
307 | /* Useful macros */ |
308 | |
4353a5c4 |
309 | #define LIST_INIT(list) ((list).head= (list).tail= 0) |
310 | |
37e28fde |
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,) |
4353a5c4 |
328 | #define LIST_LINK_TAIL(list,node) LIST_LINK_TAIL_PART(list,node,) |
37e28fde |
329 | |
98a3f706 |
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 | |
37e28fde |
334 | #endif |