+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#include "internal.h"
+
+/* TCP connection management */
+
+void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
+ int serv;
+ adns_query qu, nqu;
+
+ assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
+ serv= ads->tcpserver;
+ adns__warn(ads,serv,0,"TCP connection lost: %s: %s",what,why);
+ close(ads->tcpsocket);
+ ads->tcpstate= server_disconnected;
+
+ for (qu= ads->timew.head; qu; qu= nqu) {
+ nqu= qu->next;
+ if (qu->state == query_udp) continue;
+ assert(qu->state == query_tcpwait || qu->state == query_tcpsent);
+ qu->state= query_tcpwait;
+ qu->tcpfailed |= (1<<serv);
+ if (qu->tcpfailed == (1<<ads->nservers)-1) {
+ LIST_UNLINK(ads->timew,qu);
+ adns__query_fail(qu,adns_s_allservfail);
+ }
+ }
+
+ ads->tcprecv.used= ads->tcpsend.used= 0;
+ ads->tcpserver= (serv+1)%ads->nservers;
+}
+
+static void tcp_connected(adns_state ads, struct timeval now) {
+ adns_query qu, nqu;
+
+ adns__debug(ads,ads->tcpserver,0,"TCP connected");
+ ads->tcpstate= server_ok;
+ for (qu= ads->timew.head; qu; qu= nqu) {
+ nqu= qu->next;
+ if (qu->state == query_udp) continue;
+ assert (qu->state == query_tcpwait);
+ adns__query_tcp(qu,now);
+ }
+}
+
+void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
+ int r, fd, tries;
+ struct sockaddr_in addr;
+ struct protoent *proto;
+ /* fixme: single TCP timeout, not once per server */
+
+ for (tries=0; tries<ads->nservers; tries++) {
+ if (ads->tcpstate == server_connecting || ads->tcpstate == server_ok) return;
+ assert(ads->tcpstate == server_disconnected);
+ assert(!ads->tcpsend.used);
+ assert(!ads->tcprecv.used);
+
+ proto= getprotobyname("tcp");
+ if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
+ fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
+ if (fd<0) {
+ adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
+ return;
+ }
+ r= adns__setnonblock(ads,fd);
+ if (r) {
+ adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
+ close(fd);
+ return;
+ }
+ memset(&addr,0,sizeof(addr));
+ addr.sin_family= AF_INET;
+ addr.sin_port= htons(DNS_PORT);
+ addr.sin_addr= ads->servers[ads->tcpserver].addr;
+ r= connect(fd,&addr,sizeof(addr));
+ ads->tcpsocket= fd;
+ ads->tcpstate= server_connecting;
+ if (r==0) { tcp_connected(ads,now); continue; }
+ if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
+ adns__tcp_broken(ads,"connect",strerror(errno));
+ }
+}
+
+/* `Interest' functions - find out which fd's we might be interested in,
+ * and when we want to be called back for a timeout.
+ */
+
+static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval maxto) {
+ struct timeval *rbuf;
+
+ if (!tv_io) return;
+ rbuf= *tv_io;
+ if (!rbuf) {
+ *tvbuf= maxto; *tv_io= tvbuf;
+ } else {
+ if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
+ }
+fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
+ maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);
+}
+
+static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
+ struct timeval now, struct timeval maxtime) {
+ ldiv_t dr;
+
+fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
+ now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);
+ if (!tv_io) return;
+ maxtime.tv_sec -= (now.tv_sec+2);
+ maxtime.tv_usec -= (now.tv_usec-2000000);
+ dr= ldiv(maxtime.tv_usec,1000000);
+ maxtime.tv_sec += dr.quot;
+ maxtime.tv_usec -= dr.quot*1000000;
+ if (maxtime.tv_sec<0) timerclear(&maxtime);
+ inter_maxto(tv_io,tvbuf,maxtime);