fix test cases for CNAME search list bugfix
[adns] / src / event.c
CommitLineData
e576be50 1/*
2 * event.c
3 * - event loop core
4 * - TCP connection management
5 * - user-visible check/wait and event-loop-related functions
6 */
7/*
d942707d 8 * This file is
3d5cde09 9 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
d942707d 10 *
11 * It is part of adns, which is
3d5cde09 12 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
bef232ae 13 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
e576be50 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 */
656b2da9 29
4353a5c4 30#include <errno.h>
31#include <stdlib.h>
0f091044 32#include <unistd.h>
4353a5c4 33
0f091044 34#include <sys/types.h>
35#include <sys/time.h>
4353a5c4 36#include <netdb.h>
eec1d9c8 37#include <sys/socket.h>
38#include <netinet/in.h>
4353a5c4 39#include <arpa/inet.h>
40
41#include "internal.h"
2953d358 42#include "tvarith.h"
37e28fde 43
620c146d 44/* TCP connection management. */
45
f7f83b4a 46static void tcp_close(adns_state ads) {
620c146d 47 int serv;
48
49 serv= ads->tcpserver;
50 close(ads->tcpsocket);
6a578b2c 51 ads->tcpsocket= -1;
70ad7a2a 52 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
620c146d 53}
656b2da9 54
8402e34c 55void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
56 int serv;
fb901bf5 57 adns_query qu;
8402e34c 58
59 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
ddfda861 60 serv= ads->tcpserver;
f7f83b4a 61 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
62
fb901bf5 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
f7f83b4a 69 tcp_close(ads);
70 ads->tcpstate= server_broken;
71 ads->tcpserver= (serv+1)%ads->nservers;
8402e34c 72}
73
ddfda861 74static void tcp_connected(adns_state ads, struct timeval now) {
4353a5c4 75 adns_query qu, nqu;
76
68442019 77 adns__debug(ads,ads->tcpserver,0,"TCP connected");
4353a5c4 78 ads->tcpstate= server_ok;
f7f83b4a 79 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
ddfda861 80 nqu= qu->next;
f7f83b4a 81 assert(qu->state == query_tcpw);
82 adns__querysend_tcp(qu,now);
ddfda861 83 }
84}
85
86void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
8402e34c 87 int r, fd, tries;
4353a5c4 88 struct sockaddr_in addr;
89 struct protoent *proto;
8402e34c 90
91 for (tries=0; tries<ads->nservers; tries++) {
f7f83b4a 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
4353a5c4 103 assert(!ads->tcpsend.used);
104 assert(!ads->tcprecv.used);
70ad7a2a 105 assert(!ads->tcprecv_skip);
8402e34c 106
107 proto= getprotobyname("tcp");
68442019 108 if (!proto) { adns__diag(ads,-1,0,"unable to find protocol no. for TCP !"); return; }
8402e34c 109 fd= socket(AF_INET,SOCK_STREAM,proto->p_proto);
4353a5c4 110 if (fd<0) {
68442019 111 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 112 return;
113 }
114 r= adns__setnonblock(ads,fd);
115 if (r) {
68442019 116 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking: %s",strerror(r));
4353a5c4 117 close(fd);
118 return;
119 }
8402e34c 120 memset(&addr,0,sizeof(addr));
121 addr.sin_family= AF_INET;
98a3f706 122 addr.sin_port= htons(DNS_PORT);
8402e34c 123 addr.sin_addr= ads->servers[ads->tcpserver].addr;
eec1d9c8 124 r= connect(fd,(const struct sockaddr*)&addr,sizeof(addr));
8402e34c 125 ads->tcpsocket= fd;
126 ads->tcpstate= server_connecting;
f7f83b4a 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 }
ddfda861 133 adns__tcp_broken(ads,"connect",strerror(errno));
f7f83b4a 134 ads->tcpstate= server_disconnected;
8402e34c 135 }
136}
137
620c146d 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}
4353a5c4 152
4fad263d 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
4353a5c4 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;
94436798 170 if (!rbuf) {
171 *tvbuf= maxto; *tv_io= tvbuf;
172 } else {
173 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
174 }
ffbda80c 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);*/
4353a5c4 177}
178
179static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
180 struct timeval now, struct timeval maxtime) {
f7f83b4a 181 /* tv_io may be 0 */
4353a5c4 182 ldiv_t dr;
183
ffbda80c 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);*/
4353a5c4 186 if (!tv_io) return;
94436798 187 maxtime.tv_sec -= (now.tv_sec+2);
188 maxtime.tv_usec -= (now.tv_usec-2000000);
189 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 190 maxtime.tv_sec += dr.quot;
94436798 191 maxtime.tv_usec -= dr.quot*1000000;
192 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 193 inter_maxto(tv_io,tvbuf,maxtime);
194}
195
f7f83b4a 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) {
4353a5c4 199 adns_query qu, nqu;
f7f83b4a 200
201 for (qu= queue->head; qu; qu= nqu) {
4353a5c4 202 nqu= qu->next;
d855b532 203 if (!timercmp(&now,&qu->timeout,>)) {
620c146d 204 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
205 } else {
4fad263d 206 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 207 LIST_UNLINK(*queue,qu);
d8c062fa 208 if (qu->state != query_tosend) {
3955725c 209 adns__query_fail(qu,adns_s_timeout);
4353a5c4 210 } else {
d8c062fa 211 adns__query_send(qu,now);
4353a5c4 212 }
f7f83b4a 213 nqu= queue->head;
4353a5c4 214 }
215 }
f7f83b4a 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:
4fad263d 226 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 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;
4fad263d 238 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 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 */
4fad263d 249 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
f7f83b4a 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 }
4fad263d 271 return;
f7f83b4a 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}
94436798 281
620c146d 282void adns_firsttimeout(adns_state ads,
283 struct timeval **tv_io, struct timeval *tvbuf,
284 struct timeval now) {
28de6442 285 adns__consistency(ads,0,cc_entex);
620c146d 286 adns__timeouts(ads, 0, tv_io,tvbuf, now);
28de6442 287 adns__consistency(ads,0,cc_entex);
620c146d 288}
289
290void adns_processtimeouts(adns_state ads, const struct timeval *now) {
291 struct timeval tv_buf;
292
28de6442 293 adns__consistency(ads,0,cc_entex);
3e2e5fab 294 adns__must_gettimeofday(ads,&now,&tv_buf);
295 if (now) adns__timeouts(ads, 1, 0,0, *now);
28de6442 296 adns__consistency(ads,0,cc_entex);
a8768e80 297}
298
620c146d 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);
a8768e80 307
620c146d 308 pollfds_buf[0].fd= ads->udpsocket;
309 pollfds_buf[0].events= POLLIN;
310 pollfds_buf[0].revents= 0;
4353a5c4 311
312 switch (ads->tcpstate) {
313 case server_disconnected:
6c68a593 314 case server_broken:
620c146d 315 return 1;
4353a5c4 316 case server_connecting:
620c146d 317 pollfds_buf[1].events= POLLOUT;
4353a5c4 318 break;
319 case server_ok:
620c146d 320 pollfds_buf[1].events= ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
e062dcae 321 break;
4353a5c4 322 default:
323 abort();
324 }
620c146d 325 pollfds_buf[1].fd= ads->tcpsocket;
620c146d 326 return 2;
4353a5c4 327}
328
620c146d 329int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
70ad7a2a 330 int want, dgramlen, r, udpaddrlen, serv, old_skip;
98a3f706 331 byte udpbuf[DNS_MAXUDP];
37e28fde 332 struct sockaddr_in udpaddr;
620c146d 333
28de6442 334 adns__consistency(ads,0,cc_entex);
3e2e5fab 335
ddfda861 336 switch (ads->tcpstate) {
337 case server_disconnected:
fb901bf5 338 case server_broken:
ddfda861 339 case server_connecting:
ddfda861 340 break;
341 case server_ok:
620c146d 342 if (fd != ads->tcpsocket) break;
70ad7a2a 343 assert(!ads->tcprecv_skip);
428bd682 344 do {
70ad7a2a 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;
656b2da9 354 } else {
70ad7a2a 355 want= 2+dgramlen;
656b2da9 356 }
70ad7a2a 357 } else {
358 want= 2;
656b2da9 359 }
70ad7a2a 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;
3e2e5fab 363 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
620c146d 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) {
3e2e5fab 373 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
620c146d 374 if (errno==EINTR) continue;
3e2e5fab 375 if (errno_resources(errno)) { r= errno; goto xit; }
656b2da9 376 }
620c146d 377 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
656b2da9 378 }
428bd682 379 } while (ads->tcpstate == server_ok);
380 r= 0; goto xit;
ddfda861 381 default:
382 abort();
656b2da9 383 }
620c146d 384 if (fd == ads->udpsocket) {
37e28fde 385 for (;;) {
386 udpaddrlen= sizeof(udpaddr);
eec1d9c8 387 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
388 (struct sockaddr*)&udpaddr,&udpaddrlen);
37e28fde 389 if (r<0) {
3e2e5fab 390 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
620c146d 391 if (errno == EINTR) continue;
3e2e5fab 392 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 393 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
3e2e5fab 394 r= 0; goto xit;
37e28fde 395 }
396 if (udpaddrlen != sizeof(udpaddr)) {
68442019 397 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
5a0be244 398 " (expected %lu)", udpaddrlen,
399 (unsigned long)sizeof(udpaddr));
37e28fde 400 continue;
401 }
402 if (udpaddr.sin_family != AF_INET) {
68442019 403 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
4353a5c4 404 " %u (expected %u)",udpaddr.sin_family,AF_INET);
37e28fde 405 continue;
406 }
98a3f706 407 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
68442019 408 adns__diag(ads,-1,0,"datagram received from wrong port %u (expected %u)",
98a3f706 409 ntohs(udpaddr.sin_port),DNS_PORT);
37e28fde 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) {
68442019 417 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
4353a5c4 418 inet_ntoa(udpaddr.sin_addr));
37e28fde 419 continue;
420 }
ebf4877a 421 adns__procdgram(ads,udpbuf,r,serv,0,*now);
656b2da9 422 }
37e28fde 423 }
3e2e5fab 424 r= 0;
425xit:
28de6442 426 adns__consistency(ads,0,cc_entex);
3e2e5fab 427 return r;
656b2da9 428}
656b2da9 429
620c146d 430int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
656b2da9 431 int r;
620c146d 432
28de6442 433 adns__consistency(ads,0,cc_entex);
3e2e5fab 434
620c146d 435 switch (ads->tcpstate) {
436 case server_disconnected:
fb901bf5 437 case server_broken:
620c146d 438 break;
439 case server_connecting:
440 if (fd != ads->tcpsocket) break;
441 assert(ads->tcprecv.used==0);
70ad7a2a 442 assert(ads->tcprecv_skip==0);
620c146d 443 for (;;) {
3e2e5fab 444 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
620c146d 445 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
446 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
447 tcp_connected(ads,*now);
3e2e5fab 448 r= 0; goto xit;
620c146d 449 }
450 if (r>0) {
451 adns__tcp_broken(ads,"connect/read","sent data before first request");
3e2e5fab 452 r= 0; goto xit;
620c146d 453 }
454 if (errno==EINTR) continue;
3e2e5fab 455 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 456 adns__tcp_broken(ads,"connect/read",strerror(errno));
3e2e5fab 457 r= 0; goto xit;
620c146d 458 } /* not reached */
459 case server_ok:
fb901bf5 460 if (fd != ads->tcpsocket) break;
461 while (ads->tcpsend.used) {
620c146d 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;
3e2e5fab 467 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
468 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 469 adns__tcp_broken(ads,"write",strerror(errno));
3e2e5fab 470 r= 0; goto xit;
620c146d 471 } else if (r>0) {
472 ads->tcpsend.used -= r;
473 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
474 }
fb901bf5 475 }
476 r= 0;
477 goto xit;
620c146d 478 default:
479 abort();
480 }
3e2e5fab 481 r= 0;
482xit:
28de6442 483 adns__consistency(ads,0,cc_entex);
3e2e5fab 484 return r;
620c146d 485}
486
487int adns_processexceptional(adns_state ads, int fd, const struct timeval *now) {
28de6442 488 adns__consistency(ads,0,cc_entex);
620c146d 489 switch (ads->tcpstate) {
490 case server_disconnected:
fb901bf5 491 case server_broken:
620c146d 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");
3e2e5fab 497 break;
620c146d 498 default:
499 abort();
500 }
28de6442 501 adns__consistency(ads,0,cc_entex);
620c146d 502 return 0;
503}
37e28fde 504
620c146d 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) {
656b2da9 510 int r;
620c146d 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}
37e28fde 541
620c146d 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
28de6442 552 adns__consistency(ads,0,cc_entex);
3e2e5fab 553
620c146d 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);
26cc07a3 557 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
6c68a593 558 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
620c146d 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;
3e2e5fab 571
572xit:
28de6442 573 adns__consistency(ads,0,cc_entex);
620c146d 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) {
fc6a52ae 579 struct timeval tv_buf;
620c146d 580 struct pollfd pollfds[MAX_POLLFDS];
fc6a52ae 581 int npollfds, i;
620c146d 582
28de6442 583 adns__consistency(ads,0,cc_entex);
fc6a52ae 584 adns__must_gettimeofday(ads,&now,&tv_buf);
3e2e5fab 585 if (!now) goto xit;
620c146d 586 adns_processtimeouts(ads,now);
587
588 npollfds= adns__pollfds(ads,pollfds);
fc6a52ae 589 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
620c146d 590 adns__fdevents(ads,
591 pollfds,npollfds,
592 maxfd,readfds,writefds,exceptfds,
593 *now, 0);
3e2e5fab 594xit:
28de6442 595 adns__consistency(ads,0,cc_entex);
620c146d 596}
597
598/* General helpful functions. */
599
600void adns_globalsystemfailure(adns_state ads) {
28de6442 601 adns__consistency(ads,0,cc_entex);
3e2e5fab 602
f7f83b4a 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);
620c146d 605
606 switch (ads->tcpstate) {
607 case server_connecting:
608 case server_ok:
f7f83b4a 609 adns__tcp_broken(ads,0,0);
620c146d 610 break;
611 case server_disconnected:
fb901bf5 612 case server_broken:
620c146d 613 break;
614 default:
615 abort();
616 }
28de6442 617 adns__consistency(ads,0,cc_entex);
656b2da9 618}
619
620c146d 620int adns_processany(adns_state ads) {
aedf10f2 621 int r, i;
620c146d 622 struct timeval now;
623 struct pollfd pollfds[MAX_POLLFDS];
624 int npollfds;
625
28de6442 626 adns__consistency(ads,0,cc_entex);
3e2e5fab 627
620c146d 628 r= gettimeofday(&now,0);
629 if (!r) adns_processtimeouts(ads,&now);
630
25913ee2 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 */
620c146d 635 npollfds= adns__pollfds(ads,pollfds);
1ed3dba4 636 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
620c146d 637 adns__fdevents(ads,
638 pollfds,npollfds,
639 0,0,0,0,
640 now,&r);
3e2e5fab 641
28de6442 642 adns__consistency(ads,0,cc_entex);
c118a725 643 return 0;
620c146d 644}
ddfda861 645
4353a5c4 646void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 647 if (ads->iflags & adns_if_noautosys) return;
620c146d 648 adns_processany(ads);
ddfda861 649}
650
940356bd 651int adns__internal_check(adns_state ads,
652 adns_query *query_io,
653 adns_answer **answer,
654 void **context_r) {
656b2da9 655 adns_query qu;
656
657 qu= *query_io;
658 if (!qu) {
4ae90a83 659 if (ads->output.head) {
660 qu= ads->output.head;
f7f83b4a 661 } else if (ads->udpw.head || ads->tcpw.head) {
4ae90a83 662 return EAGAIN;
663 } else {
664 return ESRCH;
665 }
656b2da9 666 } else {
226c5eef 667 if (qu->id>=0) return EAGAIN;
656b2da9 668 }
669 LIST_UNLINK(ads->output,qu);
8e5b0abb 670 *answer= qu->answer;
a6536d8b 671 if (context_r) *context_r= qu->ctx.ext;
8ce38e76 672 *query_io= qu;
656b2da9 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) {
620c146d 681 int r, maxfd, rsel;
656b2da9 682 fd_set readfds, writefds, exceptfds;
26cc07a3 683 struct timeval tvbuf, *tvp;
656b2da9 684
28de6442 685 adns__consistency(ads,*query_io,cc_entex);
656b2da9 686 for (;;) {
940356bd 687 r= adns__internal_check(ads,query_io,answer_r,context_r);
226c5eef 688 if (r != EAGAIN) break;
656b2da9 689 maxfd= 0; tvp= 0;
37e28fde 690 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
620c146d 691 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
4fad263d 692 assert(tvp);
656b2da9 693 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
88372443 694 if (rsel==-1) {
620c146d 695 if (errno == EINTR) {
3e2e5fab 696 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
620c146d 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);
88372443 704 }
656b2da9 705 }
28de6442 706 adns__consistency(ads,0,cc_entex);
3e2e5fab 707 return r;
656b2da9 708}
709
710int adns_check(adns_state ads,
711 adns_query *query_io,
712 adns_answer **answer_r,
713 void **context_r) {
4353a5c4 714 struct timeval now;
715 int r;
716
28de6442 717 adns__consistency(ads,*query_io,cc_entex);
620c146d 718 r= gettimeofday(&now,0);
719 if (!r) adns__autosys(ads,now);
3e2e5fab 720
940356bd 721 r= adns__internal_check(ads,query_io,answer_r,context_r);
28de6442 722 adns__consistency(ads,0,cc_entex);
3e2e5fab 723 return r;
656b2da9 724}