+ } /* never reached */
+ default:
+ abort();
+ }
+ if (fd == ads->udpsocket) {
+ for (;;) {
+ udpaddrlen= sizeof(udpaddr);
+ r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
+ (struct sockaddr*)&udpaddr,&udpaddrlen);
+ if (r<0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
+ if (errno == EINTR) continue;
+ if (errno_resources(errno)) return errno;
+ adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
+ return 0;
+ }
+ if (udpaddrlen != sizeof(udpaddr)) {
+ adns__diag(ads,-1,0,"datagram received with wrong address length %d"
+ " (expected %d)", udpaddrlen,sizeof(udpaddr));
+ continue;
+ }
+ if (udpaddr.sin_family != AF_INET) {
+ adns__diag(ads,-1,0,"datagram received with wrong protocol family"
+ " %u (expected %u)",udpaddr.sin_family,AF_INET);
+ continue;
+ }
+ if (ntohs(udpaddr.sin_port) != DNS_PORT) {
+ adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
+ ntohs(udpaddr.sin_port),DNS_PORT);
+ continue;
+ }
+ for (serv= 0;
+ serv < ads->nservers &&
+ ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
+ serv++);
+ if (serv >= ads->nservers) {
+ adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
+ inet_ntoa(udpaddr.sin_addr));
+ continue;
+ }
+ adns__procdgram(ads,udpbuf,r,serv,0,*now);
+ }
+ }
+ return 0;
+}
+
+int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
+ int r;
+
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ break;
+ case server_connecting:
+ if (fd != ads->tcpsocket) break;
+ assert(ads->tcprecv.used==0);
+ for (;;) {
+ if (!adns__vbuf_ensure(&ads->tcprecv,1)) return ENOMEM;
+ r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
+ if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
+ tcp_connected(ads,*now);
+ return 0;
+ }
+ if (r>0) {
+ adns__tcp_broken(ads,"connect/read","sent data before first request");
+ return 0;
+ }
+ 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;