chiark / gitweb /
@@ -1,10 +1,24 @@
[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"
cc0f95d0 398 " (expected %lu)", udpaddrlen,
399 (unsigned long)sizeof(udpaddr));
dfdbb32c 400 continue;
401 }
402 if (udpaddr.sin_family != AF_INET) {
ae41e040 403 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
d05cc330 404 " %u (expected %u)",udpaddr.sin_family,AF_INET);
dfdbb32c 405 continue;
406 }
5c596e4d 407 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
ae41e040 408 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
5c596e4d 409 ntohs(udpaddr.sin_port),DNS_PORT);
dfdbb32c 410 continue;
411 }
412 for (serv= 0;
413 serv < ads->nservers &&
414 ads->servers[serv].addr.s_addr != udpaddr.sin_addr.s_addr;
415 serv++);
416 if (serv >= ads->nservers) {
ae41e040 417 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
d05cc330 418 inet_ntoa(udpaddr.sin_addr));
dfdbb32c 419 continue;
420 }
c84b7355 421 adns__procdgram(ads,udpbuf,r,serv,0,*now);
6f17710a 422 }
dfdbb32c 423 }
1389dc72 424 r= 0;
425xit:
2ac463bf 426 adns__consistency(ads,0,cc_entex);
1389dc72 427 return r;
6f17710a 428}
6f17710a 429
74c94831 430int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
431 int r;
432
2ac463bf 433 adns__consistency(ads,0,cc_entex);
1389dc72 434
74c94831 435 switch (ads->tcpstate) {
436 case server_disconnected:
486ee6b1 437 case server_broken:
74c94831 438 break;
439 case server_connecting:
440 if (fd != ads->tcpsocket) break;
441 assert(ads->tcprecv.used==0);
ab898cf4 442 assert(ads->tcprecv_skip==0);
74c94831 443 for (;;) {
1389dc72 444 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
74c94831 445 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
446 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
447 tcp_connected(ads,*now);
1389dc72 448 r= 0; goto xit;
74c94831 449 }
450 if (r>0) {
451 adns__tcp_broken(ads,"connect/read","sent data before first request");
1389dc72 452 r= 0; goto xit;
74c94831 453 }
454 if (errno==EINTR) continue;
1389dc72 455 if (errno_resources(errno)) { r= errno; goto xit; }
74c94831 456 adns__tcp_broken(ads,"connect/read",strerror(errno));
1389dc72 457 r= 0; goto xit;
74c94831 458 } /* not reached */
459 case server_ok:
486ee6b1 460 if (fd != ads->tcpsocket) break;
461 while (ads->tcpsend.used) {
74c94831 462 adns__sigpipe_protect(ads);
463 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
464 adns__sigpipe_unprotect(ads);
465 if (r<0) {
466 if (errno==EINTR) continue;
1389dc72 467 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
468 if (errno_resources(errno)) { r= errno; goto xit; }
74c94831 469 adns__tcp_broken(ads,"write",strerror(errno));
1389dc72 470 r= 0; goto xit;
74c94831 471 } else if (r>0) {
472 ads->tcpsend.used -= r;
473 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
474 }
486ee6b1 475 }
476 r= 0;
477 goto xit;
74c94831 478 default:
479 abort();
480 }
1389dc72 481 r= 0;
482xit:
2ac463bf 483 adns__consistency(ads,0,cc_entex);
1389dc72 484 return r;
74c94831 485}
486
487int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
2ac463bf 488 adns__consistency(ads,0,cc_entex);
74c94831 489 switch (ads->tcpstate) {
490 case server_disconnected:
486ee6b1 491 case server_broken:
74c94831 492 break;
493 case server_connecting:
494 case server_ok:
495 if (fd != ads->tcpsocket) break;
496 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
1389dc72 497 break;
74c94831 498 default:
499 abort();
500 }
2ac463bf 501 adns__consistency(ads,0,cc_entex);
74c94831 502 return 0;
503}
504
505static void fd_event(adns_state ads, int fd,
506 int revent, int pollflag,
507 int maxfd, const fd_set *fds,
508 int (*func)(adns_state, int fd, const struct timeval *now),
509 struct timeval now, int *r_r) {
6f17710a 510 int r;
74c94831 511
512 if (!(revent & pollflag)) return;
513 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
514 r= func(ads,fd,&now);
515 if (r) {
516 if (r_r) {
517 *r_r= r;
518 } else {
519 adns__diag(ads,-1,0,"process fd failed after select: %s",strerror(errno));
520 adns_globalsystemfailure(ads);
521 }
522 }
523}
524
525void adns__fdevents(adns_state ads,
526 const struct pollfd *pollfds, int npollfds,
527 int maxfd, const fd_set *readfds,
528 const fd_set *writefds, const fd_set *exceptfds,
529 struct timeval now, int *r_r) {
530 int i, fd, revents;
531
532 for (i=0; i<npollfds; i++) {
533 fd= pollfds[i].fd;
534 if (fd >= maxfd) maxfd= fd+1;
535 revents= pollfds[i].revents;
536 fd_event(ads,fd, revents,POLLIN, maxfd,readfds, adns_processreadable,now,r_r);
537 fd_event(ads,fd, revents,POLLOUT, maxfd,writefds, adns_processwriteable,now,r_r);
538 fd_event(ads,fd, revents,POLLPRI, maxfd,exceptfds, adns_processexceptional,now,r_r);
539 }
540}
dfdbb32c 541
74c94831 542/* Wrappers for select(2). */
543
544void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
545 fd_set *writefds_io, fd_set *exceptfds_io,
546 struct timeval **tv_mod, struct timeval *tv_tobuf,
547 const struct timeval *now) {
548 struct timeval tv_nowbuf;
549 struct pollfd pollfds[MAX_POLLFDS];
550 int i, fd, maxfd, npollfds;
551
2ac463bf 552 adns__consistency(ads,0,cc_entex);
1389dc72 553
74c94831 554 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
555 /* The caller is planning to sleep. */
556 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
caae234b 557 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
da0e3c0a 558 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
74c94831 559 }
560
561 npollfds= adns__pollfds(ads,pollfds);
562 maxfd= *maxfd_io;
563 for (i=0; i<npollfds; i++) {
564 fd= pollfds[i].fd;
565 if (fd >= maxfd) maxfd= fd+1;
566 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
567 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
568 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
569 }
570 *maxfd_io= maxfd;
1389dc72 571
572xit:
2ac463bf 573 adns__consistency(ads,0,cc_entex);
74c94831 574}
575
576void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
577 const fd_set *writefds, const fd_set *exceptfds,
578 const struct timeval *now) {
4f973eb5 579 struct timeval tv_buf;
74c94831 580 struct pollfd pollfds[MAX_POLLFDS];
4f973eb5 581 int npollfds, i;
74c94831 582
2ac463bf 583 adns__consistency(ads,0,cc_entex);
4f973eb5 584 adns__must_gettimeofday(ads,&now,&tv_buf);
1389dc72 585 if (!now) goto xit;
74c94831 586 adns_processtimeouts(ads,now);
587
588 npollfds= adns__pollfds(ads,pollfds);
4f973eb5 589 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
74c94831 590 adns__fdevents(ads,
591 pollfds,npollfds,
592 maxfd,readfds,writefds,exceptfds,
593 *now, 0);
1389dc72 594xit:
2ac463bf 595 adns__consistency(ads,0,cc_entex);
74c94831 596}
597
598/* General helpful functions. */
599
600void adns_globalsystemfailure(adns_state ads) {
2ac463bf 601 adns__consistency(ads,0,cc_entex);
1389dc72 602
d0a057ac 603 while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
604 while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
74c94831 605
606 switch (ads->tcpstate) {
607 case server_connecting:
608 case server_ok:
d0a057ac 609 adns__tcp_broken(ads,0,0);
74c94831 610 break;
611 case server_disconnected:
486ee6b1 612 case server_broken:
74c94831 613 break;
614 default:
615 abort();
616 }
2ac463bf 617 adns__consistency(ads,0,cc_entex);
6f17710a 618}
619
74c94831 620int adns_processany(adns_state ads) {
634186ec 621 int r, i;
74c94831 622 struct timeval now;
623 struct pollfd pollfds[MAX_POLLFDS];
624 int npollfds;
625
2ac463bf 626 adns__consistency(ads,0,cc_entex);
1389dc72 627
74c94831 628 r= gettimeofday(&now,0);
629 if (!r) adns_processtimeouts(ads,&now);
630
82ab0d30 631 /* We just use adns__fdevents to loop over the fd's trying them.
632 * This seems more sensible than calling select, since we're most
633 * likely just to want to do a read on one or two fds anyway.
634 */
74c94831 635 npollfds= adns__pollfds(ads,pollfds);
95fa67fd 636 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
74c94831 637 adns__fdevents(ads,
638 pollfds,npollfds,
639 0,0,0,0,
640 now,&r);
1389dc72 641
2ac463bf 642 adns__consistency(ads,0,cc_entex);
d16697d9 643 return 0;
74c94831 644}
96e79df5 645
d05cc330 646void adns__autosys(adns_state ads, struct timeval now) {
96e79df5 647 if (ads->iflags & adns_if_noautosys) return;
74c94831 648 adns_processany(ads);
96e79df5 649}
650
ef20fccf 651int adns__internal_check(adns_state ads,
652 adns_query *query_io,
653 adns_answer **answer,
654 void **context_r) {
6f17710a 655 adns_query qu;
656
657 qu= *query_io;
658 if (!qu) {
5773be3c 659 if (ads->output.head) {
660 qu= ads->output.head;
d0a057ac 661 } else if (ads->udpw.head || ads->tcpw.head) {
5773be3c 662 return EAGAIN;
663 } else {
664 return ESRCH;
665 }
6f17710a 666 } else {
c6f9dc7b 667 if (qu->id>=0) return EAGAIN;
6f17710a 668 }
669 LIST_UNLINK(ads->output,qu);
965c9782 670 *answer= qu->answer;
cd1bde2f 671 if (context_r) *context_r= qu->ctx.ext;
8f2aa812 672 *query_io= qu;
6f17710a 673 free(qu);
674 return 0;
675}
676
677int adns_wait(adns_state ads,
678 adns_query *query_io,
679 adns_answer **answer_r,
680 void **context_r) {
74c94831 681 int r, maxfd, rsel;
6f17710a 682 fd_set readfds, writefds, exceptfds;
caae234b 683 struct timeval tvbuf, *tvp;
6f17710a 684
2ac463bf 685 adns__consistency(ads,*query_io,cc_entex);
6f17710a 686 for (;;) {
ef20fccf 687 r= adns__internal_check(ads,query_io,answer_r,context_r);
c6f9dc7b 688 if (r != EAGAIN) break;
6f17710a 689 maxfd= 0; tvp= 0;
dfdbb32c 690 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
74c94831 691 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
8744cce8 692 assert(tvp);
6f17710a 693 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
f318f883 694 if (rsel==-1) {
74c94831 695 if (errno == EINTR) {
1389dc72 696 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
74c94831 697 } else {
698 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
699 adns_globalsystemfailure(ads);
700 }
701 } else {
702 assert(rsel >= 0);
703 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
f318f883 704 }
6f17710a 705 }
2ac463bf 706 adns__consistency(ads,0,cc_entex);
1389dc72 707 return r;
6f17710a 708}
709
710int adns_check(adns_state ads,
711 adns_query *query_io,
712 adns_answer **answer_r,
713 void **context_r) {
d05cc330 714 struct timeval now;
715 int r;
716
2ac463bf 717 adns__consistency(ads,*query_io,cc_entex);
74c94831 718 r= gettimeofday(&now,0);
719 if (!r) adns__autosys(ads,now);
1389dc72 720
ef20fccf 721 r= adns__internal_check(ads,query_io,answer_r,context_r);
2ac463bf 722 adns__consistency(ads,0,cc_entex);
1389dc72 723 return r;
6f17710a 724}