chiark / gitweb /
.gitignore: Add *.o *.so *.a and *~
[adns.git] / src / event.c
1 /*
2  * event.c
3  * - event loop core
4  * - TCP connection management
5  * - user-visible check/wait and event-loop-related functions
6  */
7 /*
8  *  This file is part of adns, which is
9  *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
10  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
11  *    Copyright (C) 1991 Massachusetts Institute of Technology
12  *  (See the file INSTALL for full details.)
13  *  
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License as published by
16  *  the Free Software Foundation; either version 2, or (at your option)
17  *  any later version.
18  *  
19  *  This program is distributed in the hope that it will be useful,
20  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *  GNU General Public License for more details.
23  *  
24  *  You should have received a copy of the GNU General Public License
25  *  along with this program; if not, write to the Free Software Foundation,
26  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
27  */
28
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <netdb.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39
40 #include "internal.h"
41 #include "tvarith.h"
42
43 /* TCP connection management. */
44
45 static void tcp_close(adns_state ads) {
46   int serv;
47   
48   serv= ads->tcpserver;
49   close(ads->tcpsocket);
50   ads->tcpsocket= -1;
51   ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
52 }
53
54 void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
55   int serv;
56   adns_query qu;
57   
58   assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
59   serv= ads->tcpserver;
60   if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
61
62   if (ads->tcpstate == server_connecting) {
63     /* Counts as a retry for all the queries waiting for TCP. */
64     for (qu= ads->tcpw.head; qu; qu= qu->next)
65       qu->retries++;
66   }
67
68   tcp_close(ads);
69   ads->tcpstate= server_broken;
70   ads->tcpserver= (serv+1)%ads->nservers;
71 }
72
73 static void tcp_connected(adns_state ads, struct timeval now) {
74   adns_query qu, nqu;
75   
76   adns__debug(ads,ads->tcpserver,0,"TCP connected");
77   ads->tcpstate= server_ok;
78   for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
79     nqu= qu->next;
80     assert(qu->state == query_tcpw);
81     adns__querysend_tcp(qu,now);
82   }
83 }
84
85 static void tcp_broken_events(adns_state ads) {
86   adns_query qu, nqu;
87   
88   assert(ads->tcpstate == server_broken);
89   for (qu= ads->tcpw.head; qu; qu= nqu) {
90     nqu= qu->next;
91     assert(qu->state == query_tcpw);
92     if (qu->retries > ads->nservers) {
93       LIST_UNLINK(ads->tcpw,qu);
94       adns__query_fail(qu,adns_s_allservfail);
95     }
96   }
97   ads->tcpstate= server_disconnected;
98 }
99
100 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
101   int r, fd, tries;
102   struct sockaddr_in addr;
103   struct protoent *proto;
104
105   for (tries=0; tries<ads->nservers; tries++) {
106     switch (ads->tcpstate) {
107     case server_connecting:
108     case server_ok:
109     case server_broken:
110       return;
111     case server_disconnected:
112       break;
113     default:
114       abort();
115     }
116     
117     assert(!ads->tcpsend.used);
118     assert(!ads->tcprecv.used);
119     assert(!ads->tcprecv_skip);
120
121     proto= getprotobyname("tcp");
122     if (!proto) {
123       adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
124       return;
125     }
126     fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
127     if (fd<0) {
128       adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
129       return;
130     }
131     r= adns__setnonblock(ads,fd);
132     if (r) {
133       adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
134                  " %s",strerror(r));
135       close(fd);
136       return;
137     }
138     memset(&addr,0,sizeof(addr));
139     addr.sin_family= AF_INET;
140     addr.sin_port= htons(DNS_PORT);
141     addr.sin_addr= ads->servers[ads->tcpserver].addr;
142     r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
143     ads->tcpsocket= fd;
144     ads->tcpstate= server_connecting;
145     if (r==0) { tcp_connected(ads,now); return; }
146     if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
147       ads->tcptimeout= now;
148       timevaladd(&ads->tcptimeout,TCPCONNMS);
149       return;
150     }
151     adns__tcp_broken(ads,"connect",strerror(errno));
152     tcp_broken_events(ads);
153   }
154 }
155
156 /* Timeout handling functions. */
157
158 void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
159                              struct timeval *tv_buf) {
160   const struct timeval *now;
161   int r;
162
163   now= *now_io;
164   if (now) return;
165   r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
166   adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
167   adns_globalsystemfailure(ads);
168   return;
169 }
170
171 static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
172   struct timeval *rbuf;
173
174   if (!tv_io) return;
175
176   rbuf= *tv_io;
177   if (!rbuf) { *tv_io= rbuf= tvbuf; }
178
179   timerclear(rbuf);
180 }
181     
182 static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
183                         struct timeval maxto) {
184   struct timeval *rbuf;
185
186   if (!tv_io) return;
187   rbuf= *tv_io;
188   if (!rbuf) {
189     *tvbuf= maxto; *tv_io= tvbuf;
190   } else {
191     if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
192   }
193 /*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
194         maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
195 }
196
197 static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
198                            struct timeval now, struct timeval maxtime) {
199   /* tv_io may be 0 */
200   ldiv_t dr;
201
202 /*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
203         now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
204   if (!tv_io) return;
205   maxtime.tv_sec -= (now.tv_sec+2);
206   maxtime.tv_usec -= (now.tv_usec-2000000);
207   dr= ldiv(maxtime.tv_usec,1000000);
208   maxtime.tv_sec += dr.quot;
209   maxtime.tv_usec -= dr.quot*1000000;
210   if (maxtime.tv_sec<0) timerclear(&maxtime);
211   inter_maxto(tv_io,tvbuf,maxtime);
212 }
213
214 static void timeouts_queue(adns_state ads, int act,
215                            struct timeval **tv_io, struct timeval *tvbuf,
216                            struct timeval now, struct query_queue *queue) {
217   adns_query qu, nqu;
218   
219   for (qu= queue->head; qu; qu= nqu) {
220     nqu= qu->next;
221     if (!timercmp(&now,&qu->timeout,>)) {
222       inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
223     } else {
224       if (!act) { inter_immed(tv_io,tvbuf); return; }
225       LIST_UNLINK(*queue,qu);
226       if (qu->state != query_tosend) {
227         adns__query_fail(qu,adns_s_timeout);
228       } else {
229         adns__query_send(qu,now);
230       }
231       nqu= queue->head;
232     }
233   }
234 }
235
236 static void tcp_events(adns_state ads, int act,
237                        struct timeval **tv_io, struct timeval *tvbuf,
238                        struct timeval now) {
239   for (;;) {
240     switch (ads->tcpstate) {
241     case server_broken:
242       if (!act) { inter_immed(tv_io,tvbuf); return; }
243       tcp_broken_events(ads);
244     case server_disconnected: /* fall through */
245       if (!ads->tcpw.head) return;
246       if (!act) { inter_immed(tv_io,tvbuf); return; }
247       adns__tcp_tryconnect(ads,now);
248       break;
249     case server_ok:
250       if (ads->tcpw.head) return;
251       if (!ads->tcptimeout.tv_sec) {
252         assert(!ads->tcptimeout.tv_usec);
253         ads->tcptimeout= now;
254         timevaladd(&ads->tcptimeout,TCPIDLEMS);
255       }
256     case server_connecting: /* fall through */
257       if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
258         inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
259         return;
260       } {
261         /* TCP timeout has happened */
262         switch (ads->tcpstate) {
263         case server_connecting: /* failed to connect */
264           adns__tcp_broken(ads,"unable to make connection","timed out");
265           break;
266         case server_ok: /* idle timeout */
267           tcp_close(ads);
268           ads->tcpstate= server_disconnected;
269           return;
270         default:
271           abort();
272         }
273       }
274       break;
275     default:
276       abort();
277     }
278   }
279   return;
280 }
281
282 void adns__timeouts(adns_state ads, int act,
283                     struct timeval **tv_io, struct timeval *tvbuf,
284                     struct timeval now) {
285   timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
286   timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
287   tcp_events(ads,act,tv_io,tvbuf,now);
288 }
289
290 void adns_firsttimeout(adns_state ads,
291                        struct timeval **tv_io, struct timeval *tvbuf,
292                        struct timeval now) {
293   adns__consistency(ads,0,cc_entex);
294   adns__timeouts(ads, 0, tv_io,tvbuf, now);
295   adns__consistency(ads,0,cc_entex);
296 }
297
298 void adns_processtimeouts(adns_state ads, const struct timeval *now) {
299   struct timeval tv_buf;
300
301   adns__consistency(ads,0,cc_entex);
302   adns__must_gettimeofday(ads,&now,&tv_buf);
303   if (now) adns__timeouts(ads, 1, 0,0, *now);
304   adns__consistency(ads,0,cc_entex);
305 }
306
307 /* fd handling functions.  These are the top-level of the real work of
308  * reception and often transmission.
309  */
310
311 int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
312   /* Returns the number of entries filled in.  Always zeroes revents. */
313
314   assert(MAX_POLLFDS==2);
315
316   pollfds_buf[0].fd= ads->udpsocket;
317   pollfds_buf[0].events= POLLIN;
318   pollfds_buf[0].revents= 0;
319
320   switch (ads->tcpstate) {
321   case server_disconnected:
322   case server_broken:
323     return 1;
324   case server_connecting:
325     pollfds_buf[1].events= POLLOUT;
326     break;
327   case server_ok:
328     pollfds_buf[1].events=
329       ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
330     break;
331   default:
332     abort();
333   }
334   pollfds_buf[1].fd= ads->tcpsocket;
335   return 2;
336 }
337
338 int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
339   int want, dgramlen, r, udpaddrlen, serv, old_skip;
340   byte udpbuf[DNS_MAXUDP];
341   struct sockaddr_in udpaddr;
342   
343   adns__consistency(ads,0,cc_entex);
344
345   switch (ads->tcpstate) {
346   case server_disconnected:
347   case server_broken:
348   case server_connecting:
349     break;
350   case server_ok:
351     if (fd != ads->tcpsocket) break;
352     assert(!ads->tcprecv_skip);
353     do {
354       if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
355         dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
356                    ads->tcprecv.buf[ads->tcprecv_skip+1]);
357         if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
358           old_skip= ads->tcprecv_skip;
359           ads->tcprecv_skip += 2+dgramlen;
360           adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
361                           dgramlen, ads->tcpserver, 1,*now);
362           continue;
363         } else {
364           want= 2+dgramlen;
365         }
366       } else {
367         want= 2;
368       }
369       ads->tcprecv.used -= ads->tcprecv_skip;
370       memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
371               ads->tcprecv.used);
372       ads->tcprecv_skip= 0;
373       if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
374       assert(ads->tcprecv.used <= ads->tcprecv.avail);
375       if (ads->tcprecv.used == ads->tcprecv.avail) continue;
376       r= read(ads->tcpsocket,
377               ads->tcprecv.buf+ads->tcprecv.used,
378               ads->tcprecv.avail-ads->tcprecv.used);
379       if (r>0) {
380         ads->tcprecv.used+= r;
381       } else {
382         if (r) {
383           if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
384           if (errno==EINTR) continue;
385           if (errno_resources(errno)) { r= errno; goto xit; }
386         }
387         adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
388       }
389     } while (ads->tcpstate == server_ok);
390     r= 0; goto xit;
391   default:
392     abort();
393   }
394   if (fd == ads->udpsocket) {
395     for (;;) {
396       udpaddrlen= sizeof(udpaddr);
397       r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
398                   (struct sockaddr*)&udpaddr,&udpaddrlen);
399       if (r<0) {
400         if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
401         if (errno == EINTR) continue;
402         if (errno_resources(errno)) { r= errno; goto xit; }
403         adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
404         r= 0; goto xit;
405       }
406       if (udpaddrlen != sizeof(udpaddr)) {
407         adns__diag(ads,-1,0,"datagram received with wrong address length %d"
408                    " (expected %lu)", udpaddrlen,
409                    (unsigned long)sizeof(udpaddr));
410         continue;
411       }
412       if (udpaddr.sin_family != AF_INET) {
413         adns__diag(ads,-1,0,"datagram received with wrong protocol family"
414                    " %u (expected %u)",udpaddr.sin_family,AF_INET);
415         continue;
416       }
417       if (ntohs(udpaddr.sin_port) != DNS_PORT) {
418         adns__diag(ads,-1,0,"datagram received from wrong port"
419                    " %u (expected %u)", ntohs(udpaddr.sin_port),DNS_PORT);
420         continue;
421       }
422       for (serv= 0;
423            serv < ads->nservers &&
424              ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
425            serv++);
426       if (serv >= ads->nservers) {
427         adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
428                    inet_ntoa(udpaddr.sin_addr));
429         continue;
430       }
431       adns__procdgram(ads,udpbuf,r,serv,0,*now);
432     }
433   }
434   r= 0;
435 xit:
436   adns__consistency(ads,0,cc_entex);
437   return r;
438 }
439
440 int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
441   int r;
442   
443   adns__consistency(ads,0,cc_entex);
444
445   switch (ads->tcpstate) {
446   case server_disconnected:
447   case server_broken:
448     break;
449   case server_connecting:
450     if (fd != ads->tcpsocket) break;
451     assert(ads->tcprecv.used==0);
452     assert(ads->tcprecv_skip==0);
453     for (;;) {
454       if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
455       r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
456       if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
457         tcp_connected(ads,*now);
458         r= 0; goto xit;
459       }
460       if (r>0) {
461         adns__tcp_broken(ads,"connect/read","sent data before first request");
462         r= 0; goto xit;
463       }
464       if (errno==EINTR) continue;
465       if (errno_resources(errno)) { r= errno; goto xit; }
466       adns__tcp_broken(ads,"connect/read",strerror(errno));
467       r= 0; goto xit;
468     } /* not reached */
469   case server_ok:
470     if (fd != ads->tcpsocket) break;
471     while (ads->tcpsend.used) {
472       adns__sigpipe_protect(ads);
473       r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
474       adns__sigpipe_unprotect(ads);
475       if (r<0) {
476         if (errno==EINTR) continue;
477         if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
478         if (errno_resources(errno)) { r= errno; goto xit; }
479         adns__tcp_broken(ads,"write",strerror(errno));
480         r= 0; goto xit;
481       } else if (r>0) {
482         ads->tcpsend.used -= r;
483         memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
484       }
485     }
486     r= 0;
487     goto xit;
488   default:
489     abort();
490   }
491   r= 0;
492 xit:
493   adns__consistency(ads,0,cc_entex);
494   return r;
495 }
496   
497 int adns_processexceptional(adns_state ads, int fd,
498                             const struct timeval *now) {
499   adns__consistency(ads,0,cc_entex);
500   switch (ads->tcpstate) {
501   case server_disconnected:
502   case server_broken:
503     break;
504   case server_connecting:
505   case server_ok:
506     if (fd != ads->tcpsocket) break;
507     adns__tcp_broken(ads,"poll/select","exceptional condition detected");
508     break;
509   default:
510     abort();
511   }
512   adns__consistency(ads,0,cc_entex);
513   return 0;
514 }
515
516 static void fd_event(adns_state ads, int fd,
517                      int revent, int pollflag,
518                      int maxfd, const fd_set *fds,
519                      int (*func)(adns_state, int fd,
520                                  const struct timeval *now),
521                      struct timeval now, int *r_r) {
522   int r;
523   
524   if (!(revent & pollflag)) return;
525   if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
526   r= func(ads,fd,&now);
527   if (r) {
528     if (r_r) {
529       *r_r= r;
530     } else {
531       adns__diag(ads,-1,0,"process fd failed after select:"
532                  " %s",strerror(errno));
533       adns_globalsystemfailure(ads);
534     }
535   }
536 }
537
538 void adns__fdevents(adns_state ads,
539                     const struct pollfd *pollfds, int npollfds,
540                     int maxfd, const fd_set *readfds,
541                     const fd_set *writefds, const fd_set *exceptfds,
542                     struct timeval now, int *r_r) {
543   int i, fd, revents;
544
545   for (i=0; i<npollfds; i++) {
546     fd= pollfds[i].fd;
547     if (fd >= maxfd) maxfd= fd+1;
548     revents= pollfds[i].revents;
549 #define EV(pollfl,fds,how)  \
550     fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
551     EV( POLLIN,  readfds,   readable    );
552     EV( POLLOUT, writefds,  writeable   );
553     EV( POLLPRI, exceptfds, exceptional );
554 #undef EV
555   }
556 }
557
558 /* Wrappers for select(2). */
559
560 void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
561                        fd_set *writefds_io, fd_set *exceptfds_io,
562                        struct timeval **tv_mod, struct timeval *tv_tobuf,
563                        const struct timeval *now) {
564   struct timeval tv_nowbuf;
565   struct pollfd pollfds[MAX_POLLFDS];
566   int i, fd, maxfd, npollfds;
567   
568   adns__consistency(ads,0,cc_entex);
569
570   if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
571     /* The caller is planning to sleep. */
572     adns__must_gettimeofday(ads,&now,&tv_nowbuf);
573     if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
574     adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
575   }
576
577   npollfds= adns__pollfds(ads,pollfds);
578   maxfd= *maxfd_io;
579   for (i=0; i<npollfds; i++) {
580     fd= pollfds[i].fd;
581     if (fd >= maxfd) maxfd= fd+1;
582     if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
583     if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
584     if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
585   }
586   *maxfd_io= maxfd;
587
588 xit:
589   adns__consistency(ads,0,cc_entex);
590 }
591
592 void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
593                       const fd_set *writefds, const fd_set *exceptfds,
594                       const struct timeval *now) {
595   struct timeval tv_buf;
596   struct pollfd pollfds[MAX_POLLFDS];
597   int npollfds, i;
598
599   adns__consistency(ads,0,cc_entex);
600   adns__must_gettimeofday(ads,&now,&tv_buf);
601   if (!now) goto xit;
602   adns_processtimeouts(ads,now);
603
604   npollfds= adns__pollfds(ads,pollfds);
605   for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
606   adns__fdevents(ads,
607                  pollfds,npollfds,
608                  maxfd,readfds,writefds,exceptfds,
609                  *now, 0);
610 xit:
611   adns__consistency(ads,0,cc_entex);
612 }
613
614 /* General helpful functions. */
615
616 void adns_globalsystemfailure(adns_state ads) {
617   adns__consistency(ads,0,cc_entex);
618
619   while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
620   while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
621   
622   switch (ads->tcpstate) {
623   case server_connecting:
624   case server_ok:
625     adns__tcp_broken(ads,0,0);
626     break;
627   case server_disconnected:
628   case server_broken:
629     break;
630   default:
631     abort();
632   }
633   adns__consistency(ads,0,cc_entex);
634 }
635
636 int adns_processany(adns_state ads) {
637   int r, i;
638   struct timeval now;
639   struct pollfd pollfds[MAX_POLLFDS];
640   int npollfds;
641
642   adns__consistency(ads,0,cc_entex);
643
644   r= gettimeofday(&now,0);
645   if (!r) adns_processtimeouts(ads,&now);
646
647   /* We just use adns__fdevents to loop over the fd's trying them.
648    * This seems more sensible than calling select, since we're most
649    * likely just to want to do a read on one or two fds anyway.
650    */
651   npollfds= adns__pollfds(ads,pollfds);
652   for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
653   adns__fdevents(ads,
654                  pollfds,npollfds,
655                  0,0,0,0,
656                  now,&r);
657
658   adns__consistency(ads,0,cc_entex);
659   return 0;
660 }
661
662 void adns__autosys(adns_state ads, struct timeval now) {
663   if (ads->iflags & adns_if_noautosys) return;
664   adns_processany(ads);
665 }
666
667 int adns__internal_check(adns_state ads,
668                          adns_query *query_io,
669                          adns_answer **answer,
670                          void **context_r) {
671   adns_query qu;
672
673   qu= *query_io;
674   if (!qu) {
675     if (ads->output.head) {
676       qu= ads->output.head;
677     } else if (ads->udpw.head || ads->tcpw.head) {
678       return EAGAIN;
679     } else {
680       return ESRCH;
681     }
682   } else {
683     if (qu->id>=0) return EAGAIN;
684   }
685   LIST_UNLINK(ads->output,qu);
686   *answer= qu->answer;
687   if (context_r) *context_r= qu->ctx.ext;
688   *query_io= qu;
689   free(qu);
690   return 0;
691 }
692
693 int adns_wait(adns_state ads,
694               adns_query *query_io,
695               adns_answer **answer_r,
696               void **context_r) {
697   int r, maxfd, rsel;
698   fd_set readfds, writefds, exceptfds;
699   struct timeval tvbuf, *tvp;
700   
701   adns__consistency(ads,*query_io,cc_entex);
702   for (;;) {
703     r= adns__internal_check(ads,query_io,answer_r,context_r);
704     if (r != EAGAIN) break;
705     maxfd= 0; tvp= 0;
706     FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
707     adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
708     assert(tvp);
709     rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
710     if (rsel==-1) {
711       if (errno == EINTR) {
712         if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
713       } else {
714         adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
715         adns_globalsystemfailure(ads);
716       }
717     } else {
718       assert(rsel >= 0);
719       adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
720     }
721   }
722   adns__consistency(ads,0,cc_entex);
723   return r;
724 }
725
726 int adns_check(adns_state ads,
727                adns_query *query_io,
728                adns_answer **answer_r,
729                void **context_r) {
730   struct timeval now;
731   int r;
732   
733   adns__consistency(ads,*query_io,cc_entex);
734   r= gettimeofday(&now,0);
735   if (!r) adns__autosys(ads,now);
736
737   r= adns__internal_check(ads,query_io,answer_r,context_r);
738   adns__consistency(ads,0,cc_entex);
739   return r;
740 }