*/
/*
* This file is part of adns, which is
- * Copyright (C) 1997-2000,2003,2006 Ian Jackson
+ * Copyright (C) 1997-2000,2003,2006,2014-2016 Ian Jackson
+ * Copyright (C) 2014 Mark Wooding
* Copyright (C) 1999-2000,2003,2006 Tony Finch
* Copyright (C) 1991 Massachusetts Institute of Technology
* (See the file INSTALL for full details.)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
+ * the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program; if not, write to the Free Software Foundation.
*/
#include <errno.h>
}
int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
- int want, dgramlen, r, i, udpaddrlen, serv, old_skip;
+ int want, dgramlen, r, i, serv, old_skip;
+ socklen_t udpaddrlen;
byte udpbuf[DNS_MAXUDP];
char addrbuf[ADNS_ADDR2TEXT_BUFLEN];
struct udpsocket *udp;
}
for (i=0; i<ads->nudpsockets; i++) {
udp= &ads->udpsockets[i];
- if (fd == udp->fd) {
- for (;;) {
- udpaddrlen= sizeof(udpaddr);
- r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
- if (r<0) {
- if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
- if (errno == EINTR) continue;
- if (errno_resources(errno)) { r= errno; goto xit; }
- adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
- r= 0; goto xit;
- }
- for (serv= 0;
- serv < ads->nservers &&
- !adns__sockaddrs_equal(&udpaddr.sa,
- &ads->servers[serv].addr.sa);
- serv++);
- if (serv >= ads->nservers) {
- adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
- adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
- continue;
- }
- adns__procdgram(ads,udpbuf,r,serv,0,*now);
+ if (fd != udp->fd) continue;
+ for (;;) {
+ udpaddrlen= sizeof(udpaddr);
+ r= recvfrom(fd,udpbuf,sizeof(udpbuf),0, &udpaddr.sa,&udpaddrlen);
+ if (r<0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
+ if (errno == EINTR) continue;
+ if (errno_resources(errno)) { r= errno; goto xit; }
+ adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
+ r= 0; goto xit;
}
- break;
+ for (serv= 0;
+ serv < ads->nservers &&
+ !adns__sockaddrs_equal(&udpaddr.sa,
+ &ads->servers[serv].addr.sa);
+ serv++);
+ if (serv >= ads->nservers) {
+ adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
+ adns__sockaddr_ntoa(&udpaddr.sa, addrbuf));
+ continue;
+ }
+ adns__procdgram(ads,udpbuf,r,serv,0,*now);
}
+ break;
}
r= 0;
xit:
assert(ads->tcprecv.used==0);
assert(ads->tcprecv_skip==0);
for (;;) {
+ /* This function can be called even if the fd wasn't actually
+ * flagged as writeable. For asynch tcp connect we have to
+ * actually use the writeability to tell us the connect has
+ * completed (or failed), so we need to double check. */
+ fd_set writeable;
+ struct timeval timeout = { 0,0 };
+ FD_ZERO(&writeable);
+ FD_SET(ads->tcpsocket,&writeable);
+ r= select(ads->tcpsocket+1,0,&writeable,0,&timeout);
+ if (r==0) break;
+ if (r<0) {
+ if (errno==EINTR) continue;
+ adns__tcp_broken(ads,"select","failed connecting writeability check");
+ r= 0; goto xit;
+ }
+ assert(FD_ISSET(ads->tcpsocket,&writeable));
if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
- r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
+ r= read(ads->tcpsocket,ads->tcprecv.buf,1);
if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
tcp_connected(ads,*now);
r= 0; goto xit;
adns__tcp_broken(ads,"write",strerror(errno));
r= 0; goto xit;
} else if (r>0) {
+ assert(r <= ads->tcpsend.used);
ads->tcpsend.used -= r;
memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
}
/* General helpful functions. */
void adns_globalsystemfailure(adns_state ads) {
+ /* Must not be called by adns during actual processing of a
+ * particular query, since it reenters adns. Only safe to call in
+ * situations where it would be safe to call adns_returning. */
adns__consistency(ads,0,cc_entex);
- while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
- while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
+ for (;;) {
+ adns_query qu;
+#define GSF_QQ(QQ) \
+ if ((qu= ads->QQ.head)) { \
+ LIST_UNLINK(ads->QQ,qu); \
+ adns__query_fail(qu, adns_s_systemfail); \
+ continue; \
+ }
+ GSF_QQ(udpw);
+ GSF_QQ(tcpw);
+#undef GSF_QQ
+ break;
+ }
switch (ads->tcpstate) {
case server_connecting: