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