+ adns__debug(ads,ads->tcpserver,0,"TCP connected");
+ ads->tcpstate= server_ok;
+ for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
+ nqu= qu->next;
+ assert(qu->state == query_tcpw);
+ adns__querysend_tcp(qu,now);
+ }
+}
+
+void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
+ int r, fd, tries;
+ struct sockaddr_in addr;
+ struct protoent *proto;
+
+ for (tries=0; tries<ads->nservers; tries++) {
+ switch (ads->tcpstate) {
+ case server_connecting:
+ case server_ok:
+ case server_broken:
+ return;
+ case server_disconnected:
+ break;
+ default:
+ abort();
+ }
+
+ assert(!ads->tcpsend.used);
+ assert(!ads->tcprecv.used);
+ assert(!ads->tcprecv_skip);
+
+ 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,(const struct sockaddr*)&addr,sizeof(addr));
+ ads->tcpsocket= fd;
+ ads->tcpstate= server_connecting;
+ if (r==0) { tcp_connected(ads,now); return; }
+ if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
+ ads->tcptimeout= now;
+ timevaladd(&ads->tcptimeout,TCPCONNMS);
+ return;
+ }
+ adns__tcp_broken(ads,"connect",strerror(errno));
+ ads->tcpstate= server_disconnected;
+ }
+}
+
+/* Timeout handling functions. */
+
+void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
+ struct timeval *tv_buf) {
+ const struct timeval *now;
+ int r;
+
+ now= *now_io;
+ if (now) return;
+ r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
+ adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
+ adns_globalsystemfailure(ads);
+ return;
+}
+
+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);*/
+}