+void adns__fdevents(adns_state ads,
+ const struct pollfd *pollfds, int npollfds,
+ int maxfd, const fd_set *readfds,
+ const fd_set *writefds, const fd_set *exceptfds,
+ struct timeval now, int *r_r) {
+ int i, fd, revents;
+
+ for (i=0; i<npollfds; i++) {
+ fd= pollfds[i].fd;
+ if (fd >= maxfd) maxfd= fd+1;
+ revents= pollfds[i].revents;
+#define EV(pollfl,fds,how) \
+ fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
+ EV( POLLIN, readfds, readable );
+ EV( POLLOUT, writefds, writeable );
+ EV( POLLPRI, exceptfds, exceptional );
+#undef EV
+ }
+}
+
+/* Wrappers for select(2). */
+
+void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
+ fd_set *writefds_io, fd_set *exceptfds_io,
+ struct timeval **tv_mod, struct timeval *tv_tobuf,
+ const struct timeval *now) {
+ struct timeval tv_nowbuf;
+ struct pollfd pollfds[MAX_POLLFDS];
+ int i, fd, maxfd, npollfds;
+
+ adns__consistency(ads,0,cc_enter);
+
+ if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
+ /* The caller is planning to sleep. */
+ adns__must_gettimeofday(ads,&now,&tv_nowbuf);
+ if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
+ adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
+ }
+
+ npollfds= adns__pollfds(ads,pollfds);
+ maxfd= *maxfd_io;
+ for (i=0; i<npollfds; i++) {
+ fd= pollfds[i].fd;
+ if (fd >= maxfd) maxfd= fd+1;
+ if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
+ if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
+ if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
+ }
+ *maxfd_io= maxfd;
+
+xit:
+ adns__returning(ads,0);
+}
+
+void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
+ const fd_set *writefds, const fd_set *exceptfds,
+ const struct timeval *now) {
+ struct timeval tv_buf;
+ struct pollfd pollfds[MAX_POLLFDS];
+ int npollfds, i;
+
+ adns__consistency(ads,0,cc_enter);
+ adns__must_gettimeofday(ads,&now,&tv_buf);
+ if (!now) goto xit;
+ adns_processtimeouts(ads,now);
+
+ npollfds= adns__pollfds(ads,pollfds);
+ for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
+ adns__fdevents(ads,
+ pollfds,npollfds,
+ maxfd,readfds,writefds,exceptfds,
+ *now, 0);
+xit:
+ adns__returning(ads,0);
+}
+
+/* General helpful functions. */
+
+void adns_globalsystemfailure(adns_state ads) {
+ /* Must not be called by adns during actual processing of a
+ * particular query, since it reenters adns. Only safe to call in
+ * situations where it would be safe to call adns_returning. */
+ adns__consistency(ads,0,cc_enter);
+
+ for (;;) {
+ adns_query qu;
+#define GSF_QQ(QQ) \
+ if ((qu= ads->QQ.head)) { \
+ LIST_UNLINK(ads->QQ,qu); \
+ adns__query_fail(qu, adns_s_systemfail); \
+ continue; \
+ }
+ GSF_QQ(udpw);
+ GSF_QQ(tcpw);
+#undef GSF_QQ
+ break;
+ }
+
+ switch (ads->tcpstate) {
+ case server_connecting:
+ case server_ok:
+ adns__tcp_broken(ads,0,0);
+ break;
+ case server_disconnected:
+ case server_broken:
+ break;
+ default:
+ abort();
+ }
+ adns__returning(ads,0);
+}
+
+int adns_processany(adns_state ads) {
+ int r, i;
+ struct timeval now;
+ struct pollfd pollfds[MAX_POLLFDS];
+ int npollfds;
+
+ adns__consistency(ads,0,cc_enter);
+
+ r= gettimeofday(&now,0);
+ if (!r) adns_processtimeouts(ads,&now);
+
+ /* We just use adns__fdevents to loop over the fd's trying them.
+ * This seems more sensible than calling select, since we're most
+ * likely just to want to do a read on one or two fds anyway.
+ */
+ npollfds= adns__pollfds(ads,pollfds);
+ for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
+ adns__fdevents(ads,
+ pollfds,npollfds,
+ 0,0,0,0,
+ now,&r);
+
+ adns__returning(ads,0);
+ return 0;
+}
+
+void adns__autosys(adns_state ads, struct timeval now) {
+ if (ads->iflags & adns_if_noautosys) return;
+ adns_processany(ads);
+}
+
+int adns__internal_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer,
+ void **context_r) {