+ if (errno==EINTR) continue;
+ if (errno_resources(errno)) return errno;
+ adns__tcp_broken(ads,"connect/read",strerror(errno));
+ return 0;
+ } /* not reached */
+ case server_ok:
+ if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break;
+ for (;;) {
+ adns__sigpipe_protect(ads);
+ r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
+ adns__sigpipe_unprotect(ads);
+ if (r<0) {
+ if (errno==EINTR) continue;
+ if (errno==EAGAIN || errno==EWOULDBLOCK) return 0;
+ if (errno_resources(errno)) return errno;
+ adns__tcp_broken(ads,"write",strerror(errno));
+ return 0;
+ } else if (r>0) {
+ ads->tcpsend.used -= r;
+ memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
+ }
+ } /* not reached */
+ default:
+ abort();
+ }
+ return 0;
+}
+
+int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ break;
+ case server_connecting:
+ case server_ok:
+ if (fd != ads->tcpsocket) break;
+ adns__tcp_broken(ads,"poll/select","exceptional condition detected");
+ return 0;
+ default:
+ abort();
+ }
+ return 0;
+}
+
+static void fd_event(adns_state ads, int fd,
+ int revent, int pollflag,
+ int maxfd, const fd_set *fds,
+ int (*func)(adns_state, int fd, const struct timeval *now),
+ struct timeval now, int *r_r) {
+ int r;
+
+ if (!(revent & pollflag)) return;
+ if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
+ r= func(ads,fd,&now);
+ if (r) {
+ if (r_r) {
+ *r_r= r;