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