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