5db99a2e |
1 | /**/ |
2 | |
e7afa27f |
3 | #include <stdarg.h> |
4 | #include <stdio.h> |
5 | #include <errno.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
5617a135 |
8 | #include <assert.h> |
59258c2d |
9 | #include <unistd.h> |
e7afa27f |
10 | |
59258c2d |
11 | #include <netdb.h> |
5db99a2e |
12 | #include <arpa/nameser.h> |
e7afa27f |
13 | #include <sys/socket.h> |
14 | #include <netinet/in.h> |
15 | #include <arpa/inet.h> |
5db99a2e |
16 | |
17 | #include "adns-internal.h" |
18 | |
6927715a |
19 | #define LIST_UNLINK_PART(list,node,part) \ |
5db99a2e |
20 | do { \ |
6927715a |
21 | if ((node)->back) (node)->back->part next= (node)->part next; \ |
22 | else (list).head= (node)->part next; \ |
23 | if ((node)->next) (node)->next->part back= (node)->part back; \ |
24 | else (list).tail= (node)->part back; \ |
5db99a2e |
25 | } while(0) |
26 | |
6927715a |
27 | #define LIST_LINK_TAIL_PART(list,node,part) \ |
5db99a2e |
28 | do { \ |
6927715a |
29 | (node)->part back= 0; \ |
30 | (node)->part next= (list).tail; \ |
31 | if ((list).tail) (list).tail->part back= (node); else (list).part head= (node); \ |
5db99a2e |
32 | (list).tail= (node); \ |
33 | } while(0) |
34 | |
6927715a |
35 | #define LIST_UNLINK(list,node) LIST_UNLINK_PART(list,node,) |
36 | #define LIST_LINK_TAIL_PART(list,node) LIST_LINK_TAIL(list,node,) |
37 | |
e7afa27f |
38 | static void vdebug(adns_state ads, const char *fmt, va_list al) { |
39 | if (!(ads->iflags & adns_if_debug)) return; |
40 | fputs("adns debug: ",stderr); |
41 | vfprintf(stderr,fmt,al); |
42 | fputc('\n',stderr); |
43 | } |
44 | |
45 | static void debug(adns_state ads, const char *fmt, ...) { |
46 | va_list al; |
47 | |
48 | va_start(al,fmt); |
49 | vdebug(ads,fmt,al); |
50 | va_end(al); |
51 | } |
52 | |
53 | static void vdiag(adns_state ads, const char *fmt, va_list al) { |
54 | if (ads->iflags & adns_if_noerrprint) return; |
55 | fputs("adns: ",stderr); |
56 | vfprintf(stderr,fmt,al); |
57 | fputc('\n',stderr); |
58 | } |
59 | |
60 | static void diag(adns_state ads, const char *fmt, ...) { |
61 | va_list al; |
62 | |
63 | va_start(al,fmt); |
64 | vdiag(ads,fmt,al); |
65 | va_end(al); |
66 | } |
67 | |
68 | static void addserver(adns_state ads, struct in_addr addr) { |
5617a135 |
69 | int i; |
59258c2d |
70 | struct server *ss; |
5617a135 |
71 | |
72 | for (i=0; i<ads->nservers; i++) { |
73 | if (ads->servers[i].addr.s_addr == addr.s_addr) { |
74 | debug(ads,"duplicate nameserver %s ignored",inet_ntoa(addr)); |
75 | return; |
76 | } |
77 | } |
78 | |
5db99a2e |
79 | if (ads->nservers>=MAXSERVERS) { |
e7afa27f |
80 | diag(ads,"too many nameservers, ignoring %s",inet_ntoa(addr)); |
5617a135 |
81 | return; |
5db99a2e |
82 | } |
5617a135 |
83 | |
59258c2d |
84 | ss= ads->servers+ads->nservers; |
85 | ss->addr= addr; |
86 | ss->state= server_disc; |
87 | ss->connw.head= ss->connw.tail= 0; |
5617a135 |
88 | ads->nservers++; |
5db99a2e |
89 | } |
90 | |
e7afa27f |
91 | static void configparseerr(adns_state ads, const char *fn, int lno, |
92 | const char *fmt, ...) { |
93 | va_list al; |
94 | |
95 | if (ads->iflags & adns_if_noerrprint) return; |
96 | if (lno==-1) fprintf(stderr,"adns: %s: ",fn); |
97 | else fprintf(stderr,"adns: %s:%d: ",fn,lno); |
98 | va_start(al,fmt); |
99 | vfprintf(stderr,fmt,al); |
100 | va_end(al); |
101 | fputc('\n',stderr); |
102 | } |
103 | |
104 | static void ccf_nameserver(adns_state ads, const char *fn, int lno, const char *buf) { |
105 | struct in_addr ia; |
106 | |
107 | if (!inet_aton(buf,&ia)) { |
108 | configparseerr(ads,fn,lno,"invalid nameserver address `%s'",buf); |
109 | return; |
110 | } |
111 | debug(ads,"using nameserver %s",inet_ntoa(ia)); |
112 | addserver(ads,ia); |
113 | } |
114 | |
115 | static void ccf_search(adns_state ads, const char *fn, int lno, const char *buf) { |
116 | if (!buf) return; |
117 | diag(ads,"warning - `search' ignored FIXME"); |
118 | } |
119 | |
120 | static void ccf_sortlist(adns_state ads, const char *fn, int lno, const char *buf) { |
121 | diag(ads,"warning - `sortlist' ignored FIXME"); |
122 | } |
123 | |
124 | static void ccf_options(adns_state ads, const char *fn, int lno, const char *buf) { |
125 | if (!buf) return; |
126 | diag(ads,"warning - `options' ignored FIXME"); |
127 | } |
128 | |
129 | static void ccf_clearnss(adns_state ads, const char *fn, int lno, const char *buf) { |
130 | ads->nservers= 0; |
131 | } |
132 | |
133 | static const struct configcommandinfo { |
134 | const char *name; |
135 | void (*fn)(adns_state ads, const char *fn, int lno, const char *buf); |
136 | } configcommandinfos[]= { |
137 | { "nameserver", ccf_nameserver }, |
138 | { "domain", ccf_search }, |
139 | { "search", ccf_search }, |
140 | { "sortlist", ccf_sortlist }, |
141 | { "options", ccf_options }, |
142 | { "clearnameservers", ccf_clearnss }, |
143 | { 0 } |
144 | }; |
145 | |
31d3a9d0 |
146 | static int ctype_whitespace(int c) { return c==' ' || c=='\n' || c=='\t'; } |
147 | static int ctype_digit(int c) { return c>='0' && c<='9'; } |
e7afa27f |
148 | |
149 | static void readconfig(adns_state ads, const char *filename) { |
150 | char linebuf[2000], *p, *q; |
151 | FILE *file; |
152 | int lno, l, c; |
153 | const struct configcommandinfo *ccip; |
154 | |
155 | file= fopen(filename,"r"); |
156 | if (!file) { |
157 | if (errno == ENOENT) { |
158 | debug(ads,"configuration file `%s' does not exist",filename); |
159 | return; |
160 | } |
161 | diag(ads,"cannot open configuration file `%s': %s",filename,strerror(errno)); |
162 | return; |
163 | } |
164 | |
165 | for (lno=1; fgets(linebuf,sizeof(linebuf),file); lno++) { |
166 | l= strlen(linebuf); |
167 | if (!l) continue; |
168 | if (linebuf[l-1] != '\n' && !feof(file)) { |
169 | diag(ads,"%s:%d: line too long",filename,lno); |
170 | while ((c= getc(file)) != EOF && c != '\n') { } |
171 | if (c == EOF) break; |
172 | continue; |
173 | } |
31d3a9d0 |
174 | while (l>0 && ctype_whitespace(linebuf[l-1])) l--; |
e7afa27f |
175 | linebuf[l]= 0; |
176 | p= linebuf; |
31d3a9d0 |
177 | while (ctype_whitespace(*p)) p++; |
e7afa27f |
178 | if (*p == '#' || *p == '\n') continue; |
179 | q= p; |
31d3a9d0 |
180 | while (*q && !ctype_whitespace(*q)) q++; |
e7afa27f |
181 | for (ccip=configcommandinfos; |
182 | ccip->name && strncmp(ccip->name,p,q-p); |
183 | ccip++); |
184 | if (!ccip->name) { |
185 | diag(ads,"%s:%d: unknown configuration directive `%.*s'",filename,lno,q-p,p); |
186 | continue; |
187 | } |
31d3a9d0 |
188 | while (ctype_whitespace(*q)) q++; |
e7afa27f |
189 | ccip->fn(ads,filename,lno,q); |
190 | } |
191 | if (ferror(file)) { |
192 | diag(ads,"%s:%d: read error: %s",filename,lno,strerror(errno)); |
193 | } |
194 | fclose(file); |
195 | } |
196 | |
197 | static const char *instrum_getenv(adns_state ads, const char *envvar) { |
198 | const char *value; |
199 | |
200 | value= getenv(envvar); |
5617a135 |
201 | if (!value) debug(ads,"environment variable %s not set",envvar); |
202 | else debug(ads,"environment variable %s set to `%s'",envvar,value); |
e7afa27f |
203 | return value; |
5db99a2e |
204 | } |
205 | |
e7afa27f |
206 | static void readconfigenv(adns_state ads, const char *envvar) { |
5db99a2e |
207 | const char *filename; |
208 | |
e7afa27f |
209 | if (ads->iflags & adns_if_noenv) { |
210 | debug(ads,"not checking environment variable `%s'",envvar); |
211 | return; |
212 | } |
213 | filename= instrum_getenv(ads,envvar); |
214 | if (filename) readconfig(ads,filename); |
5db99a2e |
215 | } |
216 | |
e7afa27f |
217 | int adns_init(adns_state *ads_r, adns_initflags flags) { |
5db99a2e |
218 | adns_state ads; |
e7afa27f |
219 | const char *res_options, *adns_res_options; |
59258c2d |
220 | struct protoent *proto; |
31d3a9d0 |
221 | int r; |
5db99a2e |
222 | |
223 | ads= malloc(sizeof(*ads)); if (!ads) return errno; |
9ab4dfce |
224 | ads->tosend.head= ads->tosend.tail= 0; |
5db99a2e |
225 | ads->timew.head= ads->timew.tail= 0; |
e7afa27f |
226 | ads->childw.head= ads->childw.tail= 0; |
227 | ads->output.head= ads->output.tail= 0; |
59258c2d |
228 | ads->nextid= 0x311f; |
5db99a2e |
229 | ads->udpsocket= -1; |
230 | ads->qbufavail= 0; |
231 | ads->qbuf= 0; |
232 | ads->tcpbufavail= ads->tcpbufused= ads->tcpbufdone= 0; |
233 | ads->tcpbuf= 0; |
e7afa27f |
234 | ads->iflags= flags; |
5db99a2e |
235 | ads->nservers= 0; |
e7afa27f |
236 | ads->iflags= flags; |
237 | |
238 | res_options= instrum_getenv(ads,"RES_OPTIONS"); |
239 | adns_res_options= instrum_getenv(ads,"ADNS_RES_OPTIONS"); |
240 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
241 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
5db99a2e |
242 | |
243 | readconfig(ads,"/etc/resolv.conf"); |
244 | readconfigenv(ads,"RES_CONF"); |
245 | readconfigenv(ads,"ADNS_RES_CONF"); |
e7afa27f |
246 | |
247 | ccf_options(ads,"RES_OPTIONS",-1,res_options); |
248 | ccf_options(ads,"ADNS_RES_OPTIONS",-1,adns_res_options); |
249 | |
250 | ccf_search(ads,"LOCALDOMAIN",-1,instrum_getenv(ads,"LOCALDOMAIN")); |
251 | ccf_search(ads,"ADNS_LOCALDOMAIN",-1,instrum_getenv(ads,"ADNS_LOCALDOMAIN")); |
252 | |
5db99a2e |
253 | if (!ads->nservers) { |
e7afa27f |
254 | struct in_addr ia; |
255 | if (ads->iflags & adns_if_debug) |
5db99a2e |
256 | fprintf(stderr,"adns: no nameservers, using localhost\n"); |
e7afa27f |
257 | ia.s_addr= INADDR_LOOPBACK; |
258 | addserver(ads,ia); |
5db99a2e |
259 | } |
59258c2d |
260 | |
261 | proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; } |
262 | ads->udpsocket= socket(AF_INET,SOCK_DGRAM,proto->p_proto); |
263 | if (!ads->udpsocket) { r= errno; goto x_closeudp; } |
5db99a2e |
264 | |
265 | *ads_r= ads; |
266 | return 0; |
59258c2d |
267 | |
268 | x_closeudp: |
269 | close(ads->udpsocket); |
270 | x_free: |
271 | free(ads); |
272 | return r; |
5db99a2e |
273 | } |
274 | |
e7afa27f |
275 | static void query_fail(adns_state ads, adns_query qu, adns_status stat) { |
5617a135 |
276 | adns_answer *ans; |
5db99a2e |
277 | |
278 | ans= qu->answer; |
279 | if (!ans) ans= malloc(sizeof(*qu->answer)); |
280 | if (ans) { |
281 | ans->status= stat; |
282 | ans->cname= 0; |
283 | ans->type= qu->type; |
284 | ans->nrrs= 0; |
285 | } |
286 | qu->answer= ans; |
9ab4dfce |
287 | qu->id= -1; |
288 | LIST_LINK_TAIL(ads->output,qu); |
5db99a2e |
289 | } |
290 | |
59258c2d |
291 | int adns_finish(adns_state ads) { |
292 | abort(); /* FIXME */ |
293 | } |
294 | |
6927715a |
295 | static void autosys(adns_state ads, struct timeval now) { |
5617a135 |
296 | if (ads->iflags & adns_if_noautosys) return; |
297 | adns_callback(ads,-1,0,0,0); |
298 | } |
299 | |
300 | void adns_cancel(adns_state ads, adns_query query) { |
301 | abort(); /* FIXME */ |
302 | } |
303 | |
88055eee |
304 | static int callb_checkfd(int maxfd, const fd_set *fds, int fd) { |
305 | return maxfd<0 || !fds ? 1 : |
306 | fd<maxfd && FD_ISSET(fd,fds); |
307 | } |
308 | |
5617a135 |
309 | int adns_callback(adns_state ads, int maxfd, |
310 | const fd_set *readfds, const fd_set *writefds, |
311 | const fd_set *exceptfds) { |
88055eee |
312 | int skip, dgramlen, count; |
313 | enum adns__tcpstate oldtcpstate; |
314 | |
315 | count= 0; |
316 | oldtcpstate= ads->tcpstate; |
317 | |
318 | if (ads->tcpstate == server_connecting) { |
319 | if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) { |
320 | count++; |
321 | assert(ads->tcprecv.used==0); |
322 | vbuf_ensure(&ads->tcprecv,1); |
323 | if (ads->tcprecv.buf) { |
324 | r= read(ads->tcpsocket,&ads->tcprecv.buf,1); |
325 | if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) { |
326 | diag("nameserver %s TCP connection made", |
327 | inet_ntoa(ads->servers[ads->tcpserver].addr)); |
328 | ads->tcpstate= server_connected; |
329 | } else if (r>0) { |
330 | tcpserver_broken(ads,"connect/read","sent data before first request"); |
331 | } else if (errno!=EINTR) { |
332 | tcpserver_broken(ads,"connect",strerror(errno)); |
333 | } |
334 | } |
335 | } |
336 | } |
337 | if (ads->tcpstate == server_connected) { |
338 | if (oldtcpstate == server_connected) |
339 | count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) + |
340 | callb_checkfd(maxfd,exceptfds,ads->tcpsocket) + |
341 | (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)); |
342 | if (oldtcpstate != server_connected || callb_checkfd(maxfd,readfds,ads->tcpsocket)) { |
343 | skip= 0; |
344 | for (;;) { |
345 | if (ads->tcprecv.used<skip+2) { |
346 | want= 2; |
347 | } else { |
348 | dgramlen= (ads->tcprecv.buf[skip]<<8) | ads->tcprecv.buf[skip+1]; |
349 | if (ads->tcprecv.used<skip+2+dgramlen) { |
350 | want= 2+dgramlen; |
351 | } else { |
352 | procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,-1); |
353 | skip+= 2+dgramlen; continue; |
354 | } |
355 | } |
356 | Ads->tcprecv.used -= skip; |
357 | memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used); |
358 | vbuf_ensure(&ads->tcprecv,want); |
359 | if (ads->tcprecv.used >= ads->tcprecv.avail) break; |
360 | r= read(ads->tcpsocket, |
361 | ads->tcprecv.buf+ads->tcprecv.used, |
362 | ads->tcprecv.avail-ads->tcprecv.used); |
363 | if (r>0) { |
364 | ads->tcprecv.used+= r; |
365 | } else { |
366 | if (r<0) { |
367 | if (errno==EAGAIN || errno==EWOULDBLOCK || errno==ENOMEM) break; |
368 | if (errno==EINTR) continue; |
369 | } |
370 | tcpserver_broken(ads->tcpserver,"read",r?strerror(errno):"closed"); |
371 | break; |
372 | } |
373 | } |
374 | } else if (callb_checkfd(maxfd,exceptfds,ads->tcpsocket)) { |
375 | tcpserver_broken(ads->tcpserver,"select","exceptional condition detected"); |
376 | } else if (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket)) { |
377 | r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used); |
378 | if (r<0) { |
379 | if (errno!=EAGAIN && errno!=EWOULDBLOCK && errno!=ENOMEM && errno!=EINTR) { |
380 | tcpserver_broken(ads->tcpserver,"write",strerror(errno)); |
381 | } |
382 | } else if (r>0) { |
383 | ads->tcpsend.used -= r; |
384 | memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used); |
385 | } |
386 | } |
387 | } |
388 | |
389 | if ( |
390 | break; |
391 | |
392 | |
393 | } |
394 | |
395 | tcpserver_broken( |
396 | |
397 | if (ads- |
398 | used= 0; |
399 | for (;;) { |
400 | vbuf_ensure(&ads->tcprecv,2); |
401 | vbuf_ensure(&ads->tcprecv, |
402 | if (ads->tcprecv.avail<2) break; |
403 | if (ads->tcprecv.used |
404 | |
405 | if (ads->tcprecv.used<2 && ads->tcprecv.avail |
406 | if (ads->tcprecv.used<2 && ads->tcprecv.avail |
407 | r= read(ads->tcpsocket, |
408 | if (adns->tcprecv.used<2) { |
409 | if ( |
410 | |
411 | if (ads->tcpstate != server_disc) { |
412 | |
413 | |
414 | } |
415 | if (maxfd<0 || !readfds || (FD_ISSET |
416 | ads-> |
417 | |
5617a135 |
418 | abort(); /* FIXME */ |
419 | } |
88055eee |
420 | diag("nameserver #%d (%s) TCP connection died: %s", |
421 | inet_ntoa(ads->servers[tcpserver].addr), |
5617a135 |
422 | |
6927715a |
423 | static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf, |
424 | struct timeval maxto) { |
425 | struct timeval rbuf; |
426 | |
427 | rbuf= *tv_io; |
428 | if (!rbuf) { *tvbuf= maxto; *tv_io= tvbuf; return; } |
429 | if (timercmp(rbuf,&maxto,>)) *rbuf= maxto; |
430 | } |
431 | |
432 | static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf, |
433 | struct timeval now, struct timeval maxtime) { |
434 | ldiv_t dr; |
435 | |
436 | maxtime.tv_sec -= (now.tv_sec-1); |
437 | maxtime.tv_usec += (1000-now.tv_usec); |
438 | dr= ldiv(maxtime.tv_usec,1000); |
439 | maxtime.tv_sec += dr.quot; |
440 | maxtime.tv_usec -= dr.rem; |
441 | inter_maxto(tv_io,tvbuf,maxtime); |
442 | } |
443 | |
444 | static void localresourcerr(struct timeval **tv_io, struct timeval *tvbuf, |
445 | const char *syscall) { |
446 | struct timeval tvto_lr; |
447 | |
448 | diag(ads,"local system resources scarce (during %s): %s",syscall,strerror(errno)); |
449 | timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS); |
450 | inter_maxto(tv_io, tvbuf, tvto_lr); |
451 | return; |
452 | } |
453 | |
454 | static inline void timevaladd(struct timeval *tv_io, long ms) { |
455 | struct timeval tmp; |
456 | assert(ms>=0); |
457 | tmp= *tv_io; |
458 | tmp.tv_usec += (ms%1000)*1000; |
459 | tmp.tv_sec += ms/1000; |
460 | if (tmp.tv_usec >= 1000) { tmp.tv_sec++; tmp.tv_usec -= 1000; } |
461 | *tv_io= tmp; |
462 | } |
463 | |
88055eee |
464 | static void inter_addfd(int *maxfd, fd_set *fds, int fd) { |
465 | if (fd>=*maxfd) *maxfd= fd+1; |
466 | FD_SET(fd,fds); |
467 | } |
468 | |
6927715a |
469 | void adns_interest(adns_state ads, int *maxfd, |
470 | fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
471 | struct timeval **tv_io, struct timeval *tvbuf) { |
472 | struct timeval now; |
473 | adns_query qu; |
474 | int r; |
475 | |
476 | r= gettimeofday(&now,0); |
477 | if (r) { localresourcerr(tv_io,tvbuf,"gettimeofday"); return; } |
478 | |
479 | for (qu= ads->timew; qu; qu= nqu) { |
480 | nqu= qu->next; |
481 | if (timercmp(&now,qu->timeout,>)) { |
482 | DLIST_UNLINK(ads->timew,qu); |
483 | if (qu->nextudpserver == -1) { |
484 | query_fail(ads,qu,adns_s_notresponding); |
485 | } else { |
486 | DLIST_LINKTAIL(ads->tosend,qu); |
487 | } |
488 | } else { |
489 | inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); |
490 | } |
491 | } |
492 | |
493 | for (qu= ads->tosend; qu; qu= nqu) { |
494 | nqu= qu->next; |
495 | quproc_tosend(ads,qu,now); |
496 | } |
497 | |
88055eee |
498 | inter_addfd(maxfd,readfds,ads->udpsocket); |
6927715a |
499 | switch (ads->tcpstate) { |
500 | case server_disc: |
501 | break; |
502 | case server_connecting: |
6927715a |
503 | inter_addfd(maxfd,writefds,ads->tcpsocket); |
6927715a |
504 | break; |
505 | case server_connected: |
506 | inter_addfd(maxfd,readfds,ads->tcpsocket); |
507 | inter_addfd(maxfd,exceptfds,ads->tcpsocket); |
508 | if (ads->opbufused) inter_addfd(maxfd,writefds,ads->tcpsocket); |
509 | default: |
510 | abort(); |
511 | } |
512 | |
513 | } |
514 | |
5617a135 |
515 | static int internal_check(adns_state ads, |
516 | adns_query *query_io, |
9ab4dfce |
517 | adns_answer **answer, |
518 | void **context_r) { |
519 | adns_query qu; |
520 | |
521 | qu= *query_io; |
522 | if (!qu) { |
523 | if (!ads->output.head) return EWOULDBLOCK; |
524 | qu= ads->output.head; |
525 | } else { |
526 | if (qu->id>=0) return EWOULDBLOCK; |
527 | } |
528 | LIST_UNLINK(ads->output,qu); |
529 | *answer= qu->answer; |
530 | if (context_r) *context_r= qu->context; |
531 | free(qu); |
532 | return 0; |
5617a135 |
533 | } |
534 | |
535 | int adns_wait(adns_state ads, |
536 | adns_query *query_io, |
9ab4dfce |
537 | adns_answer **answer_r, |
538 | void **context_r) { |
5617a135 |
539 | int r, maxfd, rsel, rcb; |
540 | fd_set readfds, writefds, exceptfds; |
541 | struct timeval tvbuf, *tvp; |
542 | |
543 | for (;;) { |
9ab4dfce |
544 | r= internal_check(ads,query_io,answer_r,context_r); |
5617a135 |
545 | if (r && r != EWOULDBLOCK) return r; |
546 | FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); |
547 | maxfd= 0; tvp= 0; |
548 | adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf); |
549 | rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp); |
550 | if (rsel==-1) return r; |
551 | rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds); |
552 | assert(rcb==rsel); |
553 | } |
554 | } |
555 | |
556 | int adns_check(adns_state ads, |
557 | adns_query *query_io, |
9ab4dfce |
558 | adns_answer **answer_r, |
559 | void **context_r) { |
5617a135 |
560 | autosys(ads); |
9ab4dfce |
561 | return internal_check(ads,query_io,answer_r,context_r); |
5617a135 |
562 | } |
563 | |
564 | int adns_synchronous(adns_state ads, |
565 | const char *owner, |
566 | adns_rrtype type, |
ebd44fbe |
567 | adns_queryflags flags, |
9ab4dfce |
568 | adns_answer **answer_r) { |
5617a135 |
569 | adns_query qu; |
570 | int r; |
571 | |
572 | r= adns_submit(ads,owner,type,flags,0,&qu); |
573 | if (r) return r; |
574 | |
ebd44fbe |
575 | do { |
9ab4dfce |
576 | r= adns_wait(ads,&qu,answer_r,0); |
ebd44fbe |
577 | } while (r==EINTR); |
5617a135 |
578 | if (r) adns_cancel(ads,qu); |
579 | return r; |
580 | } |
581 | |
31d3a9d0 |
582 | static adns_status mkquery(adns_state ads, const char *owner, int ol, int id, |
583 | adns_rrtype type, adns_queryflags flags, int *qml_r) { |
584 | int ll, c, nlabs, qbufreq; |
585 | unsigned char label[255], *nqbuf; |
586 | const char *p, *pe; |
587 | |
588 | #define MKQUERY_ADDB(b) *nqbuf++= (b) |
589 | #define MKQUERY_ADDW(w) (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff)) |
590 | |
591 | qbufreq= 12+strlen(owner)+3; |
592 | if (ads->qbufavail < qbufreq) { |
593 | nqbuf= realloc(ads->qbuf,qbufreq); |
594 | if (!nqbuf) return adns_s_nolocalmem; |
595 | ads->qbuf= nqbuf; ads->qbufavail= qbufreq; |
596 | } |
597 | nqbuf= ads->qbuf; |
598 | |
599 | MKQUERY_ADDW(id); |
600 | MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */ |
601 | MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */ |
602 | MKQUERY_ADDW(1); /* QDCOUNT=1 */ |
603 | MKQUERY_ADDW(0); /* ANCOUNT=0 */ |
604 | MKQUERY_ADDW(0); /* NSCOUNT=0 */ |
605 | MKQUERY_ADDW(0); /* ARCOUNT=0 */ |
606 | p= owner; pe= owner+ol; |
607 | nlabs= 0; |
608 | if (!*p) return adns_s_invaliddomain; |
609 | do { |
610 | ll= 0; |
611 | while (p!=pe && (c= *p++)!='.') { |
612 | if (c=='\\') { |
613 | if (!(flags & adns_f_anyquote)) return adns_s_invaliddomain; |
614 | if (ctype_digit(p[0])) { |
615 | if (ctype_digit(p[1]) && ctype_digit(p[2])) { |
616 | c= (*p++ - '0')*100 + (*p++ - '0')*10 + (*p++ - '0'); |
617 | if (c >= 256) return adns_s_invaliddomain; |
618 | } else { |
619 | return adns_s_invaliddomain; |
620 | } |
621 | } else if (!(c= *p++)) { |
622 | return adns_s_invaliddomain; |
623 | } |
624 | } |
625 | if (!(flags & adns_f_anyquote)) { |
626 | if ((c >= '0' && c <= '9') || c == '-') { |
627 | if (!ll) return adns_s_invaliddomain; |
628 | } else if ((c < 'a' || c > 'z') && (c < 'A' && c > 'Z')) { |
629 | return adns_s_invaliddomain; |
630 | } |
631 | } |
632 | if (ll == sizeof(label)) return adns_s_invaliddomain; |
633 | label[ll++]= c; |
634 | } |
635 | if (!ll) return adns_s_invaliddomain; |
636 | if (nlabs++ > 63) return adns_s_invaliddomain; |
637 | MKQUERY_ADDB(ll); |
638 | memcpy(nqbuf,label,ll); nqbuf+= ll; |
639 | } while (p!=pe); |
640 | |
641 | MKQUERY_ADDB(0); |
642 | MKQUERY_ADDW(type & adns__rrt_typemask); /* QTYPE */ |
643 | MKQUERY_ADDW(1); /* QCLASS=IN */ |
644 | |
645 | *qml_r= nqbuf - ads->qbuf; |
646 | |
647 | return adns_s_ok; |
59258c2d |
648 | } |
649 | |
31d3a9d0 |
650 | static adns_query allocquery(adns_state ads, const char *owner, int ol, |
651 | int qml, int id, adns_rrtype type, |
652 | adns_queryflags flags, void *context) { |
5db99a2e |
653 | adns_query qu; |
31d3a9d0 |
654 | unsigned char *qm; |
655 | |
656 | qu= malloc(sizeof(*qu)+ol+1+qml); if (!qu) return 0; |
6927715a |
657 | qu->next= qu->back= qu->parent= 0; |
658 | qu->children.head= qu->children.tail= 0; |
659 | qu->siblings.next= qu->siblings.back= 0; |
31d3a9d0 |
660 | qu->id= id; |
5db99a2e |
661 | qu->type= type; |
662 | qu->answer= 0; |
663 | qu->flags= flags; |
664 | qu->context= context; |
e7afa27f |
665 | qu->udpretries= 0; |
59258c2d |
666 | qu->sentudp= qu->senttcp= 0; |
667 | qu->nextserver= 0; |
5db99a2e |
668 | memcpy(qu->owner,owner,ol); qu->owner[ol]= 0; |
31d3a9d0 |
669 | qu->querymsg= qm= qu->owner+ol+1; |
670 | memcpy(qm,ads->qbuf,qml); |
671 | qu->querylen= qml; |
672 | return qu; |
673 | } |
59258c2d |
674 | |
31d3a9d0 |
675 | static int failsubmit(adns_state ads, void *context, adns_query *query_r, |
676 | adns_rrtype type, adns_queryflags flags, |
677 | int id, adns_status stat) { |
678 | adns_query qu; |
59258c2d |
679 | |
31d3a9d0 |
680 | qu= allocquery(ads,0,0,0,id,type,flags,context); if (!qu) return errno; |
681 | query_fail(ads,qu,stat); |
682 | *query_r= qu; |
683 | return 0; |
684 | } |
685 | |
6927715a |
686 | static void quproc_tosend(adns_state ads, adns_query qu, struct timeval now) { |
687 | /* Query must be on the `tosend' queue, and guarantees to remove it. */ |
9ab4dfce |
688 | struct sockaddr_in servaddr; |
6927715a |
689 | int serv; |
690 | |
691 | if (qu->nextudpserver != -1) { |
692 | if (qu->udpretries >= UDPMAXRETRIES) { |
693 | DLIST_UNLINK(ads->tosend,qu); |
694 | query_fail(ads,qu,adns_s_notresponding); |
695 | return; |
696 | } |
697 | serv= qu->nextudpserver; |
698 | memset(&servaddr,0,sizeof(servaddr)); |
699 | servaddr.sin_family= AF_INET; |
700 | servaddr.sin_addr= ads->servers[serv].addr; |
701 | servaddr.sin_port= htons(53); |
702 | r= sendto(ads->udpsocket,qu->querymsg,qu->querylen,0,&servaddr,sizeof(servaddr)); |
703 | if (r<0 && errno == EMSGSIZE) { |
704 | qu->nextudpserver= -1; |
705 | } else { |
706 | if (r<0) { |
707 | diag("sendto %s failed: %s",inet_ntoa(servaddr.sin_addr),strerror(errno)); |
708 | } |
709 | DLIST_UNLINK(ads->tosend,qu); |
710 | timevaladd(&now,UDPRETRYMS); |
711 | qu->timeout= now; |
712 | qu->sentudp |= (1<<serv); |
713 | qu->nextudpserver= (serv+1)%ads->nservers; |
714 | qu->udpretries++; |
715 | DLIST_LINKTAIL(ads->timew,qu); |
716 | return; |
717 | } |
718 | } |
719 | |
720 | for (;;) { |
721 | serv= tcpserver_get(ads); |
722 | if (serv<0) { r=0; break; } |
723 | if (ads->opbufused) { r=0; break; } |
724 | r= write(ads->tcpsocket,qu->querymsg,qu->querylen); |
725 | if (r >= 0) break; |
726 | if (errno == EAGAIN || errno == EINTR || errno == ENOSPC || |
727 | errno == ENOBUFS || errno == ENOMEM) { |
728 | r= 0; break; |
729 | } |
730 | tcpserver_broken(serv); |
731 | } |
732 | if (r < qu->querylen) { |
733 | newopbufused= qu->opbufused + (qu->querylen-r); |
734 | if (newopbufused > ads->opbufavail) { |
735 | newopbufavail= ads->newopbufused<<1; |
736 | newopbuf= realloc(newopbufavail); |
737 | if (!newopbuf) { |
738 | DLIST_UNLINK(ads->tosend,qu); |
739 | query_fail(ads,qu,adns_s_nolocalmem); |
740 | return; |
741 | } |
742 | ads->opbuf= newopbuf; |
743 | ads->opbufavail= newopbufavail; |
744 | } |
745 | memcpy(ads->opbuf+ads->opbufused,qu->querymsg+r,qu->querylen-r); |
746 | ads->opbufused= newopbufused; |
747 | } |
748 | DLIST_UNLINK(ads->tosend,qu); |
749 | timevaladd(&now,TCPMS); |
750 | qu->timeout= now; |
751 | qu->senttcp |= (1<<qu->nextserver); |
752 | DLIST_LINKTAIL(ads->timew,qu); |
9ab4dfce |
753 | } |
754 | |
31d3a9d0 |
755 | int adns_submit(adns_state ads, |
756 | const char *owner, |
757 | adns_rrtype type, |
758 | adns_queryflags flags, |
759 | void *context, |
760 | adns_query *query_r) { |
761 | adns_query qu; |
762 | adns_status stat; |
763 | int ol, id, qml; |
764 | |
765 | id= ads->nextid++; |
766 | |
767 | ol= strlen(owner); |
768 | if (ol<=1 || ol>MAXDNAME+1) |
769 | return failsubmit(ads,context,query_r,type,flags,id,adns_s_invaliddomain); |
770 | if (owner[ol-1]=='.' && owner[ol-2]!='\\') { flags &= ~adns_f_search; ol--; } |
771 | |
772 | stat= mkquery(ads,owner,ol,id,type,flags,&qml); |
773 | if (stat) return failsubmit(ads,context,query_r,type,flags,id,stat); |
774 | |
775 | qu= allocquery(ads,owner,ol,qml,id,type,flags,context); if (!qu) return errno; |
6927715a |
776 | if (qu->flags & adns_f_usevc) qu->udpretries= -1; |
9ab4dfce |
777 | LIST_LINK_TAIL(ads->tosend,qu); |
6927715a |
778 | |
779 | r= gettimeofday(&now,0); if (r) return; |
780 | quproc_tosend(ads,qu,now); |
781 | autosys(ads,now); |
59258c2d |
782 | |
5db99a2e |
783 | *query_r= qu; |
5617a135 |
784 | return 0; |
5db99a2e |
785 | } |