/**/
-static void autosys(adns_state ads, struct timeval now) {
- if (ads->iflags & adns_if_noautosys) return;
- adns_callback(ads,-1,0,0,0);
+#include "adns-internal.h"
+
+/* TCP connection management */
+
+void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
+ int serv;
+
+ assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
+ serv= ads->tcpserver;
+ warn("TCP connection lost: %s: %s",serv,why);
+ close(ads->tcpsocket);
+ ads->tcpstate= server_disconnected;
+
+ for (qu= ads->timew; 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) {
+ DLIST_UNLINK(ads->timew,qu);
+ adns__query_fail(ads,qu,adns_s_allservfail);
+ }
+ }
+
+ ads->tcpbuf.used= 0;
+ ads->tcpserver= (serv+1)%ads->nservers;
+}
+
+static void tcp_connected(adns_state ads, struct timeval now) {
+ debug("TCP connected",ads->tcpserver);
+ ads->tcpstate= server_connected;
+ 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(ads,qu,now);
+ }
+}
+
+void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
+ int r, fd, tries;
+ sockaddr_in addr;
+ /* 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->tcpbuf.used);
+
+ proto= getprotobyname("tcp");
+ if (!proto) { diag(ads,"unable to find protocol number for TCP !",-1); return; }
+ fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
+ if (fd<0) { diag(ads,"cannot create TCP socket: %s",-1,strerror(errno)); return; }
+ if (!adns__setnonblock(fd)) return;
+ memset(&addr,0,sizeof(addr));
+ addr.sin_family= AF_INET;
+ addr.sin_port= htons(NSPORT);
+ 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); continue; }
+ if (errno == EWOULDBLOCK || errno == EINPROGRESS) return;
+ adns__tcp_broken(ads,"connect",strerror(errno));
+ }
}
+/* Callback procedures - these do the real work of reception and timeout, etc. */
+
static int callb_checkfd(int maxfd, const fd_set *fds, int fd) {
return maxfd<0 || !fds ? 1 :
fd<maxfd && FD_ISSET(fd,fds);
}
-int adns_callback(adns_state ads, int maxfd,
- const fd_set *readfds, const fd_set *writefds,
- const fd_set *exceptfds) {
- int skip, dgramlen, count;
+static int internal_callback(adns_state ads, int maxfd,
+ const fd_set *readfds, const fd_set *writefds,
+ const fd_set *exceptfds) {
+ int skip, dgramlen, count, udpaddrlen, oldtcpsocket;
enum adns__tcpstate oldtcpstate;
+ unsigned char udpbuf[UDPMAXDGRAM];
+ struct sockaddr_in udpaddr;
count= 0;
- oldtcpstate= ads->tcpstate;
-
- if (ads->tcpstate == server_connecting) {
+
+ switch (ads->tcpstate) {
+ case server_disconnected:
+ break;
+ case server_connecting:
if (callb_checkfd(maxfd,writefds,ads->tcpsocket)) {
count++;
assert(ads->tcprecv.used==0);
if (ads->tcprecv.buf) {
r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
- diag("nameserver %s TCP connection made",
- inet_ntoa(ads->servers[ads->tcpserver].addr));
- ads->tcpstate= server_connected;
+ tcpserver_connected(ads);
} else if (r>0) {
tcpserver_broken(ads,"connect/read","sent data before first request");
} else if (errno!=EINTR) {
- tcpserver_broken(ads,"connect",strerror(errno));
+ tcpserver_broken(ads,"connect/read",strerror(errno));
}
}
}
- }
- if (ads->tcpstate == server_connected) {
- if (oldtcpstate == server_connected)
- count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) +
- callb_checkfd(maxfd,exceptfds,ads->tcpsocket) +
- (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket));
- if (oldtcpstate != server_connected || callb_checkfd(maxfd,readfds,ads->tcpsocket)) {
+ break;
+ case server_ok:
+ count+= callb_checkfd(maxfd,readfds,ads->tcpsocket) +
+ callb_checkfd(maxfd,exceptfds,ads->tcpsocket) +
+ (ads->tcpsend.used && callb_checkfd(maxfd,writefds,ads->tcpsocket));
+ if (callb_checkfd(maxfd,readfds,ads->tcpsocket)) {
skip= 0;
for (;;) {
if (ads->tcprecv.used<skip+2) {
if (ads->tcprecv.used<skip+2+dgramlen) {
want= 2+dgramlen;
} else {
- procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,-1);
+ procdgram(ads,ads->tcprecv.buf+skip+2,dgramlen,ads->tcpserver);
skip+= 2+dgramlen; continue;
}
}
- Ads->tcprecv.used -= skip;
+ ads->tcprecv.used -= skip;
memmove(ads->tcprecv.buf,ads->tcprecv.buf+skip,ads->tcprecv.used);
vbuf_ensure(&ads->tcprecv,want);
if (ads->tcprecv.used >= ads->tcprecv.avail) break;
memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
}
}
+ default:
+ abort();
}
- if (
- break;
-
-
- }
-
- tcpserver_broken(
-
- if (ads-
- used= 0;
- for (;;) {
- vbuf_ensure(&ads->tcprecv,2);
- vbuf_ensure(&ads->tcprecv,
- if (ads->tcprecv.avail<2) break;
- if (ads->tcprecv.used
-
- if (ads->tcprecv.used<2 && ads->tcprecv.avail
- if (ads->tcprecv.used<2 && ads->tcprecv.avail
- r= read(ads->tcpsocket,
- if (adns->tcprecv.used<2) {
- if (
-
- if (ads->tcpstate != server_disc) {
-
-
+ if (callb_checkfd(maxfd,readfds,ads->udpsocket)) {
+ count++;
+ for (;;) {
+ udpaddrlen= sizeof(udpaddr);
+ r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,&udpaddr,&udpaddrlen);
+ if (r<0) {
+ if (!(errno == EAGAIN || errno == EWOULDBLOCK ||
+ errno == EINTR || errno == ENOMEM || errno == ENOBUFS))
+ warn("datagram receive error: %s",strerror(errno));
+ break;
+ }
+ if (udpaddrlen != sizeof(udpaddr)) {
+ diag("datagram received with wrong address length %d (expected %d)",
+ udpaddrlen,sizeof(udpaddr));
+ continue;
+ }
+ if (udpaddr.sin_family != AF_INET) {
+ diag("datagram received with wrong protocol family %u (expected %u)",
+ udpaddr.sin_family,AF_INET);
+ continue;
+ }
+ if (ntohs(udpaddr.sin_port) != NSPORT) {
+ diag("datagram received from wrong port %u (expected %u)",
+ ntohs(udpaddr.sin_port),NSPORT);
+ continue;
+ }
+ for (serv= 0;
+ serv < ads->nservers &&
+ ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
+ serv++);
+ if (serv >= ads->nservers) {
+ warn("datagram received from unknown nameserver %s",inet_ntoa(udpaddr.sin_addr));
+ continue;
+ }
+ procdgram(ads,udpbuf,r,serv);
+ }
+ }
+}
+
+static void checktimeouts(adns_state ads, struct timeval now,
+ struct timeval **tv_io, struct timeval *tvbuf) {
+ for (qu= ads->timew; qu; qu= nqu) {
+ nqu= qu->next;
+ if (timercmp(&now,qu->timeout,>)) {
+ DLIST_UNLINK(ads->timew,qu);
+ if (qu->state != state_udp) {
+ query_fail(ads,qu,adns_s_notresponding);
+ } else {
+ adns__query_udp(ads,qu,now);
+ }
+ } else {
+ inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
}
- if (maxfd<0 || !readfds || (FD_ISSET
- ads->
-
- abort(); /* FIXME */
+ }
+}
+
+int adns_callback(adns_state ads, int maxfd,
+ const fd_set *readfds, const fd_set *writefds,
+ const fd_set *exceptfds) {
+ struct timeval now;
+
+ r= gettimeofday(&now,0);
+ if (!r) checktimeouts(ads,now,0,0);
+ return internal_callback(ads,maxfd,readfds,writefds,exceptfds);
}
- diag("nameserver #%d (%s) TCP connection died: %s",
- inet_ntoa(ads->servers[tcpserver].addr),
+
+/* `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; return; }
if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
struct timeval now, struct timeval maxtime) {
ldiv_t dr;
-
+
+ if (!tv_io) return;
maxtime.tv_sec -= (now.tv_sec-1);
maxtime.tv_usec += (1000-now.tv_usec);
dr= ldiv(maxtime.tv_usec,1000);
inter_maxto(tv_io,tvbuf,maxtime);
}
-static void localresourcerr(struct timeval **tv_io, struct timeval *tvbuf,
- const char *syscall) {
- struct timeval tvto_lr;
-
- diag(ads,"local system resources scarce (during %s): %s",syscall,strerror(errno));
- timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS);
- inter_maxto(tv_io, tvbuf, tvto_lr);
- return;
-}
-
static void inter_addfd(int *maxfd, fd_set *fds, int fd) {
+ if (!maxfd || !fds) return;
if (fd>=*maxfd) *maxfd= fd+1;
FD_SET(fd,fds);
}
fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval **tv_io, struct timeval *tvbuf) {
struct timeval now;
+ struct timeval tvto_lr;
adns_query qu;
int r;
r= gettimeofday(&now,0);
- if (r) { localresourcerr(tv_io,tvbuf,"gettimeofday"); return; }
-
- for (qu= ads->timew; qu; qu= nqu) {
- nqu= qu->next;
- if (timercmp(&now,qu->timeout,>)) {
- DLIST_UNLINK(ads->timew,qu);
- if (qu->nextudpserver == -1) {
- query_fail(ads,qu,adns_s_notresponding);
- } else {
- DLIST_LINKTAIL(ads->tosend,qu);
- }
- } else {
- inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
- }
+ if (r) {
+ warn(ads,"gettimeofday failed - will sleep for a bit: %s",-1,strerror(errno));
+ timerclear(&tvto_lr); timevaladd(&tvto_lr,LOCALRESOURCEMS);
+ inter_maxto(tv_io, tvbuf, tvto_lr);
+ } else {
+ checktimeouts(ads,now,tv_io,tvbuf);
}
- for (qu= ads->tosend; qu; qu= nqu) {
- nqu= qu->next;
- quproc_tosend(ads,qu,now);
- }
-
inter_addfd(maxfd,readfds,ads->udpsocket);
+
switch (ads->tcpstate) {
case server_disc:
break;
default:
abort();
}
-
+}
+
+/* User-visible functions and their implementation. */
+
+static void autosys(adns_state ads, struct timeval now) {
+ if (ads->iflags & adns_if_noautosys) return;
+ adns_callback(ads,-1,0,0,0);
}
static int internal_check(adns_state ads,
for (;;) {
r= internal_check(ads,query_io,answer_r,context_r);
if (r && r != EWOULDBLOCK) return r;
- FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
maxfd= 0; tvp= 0;
+ FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
adns_interest(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf);
rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
if (rsel==-1) return r;
- rcb= adns_callback(ads,maxfd,&readfds,&writefds,&exceptfds);
+ rcb= internal_callback(ads,maxfd,&readfds,&writefds,&exceptfds);
assert(rcb==rsel);
}
}