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