src/: Fix internals to carry around address families.
[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/*
ae8cc977 8 * This file is part of adns, which is
9 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
10 * Copyright (C) 1999-2000,2003,2006 Tony Finch
11 * Copyright (C) 1991 Massachusetts Institute of Technology
12 * (See the file INSTALL for full details.)
e576be50 13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
656b2da9 28
4353a5c4 29#include <errno.h>
30#include <stdlib.h>
0f091044 31#include <unistd.h>
4353a5c4 32
0f091044 33#include <sys/types.h>
34#include <sys/time.h>
4353a5c4 35#include <netdb.h>
eec1d9c8 36#include <sys/socket.h>
37#include <netinet/in.h>
4353a5c4 38#include <arpa/inet.h>
39
40#include "internal.h"
2953d358 41#include "tvarith.h"
37e28fde 42
620c146d 43/* TCP connection management. */
44
f7f83b4a 45static void tcp_close(adns_state ads) {
620c146d 46 close(ads->tcpsocket);
6a578b2c 47 ads->tcpsocket= -1;
70ad7a2a 48 ads->tcprecv.used= ads->tcprecv_skip= ads->tcpsend.used= 0;
620c146d 49}
656b2da9 50
8402e34c 51void adns__tcp_broken(adns_state ads, const char *what, const char *why) {
52 int serv;
fb901bf5 53 adns_query qu;
8402e34c 54
55 assert(ads->tcpstate == server_connecting || ads->tcpstate == server_ok);
ddfda861 56 serv= ads->tcpserver;
f7f83b4a 57 if (what) adns__warn(ads,serv,0,"TCP connection failed: %s: %s",what,why);
58
fb901bf5 59 if (ads->tcpstate == server_connecting) {
60 /* Counts as a retry for all the queries waiting for TCP. */
61 for (qu= ads->tcpw.head; qu; qu= qu->next)
62 qu->retries++;
63 }
64
f7f83b4a 65 tcp_close(ads);
66 ads->tcpstate= server_broken;
67 ads->tcpserver= (serv+1)%ads->nservers;
8402e34c 68}
69
ddfda861 70static void tcp_connected(adns_state ads, struct timeval now) {
4353a5c4 71 adns_query qu, nqu;
72
68442019 73 adns__debug(ads,ads->tcpserver,0,"TCP connected");
4353a5c4 74 ads->tcpstate= server_ok;
f7f83b4a 75 for (qu= ads->tcpw.head; qu && ads->tcpstate == server_ok; qu= nqu) {
ddfda861 76 nqu= qu->next;
f7f83b4a 77 assert(qu->state == query_tcpw);
78 adns__querysend_tcp(qu,now);
ddfda861 79 }
80}
81
237ce710 82static void tcp_broken_events(adns_state ads) {
83 adns_query qu, nqu;
84
85 assert(ads->tcpstate == server_broken);
86 for (qu= ads->tcpw.head; qu; qu= nqu) {
87 nqu= qu->next;
88 assert(qu->state == query_tcpw);
89 if (qu->retries > ads->nservers) {
90 LIST_UNLINK(ads->tcpw,qu);
91 adns__query_fail(qu,adns_s_allservfail);
92 }
93 }
94 ads->tcpstate= server_disconnected;
95}
96
ddfda861 97void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
8402e34c 98 int r, fd, tries;
c0ec02b5 99 adns_rr_addr *addr;
4353a5c4 100 struct protoent *proto;
8402e34c 101
102 for (tries=0; tries<ads->nservers; tries++) {
f7f83b4a 103 switch (ads->tcpstate) {
104 case server_connecting:
105 case server_ok:
106 case server_broken:
107 return;
108 case server_disconnected:
109 break;
110 default:
111 abort();
112 }
113
4353a5c4 114 assert(!ads->tcpsend.used);
115 assert(!ads->tcprecv.used);
70ad7a2a 116 assert(!ads->tcprecv_skip);
8402e34c 117
118 proto= getprotobyname("tcp");
609133ee 119 if (!proto) {
120 adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
121 return;
122 }
c0ec02b5
MW
123 addr = &ads->servers[ads->tcpserver];
124 fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
4353a5c4 125 if (fd<0) {
68442019 126 adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
4353a5c4 127 return;
128 }
129 r= adns__setnonblock(ads,fd);
130 if (r) {
609133ee 131 adns__diag(ads,-1,0,"cannot make TCP socket nonblocking:"
132 " %s",strerror(r));
4353a5c4 133 close(fd);
134 return;
135 }
c0ec02b5 136 r= connect(fd,&addr->addr.sa,addr->len);
8402e34c 137 ads->tcpsocket= fd;
138 ads->tcpstate= server_connecting;
f7f83b4a 139 if (r==0) { tcp_connected(ads,now); return; }
140 if (errno == EWOULDBLOCK || errno == EINPROGRESS) {
141 ads->tcptimeout= now;
142 timevaladd(&ads->tcptimeout,TCPCONNMS);
143 return;
144 }
ddfda861 145 adns__tcp_broken(ads,"connect",strerror(errno));
237ce710 146 tcp_broken_events(ads);
8402e34c 147 }
148}
149
620c146d 150/* Timeout handling functions. */
151
152void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io,
153 struct timeval *tv_buf) {
154 const struct timeval *now;
155 int r;
156
157 now= *now_io;
158 if (now) return;
159 r= gettimeofday(tv_buf,0); if (!r) { *now_io= tv_buf; return; }
160 adns__diag(ads,-1,0,"gettimeofday failed: %s",strerror(errno));
161 adns_globalsystemfailure(ads);
162 return;
163}
4353a5c4 164
4fad263d 165static void inter_immed(struct timeval **tv_io, struct timeval *tvbuf) {
166 struct timeval *rbuf;
167
168 if (!tv_io) return;
169
170 rbuf= *tv_io;
171 if (!rbuf) { *tv_io= rbuf= tvbuf; }
172
173 timerclear(rbuf);
174}
175
4353a5c4 176static void inter_maxto(struct timeval **tv_io, struct timeval *tvbuf,
177 struct timeval maxto) {
178 struct timeval *rbuf;
179
180 if (!tv_io) return;
181 rbuf= *tv_io;
94436798 182 if (!rbuf) {
183 *tvbuf= maxto; *tv_io= tvbuf;
184 } else {
185 if (timercmp(rbuf,&maxto,>)) *rbuf= maxto;
186 }
ffbda80c 187/*fprintf(stderr,"inter_maxto maxto=%ld.%06ld result=%ld.%06ld\n",
188 maxto.tv_sec,maxto.tv_usec,(**tv_io).tv_sec,(**tv_io).tv_usec);*/
4353a5c4 189}
190
191static void inter_maxtoabs(struct timeval **tv_io, struct timeval *tvbuf,
192 struct timeval now, struct timeval maxtime) {
f7f83b4a 193 /* tv_io may be 0 */
4353a5c4 194 ldiv_t dr;
195
ffbda80c 196/*fprintf(stderr,"inter_maxtoabs now=%ld.%06ld maxtime=%ld.%06ld\n",
197 now.tv_sec,now.tv_usec,maxtime.tv_sec,maxtime.tv_usec);*/
4353a5c4 198 if (!tv_io) return;
94436798 199 maxtime.tv_sec -= (now.tv_sec+2);
200 maxtime.tv_usec -= (now.tv_usec-2000000);
201 dr= ldiv(maxtime.tv_usec,1000000);
4353a5c4 202 maxtime.tv_sec += dr.quot;
94436798 203 maxtime.tv_usec -= dr.quot*1000000;
204 if (maxtime.tv_sec<0) timerclear(&maxtime);
4353a5c4 205 inter_maxto(tv_io,tvbuf,maxtime);
206}
207
f7f83b4a 208static void timeouts_queue(adns_state ads, int act,
209 struct timeval **tv_io, struct timeval *tvbuf,
210 struct timeval now, struct query_queue *queue) {
4353a5c4 211 adns_query qu, nqu;
f7f83b4a 212
213 for (qu= queue->head; qu; qu= nqu) {
4353a5c4 214 nqu= qu->next;
d855b532 215 if (!timercmp(&now,&qu->timeout,>)) {
620c146d 216 inter_maxtoabs(tv_io,tvbuf,now,qu->timeout);
217 } else {
4fad263d 218 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 219 LIST_UNLINK(*queue,qu);
d8c062fa 220 if (qu->state != query_tosend) {
3955725c 221 adns__query_fail(qu,adns_s_timeout);
4353a5c4 222 } else {
d8c062fa 223 adns__query_send(qu,now);
4353a5c4 224 }
f7f83b4a 225 nqu= queue->head;
4353a5c4 226 }
227 }
f7f83b4a 228}
229
230static void tcp_events(adns_state ads, int act,
231 struct timeval **tv_io, struct timeval *tvbuf,
232 struct timeval now) {
f7f83b4a 233 for (;;) {
234 switch (ads->tcpstate) {
235 case server_broken:
4fad263d 236 if (!act) { inter_immed(tv_io,tvbuf); return; }
237ce710 237 tcp_broken_events(ads);
f7f83b4a 238 case server_disconnected: /* fall through */
239 if (!ads->tcpw.head) return;
4fad263d 240 if (!act) { inter_immed(tv_io,tvbuf); return; }
f7f83b4a 241 adns__tcp_tryconnect(ads,now);
242 break;
243 case server_ok:
244 if (ads->tcpw.head) return;
245 if (!ads->tcptimeout.tv_sec) {
246 assert(!ads->tcptimeout.tv_usec);
247 ads->tcptimeout= now;
248 timevaladd(&ads->tcptimeout,TCPIDLEMS);
249 }
250 case server_connecting: /* fall through */
4fad263d 251 if (!act || !timercmp(&now,&ads->tcptimeout,>)) {
f7f83b4a 252 inter_maxtoabs(tv_io,tvbuf,now,ads->tcptimeout);
253 return;
254 } {
255 /* TCP timeout has happened */
256 switch (ads->tcpstate) {
257 case server_connecting: /* failed to connect */
258 adns__tcp_broken(ads,"unable to make connection","timed out");
259 break;
260 case server_ok: /* idle timeout */
261 tcp_close(ads);
262 ads->tcpstate= server_disconnected;
263 return;
264 default:
265 abort();
266 }
267 }
268 break;
269 default:
270 abort();
271 }
272 }
4fad263d 273 return;
f7f83b4a 274}
275
276void adns__timeouts(adns_state ads, int act,
277 struct timeval **tv_io, struct timeval *tvbuf,
278 struct timeval now) {
279 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->udpw);
280 timeouts_queue(ads,act,tv_io,tvbuf,now, &ads->tcpw);
281 tcp_events(ads,act,tv_io,tvbuf,now);
282}
94436798 283
620c146d 284void adns_firsttimeout(adns_state ads,
285 struct timeval **tv_io, struct timeval *tvbuf,
286 struct timeval now) {
28de6442 287 adns__consistency(ads,0,cc_entex);
620c146d 288 adns__timeouts(ads, 0, tv_io,tvbuf, now);
28de6442 289 adns__consistency(ads,0,cc_entex);
620c146d 290}
291
292void adns_processtimeouts(adns_state ads, const struct timeval *now) {
293 struct timeval tv_buf;
294
28de6442 295 adns__consistency(ads,0,cc_entex);
3e2e5fab 296 adns__must_gettimeofday(ads,&now,&tv_buf);
297 if (now) adns__timeouts(ads, 1, 0,0, *now);
28de6442 298 adns__consistency(ads,0,cc_entex);
a8768e80 299}
300
620c146d 301/* fd handling functions. These are the top-level of the real work of
302 * reception and often transmission.
303 */
304
305int adns__pollfds(adns_state ads, struct pollfd pollfds_buf[MAX_POLLFDS]) {
306 /* Returns the number of entries filled in. Always zeroes revents. */
307
308 assert(MAX_POLLFDS==2);
a8768e80 309
620c146d 310 pollfds_buf[0].fd= ads->udpsocket;
311 pollfds_buf[0].events= POLLIN;
312 pollfds_buf[0].revents= 0;
4353a5c4 313
314 switch (ads->tcpstate) {
315 case server_disconnected:
6c68a593 316 case server_broken:
620c146d 317 return 1;
4353a5c4 318 case server_connecting:
620c146d 319 pollfds_buf[1].events= POLLOUT;
4353a5c4 320 break;
321 case server_ok:
609133ee 322 pollfds_buf[1].events=
323 ads->tcpsend.used ? POLLIN|POLLOUT|POLLPRI : POLLIN|POLLPRI;
e062dcae 324 break;
4353a5c4 325 default:
326 abort();
327 }
620c146d 328 pollfds_buf[1].fd= ads->tcpsocket;
620c146d 329 return 2;
4353a5c4 330}
331
620c146d 332int adns_processreadable(adns_state ads, int fd, const struct timeval *now) {
70ad7a2a 333 int want, dgramlen, r, udpaddrlen, serv, old_skip;
98a3f706 334 byte udpbuf[DNS_MAXUDP];
37e28fde 335 struct sockaddr_in udpaddr;
620c146d 336
28de6442 337 adns__consistency(ads,0,cc_entex);
3e2e5fab 338
ddfda861 339 switch (ads->tcpstate) {
340 case server_disconnected:
fb901bf5 341 case server_broken:
ddfda861 342 case server_connecting:
ddfda861 343 break;
344 case server_ok:
620c146d 345 if (fd != ads->tcpsocket) break;
70ad7a2a 346 assert(!ads->tcprecv_skip);
428bd682 347 do {
70ad7a2a 348 if (ads->tcprecv.used >= ads->tcprecv_skip+2) {
349 dgramlen= ((ads->tcprecv.buf[ads->tcprecv_skip]<<8) |
350 ads->tcprecv.buf[ads->tcprecv_skip+1]);
351 if (ads->tcprecv.used >= ads->tcprecv_skip+2+dgramlen) {
352 old_skip= ads->tcprecv_skip;
353 ads->tcprecv_skip += 2+dgramlen;
354 adns__procdgram(ads, ads->tcprecv.buf+old_skip+2,
355 dgramlen, ads->tcpserver, 1,*now);
356 continue;
656b2da9 357 } else {
70ad7a2a 358 want= 2+dgramlen;
656b2da9 359 }
70ad7a2a 360 } else {
361 want= 2;
656b2da9 362 }
70ad7a2a 363 ads->tcprecv.used -= ads->tcprecv_skip;
609133ee 364 memmove(ads->tcprecv.buf, ads->tcprecv.buf+ads->tcprecv_skip,
365 ads->tcprecv.used);
70ad7a2a 366 ads->tcprecv_skip= 0;
3e2e5fab 367 if (!adns__vbuf_ensure(&ads->tcprecv,want)) { r= ENOMEM; goto xit; }
620c146d 368 assert(ads->tcprecv.used <= ads->tcprecv.avail);
369 if (ads->tcprecv.used == ads->tcprecv.avail) continue;
370 r= read(ads->tcpsocket,
371 ads->tcprecv.buf+ads->tcprecv.used,
372 ads->tcprecv.avail-ads->tcprecv.used);
373 if (r>0) {
374 ads->tcprecv.used+= r;
375 } else {
376 if (r) {
3e2e5fab 377 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
620c146d 378 if (errno==EINTR) continue;
3e2e5fab 379 if (errno_resources(errno)) { r= errno; goto xit; }
656b2da9 380 }
620c146d 381 adns__tcp_broken(ads,"read",r?strerror(errno):"closed");
656b2da9 382 }
428bd682 383 } while (ads->tcpstate == server_ok);
384 r= 0; goto xit;
ddfda861 385 default:
386 abort();
656b2da9 387 }
620c146d 388 if (fd == ads->udpsocket) {
37e28fde 389 for (;;) {
390 udpaddrlen= sizeof(udpaddr);
eec1d9c8 391 r= recvfrom(ads->udpsocket,udpbuf,sizeof(udpbuf),0,
392 (struct sockaddr*)&udpaddr,&udpaddrlen);
37e28fde 393 if (r<0) {
3e2e5fab 394 if (errno == EAGAIN || errno == EWOULDBLOCK) { r= 0; goto xit; }
620c146d 395 if (errno == EINTR) continue;
3e2e5fab 396 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 397 adns__warn(ads,-1,0,"datagram receive error: %s",strerror(errno));
3e2e5fab 398 r= 0; goto xit;
37e28fde 399 }
400 if (udpaddrlen != sizeof(udpaddr)) {
68442019 401 adns__diag(ads,-1,0,"datagram received with wrong address length %d"
5a0be244 402 " (expected %lu)", udpaddrlen,
403 (unsigned long)sizeof(udpaddr));
37e28fde 404 continue;
405 }
406 if (udpaddr.sin_family != AF_INET) {
68442019 407 adns__diag(ads,-1,0,"datagram received with wrong protocol family"
4353a5c4 408 " %u (expected %u)",udpaddr.sin_family,AF_INET);
37e28fde 409 continue;
410 }
98a3f706 411 if (ntohs(udpaddr.sin_port) != DNS_PORT) {
609133ee 412 adns__diag(ads,-1,0,"datagram received from wrong port"
413 " %u (expected %u)", ntohs(udpaddr.sin_port),DNS_PORT);
37e28fde 414 continue;
415 }
416 for (serv= 0;
417 serv < ads->nservers &&
c0ec02b5
MW
418 (assert(ads->servers[serv].addr.sa.sa_family==AF_INET),
419 ads->servers[serv].addr.inet.sin_addr.s_addr !=
420 udpaddr.sin_addr.s_addr);
37e28fde 421 serv++);
422 if (serv >= ads->nservers) {
68442019 423 adns__warn(ads,-1,0,"datagram received from unknown nameserver %s",
4353a5c4 424 inet_ntoa(udpaddr.sin_addr));
37e28fde 425 continue;
426 }
ebf4877a 427 adns__procdgram(ads,udpbuf,r,serv,0,*now);
656b2da9 428 }
37e28fde 429 }
3e2e5fab 430 r= 0;
431xit:
28de6442 432 adns__consistency(ads,0,cc_entex);
3e2e5fab 433 return r;
656b2da9 434}
656b2da9 435
620c146d 436int adns_processwriteable(adns_state ads, int fd, const struct timeval *now) {
656b2da9 437 int r;
620c146d 438
28de6442 439 adns__consistency(ads,0,cc_entex);
3e2e5fab 440
620c146d 441 switch (ads->tcpstate) {
442 case server_disconnected:
fb901bf5 443 case server_broken:
620c146d 444 break;
445 case server_connecting:
446 if (fd != ads->tcpsocket) break;
447 assert(ads->tcprecv.used==0);
70ad7a2a 448 assert(ads->tcprecv_skip==0);
620c146d 449 for (;;) {
3e2e5fab 450 if (!adns__vbuf_ensure(&ads->tcprecv,1)) { r= ENOMEM; goto xit; }
620c146d 451 r= read(ads->tcpsocket,&ads->tcprecv.buf,1);
452 if (r==0 || (r<0 && (errno==EAGAIN || errno==EWOULDBLOCK))) {
453 tcp_connected(ads,*now);
3e2e5fab 454 r= 0; goto xit;
620c146d 455 }
456 if (r>0) {
457 adns__tcp_broken(ads,"connect/read","sent data before first request");
3e2e5fab 458 r= 0; goto xit;
620c146d 459 }
460 if (errno==EINTR) continue;
3e2e5fab 461 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 462 adns__tcp_broken(ads,"connect/read",strerror(errno));
3e2e5fab 463 r= 0; goto xit;
620c146d 464 } /* not reached */
465 case server_ok:
fb901bf5 466 if (fd != ads->tcpsocket) break;
467 while (ads->tcpsend.used) {
620c146d 468 adns__sigpipe_protect(ads);
469 r= write(ads->tcpsocket,ads->tcpsend.buf,ads->tcpsend.used);
470 adns__sigpipe_unprotect(ads);
471 if (r<0) {
472 if (errno==EINTR) continue;
3e2e5fab 473 if (errno==EAGAIN || errno==EWOULDBLOCK) { r= 0; goto xit; }
474 if (errno_resources(errno)) { r= errno; goto xit; }
620c146d 475 adns__tcp_broken(ads,"write",strerror(errno));
3e2e5fab 476 r= 0; goto xit;
620c146d 477 } else if (r>0) {
478 ads->tcpsend.used -= r;
479 memmove(ads->tcpsend.buf,ads->tcpsend.buf+r,ads->tcpsend.used);
480 }
fb901bf5 481 }
482 r= 0;
483 goto xit;
620c146d 484 default:
485 abort();
486 }
3e2e5fab 487 r= 0;
488xit:
28de6442 489 adns__consistency(ads,0,cc_entex);
3e2e5fab 490 return r;
620c146d 491}
492
609133ee 493int adns_processexceptional(adns_state ads, int fd,
494 const struct timeval *now) {
28de6442 495 adns__consistency(ads,0,cc_entex);
620c146d 496 switch (ads->tcpstate) {
497 case server_disconnected:
fb901bf5 498 case server_broken:
620c146d 499 break;
500 case server_connecting:
501 case server_ok:
502 if (fd != ads->tcpsocket) break;
503 adns__tcp_broken(ads,"poll/select","exceptional condition detected");
3e2e5fab 504 break;
620c146d 505 default:
506 abort();
507 }
28de6442 508 adns__consistency(ads,0,cc_entex);
620c146d 509 return 0;
510}
37e28fde 511
620c146d 512static void fd_event(adns_state ads, int fd,
513 int revent, int pollflag,
514 int maxfd, const fd_set *fds,
609133ee 515 int (*func)(adns_state, int fd,
516 const struct timeval *now),
620c146d 517 struct timeval now, int *r_r) {
656b2da9 518 int r;
620c146d 519
520 if (!(revent & pollflag)) return;
521 if (fds && !(fd<maxfd && FD_ISSET(fd,fds))) return;
522 r= func(ads,fd,&now);
523 if (r) {
524 if (r_r) {
525 *r_r= r;
526 } else {
609133ee 527 adns__diag(ads,-1,0,"process fd failed after select:"
528 " %s",strerror(errno));
620c146d 529 adns_globalsystemfailure(ads);
530 }
531 }
532}
533
534void adns__fdevents(adns_state ads,
535 const struct pollfd *pollfds, int npollfds,
536 int maxfd, const fd_set *readfds,
537 const fd_set *writefds, const fd_set *exceptfds,
538 struct timeval now, int *r_r) {
539 int i, fd, revents;
540
541 for (i=0; i<npollfds; i++) {
542 fd= pollfds[i].fd;
543 if (fd >= maxfd) maxfd= fd+1;
544 revents= pollfds[i].revents;
609133ee 545#define EV(pollfl,fds,how) \
546 fd_event(ads,fd, revents,pollfl, maxfd,fds, adns_process##how,now,r_r)
547 EV( POLLIN, readfds, readable );
548 EV( POLLOUT, writefds, writeable );
549 EV( POLLPRI, exceptfds, exceptional );
550#undef EV
620c146d 551 }
552}
37e28fde 553
620c146d 554/* Wrappers for select(2). */
555
556void adns_beforeselect(adns_state ads, int *maxfd_io, fd_set *readfds_io,
557 fd_set *writefds_io, fd_set *exceptfds_io,
558 struct timeval **tv_mod, struct timeval *tv_tobuf,
559 const struct timeval *now) {
560 struct timeval tv_nowbuf;
561 struct pollfd pollfds[MAX_POLLFDS];
562 int i, fd, maxfd, npollfds;
563
28de6442 564 adns__consistency(ads,0,cc_entex);
3e2e5fab 565
620c146d 566 if (tv_mod && (!*tv_mod || (*tv_mod)->tv_sec || (*tv_mod)->tv_usec)) {
567 /* The caller is planning to sleep. */
568 adns__must_gettimeofday(ads,&now,&tv_nowbuf);
26cc07a3 569 if (!now) { inter_immed(tv_mod,tv_tobuf); goto xit; }
6c68a593 570 adns__timeouts(ads, 0, tv_mod,tv_tobuf, *now);
620c146d 571 }
572
573 npollfds= adns__pollfds(ads,pollfds);
574 maxfd= *maxfd_io;
575 for (i=0; i<npollfds; i++) {
576 fd= pollfds[i].fd;
577 if (fd >= maxfd) maxfd= fd+1;
578 if (pollfds[i].events & POLLIN) FD_SET(fd,readfds_io);
579 if (pollfds[i].events & POLLOUT) FD_SET(fd,writefds_io);
580 if (pollfds[i].events & POLLPRI) FD_SET(fd,exceptfds_io);
581 }
582 *maxfd_io= maxfd;
3e2e5fab 583
584xit:
28de6442 585 adns__consistency(ads,0,cc_entex);
620c146d 586}
587
588void adns_afterselect(adns_state ads, int maxfd, const fd_set *readfds,
589 const fd_set *writefds, const fd_set *exceptfds,
590 const struct timeval *now) {
fc6a52ae 591 struct timeval tv_buf;
620c146d 592 struct pollfd pollfds[MAX_POLLFDS];
fc6a52ae 593 int npollfds, i;
620c146d 594
28de6442 595 adns__consistency(ads,0,cc_entex);
fc6a52ae 596 adns__must_gettimeofday(ads,&now,&tv_buf);
3e2e5fab 597 if (!now) goto xit;
620c146d 598 adns_processtimeouts(ads,now);
599
600 npollfds= adns__pollfds(ads,pollfds);
fc6a52ae 601 for (i=0; i<npollfds; i++) pollfds[i].revents= POLLIN|POLLOUT|POLLPRI;
620c146d 602 adns__fdevents(ads,
603 pollfds,npollfds,
604 maxfd,readfds,writefds,exceptfds,
605 *now, 0);
3e2e5fab 606xit:
28de6442 607 adns__consistency(ads,0,cc_entex);
620c146d 608}
609
610/* General helpful functions. */
611
612void adns_globalsystemfailure(adns_state ads) {
28de6442 613 adns__consistency(ads,0,cc_entex);
3e2e5fab 614
f7f83b4a 615 while (ads->udpw.head) adns__query_fail(ads->udpw.head, adns_s_systemfail);
616 while (ads->tcpw.head) adns__query_fail(ads->tcpw.head, adns_s_systemfail);
620c146d 617
618 switch (ads->tcpstate) {
619 case server_connecting:
620 case server_ok:
f7f83b4a 621 adns__tcp_broken(ads,0,0);
620c146d 622 break;
623 case server_disconnected:
fb901bf5 624 case server_broken:
620c146d 625 break;
626 default:
627 abort();
628 }
28de6442 629 adns__consistency(ads,0,cc_entex);
656b2da9 630}
631
620c146d 632int adns_processany(adns_state ads) {
aedf10f2 633 int r, i;
620c146d 634 struct timeval now;
635 struct pollfd pollfds[MAX_POLLFDS];
636 int npollfds;
637
28de6442 638 adns__consistency(ads,0,cc_entex);
3e2e5fab 639
620c146d 640 r= gettimeofday(&now,0);
641 if (!r) adns_processtimeouts(ads,&now);
642
25913ee2 643 /* We just use adns__fdevents to loop over the fd's trying them.
644 * This seems more sensible than calling select, since we're most
645 * likely just to want to do a read on one or two fds anyway.
646 */
620c146d 647 npollfds= adns__pollfds(ads,pollfds);
1ed3dba4 648 for (i=0; i<npollfds; i++) pollfds[i].revents= pollfds[i].events & ~POLLPRI;
620c146d 649 adns__fdevents(ads,
650 pollfds,npollfds,
651 0,0,0,0,
652 now,&r);
3e2e5fab 653
28de6442 654 adns__consistency(ads,0,cc_entex);
c118a725 655 return 0;
620c146d 656}
ddfda861 657
4353a5c4 658void adns__autosys(adns_state ads, struct timeval now) {
ddfda861 659 if (ads->iflags & adns_if_noautosys) return;
620c146d 660 adns_processany(ads);
ddfda861 661}
662
940356bd 663int adns__internal_check(adns_state ads,
664 adns_query *query_io,
665 adns_answer **answer,
666 void **context_r) {
656b2da9 667 adns_query qu;
668
669 qu= *query_io;
670 if (!qu) {
4ae90a83 671 if (ads->output.head) {
672 qu= ads->output.head;
f7f83b4a 673 } else if (ads->udpw.head || ads->tcpw.head) {
4ae90a83 674 return EAGAIN;
675 } else {
676 return ESRCH;
677 }
656b2da9 678 } else {
226c5eef 679 if (qu->id>=0) return EAGAIN;
656b2da9 680 }
681 LIST_UNLINK(ads->output,qu);
8e5b0abb 682 *answer= qu->answer;
a6536d8b 683 if (context_r) *context_r= qu->ctx.ext;
8ce38e76 684 *query_io= qu;
656b2da9 685 free(qu);
686 return 0;
687}
688
689int adns_wait(adns_state ads,
690 adns_query *query_io,
691 adns_answer **answer_r,
692 void **context_r) {
620c146d 693 int r, maxfd, rsel;
656b2da9 694 fd_set readfds, writefds, exceptfds;
26cc07a3 695 struct timeval tvbuf, *tvp;
656b2da9 696
28de6442 697 adns__consistency(ads,*query_io,cc_entex);
656b2da9 698 for (;;) {
940356bd 699 r= adns__internal_check(ads,query_io,answer_r,context_r);
226c5eef 700 if (r != EAGAIN) break;
656b2da9 701 maxfd= 0; tvp= 0;
37e28fde 702 FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
620c146d 703 adns_beforeselect(ads,&maxfd,&readfds,&writefds,&exceptfds,&tvp,&tvbuf,0);
4fad263d 704 assert(tvp);
656b2da9 705 rsel= select(maxfd,&readfds,&writefds,&exceptfds,tvp);
88372443 706 if (rsel==-1) {
620c146d 707 if (errno == EINTR) {
3e2e5fab 708 if (ads->iflags & adns_if_eintr) { r= EINTR; break; }
620c146d 709 } else {
710 adns__diag(ads,-1,0,"select failed in wait: %s",strerror(errno));
711 adns_globalsystemfailure(ads);
712 }
713 } else {
714 assert(rsel >= 0);
715 adns_afterselect(ads,maxfd,&readfds,&writefds,&exceptfds,0);
88372443 716 }
656b2da9 717 }
28de6442 718 adns__consistency(ads,0,cc_entex);
3e2e5fab 719 return r;
656b2da9 720}
721
722int adns_check(adns_state ads,
723 adns_query *query_io,
724 adns_answer **answer_r,
725 void **context_r) {
4353a5c4 726 struct timeval now;
727 int r;
728
28de6442 729 adns__consistency(ads,*query_io,cc_entex);
620c146d 730 r= gettimeofday(&now,0);
731 if (!r) adns__autosys(ads,now);
3e2e5fab 732
940356bd 733 r= adns__internal_check(ads,query_io,answer_r,context_r);
28de6442 734 adns__consistency(ads,0,cc_entex);
3e2e5fab 735 return r;
656b2da9 736}