+static void tcp_events(adns_state ads, int act,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now) {
+ for (;;) {
+ switch (ads->tcpstate) {
+ case server_broken:
+ if (!act) { inter_immed(tv_io,tvbuf); return; }
+ tcp_broken_events(ads);
+ case server_disconnected: /* fall through */
+ if (!ads->tcpw.head) return;
+ if (!act) { inter_immed(tv_io,tvbuf); return; }
+ adns__tcp_tryconnect(ads,now);
+ break;
+ case server_ok:
+ if (ads->tcpw.head) return;
+ if (!ads->tcptimeout.tv_sec) {
+ assert(!ads->tcptimeout.tv_usec);
+ ads->tcptimeout= now;
+ timevaladd(&ads->tcptimeout,TCPIDLEMS);
+ }
+ case server_connecting: /* fall through */
+ if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
+ inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
+ return;
+ } {
+ /* TCP timeout has happened */
+ switch (ads->tcpstate) {
+ case server_connecting: /* failed to connect */
+ adns__tcp_broken(ads,"unable to make connection","timed out");
+ break;
+ case server_ok: /* idle timeout */
+ tcp_close(ads);
+ ads->tcpstate= server_disconnected;
+ return;
+ default:
+ abort();
+ }
+ }
+ break;
+ default:
+ abort();
+ }
+ }
+ return;
+}
+
+void adns__timeouts(adns_state ads, int act,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now) {
+ timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
+ timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
+ tcp_events(ads,act,tv_io,tvbuf,now);
+}
+
+void adns_firsttimeout(adns_state ads,
+ struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now) {
+ adns__consistency(ads,0,cc_entex);
+ adns__timeouts(ads, 0, tv_io,tvbuf, now);
+ adns__consistency(ads,0,cc_entex);
+}
+
+void adns_processtimeouts(adns_state ads, const struct timeval *now) {
+ struct timeval tv_buf;
+
+ adns__consistency(ads,0,cc_entex);
+ adns__must_gettimeofday(ads,&now,&tv_buf);
+ if (now) adns__timeouts(ads, 1, 0,0, *now);
+ adns__consistency(ads,0,cc_entex);
+}
+
+/* fd handling functions. These are the top-level of the real work of
+ * reception and often transmission.
+ */
+
+int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
+ /* Returns the number of entries filled in. Always zeroes revents. */
+
+ int i;
+
+ assert(MAX_POLLFDS == MAXUDP + 1);
+
+ for (i=0; i<ads->nudp; i++) {
+ pollfds_buf[i].fd= ads->udpsocket[i].fd;
+ pollfds_buf[i].events= POLLIN;
+ pollfds_buf[i].revents= 0;