On timeout with adnsresfilter in non-forever mode, resubmit queries.
[adns] / src / event.c
CommitLineData
e576be50 1/*
2 * event.c
3 * - event loop core
4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
6 */
7/*
d942707d 8 * This file is
9 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
10 *
11 * It is part of adns, which is
12 * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
13 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
e576be50 14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
18 * any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software Foundation,
27 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
656b2da9 29
4353a5c4 30#include <errno.h>
31#include <stdlib.h>
0f091044 32#include <unistd.h>
4353a5c4 33
0f091044 34#include <sys/types.h>
35#include <sys/time.h>
4353a5c4 36#include <netdb.h>
eec1d9c8 37#include <sys/socket.h>
38#include <netinet/in.h>
4353a5c4 39#include <arpa/inet.h>
40
41#include "internal.h"
37e28fde 42
620c146d 43/* TCP connection management. */
44
f7f83b4a 45static void tcp_close(adns_state ads) {
620c146d 46 int serv;
47
48 serv= ads->tcpserver;
49 close(ads->tcpsocket);
6a578b2c 50 ads->tcpsocket= -1;
70ad7a2a 51 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
620c146d 52}
656b2da9 53
8402e34c 54void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
55 int serv;
56
57 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
ddfda861 58 serv= ads->tcpserver;
f7f83b4a 59 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
60
61 tcp_close(ads);
62 ads->tcpstate= server_broken;
63 ads->tcpserver= (serv+1)%ads->nservers;
8402e34c 64}
65
ddfda861 66static void tcp_connected(adns_state ads, struct timeval now) {
4353a5c4 67 adns_query qu, nqu;
68
68442019 69 adns__debug(ads,ads->tcpserver,0,"TCP connected");
4353a5c4 70 ads->tcpstate= server_ok;
f7f83b4a 71 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
ddfda861 72 nqu= qu->next;
f7f83b4a 73 assert(qu->state == query_tcpw);
74 adns__querysend_tcp(qu,now);
ddfda861 75 }
76}
77
78void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
8402e34c 79 int r, fd, tries;
4353a5c4 80 struct sockaddr_in addr;
81 struct protoent *proto;
8402e34c 82
83 for (tries=0; tries<ads->nservers; tries++) {
f7f83b4a 84 switch (ads->tcpstate) {
85 case server_connecting:
86 case server_ok:
87 case server_broken:
88 return;
89 case server_disconnected:
90 break;
91 default:
92 abort();
93 }
94
4353a5c4 95 assert(!ads->tcpsend.used);
96 assert(!ads->tcprecv.used);
70ad7a2a 97 assert(!ads->tcprecv_skip);
8402e34c 98
99 proto= getprotobyname("tcp");
68442019 100 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
8402e34c 101 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
4353a5c4 102 if (fd<0) {
68442019 103 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 104 return;
105 }
106 r= adns__setnonblock(ads,fd);
107 if (r) {
68442019 108 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
4353a5c4 109 close(fd);
110 return;
111 }
8402e34c 112 memset(&addr,0,sizeof(addr));
113 addr.sin_family= AF_INET;
98a3f706 114 addr.sin_port= htons(DNS_PORT);
8402e34c 115 addr.sin_addr= ads->servers[ads->tcpserver].addr;
eec1d9c8 116 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
8402e34c 117 ads->tcpsocket= fd;
118 ads->tcpstate= server_connecting;
f7f83b4a 119 if (r==0) { tcp_connected(ads,now); return; }
120 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
121 ads->tcptimeout= now;
122 timevaladd(&ads->tcptimeout,TCPCONNMS);
123 return;
124 }
ddfda861 125 adns__tcp_broken(ads,"connect",strerror(errno));
f7f83b4a 126 ads->tcpstate= server_disconnected;
8402e34c 127 }
128}
129
620c146d 130/* Timeout handling functions. */
131
132void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
133 struct timeval *tv_buf) {
134 const struct timeval *now;
135 int r;
136
137 now= *now_io;
138 if (now) return;
139 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
140 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
141 adns_globalsystemfailure(ads);
142 return;
143}
4353a5c4 144
145static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
146 struct timeval maxto) {
147 struct timeval *rbuf;
148
149 if (!tv_io) return;
150 rbuf= *tv_io;
94436798 151 if (!rbuf) {
152 *tvbuf= maxto; *tv_io= tvbuf;
153 } else {
154 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
155 }
ffbda80c 156/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
157 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
4353a5c4 158}
159
160static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
161 struct timeval now, struct timeval maxtime) {
f7f83b4a 162 /* tv_io may be 0 */
4353a5c4 163 ldiv_t dr;
164
ffbda80c 165/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
166 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
4353a5c4 167 if (!tv_io) return;
94436798 168 maxtime.tv_sec -= (now.tv_sec+2);
169 maxtime.tv_usec -= (now.tv_usec-2000000);
170 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 171 maxtime.tv_sec += dr.quot;
94436798 172 maxtime.tv_usec -= dr.quot*1000000;
173 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 174 inter_maxto(tv_io,tvbuf,maxtime);
175}
176
f7f83b4a 177static void timeouts_queue(adns_state ads, int act,
178 struct timeval **tv_io, struct timeval *tvbuf,
179 struct timeval now, struct query_queue *queue) {
4353a5c4 180 adns_query qu, nqu;
f7f83b4a 181
182 for (qu= queue->head; qu; qu= nqu) {
4353a5c4 183 nqu= qu->next;
d855b532 184 if (!timercmp(&now,&qu->timeout,>)) {
620c146d 185 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
186 } else {
3905bfc2 187 if (!act) {
6a9f9437 188 tvbuf->tv_sec= 0;
189 tvbuf->tv_usec= 0;
190 *tv_io= tvbuf;
3905bfc2 191 return;
192 }
f7f83b4a 193 LIST_UNLINK(*queue,qu);
d8c062fa 194 if (qu->state != query_tosend) {
3955725c 195 adns__query_fail(qu,adns_s_timeout);
4353a5c4 196 } else {
d8c062fa 197 adns__query_send(qu,now);
4353a5c4 198 }
f7f83b4a 199 nqu= queue->head;
4353a5c4 200 }
201 }
f7f83b4a 202}
203
204static void tcp_events(adns_state ads, int act,
205 struct timeval **tv_io, struct timeval *tvbuf,
206 struct timeval now) {
207 adns_query qu, nqu;
208
209 for (;;) {
210 switch (ads->tcpstate) {
211 case server_broken:
212 for (qu= ads->tcpw.head; qu; qu= nqu) {
213 nqu= qu->next;
214 assert(qu->state == query_tcpw);
215 if (qu->retries > ads->nservers) {
216 LIST_UNLINK(ads->tcpw,qu);
217 adns__query_fail(qu,adns_s_allservfail);
218 }
219 }
220 ads->tcpstate= server_disconnected;
221 case server_disconnected: /* fall through */
222 if (!ads->tcpw.head) return;
223 adns__tcp_tryconnect(ads,now);
224 break;
225 case server_ok:
226 if (ads->tcpw.head) return;
227 if (!ads->tcptimeout.tv_sec) {
228 assert(!ads->tcptimeout.tv_usec);
229 ads->tcptimeout= now;
230 timevaladd(&ads->tcptimeout,TCPIDLEMS);
231 }
232 case server_connecting: /* fall through */
233 if (!timercmp(&now,&ads->tcptimeout,>)) {
234 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
235 return;
236 } {
237 /* TCP timeout has happened */
238 switch (ads->tcpstate) {
239 case server_connecting: /* failed to connect */
240 adns__tcp_broken(ads,"unable to make connection","timed out");
241 break;
242 case server_ok: /* idle timeout */
243 tcp_close(ads);
244 ads->tcpstate= server_disconnected;
245 return;
246 default:
247 abort();
248 }
249 }
250 break;
251 default:
252 abort();
253 }
254 }
255}
256
257void adns__timeouts(adns_state ads, int act,
258 struct timeval **tv_io, struct timeval *tvbuf,
259 struct timeval now) {
260 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
261 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
262 tcp_events(ads,act,tv_io,tvbuf,now);
263}
94436798 264
620c146d 265void adns_firsttimeout(adns_state ads,
266 struct timeval **tv_io, struct timeval *tvbuf,
267 struct timeval now) {
28de6442 268 adns__consistency(ads,0,cc_entex);
620c146d 269 adns__timeouts(ads, 0, tv_io,tvbuf, now);
28de6442 270 adns__consistency(ads,0,cc_entex);
620c146d 271}
272
273void adns_processtimeouts(adns_state ads, const struct timeval *now) {
274 struct timeval tv_buf;
275
28de6442 276 adns__consistency(ads,0,cc_entex);
3e2e5fab 277 adns__must_gettimeofday(ads,&now,&tv_buf);
278 if (now) adns__timeouts(ads, 1, 0,0, *now);
28de6442 279 adns__consistency(ads,0,cc_entex);
a8768e80 280}
281
620c146d 282/* fd handling functions. These are the top-level of the real work of
283 * reception and often transmission.
284 */
285
286int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
287 /* Returns the number of entries filled in. Always zeroes revents. */
288
289 assert(MAX_POLLFDS==2);
a8768e80 290
620c146d 291 pollfds_buf[0].fd= ads->udpsocket;
292 pollfds_buf[0].events= POLLIN;
293 pollfds_buf[0].revents= 0;
4353a5c4 294
295 switch (ads->tcpstate) {
296 case server_disconnected:
620c146d 297 return 1;
4353a5c4 298 case server_connecting:
620c146d 299 pollfds_buf[1].events= POLLOUT;
4353a5c4 300 break;
301 case server_ok:
620c146d 302 pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
e062dcae 303 break;
4353a5c4 304 default:
305 abort();
306 }
620c146d 307 pollfds_buf[1].fd= ads->tcpsocket;
620c146d 308 return 2;
4353a5c4 309}
310
620c146d 311int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
70ad7a2a 312 int want, dgramlen, r, udpaddrlen, serv, old_skip;
98a3f706 313 byte udpbuf[DNS_MAXUDP];
37e28fde 314 struct sockaddr_in udpaddr;
620c146d 315
28de6442 316 adns__consistency(ads,0,cc_entex);
3e2e5fab 317
ddfda861 318 switch (ads->tcpstate) {
319 case server_disconnected:
ddfda861 320 case server_connecting:
ddfda861 321 break;
322 case server_ok:
620c146d 323 if (fd != ads->tcpsocket) break;
70ad7a2a 324 assert(!ads->tcprecv_skip);
620c146d 325 for (;;) {
70ad7a2a 326 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
327 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
328 ads->tcprecv.buf[ads->tcprecv_skip+1]);
329 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
330 old_skip= ads->tcprecv_skip;
331 ads->tcprecv_skip += 2+dgramlen;
332 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
333 dgramlen, ads->tcpserver, 1,*now);
334 continue;
656b2da9 335 } else {
70ad7a2a 336 want= 2+dgramlen;
656b2da9 337 }
70ad7a2a 338 } else {
339 want= 2;
656b2da9 340 }
70ad7a2a 341 ads->tcprecv.used -= ads->tcprecv_skip;
342 memmove(ads->tcprecv.buf,ads->tcprecv.buf+ads->tcprecv_skip,ads->tcprecv.used);
343 ads->tcprecv_skip= 0;
3e2e5fab 344 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
620c146d 345 assert(ads->tcprecv.used <= ads->tcprecv.avail);
346 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
347 r= read(ads->tcpsocket,
348 ads->tcprecv.buf+ads->tcprecv.used,
349 ads->tcprecv.avail-ads->tcprecv.used);
350 if (r>0) {
351 ads->tcprecv.used+= r;
352 } else {
353 if (r) {
3e2e5fab 354 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
620c146d 355 if (errno==EINTR) continue;
3e2e5fab 356 if (errno_resources(errno)) { r= errno; goto xit; }
656b2da9 357 }
620c146d 358 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
3e2e5fab 359 r= 0; goto xit;
656b2da9 360 }
620c146d 361 } /* never reached */
ddfda861 362 default:
363 abort();
656b2da9 364 }
620c146d 365 if (fd == ads->udpsocket) {
37e28fde 366 for (;;) {
367 udpaddrlen= sizeof(udpaddr);
eec1d9c8 368 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
369 (struct sockaddr*)&udpaddr,&udpaddrlen);
37e28fde 370 if (r<0) {
3e2e5fab 371 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
620c146d 372 if (errno == EINTR) continue;
3e2e5fab 373 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 374 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
3e2e5fab 375 r= 0; goto xit;
37e28fde 376 }
377 if (udpaddrlen != sizeof(udpaddr)) {
68442019 378 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
379 " (expected %d)", udpaddrlen,sizeof(udpaddr));
37e28fde 380 continue;
381 }
382 if (udpaddr.sin_family != AF_INET) {
68442019 383 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
4353a5c4 384 " %u (expected %u)",udpaddr.sin_family,AF_INET);
37e28fde 385 continue;
386 }
98a3f706 387 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
68442019 388 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
98a3f706 389 ntohs(udpaddr.sin_port),DNS_PORT);
37e28fde 390 continue;
391 }
392 for (serv= 0;
393 serv < ads->nservers &&
394 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
395 serv++);
396 if (serv >= ads->nservers) {
68442019 397 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
4353a5c4 398 inet_ntoa(udpaddr.sin_addr));
37e28fde 399 continue;
400 }
ebf4877a 401 adns__procdgram(ads,udpbuf,r,serv,0,*now);
656b2da9 402 }
37e28fde 403 }
3e2e5fab 404 r= 0;
405xit:
28de6442 406 adns__consistency(ads,0,cc_entex);
3e2e5fab 407 return r;
656b2da9 408}
656b2da9 409
620c146d 410int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
656b2da9 411 int r;
620c146d 412
28de6442 413 adns__consistency(ads,0,cc_entex);
3e2e5fab 414
620c146d 415 switch (ads->tcpstate) {
416 case server_disconnected:
417 break;
418 case server_connecting:
419 if (fd != ads->tcpsocket) break;
420 assert(ads->tcprecv.used==0);
70ad7a2a 421 assert(ads->tcprecv_skip==0);
620c146d 422 for (;;) {
3e2e5fab 423 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
620c146d 424 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
425 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
426 tcp_connected(ads,*now);
3e2e5fab 427 r= 0; goto xit;
620c146d 428 }
429 if (r>0) {
430 adns__tcp_broken(ads,"connect/read","sent data before first request");
3e2e5fab 431 r= 0; goto xit;
620c146d 432 }
433 if (errno==EINTR) continue;
3e2e5fab 434 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 435 adns__tcp_broken(ads,"connect/read",strerror(errno));
3e2e5fab 436 r= 0; goto xit;
620c146d 437 } /* not reached */
438 case server_ok:
439 if (!(ads->tcpsend.used && fd == ads->tcpsocket)) break;
440 for (;;) {
441 adns__sigpipe_protect(ads);
442 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
443 adns__sigpipe_unprotect(ads);
444 if (r<0) {
445 if (errno==EINTR) continue;
3e2e5fab 446 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
447 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 448 adns__tcp_broken(ads,"write",strerror(errno));
3e2e5fab 449 r= 0; goto xit;
620c146d 450 } else if (r>0) {
451 ads->tcpsend.used -= r;
452 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
453 }
454 } /* not reached */
455 default:
456 abort();
457 }
3e2e5fab 458 r= 0;
459xit:
28de6442 460 adns__consistency(ads,0,cc_entex);
3e2e5fab 461 return r;
620c146d 462}
463
464int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
28de6442 465 adns__consistency(ads,0,cc_entex);
620c146d 466 switch (ads->tcpstate) {
467 case server_disconnected:
468 break;
469 case server_connecting:
470 case server_ok:
471 if (fd != ads->tcpsocket) break;
472 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
3e2e5fab 473 break;
620c146d 474 default:
475 abort();
476 }
28de6442 477 adns__consistency(ads,0,cc_entex);
620c146d 478 return 0;
479}
37e28fde 480
620c146d 481static void fd_event(adns_state ads, int fd,
482 int revent, int pollflag,
483 int maxfd, const fd_set *fds,
484 int (*func)(adns_state, int fd, const struct timeval *now),
485 struct timeval now, int *r_r) {
656b2da9 486 int r;
620c146d 487
488 if (!(revent & pollflag)) return;
489 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
490 r= func(ads,fd,&now);
491 if (r) {
492 if (r_r) {
493 *r_r= r;
494 } else {
495 adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno));
496 adns_globalsystemfailure(ads);
497 }
498 }
499}
500
501void adns__fdevents(adns_state ads,
502 const struct pollfd *pollfds, int npollfds,
503 int maxfd, const fd_set *readfds,
504 const fd_set *writefds, const fd_set *exceptfds,
505 struct timeval now, int *r_r) {
506 int i, fd, revents;
507
508 for (i=0; i<npollfds; i++) {
509 fd= pollfds[i].fd;
510 if (fd >= maxfd) maxfd= fd+1;
511 revents= pollfds[i].revents;
512 fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r);
513 fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r);
514 fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r);
515 }
516}
37e28fde 517
620c146d 518/* Wrappers for select(2). */
519
520void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
521 fd_set *writefds_io, fd_set *exceptfds_io,
522 struct timeval **tv_mod, struct timeval *tv_tobuf,
523 const struct timeval *now) {
524 struct timeval tv_nowbuf;
525 struct pollfd pollfds[MAX_POLLFDS];
526 int i, fd, maxfd, npollfds;
527
28de6442 528 adns__consistency(ads,0,cc_entex);
3e2e5fab 529
620c146d 530 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
531 /* The caller is planning to sleep. */
532 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
3e2e5fab 533 if (!now) goto xit;
620c146d 534 adns__timeouts(ads, 1, tv_mod,tv_tobuf, *now);
535 }
536
537 npollfds= adns__pollfds(ads,pollfds);
538 maxfd= *maxfd_io;
539 for (i=0; i<npollfds; i++) {
540 fd= pollfds[i].fd;
541 if (fd >= maxfd) maxfd= fd+1;
542 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
543 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
544 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
545 }
546 *maxfd_io= maxfd;
3e2e5fab 547
548xit:
28de6442 549 adns__consistency(ads,0,cc_entex);
620c146d 550}
551
552void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
553 const fd_set *writefds, const fd_set *exceptfds,
554 const struct timeval *now) {
fc6a52ae 555 struct timeval tv_buf;
620c146d 556 struct pollfd pollfds[MAX_POLLFDS];
fc6a52ae 557 int npollfds, i;
620c146d 558
28de6442 559 adns__consistency(ads,0,cc_entex);
fc6a52ae 560 adns__must_gettimeofday(ads,&now,&tv_buf);
3e2e5fab 561 if (!now) goto xit;
620c146d 562 adns_processtimeouts(ads,now);
563
564 npollfds= adns__pollfds(ads,pollfds);
fc6a52ae 565 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
620c146d 566 adns__fdevents(ads,
567 pollfds,npollfds,
568 maxfd,readfds,writefds,exceptfds,
569 *now, 0);
3e2e5fab 570xit:
28de6442 571 adns__consistency(ads,0,cc_entex);
620c146d 572}
573
574/* General helpful functions. */
575
576void adns_globalsystemfailure(adns_state ads) {
28de6442 577 adns__consistency(ads,0,cc_entex);
3e2e5fab 578
f7f83b4a 579 while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
580 while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
620c146d 581
582 switch (ads->tcpstate) {
583 case server_connecting:
584 case server_ok:
f7f83b4a 585 adns__tcp_broken(ads,0,0);
620c146d 586 break;
587 case server_disconnected:
588 break;
589 default:
590 abort();
591 }
28de6442 592 adns__consistency(ads,0,cc_entex);
656b2da9 593}
594
620c146d 595int adns_processany(adns_state ads) {
aedf10f2 596 int r, i;
620c146d 597 struct timeval now;
598 struct pollfd pollfds[MAX_POLLFDS];
599 int npollfds;
600
28de6442 601 adns__consistency(ads,0,cc_entex);
3e2e5fab 602
620c146d 603 r= gettimeofday(&now,0);
604 if (!r) adns_processtimeouts(ads,&now);
605
25913ee2 606 /* We just use adns__fdevents to loop over the fd's trying them.
607 * This seems more sensible than calling select, since we're most
608 * likely just to want to do a read on one or two fds anyway.
609 */
620c146d 610 npollfds= adns__pollfds(ads,pollfds);
aedf10f2 611 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events;
620c146d 612 adns__fdevents(ads,
613 pollfds,npollfds,
614 0,0,0,0,
615 now,&r);
3e2e5fab 616
28de6442 617 adns__consistency(ads,0,cc_entex);
c118a725 618 return 0;
620c146d 619}
ddfda861 620
4353a5c4 621void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 622 if (ads->iflags & adns_if_noautosys) return;
620c146d 623 adns_processany(ads);
ddfda861 624}
625
940356bd 626int adns__internal_check(adns_state ads,
627 adns_query *query_io,
628 adns_answer **answer,
629 void **context_r) {
656b2da9 630 adns_query qu;
631
632 qu= *query_io;
633 if (!qu) {
4ae90a83 634 if (ads->output.head) {
635 qu= ads->output.head;
f7f83b4a 636 } else if (ads->udpw.head || ads->tcpw.head) {
4ae90a83 637 return EAGAIN;
638 } else {
639 return ESRCH;
640 }
656b2da9 641 } else {
226c5eef 642 if (qu->id>=0) return EAGAIN;
656b2da9 643 }
644 LIST_UNLINK(ads->output,qu);
8e5b0abb 645 *answer= qu->answer;
a6536d8b 646 if (context_r) *context_r= qu->ctx.ext;
8ce38e76 647 *query_io= qu;
656b2da9 648 free(qu);
649 return 0;
650}
651
652int adns_wait(adns_state ads,
653 adns_query *query_io,
654 adns_answer **answer_r,
655 void **context_r) {
620c146d 656 int r, maxfd, rsel;
656b2da9 657 fd_set readfds, writefds, exceptfds;
658 struct timeval tvbuf, *tvp;
659
28de6442 660 adns__consistency(ads,*query_io,cc_entex);
656b2da9 661 for (;;) {
940356bd 662 r= adns__internal_check(ads,query_io,answer_r,context_r);
226c5eef 663 if (r != EAGAIN) break;
656b2da9 664 maxfd= 0; tvp= 0;
37e28fde 665 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
620c146d 666 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
656b2da9 667 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
88372443 668 if (rsel==-1) {
620c146d 669 if (errno == EINTR) {
3e2e5fab 670 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
620c146d 671 } else {
672 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
673 adns_globalsystemfailure(ads);
674 }
675 } else {
676 assert(rsel >= 0);
677 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
88372443 678 }
656b2da9 679 }
28de6442 680 adns__consistency(ads,0,cc_entex);
3e2e5fab 681 return r;
656b2da9 682}
683
684int adns_check(adns_state ads,
685 adns_query *query_io,
686 adns_answer **answer_r,
687 void **context_r) {
4353a5c4 688 struct timeval now;
689 int r;
690
28de6442 691 adns__consistency(ads,*query_io,cc_entex);
620c146d 692 r= gettimeofday(&now,0);
693 if (!r) adns__autosys(ads,now);
3e2e5fab 694
940356bd 695 r= adns__internal_check(ads,query_io,answer_r,context_r);
28de6442 696 adns__consistency(ads,0,cc_entex);
3e2e5fab 697 return r;
656b2da9 698}