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